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