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