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