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