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