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