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