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 |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 13 | * |
| 14 | * @section TRP_DESIGN Design |
| 15 | * |
| 16 | * @code |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 17 | * +---------+ +---------+ +---------+ |
| 18 | * output | trp | | trb | | tro | |
| 19 | * <---+ Print +<---+ Browse +<-->+ Obtain | |
| 20 | * | | | | | | |
| 21 | * +---------+ +----+----+ +---------+ |
| 22 | * ^ |
| 23 | * | |
| 24 | * +----+----+ |
| 25 | * | trm | |
| 26 | * | Manager | |
| 27 | * | | |
| 28 | * +----+----+ |
| 29 | * ^ |
| 30 | * | input |
| 31 | * + |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 32 | * @endcode |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 33 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 34 | * @subsection TRP_GLOSSARY Glossary |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 35 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 36 | * @subsubsection TRP_trm trm |
| 37 | * Manager functions are at the peak of abstraction. They are |
| 38 | * able to print individual sections of the YANG tree diagram |
| 39 | * (eg module, notifications, rpcs ...) and they call |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 40 | * Browse functions (@ref TRP_trb). |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 41 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 42 | * @subsubsection TRP_trb trb |
| 43 | * Browse functions contain a general algorithm (Preorder DFS) |
| 44 | * for traversing the tree. It does not matter what data type |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 45 | * the tree contains (@ref lysc_node or @ref lysp_node), because it |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 46 | * requires a ready-made getter functions for traversing the tree |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 47 | * (@ref trt_fp_all) and transformation function to its own node |
| 48 | * data type (@ref trt_node). These getter functions are generally |
| 49 | * referred to as @ref TRP_tro. Browse functions can repeatedly |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 50 | * traverse nodes in the tree, for example, to calculate the alignment |
| 51 | * gap before the nodes \<type\> in the YANG Tree Diagram. |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 52 | * The obtained @ref trt_node is passed to the @ref TRP_trp functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 53 | * to print the Tree diagram. |
| 54 | * |
| 55 | * @subsubsection TRP_tro tro |
| 56 | * Functions that provide an extra wrapper for the libyang library. |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 57 | * The Obtain functions are further specialized according to whether |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 58 | * they operate on lysp_tree (@ref TRP_trop) or lysc_tree |
| 59 | * (@ref TRP_troc). If they are general algorithms, then they have the |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 60 | * prefix \b tro_. The Obtain functions provide information to |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 61 | * @ref TRP_trb functions for printing the Tree diagram. |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 62 | * |
| 63 | * @subsubsection TRP_trop trop |
| 64 | * Functions for Obtaining information from Parsed schema tree. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 65 | * |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 66 | * @subsubsection TRP_troc troc |
| 67 | * Functions for Obtaining information from Compiled schema tree. |
| 68 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 69 | * @subsubsection TRP_trp trp |
| 70 | * Print functions take care of the printing YANG diagram. They can |
| 71 | * also split one node into multiple lines if the node does not fit |
| 72 | * on one line. |
| 73 | * |
| 74 | * @subsubsection TRP_trt trt |
| 75 | * Data type marking in the printer_tree module. |
| 76 | * |
| 77 | * @subsubsection TRP_trg trg |
| 78 | * General functions. |
| 79 | * |
| 80 | * @subsection TRP_ADJUSTMENTS Adjustments |
| 81 | * It is assumed that the changes are likely to take place mainly for |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 82 | * @ref TRP_tro, @ref TRP_trop or @ref TRP_troc functions because |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 83 | * they are the only ones dependent on libyang implementation. |
| 84 | * In special cases, changes will also need to be made to the |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 85 | * @ref TRP_trp functions if a special algorithm is needed to print |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 86 | * (right now this is prepared for printing list's keys |
| 87 | * and if-features). |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 88 | */ |
| 89 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 90 | #include <assert.h> |
| 91 | #include <string.h> |
| 92 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 93 | #include "compat.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 94 | #include "ly_common.h" |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 95 | #include "out_internal.h" |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 96 | #include "plugins_exts.h" |
| 97 | #include "plugins_types.h" |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 98 | #include "printer_internal.h" |
aPiecek | 704f8e9 | 2021-08-25 13:35:05 +0200 | [diff] [blame] | 99 | #include "printer_schema.h" |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 100 | #include "tree_schema_internal.h" |
| 101 | #include "xpath.h" |
| 102 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 103 | /** |
| 104 | * @brief List of available actions. |
| 105 | */ |
| 106 | typedef enum { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 107 | TRD_PRINT = 0, /**< Normal behavior. It just prints. */ |
| 108 | TRD_CHAR_COUNT /**< Characters will be counted instead of printing. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 109 | } trt_ly_out_clb_arg_flag; |
| 110 | |
| 111 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 112 | * @brief Structure is passed as 'writeclb' argument |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 113 | * to the ::ly_out_new_clb(). |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 114 | */ |
| 115 | struct ly_out_clb_arg { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 116 | trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */ |
| 117 | struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */ |
| 118 | size_t counter; /**< Counter of printed characters. */ |
| 119 | LY_ERR last_error; /**< The last error that occurred. If no error has occurred, it will be ::LY_SUCCESS. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /** |
| 123 | * @brief Initialize struct ly_out_clb_arg with default settings. |
| 124 | */ |
| 125 | #define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 126 | (struct ly_out_clb_arg) { \ |
| 127 | .mode = MODE, .out = OUT, \ |
| 128 | .counter = COUNTER, .last_error = LAST_ERROR \ |
| 129 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 130 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 131 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 132 | * Print getters |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 133 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * @brief Callback functions that prints special cases. |
| 137 | * |
| 138 | * It just groups together tree context with trt_fp_print. |
| 139 | */ |
| 140 | struct trt_cf_print { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 141 | const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */ |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 142 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 143 | void (*pf)(const struct trt_tree_ctx *, struct ly_out *); /**< Pointing to function which printing list's keys or features. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * @brief Callback functions for printing special cases. |
| 148 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 149 | * Functions with the suffix 'trp' can print most of the text on |
| 150 | * output, just by setting the pointer to the string. But in some |
| 151 | * cases, it's not that simple, because its entire string is fragmented |
| 152 | * in memory. For example, for printing list's keys or if-features. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 153 | * However, this depends on how the libyang library is implemented. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 154 | * This implementation of the printer_tree module goes through |
| 155 | * a lysp tree, but if it goes through a lysc tree, these special cases |
| 156 | * would be different. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 157 | * Functions must print including spaces or delimiters between names. |
| 158 | */ |
| 159 | struct trt_fp_print { |
| 160 | void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */ |
| 161 | void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */ |
| 162 | }; |
| 163 | |
| 164 | /** |
| 165 | * @brief Package which only groups getter function. |
| 166 | */ |
| 167 | struct trt_pck_print { |
| 168 | const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */ |
| 169 | struct trt_fp_print fps; /**< Print function. */ |
| 170 | }; |
| 171 | |
| 172 | /** |
| 173 | * @brief Initialize struct trt_pck_print by parameters. |
| 174 | */ |
| 175 | #define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 176 | (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT} |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 177 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 178 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 179 | * Indent |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 180 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 181 | |
| 182 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 183 | * @brief Constants which are defined in the RFC or are observable |
| 184 | * from the pyang tool. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 185 | */ |
| 186 | typedef enum { |
| 187 | TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */ |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 188 | TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with |
| 189 | a whitespace offset of at least two characters. */ |
| 190 | TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */ |
| 191 | TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */ |
| 192 | TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */ |
| 193 | TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 194 | TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */ |
| 195 | } trt_cnf_indent; |
| 196 | |
| 197 | /** |
| 198 | * @brief Type of indent in node. |
| 199 | */ |
| 200 | typedef enum { |
| 201 | TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */ |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 202 | TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 203 | TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */ |
| 204 | } trt_indent_in_node_type; |
| 205 | |
| 206 | /** Constant to indicate the need to break a line. */ |
| 207 | #define TRD_LINEBREAK -1 |
| 208 | |
| 209 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 210 | * @brief Records the alignment between the individual |
| 211 | * elements of the node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 212 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 213 | * @see trp_default_indent_in_node, trp_try_normal_indent_in_node |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 214 | */ |
| 215 | struct trt_indent_in_node { |
| 216 | trt_indent_in_node_type type; /**< Type of indent in node. */ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 217 | int16_t btw_name_opts; /**< Indent between node name and \<opts\>. */ |
| 218 | int16_t btw_opts_type; /**< Indent between \<opts\> and \<type\>. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 219 | int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */ |
| 220 | }; |
| 221 | |
| 222 | /** |
| 223 | * @brief Type of wrappers to be printed. |
| 224 | */ |
| 225 | typedef enum { |
| 226 | TRD_WRAPPER_TOP = 0, /**< Related to the module. */ |
| 227 | TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */ |
| 228 | } trd_wrapper_type; |
| 229 | |
| 230 | /** |
| 231 | * @brief For resolving sibling symbol ('|') placement. |
| 232 | * |
| 233 | * Bit indicates where the sibling symbol must be printed. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 234 | * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 235 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 236 | * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY, |
| 237 | * trp_wrapper_set_mark, trp_wrapper_set_shift, |
| 238 | * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 239 | */ |
| 240 | struct trt_wrapper { |
| 241 | trd_wrapper_type type; /**< Location of the wrapper. */ |
| 242 | uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed. |
| 243 | It follows that the maximum immersion of the printable node is 64. */ |
| 244 | uint32_t actual_pos; /**< Actual position in bit_marks. */ |
| 245 | }; |
| 246 | |
| 247 | /** |
| 248 | * @brief Get wrapper related to the module section. |
| 249 | * |
| 250 | * @code |
| 251 | * module: <module-name> |
| 252 | * +--<node> |
| 253 | * | |
| 254 | * @endcode |
| 255 | */ |
| 256 | #define TRP_INIT_WRAPPER_TOP \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 257 | (struct trt_wrapper) { \ |
| 258 | .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \ |
| 259 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 260 | |
| 261 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 262 | * @brief Get wrapper related to subsection |
| 263 | * e.g. Augmenations or Groupings. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 264 | * |
| 265 | * @code |
| 266 | * module: <module-name> |
| 267 | * +--<node> |
| 268 | * |
| 269 | * augment <target-node>: |
| 270 | * +--<node> |
| 271 | * @endcode |
| 272 | */ |
| 273 | #define TRP_INIT_WRAPPER_BODY \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 274 | (struct trt_wrapper) { \ |
| 275 | .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \ |
| 276 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 277 | |
| 278 | /** |
| 279 | * @brief Package which only groups wrapper and indent in node. |
| 280 | */ |
| 281 | struct trt_pck_indent { |
| 282 | struct trt_wrapper wrapper; /**< Coded " | | " sequence. */ |
| 283 | struct trt_indent_in_node in_node; /**< Indent in node. */ |
| 284 | }; |
| 285 | |
| 286 | /** |
| 287 | * @brief Initialize struct trt_pck_indent by parameters. |
| 288 | */ |
| 289 | #define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 290 | (struct trt_pck_indent){ \ |
| 291 | .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \ |
| 292 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 293 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 294 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 295 | * flags |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 296 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 297 | |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 298 | #define TRD_FLAGS_TYPE_EMPTY "--" |
| 299 | #define TRD_FLAGS_TYPE_RW "rw" |
| 300 | #define TRD_FLAGS_TYPE_RO "ro" |
| 301 | #define TRD_FLAGS_TYPE_RPC_INPUT_PARAMS "-w" |
| 302 | #define TRD_FLAGS_TYPE_USES_OF_GROUPING "-u" |
| 303 | #define TRD_FLAGS_TYPE_RPC "-x" |
| 304 | #define TRD_FLAGS_TYPE_NOTIF "-n" |
| 305 | #define TRD_FLAGS_TYPE_MOUNT_POINT "mp" |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 306 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 307 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 308 | * node_name and opts |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 309 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 310 | |
| 311 | #define TRD_NODE_NAME_PREFIX_CHOICE "(" |
| 312 | #define TRD_NODE_NAME_PREFIX_CASE ":(" |
| 313 | #define TRD_NODE_NAME_TRIPLE_DOT "..." |
| 314 | |
| 315 | /** |
| 316 | * @brief Type of the node. |
| 317 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 318 | * Used mainly to complete the correct \<opts\> next to or |
| 319 | * around the \<name\>. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 320 | */ |
| 321 | typedef enum { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 322 | TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */ |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 323 | TRD_NODE_CASE, /**< For case node. :(\<name\>) */ |
| 324 | TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 325 | TRD_NODE_TRIPLE_DOT /**< For collapsed sibling nodes and their children. Special case which doesn't belong here very well. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 326 | } trt_node_type; |
| 327 | |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 328 | #define TRD_NODE_OPTIONAL "?" /**< For an optional leaf, anydata, or anyxml. \<name\>? */ |
| 329 | #define TRD_NODE_CONTAINER "!" /**< For a presence container. \<name\>! */ |
| 330 | #define TRD_NODE_LISTLEAFLIST "*" /**< For a leaf-list or list. \<name\>* */ |
| 331 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 332 | /** |
| 333 | * @brief Type of node and his name. |
| 334 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 335 | * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY, |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 336 | * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys |
| 337 | */ |
| 338 | struct trt_node_name { |
| 339 | trt_node_type type; /**< Type of the node relevant for printing. */ |
aPiecek | bca5777 | 2022-10-13 13:51:59 +0200 | [diff] [blame] | 340 | ly_bool keys; /**< Set to 1 if [\<keys\>] are to be printed. Valid for some types only. */ |
aPiecek | 34fa377 | 2021-05-21 12:35:46 +0200 | [diff] [blame] | 341 | const char *module_prefix; /**< If the node is augmented into the tree from another module, |
| 342 | so this is the prefix of that module. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 343 | const char *str; /**< Name of the node. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 344 | const char *add_opts; /**< Additional opts symbol from plugin. */ |
| 345 | const char *opts; /**< The \<opts\> symbol. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | /** |
| 349 | * @brief Create struct trt_node_name as empty. |
| 350 | */ |
| 351 | #define TRP_EMPTY_NODE_NAME \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 352 | (struct trt_node_name) { \ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 353 | .type = TRD_NODE_ELSE, .keys = 0, .module_prefix = NULL, .str = NULL, .opts = NULL, .add_opts = NULL \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 354 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 355 | |
| 356 | /** |
| 357 | * @brief Check if struct trt_node_name is empty. |
| 358 | */ |
| 359 | #define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \ |
| 360 | !NODE_NAME.str |
| 361 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 362 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 363 | * type |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 364 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 365 | |
| 366 | /** |
| 367 | * @brief Type of the \<type\> |
| 368 | */ |
| 369 | typedef enum { |
| 370 | TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */ |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 371 | TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */ |
| 372 | TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 373 | So set type as ::TRD_TYPE_TARGET. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 374 | TRD_TYPE_EMPTY /**< Type is not used at all. */ |
| 375 | } trt_type_type; |
| 376 | |
| 377 | /** |
| 378 | * @brief \<type\> in the \<node\>. |
| 379 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 380 | * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 381 | */ |
| 382 | struct trt_type { |
| 383 | trt_type_type type; /**< Type of the \<type\>. */ |
| 384 | const char *str; /**< Path or name of the type. */ |
| 385 | }; |
| 386 | |
| 387 | /** |
| 388 | * @brief Create empty struct trt_type. |
| 389 | */ |
| 390 | #define TRP_EMPTY_TRT_TYPE \ |
| 391 | (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL} |
| 392 | |
| 393 | /** |
| 394 | * @brief Check if struct trt_type is empty. |
| 395 | */ |
| 396 | #define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \ |
| 397 | TYPE_OF_TYPE.type == TRD_TYPE_EMPTY |
| 398 | |
| 399 | /** |
| 400 | * @brief Initialize struct trt_type by parameters. |
| 401 | */ |
| 402 | #define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \ |
| 403 | (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING} |
| 404 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 405 | /** |
| 406 | * @brief If-feature type. |
| 407 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 408 | typedef enum { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 409 | TRD_IFF_NON_PRESENT = 0, /**< iffeatures are not present. */ |
| 410 | TRD_IFF_PRESENT, /**< iffeatures are present and will be printed by |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 411 | trt_fp_print.print_features_names callback */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 412 | TRD_IFF_OVERR /**< iffeatures are override by plugin */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 413 | } trt_iffeatures_type; |
| 414 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 415 | /** |
| 416 | * @brief \<if-features\>. |
| 417 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 418 | struct trt_iffeatures { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 419 | trt_iffeatures_type type; /**< Type of iffeature. */ |
| 420 | char *str; /**< iffeatures string ready to print. Set if TRD_IFF_OVERR is set. */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 421 | }; |
| 422 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 423 | /** |
| 424 | * @brief Create empty iffeatures. |
| 425 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 426 | #define TRP_EMPTY_TRT_IFFEATURES \ |
| 427 | (struct trt_iffeatures) {.type = TRD_IFF_NON_PRESENT} |
| 428 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 429 | /** |
| 430 | * @brief Check if iffeatures is empty. |
| 431 | * |
| 432 | * @param[in] IFF_TYPE value from trt_iffeatures.type. |
| 433 | * @return 1 if is empty. |
| 434 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 435 | #define TRP_EMPTY_TRT_IFFEATURES_IS_EMPTY(IFF_TYPE) \ |
| 436 | (IFF_TYPE == TRD_IFF_NON_PRESENT) |
| 437 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 438 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 439 | * node |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 440 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * @brief \<node\> data for printing. |
| 444 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 445 | * It contains RFC's: |
| 446 | * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 447 | * Item \<opts\> is moved to part struct trt_node_name. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 448 | * For printing [\<keys\>] and if-features is required special |
| 449 | * functions which prints them. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 450 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 451 | * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty, |
| 452 | * trp_print_node_up_to_name, trp_print_divided_node_up_to_name, |
| 453 | * trp_print_node |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 454 | */ |
| 455 | struct trt_node { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 456 | const char *status; /**< \<status\>. */ |
| 457 | const char *flags; /**< \<flags\>. */ |
aPiecek | 7ed8d03 | 2022-10-10 12:32:27 +0200 | [diff] [blame] | 458 | struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */ |
| 459 | struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 460 | struct trt_iffeatures iffeatures; /**< \<if-features\>. */ |
aPiecek | 7ed8d03 | 2022-10-10 12:32:27 +0200 | [diff] [blame] | 461 | ly_bool last_one; /**< Information about whether the node is the last. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 462 | }; |
| 463 | |
| 464 | /** |
| 465 | * @brief Create struct trt_node as empty. |
| 466 | */ |
| 467 | #define TRP_EMPTY_NODE \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 468 | (struct trt_node) { \ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 469 | .status = NULL, \ |
| 470 | .flags = NULL, \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 471 | .name = TRP_EMPTY_NODE_NAME, \ |
| 472 | .type = TRP_EMPTY_TRT_TYPE, \ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 473 | .iffeatures = TRP_EMPTY_TRT_IFFEATURES, \ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 474 | .last_one = 1 \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 475 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 476 | |
| 477 | /** |
| 478 | * @brief Package which only groups indent and node. |
| 479 | */ |
| 480 | struct trt_pair_indent_node { |
| 481 | struct trt_indent_in_node indent; |
| 482 | struct trt_node node; |
| 483 | }; |
| 484 | |
| 485 | /** |
| 486 | * @brief Initialize struct trt_pair_indent_node by parameters. |
| 487 | */ |
| 488 | #define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 489 | (struct trt_pair_indent_node) { \ |
| 490 | .indent = INDENT_IN_NODE, .node = NODE \ |
| 491 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 492 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 493 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 494 | * statement |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 495 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 496 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 497 | #define TRD_KEYWORD_MODULE "module" |
| 498 | #define TRD_KEYWORD_SUBMODULE "submodule" |
| 499 | #define TRD_KEYWORD_AUGMENT "augment" |
| 500 | #define TRD_KEYWORD_RPC "rpcs" |
| 501 | #define TRD_KEYWORD_NOTIF "notifications" |
| 502 | #define TRD_KEYWORD_GROUPING "grouping" |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 503 | |
| 504 | /** |
| 505 | * @brief Main sign of the tree nodes. |
| 506 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 507 | * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 508 | * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str, |
| 509 | * trt_print_keyword_stmt_end, trp_print_keyword_stmt |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 510 | */ |
| 511 | struct trt_keyword_stmt { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 512 | const char *section_name; /**< String containing section name. */ |
| 513 | const char *argument; /**< Name or path located begind section name. */ |
| 514 | ly_bool has_node; /**< Flag if section has any nodes. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | /** |
| 518 | * @brief Create struct trt_keyword_stmt as empty. |
| 519 | */ |
| 520 | #define TRP_EMPTY_KEYWORD_STMT \ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 521 | (struct trt_keyword_stmt) {.section_name = NULL, .argument = NULL, .has_node = 0} |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 522 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 523 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 524 | * Modify getters |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 525 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 526 | |
| 527 | struct trt_parent_cache; |
| 528 | |
| 529 | /** |
| 530 | * @brief Functions that change the state of the tree_ctx structure. |
| 531 | * |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 532 | * The 'trop' or 'troc' functions are set here, which provide data |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 533 | * for the 'trp' printing functions and are also called from the |
| 534 | * 'trb' browsing functions when walking through a tree. These callback |
| 535 | * functions need to be checked or reformulated if changes to the |
| 536 | * libyang library affect the printing tree. For all, if the value |
| 537 | * cannot be returned, its empty version obtained by relevant TRP_EMPTY |
| 538 | * macro is returned. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 539 | */ |
| 540 | struct trt_fp_modify_ctx { |
| 541 | ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 542 | struct trt_node (*first_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump on the first of the siblings. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 543 | struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */ |
| 544 | struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 545 | }; |
| 546 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 547 | /** |
| 548 | * @brief Create modify functions for compiled tree. |
| 549 | */ |
| 550 | #define TRP_TRT_FP_MODIFY_COMPILED \ |
| 551 | (struct trt_fp_modify_ctx) { \ |
| 552 | .parent = troc_modi_parent, \ |
| 553 | .first_sibling = troc_modi_first_sibling, \ |
| 554 | .next_sibling = troc_modi_next_sibling, \ |
| 555 | .next_child = troc_modi_next_child, \ |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @brief Create modify functions for parsed tree. |
| 560 | */ |
| 561 | #define TRP_TRT_FP_MODIFY_PARSED \ |
| 562 | (struct trt_fp_modify_ctx) { \ |
| 563 | .parent = trop_modi_parent, \ |
| 564 | .first_sibling = trop_modi_first_sibling, \ |
| 565 | .next_sibling = trop_modi_next_sibling, \ |
| 566 | .next_child = trop_modi_next_child, \ |
| 567 | } |
| 568 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 569 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 570 | * Read getters |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 571 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * @brief Functions that do not change the state of the tree_structure. |
| 575 | * |
| 576 | * For details see trt_fp_modify_ctx. |
| 577 | */ |
| 578 | struct trt_fp_read { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 579 | struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 580 | struct trt_node (*node)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Get current node. */ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 581 | ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 582 | ly_bool (*if_parent_exists)(const struct trt_tree_ctx *); /**< Check if node's parent exists. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 583 | }; |
| 584 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 585 | /** |
| 586 | * @brief Create read functions for compiled tree. |
| 587 | */ |
| 588 | #define TRP_TRT_FP_READ_COMPILED \ |
| 589 | (struct trt_fp_read) { \ |
| 590 | .module_name = tro_read_module_name, \ |
| 591 | .node = troc_read_node, \ |
| 592 | .if_sibling_exists = troc_read_if_sibling_exists, \ |
| 593 | .if_parent_exists = tro_read_if_sibling_exists \ |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * @brief Create read functions for parsed tree. |
| 598 | */ |
| 599 | #define TRP_TRT_FP_READ_PARSED \ |
| 600 | (struct trt_fp_read) { \ |
| 601 | .module_name = tro_read_module_name, \ |
| 602 | .node = trop_read_node, \ |
| 603 | .if_sibling_exists = trop_read_if_sibling_exists, \ |
| 604 | .if_parent_exists = tro_read_if_sibling_exists \ |
| 605 | } |
| 606 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 607 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 608 | * All getters |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 609 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 610 | |
| 611 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 612 | * @brief A set of all necessary functions that must be provided |
| 613 | * for the printer. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 614 | */ |
| 615 | struct trt_fp_all { |
| 616 | struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */ |
| 617 | struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */ |
| 618 | struct trt_fp_print print; /**< Functions pointers for printing special items in node. */ |
| 619 | }; |
| 620 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 621 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 622 | * Printer context |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 623 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 624 | |
| 625 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 626 | * @brief Main structure for @ref TRP_trp part. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 627 | */ |
| 628 | struct trt_printer_ctx { |
| 629 | struct ly_out *out; /**< Handler to printing. */ |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 630 | struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 631 | size_t max_line_length; /**< The maximum number of characters that can be |
| 632 | printed on one line, including the last. */ |
| 633 | }; |
| 634 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 635 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 636 | * Tro functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 637 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 638 | |
| 639 | /** |
| 640 | * @brief The name of the section to which the node belongs. |
| 641 | */ |
| 642 | typedef enum { |
| 643 | TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */ |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 644 | TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */ |
| 645 | TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */ |
| 646 | TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */ |
| 647 | TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 648 | TRD_SECT_PLUG_DATA /**< The node belongs to some plugin section. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 649 | } trt_actual_section; |
| 650 | |
| 651 | /** |
| 652 | * @brief Types of nodes that have some effect on their children. |
| 653 | */ |
| 654 | typedef enum { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 655 | TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */ |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 656 | TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */ |
| 657 | TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 658 | TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 659 | } trt_ancestor_type; |
| 660 | |
| 661 | /** |
| 662 | * @brief Saved information when browsing the tree downwards. |
| 663 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 664 | * This structure helps prevent frequent retrieval of information |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 665 | * from the tree. Functions @ref TRP_trb are designed to preserve |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 666 | * this structures during their recursive calls. This functions do not |
| 667 | * interfere in any way with this data. This structure |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 668 | * is used by @ref TRP_trop functions which, thanks to this |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 669 | * structure, can return a node with the correct data. The word |
| 670 | * \b parent is in the structure name, because this data refers to |
| 671 | * the last parent and at the same time the states of its |
| 672 | * ancestors data. Only the function jumping on the child |
| 673 | * (next_child(...)) creates this structure, because the pointer |
| 674 | * to the current node moves down the tree. It's like passing |
| 675 | * the genetic code to children. Some data must be inherited and |
| 676 | * there are two approaches to this problem. Either it will always |
| 677 | * be determined which inheritance states belong to the current node |
| 678 | * (which can lead to regular travel to the root node) or |
| 679 | * the inheritance states will be stored during the recursive calls. |
| 680 | * So the problem was solved by the second option. Why does |
| 681 | * the structure contain this data? Because it walks through |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 682 | * the lysp tree. For walks through the lysc tree is trt_parent_cache |
| 683 | * useless. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 684 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 685 | * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 686 | */ |
| 687 | struct trt_parent_cache { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 688 | trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */ |
| 689 | uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */ |
| 690 | uint16_t lys_config; /**< Inherited config W or R. */ |
| 691 | const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 692 | }; |
| 693 | |
| 694 | /** |
| 695 | * @brief Return trt_parent_cache filled with default values. |
| 696 | */ |
| 697 | #define TRP_EMPTY_PARENT_CACHE \ |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 698 | (struct trt_parent_cache) { \ |
| 699 | .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \ |
| 700 | .lys_config = LYS_CONFIG_W, .last_list = NULL \ |
| 701 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 702 | |
| 703 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 704 | * @brief Node override from plugin. |
| 705 | */ |
| 706 | struct lyplg_ext_sprinter_tree_node_override { |
| 707 | const char *flags; /**< Override for \<flags\>. */ |
| 708 | const char *add_opts; /**< Additional symbols for \<opts\>. */ |
| 709 | }; |
| 710 | |
| 711 | /** |
| 712 | * @brief Context for plugin extension. |
| 713 | */ |
| 714 | struct trt_plugin_ctx { |
| 715 | struct lyspr_tree_ctx *ctx; /**< Pointer to main context. */ |
| 716 | struct lyspr_tree_schema *schema; /**< Current schema to print. */ |
| 717 | ly_bool filtered; /**< Flag if current node is filtered. */ |
| 718 | struct lyplg_ext_sprinter_tree_node_override node_overr; /**< Current node override. */ |
| 719 | ly_bool last_schema; /**< Flag if schema is last. */ |
| 720 | ly_bool last_error; /**< Last error from plugin. */ |
| 721 | }; |
| 722 | |
| 723 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 724 | * @brief Main structure for browsing the libyang tree |
| 725 | */ |
| 726 | struct trt_tree_ctx { |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 727 | ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree. |
| 728 | It is assumed that once set, it does not change. |
| 729 | If it is true then trt_tree_ctx.pn and |
| 730 | trt_tree_ctx.tpn are not used. |
| 731 | If it is false then trt_tree_ctx.cn is not used. */ |
| 732 | trt_actual_section section; /**< To which section pn points. */ |
| 733 | const struct lysp_module *pmod; /**< Parsed YANG schema tree. */ |
| 734 | const struct lysc_module *cmod; /**< Compiled YANG schema tree. */ |
| 735 | const struct lysp_node *pn; /**< Actual pointer to parsed node. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 736 | const struct lysp_node *tpn; /**< Pointer to actual top-node. */ |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 737 | const struct lysc_node *cn; /**< Actual pointer to compiled node. */ |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 738 | LY_ERR last_error; /**< Error value during printing. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 739 | |
| 740 | struct trt_plugin_ctx plugin_ctx; /**< Context for plugin. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 741 | }; |
| 742 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 743 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 744 | * @brief Create empty node override. |
| 745 | */ |
| 746 | #define TRP_TREE_CTX_EMPTY_NODE_OVERR \ |
| 747 | (struct lyplg_ext_sprinter_tree_node_override) { \ |
| 748 | .flags = NULL, \ |
| 749 | .add_opts = NULL, \ |
| 750 | } |
| 751 | |
| 752 | /** |
aPiecek | bbc0293 | 2021-05-21 07:19:41 +0200 | [diff] [blame] | 753 | * @brief Check if lysp node is available from |
| 754 | * the current compiled node. |
| 755 | * |
| 756 | * Use only if trt_tree_ctx.lysc_tree is set to true. |
| 757 | */ |
| 758 | #define TRP_TREE_CTX_LYSP_NODE_PRESENT(CN) \ |
| 759 | (CN->priv) |
| 760 | |
| 761 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 762 | * @brief Get lysp_node from trt_tree_ctx.cn. |
aPiecek | bbc0293 | 2021-05-21 07:19:41 +0200 | [diff] [blame] | 763 | * |
| 764 | * Use only if :TRP_TREE_CTX_LYSP_NODE_PRESENT returns true |
| 765 | * for that node. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 766 | */ |
| 767 | #define TRP_TREE_CTX_GET_LYSP_NODE(CN) \ |
| 768 | ((const struct lysp_node *)CN->priv) |
| 769 | |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 770 | /** Getter function for ::trop_node_charptr(). */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 771 | typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn); |
| 772 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 773 | /** |
| 774 | * @brief Simple getter functions for lysp and lysc nodes. |
| 775 | * |
| 776 | * This structure is useful if we have a general algorithm |
| 777 | * (tro function) that can be used for both lysc and lysp nodes. |
| 778 | * Thanks to this structure, we prevent code redundancy. |
| 779 | * We don't have to write basically the same algorithm twice |
| 780 | * for lysp and lysc trees. |
| 781 | */ |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 782 | struct tro_getters { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 783 | uint16_t (*nodetype)(const void *); /**< Get nodetype. */ |
| 784 | const void *(*next)(const void *); /**< Get sibling. */ |
| 785 | const void *(*parent)(const void *); /**< Get parent. */ |
| 786 | const void *(*child)(const void *); /**< Get child. */ |
| 787 | const void *(*actions)(const void *); /**< Get actions. */ |
| 788 | const void *(*action_input)(const void *); /**< Get input action from action node. */ |
| 789 | const void *(*action_output)(const void *); /**< Get output action from action node. */ |
| 790 | const void *(*notifs)(const void *); /**< Get notifs. */ |
| 791 | }; |
| 792 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 793 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 794 | * Definition of the general Trg functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 795 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 796 | |
| 797 | /** |
| 798 | * @brief Print a substring but limited to the maximum length. |
| 799 | * @param[in] str is pointer to source. |
| 800 | * @param[in] len is number of characters to be printed. |
| 801 | * @param[in,out] out is output handler. |
| 802 | * @return str parameter shifted by len. |
| 803 | */ |
| 804 | static const char * |
| 805 | trg_print_substr(const char *str, size_t len, struct ly_out *out) |
| 806 | { |
| 807 | for (size_t i = 0; i < len; i++) { |
| 808 | ly_print_(out, "%c", str[0]); |
| 809 | str++; |
| 810 | } |
| 811 | return str; |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * @brief Pointer is not NULL and does not point to an empty string. |
| 816 | * @param[in] str is pointer to string to be checked. |
| 817 | * @return 1 if str pointing to non empty string otherwise 0. |
| 818 | */ |
| 819 | static ly_bool |
| 820 | trg_charptr_has_data(const char *str) |
| 821 | { |
| 822 | return (str) && (str[0] != '\0'); |
| 823 | } |
| 824 | |
| 825 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 826 | * @brief Check if @p word in @p src is present where words are |
| 827 | * delimited by @p delim. |
| 828 | * @param[in] src is source where words are separated by @p delim. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 829 | * @param[in] word to be searched. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 830 | * @param[in] delim is delimiter between @p words in @p src. |
| 831 | * @return 1 if src contains @p word otherwise 0. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 832 | */ |
| 833 | static ly_bool |
| 834 | trg_word_is_present(const char *src, const char *word, char delim) |
| 835 | { |
| 836 | const char *hit; |
| 837 | |
| 838 | if ((!src) || (src[0] == '\0') || (!word)) { |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | hit = strstr(src, word); |
| 843 | |
| 844 | if (hit) { |
| 845 | /* word was founded at the begin of src |
| 846 | * OR it match somewhere after delim |
| 847 | */ |
| 848 | if ((hit == src) || (hit[-1] == delim)) { |
| 849 | /* end of word was founded at the end of src |
| 850 | * OR end of word was match somewhere before delim |
| 851 | */ |
| 852 | char delim_or_end = (hit + strlen(word))[0]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 853 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 854 | if ((delim_or_end == '\0') || (delim_or_end == delim)) { |
| 855 | return 1; |
| 856 | } |
| 857 | } |
| 858 | /* after -> hit is just substr and it's not the whole word */ |
| 859 | /* jump to the next word */ |
| 860 | for ( ; (src[0] != '\0') && (src[0] != delim); src++) {} |
| 861 | /* skip delim */ |
| 862 | src = src[0] == '\0' ? src : src + 1; |
| 863 | /* continue with searching */ |
| 864 | return trg_word_is_present(src, word, delim); |
| 865 | } else { |
| 866 | return 0; |
| 867 | } |
| 868 | } |
| 869 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 870 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 871 | * Definition of printer functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 872 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 873 | |
| 874 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 875 | * @brief Write callback for ::ly_out_new_clb(). |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 876 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 877 | * @param[in] user_data is type of struct ly_out_clb_arg. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 878 | * @param[in] buf contains input characters |
| 879 | * @param[in] count is number of characters in buf. |
| 880 | * @return Number of printed bytes. |
| 881 | * @return Negative value in case of error. |
| 882 | */ |
| 883 | static ssize_t |
| 884 | trp_ly_out_clb_func(void *user_data, const void *buf, size_t count) |
| 885 | { |
| 886 | LY_ERR erc = LY_SUCCESS; |
| 887 | struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data; |
| 888 | |
| 889 | switch (data->mode) { |
| 890 | case TRD_PRINT: |
| 891 | erc = ly_write_(data->out, buf, count); |
| 892 | break; |
| 893 | case TRD_CHAR_COUNT: |
| 894 | data->counter = data->counter + count; |
| 895 | break; |
| 896 | default: |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | if (erc != LY_SUCCESS) { |
| 901 | data->last_error = erc; |
| 902 | return -1; |
| 903 | } else { |
| 904 | return count; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * @brief Check that indent in node can be considered as equivalent. |
| 910 | * @param[in] first is the first indent in node. |
| 911 | * @param[in] second is the second indent in node. |
| 912 | * @return 1 if indents are equivalent otherwise 0. |
| 913 | */ |
| 914 | static ly_bool |
| 915 | trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second) |
| 916 | { |
| 917 | const ly_bool a = first.type == second.type; |
| 918 | const ly_bool b = first.btw_name_opts == second.btw_name_opts; |
| 919 | const ly_bool c = first.btw_opts_type == second.btw_opts_type; |
| 920 | const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures; |
| 921 | |
| 922 | return a && b && c && d; |
| 923 | } |
| 924 | |
| 925 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 926 | * @brief Setting space character because node is last sibling. |
| 927 | * @param[in] wr is wrapper over which the shift operation |
| 928 | * is to be performed. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 929 | * @return New shifted wrapper. |
| 930 | */ |
| 931 | static struct trt_wrapper |
| 932 | trp_wrapper_set_shift(struct trt_wrapper wr) |
| 933 | { |
| 934 | assert(wr.actual_pos < 64); |
| 935 | /* +--<node> |
| 936 | * +--<node> |
| 937 | */ |
| 938 | wr.actual_pos++; |
| 939 | return wr; |
| 940 | } |
| 941 | |
| 942 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 943 | * @brief Setting '|' symbol because node is divided or |
| 944 | * it is not last sibling. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 945 | * @param[in] wr is source of wrapper. |
| 946 | * @return New wrapper which is marked at actual position and shifted. |
| 947 | */ |
| 948 | static struct trt_wrapper |
| 949 | trp_wrapper_set_mark(struct trt_wrapper wr) |
| 950 | { |
| 951 | assert(wr.actual_pos < 64); |
| 952 | wr.bit_marks1 |= 1U << wr.actual_pos; |
| 953 | return trp_wrapper_set_shift(wr); |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * @brief Setting ' ' symbol if node is last sibling otherwise set '|'. |
| 958 | * @param[in] wr is actual wrapper. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 959 | * @param[in] last_one is flag. Value 1 saying if the node is the last |
| 960 | * and has no more siblings. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 961 | * @return New wrapper for the actual node. |
| 962 | */ |
| 963 | static struct trt_wrapper |
| 964 | trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one) |
| 965 | { |
| 966 | return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr); |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * @brief Test if the wrappers are equivalent. |
| 971 | * @param[in] first is the first wrapper. |
| 972 | * @param[in] second is the second wrapper. |
| 973 | * @return 1 if the wrappers are equivalent otherwise 0. |
| 974 | */ |
| 975 | static ly_bool |
| 976 | trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second) |
| 977 | { |
| 978 | const ly_bool a = first.type == second.type; |
| 979 | const ly_bool b = first.bit_marks1 == second.bit_marks1; |
| 980 | const ly_bool c = first.actual_pos == second.actual_pos; |
| 981 | |
| 982 | return a && b && c; |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * @brief Print " | " sequence on line. |
| 987 | * @param[in] wr is wrapper to be printed. |
| 988 | * @param[in,out] out is output handler. |
| 989 | */ |
| 990 | static void |
| 991 | trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out) |
| 992 | { |
| 993 | uint32_t lb; |
| 994 | |
| 995 | if (wr.type == TRD_WRAPPER_TOP) { |
| 996 | lb = TRD_INDENT_LINE_BEGIN; |
| 997 | } else if (wr.type == TRD_WRAPPER_BODY) { |
| 998 | lb = TRD_INDENT_LINE_BEGIN * 2; |
| 999 | } else { |
| 1000 | lb = TRD_INDENT_LINE_BEGIN; |
| 1001 | } |
| 1002 | |
| 1003 | ly_print_(out, "%*c", lb, ' '); |
| 1004 | |
| 1005 | if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) { |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | for (uint32_t i = 0; i < wr.actual_pos; i++) { |
| 1010 | /** Test if the bit on the index is set. */ |
| 1011 | if ((wr.bit_marks1 >> i) & 1U) { |
| 1012 | ly_print_(out, "|"); |
| 1013 | } else { |
| 1014 | ly_print_(out, " "); |
| 1015 | } |
| 1016 | |
| 1017 | if (i != wr.actual_pos) { |
| 1018 | ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' '); |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * @brief Check if struct trt_node is empty. |
| 1025 | * @param[in] node is item to test. |
| 1026 | * @return 1 if node is considered empty otherwise 0. |
| 1027 | */ |
| 1028 | static ly_bool |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1029 | trp_node_is_empty(const struct trt_node *node) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1030 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1031 | const ly_bool a = TRP_EMPTY_TRT_IFFEATURES_IS_EMPTY(node->iffeatures.type); |
| 1032 | const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node->type); |
| 1033 | const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node->name); |
| 1034 | const ly_bool d = node->flags == NULL; |
| 1035 | const ly_bool e = node->status == NULL; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1036 | |
| 1037 | return a && b && c && d && e; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1041 | * @brief Check if [\<keys\>], \<type\> and |
| 1042 | * \<iffeatures\> are empty/not_set. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1043 | * @param[in] node is item to test. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1044 | * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\> |
| 1045 | * otherwise 0. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1046 | */ |
| 1047 | static ly_bool |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1048 | trp_node_body_is_empty(const struct trt_node *node) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1049 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1050 | const ly_bool a = TRP_EMPTY_TRT_IFFEATURES_IS_EMPTY(node->iffeatures.type); |
| 1051 | const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node->type); |
| 1052 | const ly_bool c = !node->name.keys; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1053 | |
| 1054 | return a && b && c; |
| 1055 | } |
| 1056 | |
| 1057 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1058 | * @brief Print entire struct trt_node_name structure. |
| 1059 | * @param[in] node_name is item to print. |
| 1060 | * @param[in,out] out is output handler. |
| 1061 | */ |
| 1062 | static void |
| 1063 | trp_print_node_name(struct trt_node_name node_name, struct ly_out *out) |
| 1064 | { |
| 1065 | const char *mod_prefix; |
| 1066 | const char *colon; |
| 1067 | const char trd_node_name_suffix_choice[] = ")"; |
| 1068 | const char trd_node_name_suffix_case[] = ")"; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1069 | |
| 1070 | if (TRP_NODE_NAME_IS_EMPTY(node_name)) { |
| 1071 | return; |
| 1072 | } |
| 1073 | |
| 1074 | if (node_name.module_prefix) { |
| 1075 | mod_prefix = node_name.module_prefix; |
| 1076 | colon = ":"; |
| 1077 | } else { |
| 1078 | mod_prefix = ""; |
| 1079 | colon = ""; |
| 1080 | } |
| 1081 | |
| 1082 | switch (node_name.type) { |
| 1083 | case TRD_NODE_ELSE: |
| 1084 | ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str); |
| 1085 | break; |
| 1086 | case TRD_NODE_CASE: |
| 1087 | ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case); |
| 1088 | break; |
| 1089 | case TRD_NODE_CHOICE: |
| 1090 | ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice); |
| 1091 | break; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1092 | case TRD_NODE_TRIPLE_DOT: |
| 1093 | ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT); |
| 1094 | break; |
| 1095 | default: |
| 1096 | break; |
| 1097 | } |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 1098 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1099 | if (node_name.add_opts) { |
| 1100 | ly_print_(out, "%s", node_name.add_opts); |
| 1101 | } |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 1102 | if (node_name.opts) { |
| 1103 | ly_print_(out, "%s", node_name.opts); |
| 1104 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1108 | * @brief Check if mark (?, !, *, /, @) is implicitly contained in |
| 1109 | * struct trt_node_name. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1110 | * @param[in] node_name is structure containing the 'mark'. |
| 1111 | * @return 1 if contain otherwise 0. |
| 1112 | */ |
| 1113 | static ly_bool |
| 1114 | trp_mark_is_used(struct trt_node_name node_name) |
| 1115 | { |
| 1116 | if (TRP_NODE_NAME_IS_EMPTY(node_name)) { |
| 1117 | return 0; |
aPiecek | bca5777 | 2022-10-13 13:51:59 +0200 | [diff] [blame] | 1118 | } else if (node_name.keys) { |
| 1119 | return 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | switch (node_name.type) { |
| 1123 | case TRD_NODE_ELSE: |
| 1124 | case TRD_NODE_CASE: |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1125 | return 0; |
| 1126 | default: |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1127 | if (node_name.add_opts || node_name.opts) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 1128 | return 1; |
| 1129 | } else { |
| 1130 | return 0; |
| 1131 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * @brief Print opts keys. |
| 1137 | * @param[in] node_name contains type of the node with his name. |
| 1138 | * @param[in] btw_name_opts is number of spaces between name and [keys]. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1139 | * @param[in] cf is basically a pointer to the function that prints |
| 1140 | * the keys. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1141 | * @param[in,out] out is output handler. |
| 1142 | */ |
| 1143 | static void |
| 1144 | trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out) |
| 1145 | { |
aPiecek | bca5777 | 2022-10-13 13:51:59 +0200 | [diff] [blame] | 1146 | if (!node_name.keys) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1147 | return; |
| 1148 | } |
| 1149 | |
| 1150 | /* <name><mark>___<keys>*/ |
| 1151 | if (btw_name_opts > 0) { |
| 1152 | ly_print_(out, "%*c", btw_name_opts, ' '); |
| 1153 | } |
| 1154 | ly_print_(out, "["); |
| 1155 | cf.pf(cf.ctx, out); |
| 1156 | ly_print_(out, "]"); |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * @brief Print entire struct trt_type structure. |
| 1161 | * @param[in] type is item to print. |
| 1162 | * @param[in,out] out is output handler. |
| 1163 | */ |
| 1164 | static void |
| 1165 | trp_print_type(struct trt_type type, struct ly_out *out) |
| 1166 | { |
| 1167 | if (TRP_TRT_TYPE_IS_EMPTY(type)) { |
| 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | switch (type.type) { |
| 1172 | case TRD_TYPE_NAME: |
| 1173 | ly_print_(out, "%s", type.str); |
| 1174 | break; |
| 1175 | case TRD_TYPE_TARGET: |
| 1176 | ly_print_(out, "-> %s", type.str); |
| 1177 | break; |
| 1178 | case TRD_TYPE_LEAFREF: |
| 1179 | ly_print_(out, "leafref"); |
| 1180 | default: |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * @brief Print all iffeatures of node |
| 1187 | * |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1188 | * @param[in] iff is iffeatures to print. |
| 1189 | * @param[in] cf is basically a pointer to the function that prints the list of features. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1190 | * @param[in,out] out is output handler. |
| 1191 | */ |
| 1192 | static void |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 1193 | trp_print_iffeatures(struct trt_iffeatures iff, struct trt_cf_print cf, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1194 | { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 1195 | if (iff.type == TRD_IFF_PRESENT) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1196 | ly_print_(out, "{"); |
| 1197 | cf.pf(cf.ctx, out); |
| 1198 | ly_print_(out, "}?"); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1199 | } else if (iff.type == TRD_IFF_OVERR) { |
| 1200 | ly_print_(out, "%s", iff.str); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * @brief Print just \<status\>--\<flags\> \<name\> with opts mark. |
| 1206 | * @param[in] node contains items to print. |
| 1207 | * @param[in] out is output handler. |
| 1208 | */ |
| 1209 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1210 | trp_print_node_up_to_name(const struct trt_node *node, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1211 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1212 | if (node->name.type == TRD_NODE_TRIPLE_DOT) { |
| 1213 | trp_print_node_name(node->name, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | /* <status>--<flags> */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1217 | ly_print_(out, "%s", node->status); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1218 | ly_print_(out, "--"); |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1219 | /* If the node is a case node, there is no space before the <name> |
| 1220 | * also case node has no flags. |
| 1221 | */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1222 | if (node->flags && (node->name.type != TRD_NODE_CASE)) { |
| 1223 | ly_print_(out, "%s", node->flags); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1224 | ly_print_(out, " "); |
| 1225 | } |
| 1226 | /* <name> */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1227 | trp_print_node_name(node->name, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1231 | * @brief Print alignment (spaces) instead of |
| 1232 | * \<status\>--\<flags\> \<name\> for divided node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1233 | * @param[in] node contains items to print. |
| 1234 | * @param[in] out is output handler. |
| 1235 | */ |
| 1236 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1237 | trp_print_divided_node_up_to_name(const struct trt_node *node, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1238 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1239 | uint32_t space = strlen(node->flags); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1240 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1241 | if (node->name.type == TRD_NODE_CASE) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1242 | /* :(<name> */ |
| 1243 | space += strlen(TRD_NODE_NAME_PREFIX_CASE); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1244 | } else if (node->name.type == TRD_NODE_CHOICE) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1245 | /* (<name> */ |
| 1246 | space += strlen(TRD_NODE_NAME_PREFIX_CHOICE); |
| 1247 | } else { |
| 1248 | /* _<name> */ |
| 1249 | space += strlen(" "); |
| 1250 | } |
| 1251 | |
| 1252 | /* <name> |
| 1253 | * __ |
| 1254 | */ |
| 1255 | space += TRD_INDENT_LONG_LINE_BREAK; |
| 1256 | |
| 1257 | ly_print_(out, "%*c", space, ' '); |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * @brief Print struct trt_node structure. |
| 1262 | * @param[in] node is item to print. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1263 | * @param[in] pck package of functions for |
| 1264 | * printing [\<keys\>] and \<iffeatures\>. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1265 | * @param[in] indent is the indent in node. |
| 1266 | * @param[in,out] out is output handler. |
| 1267 | */ |
| 1268 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1269 | trp_print_node(const struct trt_node *node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1270 | { |
| 1271 | ly_bool triple_dot; |
| 1272 | ly_bool divided; |
| 1273 | struct trt_cf_print cf_print_keys; |
| 1274 | struct trt_cf_print cf_print_iffeatures; |
| 1275 | |
| 1276 | if (trp_node_is_empty(node)) { |
| 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | /* <status>--<flags> <name><opts> <type> <if-features> */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1281 | triple_dot = node->name.type == TRD_NODE_TRIPLE_DOT; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1282 | divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED; |
| 1283 | |
| 1284 | if (triple_dot) { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1285 | trp_print_node_name(node->name, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1286 | return; |
| 1287 | } else if (!divided) { |
| 1288 | trp_print_node_up_to_name(node, out); |
| 1289 | } else { |
| 1290 | trp_print_divided_node_up_to_name(node, out); |
| 1291 | } |
| 1292 | |
| 1293 | /* <opts> */ |
| 1294 | /* <name>___<opts>*/ |
| 1295 | cf_print_keys.ctx = pck.tree_ctx; |
| 1296 | cf_print_keys.pf = pck.fps.print_keys; |
| 1297 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1298 | trp_print_opts_keys(node->name, indent.btw_name_opts, cf_print_keys, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1299 | |
| 1300 | /* <opts>__<type> */ |
| 1301 | if (indent.btw_opts_type > 0) { |
| 1302 | ly_print_(out, "%*c", indent.btw_opts_type, ' '); |
| 1303 | } |
| 1304 | |
| 1305 | /* <type> */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1306 | trp_print_type(node->type, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1307 | |
| 1308 | /* <type>__<iffeatures> */ |
| 1309 | if (indent.btw_type_iffeatures > 0) { |
| 1310 | ly_print_(out, "%*c", indent.btw_type_iffeatures, ' '); |
| 1311 | } |
| 1312 | |
| 1313 | /* <iffeatures> */ |
| 1314 | cf_print_iffeatures.ctx = pck.tree_ctx; |
| 1315 | cf_print_iffeatures.pf = pck.fps.print_features_names; |
| 1316 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1317 | trp_print_iffeatures(node->iffeatures, cf_print_iffeatures, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1321 | * @brief Print keyword based on trt_keyword_stmt.type. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1322 | * @param[in] ks is keyword statement to print. |
| 1323 | * @param[in,out] out is output handler |
| 1324 | */ |
| 1325 | static void |
| 1326 | trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out) |
| 1327 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1328 | if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) || |
| 1329 | !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) { |
| 1330 | ly_print_(out, "%s: ", ks.section_name); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1331 | return; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1332 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1333 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1334 | ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' '); |
| 1335 | if (ks.argument) { |
| 1336 | ly_print_(out, "%s ", ks.section_name); |
| 1337 | } else { |
| 1338 | ly_print_(out, "%s", ks.section_name); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1343 | * @brief Print trt_keyword_stmt.str which is string of name or path. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1344 | * @param[in] ks is keyword statement structure. |
| 1345 | * @param[in] mll is max line length. |
| 1346 | * @param[in,out] out is output handler. |
| 1347 | */ |
| 1348 | static void |
| 1349 | trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out) |
| 1350 | { |
| 1351 | uint32_t ind_initial; |
| 1352 | uint32_t ind_divided; |
| 1353 | /* flag if path must be splitted to more lines */ |
| 1354 | ly_bool linebreak_was_set; |
| 1355 | /* flag if at least one subpath was printed */ |
| 1356 | ly_bool subpath_printed; |
| 1357 | /* the sum of the sizes of the substrings on the current line */ |
| 1358 | uint32_t how_far; |
| 1359 | /* pointer to start of the subpath */ |
| 1360 | const char *sub_ptr; |
| 1361 | /* size of subpath from sub_ptr */ |
| 1362 | size_t sub_len; |
| 1363 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1364 | if ((!ks.argument) || (ks.argument[0] == '\0')) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1365 | return; |
| 1366 | } |
| 1367 | |
| 1368 | /* module name cannot be splitted */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1369 | if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) || !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) { |
| 1370 | ly_print_(out, "%s", ks.argument); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | /* after -> for trd_keyword_stmt_body do */ |
| 1375 | |
| 1376 | /* set begin indentation */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1377 | ind_initial = TRD_INDENT_LINE_BEGIN + strlen(ks.section_name) + 1; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1378 | ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK; |
| 1379 | linebreak_was_set = 0; |
| 1380 | subpath_printed = 0; |
| 1381 | how_far = 0; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1382 | sub_ptr = ks.argument; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1383 | sub_len = 0; |
| 1384 | |
| 1385 | while (sub_ptr[0] != '\0') { |
| 1386 | uint32_t ind; |
| 1387 | /* skip slash */ |
| 1388 | const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1389 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1390 | /* get position of the end of substr */ |
| 1391 | tmp = strchr(tmp, '/'); |
| 1392 | /* set correct size if this is a last substring */ |
| 1393 | sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr); |
| 1394 | /* actualize sum of the substring's sizes on the current line */ |
| 1395 | how_far += sub_len; |
| 1396 | /* correction due to colon character if it this is last substring */ |
| 1397 | how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far; |
| 1398 | /* choose indentation which depends on |
| 1399 | * whether the string is printed on multiple lines or not |
| 1400 | */ |
| 1401 | ind = linebreak_was_set ? ind_divided : ind_initial; |
| 1402 | if (ind + how_far <= mll) { |
| 1403 | /* printing before max line length */ |
| 1404 | sub_ptr = trg_print_substr(sub_ptr, sub_len, out); |
| 1405 | subpath_printed = 1; |
| 1406 | } else { |
| 1407 | /* printing on new line */ |
| 1408 | if (subpath_printed == 0) { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1409 | /* first subpath is too long |
| 1410 | * but print it at first line anyway |
| 1411 | */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1412 | sub_ptr = trg_print_substr(sub_ptr, sub_len, out); |
| 1413 | subpath_printed = 1; |
| 1414 | continue; |
| 1415 | } |
| 1416 | ly_print_(out, "\n"); |
| 1417 | ly_print_(out, "%*c", ind_divided, ' '); |
| 1418 | linebreak_was_set = 1; |
| 1419 | sub_ptr = trg_print_substr(sub_ptr, sub_len, out); |
| 1420 | how_far = sub_len; |
| 1421 | subpath_printed = 1; |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1427 | * @brief Print separator based on trt_keyword_stmt.type |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1428 | * @param[in] ks is keyword statement structure. |
| 1429 | * @param[in,out] out is output handler. |
| 1430 | */ |
| 1431 | static void |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1432 | trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1433 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1434 | if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) || !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) { |
| 1435 | return; |
| 1436 | } else if (ks.has_node) { |
| 1437 | ly_print_(out, ":"); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * @brief Print entire struct trt_keyword_stmt structure. |
| 1443 | * @param[in] ks is item to print. |
| 1444 | * @param[in] mll is max line length. |
| 1445 | * @param[in,out] out is output handler. |
| 1446 | */ |
| 1447 | static void |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1448 | trp_print_keyword_stmt(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1449 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1450 | assert(ks.section_name); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1451 | trt_print_keyword_stmt_begin(ks, out); |
| 1452 | trt_print_keyword_stmt_str(ks, mll, out); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1453 | trt_print_keyword_stmt_end(ks, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1454 | } |
| 1455 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1456 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1457 | * Main trp functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1458 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1459 | |
| 1460 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1461 | * @brief Printing one line including wrapper and node |
| 1462 | * which can be incomplete (divided). |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1463 | * @param[in] node is \<node\> representation. |
| 1464 | * @param[in] pck contains special printing functions callback. |
| 1465 | * @param[in] indent contains wrapper and indent in node numbers. |
| 1466 | * @param[in,out] out is output handler. |
| 1467 | */ |
| 1468 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1469 | trp_print_line(const struct trt_node *node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1470 | { |
| 1471 | trp_print_wrapper(indent.wrapper, out); |
| 1472 | trp_print_node(node, pck, indent.in_node, out); |
| 1473 | } |
| 1474 | |
| 1475 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1476 | * @brief Printing one line including wrapper and |
| 1477 | * \<status\>--\<flags\> \<name\>\<option_mark\>. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1478 | * @param[in] node is \<node\> representation. |
| 1479 | * @param[in] wr is wrapper for printing indentation before node. |
| 1480 | * @param[in] out is output handler. |
| 1481 | */ |
| 1482 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1483 | trp_print_line_up_to_node_name(const struct trt_node *node, struct trt_wrapper wr, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1484 | { |
| 1485 | trp_print_wrapper(wr, out); |
| 1486 | trp_print_node_up_to_name(node, out); |
| 1487 | } |
| 1488 | |
| 1489 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1490 | * @brief Check if leafref target must be change to string 'leafref' |
| 1491 | * because his target string is too long. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1492 | * @param[in] node containing leafref target. |
| 1493 | * @param[in] wr is wrapper for printing indentation before node. |
| 1494 | * @param[in] mll is max line length. |
| 1495 | * @param[in] out is output handler. |
| 1496 | * @return true if leafref must be changed to string 'leafref'. |
| 1497 | */ |
| 1498 | static ly_bool |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1499 | trp_leafref_target_is_too_long(const struct trt_node *node, struct trt_wrapper wr, size_t mll, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1500 | { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 1501 | size_t type_len; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1502 | struct ly_out_clb_arg *data; |
| 1503 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1504 | if (node->type.type != TRD_TYPE_TARGET) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | /* set ly_out to counting characters */ |
| 1509 | data = out->method.clb.arg; |
| 1510 | |
| 1511 | data->counter = 0; |
| 1512 | data->mode = TRD_CHAR_COUNT; |
| 1513 | /* count number of printed bytes */ |
| 1514 | trp_print_wrapper(wr, out); |
| 1515 | ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' '); |
| 1516 | trp_print_divided_node_up_to_name(node, out); |
| 1517 | data->mode = TRD_PRINT; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1518 | type_len = strlen(node->type.str); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1519 | |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 1520 | return data->counter + type_len > mll; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | /** |
| 1524 | * @brief Get default indent in node based on node values. |
| 1525 | * @param[in] node is \<node\> representation. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1526 | * @return Default indent in node assuming that the node |
| 1527 | * will not be divided. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1528 | */ |
| 1529 | static struct trt_indent_in_node |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1530 | trp_default_indent_in_node(const struct trt_node *node) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1531 | { |
| 1532 | struct trt_indent_in_node ret; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1533 | uint32_t opts_len = 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1534 | |
| 1535 | ret.type = TRD_INDENT_IN_NODE_NORMAL; |
| 1536 | |
| 1537 | /* btw_name_opts */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1538 | ret.btw_name_opts = node->name.keys ? TRD_INDENT_BEFORE_KEYS : 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1539 | |
| 1540 | /* btw_opts_type */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1541 | if (!(TRP_TRT_TYPE_IS_EMPTY(node->type))) { |
| 1542 | if (trp_mark_is_used(node->name)) { |
| 1543 | opts_len += node->name.add_opts ? strlen(node->name.add_opts) : 0; |
| 1544 | opts_len += node->name.opts ? strlen(node->name.opts) : 0; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1545 | ret.btw_opts_type = TRD_INDENT_BEFORE_TYPE > opts_len ? 1 : TRD_INDENT_BEFORE_TYPE - opts_len; |
| 1546 | } else { |
| 1547 | ret.btw_opts_type = TRD_INDENT_BEFORE_TYPE; |
| 1548 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1549 | } else { |
| 1550 | ret.btw_opts_type = 0; |
| 1551 | } |
| 1552 | |
| 1553 | /* btw_type_iffeatures */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1554 | ret.btw_type_iffeatures = node->iffeatures.type == TRD_IFF_PRESENT ? TRD_INDENT_BEFORE_IFFEATURES : 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1555 | |
| 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | /** |
| 1560 | * @brief Setting linebreaks in trt_indent_in_node. |
| 1561 | * |
| 1562 | * The order where the linebreak tag can be placed is from the end. |
| 1563 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1564 | * @param[in] indent containing alignment lengths |
| 1565 | * or already linebreak marks. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1566 | * @return indent with a newly placed linebreak tag. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1567 | * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible |
| 1568 | * to place a more linebreaks. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1569 | */ |
| 1570 | static struct trt_indent_in_node |
| 1571 | trp_indent_in_node_place_break(struct trt_indent_in_node indent) |
| 1572 | { |
| 1573 | /* somewhere must be set a line break in node */ |
| 1574 | struct trt_indent_in_node ret = indent; |
| 1575 | |
| 1576 | /* gradually break the node from the end */ |
| 1577 | if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) { |
| 1578 | ret.btw_type_iffeatures = TRD_LINEBREAK; |
| 1579 | } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) { |
| 1580 | ret.btw_opts_type = TRD_LINEBREAK; |
| 1581 | } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) { |
| 1582 | /* set line break between name and opts */ |
| 1583 | ret.btw_name_opts = TRD_LINEBREAK; |
| 1584 | } else { |
| 1585 | /* it is not possible to place a more line breaks, |
| 1586 | * unfortunately the max_line_length constraint is violated |
| 1587 | */ |
| 1588 | ret.type = TRD_INDENT_IN_NODE_FAILED; |
| 1589 | } |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
| 1593 | /** |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1594 | * @brief Set the first half of the node based on the linebreak mark. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1595 | * |
| 1596 | * Items in the second half of the node will be empty. |
| 1597 | * |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1598 | * @param[in,out] innod contains information in which part of the \<node\> |
| 1599 | * the first half ends. Set first half of the node, indent is unchanged. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1600 | */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1601 | static void |
| 1602 | trp_first_half_node(struct trt_pair_indent_node *innod) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1603 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1604 | if (innod->indent.btw_name_opts == TRD_LINEBREAK) { |
| 1605 | innod->node.type = TRP_EMPTY_TRT_TYPE; |
| 1606 | innod->node.iffeatures = TRP_EMPTY_TRT_IFFEATURES; |
| 1607 | } else if (innod->indent.btw_opts_type == TRD_LINEBREAK) { |
| 1608 | innod->node.type = TRP_EMPTY_TRT_TYPE; |
| 1609 | innod->node.iffeatures = TRP_EMPTY_TRT_IFFEATURES; |
| 1610 | } else if (innod->indent.btw_type_iffeatures == TRD_LINEBREAK) { |
| 1611 | innod->node.iffeatures = TRP_EMPTY_TRT_IFFEATURES; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1612 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /** |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1616 | * @brief Set the second half of the node based on the linebreak mark. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1617 | * |
| 1618 | * Items in the first half of the node will be empty. |
| 1619 | * Indentations belonging to the first node will be reset to zero. |
| 1620 | * |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1621 | * @param[in,out] innod contains information in which part of the \<node\> |
| 1622 | * the second half starts. Set second half of the node, indent is newly set. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1623 | */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1624 | static void |
| 1625 | trp_second_half_node(struct trt_pair_indent_node *innod) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1626 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1627 | if (innod->indent.btw_name_opts < 0) { |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1628 | /* Logically, the information up to token <opts> should |
| 1629 | * be deleted, but the the trp_print_node function needs it to |
| 1630 | * create the correct indent. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1631 | */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1632 | innod->indent.btw_name_opts = 0; |
| 1633 | innod->indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(innod->node.type) ? 0 : TRD_INDENT_BEFORE_TYPE; |
| 1634 | innod->indent.btw_type_iffeatures = innod->node.iffeatures.type == TRD_IFF_NON_PRESENT ? 0 : TRD_INDENT_BEFORE_IFFEATURES; |
| 1635 | } else if (innod->indent.btw_opts_type == TRD_LINEBREAK) { |
| 1636 | innod->indent.btw_name_opts = 0; |
| 1637 | innod->indent.btw_opts_type = 0; |
| 1638 | innod->indent.btw_type_iffeatures = innod->node.iffeatures.type == TRD_IFF_NON_PRESENT ? 0 : TRD_INDENT_BEFORE_IFFEATURES; |
| 1639 | } else if (innod->indent.btw_type_iffeatures == TRD_LINEBREAK) { |
| 1640 | innod->node.type = TRP_EMPTY_TRT_TYPE; |
| 1641 | innod->indent.btw_name_opts = 0; |
| 1642 | innod->indent.btw_opts_type = 0; |
| 1643 | innod->indent.btw_type_iffeatures = 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1644 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /** |
| 1648 | * @brief Get the correct alignment for the node. |
| 1649 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1650 | * This function is recursively called itself. It's like a backend |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 1651 | * function for a function ::trp_try_normal_indent_in_node(). |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1652 | * |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1653 | * @param[in] pck contains speciall callback functions for printing. |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1654 | * @param[in] wrapper contains information about '|' context. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1655 | * @param[in] mll is max line length. |
| 1656 | * @param[in,out] cnt counting number of characters to print. |
| 1657 | * @param[in,out] out is output handler. |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1658 | * @param[in,out] innod pair of node and indentation numbers of that node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1659 | */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1660 | static void |
| 1661 | trp_try_normal_indent_in_node_(struct trt_pck_print pck, struct trt_wrapper wrapper, size_t mll, size_t *cnt, |
| 1662 | struct ly_out *out, struct trt_pair_indent_node *innod) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1663 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1664 | trp_print_line(&innod->node, pck, TRP_INIT_PCK_INDENT(wrapper, innod->indent), out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1665 | |
| 1666 | if (*cnt <= mll) { |
| 1667 | /* success */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1668 | return; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1669 | } else { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1670 | innod->indent = trp_indent_in_node_place_break(innod->indent); |
| 1671 | if (innod->indent.type != TRD_INDENT_IN_NODE_FAILED) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1672 | /* erase information in node due to line break */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1673 | trp_first_half_node(innod); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1674 | /* check if line fits, recursive call */ |
| 1675 | *cnt = 0; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1676 | trp_try_normal_indent_in_node_(pck, wrapper, mll, cnt, out, innod); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1677 | /* make sure that the result will be with the status divided |
| 1678 | * or eventually with status failed */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1679 | innod->indent.type = innod->indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1680 | } |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1681 | return; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * @brief Get the correct alignment for the node. |
| 1687 | * |
| 1688 | * @param[in] node is \<node\> representation. |
| 1689 | * @param[in] pck contains speciall callback functions for printing. |
| 1690 | * @param[in] indent contains wrapper and indent in node numbers. |
| 1691 | * @param[in] mll is max line length. |
| 1692 | * @param[in,out] out is output handler. |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1693 | * @param[out] innod If the node does not fit in the line, some indent variable has negative value as a line break sign |
| 1694 | * and therefore ::TRD_INDENT_IN_NODE_DIVIDED is set. |
| 1695 | * If the node fits into the line, all indent variables values has non-negative number and therefore |
| 1696 | * ::TRD_INDENT_IN_NODE_NORMAL is set. |
| 1697 | * If the node does not fit into the line, all indent variables has negative or zero values, function failed |
| 1698 | * and therefore ::TRD_INDENT_IN_NODE_FAILED is set. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1699 | */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1700 | static void |
| 1701 | trp_try_normal_indent_in_node(const struct trt_node *node, struct trt_pck_print pck, struct trt_pck_indent indent, |
| 1702 | size_t mll, struct ly_out *out, struct trt_pair_indent_node *innod) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1703 | { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1704 | struct ly_out_clb_arg *data; |
| 1705 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1706 | *innod = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, *node); |
| 1707 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1708 | /* set ly_out to counting characters */ |
| 1709 | data = out->method.clb.arg; |
| 1710 | |
| 1711 | data->counter = 0; |
| 1712 | data->mode = TRD_CHAR_COUNT; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1713 | trp_try_normal_indent_in_node_(pck, indent.wrapper, mll, &data->counter, out, innod); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1714 | data->mode = TRD_PRINT; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 1718 | * @brief Auxiliary function for ::trp_print_entire_node() |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1719 | * that prints split nodes. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1720 | * @param[in] node is node representation. |
| 1721 | * @param[in] ppck contains speciall callback functions for printing. |
| 1722 | * @param[in] ipck contains wrapper and indent in node numbers. |
| 1723 | * @param[in] mll is max line length. |
| 1724 | * @param[in,out] out is output handler. |
| 1725 | */ |
| 1726 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1727 | trp_print_divided_node(const struct trt_node *node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1728 | { |
| 1729 | ly_bool entire_node_was_printed; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1730 | struct trt_pair_indent_node innod; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1731 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1732 | trp_try_normal_indent_in_node(node, ppck, ipck, mll, out, &innod); |
| 1733 | |
| 1734 | if (innod.indent.type == TRD_INDENT_IN_NODE_FAILED) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1735 | /* nothing can be done, continue as usual */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1736 | innod.indent.type = TRD_INDENT_IN_NODE_DIVIDED; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1739 | trp_print_line(&innod.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, innod.indent), out); |
| 1740 | entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, innod.indent); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1741 | |
| 1742 | if (!entire_node_was_printed) { |
| 1743 | ly_print_(out, "\n"); |
| 1744 | /* continue with second half node */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1745 | innod.node = *node; |
| 1746 | trp_second_half_node(&innod); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1747 | /* continue with printing node */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1748 | trp_print_divided_node(&innod.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, innod.indent), mll, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1749 | } else { |
| 1750 | return; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1755 | * @brief Printing of the wrapper and the whole node, |
| 1756 | * which can be divided into several lines. |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1757 | * @param[in] node_p is node representation. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1758 | * @param[in] ppck contains speciall callback functions for printing. |
| 1759 | * @param[in] ipck contains wrapper and indent in node numbers. |
| 1760 | * @param[in] mll is max line length. |
| 1761 | * @param[in,out] out is output handler. |
| 1762 | */ |
| 1763 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1764 | trp_print_entire_node(const struct trt_node *node_p, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, |
| 1765 | struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1766 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1767 | struct trt_pair_indent_node innod; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1768 | struct trt_pck_indent tmp; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1769 | struct trt_node node; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1770 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1771 | node = *node_p; |
| 1772 | if (trp_leafref_target_is_too_long(&node, ipck.wrapper, mll, out)) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1773 | node.type.type = TRD_TYPE_LEAFREF; |
| 1774 | } |
| 1775 | |
| 1776 | /* check if normal indent is possible */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1777 | trp_try_normal_indent_in_node(&node, ppck, ipck, mll, out, &innod); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1778 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1779 | if (innod.indent.type == TRD_INDENT_IN_NODE_NORMAL) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1780 | /* node fits to one line */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1781 | trp_print_line(&node, ppck, ipck, out); |
| 1782 | } else if (innod.indent.type == TRD_INDENT_IN_NODE_DIVIDED) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1783 | /* node will be divided */ |
| 1784 | /* print first half */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1785 | tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, innod.indent); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1786 | /* pretend that this is normal node */ |
| 1787 | tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL; |
| 1788 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1789 | trp_print_line(&innod.node, ppck, tmp, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1790 | ly_print_(out, "\n"); |
| 1791 | |
| 1792 | /* continue with second half on new line */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1793 | innod.node = node; |
| 1794 | trp_second_half_node(&innod); |
| 1795 | tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), innod.indent); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1796 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1797 | trp_print_divided_node(&innod.node, ppck, tmp, mll, out); |
| 1798 | } else if (innod.indent.type == TRD_INDENT_IN_NODE_FAILED) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1799 | /* node name is too long */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1800 | trp_print_line_up_to_node_name(&node, ipck.wrapper, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1801 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1802 | if (trp_node_body_is_empty(&node)) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1803 | return; |
| 1804 | } else { |
| 1805 | ly_print_(out, "\n"); |
| 1806 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1807 | innod.node = node; |
| 1808 | trp_second_half_node(&innod); |
| 1809 | innod.indent.type = TRD_INDENT_IN_NODE_DIVIDED; |
| 1810 | tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), innod.indent); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1811 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 1812 | trp_print_divided_node(&innod.node, ppck, tmp, mll, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1813 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1814 | } |
| 1815 | } |
| 1816 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1817 | /** |
| 1818 | * @brief Check if parent-stmt is valid for printing extensinon. |
| 1819 | * |
| 1820 | * @param[in] lysc_tree flag if ext is from compiled tree. |
| 1821 | * @param[in] ext Extension to check. |
| 1822 | * @return 1 if extension is valid. |
| 1823 | */ |
| 1824 | static ly_bool |
| 1825 | trp_ext_parent_is_valid(ly_bool lysc_tree, void *ext) |
| 1826 | { |
| 1827 | enum ly_stmt parent_stmt; |
| 1828 | |
| 1829 | if (lysc_tree) { |
| 1830 | parent_stmt = ((struct lysc_ext_instance *)ext)->parent_stmt; |
| 1831 | } else { |
| 1832 | parent_stmt = ((struct lysp_ext_instance *)ext)->parent_stmt; |
| 1833 | } |
| 1834 | if ((parent_stmt & LY_STMT_OP_MASK) || (parent_stmt & LY_STMT_DATA_NODE_MASK) || |
| 1835 | (parent_stmt & LY_STMT_SUBMODULE) || parent_stmt & LY_STMT_MODULE) { |
| 1836 | return 1; |
| 1837 | } else { |
| 1838 | return 0; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | /** |
| 1843 | * @brief Check if printer_tree can use node extension. |
| 1844 | * |
| 1845 | * @param[in] lysc_tree Flag if @p node is compiled. |
| 1846 | * @param[in] node to check. Its type is lysc_node or lysp_node. |
| 1847 | * @return Pointer to extension instance which printer_tree can used. |
| 1848 | */ |
| 1849 | static void * |
| 1850 | trp_ext_is_present(ly_bool lysc_tree, const void *node) |
| 1851 | { |
| 1852 | const struct lysp_node *pn; |
| 1853 | const struct lysc_node *cn; |
| 1854 | LY_ARRAY_COUNT_TYPE i; |
| 1855 | void *ret = NULL; |
| 1856 | |
| 1857 | if (!node) { |
| 1858 | return NULL; |
| 1859 | } |
| 1860 | |
| 1861 | if (lysc_tree) { |
| 1862 | cn = (const struct lysc_node *)node; |
| 1863 | LY_ARRAY_FOR(cn->exts, i) { |
| 1864 | if (!(cn->exts && cn->exts->def->plugin && cn->exts->def->plugin->printer_ctree)) { |
| 1865 | continue; |
| 1866 | } |
| 1867 | if (!trp_ext_parent_is_valid(1, &cn->exts[i])) { |
| 1868 | continue; |
| 1869 | } |
| 1870 | ret = &cn->exts[i]; |
| 1871 | break; |
| 1872 | } |
| 1873 | } else { |
| 1874 | pn = (const struct lysp_node *)node; |
| 1875 | LY_ARRAY_FOR(pn->exts, i) { |
Peter Kosyh | 369c66e | 2023-08-06 13:54:23 +0300 | [diff] [blame] | 1876 | if (!(pn->exts && pn->exts->record && pn->exts->record->plugin.printer_ptree)) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1877 | continue; |
| 1878 | } |
| 1879 | if (!trp_ext_parent_is_valid(0, &pn->exts[i])) { |
| 1880 | continue; |
| 1881 | } |
| 1882 | ret = &pn->exts[i]; |
| 1883 | break; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | return ret; |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * @brief Check if printer_tree can use node extension. |
| 1892 | * |
| 1893 | * @param[in] tc Context with current node. |
| 1894 | * @return 1 if some extension for printer_tree is valid. |
| 1895 | */ |
| 1896 | static ly_bool |
| 1897 | trp_ext_is_present_in_node(struct trt_tree_ctx *tc) |
| 1898 | { |
| 1899 | if (tc->lysc_tree && trp_ext_is_present(tc->lysc_tree, tc->cn)) { |
| 1900 | return 1; |
| 1901 | } else if (trp_ext_is_present(tc->lysc_tree, tc->pn)) { |
| 1902 | return 1; |
| 1903 | } |
| 1904 | |
| 1905 | return 0; |
| 1906 | } |
| 1907 | |
| 1908 | /** |
| 1909 | * @brief Release allocated memory and set pointers to NULL. |
| 1910 | * |
| 1911 | * @param[in,out] overr is override structure to release. |
| 1912 | * @param[out] filtered is flag to reset. |
| 1913 | */ |
| 1914 | static void |
| 1915 | trp_ext_free_node_override(struct lyplg_ext_sprinter_tree_node_override *overr, ly_bool *filtered) |
| 1916 | { |
| 1917 | *filtered = 0; |
| 1918 | overr->flags = NULL; |
| 1919 | overr->add_opts = NULL; |
| 1920 | } |
| 1921 | |
| 1922 | /** |
| 1923 | * @brief Release private plugin data. |
| 1924 | * |
| 1925 | * @param[in,out] plug_ctx is plugin context. |
| 1926 | */ |
| 1927 | static void |
| 1928 | trp_ext_free_plugin_ctx(struct lyspr_tree_ctx *plug_ctx) |
| 1929 | { |
| 1930 | LY_ARRAY_FREE(plug_ctx->schemas); |
| 1931 | if (plug_ctx->free_plugin_priv) { |
| 1932 | plug_ctx->free_plugin_priv(plug_ctx->plugin_priv); |
| 1933 | } |
| 1934 | } |
| 1935 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 1936 | /********************************************************************** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 1937 | * trop and troc getters |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 1938 | *********************************************************************/ |
| 1939 | |
| 1940 | /** |
| 1941 | * @brief Get nodetype. |
| 1942 | * @param[in] node is any lysp_node. |
| 1943 | */ |
| 1944 | static uint16_t |
| 1945 | trop_nodetype(const void *node) |
| 1946 | { |
| 1947 | return ((const struct lysp_node *)node)->nodetype; |
| 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * @brief Get sibling. |
| 1952 | * @param[in] node is any lysp_node. |
| 1953 | */ |
| 1954 | static const void * |
| 1955 | trop_next(const void *node) |
| 1956 | { |
| 1957 | return ((const struct lysp_node *)node)->next; |
| 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * @brief Get parent. |
| 1962 | * @param[in] node is any lysp_node. |
| 1963 | */ |
| 1964 | static const void * |
| 1965 | trop_parent(const void *node) |
| 1966 | { |
| 1967 | return ((const struct lysp_node *)node)->parent; |
| 1968 | } |
| 1969 | |
| 1970 | /** |
| 1971 | * @brief Try to get child. |
| 1972 | * @param[in] node is any lysp_node. |
| 1973 | */ |
| 1974 | static const void * |
| 1975 | trop_child(const void *node) |
| 1976 | { |
| 1977 | return lysp_node_child(node); |
| 1978 | } |
| 1979 | |
| 1980 | /** |
| 1981 | * @brief Try to get action. |
| 1982 | * @param[in] node is any lysp_node. |
| 1983 | */ |
| 1984 | static const void * |
| 1985 | trop_actions(const void *node) |
| 1986 | { |
| 1987 | return lysp_node_actions(node); |
| 1988 | } |
| 1989 | |
| 1990 | /** |
| 1991 | * @brief Try to get action. |
| 1992 | * @param[in] node must be of type lysp_node_action. |
| 1993 | */ |
| 1994 | static const void * |
| 1995 | trop_action_input(const void *node) |
| 1996 | { |
| 1997 | return &((const struct lysp_node_action *)node)->input; |
| 1998 | } |
| 1999 | |
| 2000 | /** |
| 2001 | * @brief Try to get action. |
| 2002 | * @param[in] node must be of type lysp_node_action. |
| 2003 | */ |
| 2004 | static const void * |
| 2005 | trop_action_output(const void *node) |
| 2006 | { |
| 2007 | return &((const struct lysp_node_action *)node)->output; |
| 2008 | } |
| 2009 | |
| 2010 | /** |
| 2011 | * @brief Try to get action. |
| 2012 | * @param[in] node is any lysp_node. |
| 2013 | */ |
| 2014 | static const void * |
| 2015 | trop_notifs(const void *node) |
| 2016 | { |
| 2017 | return lysp_node_notifs(node); |
| 2018 | } |
| 2019 | |
| 2020 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 2021 | * @brief Fill struct tro_getters with @ref TRP_trop getters |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2022 | * which are adapted to lysp nodes. |
| 2023 | */ |
| 2024 | static struct tro_getters |
Michal Vasko | 45acb84 | 2022-11-08 09:23:17 +0100 | [diff] [blame] | 2025 | trop_init_getters(void) |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2026 | { |
| 2027 | return (struct tro_getters) { |
| 2028 | .nodetype = trop_nodetype, |
| 2029 | .next = trop_next, |
| 2030 | .parent = trop_parent, |
| 2031 | .child = trop_child, |
| 2032 | .actions = trop_actions, |
| 2033 | .action_input = trop_action_input, |
| 2034 | .action_output = trop_action_output, |
| 2035 | .notifs = trop_notifs |
| 2036 | }; |
| 2037 | } |
| 2038 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2039 | /** |
| 2040 | * @brief Get nodetype. |
| 2041 | * @param[in] node is any lysc_node. |
| 2042 | */ |
| 2043 | static uint16_t |
| 2044 | troc_nodetype(const void *node) |
| 2045 | { |
| 2046 | return ((const struct lysc_node *)node)->nodetype; |
| 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * @brief Get sibling. |
| 2051 | * @param[in] node is any lysc_node. |
| 2052 | */ |
| 2053 | static const void * |
| 2054 | troc_next(const void *node) |
| 2055 | { |
| 2056 | return ((const struct lysc_node *)node)->next; |
| 2057 | } |
| 2058 | |
| 2059 | /** |
| 2060 | * @brief Get parent. |
| 2061 | * @param[in] node is any lysc_node. |
| 2062 | */ |
| 2063 | static const void * |
| 2064 | troc_parent(const void *node) |
| 2065 | { |
| 2066 | return ((const struct lysc_node *)node)->parent; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
| 2070 | * @brief Try to get child. |
| 2071 | * @param[in] node is any lysc_node. |
| 2072 | */ |
| 2073 | static const void * |
| 2074 | troc_child(const void *node) |
| 2075 | { |
| 2076 | return lysc_node_child(node); |
| 2077 | } |
| 2078 | |
| 2079 | /** |
| 2080 | * @brief Try to get action. |
| 2081 | * @param[in] node is any lysc_node. |
| 2082 | */ |
| 2083 | static const void * |
| 2084 | troc_actions(const void *node) |
| 2085 | { |
| 2086 | return lysc_node_actions(node); |
| 2087 | } |
| 2088 | |
| 2089 | /** |
| 2090 | * @brief Try to get action. |
| 2091 | * @param[in] node must be of type lysc_node_action. |
| 2092 | */ |
| 2093 | static const void * |
| 2094 | troc_action_input(const void *node) |
| 2095 | { |
| 2096 | return &((const struct lysc_node_action *)node)->input; |
| 2097 | } |
| 2098 | |
| 2099 | /** |
| 2100 | * @brief Try to get action. |
| 2101 | * @param[in] node must be of type lysc_node_action. |
| 2102 | */ |
| 2103 | static const void * |
| 2104 | troc_action_output(const void *node) |
| 2105 | { |
| 2106 | return &((const struct lysc_node_action *)node)->output; |
| 2107 | } |
| 2108 | |
| 2109 | /** |
| 2110 | * @brief Try to get action. |
| 2111 | * @param[in] node is any lysc_node. |
| 2112 | */ |
| 2113 | static const void * |
| 2114 | troc_notifs(const void *node) |
| 2115 | { |
| 2116 | return lysc_node_notifs(node); |
| 2117 | } |
| 2118 | |
| 2119 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 2120 | * @brief Fill struct tro_getters with @ref TRP_troc getters |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2121 | * which are adapted to lysc nodes. |
| 2122 | */ |
| 2123 | static struct tro_getters |
Michal Vasko | 45acb84 | 2022-11-08 09:23:17 +0100 | [diff] [blame] | 2124 | troc_init_getters(void) |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2125 | { |
| 2126 | return (struct tro_getters) { |
| 2127 | .nodetype = troc_nodetype, |
| 2128 | .next = troc_next, |
| 2129 | .parent = troc_parent, |
| 2130 | .child = troc_child, |
| 2131 | .actions = troc_actions, |
| 2132 | .action_input = troc_action_input, |
| 2133 | .action_output = troc_action_output, |
| 2134 | .notifs = troc_notifs |
| 2135 | }; |
| 2136 | } |
| 2137 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2138 | /********************************************************************** |
| 2139 | * tro functions |
| 2140 | *********************************************************************/ |
| 2141 | |
| 2142 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2143 | * @brief Call override function for @p node. |
| 2144 | * |
| 2145 | * @param[in] lysc_tree if @p node is compiled. |
| 2146 | * @param[in] node to create override. |
| 2147 | * @param[in] erase_node_overr if override structure must be reseted. |
| 2148 | * @param[in,out] plc current plugin context. |
| 2149 | * @return pointer to override structure or NULL. Override structure in @p plc is updated too. |
| 2150 | */ |
| 2151 | static struct lyplg_ext_sprinter_tree_node_override * |
| 2152 | tro_set_node_overr(ly_bool lysc_tree, const void *node, ly_bool erase_node_overr, struct trt_plugin_ctx *plc) |
| 2153 | { |
| 2154 | LY_ERR rc = LY_SUCCESS; |
| 2155 | struct lyplg_ext_sprinter_tree_node_override *no; |
| 2156 | struct lyspr_tree_ctx *plug_ctx; |
| 2157 | struct lysc_ext_instance *ce; |
| 2158 | struct lysp_ext_instance *pe; |
| 2159 | |
| 2160 | if (erase_node_overr) { |
| 2161 | trp_ext_free_node_override(&plc->node_overr, &plc->filtered); |
| 2162 | } |
| 2163 | no = &plc->node_overr; |
| 2164 | if (!plc->ctx && lysc_tree && (ce = trp_ext_is_present(lysc_tree, node))) { |
| 2165 | rc = ce->def->plugin->printer_ctree(ce, NULL, &no->flags, &no->add_opts); |
| 2166 | } else if (!plc->ctx && (pe = trp_ext_is_present(lysc_tree, node))) { |
| 2167 | rc = pe->record->plugin.printer_ptree(pe, NULL, &no->flags, &no->add_opts); |
| 2168 | } else if (plc->ctx) { |
| 2169 | if (plc->schema && plc->schema->compiled && plc->schema->cn_overr) { |
| 2170 | rc = plc->schema->cn_overr(node, plc->ctx->plugin_priv, &plc->filtered, &no->flags, &no->add_opts); |
| 2171 | } else if (plc->schema && plc->schema->pn_overr) { |
| 2172 | rc = plc->schema->pn_overr(node, plc->ctx->plugin_priv, &plc->filtered, &no->flags, &no->add_opts); |
| 2173 | } else { |
| 2174 | no = NULL; |
| 2175 | } |
| 2176 | if (trp_ext_is_present(lysc_tree, node)) { |
| 2177 | plug_ctx = plc->ctx; |
| 2178 | plc->ctx = NULL; |
| 2179 | tro_set_node_overr(lysc_tree, node, 0, plc); |
| 2180 | plc->ctx = plug_ctx; |
| 2181 | } |
| 2182 | } else { |
| 2183 | no = NULL; |
| 2184 | } |
| 2185 | |
| 2186 | if (rc) { |
| 2187 | plc->last_error = rc; |
| 2188 | no = NULL; |
| 2189 | } |
| 2190 | |
| 2191 | return no; |
| 2192 | } |
| 2193 | |
| 2194 | /** |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2195 | * @brief Get next sibling of the current node. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2196 | * |
| 2197 | * This is a general algorithm that is able to |
| 2198 | * work with lysp_node or lysc_node. |
| 2199 | * |
| 2200 | * @param[in] node points to lysp_node or lysc_node. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2201 | * @param[in] tc current tree context. |
| 2202 | * @return next sibling node. |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2203 | */ |
| 2204 | static const void * |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2205 | tro_next_sibling(const void *node, const struct trt_tree_ctx *tc) |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2206 | { |
| 2207 | struct tro_getters get; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2208 | struct trt_plugin_ctx plugin_ctx; |
| 2209 | const void *tmp, *parent, *sibl; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2210 | |
| 2211 | assert(node); |
| 2212 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2213 | get = tc->lysc_tree ? troc_init_getters() : trop_init_getters(); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2214 | |
| 2215 | if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) { |
| 2216 | if ((tmp = get.next(node))) { |
| 2217 | /* next action exists */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2218 | sibl = tmp; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2219 | } else if ((parent = get.parent(node))) { |
| 2220 | /* maybe if notif exists as sibling */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2221 | sibl = get.notifs(parent); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2222 | } else { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2223 | sibl = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2224 | } |
| 2225 | } else if (get.nodetype(node) & LYS_INPUT) { |
| 2226 | if ((parent = get.parent(node))) { |
| 2227 | /* if output action has data */ |
| 2228 | if (get.child(get.action_output(parent))) { |
| 2229 | /* then next sibling is output action */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2230 | sibl = get.action_output(parent); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2231 | } else { |
| 2232 | /* input action cannot have siblings other |
| 2233 | * than output action. |
| 2234 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2235 | sibl = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2236 | } |
| 2237 | } else { |
| 2238 | /* there is no way how to get output action */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2239 | sibl = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2240 | } |
| 2241 | } else if (get.nodetype(node) & LYS_OUTPUT) { |
| 2242 | /* output action cannot have siblings */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2243 | sibl = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2244 | } else if (get.nodetype(node) & LYS_NOTIF) { |
| 2245 | /* must have as a sibling only notif */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2246 | sibl = get.next(node); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2247 | } else { |
| 2248 | /* for rest of nodes */ |
| 2249 | if ((tmp = get.next(node))) { |
| 2250 | /* some sibling exists */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2251 | sibl = tmp; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2252 | } else if ((parent = get.parent(node))) { |
| 2253 | /* Action and notif are siblings too. |
| 2254 | * They can be reached through parent. |
| 2255 | */ |
| 2256 | if ((tmp = get.actions(parent))) { |
| 2257 | /* next sibling is action */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2258 | sibl = tmp; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2259 | } else if ((tmp = get.notifs(parent))) { |
| 2260 | /* next sibling is notif */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2261 | sibl = tmp; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2262 | } else { |
| 2263 | /* sibling not exists */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2264 | sibl = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2265 | } |
| 2266 | } else { |
| 2267 | /* sibling not exists */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2268 | sibl = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2272 | plugin_ctx = tc->plugin_ctx; |
| 2273 | if (sibl && tro_set_node_overr(tc->lysc_tree, sibl, 1, &plugin_ctx) && plugin_ctx.filtered) { |
| 2274 | return tro_next_sibling(sibl, tc); |
| 2275 | } |
| 2276 | |
| 2277 | return sibl; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | /** |
| 2281 | * @brief Get child of the current node. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2282 | * |
| 2283 | * This is a general algorithm that is able to |
| 2284 | * work with lysp_node or lysc_node. |
| 2285 | * |
| 2286 | * @param[in] node points to lysp_node or lysc_node. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2287 | * @param[in] tc current tree context. |
| 2288 | * @return child node. |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2289 | */ |
| 2290 | static const void * |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2291 | tro_next_child(const void *node, const struct trt_tree_ctx *tc) |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2292 | { |
| 2293 | struct tro_getters get; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2294 | struct trt_plugin_ctx plugin_ctx; |
| 2295 | const void *tmp, *child; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2296 | |
| 2297 | assert(node); |
| 2298 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2299 | get = tc->lysc_tree ? troc_init_getters() : trop_init_getters(); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2300 | |
| 2301 | if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) { |
| 2302 | if (get.child(get.action_input(node))) { |
| 2303 | /* go to LYS_INPUT */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2304 | child = get.action_input(node); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2305 | } else if (get.child(get.action_output(node))) { |
| 2306 | /* go to LYS_OUTPUT */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2307 | child = get.action_output(node); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2308 | } else { |
| 2309 | /* input action and output action have no data */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2310 | child = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2311 | } |
| 2312 | } else { |
| 2313 | if ((tmp = get.child(node))) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2314 | child = tmp; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2315 | } else { |
| 2316 | /* current node can't have children or has no children */ |
| 2317 | /* but maybe has some actions or notifs */ |
| 2318 | if ((tmp = get.actions(node))) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2319 | child = tmp; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2320 | } else if ((tmp = get.notifs(node))) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2321 | child = tmp; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2322 | } else { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2323 | child = NULL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2324 | } |
| 2325 | } |
| 2326 | } |
| 2327 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2328 | plugin_ctx = tc->plugin_ctx; |
| 2329 | if (child && tro_set_node_overr(tc->lysc_tree, child, 1, &plugin_ctx) && plugin_ctx.filtered) { |
| 2330 | return tro_next_sibling(child, tc); |
| 2331 | } |
| 2332 | |
| 2333 | return child; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2337 | * @brief Get new trt_parent_cache if we apply the transfer |
| 2338 | * to the child node in the tree. |
| 2339 | * @param[in] ca is parent cache for current node. |
| 2340 | * @param[in] tc contains current tree node. |
| 2341 | * @return Cache for the current node. |
| 2342 | */ |
| 2343 | static struct trt_parent_cache |
| 2344 | tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc) |
| 2345 | { |
| 2346 | struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE; |
| 2347 | |
| 2348 | if (!tc->lysc_tree) { |
| 2349 | const struct lysp_node *pn = tc->pn; |
| 2350 | |
| 2351 | ret.ancestor = |
| 2352 | pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT : |
| 2353 | pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT : |
| 2354 | pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF : |
| 2355 | ca.ancestor; |
| 2356 | |
| 2357 | ret.lys_status = |
| 2358 | pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags : |
| 2359 | ca.lys_status; |
| 2360 | |
| 2361 | ret.lys_config = |
| 2362 | ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */ |
| 2363 | ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R : |
| 2364 | pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags : |
| 2365 | ca.lys_config; |
| 2366 | |
| 2367 | ret.last_list = |
| 2368 | pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn : |
| 2369 | ca.last_list; |
| 2370 | } |
| 2371 | |
| 2372 | return ret; |
| 2373 | } |
| 2374 | |
| 2375 | /** |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2376 | * @brief Transformation of the Schema nodes flags to |
| 2377 | * Tree diagram \<status\>. |
| 2378 | * @param[in] flags is node's flags obtained from the tree. |
| 2379 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2380 | static char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2381 | tro_flags2status(uint16_t flags) |
| 2382 | { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2383 | return flags & LYS_STATUS_OBSLT ? "o" : |
| 2384 | flags & LYS_STATUS_DEPRC ? "x" : |
| 2385 | "+"; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | /** |
| 2389 | * @brief Transformation of the Schema nodes flags to Tree diagram |
| 2390 | * \<flags\> but more specifically 'ro' or 'rw'. |
| 2391 | * @param[in] flags is node's flags obtained from the tree. |
| 2392 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2393 | static char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2394 | tro_flags2config(uint16_t flags) |
| 2395 | { |
| 2396 | return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO : |
| 2397 | flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW : |
| 2398 | TRD_FLAGS_TYPE_EMPTY; |
| 2399 | } |
| 2400 | |
| 2401 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2402 | * @brief Print current node's iffeatures. |
| 2403 | * @param[in] tc is tree context. |
| 2404 | * @param[in,out] out is output handler. |
| 2405 | */ |
| 2406 | static void |
| 2407 | tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out) |
| 2408 | { |
| 2409 | const struct lysp_qname *iffs; |
| 2410 | |
aPiecek | bbc0293 | 2021-05-21 07:19:41 +0200 | [diff] [blame] | 2411 | if (tc->lysc_tree) { |
| 2412 | assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn)); |
| 2413 | iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures; |
| 2414 | } else { |
| 2415 | iffs = tc->pn->iffeatures; |
| 2416 | } |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2417 | LY_ARRAY_COUNT_TYPE i; |
| 2418 | |
| 2419 | LY_ARRAY_FOR(iffs, i) { |
| 2420 | if (i == 0) { |
| 2421 | ly_print_(out, "%s", iffs[i].str); |
| 2422 | } else { |
| 2423 | ly_print_(out, ",%s", iffs[i].str); |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | } |
| 2428 | |
| 2429 | /** |
| 2430 | * @brief Print current list's keys. |
| 2431 | * |
| 2432 | * Well, actually printing keys in the lysp_tree is trivial, |
| 2433 | * because char* points to all keys. However, special functions have |
| 2434 | * been reserved for this, because in principle the list of elements |
| 2435 | * can have more implementations. |
| 2436 | * |
| 2437 | * @param[in] tc is tree context. |
| 2438 | * @param[in,out] out is output handler. |
| 2439 | */ |
| 2440 | static void |
| 2441 | tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out) |
| 2442 | { |
| 2443 | const struct lysp_node_list *list; |
| 2444 | |
aPiecek | bbc0293 | 2021-05-21 07:19:41 +0200 | [diff] [blame] | 2445 | if (tc->lysc_tree) { |
| 2446 | assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn)); |
| 2447 | list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn); |
| 2448 | } else { |
| 2449 | list = (const struct lysp_node_list *)tc->pn; |
| 2450 | } |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2451 | assert(list->nodetype & LYS_LIST); |
| 2452 | |
| 2453 | if (trg_charptr_has_data(list->key)) { |
| 2454 | ly_print_(out, "%s", list->key); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2459 | * @brief Get address of the current node. |
| 2460 | * @param[in] tc contains current node. |
| 2461 | * @return Address of lysc_node or lysp_node, or NULL. |
| 2462 | */ |
| 2463 | static const void * |
| 2464 | tro_tree_ctx_get_node(const struct trt_tree_ctx *tc) |
| 2465 | { |
| 2466 | return tc->lysc_tree ? |
| 2467 | (const void *)tc->cn : |
| 2468 | (const void *)tc->pn; |
| 2469 | } |
| 2470 | |
| 2471 | /** |
| 2472 | * @brief Get address of current node's child. |
| 2473 | * @param[in,out] tc contains current node. |
| 2474 | */ |
| 2475 | static const void * |
| 2476 | tro_tree_ctx_get_child(const struct trt_tree_ctx *tc) |
| 2477 | { |
| 2478 | if (!tro_tree_ctx_get_node(tc)) { |
| 2479 | return NULL; |
| 2480 | } |
| 2481 | |
| 2482 | if (tc->lysc_tree) { |
| 2483 | return lysc_node_child(tc->cn); |
| 2484 | } else { |
| 2485 | return lysp_node_child(tc->pn); |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2490 | * @brief Get rpcs section if exists. |
| 2491 | * @param[in,out] tc is tree context. |
| 2492 | * @return Section representation if it exists. The @p tc is modified |
| 2493 | * and his pointer points to the first node in rpcs section. |
| 2494 | * @return Empty section representation otherwise. |
| 2495 | */ |
| 2496 | static struct trt_keyword_stmt |
| 2497 | tro_modi_get_rpcs(struct trt_tree_ctx *tc) |
| 2498 | { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2499 | assert(tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2500 | const void *actions; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2501 | struct trt_keyword_stmt ret = {0}; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2502 | |
| 2503 | if (tc->lysc_tree) { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2504 | actions = tc->cmod->rpcs; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2505 | if (actions) { |
| 2506 | tc->cn = actions; |
| 2507 | } |
| 2508 | } else { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2509 | actions = tc->pmod->rpcs; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2510 | if (actions) { |
| 2511 | tc->pn = actions; |
| 2512 | tc->tpn = tc->pn; |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | if (actions) { |
| 2517 | tc->section = TRD_SECT_RPCS; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2518 | ret.section_name = TRD_KEYWORD_RPC; |
| 2519 | ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2520 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2521 | |
| 2522 | return ret; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | /** |
| 2526 | * @brief Get notification section if exists |
| 2527 | * @param[in,out] tc is tree context. |
| 2528 | * @return Section representation if it exists. |
| 2529 | * The @p tc is modified and his pointer points to the |
| 2530 | * first node in notification section. |
| 2531 | * @return Empty section representation otherwise. |
| 2532 | */ |
| 2533 | static struct trt_keyword_stmt |
| 2534 | tro_modi_get_notifications(struct trt_tree_ctx *tc) |
| 2535 | { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2536 | assert(tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2537 | const void *notifs; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2538 | struct trt_keyword_stmt ret = {0}; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2539 | |
| 2540 | if (tc->lysc_tree) { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2541 | notifs = tc->cmod->notifs; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2542 | if (notifs) { |
| 2543 | tc->cn = notifs; |
| 2544 | } |
| 2545 | } else { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2546 | notifs = tc->pmod->notifs; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2547 | if (notifs) { |
| 2548 | tc->pn = notifs; |
| 2549 | tc->tpn = tc->pn; |
| 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | if (notifs) { |
| 2554 | tc->section = TRD_SECT_NOTIF; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2555 | ret.section_name = TRD_KEYWORD_NOTIF; |
| 2556 | ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2557 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2558 | |
| 2559 | return ret; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2562 | static struct trt_keyword_stmt |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2563 | tro_get_ext_section(struct trt_tree_ctx *tc, void *ext, struct lyspr_tree_ctx *plug_ctx) |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2564 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2565 | struct trt_keyword_stmt ret = {0}; |
| 2566 | struct lysc_ext_instance *ce = NULL; |
| 2567 | struct lysp_ext_instance *pe = NULL; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 2568 | |
| 2569 | if (tc->lysc_tree) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2570 | ce = ext; |
| 2571 | ret.section_name = ce->def->name; |
| 2572 | ret.argument = ce->argument; |
| 2573 | ret.has_node = plug_ctx->schemas->ctree ? 1 : 0; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 2574 | } else { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2575 | pe = ext; |
| 2576 | ret.section_name = pe->def->name; |
| 2577 | ret.argument = pe->argument; |
| 2578 | ret.has_node = plug_ctx->schemas->ptree ? 1 : 0; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 2579 | } |
| 2580 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2581 | return ret; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | /** |
| 2585 | * @brief Get name of the module. |
| 2586 | * @param[in] tc is context of the tree. |
| 2587 | */ |
| 2588 | static struct trt_keyword_stmt |
| 2589 | tro_read_module_name(const struct trt_tree_ctx *tc) |
| 2590 | { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2591 | assert(tc); |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2592 | struct trt_keyword_stmt ret; |
| 2593 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2594 | ret.section_name = !tc->lysc_tree && tc->pmod->is_submod ? |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2595 | TRD_KEYWORD_SUBMODULE : |
| 2596 | TRD_KEYWORD_MODULE; |
| 2597 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2598 | ret.argument = !tc->lysc_tree ? |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2599 | LYSP_MODULE_NAME(tc->pmod) : |
| 2600 | tc->cmod->mod->name; |
| 2601 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2602 | ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0; |
| 2603 | |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 2604 | return ret; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2605 | } |
| 2606 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2607 | static ly_bool |
| 2608 | tro_read_if_sibling_exists(const struct trt_tree_ctx *tc) |
| 2609 | { |
| 2610 | const void *parent; |
| 2611 | |
| 2612 | if (tc->lysc_tree) { |
| 2613 | parent = troc_parent(tc->cn); |
| 2614 | } else { |
| 2615 | parent = trop_parent(tc->pn); |
| 2616 | } |
| 2617 | |
| 2618 | return parent ? 1 : 0; |
| 2619 | } |
| 2620 | |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 2621 | /** |
| 2622 | * @brief Create implicit "case" node as parent of @p node. |
| 2623 | * @param[in] node child of implicit case node. |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 2624 | * @param[out] case_node created case node. |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 2625 | */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 2626 | static void |
| 2627 | tro_create_implicit_case_node(const struct trt_node *node, struct trt_node *case_node) |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 2628 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 2629 | case_node->status = node->status; |
| 2630 | case_node->flags = TRD_FLAGS_TYPE_EMPTY; |
| 2631 | case_node->name.type = TRD_NODE_CASE; |
| 2632 | case_node->name.keys = node->name.keys; |
| 2633 | case_node->name.module_prefix = node->name.module_prefix; |
| 2634 | case_node->name.str = node->name.str; |
| 2635 | case_node->name.opts = node->name.opts; |
| 2636 | case_node->name.add_opts = node->name.add_opts; |
| 2637 | case_node->type = TRP_EMPTY_TRT_TYPE; |
| 2638 | case_node->iffeatures = TRP_EMPTY_TRT_IFFEATURES; |
| 2639 | case_node->last_one = node->last_one; |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2642 | /********************************************************************** |
| 2643 | * Definition of trop reading functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2644 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2645 | |
| 2646 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2647 | * @brief Check if list statement has keys. |
| 2648 | * @param[in] pn is pointer to the list. |
| 2649 | * @return 1 if has keys, otherwise 0. |
| 2650 | */ |
| 2651 | static ly_bool |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2652 | trop_list_has_keys(const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2653 | { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2654 | return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | /** |
| 2658 | * @brief Check if it contains at least one feature. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 2659 | * @param[in] pn is current node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2660 | * @return 1 if has if-features, otherwise 0. |
| 2661 | */ |
| 2662 | static ly_bool |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2663 | trop_node_has_iffeature(const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2664 | { |
| 2665 | LY_ARRAY_COUNT_TYPE u; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2666 | const struct lysp_qname *iffs; |
| 2667 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2668 | ly_bool ret = 0; |
| 2669 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2670 | iffs = pn->iffeatures; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2671 | LY_ARRAY_FOR(iffs, u) { |
| 2672 | ret = 1; |
| 2673 | break; |
| 2674 | } |
| 2675 | return ret; |
| 2676 | } |
| 2677 | |
| 2678 | /** |
| 2679 | * @brief Find out if leaf is also the key in last list. |
| 2680 | * @param[in] pn is pointer to leaf. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2681 | * @param[in] ca_last_list is pointer to last visited list. |
| 2682 | * Obtained from trt_parent_cache. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2683 | * @return 1 if leaf is also the key, otherwise 0. |
| 2684 | */ |
| 2685 | static ly_bool |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2686 | trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2687 | { |
| 2688 | const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn; |
| 2689 | const struct lysp_node_list *list = ca_last_list; |
| 2690 | |
| 2691 | if (!list) { |
| 2692 | return 0; |
| 2693 | } |
| 2694 | return trg_charptr_has_data(list->key) ? |
| 2695 | trg_word_is_present(list->key, leaf->name, ' ') : 0; |
| 2696 | } |
| 2697 | |
| 2698 | /** |
| 2699 | * @brief Check if container's type is presence. |
| 2700 | * @param[in] pn is pointer to container. |
| 2701 | * @return 1 if container has presence statement, otherwise 0. |
| 2702 | */ |
| 2703 | static ly_bool |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2704 | trop_container_has_presence(const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2705 | { |
| 2706 | return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence); |
| 2707 | } |
| 2708 | |
| 2709 | /** |
| 2710 | * @brief Get leaflist's path without lysp_node type control. |
| 2711 | * @param[in] pn is pointer to the leaflist. |
| 2712 | */ |
| 2713 | static const char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2714 | trop_leaflist_refpath(const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2715 | { |
| 2716 | const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn; |
| 2717 | |
| 2718 | return list->type.path ? list->type.path->expr : NULL; |
| 2719 | } |
| 2720 | |
| 2721 | /** |
| 2722 | * @brief Get leaflist's type name without lysp_node type control. |
| 2723 | * @param[in] pn is pointer to the leaflist. |
| 2724 | */ |
| 2725 | static const char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2726 | trop_leaflist_type_name(const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2727 | { |
| 2728 | const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn; |
| 2729 | |
| 2730 | return list->type.name; |
| 2731 | } |
| 2732 | |
| 2733 | /** |
| 2734 | * @brief Get leaf's path without lysp_node type control. |
| 2735 | * @param[in] pn is pointer to the leaf node. |
| 2736 | */ |
| 2737 | static const char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2738 | trop_leaf_refpath(const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2739 | { |
| 2740 | const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn; |
| 2741 | |
| 2742 | return leaf->type.path ? leaf->type.path->expr : NULL; |
| 2743 | } |
| 2744 | |
| 2745 | /** |
| 2746 | * @brief Get leaf's type name without lysp_node type control. |
| 2747 | * @param[in] pn is pointer to the leaf's type name. |
| 2748 | */ |
| 2749 | static const char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2750 | trop_leaf_type_name(const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2751 | { |
| 2752 | const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn; |
| 2753 | |
| 2754 | return leaf->type.name; |
| 2755 | } |
| 2756 | |
| 2757 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2758 | * @brief Get pointer to data using node type specification |
| 2759 | * and getter function. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2760 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2761 | * @param[in] flags is node type specification. |
| 2762 | * If it is the correct node, the getter function is called. |
| 2763 | * @param[in] f is getter function which provides the desired |
| 2764 | * char pointer from the structure. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2765 | * @param[in] pn pointer to node. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2766 | * @return NULL if node has wrong type or getter function return |
| 2767 | * pointer to NULL. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2768 | * @return Pointer to desired char pointer obtained from the node. |
| 2769 | */ |
| 2770 | static const char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2771 | trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2772 | { |
| 2773 | if (pn->nodetype & flags) { |
| 2774 | const char *ret = f(pn); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 2775 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2776 | return trg_charptr_has_data(ret) ? ret : NULL; |
| 2777 | } else { |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2783 | * @brief Resolve \<status\> of the current node. |
| 2784 | * @param[in] nodetype is node's type obtained from the tree. |
| 2785 | * @param[in] flags is node's flags obtained from the tree. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2786 | * @param[in] ca_lys_status is inherited status obtained from trt_parent_cache. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2787 | * @return The status type. |
| 2788 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2789 | static char * |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2790 | trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2791 | { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2792 | if (nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2793 | /* LYS_INPUT and LYS_OUTPUT is special case */ |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2794 | return tro_flags2status(ca_lys_status); |
| 2795 | /* if ancestor's status is deprc or obslt |
| 2796 | * and also node's status is not set |
| 2797 | */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2798 | } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) { |
| 2799 | /* get ancestor's status */ |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2800 | return tro_flags2status(ca_lys_status); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2801 | } else { |
| 2802 | /* else get node's status */ |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2803 | return tro_flags2status(flags); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | /** |
| 2808 | * @brief Resolve \<flags\> of the current node. |
| 2809 | * @param[in] nodetype is node's type obtained from the tree. |
| 2810 | * @param[in] flags is node's flags obtained from the tree. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2811 | * @param[in] ca_ancestor is ancestor type obtained from trt_parent_cache. |
| 2812 | * @param[in] ca_lys_config is inherited config item obtained from trt_parent_cache. |
| 2813 | * @param[in] no Override structure for flags. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2814 | * @return The flags type. |
| 2815 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2816 | static const char * |
| 2817 | trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config, |
| 2818 | struct lyplg_ext_sprinter_tree_node_override *no) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2819 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2820 | if (no && no->flags) { |
| 2821 | return no->flags; |
| 2822 | } else if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2823 | return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS; |
| 2824 | } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) { |
| 2825 | return TRD_FLAGS_TYPE_RO; |
| 2826 | } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) { |
| 2827 | return TRD_FLAGS_TYPE_RO; |
| 2828 | } else if (nodetype & LYS_NOTIF) { |
| 2829 | return TRD_FLAGS_TYPE_NOTIF; |
| 2830 | } else if (nodetype & LYS_USES) { |
| 2831 | return TRD_FLAGS_TYPE_USES_OF_GROUPING; |
| 2832 | } else if (nodetype & (LYS_RPC | LYS_ACTION)) { |
| 2833 | return TRD_FLAGS_TYPE_RPC; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2834 | } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2835 | /* config is not set. Look at ancestor's config */ |
| 2836 | return tro_flags2config(ca_lys_config); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2837 | } else { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2838 | return tro_flags2config(flags); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | /** |
| 2843 | * @brief Resolve node type of the current node. |
| 2844 | * @param[in] pn is pointer to the current node in the tree. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2845 | * @param[in] ca_last_list is pointer to the last visited list. Obtained from the trt_parent_cache. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2846 | * @param[out] type Resolved type of node. |
| 2847 | * @param[out] opts Resolved opts of node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2848 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2849 | static void |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2850 | trop_resolve_node_opts(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list, trt_node_type *type, |
| 2851 | const char **opts) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2852 | { |
| 2853 | if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2854 | *type = TRD_NODE_ELSE; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2855 | } else if (pn->nodetype & LYS_CASE) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2856 | *type = TRD_NODE_CASE; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2857 | } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2858 | *type = TRD_NODE_CHOICE; |
| 2859 | *opts = TRD_NODE_OPTIONAL; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2860 | } else if (pn->nodetype & LYS_CHOICE) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2861 | *type = TRD_NODE_CHOICE; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2862 | } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2863 | *opts = TRD_NODE_CONTAINER; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2864 | } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2865 | *opts = TRD_NODE_LISTLEAFLIST; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2866 | } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2867 | *opts = TRD_NODE_OPTIONAL; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2868 | } else if ((pn->nodetype & LYS_LEAF) && !(pn->flags & LYS_MAND_TRUE) && (!trop_leaf_is_key(pn, ca_last_list))) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2869 | *opts = TRD_NODE_OPTIONAL; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2870 | } else { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 2871 | *type = TRD_NODE_ELSE; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | /** |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2876 | * @brief Resolve \<type\> of the current node. |
| 2877 | * @param[in] pn is current node. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2878 | * @return Resolved type. |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2879 | */ |
| 2880 | static struct trt_type |
| 2881 | trop_resolve_type(const struct lysp_node *pn) |
| 2882 | { |
| 2883 | const char *tmp = NULL; |
| 2884 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2885 | if (!pn) { |
| 2886 | return TRP_EMPTY_TRT_TYPE; |
| 2887 | } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2888 | return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp); |
| 2889 | } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) { |
| 2890 | return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp); |
| 2891 | } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) { |
| 2892 | return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp); |
| 2893 | } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) { |
| 2894 | return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp); |
| 2895 | } else if (pn->nodetype == LYS_ANYDATA) { |
| 2896 | return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata"); |
| 2897 | } else if (pn->nodetype & LYS_ANYXML) { |
| 2898 | return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml"); |
| 2899 | } else { |
| 2900 | return TRP_EMPTY_TRT_TYPE; |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2905 | * @brief Resolve iffeatures. |
| 2906 | * |
| 2907 | * @param[in] pn is current parsed node. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2908 | * @return Resolved iffeatures. |
| 2909 | */ |
| 2910 | static struct trt_iffeatures |
| 2911 | trop_resolve_iffeatures(const struct lysp_node *pn) |
| 2912 | { |
| 2913 | struct trt_iffeatures iff; |
| 2914 | |
| 2915 | if (pn && trop_node_has_iffeature(pn)) { |
| 2916 | iff.type = TRD_IFF_PRESENT; |
| 2917 | iff.str = NULL; |
| 2918 | } else { |
| 2919 | iff.type = TRD_IFF_NON_PRESENT; |
| 2920 | iff.str = NULL; |
| 2921 | } |
| 2922 | |
| 2923 | return iff; |
| 2924 | } |
| 2925 | |
| 2926 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2927 | * @brief Transformation of current lysp_node to struct trt_node. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2928 | * @param[in] ca contains stored important data |
| 2929 | * when browsing the tree downwards. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2930 | * @param[in] tc is context of the tree. |
| 2931 | */ |
| 2932 | static struct trt_node |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2933 | trop_read_node(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2934 | { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2935 | const struct lysp_node *pn; |
| 2936 | struct trt_node ret; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2937 | struct lyplg_ext_sprinter_tree_node_override *no; |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2938 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2939 | assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN); |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2940 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2941 | no = tro_set_node_overr(tc->lysc_tree, tc->pn, 1, &tc->plugin_ctx); |
| 2942 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2943 | pn = tc->pn; |
| 2944 | ret = TRP_EMPTY_NODE; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2945 | |
| 2946 | /* <status> */ |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2947 | ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2948 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2949 | /* <flags> */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2950 | ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config, no); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2951 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2952 | /* set type of the node */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2953 | trop_resolve_node_opts(pn, ca.last_list, &ret.name.type, &ret.name.opts); |
| 2954 | ret.name.add_opts = no && no->add_opts ? no->add_opts : NULL; |
aPiecek | bca5777 | 2022-10-13 13:51:59 +0200 | [diff] [blame] | 2955 | ret.name.keys = (tc->pn->nodetype & LYS_LIST) && trop_list_has_keys(tc->pn); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2956 | |
aPiecek | 34fa377 | 2021-05-21 12:35:46 +0200 | [diff] [blame] | 2957 | /* The parsed tree is not compiled, so no node can be augmented |
| 2958 | * from another module. This means that nodes from the parsed tree |
| 2959 | * will never have the prefix. |
| 2960 | */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2961 | ret.name.module_prefix = NULL; |
| 2962 | |
| 2963 | /* set node's name */ |
| 2964 | ret.name.str = pn->name; |
| 2965 | |
| 2966 | /* <type> */ |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2967 | ret.type = trop_resolve_type(pn); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2968 | |
| 2969 | /* <iffeature> */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2970 | ret.iffeatures = trop_resolve_iffeatures(pn); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2971 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2972 | ret.last_one = !tro_next_sibling(pn, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2973 | |
| 2974 | return ret; |
| 2975 | } |
| 2976 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2977 | /** |
| 2978 | * @brief Find out if the current node has siblings. |
| 2979 | * @param[in] tc is context of the tree. |
| 2980 | * @return 1 if sibling exists otherwise 0. |
| 2981 | */ |
| 2982 | static ly_bool |
| 2983 | trop_read_if_sibling_exists(const struct trt_tree_ctx *tc) |
| 2984 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 2985 | return tro_next_sibling(tc->pn, tc) != NULL; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 2986 | } |
| 2987 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2988 | /********************************************************************** |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 2989 | * Modify trop getters |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2990 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2991 | |
| 2992 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2993 | * @brief Change current node pointer to its parent |
| 2994 | * but only if parent exists. |
| 2995 | * @param[in,out] tc is tree context. |
| 2996 | * Contains pointer to the current node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2997 | * @return 1 if the node had parents and the change was successful. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 2998 | * @return 0 if the node did not have parents. |
| 2999 | * The pointer to the current node did not change. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3000 | */ |
| 3001 | static ly_bool |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3002 | trop_modi_parent(struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3003 | { |
| 3004 | assert(tc && tc->pn); |
| 3005 | /* If no parent exists, stay in actual node. */ |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3006 | if ((tc->pn != tc->tpn) && (tc->pn->parent)) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3007 | tc->pn = tc->pn->parent; |
| 3008 | return 1; |
| 3009 | } else { |
| 3010 | return 0; |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3015 | * @brief Change the current node pointer to its child |
| 3016 | * but only if exists. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3017 | * @param[in] ca contains inherited data from ancestors. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3018 | * @param[in,out] tc is context of the tree. |
| 3019 | * Contains pointer to the current node. |
| 3020 | * @return Non-empty \<node\> representation of the current |
| 3021 | * node's child. The @p tc is modified. |
| 3022 | * @return Empty \<node\> representation if child don't exists. |
| 3023 | * The @p tc is not modified. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3024 | */ |
| 3025 | static struct trt_node |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3026 | trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3027 | { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3028 | const struct lysp_node *tmp; |
| 3029 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3030 | assert(tc && tc->pn); |
| 3031 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3032 | if ((tmp = tro_next_child(tc->pn, tc))) { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3033 | tc->pn = tmp; |
aPiecek | 67521c2 | 2022-10-19 09:15:02 +0200 | [diff] [blame] | 3034 | return trop_read_node(ca, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3035 | } else { |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3036 | return TRP_EMPTY_NODE; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3041 | * @brief Change the pointer to the current node to its next sibling |
| 3042 | * only if exists. |
| 3043 | * @param[in] ca contains inherited data from ancestors. |
| 3044 | * @param[in,out] tc is tree context. |
| 3045 | * Contains pointer to the current node. |
| 3046 | * @return Non-empty \<node\> representation if sibling exists. |
| 3047 | * The @p tc is modified. |
| 3048 | * @return Empty \<node\> representation otherwise. |
| 3049 | * The @p tc is not modified. |
| 3050 | */ |
| 3051 | static struct trt_node |
| 3052 | trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
| 3053 | { |
| 3054 | const struct lysp_node *pn; |
| 3055 | |
| 3056 | assert(tc && tc->pn); |
| 3057 | |
| 3058 | pn = tro_next_sibling(tc->pn, tc); |
| 3059 | |
| 3060 | if (pn) { |
| 3061 | if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_PLUG_DATA)) { |
| 3062 | tc->tpn = pn; |
| 3063 | } |
| 3064 | tc->pn = pn; |
| 3065 | return trop_read_node(ca, tc); |
| 3066 | } else { |
| 3067 | return TRP_EMPTY_NODE; |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3072 | * @brief Change the current node pointer to the first child of node's |
| 3073 | * parent. If current node is already first sibling/child then nothing |
| 3074 | * will change. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3075 | * @param[in] ca Settings of parent. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3076 | * @param[in,out] tc is tree context. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3077 | * @return node for printing. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3078 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3079 | static struct trt_node |
| 3080 | trop_modi_first_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3081 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3082 | struct trt_node node; |
| 3083 | |
| 3084 | assert(tc && tc->pn); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3085 | |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3086 | if (trop_modi_parent(tc)) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3087 | node = trop_modi_next_child(ca, tc); |
| 3088 | } else if (tc->plugin_ctx.schema) { |
| 3089 | tc->pn = tc->plugin_ctx.schema->ptree; |
| 3090 | tc->tpn = tc->pn; |
| 3091 | node = trop_read_node(ca, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3092 | } else { |
| 3093 | /* current node is top-node */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3094 | switch (tc->section) { |
| 3095 | case TRD_SECT_MODULE: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3096 | tc->pn = tc->pmod->data; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3097 | tc->tpn = tc->pn; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3098 | break; |
| 3099 | case TRD_SECT_AUGMENT: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3100 | tc->pn = (const struct lysp_node *)tc->pmod->augments; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3101 | tc->tpn = tc->pn; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3102 | break; |
| 3103 | case TRD_SECT_RPCS: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3104 | tc->pn = (const struct lysp_node *)tc->pmod->rpcs; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3105 | tc->tpn = tc->pn; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3106 | break; |
| 3107 | case TRD_SECT_NOTIF: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3108 | tc->pn = (const struct lysp_node *)tc->pmod->notifs; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3109 | tc->tpn = tc->pn; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3110 | break; |
| 3111 | case TRD_SECT_GROUPING: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3112 | tc->pn = (const struct lysp_node *)tc->pmod->groupings; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3113 | tc->tpn = tc->pn; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3114 | break; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3115 | case TRD_SECT_PLUG_DATA: |
| 3116 | /* Nothing to do. */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3117 | break; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3118 | default: |
| 3119 | assert(0); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3120 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3121 | node = trop_read_node(ca, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3122 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3123 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3124 | if (tc->plugin_ctx.filtered) { |
| 3125 | node = trop_modi_next_sibling(ca, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3126 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3127 | |
| 3128 | return node; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | /** |
| 3132 | * @brief Get next (or first) augment section if exists. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3133 | * @param[in,out] tc is tree context. It is modified and his current |
| 3134 | * node is set to the lysp_node_augment. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3135 | * @return Section's representation if (next augment) section exists. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3136 | * @return Empty section structure otherwise. |
| 3137 | */ |
| 3138 | static struct trt_keyword_stmt |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3139 | trop_modi_next_augment(struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3140 | { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3141 | assert(tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3142 | const struct lysp_node_augment *augs; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3143 | struct trt_keyword_stmt ret = {0}; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3144 | |
| 3145 | /* if next_augment func was called for the first time */ |
| 3146 | if (tc->section != TRD_SECT_AUGMENT) { |
| 3147 | tc->section = TRD_SECT_AUGMENT; |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3148 | augs = tc->pmod->augments; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3149 | } else { |
| 3150 | /* get augment sibling from top-node pointer */ |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 3151 | augs = (const struct lysp_node_augment *)tc->tpn->next; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3152 | } |
| 3153 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 3154 | if (augs) { |
| 3155 | tc->pn = &augs->node; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3156 | tc->tpn = tc->pn; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3157 | ret.section_name = TRD_KEYWORD_AUGMENT; |
| 3158 | ret.argument = augs->nodeid; |
| 3159 | ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3160 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3161 | |
| 3162 | return ret; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3166 | * @brief Get next (or first) grouping section if exists |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3167 | * @param[in,out] tc is tree context. It is modified and his current |
| 3168 | * node is set to the lysp_node_grp. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3169 | * @return The next (or first) section representation if it exists. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3170 | * @return Empty section representation otherwise. |
| 3171 | */ |
| 3172 | static struct trt_keyword_stmt |
aPiecek | ef1e58e | 2021-04-19 13:19:44 +0200 | [diff] [blame] | 3173 | trop_modi_next_grouping(struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3174 | { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3175 | assert(tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3176 | const struct lysp_node_grp *grps; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3177 | struct trt_keyword_stmt ret = {0}; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3178 | |
| 3179 | if (tc->section != TRD_SECT_GROUPING) { |
| 3180 | tc->section = TRD_SECT_GROUPING; |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3181 | grps = tc->pmod->groupings; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3182 | } else { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 3183 | grps = (const struct lysp_node_grp *)tc->tpn->next; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3184 | } |
| 3185 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 3186 | if (grps) { |
| 3187 | tc->pn = &grps->node; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3188 | tc->tpn = tc->pn; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3189 | ret.section_name = TRD_KEYWORD_GROUPING; |
| 3190 | ret.argument = grps->name; |
| 3191 | ret.has_node = tro_tree_ctx_get_child(tc) ? 1 : 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3192 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3193 | |
| 3194 | return ret; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3195 | } |
| 3196 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3197 | /********************************************************************** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3198 | * Definition of troc reading functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3199 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3200 | |
| 3201 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3202 | * @copydoc trop_read_if_sibling_exists |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3203 | */ |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3204 | static ly_bool |
| 3205 | troc_read_if_sibling_exists(const struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3206 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3207 | return tro_next_sibling(tc->cn, tc) != NULL; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3208 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3209 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3210 | /** |
| 3211 | * @brief Resolve \<flags\> of the current node. |
| 3212 | * |
| 3213 | * Use this function only if trt_tree_ctx.lysc_tree is true. |
| 3214 | * |
| 3215 | * @param[in] nodetype is current lysc_node.nodetype. |
| 3216 | * @param[in] flags is current lysc_node.flags. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3217 | * @param[in] no Override structure for flags. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3218 | * @return The flags type. |
| 3219 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3220 | static const char * |
| 3221 | troc_resolve_flags(uint16_t nodetype, uint16_t flags, struct lyplg_ext_sprinter_tree_node_override *no) |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3222 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3223 | if (no && no->flags) { |
| 3224 | return no->flags; |
| 3225 | } else if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) { |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3226 | return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS; |
| 3227 | } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) { |
| 3228 | return TRD_FLAGS_TYPE_RO; |
| 3229 | } else if (nodetype & LYS_IS_NOTIF) { |
| 3230 | return TRD_FLAGS_TYPE_RO; |
| 3231 | } else if (nodetype & LYS_NOTIF) { |
| 3232 | return TRD_FLAGS_TYPE_NOTIF; |
| 3233 | } else if (nodetype & LYS_USES) { |
| 3234 | return TRD_FLAGS_TYPE_USES_OF_GROUPING; |
| 3235 | } else if (nodetype & (LYS_RPC | LYS_ACTION)) { |
| 3236 | return TRD_FLAGS_TYPE_RPC; |
| 3237 | } else { |
| 3238 | return tro_flags2config(flags); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3239 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3243 | * @brief Resolve node type of the current node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3244 | * |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3245 | * Use this function only if trt_tree_ctx.lysc_tree is true. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3246 | * |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3247 | * @param[in] nodetype is current lysc_node.nodetype. |
| 3248 | * @param[in] flags is current lysc_node.flags. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3249 | * @param[out] type Resolved type of node. |
| 3250 | * @param[out] opts Resolved opts. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3251 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3252 | static void |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3253 | troc_resolve_node_opts(uint16_t nodetype, uint16_t flags, trt_node_type *type, const char **opts) |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3254 | { |
| 3255 | if (nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3256 | *type = TRD_NODE_ELSE; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3257 | } else if (nodetype & LYS_CASE) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3258 | *type = TRD_NODE_CASE; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3259 | } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3260 | *type = TRD_NODE_CHOICE; |
| 3261 | *opts = TRD_NODE_OPTIONAL; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3262 | } else if (nodetype & LYS_CHOICE) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3263 | *type = TRD_NODE_CHOICE; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3264 | } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3265 | *opts = TRD_NODE_CONTAINER; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3266 | } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3267 | *opts = TRD_NODE_LISTLEAFLIST; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3268 | } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3269 | *opts = TRD_NODE_OPTIONAL; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3270 | } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3271 | *opts = TRD_NODE_OPTIONAL; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3272 | } else { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3273 | *type = TRD_NODE_ELSE; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3278 | * @brief Resolve prefix (\<prefix\>:\<name\>) of node that has been |
aPiecek | 34fa377 | 2021-05-21 12:35:46 +0200 | [diff] [blame] | 3279 | * placed from another module via an augment statement. |
| 3280 | * |
| 3281 | * @param[in] cn is current compiled node. |
| 3282 | * @param[in] current_compiled_module is module whose nodes are |
| 3283 | * currently being printed. |
| 3284 | * @return Prefix of foreign module or NULL. |
| 3285 | */ |
| 3286 | static const char * |
| 3287 | troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module) |
| 3288 | { |
| 3289 | const struct lys_module *node_module; |
| 3290 | const char *ret = NULL; |
| 3291 | |
| 3292 | node_module = cn->module; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3293 | if (!node_module || !current_compiled_module) { |
| 3294 | return NULL; |
| 3295 | } else if (node_module->compiled != current_compiled_module) { |
aPiecek | 34fa377 | 2021-05-21 12:35:46 +0200 | [diff] [blame] | 3296 | ret = node_module->prefix; |
| 3297 | } |
| 3298 | |
| 3299 | return ret; |
| 3300 | } |
| 3301 | |
| 3302 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3303 | * @brief Transformation of current lysc_node to struct trt_node. |
| 3304 | * @param[in] ca is not used. |
| 3305 | * @param[in] tc is context of the tree. |
| 3306 | */ |
| 3307 | static struct trt_node |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3308 | troc_read_node(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3309 | { |
| 3310 | (void) ca; |
| 3311 | const struct lysc_node *cn; |
| 3312 | struct trt_node ret; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3313 | struct lyplg_ext_sprinter_tree_node_override *no; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3314 | |
aPiecek | 082c7dc | 2021-05-20 08:55:07 +0200 | [diff] [blame] | 3315 | assert(tc && tc->cn); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3316 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3317 | no = tro_set_node_overr(tc->lysc_tree, tc->cn, 1, &tc->plugin_ctx); |
| 3318 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3319 | cn = tc->cn; |
| 3320 | ret = TRP_EMPTY_NODE; |
| 3321 | |
| 3322 | /* <status> */ |
| 3323 | ret.status = tro_flags2status(cn->flags); |
| 3324 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3325 | /* <flags> */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3326 | ret.flags = troc_resolve_flags(cn->nodetype, cn->flags, no); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3327 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3328 | /* set type of the node */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3329 | troc_resolve_node_opts(cn->nodetype, cn->flags, &ret.name.type, &ret.name.opts); |
| 3330 | ret.name.add_opts = no && no->add_opts ? no->add_opts : NULL; |
aPiecek | bca5777 | 2022-10-13 13:51:59 +0200 | [diff] [blame] | 3331 | ret.name.keys = (cn->nodetype & LYS_LIST) && !(cn->flags & LYS_KEYLESS); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3332 | |
aPiecek | 34fa377 | 2021-05-21 12:35:46 +0200 | [diff] [blame] | 3333 | /* <prefix> */ |
| 3334 | ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3335 | |
| 3336 | /* set node's name */ |
| 3337 | ret.name.str = cn->name; |
| 3338 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3339 | /* <type> */ |
| 3340 | ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn)); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3341 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3342 | /* <iffeature> */ |
| 3343 | ret.iffeatures = trop_resolve_iffeatures(TRP_TREE_CTX_GET_LYSP_NODE(cn)); |
aPiecek | 082c7dc | 2021-05-20 08:55:07 +0200 | [diff] [blame] | 3344 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3345 | ret.last_one = !tro_next_sibling(cn, tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3346 | |
| 3347 | return ret; |
| 3348 | } |
| 3349 | |
| 3350 | /********************************************************************** |
| 3351 | * Modify troc getters |
| 3352 | *********************************************************************/ |
| 3353 | |
| 3354 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 3355 | * @copydoc ::trop_modi_parent() |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3356 | */ |
| 3357 | static ly_bool |
| 3358 | troc_modi_parent(struct trt_tree_ctx *tc) |
| 3359 | { |
| 3360 | assert(tc && tc->cn); |
| 3361 | /* If no parent exists, stay in actual node. */ |
| 3362 | if (tc->cn->parent) { |
| 3363 | tc->cn = tc->cn->parent; |
| 3364 | return 1; |
| 3365 | } else { |
| 3366 | return 0; |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 3371 | * @copydoc ::trop_modi_next_sibling() |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3372 | */ |
| 3373 | static struct trt_node |
| 3374 | troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
| 3375 | { |
| 3376 | const struct lysc_node *cn; |
| 3377 | |
| 3378 | assert(tc && tc->cn); |
| 3379 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3380 | cn = tro_next_sibling(tc->cn, tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3381 | |
| 3382 | /* if next sibling exists */ |
| 3383 | if (cn) { |
| 3384 | /* update trt_tree_ctx */ |
| 3385 | tc->cn = cn; |
| 3386 | return troc_read_node(ca, tc); |
| 3387 | } else { |
| 3388 | return TRP_EMPTY_NODE; |
| 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | /** |
| 3393 | * @copydoc trop_modi_next_child() |
| 3394 | */ |
| 3395 | static struct trt_node |
| 3396 | troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
| 3397 | { |
| 3398 | const struct lysc_node *tmp; |
| 3399 | |
| 3400 | assert(tc && tc->cn); |
| 3401 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3402 | if ((tmp = tro_next_child(tc->cn, tc))) { |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3403 | tc->cn = tmp; |
| 3404 | return troc_read_node(ca, tc); |
| 3405 | } else { |
| 3406 | return TRP_EMPTY_NODE; |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | /** |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 3411 | * @copydoc ::trop_modi_first_sibling() |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3412 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3413 | static struct trt_node |
| 3414 | troc_modi_first_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3415 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3416 | struct trt_node node; |
| 3417 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3418 | assert(tc && tc->cn); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3419 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3420 | if (troc_modi_parent(tc)) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3421 | node = troc_modi_next_child(ca, tc); |
| 3422 | } else if (tc->plugin_ctx.schema) { |
| 3423 | tc->cn = tc->plugin_ctx.schema->ctree; |
| 3424 | node = troc_read_node(ca, tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3425 | } else { |
| 3426 | /* current node is top-node */ |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3427 | switch (tc->section) { |
| 3428 | case TRD_SECT_MODULE: |
aPiecek | e4362cc | 2023-03-29 12:07:41 +0200 | [diff] [blame] | 3429 | tc->cn = tc->cn->module->compiled->data; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3430 | break; |
| 3431 | case TRD_SECT_RPCS: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3432 | tc->cn = (const struct lysc_node *)tc->cmod->rpcs; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3433 | break; |
| 3434 | case TRD_SECT_NOTIF: |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 3435 | tc->cn = (const struct lysc_node *)tc->cmod->notifs; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3436 | break; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3437 | case TRD_SECT_PLUG_DATA: |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 3438 | /* nothing to do */ |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3439 | break; |
| 3440 | default: |
| 3441 | assert(0); |
| 3442 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3443 | node = troc_read_node(ca, tc); |
aPiecek | 8f1073c | 2022-10-17 16:44:49 +0200 | [diff] [blame] | 3444 | } |
| 3445 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3446 | if (tc->plugin_ctx.filtered) { |
| 3447 | node = troc_modi_next_sibling(ca, tc); |
aPiecek | 8f1073c | 2022-10-17 16:44:49 +0200 | [diff] [blame] | 3448 | } |
| 3449 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3450 | return node; |
aPiecek | 8f1073c | 2022-10-17 16:44:49 +0200 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3454 | * Definition of tree browsing functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3455 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3456 | |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3457 | static uint32_t |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3458 | trb_gap_to_opts(const struct trt_node *node) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3459 | { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3460 | uint32_t len = 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3461 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3462 | if (node->name.keys) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3463 | return 0; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3464 | } |
| 3465 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3466 | if (node->flags) { |
| 3467 | len += strlen(node->flags); |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3468 | /* space between flags and name */ |
| 3469 | len += 1; |
| 3470 | } else { |
| 3471 | /* space between -- and name */ |
| 3472 | len += 1; |
| 3473 | } |
| 3474 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3475 | switch (node->name.type) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3476 | case TRD_NODE_CASE: |
| 3477 | /* ':' is already counted. Plus parentheses. */ |
| 3478 | len += 2; |
| 3479 | break; |
| 3480 | case TRD_NODE_CHOICE: |
| 3481 | /* Plus parentheses. */ |
| 3482 | len += 2; |
| 3483 | break; |
| 3484 | default: |
| 3485 | break; |
| 3486 | } |
| 3487 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3488 | if (node->name.module_prefix) { |
aPiecek | c4da6c8 | 2023-05-19 14:01:50 +0200 | [diff] [blame] | 3489 | /* prefix_name and ':' */ |
| 3490 | len += strlen(node->name.module_prefix) + 1; |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3491 | } |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3492 | if (node->name.str) { |
| 3493 | len += strlen(node->name.str); |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3494 | } |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3495 | if (node->name.add_opts) { |
| 3496 | len += strlen(node->name.add_opts); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3497 | } |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3498 | if (node->name.opts) { |
| 3499 | len += strlen(node->name.opts); |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | return len; |
| 3503 | } |
| 3504 | |
| 3505 | static uint32_t |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3506 | trb_gap_to_type(const struct trt_node *node) |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3507 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3508 | uint32_t len, opts_len; |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3509 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3510 | if (node->name.keys) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | len = trb_gap_to_opts(node); |
| 3515 | /* Gap between opts and type. */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3516 | opts_len = 0; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3517 | opts_len += node->name.add_opts ? strlen(node->name.add_opts) : 0; |
| 3518 | opts_len += node->name.opts ? strlen(node->name.opts) : 0; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3519 | if (opts_len >= TRD_INDENT_BEFORE_TYPE) { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3520 | /* At least one space should be there. */ |
| 3521 | len += 1; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3522 | } else if (node->name.add_opts || node->name.opts) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3523 | len += TRD_INDENT_BEFORE_TYPE - opts_len; |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3524 | } else { |
| 3525 | len += TRD_INDENT_BEFORE_TYPE; |
| 3526 | } |
| 3527 | |
| 3528 | return len; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3532 | * @brief Calculate the trt_indent_in_node.btw_opts_type indent size |
| 3533 | * for a particular node. |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3534 | * @param[in] node for which we get btw_opts_type. |
| 3535 | * @param[in] max_gap_before_type is the maximum value of btw_opts_type |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3536 | * that it can have. |
| 3537 | * @return Indent between \<opts\> and \<type\> for node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3538 | */ |
| 3539 | static int16_t |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3540 | trb_calc_btw_opts_type(const struct trt_node *node, int16_t max_gap_before_type) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3541 | { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3542 | uint32_t to_opts_len; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3543 | |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3544 | to_opts_len = trb_gap_to_opts(node); |
| 3545 | if (to_opts_len == 0) { |
| 3546 | return 1; |
| 3547 | } else { |
| 3548 | return max_gap_before_type - to_opts_len; |
| 3549 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | /** |
| 3553 | * @brief Print node. |
| 3554 | * |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 3555 | * This function is wrapper for ::trp_print_entire_node(). |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3556 | * But difference is that take @p max_gap_before_type which will be |
| 3557 | * used to set the unified alignment. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3558 | * |
aPiecek | 9bdd759 | 2021-05-20 08:13:20 +0200 | [diff] [blame] | 3559 | * @param[in] node to print. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3560 | * @param[in] max_gap_before_type is number of indent before \<type\>. |
| 3561 | * @param[in] wr is wrapper for printing indentation before node. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3562 | * @param[in] pc contains mainly functions for printing. |
| 3563 | * @param[in] tc is tree context. |
| 3564 | */ |
| 3565 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3566 | trb_print_entire_node(const struct trt_node *node, uint32_t max_gap_before_type, struct trt_wrapper wr, |
| 3567 | struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3568 | { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3569 | struct trt_indent_in_node ind = trp_default_indent_in_node(node); |
| 3570 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3571 | if ((max_gap_before_type > 0) && (node->type.type != TRD_TYPE_EMPTY)) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3572 | /* print actual node with unified indent */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3573 | ind.btw_opts_type = trb_calc_btw_opts_type(node, max_gap_before_type); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3574 | } |
| 3575 | /* after -> print actual node with default indent */ |
| 3576 | trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print), |
| 3577 | TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out); |
| 3578 | } |
| 3579 | |
| 3580 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3581 | * @brief Check if parent of the current node is the last |
| 3582 | * of his siblings. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3583 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3584 | * To mantain stability use this function only if the current node is |
| 3585 | * the first of the siblings. |
| 3586 | * Side-effect -> current node is set to the first sibling |
| 3587 | * if node has a parent otherwise no side-effect. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3588 | * |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 3589 | * @param[in] fp contains all @ref TRP_tro callback functions. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3590 | * @param[in,out] tc is tree context. |
| 3591 | * @return 1 if parent is last sibling otherwise 0. |
| 3592 | */ |
| 3593 | static ly_bool |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3594 | trb_node_is_last_sibling(const struct trt_fp_all *fp, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3595 | { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3596 | if (fp->read.if_parent_exists(tc)) { |
| 3597 | return !fp->read.if_sibling_exists(tc); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3598 | } else { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3599 | return !fp->read.if_sibling_exists(tc) && tc->plugin_ctx.last_schema; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | /** |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3604 | * @brief For all siblings find maximal space from '--' to \<type\>. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3605 | * |
| 3606 | * Side-effect -> Current node is set to the first sibling. |
| 3607 | * |
| 3608 | * @param[in] ca contains inherited data from ancestors. |
| 3609 | * @param[in] pc contains mainly functions for printing. |
| 3610 | * @param[in,out] tc is tree context. |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3611 | * @return max space. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3612 | */ |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3613 | static uint32_t |
| 3614 | trb_max_gap_to_type(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3615 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3616 | struct trt_node node; |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3617 | int32_t maxlen, len; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3618 | |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3619 | maxlen = 0; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3620 | for (node = pc->fp.modify.first_sibling(ca, tc); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3621 | !trp_node_is_empty(&node); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3622 | node = pc->fp.modify.next_sibling(ca, tc)) { |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3623 | len = trb_gap_to_type(&node); |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3624 | maxlen = maxlen < len ? len : maxlen; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3625 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3626 | pc->fp.modify.first_sibling(ca, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3627 | |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3628 | return maxlen; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | /** |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3632 | * @brief Find out if it is possible to unify |
| 3633 | * the alignment before \<type\>. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3634 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3635 | * The goal is for all node siblings to have the same alignment |
| 3636 | * for \<type\> as if they were in a column. All siblings who cannot |
| 3637 | * adapt because they do not fit on the line at all are ignored. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3638 | * Side-effect -> Current node is set to the first sibling. |
| 3639 | * |
| 3640 | * @param[in] ca contains inherited data from ancestors. |
| 3641 | * @param[in] pc contains mainly functions for printing. |
| 3642 | * @param[in,out] tc is tree context. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3643 | * @return positive number indicating the maximum number of spaces |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3644 | * before \<type\> if the length of the flags, node name and opts is 0. To calculate |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 3645 | * the trt_indent_in_node.btw_opts_type indent size for a particular |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 3646 | * node, use the ::trb_calc_btw_opts_type(). |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3647 | */ |
| 3648 | static uint32_t |
| 3649 | trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 3650 | { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3651 | return trb_max_gap_to_type(ca, pc, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | /** |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3655 | * @brief Check if there is no case statement |
| 3656 | * under the choice statement. |
| 3657 | * |
| 3658 | * It can return true only if the Parsed schema tree |
| 3659 | * is used for browsing. |
| 3660 | * |
| 3661 | * @param[in] tc is tree context. |
| 3662 | * @return 1 if implicit case statement is present otherwise 0. |
| 3663 | */ |
| 3664 | static ly_bool |
| 3665 | trb_need_implicit_node_case(struct trt_tree_ctx *tc) |
| 3666 | { |
| 3667 | return !tc->lysc_tree && tc->pn->parent && |
| 3668 | (tc->pn->parent->nodetype & LYS_CHOICE) && |
| 3669 | (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | |
| 3670 | LYS_LEAF | LYS_LEAFLIST)); |
| 3671 | } |
| 3672 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3673 | static void trb_print_subtree_nodes(struct trt_node *node, uint32_t max_gap_before_type, |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3674 | struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc); |
| 3675 | |
| 3676 | /** |
| 3677 | * @brief Print implicit case node and his subtree. |
| 3678 | * |
| 3679 | * @param[in] node is child of implicit case. |
| 3680 | * @param[in] wr is wrapper for printing identation before node. |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3681 | * @param[in] pc contains mainly functions for printing. |
| 3682 | * @param[in] tc is tree context. Its settings should be the same as |
| 3683 | * before the function call. |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 3684 | * @return new indentation wrapper for @p node. |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3685 | */ |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 3686 | static struct trt_wrapper |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3687 | trb_print_implicit_node(const struct trt_node *node, struct trt_wrapper wr, struct trt_printer_ctx *pc, |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 3688 | struct trt_tree_ctx *tc) |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3689 | { |
| 3690 | struct trt_node case_node; |
| 3691 | struct trt_wrapper wr_case_child; |
| 3692 | |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3693 | tro_create_implicit_case_node(node, &case_node); |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3694 | ly_print_(pc->out, "\n"); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3695 | trb_print_entire_node(&case_node, 0, wr, pc, tc); |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 3696 | ly_print_(pc->out, "\n"); |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3697 | wr_case_child = pc->fp.read.if_sibling_exists(tc) ? |
| 3698 | trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr); |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 3699 | return wr_case_child; |
aPiecek | b8d5a0a | 2021-05-20 08:20:24 +0200 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | /** |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3703 | * @brief Calculate the wrapper about how deep in the tree the node is. |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 3704 | * @param[in] wr_in A wrapper to use as a starting point |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3705 | * @param[in] node from which to count. |
| 3706 | * @return wrapper for @p node. |
| 3707 | */ |
| 3708 | static struct trt_wrapper |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 3709 | trb_count_depth(const struct trt_wrapper *wr_in, const struct lysc_node *node) |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3710 | { |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 3711 | struct trt_wrapper wr = wr_in ? *wr_in : TRP_INIT_WRAPPER_TOP; |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3712 | const struct lysc_node *parent; |
| 3713 | |
| 3714 | if (!node) { |
| 3715 | return wr; |
| 3716 | } |
| 3717 | |
| 3718 | for (parent = node->parent; parent; parent = parent->parent) { |
| 3719 | wr = trp_wrapper_set_shift(wr); |
| 3720 | } |
| 3721 | |
| 3722 | return wr; |
| 3723 | } |
| 3724 | |
| 3725 | /** |
| 3726 | * @brief Print all parent nodes of @p node and the @p node itself. |
| 3727 | * |
| 3728 | * Side-effect -> trt_tree_ctx.cn will be set to @p node. |
| 3729 | * |
| 3730 | * @param[in] node on which the function is focused. |
aPiecek | 7ed8d03 | 2022-10-10 12:32:27 +0200 | [diff] [blame] | 3731 | * @param[in] wr_in for printing identation before node. |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 3732 | * @param[in] pc is @ref TRP_trp settings. |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3733 | * @param[in,out] tc is context of tree printer. |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3734 | */ |
| 3735 | static void |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 3736 | trb_print_parents(const struct lysc_node *node, struct trt_wrapper *wr_in, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3737 | { |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3738 | uint32_t max_gap_before_type; |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3739 | struct trt_wrapper wr; |
aPiecek | 9bdd759 | 2021-05-20 08:13:20 +0200 | [diff] [blame] | 3740 | struct trt_node print_node; |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3741 | |
| 3742 | assert(pc && tc && tc->section == TRD_SECT_MODULE); |
| 3743 | |
| 3744 | /* stop recursion */ |
| 3745 | if (!node) { |
| 3746 | return; |
| 3747 | } |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 3748 | trb_print_parents(node->parent, wr_in, pc, tc); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3749 | |
| 3750 | /* setup for printing */ |
| 3751 | tc->cn = node; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 3752 | wr = trb_count_depth(wr_in, node); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3753 | |
| 3754 | /* print node */ |
| 3755 | ly_print_(pc->out, "\n"); |
aPiecek | 9bdd759 | 2021-05-20 08:13:20 +0200 | [diff] [blame] | 3756 | print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc); |
aPiecek | 0bc6225 | 2023-03-30 10:06:25 +0200 | [diff] [blame] | 3757 | /* siblings do not print, so the node is always considered the last */ |
| 3758 | print_node.last_one = 1; |
aPiecek | 41219f9 | 2022-10-26 11:24:40 +0200 | [diff] [blame] | 3759 | max_gap_before_type = trb_max_gap_to_type(TRP_EMPTY_PARENT_CACHE, pc, tc); |
aPiecek | fa3f687 | 2023-03-29 11:33:32 +0200 | [diff] [blame] | 3760 | tc->cn = node; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3761 | trb_print_entire_node(&print_node, max_gap_before_type, wr, pc, tc); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | /** |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 3765 | * @brief Set current node on its child. |
| 3766 | * @param[in,out] tc contains current node. |
| 3767 | */ |
| 3768 | static void |
| 3769 | trb_tree_ctx_set_child(struct trt_tree_ctx *tc) |
| 3770 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3771 | const void *node = tro_tree_ctx_get_child(tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 3772 | |
| 3773 | if (tc->lysc_tree) { |
| 3774 | tc->cn = node; |
| 3775 | } else { |
| 3776 | tc->pn = node; |
| 3777 | } |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3781 | * @brief Move extension iterator to the next position. |
| 3782 | * |
| 3783 | * @param[in] lysc_tree flag if exts is from compiled tree. |
| 3784 | * @param[in] exts is current array of extensions. |
| 3785 | * @param[in,out] i is state of iterator. |
| 3786 | * @return Pointer to the first/next extension. |
| 3787 | */ |
| 3788 | static void * |
Michal Vasko | 8f14b30 | 2024-04-16 09:30:14 +0200 | [diff] [blame] | 3789 | trb_ext_iter_next(ly_bool lysc_tree, void *exts, LY_ARRAY_COUNT_TYPE *i) |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3790 | { |
| 3791 | void *ext = NULL; |
| 3792 | struct lysc_ext_instance *ce; |
| 3793 | struct lysp_ext_instance *pe; |
| 3794 | |
| 3795 | if (!exts) { |
| 3796 | return NULL; |
| 3797 | } |
| 3798 | |
| 3799 | if (lysc_tree) { |
| 3800 | ce = exts; |
| 3801 | while (*i < LY_ARRAY_COUNT(ce)) { |
aPiecek | 7cf319c | 2023-01-13 10:51:49 +0100 | [diff] [blame] | 3802 | if (ce->def->plugin && trp_ext_parent_is_valid(1, &ce[*i])) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3803 | ext = &ce[*i]; |
| 3804 | break; |
| 3805 | } |
| 3806 | ++(*i); |
| 3807 | } |
| 3808 | } else { |
| 3809 | pe = exts; |
| 3810 | while (*i < LY_ARRAY_COUNT(pe)) { |
| 3811 | if (trp_ext_parent_is_valid(0, &pe[*i])) { |
| 3812 | ext = &pe[*i]; |
| 3813 | break; |
| 3814 | } |
| 3815 | ++(*i); |
| 3816 | } |
| 3817 | } |
| 3818 | ++(*i); |
| 3819 | |
| 3820 | return ext; |
| 3821 | } |
| 3822 | |
| 3823 | /** |
| 3824 | * @brief Iterate over extensions in module. |
| 3825 | * |
| 3826 | * @param[in] tc contains current node. |
| 3827 | * @param[in,out] i is state of iterator. |
| 3828 | * @return First/next extension or NULL. |
| 3829 | */ |
| 3830 | static void * |
Michal Vasko | 8f14b30 | 2024-04-16 09:30:14 +0200 | [diff] [blame] | 3831 | trb_mod_ext_iter(const struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE *i) |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3832 | { |
| 3833 | if (tc->lysc_tree) { |
| 3834 | return trb_ext_iter_next(1, tc->cmod->exts, i); |
| 3835 | } else { |
| 3836 | return trb_ext_iter_next(0, tc->pmod->exts, i); |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | /** |
| 3841 | * @brief Iterate over extensions in node. |
| 3842 | * |
| 3843 | * @param[in] tc contains current node. |
| 3844 | * @param[in,out] i is state of iterator. |
| 3845 | * @return First/next extension or NULL. |
| 3846 | */ |
| 3847 | static void * |
Michal Vasko | 8f14b30 | 2024-04-16 09:30:14 +0200 | [diff] [blame] | 3848 | trb_ext_iter(const struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE *i) |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3849 | { |
| 3850 | if (tc->lysc_tree) { |
| 3851 | return trb_ext_iter_next(1, tc->cn->exts, i); |
| 3852 | } else { |
| 3853 | return trb_ext_iter_next(0, tc->pn->exts, i); |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | /** |
| 3858 | * @brief Initialize plugin context. |
| 3859 | * |
| 3860 | * @param[in] compiled if @p ext is lysc structure. |
| 3861 | * @param[in] ext current processed extension. |
| 3862 | * @param[out] plug_ctx is plugin context which will be initialized. |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 3863 | * @param[out] ignore plugin callback is NULL. |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3864 | * @return LY_ERR value. |
| 3865 | */ |
| 3866 | static LY_ERR |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 3867 | tro_ext_printer_tree(ly_bool compiled, void *ext, const struct lyspr_tree_ctx *plug_ctx, ly_bool *ignore) |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3868 | { |
| 3869 | struct lysc_ext_instance *ext_comp; |
| 3870 | struct lysp_ext_instance *ext_pars; |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 3871 | const struct lyplg_ext *plugin; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3872 | const char *flags = NULL, *add_opts = NULL; |
| 3873 | |
| 3874 | if (compiled) { |
| 3875 | ext_comp = ext; |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 3876 | plugin = ext_comp->def->plugin; |
| 3877 | if (!plugin->printer_ctree) { |
| 3878 | *ignore = 1; |
| 3879 | return LY_SUCCESS; |
Michal Vasko | 1f9925c | 2023-04-28 09:08:42 +0200 | [diff] [blame] | 3880 | } |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 3881 | return plugin->printer_ctree(ext, plug_ctx, &flags, &add_opts); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3882 | } else { |
| 3883 | ext_pars = ext; |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 3884 | plugin = &ext_pars->record->plugin; |
| 3885 | if (!plugin->printer_ptree) { |
| 3886 | *ignore = 1; |
| 3887 | return LY_SUCCESS; |
Michal Vasko | 1f9925c | 2023-04-28 09:08:42 +0200 | [diff] [blame] | 3888 | } |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 3889 | return plugin->printer_ptree(ext, plug_ctx, &flags, &add_opts); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3890 | } |
Michal Vasko | 1f9925c | 2023-04-28 09:08:42 +0200 | [diff] [blame] | 3891 | |
| 3892 | return LY_SUCCESS; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | /** |
| 3896 | * @brief Reset tree context by plugin context. |
| 3897 | * |
| 3898 | * @param[in] plug_ctx is plugin context. |
| 3899 | * @param[in] i which index in schemas should be used. |
| 3900 | * @param[in] pc are printing functions. |
| 3901 | * @param[out] tc tree context which will be updated. |
| 3902 | */ |
| 3903 | static void |
| 3904 | trm_reset_tree_ctx_by_plugin(struct lyspr_tree_ctx *plug_ctx, LY_ARRAY_COUNT_TYPE i, struct trt_printer_ctx *pc, |
| 3905 | struct trt_tree_ctx *tc) |
| 3906 | { |
| 3907 | tc->plugin_ctx.ctx = plug_ctx; |
| 3908 | tc->pmod = NULL; |
| 3909 | tc->cmod = NULL; |
| 3910 | if (plug_ctx->schemas[i].compiled) { |
| 3911 | tc->lysc_tree = 1; |
| 3912 | tc->cn = plug_ctx->schemas[i].ctree; |
| 3913 | tc->plugin_ctx.schema = &plug_ctx->schemas[i]; |
| 3914 | pc->fp.modify = TRP_TRT_FP_MODIFY_COMPILED; |
| 3915 | pc->fp.read = TRP_TRT_FP_READ_COMPILED; |
| 3916 | } else { |
| 3917 | tc->lysc_tree = 0; |
| 3918 | tc->pn = plug_ctx->schemas[i].ptree; |
| 3919 | tc->tpn = tc->pn; |
| 3920 | tc->plugin_ctx.schema = &plug_ctx->schemas[i]; |
| 3921 | pc->fp.modify = TRP_TRT_FP_MODIFY_PARSED; |
| 3922 | pc->fp.read = TRP_TRT_FP_READ_PARSED; |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | /** |
| 3927 | * @brief Print schemas from plugin context. |
| 3928 | * |
| 3929 | * @param[in] plug_ctx is plugin context. |
| 3930 | * @param[in] last_nodes if this schemas will be the last. |
| 3931 | * @param[in] max_gap_before_type is gap before type. |
| 3932 | * @param[in] wr is indentation wrapper. |
| 3933 | * @param[in] ca containing information from parent. |
| 3934 | * @param[in] pc functions for tree traversing. |
| 3935 | * @param[in] tc current tree context. |
| 3936 | */ |
| 3937 | static void |
| 3938 | trb_ext_print_schemas(struct lyspr_tree_ctx *plug_ctx, ly_bool last_nodes, uint32_t max_gap_before_type, |
| 3939 | struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 3940 | { |
| 3941 | LY_ARRAY_COUNT_TYPE i; |
| 3942 | struct trt_printer_ctx pc_dupl; |
| 3943 | struct trt_tree_ctx tc_dupl; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3944 | struct trt_node node; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3945 | |
| 3946 | tc_dupl = *tc; |
| 3947 | pc_dupl = *pc; |
| 3948 | |
| 3949 | LY_ARRAY_FOR(plug_ctx->schemas, i) { |
| 3950 | trm_reset_tree_ctx_by_plugin(plug_ctx, i, pc, tc); |
| 3951 | tc->plugin_ctx.last_schema = last_nodes && ((i + 1) == LY_ARRAY_COUNT(plug_ctx->schemas)); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 3952 | node = TRP_EMPTY_NODE; |
| 3953 | trb_print_subtree_nodes(&node, max_gap_before_type, wr, ca, pc, tc); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 3954 | *tc = tc_dupl; |
| 3955 | } |
| 3956 | |
| 3957 | *pc = pc_dupl; |
| 3958 | } |
| 3959 | |
| 3960 | /** |
| 3961 | * @brief Count unified indentation across schemas from extension instance. |
| 3962 | * |
| 3963 | * @param[in] plug_ctx is plugin context. |
| 3964 | * @param[in] ca containing parent settings. |
| 3965 | * @param[out] max_gap_before_type is result of unified indent. |
| 3966 | * @param[in] pc functions for tree traversing. |
| 3967 | * @param[in] tc is tree context. |
| 3968 | */ |
| 3969 | static void |
| 3970 | trb_ext_try_unified_indent(struct lyspr_tree_ctx *plug_ctx, struct trt_parent_cache ca, uint32_t *max_gap_before_type, |
| 3971 | struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 3972 | { |
| 3973 | LY_ARRAY_COUNT_TYPE i; |
| 3974 | struct trt_printer_ctx pc_dupl; |
| 3975 | struct trt_tree_ctx tc_dupl; |
| 3976 | uint32_t max; |
| 3977 | |
| 3978 | tc_dupl = *tc; |
| 3979 | pc_dupl = *pc; |
| 3980 | |
| 3981 | LY_ARRAY_FOR(plug_ctx->schemas, i) { |
| 3982 | trm_reset_tree_ctx_by_plugin(plug_ctx, i, pc, tc); |
| 3983 | max = trb_try_unified_indent(ca, pc, tc); |
| 3984 | *max_gap_before_type = max > *max_gap_before_type ? max : *max_gap_before_type; |
| 3985 | *tc = tc_dupl; |
| 3986 | } |
| 3987 | |
| 3988 | *pc = pc_dupl; |
| 3989 | } |
| 3990 | |
| 3991 | /** |
| 3992 | * @brief For every extension instance print all schemas. |
| 3993 | * |
| 3994 | * @param[in] wr indentation wrapper for node. |
| 3995 | * @param[in] ca parent settings. |
| 3996 | * @param[in] pc function used for tree traversing. |
| 3997 | * @param[in] tc tree context. |
| 3998 | */ |
| 3999 | static void |
| 4000 | trb_ext_print_instances(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, |
| 4001 | struct trt_tree_ctx *tc) |
| 4002 | { |
| 4003 | LY_ERR rc; |
| 4004 | LY_ARRAY_COUNT_TYPE i; |
| 4005 | uint64_t last_instance = UINT64_MAX; |
| 4006 | void *ext; |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4007 | ly_bool child_exists, ignore = 0; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4008 | uint32_t max, max_gap_before_type = 0; |
| 4009 | |
| 4010 | ca = tro_parent_cache_for_child(ca, tc); |
| 4011 | /* if node is last sibling, then do not add '|' to wrapper */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4012 | wr = trb_node_is_last_sibling(&pc->fp, tc) ? |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4013 | trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr); |
| 4014 | |
| 4015 | if (tc->lysc_tree) { |
| 4016 | child_exists = tro_next_child(tc->cn, tc) ? 1 : 0; |
| 4017 | } else { |
| 4018 | child_exists = tro_next_child(tc->pn, tc) ? 1 : 0; |
| 4019 | } |
| 4020 | |
| 4021 | i = 0; |
| 4022 | while ((ext = trb_ext_iter(tc, &i))) { |
| 4023 | struct lyspr_tree_ctx plug_ctx = {0}; |
| 4024 | |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4025 | rc = tro_ext_printer_tree(tc->lysc_tree, ext, &plug_ctx, &ignore); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4026 | LY_CHECK_ERR_GOTO(rc, tc->last_error = rc, end); |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4027 | if (ignore) { |
| 4028 | ignore = 0; |
| 4029 | continue; |
| 4030 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4031 | trb_ext_try_unified_indent(&plug_ctx, ca, &max_gap_before_type, pc, tc); |
| 4032 | if (plug_ctx.schemas) { |
| 4033 | last_instance = i; |
| 4034 | } |
| 4035 | trp_ext_free_plugin_ctx(&plug_ctx); |
| 4036 | } |
| 4037 | |
| 4038 | if (child_exists) { |
| 4039 | pc->fp.modify.next_child(ca, tc); |
| 4040 | max = trb_try_unified_indent(ca, pc, tc); |
| 4041 | max_gap_before_type = max > max_gap_before_type ? max : max_gap_before_type; |
| 4042 | pc->fp.modify.parent(tc); |
| 4043 | } |
| 4044 | |
| 4045 | i = 0; |
| 4046 | while ((ext = trb_ext_iter(tc, &i))) { |
| 4047 | struct lyspr_tree_ctx plug_ctx = {0}; |
| 4048 | |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4049 | rc = tro_ext_printer_tree(tc->lysc_tree, ext, &plug_ctx, &ignore); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4050 | LY_CHECK_ERR_GOTO(rc, tc->last_error = rc, end); |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4051 | if (ignore) { |
| 4052 | ignore = 0; |
| 4053 | continue; |
| 4054 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4055 | if (!child_exists && (last_instance == i)) { |
| 4056 | trb_ext_print_schemas(&plug_ctx, 1, max_gap_before_type, wr, ca, pc, tc); |
| 4057 | } else { |
| 4058 | trb_ext_print_schemas(&plug_ctx, 0, max_gap_before_type, wr, ca, pc, tc); |
| 4059 | } |
| 4060 | trp_ext_free_plugin_ctx(&plug_ctx); |
| 4061 | } |
| 4062 | |
| 4063 | end: |
| 4064 | return; |
| 4065 | } |
| 4066 | |
| 4067 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4068 | * @brief Print subtree of nodes. |
| 4069 | * |
| 4070 | * The current node is expected to be the root of the subtree. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4071 | * Before root node is no linebreak printing. This must be addressed by |
| 4072 | * the caller. Root node will also be printed. Behind last printed node |
| 4073 | * is no linebreak. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4074 | * |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4075 | * @param[in,out] node current processed node used as iterator. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4076 | * @param[in] max_gap_before_type is result from |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4077 | * ::trb_try_unified_indent() function for root node. |
| 4078 | * Set parameter to 0 if distance does not matter. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4079 | * @param[in] wr is wrapper saying how deep in the whole tree |
| 4080 | * is the root of the subtree. |
| 4081 | * @param[in] ca is parent_cache from root's parent. |
| 4082 | * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE. |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4083 | * @param[in] pc is @ref TRP_trp settings. |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4084 | * @param[in,out] tc is context of tree printer. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4085 | */ |
| 4086 | static void |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4087 | trb_print_subtree_nodes(struct trt_node *node, uint32_t max_gap_before_type, struct trt_wrapper wr, |
aPiecek | 9bdd759 | 2021-05-20 08:13:20 +0200 | [diff] [blame] | 4088 | struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4089 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4090 | if (!trp_node_is_empty(node)) { |
| 4091 | /* Print root node. */ |
| 4092 | trb_print_entire_node(node, max_gap_before_type, wr, pc, tc); |
| 4093 | if (trp_ext_is_present_in_node(tc)) { |
| 4094 | trb_ext_print_instances(wr, ca, pc, tc); |
| 4095 | } |
| 4096 | /* if node is last sibling, then do not add '|' to wrapper */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4097 | wr = trb_node_is_last_sibling(&pc->fp, tc) ? |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4098 | trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr); |
| 4099 | /* go to the child */ |
| 4100 | ca = tro_parent_cache_for_child(ca, tc); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4101 | *node = pc->fp.modify.next_child(ca, tc); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4102 | if (trp_node_is_empty(node)) { |
| 4103 | return; |
| 4104 | } |
| 4105 | /* TODO comment browse through instances + filtered. try unified indentation for children */ |
| 4106 | max_gap_before_type = trb_try_unified_indent(ca, pc, tc); |
| 4107 | } else { |
| 4108 | /* Root node is ignored, continue with child. */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4109 | *node = pc->fp.modify.first_sibling(ca, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4110 | } |
aPiecek | 67521c2 | 2022-10-19 09:15:02 +0200 | [diff] [blame] | 4111 | |
aPiecek | 67521c2 | 2022-10-19 09:15:02 +0200 | [diff] [blame] | 4112 | do { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4113 | if (!tc->plugin_ctx.filtered && !trb_need_implicit_node_case(tc)) { |
aPiecek | 67521c2 | 2022-10-19 09:15:02 +0200 | [diff] [blame] | 4114 | /* normal behavior */ |
| 4115 | ly_print_(pc->out, "\n"); |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 4116 | trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4117 | } else if (!tc->plugin_ctx.filtered) { |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 4118 | struct trt_wrapper wr_case_child; |
| 4119 | |
| 4120 | wr_case_child = trb_print_implicit_node(node, wr, pc, tc); |
| 4121 | trb_print_subtree_nodes(node, max_gap_before_type, wr_case_child, ca, pc, tc); |
aPiecek | 67521c2 | 2022-10-19 09:15:02 +0200 | [diff] [blame] | 4122 | } |
| 4123 | /* go to the actual node's sibling */ |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4124 | *node = pc->fp.modify.next_sibling(ca, tc); |
aPiecek | 49be5b4 | 2022-10-19 10:57:56 +0200 | [diff] [blame] | 4125 | } while (!trp_node_is_empty(node)); |
aPiecek | 67521c2 | 2022-10-19 09:15:02 +0200 | [diff] [blame] | 4126 | |
| 4127 | /* get back from child node to root node */ |
| 4128 | pc->fp.modify.parent(tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4132 | * @brief Print all parents and their children. |
| 4133 | * |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4134 | * This function is suitable for printing top-level nodes that |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4135 | * do not have ancestors. Function call ::trb_print_subtree_nodes() |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4136 | * for all top-level siblings. Use this function after 'module' keyword |
| 4137 | * or 'augment' and so. The nodes may not be exactly top-level in the |
| 4138 | * tree, but the function considers them that way. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4139 | * |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4140 | * @param[in] wr is wrapper saying how deeply the top-level nodes are |
| 4141 | * immersed in the tree. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4142 | * @param[pc] pc contains mainly functions for printing. |
| 4143 | * @param[in,out] tc is tree context. |
| 4144 | */ |
| 4145 | static void |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4146 | trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4147 | { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4148 | struct trt_parent_cache ca; |
aPiecek | 9bdd759 | 2021-05-20 08:13:20 +0200 | [diff] [blame] | 4149 | struct trt_node node; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4150 | uint32_t max_gap_before_type; |
| 4151 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4152 | if (!tro_tree_ctx_get_node(tc)) { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4153 | return; |
| 4154 | } |
| 4155 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4156 | ca = TRP_EMPTY_PARENT_CACHE; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4157 | max_gap_before_type = trb_try_unified_indent(ca, pc, tc); |
| 4158 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4159 | if (!tc->lysc_tree) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4160 | if ((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) { |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4161 | ca.lys_config = 0x0; |
| 4162 | } |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4163 | } |
| 4164 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4165 | for (node = pc->fp.modify.first_sibling(ca, tc); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4166 | !trp_node_is_empty(&node); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4167 | node = pc->fp.modify.next_sibling(ca, tc)) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4168 | ly_print_(pc->out, "\n"); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4169 | trb_print_subtree_nodes(&node, max_gap_before_type, wr, ca, pc, tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4170 | } |
| 4171 | } |
| 4172 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4173 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4174 | * Definition of trm main functions |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4175 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4176 | |
| 4177 | /** |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4178 | * @brief Settings if lysp_node are used for browsing through the tree. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4179 | * |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4180 | * @param[in] module YANG schema tree structure representing |
| 4181 | * YANG module. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4182 | * @param[in] out is output handler. |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4183 | * @param[in] max_line_length is the maximum line length limit |
| 4184 | * that should not be exceeded. |
| 4185 | * @param[in,out] pc will be adapted to lysp_tree. |
| 4186 | * @param[in,out] tc will be adapted to lysp_tree. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4187 | */ |
| 4188 | static void |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4189 | trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, |
| 4190 | struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4191 | { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4192 | *tc = (struct trt_tree_ctx) { |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4193 | .lysc_tree = 0, |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4194 | .section = TRD_SECT_MODULE, |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 4195 | .pmod = module->parsed, |
| 4196 | .cmod = NULL, |
| 4197 | .pn = module->parsed ? module->parsed->data : NULL, |
| 4198 | .tpn = module->parsed ? module->parsed->data : NULL, |
aPiecek | 7ed8d03 | 2022-10-10 12:32:27 +0200 | [diff] [blame] | 4199 | .cn = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4200 | .last_error = 0, |
Michal Vasko | dedded6 | 2024-02-20 15:49:25 +0100 | [diff] [blame] | 4201 | .plugin_ctx = { |
| 4202 | .ctx = NULL, |
| 4203 | .schema = NULL, |
| 4204 | .filtered = 0, |
| 4205 | .node_overr = TRP_TREE_CTX_EMPTY_NODE_OVERR, |
| 4206 | .last_schema = 1, |
| 4207 | .last_error = 0 |
| 4208 | } |
| 4209 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4210 | }; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4211 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4212 | pc->out = out; |
| 4213 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4214 | pc->fp.modify = TRP_TRT_FP_MODIFY_PARSED; |
| 4215 | pc->fp.read = TRP_TRT_FP_READ_PARSED; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4216 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4217 | pc->fp.print = (struct trt_fp_print) { |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4218 | .print_features_names = tro_print_features_names, |
| 4219 | .print_keys = tro_print_keys |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4220 | }; |
| 4221 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4222 | pc->max_line_length = max_line_length; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4226 | * @brief Settings if lysc_node are used for browsing through the tree. |
| 4227 | * |
| 4228 | * Pointers to current nodes will be set to module data. |
| 4229 | * |
| 4230 | * @param[in] module YANG schema tree structure representing |
| 4231 | * YANG module. |
| 4232 | * @param[in] out is output handler. |
| 4233 | * @param[in] max_line_length is the maximum line length limit |
| 4234 | * that should not be exceeded. |
| 4235 | * @param[in,out] pc will be adapted to lysc_tree. |
| 4236 | * @param[in,out] tc will be adapted to lysc_tree. |
| 4237 | */ |
| 4238 | static void |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4239 | trm_lysc_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, |
| 4240 | struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4241 | { |
| 4242 | *tc = (struct trt_tree_ctx) { |
| 4243 | .lysc_tree = 1, |
| 4244 | .section = TRD_SECT_MODULE, |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 4245 | .pmod = module->parsed, |
| 4246 | .cmod = module->compiled, |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4247 | .tpn = NULL, |
| 4248 | .pn = NULL, |
aPiecek | 7ed8d03 | 2022-10-10 12:32:27 +0200 | [diff] [blame] | 4249 | .cn = module->compiled->data, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4250 | .last_error = 0, |
Michal Vasko | dedded6 | 2024-02-20 15:49:25 +0100 | [diff] [blame] | 4251 | .plugin_ctx = { |
| 4252 | .ctx = NULL, |
| 4253 | .schema = NULL, |
| 4254 | .filtered = 0, |
| 4255 | .node_overr = TRP_TREE_CTX_EMPTY_NODE_OVERR, |
| 4256 | .last_schema = 1, |
| 4257 | .last_error = 0 |
| 4258 | } |
| 4259 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4260 | }; |
| 4261 | |
| 4262 | pc->out = out; |
| 4263 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4264 | pc->fp.modify = TRP_TRT_FP_MODIFY_COMPILED; |
| 4265 | pc->fp.read = TRP_TRT_FP_READ_COMPILED; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4266 | |
| 4267 | pc->fp.print = (struct trt_fp_print) { |
| 4268 | .print_features_names = tro_print_features_names, |
| 4269 | .print_keys = tro_print_keys |
| 4270 | }; |
| 4271 | |
| 4272 | pc->max_line_length = max_line_length; |
| 4273 | } |
| 4274 | |
| 4275 | /** |
| 4276 | * @brief Reset settings to browsing through the lysc tree. |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4277 | * @param[in,out] pc resets to @ref TRP_troc functions. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4278 | * @param[in,out] tc resets to lysc browsing. |
| 4279 | */ |
| 4280 | static void |
| 4281 | trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4282 | { |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4283 | LY_ERR erc; |
| 4284 | |
| 4285 | erc = tc->last_error; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4286 | trp_ext_free_node_override(&tc->plugin_ctx.node_overr, &tc->plugin_ctx.filtered); |
| 4287 | trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc); |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4288 | tc->last_error = erc; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | /** |
| 4292 | * @brief Reset settings to browsing through the lysp tree. |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4293 | * @param[in,out] pc resets to @ref TRP_trop functions. |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4294 | * @param[in,out] tc resets to lysp browsing. |
| 4295 | */ |
| 4296 | static void |
| 4297 | trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4298 | { |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4299 | LY_ERR erc; |
| 4300 | |
| 4301 | erc = tc->last_error; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4302 | trp_ext_free_node_override(&tc->plugin_ctx.node_overr, &tc->plugin_ctx.filtered); |
| 4303 | trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc); |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4304 | tc->last_error = erc; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | /** |
| 4308 | * @brief If augment's target node is located on the current module. |
| 4309 | * @param[in] pn is examined augment. |
| 4310 | * @param[in] pmod is current module. |
| 4311 | * @return 1 if nodeid refers to the local node, otherwise 0. |
| 4312 | */ |
| 4313 | static ly_bool |
| 4314 | trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod) |
| 4315 | { |
| 4316 | const char *id, *prefix, *name; |
| 4317 | size_t prefix_len, name_len; |
| 4318 | const struct lys_module *mod; |
| 4319 | ly_bool ret = 0; |
| 4320 | |
| 4321 | if (pn == NULL) { |
| 4322 | return ret; |
| 4323 | } |
| 4324 | |
| 4325 | id = pn->nodeid; |
| 4326 | if (!id) { |
| 4327 | return ret; |
| 4328 | } |
| 4329 | /* only absolute-schema-nodeid is taken into account */ |
| 4330 | assert(id[0] == '/'); |
| 4331 | ++id; |
| 4332 | |
| 4333 | ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len); |
| 4334 | if (prefix) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 4335 | mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod); |
Michal Vasko | 3003c9a | 2022-03-29 12:10:00 +0200 | [diff] [blame] | 4336 | ret = mod ? (mod->parsed == pmod) : 0; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4337 | } else { |
| 4338 | ret = 1; |
| 4339 | } |
| 4340 | |
| 4341 | return ret; |
| 4342 | } |
| 4343 | |
| 4344 | /** |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4345 | * @brief Printing section module, rpcs, notifications or yang-data. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4346 | * |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4347 | * First node must be the first child of 'module', |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4348 | * 'rpcs', 'notifications' or 'yang-data'. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4349 | * |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4350 | * @param[in] ks is section representation. |
| 4351 | * @param[in] pc contains mainly functions for printing. |
| 4352 | * @param[in,out] tc is the tree context. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4353 | */ |
| 4354 | static void |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4355 | 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] | 4356 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4357 | assert(ks.section_name); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4358 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4359 | trp_print_keyword_stmt(ks, pc->max_line_length, pc->out); |
| 4360 | if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) || !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) { |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4361 | trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4362 | } else { |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4363 | trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | |
| 4367 | /** |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4368 | * @brief Printing section augment or grouping. |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4369 | * |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4370 | * First node is 'augment' or 'grouping' itself. |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4371 | * |
| 4372 | * @param[in] ks is section representation. |
| 4373 | * @param[in] pc contains mainly functions for printing. |
| 4374 | * @param[in,out] tc is the tree context. |
| 4375 | */ |
| 4376 | static void |
| 4377 | trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4378 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4379 | assert(ks.section_name); |
| 4380 | trp_print_keyword_stmt(ks, pc->max_line_length, pc->out); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4381 | trb_tree_ctx_set_child(tc); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4382 | trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | /** |
| 4386 | * @brief Print 'module' keyword, its name and all nodes. |
| 4387 | * @param[in] pc contains mainly functions for printing. |
| 4388 | * @param[in,out] tc is the tree context. |
| 4389 | */ |
| 4390 | static void |
| 4391 | trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4392 | { |
| 4393 | trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc); |
| 4394 | } |
| 4395 | |
| 4396 | /** |
| 4397 | * @brief For all augment sections: print 'augment' keyword, |
| 4398 | * its target node and all nodes. |
| 4399 | * @param[in] pc contains mainly functions for printing. |
| 4400 | * @param[in,out] tc is the tree context. |
| 4401 | */ |
| 4402 | static void |
| 4403 | trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4404 | { |
| 4405 | ly_bool once; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4406 | ly_bool origin_was_lysc_tree = 0; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4407 | struct trt_keyword_stmt ks; |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4408 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4409 | if (tc->lysc_tree) { |
| 4410 | origin_was_lysc_tree = 1; |
| 4411 | trm_reset_to_lysp_tree_ctx(pc, tc); |
| 4412 | } |
| 4413 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4414 | once = 1; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4415 | for (ks = trop_modi_next_augment(tc); ks.section_name; ks = trop_modi_next_augment(tc)) { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4416 | |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4417 | if (origin_was_lysc_tree) { |
| 4418 | /* if lysc tree is used, then only augments targeting |
| 4419 | * another module are printed |
| 4420 | */ |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 4421 | if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) { |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4422 | continue; |
| 4423 | } |
| 4424 | } |
| 4425 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4426 | if (once) { |
| 4427 | ly_print_(pc->out, "\n"); |
| 4428 | ly_print_(pc->out, "\n"); |
| 4429 | once = 0; |
| 4430 | } else { |
| 4431 | ly_print_(pc->out, "\n"); |
| 4432 | } |
| 4433 | |
| 4434 | trm_print_section_as_subtree(ks, pc, tc); |
| 4435 | } |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4436 | |
| 4437 | if (origin_was_lysc_tree) { |
| 4438 | trm_reset_to_lysc_tree_ctx(pc, tc); |
| 4439 | } |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | /** |
| 4443 | * @brief For rpcs section: print 'rpcs' keyword and all its nodes. |
| 4444 | * @param[in] pc contains mainly functions for printing. |
| 4445 | * @param[in,out] tc is the tree context. |
| 4446 | */ |
| 4447 | static void |
| 4448 | trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4449 | { |
| 4450 | struct trt_keyword_stmt rpc; |
| 4451 | |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4452 | rpc = tro_modi_get_rpcs(tc); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4453 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4454 | if (rpc.section_name) { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4455 | ly_print_(pc->out, "\n"); |
| 4456 | ly_print_(pc->out, "\n"); |
| 4457 | trm_print_section_as_family_tree(rpc, pc, tc); |
| 4458 | } |
| 4459 | } |
| 4460 | |
| 4461 | /** |
| 4462 | * @brief For notifications section: print 'notifications' keyword |
| 4463 | * and all its nodes. |
| 4464 | * @param[in] pc contains mainly functions for printing. |
| 4465 | * @param[in,out] tc is the tree context. |
| 4466 | */ |
| 4467 | static void |
| 4468 | trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4469 | { |
| 4470 | struct trt_keyword_stmt notifs; |
| 4471 | |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4472 | notifs = tro_modi_get_notifications(tc); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4473 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4474 | if (notifs.section_name) { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4475 | ly_print_(pc->out, "\n"); |
| 4476 | ly_print_(pc->out, "\n"); |
| 4477 | trm_print_section_as_family_tree(notifs, pc, tc); |
| 4478 | } |
| 4479 | } |
| 4480 | |
| 4481 | /** |
| 4482 | * @brief For all grouping sections: print 'grouping' keyword, its name |
| 4483 | * and all nodes. |
| 4484 | * @param[in] pc contains mainly functions for printing. |
| 4485 | * @param[in,out] tc is the tree context. |
| 4486 | */ |
| 4487 | static void |
| 4488 | trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4489 | { |
| 4490 | ly_bool once; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4491 | struct trt_keyword_stmt ks; |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4492 | |
aPiecek | 01598c0 | 2021-04-23 14:18:24 +0200 | [diff] [blame] | 4493 | if (tc->lysc_tree) { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4494 | return; |
| 4495 | } |
| 4496 | |
| 4497 | once = 1; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4498 | for (ks = trop_modi_next_grouping(tc); ks.section_name; ks = trop_modi_next_grouping(tc)) { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4499 | if (once) { |
| 4500 | ly_print_(pc->out, "\n"); |
| 4501 | ly_print_(pc->out, "\n"); |
| 4502 | once = 0; |
| 4503 | } else { |
| 4504 | ly_print_(pc->out, "\n"); |
| 4505 | } |
| 4506 | trm_print_section_as_subtree(ks, pc, tc); |
| 4507 | } |
| 4508 | } |
| 4509 | |
| 4510 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4511 | * @brief Print all sections defined in plugins. |
| 4512 | * |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4513 | * @param[in] pc contains mainly functions for printing. |
| 4514 | * @param[in,out] tc is the tree context. |
| 4515 | */ |
| 4516 | static void |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4517 | trm_print_plugin_ext(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4518 | { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4519 | LY_ERR rc; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4520 | ly_bool once; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4521 | LY_ARRAY_COUNT_TYPE i = 0, j; |
| 4522 | struct trt_keyword_stmt ks, prev_ks = {0}; |
| 4523 | struct trt_printer_ctx pc_dupl; |
| 4524 | struct trt_tree_ctx tc_dupl; |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4525 | struct trt_node node; |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4526 | ly_bool ignore = 0; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4527 | uint32_t max_gap_before_type; |
| 4528 | void *ext; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4529 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4530 | tc->section = TRD_SECT_PLUG_DATA; |
| 4531 | |
| 4532 | tc_dupl = *tc; |
| 4533 | pc_dupl = *pc; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4534 | |
| 4535 | once = 1; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4536 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4537 | while ((ext = trb_mod_ext_iter(tc, &i))) { |
| 4538 | struct lyspr_tree_ctx plug_ctx = {0}; |
| 4539 | |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4540 | rc = tro_ext_printer_tree(tc->lysc_tree, ext, &plug_ctx, &ignore); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4541 | LY_CHECK_ERR_GOTO(rc, tc->last_error = rc, end); |
aPiecek | ba67462 | 2023-03-28 10:57:00 +0200 | [diff] [blame] | 4542 | if (!plug_ctx.schemas || ignore) { |
| 4543 | ignore = 0; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4544 | continue; |
| 4545 | } |
| 4546 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4547 | ks = tro_get_ext_section(tc, ext, &plug_ctx); |
| 4548 | if (once || (prev_ks.section_name && strcmp(prev_ks.section_name, ks.section_name))) { |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4549 | ly_print_(pc->out, "\n"); |
| 4550 | ly_print_(pc->out, "\n"); |
| 4551 | once = 0; |
| 4552 | } else { |
| 4553 | ly_print_(pc->out, "\n"); |
| 4554 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4555 | trp_print_keyword_stmt(ks, pc->max_line_length, pc->out); |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4556 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4557 | max_gap_before_type = 0; |
| 4558 | trb_ext_try_unified_indent(&plug_ctx, TRP_EMPTY_PARENT_CACHE, &max_gap_before_type, pc, tc); |
| 4559 | LY_ARRAY_FOR(plug_ctx.schemas, j) { |
| 4560 | trm_reset_tree_ctx_by_plugin(&plug_ctx, j, pc, tc); |
aPiecek | 02665a8 | 2022-12-14 10:38:16 +0100 | [diff] [blame] | 4561 | node = TRP_EMPTY_NODE; |
| 4562 | trb_print_subtree_nodes(&node, max_gap_before_type, TRP_INIT_WRAPPER_BODY, TRP_EMPTY_PARENT_CACHE, pc, tc); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | *tc = tc_dupl; |
| 4566 | trp_ext_free_plugin_ctx(&plug_ctx); |
| 4567 | prev_ks = ks; |
aPiecek | 96baa7f | 2021-04-23 12:32:00 +0200 | [diff] [blame] | 4568 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4569 | |
| 4570 | end: |
| 4571 | *pc = pc_dupl; |
| 4572 | return; |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | /** |
| 4576 | * @brief Print sections module, augment, rpcs, notifications, |
| 4577 | * grouping, yang-data. |
| 4578 | * @param[in] pc contains mainly functions for printing. |
| 4579 | * @param[in,out] tc is the tree context. |
| 4580 | */ |
| 4581 | static void |
| 4582 | trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 4583 | { |
| 4584 | trm_print_module_section(pc, tc); |
| 4585 | trm_print_augmentations(pc, tc); |
| 4586 | trm_print_rpcs(pc, tc); |
| 4587 | trm_print_notifications(pc, tc); |
| 4588 | trm_print_groupings(pc, tc); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4589 | trm_print_plugin_ext(pc, tc); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4590 | ly_print_(pc->out, "\n"); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4591 | } |
| 4592 | |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4593 | static LY_ERR |
| 4594 | tree_print_check_error(struct ly_out_clb_arg *out, struct trt_tree_ctx *tc) |
| 4595 | { |
| 4596 | if (out->last_error) { |
| 4597 | return out->last_error; |
| 4598 | } else if (tc->last_error) { |
| 4599 | return tc->last_error; |
| 4600 | } else { |
| 4601 | return LY_SUCCESS; |
| 4602 | } |
| 4603 | } |
| 4604 | |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4605 | /********************************************************************** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4606 | * Definition of module interface |
aPiecek | 874ea4d | 2021-04-19 12:26:36 +0200 | [diff] [blame] | 4607 | *********************************************************************/ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4608 | |
| 4609 | LY_ERR |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4610 | tree_print_module(struct ly_out *out, const struct lys_module *module, uint32_t UNUSED(options), size_t line_length) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4611 | { |
| 4612 | struct trt_printer_ctx pc; |
| 4613 | struct trt_tree_ctx tc; |
| 4614 | struct ly_out *new_out; |
| 4615 | LY_ERR erc; |
| 4616 | struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS); |
| 4617 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4618 | LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL); |
| 4619 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4620 | if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) { |
| 4621 | return erc; |
| 4622 | } |
| 4623 | |
| 4624 | line_length = line_length == 0 ? SIZE_MAX : line_length; |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4625 | if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4626 | trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4627 | } else { |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4628 | trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc); |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 4629 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4630 | |
| 4631 | trm_print_sections(&pc, &tc); |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4632 | erc = tree_print_check_error(&clb_arg, &tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4633 | |
| 4634 | ly_out_free(new_out, NULL, 1); |
| 4635 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame] | 4636 | return erc; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | LY_ERR |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4640 | tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options, size_t line_length) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4641 | { |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4642 | struct trt_printer_ctx pc; |
| 4643 | struct trt_tree_ctx tc; |
| 4644 | struct ly_out *new_out; |
| 4645 | struct trt_wrapper wr; |
| 4646 | LY_ERR erc; |
| 4647 | struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS); |
| 4648 | |
| 4649 | assert(out && node); |
| 4650 | |
| 4651 | if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) { |
| 4652 | return LY_EINVAL; |
| 4653 | } |
| 4654 | |
| 4655 | if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) { |
| 4656 | return erc; |
| 4657 | } |
| 4658 | |
| 4659 | line_length = line_length == 0 ? SIZE_MAX : line_length; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4660 | trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4661 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4662 | trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, pc.out); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 4663 | trb_print_parents(node, NULL, &pc, &tc); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4664 | |
| 4665 | if (!(options & LYS_PRINT_NO_SUBSTMT)) { |
| 4666 | tc.cn = lysc_node_child(node); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 4667 | wr = trb_count_depth(NULL, tc.cn); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4668 | trb_print_family_tree(wr, &pc, &tc); |
| 4669 | } |
| 4670 | ly_print_(out, "\n"); |
| 4671 | |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4672 | erc = tree_print_check_error(&clb_arg, &tc); |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 4673 | ly_out_free(new_out, NULL, 1); |
| 4674 | |
| 4675 | return erc; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | LY_ERR |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 4679 | tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options), |
| 4680 | size_t line_length) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4681 | { |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 4682 | struct trt_printer_ctx pc; |
| 4683 | struct trt_tree_ctx tc; |
| 4684 | struct ly_out *new_out; |
| 4685 | LY_ERR erc; |
| 4686 | struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS); |
| 4687 | |
| 4688 | assert(submodp); |
| 4689 | LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL); |
| 4690 | |
| 4691 | if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) { |
| 4692 | return erc; |
| 4693 | } |
| 4694 | |
| 4695 | line_length = line_length == 0 ? SIZE_MAX : line_length; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 4696 | trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc); |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 4697 | tc.pmod = (struct lysp_module *)submodp; |
| 4698 | tc.tpn = submodp->data; |
| 4699 | tc.pn = tc.tpn; |
| 4700 | |
| 4701 | trm_print_sections(&pc, &tc); |
aPiecek | 40f2240 | 2022-10-14 10:48:08 +0200 | [diff] [blame] | 4702 | erc = tree_print_check_error(&clb_arg, &tc); |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 4703 | |
| 4704 | ly_out_free(new_out, NULL, 1); |
| 4705 | |
| 4706 | return erc; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 4707 | } |