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