blob: 9e997cd3869dbbf8653413effbf60303721bd25b [file] [log] [blame]
aPiecek61d062b2020-11-02 11:05:09 +01001/**
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
aPiecek874ea4d2021-04-19 12:26:36 +020013 *
14 * @section TRP_DESIGN Design
15 *
16 * @code
aPiecek61d062b2020-11-02 11:05:09 +010017 * +---------+ +---------+ +---------+
18 * output | trp | | trb | | tro |
19 * <---+ Print +<---+ Browse +<-->+ Obtain |
20 * | | | | | |
21 * +---------+ +----+----+ +---------+
22 * ^
23 * |
24 * +----+----+
25 * | trm |
26 * | Manager |
27 * | |
28 * +----+----+
29 * ^
30 * | input
31 * +
aPiecek874ea4d2021-04-19 12:26:36 +020032 * @endcode
aPiecek61d062b2020-11-02 11:05:09 +010033 *
aPiecek874ea4d2021-04-19 12:26:36 +020034 * @subsection TRP_GLOSSARY Glossary
aPiecek61d062b2020-11-02 11:05:09 +010035 *
aPiecek874ea4d2021-04-19 12:26:36 +020036 * @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
aPiecek01598c02021-04-23 14:18:24 +020040 * Browse functions (@ref TRP_trb).
aPiecek61d062b2020-11-02 11:05:09 +010041 *
aPiecek874ea4d2021-04-19 12:26:36 +020042 * @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
aPiecek01598c02021-04-23 14:18:24 +020045 * the tree contains (@ref lysc_node or @ref lysp_node), because it
aPiecek874ea4d2021-04-19 12:26:36 +020046 * requires a ready-made getter functions for traversing the tree
aPiecek01598c02021-04-23 14:18:24 +020047 * (@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
aPiecek874ea4d2021-04-19 12:26:36 +020050 * traverse nodes in the tree, for example, to calculate the alignment
51 * gap before the nodes \<type\> in the YANG Tree Diagram.
aPiecek01598c02021-04-23 14:18:24 +020052 * The obtained @ref trt_node is passed to the @ref TRP_trp functions
aPiecek874ea4d2021-04-19 12:26:36 +020053 * to print the Tree diagram.
54 *
55 * @subsubsection TRP_tro tro
56 * Functions that provide an extra wrapper for the libyang library.
aPiecekef1e58e2021-04-19 13:19:44 +020057 * The Obtain functions are further specialized according to whether
aPiecek01598c02021-04-23 14:18:24 +020058 * they operate on lysp_tree (@ref TRP_trop) or lysc_tree
59 * (@ref TRP_troc). If they are general algorithms, then they have the
aPiecek3f247652021-04-19 13:40:25 +020060 * prefix \b tro_. The Obtain functions provide information to
aPiecek01598c02021-04-23 14:18:24 +020061 * @ref TRP_trb functions for printing the Tree diagram.
aPiecekef1e58e2021-04-19 13:19:44 +020062 *
63 * @subsubsection TRP_trop trop
64 * Functions for Obtaining information from Parsed schema tree.
aPiecek874ea4d2021-04-19 12:26:36 +020065 *
aPiecek3f247652021-04-19 13:40:25 +020066 * @subsubsection TRP_troc troc
67 * Functions for Obtaining information from Compiled schema tree.
68 *
aPiecek874ea4d2021-04-19 12:26:36 +020069 * @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
aPiecek01598c02021-04-23 14:18:24 +020082 * @ref TRP_tro, @ref TRP_trop or @ref TRP_troc functions because
aPiecek3f247652021-04-19 13:40:25 +020083 * they are the only ones dependent on libyang implementation.
84 * In special cases, changes will also need to be made to the
aPiecek01598c02021-04-23 14:18:24 +020085 * @ref TRP_trp functions if a special algorithm is needed to print
aPiecek3f247652021-04-19 13:40:25 +020086 * (right now this is prepared for printing list's keys
87 * and if-features).
aPiecek61d062b2020-11-02 11:05:09 +010088 */
89
aPiecek874ea4d2021-04-19 12:26:36 +020090#include <assert.h>
91#include <string.h>
92
93#include "common.h"
94#include "compat.h"
95#include "out_internal.h"
ekinzie0ab8b302022-10-10 03:03:57 -040096#include "plugins_exts.h"
97#include "plugins_types.h"
aPiecek704f8e92021-08-25 13:35:05 +020098#include "printer_schema.h"
aPiecek874ea4d2021-04-19 12:26:36 +020099#include "tree_schema_internal.h"
100#include "xpath.h"
101
aPiecek61d062b2020-11-02 11:05:09 +0100102/**
103 * @brief List of available actions.
104 */
105typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200106 TRD_PRINT = 0, /**< Normal behavior. It just prints. */
107 TRD_CHAR_COUNT /**< Characters will be counted instead of printing. */
aPiecek61d062b2020-11-02 11:05:09 +0100108} trt_ly_out_clb_arg_flag;
109
110/**
aPiecek874ea4d2021-04-19 12:26:36 +0200111 * @brief Structure is passed as 'writeclb' argument
aPiecek01598c02021-04-23 14:18:24 +0200112 * to the ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100113 */
114struct ly_out_clb_arg {
aPiecek874ea4d2021-04-19 12:26:36 +0200115 trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */
116 struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */
117 size_t counter; /**< Counter of printed characters. */
118 LY_ERR last_error; /**< The last error that occurred. If no error has occurred, it will be ::LY_SUCCESS. */
aPiecek61d062b2020-11-02 11:05:09 +0100119};
120
121/**
122 * @brief Initialize struct ly_out_clb_arg with default settings.
123 */
124#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
aPiecek874ea4d2021-04-19 12:26:36 +0200125 (struct ly_out_clb_arg) { \
126 .mode = MODE, .out = OUT, \
127 .counter = COUNTER, .last_error = LAST_ERROR \
128 }
aPiecek61d062b2020-11-02 11:05:09 +0100129
aPiecek874ea4d2021-04-19 12:26:36 +0200130/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100131 * Print getters
aPiecek874ea4d2021-04-19 12:26:36 +0200132 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100133
134/**
135 * @brief Callback functions that prints special cases.
136 *
137 * It just groups together tree context with trt_fp_print.
138 */
139struct trt_cf_print {
aPiecek874ea4d2021-04-19 12:26:36 +0200140 const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200141
aPiecek874ea4d2021-04-19 12:26:36 +0200142 void (*pf)(const struct trt_tree_ctx *, struct ly_out *); /**< Pointing to function which printing list's keys or features. */
aPiecek61d062b2020-11-02 11:05:09 +0100143};
144
145/**
146 * @brief Callback functions for printing special cases.
147 *
aPiecek874ea4d2021-04-19 12:26:36 +0200148 * Functions with the suffix 'trp' can print most of the text on
149 * output, just by setting the pointer to the string. But in some
150 * cases, it's not that simple, because its entire string is fragmented
151 * in memory. For example, for printing list's keys or if-features.
aPiecek61d062b2020-11-02 11:05:09 +0100152 * However, this depends on how the libyang library is implemented.
aPiecek3f247652021-04-19 13:40:25 +0200153 * This implementation of the printer_tree module goes through
154 * a lysp tree, but if it goes through a lysc tree, these special cases
155 * would be different.
aPiecek61d062b2020-11-02 11:05:09 +0100156 * Functions must print including spaces or delimiters between names.
157 */
158struct trt_fp_print {
159 void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */
160 void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */
161};
162
163/**
164 * @brief Package which only groups getter function.
165 */
166struct trt_pck_print {
167 const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */
168 struct trt_fp_print fps; /**< Print function. */
169};
170
171/**
172 * @brief Initialize struct trt_pck_print by parameters.
173 */
174#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
aPiecek874ea4d2021-04-19 12:26:36 +0200175 (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT}
aPiecek61d062b2020-11-02 11:05:09 +0100176
aPiecek874ea4d2021-04-19 12:26:36 +0200177/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100178 * Indent
aPiecek874ea4d2021-04-19 12:26:36 +0200179 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100180
181/**
aPiecek874ea4d2021-04-19 12:26:36 +0200182 * @brief Constants which are defined in the RFC or are observable
183 * from the pyang tool.
aPiecek61d062b2020-11-02 11:05:09 +0100184 */
185typedef enum {
186 TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100187 TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with
188 a whitespace offset of at least two characters. */
189 TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */
190 TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */
191 TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */
192 TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */
aPiecek61d062b2020-11-02 11:05:09 +0100193 TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */
194} trt_cnf_indent;
195
196/**
197 * @brief Type of indent in node.
198 */
199typedef enum {
200 TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100201 TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */
aPiecek61d062b2020-11-02 11:05:09 +0100202 TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
203} trt_indent_in_node_type;
204
205/** Constant to indicate the need to break a line. */
206#define TRD_LINEBREAK -1
207
208/**
aPiecek874ea4d2021-04-19 12:26:36 +0200209 * @brief Records the alignment between the individual
210 * elements of the node.
aPiecek61d062b2020-11-02 11:05:09 +0100211 *
aPiecek874ea4d2021-04-19 12:26:36 +0200212 * @see trp_default_indent_in_node, trp_try_normal_indent_in_node
aPiecek61d062b2020-11-02 11:05:09 +0100213 */
214struct trt_indent_in_node {
215 trt_indent_in_node_type type; /**< Type of indent in node. */
aPiecek874ea4d2021-04-19 12:26:36 +0200216 int16_t btw_name_opts; /**< Indent between node name and \<opts\>. */
217 int16_t btw_opts_type; /**< Indent between \<opts\> and \<type\>. */
aPiecek61d062b2020-11-02 11:05:09 +0100218 int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */
219};
220
221/**
222 * @brief Type of wrappers to be printed.
223 */
224typedef enum {
225 TRD_WRAPPER_TOP = 0, /**< Related to the module. */
226 TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */
227} trd_wrapper_type;
228
229/**
230 * @brief For resolving sibling symbol ('|') placement.
231 *
232 * Bit indicates where the sibling symbol must be printed.
aPiecek874ea4d2021-04-19 12:26:36 +0200233 * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS.
aPiecek61d062b2020-11-02 11:05:09 +0100234 *
aPiecek874ea4d2021-04-19 12:26:36 +0200235 * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY,
236 * trp_wrapper_set_mark, trp_wrapper_set_shift,
237 * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper
aPiecek61d062b2020-11-02 11:05:09 +0100238 */
239struct trt_wrapper {
240 trd_wrapper_type type; /**< Location of the wrapper. */
241 uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed.
242 It follows that the maximum immersion of the printable node is 64. */
243 uint32_t actual_pos; /**< Actual position in bit_marks. */
244};
245
246/**
247 * @brief Get wrapper related to the module section.
248 *
249 * @code
250 * module: <module-name>
251 * +--<node>
252 * |
253 * @endcode
254 */
255#define TRP_INIT_WRAPPER_TOP \
aPiecek874ea4d2021-04-19 12:26:36 +0200256 (struct trt_wrapper) { \
257 .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \
258 }
aPiecek61d062b2020-11-02 11:05:09 +0100259
260/**
aPiecek874ea4d2021-04-19 12:26:36 +0200261 * @brief Get wrapper related to subsection
262 * e.g. Augmenations or Groupings.
aPiecek61d062b2020-11-02 11:05:09 +0100263 *
264 * @code
265 * module: <module-name>
266 * +--<node>
267 *
268 * augment <target-node>:
269 * +--<node>
270 * @endcode
271 */
272#define TRP_INIT_WRAPPER_BODY \
aPiecek874ea4d2021-04-19 12:26:36 +0200273 (struct trt_wrapper) { \
274 .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \
275 }
aPiecek61d062b2020-11-02 11:05:09 +0100276
277/**
278 * @brief Package which only groups wrapper and indent in node.
279 */
280struct trt_pck_indent {
281 struct trt_wrapper wrapper; /**< Coded " | | " sequence. */
282 struct trt_indent_in_node in_node; /**< Indent in node. */
283};
284
285/**
286 * @brief Initialize struct trt_pck_indent by parameters.
287 */
288#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200289 (struct trt_pck_indent){ \
290 .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \
291 }
aPiecek61d062b2020-11-02 11:05:09 +0100292
aPiecek874ea4d2021-04-19 12:26:36 +0200293/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100294 * status
aPiecek874ea4d2021-04-19 12:26:36 +0200295 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100296
297/**
298 * @brief Status of the node.
299 *
aPiecek874ea4d2021-04-19 12:26:36 +0200300 * @see trp_print_status
aPiecek61d062b2020-11-02 11:05:09 +0100301 */
302typedef enum {
303 TRD_STATUS_TYPE_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100304 TRD_STATUS_TYPE_CURRENT, /**< ::LYS_STATUS_CURR */
305 TRD_STATUS_TYPE_DEPRECATED, /**< ::LYS_STATUS_DEPRC */
aPiecek874ea4d2021-04-19 12:26:36 +0200306 TRD_STATUS_TYPE_OBSOLETE /**< ::LYS_STATUS_OBSLT */
aPiecek61d062b2020-11-02 11:05:09 +0100307} trt_status_type;
308
aPiecek874ea4d2021-04-19 12:26:36 +0200309/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100310 * flags
aPiecek874ea4d2021-04-19 12:26:36 +0200311 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100312
313/**
314 * @brief Flag of the node.
315 *
aPiecek874ea4d2021-04-19 12:26:36 +0200316 * @see trp_print_flags, trp_get_flags_strlen
aPiecek61d062b2020-11-02 11:05:09 +0100317 */
318typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200319 TRD_FLAGS_TYPE_EMPTY = 0, /**< -- */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100320 TRD_FLAGS_TYPE_RW, /**< rw */
321 TRD_FLAGS_TYPE_RO, /**< ro */
322 TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */
323 TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */
324 TRD_FLAGS_TYPE_RPC, /**< -x */
325 TRD_FLAGS_TYPE_NOTIF, /**< -n */
aPiecek61d062b2020-11-02 11:05:09 +0100326 TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */
327} trt_flags_type;
328
aPiecek874ea4d2021-04-19 12:26:36 +0200329/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100330 * node_name and opts
aPiecek874ea4d2021-04-19 12:26:36 +0200331 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100332
333#define TRD_NODE_NAME_PREFIX_CHOICE "("
334#define TRD_NODE_NAME_PREFIX_CASE ":("
335#define TRD_NODE_NAME_TRIPLE_DOT "..."
336
337/**
338 * @brief Type of the node.
339 *
aPiecek874ea4d2021-04-19 12:26:36 +0200340 * Used mainly to complete the correct \<opts\> next to or
341 * around the \<name\>.
aPiecek61d062b2020-11-02 11:05:09 +0100342 */
343typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200344 TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100345 TRD_NODE_CASE, /**< For case node. :(\<name\>) */
346 TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */
347 TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */
348 TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */
349 TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */
aPiecekbca57772022-10-13 13:51:59 +0200350 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list. \<name\>* */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100351 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
352 TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */
aPiecek874ea4d2021-04-19 12:26:36 +0200353 TRD_NODE_TRIPLE_DOT /**< For collapsed sibling nodes and their children. Special case which doesn't belong here very well. */
aPiecek61d062b2020-11-02 11:05:09 +0100354} trt_node_type;
355
356/**
357 * @brief Type of node and his name.
358 *
aPiecek874ea4d2021-04-19 12:26:36 +0200359 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
aPiecek61d062b2020-11-02 11:05:09 +0100360 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
361 */
362struct trt_node_name {
363 trt_node_type type; /**< Type of the node relevant for printing. */
aPiecekbca57772022-10-13 13:51:59 +0200364 ly_bool keys; /**< Set to 1 if [\<keys\>] are to be printed. Valid for some types only. */
aPiecek34fa3772021-05-21 12:35:46 +0200365 const char *module_prefix; /**< If the node is augmented into the tree from another module,
366 so this is the prefix of that module. */
aPiecek61d062b2020-11-02 11:05:09 +0100367 const char *str; /**< Name of the node. */
368};
369
370/**
371 * @brief Create struct trt_node_name as empty.
372 */
373#define TRP_EMPTY_NODE_NAME \
aPiecek874ea4d2021-04-19 12:26:36 +0200374 (struct trt_node_name) { \
aPiecekbca57772022-10-13 13:51:59 +0200375 .type = TRD_NODE_ELSE, .keys = 0, .module_prefix = NULL, .str = NULL \
aPiecek874ea4d2021-04-19 12:26:36 +0200376 }
aPiecek61d062b2020-11-02 11:05:09 +0100377
378/**
379 * @brief Check if struct trt_node_name is empty.
380 */
381#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
382 !NODE_NAME.str
383
aPiecek874ea4d2021-04-19 12:26:36 +0200384/**
385 * @brief Every \<opts\> mark except string of list's keys
386 * has a length of one.
387 */
aPiecek61d062b2020-11-02 11:05:09 +0100388#define TRD_OPTS_MARK_LENGTH 1
389
aPiecek874ea4d2021-04-19 12:26:36 +0200390/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100391 * type
aPiecek874ea4d2021-04-19 12:26:36 +0200392 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100393
394/**
395 * @brief Type of the \<type\>
396 */
397typedef enum {
398 TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100399 TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
400 TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm.
aPiecek874ea4d2021-04-19 12:26:36 +0200401 So set type as ::TRD_TYPE_TARGET. */
aPiecek61d062b2020-11-02 11:05:09 +0100402 TRD_TYPE_EMPTY /**< Type is not used at all. */
403} trt_type_type;
404
405/**
406 * @brief \<type\> in the \<node\>.
407 *
aPiecek874ea4d2021-04-19 12:26:36 +0200408 * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
aPiecek61d062b2020-11-02 11:05:09 +0100409 */
410struct trt_type {
411 trt_type_type type; /**< Type of the \<type\>. */
412 const char *str; /**< Path or name of the type. */
413};
414
415/**
416 * @brief Create empty struct trt_type.
417 */
418#define TRP_EMPTY_TRT_TYPE \
419 (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}
420
421/**
422 * @brief Check if struct trt_type is empty.
423 */
424#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
425 TYPE_OF_TYPE.type == TRD_TYPE_EMPTY
426
427/**
428 * @brief Initialize struct trt_type by parameters.
429 */
430#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
431 (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}
432
aPiecek874ea4d2021-04-19 12:26:36 +0200433/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100434 * node
aPiecek874ea4d2021-04-19 12:26:36 +0200435 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100436
437/**
438 * @brief \<node\> data for printing.
439 *
aPiecek874ea4d2021-04-19 12:26:36 +0200440 * It contains RFC's:
441 * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
aPiecek61d062b2020-11-02 11:05:09 +0100442 * Item \<opts\> is moved to part struct trt_node_name.
aPiecek874ea4d2021-04-19 12:26:36 +0200443 * For printing [\<keys\>] and if-features is required special
444 * functions which prints them.
aPiecek61d062b2020-11-02 11:05:09 +0100445 *
aPiecek874ea4d2021-04-19 12:26:36 +0200446 * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty,
447 * trp_print_node_up_to_name, trp_print_divided_node_up_to_name,
448 * trp_print_node
aPiecek61d062b2020-11-02 11:05:09 +0100449 */
450struct trt_node {
aPiecek7ed8d032022-10-10 12:32:27 +0200451 trt_status_type status; /**< \<status\>. */
452 trt_flags_type flags; /**< \<flags\>. */
453 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
454 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
455 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and
456 will be printed by trt_fp_print.print_features_names callback. */
457 ly_bool last_one; /**< Information about whether the node is the last. */
458 struct lysc_ext_instance *mount; /**< Mount-point extension if flags == TRD_FLAGS_TYPE_MOUNT_POINT */
aPiecek61d062b2020-11-02 11:05:09 +0100459};
460
461/**
462 * @brief Create struct trt_node as empty.
463 */
464#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200465 (struct trt_node) { \
466 .status = TRD_STATUS_TYPE_EMPTY, \
467 .flags = TRD_FLAGS_TYPE_EMPTY, \
468 .name = TRP_EMPTY_NODE_NAME, \
469 .type = TRP_EMPTY_TRT_TYPE, \
470 .iffeatures = 0, \
ekinzie0ab8b302022-10-10 03:03:57 -0400471 .last_one = 1, \
472 .mount = NULL \
aPiecek874ea4d2021-04-19 12:26:36 +0200473 }
aPiecek61d062b2020-11-02 11:05:09 +0100474
475/**
476 * @brief Package which only groups indent and node.
477 */
478struct trt_pair_indent_node {
479 struct trt_indent_in_node indent;
480 struct trt_node node;
481};
482
483/**
484 * @brief Initialize struct trt_pair_indent_node by parameters.
485 */
486#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200487 (struct trt_pair_indent_node) { \
488 .indent = INDENT_IN_NODE, .node = NODE \
489 }
aPiecek61d062b2020-11-02 11:05:09 +0100490
aPiecek874ea4d2021-04-19 12:26:36 +0200491/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100492 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200493 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100494
495#define TRD_TOP_KEYWORD_MODULE "module"
496#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
497
498#define TRD_BODY_KEYWORD_AUGMENT "augment"
499#define TRD_BODY_KEYWORD_RPC "rpcs"
500#define TRD_BODY_KEYWORD_NOTIF "notifications"
501#define TRD_BODY_KEYWORD_GROUPING "grouping"
502#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
503
504/**
505 * @brief Type of the trt_keyword.
506 */
507typedef enum {
508 TRD_KEYWORD_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100509 TRD_KEYWORD_MODULE,
510 TRD_KEYWORD_SUBMODULE,
511 TRD_KEYWORD_AUGMENT,
512 TRD_KEYWORD_RPC,
513 TRD_KEYWORD_NOTIF,
514 TRD_KEYWORD_GROUPING,
aPiecek61d062b2020-11-02 11:05:09 +0100515 TRD_KEYWORD_YANG_DATA
516} trt_keyword_type;
517
518/**
519 * @brief Main sign of the tree nodes.
520 *
aPiecek874ea4d2021-04-19 12:26:36 +0200521 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100522 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
523 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
524 * trp_keyword_type_strlen
525 *
526 */
527struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200528 trt_keyword_type type; /**< String containing some of the top or body keyword. */
529 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100530};
531
532/**
533 * @brief Create struct trt_keyword_stmt as empty.
534 */
535#define TRP_EMPTY_KEYWORD_STMT \
536 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
537
538/**
539 * @brief Check if struct trt_keyword_stmt is empty.
540 */
541#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
542 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
543
544/**
545 * @brief Initialize struct trt_keyword_stmt by parameters.
546 */
547#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
548 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
549
aPiecek874ea4d2021-04-19 12:26:36 +0200550/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100551 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200552 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100553
554struct trt_parent_cache;
555
556/**
557 * @brief Functions that change the state of the tree_ctx structure.
558 *
aPiecek3f247652021-04-19 13:40:25 +0200559 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200560 * for the 'trp' printing functions and are also called from the
561 * 'trb' browsing functions when walking through a tree. These callback
562 * functions need to be checked or reformulated if changes to the
563 * libyang library affect the printing tree. For all, if the value
564 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
565 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100566 */
567struct trt_fp_modify_ctx {
568 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
569 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
570 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
571 struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */
aPiecek61d062b2020-11-02 11:05:09 +0100572};
573
aPiecek874ea4d2021-04-19 12:26:36 +0200574/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100575 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200576 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100577
578/**
579 * @brief Functions that do not change the state of the tree_structure.
580 *
581 * For details see trt_fp_modify_ctx.
582 */
583struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200584 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
585 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
586 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100587};
588
aPiecek874ea4d2021-04-19 12:26:36 +0200589/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100590 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200591 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100592
593/**
aPiecek874ea4d2021-04-19 12:26:36 +0200594 * @brief A set of all necessary functions that must be provided
595 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100596 */
597struct trt_fp_all {
598 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
599 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
600 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
601};
602
aPiecek874ea4d2021-04-19 12:26:36 +0200603/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100604 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200605 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100606
607/**
aPiecek01598c02021-04-23 14:18:24 +0200608 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100609 */
610struct trt_printer_ctx {
611 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200612 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100613 size_t max_line_length; /**< The maximum number of characters that can be
614 printed on one line, including the last. */
615};
616
aPiecek874ea4d2021-04-19 12:26:36 +0200617/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100618 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200619 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100620
621/**
622 * @brief The name of the section to which the node belongs.
623 */
624typedef enum {
625 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100626 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
627 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
628 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
629 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
aPiecek61d062b2020-11-02 11:05:09 +0100630 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
631} trt_actual_section;
632
633/**
634 * @brief Types of nodes that have some effect on their children.
635 */
636typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200637 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100638 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
639 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
aPiecek874ea4d2021-04-19 12:26:36 +0200640 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100641} trt_ancestor_type;
642
643/**
644 * @brief Saved information when browsing the tree downwards.
645 *
aPiecek874ea4d2021-04-19 12:26:36 +0200646 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200647 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200648 * this structures during their recursive calls. This functions do not
649 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200650 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200651 * structure, can return a node with the correct data. The word
652 * \b parent is in the structure name, because this data refers to
653 * the last parent and at the same time the states of its
654 * ancestors data. Only the function jumping on the child
655 * (next_child(...)) creates this structure, because the pointer
656 * to the current node moves down the tree. It's like passing
657 * the genetic code to children. Some data must be inherited and
658 * there are two approaches to this problem. Either it will always
659 * be determined which inheritance states belong to the current node
660 * (which can lead to regular travel to the root node) or
661 * the inheritance states will be stored during the recursive calls.
662 * So the problem was solved by the second option. Why does
663 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200664 * the lysp tree. For walks through the lysc tree is trt_parent_cache
665 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100666 *
aPiecek874ea4d2021-04-19 12:26:36 +0200667 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100668 */
669struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200670 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
671 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
672 uint16_t lys_config; /**< Inherited config W or R. */
673 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100674};
675
676/**
677 * @brief Return trt_parent_cache filled with default values.
678 */
679#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200680 (struct trt_parent_cache) { \
681 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
682 .lys_config = LYS_CONFIG_W, .last_list = NULL \
683 }
aPiecek61d062b2020-11-02 11:05:09 +0100684
685/**
686 * @brief Main structure for browsing the libyang tree
687 */
688struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200689 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
690 It is assumed that once set, it does not change.
691 If it is true then trt_tree_ctx.pn and
692 trt_tree_ctx.tpn are not used.
693 If it is false then trt_tree_ctx.cn is not used. */
ekinzie0ab8b302022-10-10 03:03:57 -0400694 ly_bool mounted; /**< This tree is a mounted schema */
aPiecek96baa7f2021-04-23 12:32:00 +0200695 trt_actual_section section; /**< To which section pn points. */
696 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
697 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
698 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200699
aPiecek96baa7f2021-04-23 12:32:00 +0200700 union {
701 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
702 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
703 is set to TRD_SECT_YANG_DATA. */
704 };
705 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
ekinzie0ab8b302022-10-10 03:03:57 -0400706 const struct ly_set *parent_refs; /**< List of schema nodes for top-level nodes found in mount
707 point parent references */
aPiecek40f22402022-10-14 10:48:08 +0200708 LY_ERR last_error; /**< Error value during printing. */
aPiecek61d062b2020-11-02 11:05:09 +0100709};
710
aPiecek3f247652021-04-19 13:40:25 +0200711/**
aPiecekbbc02932021-05-21 07:19:41 +0200712 * @brief Check if lysp node is available from
713 * the current compiled node.
714 *
715 * Use only if trt_tree_ctx.lysc_tree is set to true.
716 */
717#define TRP_TREE_CTX_LYSP_NODE_PRESENT(CN) \
718 (CN->priv)
719
720/**
aPiecek3f247652021-04-19 13:40:25 +0200721 * @brief Get lysp_node from trt_tree_ctx.cn.
aPiecekbbc02932021-05-21 07:19:41 +0200722 *
723 * Use only if :TRP_TREE_CTX_LYSP_NODE_PRESENT returns true
724 * for that node.
aPiecek3f247652021-04-19 13:40:25 +0200725 */
726#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
727 ((const struct lysp_node *)CN->priv)
728
aPiecek01598c02021-04-23 14:18:24 +0200729/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100730typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
731
aPiecekef1e58e2021-04-19 13:19:44 +0200732/**
733 * @brief Simple getter functions for lysp and lysc nodes.
734 *
735 * This structure is useful if we have a general algorithm
736 * (tro function) that can be used for both lysc and lysp nodes.
737 * Thanks to this structure, we prevent code redundancy.
738 * We don't have to write basically the same algorithm twice
739 * for lysp and lysc trees.
740 */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100741struct tro_getters {
aPiecekef1e58e2021-04-19 13:19:44 +0200742 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
743 const void *(*next)(const void *); /**< Get sibling. */
744 const void *(*parent)(const void *); /**< Get parent. */
745 const void *(*child)(const void *); /**< Get child. */
746 const void *(*actions)(const void *); /**< Get actions. */
747 const void *(*action_input)(const void *); /**< Get input action from action node. */
748 const void *(*action_output)(const void *); /**< Get output action from action node. */
749 const void *(*notifs)(const void *); /**< Get notifs. */
750};
751
aPiecek874ea4d2021-04-19 12:26:36 +0200752/**********************************************************************
ekinzie0ab8b302022-10-10 03:03:57 -0400753 * Forward declarations
754 *********************************************************************/
aPiecek7ed8d032022-10-10 12:32:27 +0200755static LY_ERR trb_print_mount_point(const struct lysc_ext_instance *ext, const struct trt_wrapper wr, struct trt_printer_ctx *pc);
ekinzie0ab8b302022-10-10 03:03:57 -0400756
757/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100758 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200759 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100760
761/**
762 * @brief Print a substring but limited to the maximum length.
763 * @param[in] str is pointer to source.
764 * @param[in] len is number of characters to be printed.
765 * @param[in,out] out is output handler.
766 * @return str parameter shifted by len.
767 */
768static const char *
769trg_print_substr(const char *str, size_t len, struct ly_out *out)
770{
771 for (size_t i = 0; i < len; i++) {
772 ly_print_(out, "%c", str[0]);
773 str++;
774 }
775 return str;
776}
777
778/**
779 * @brief Pointer is not NULL and does not point to an empty string.
780 * @param[in] str is pointer to string to be checked.
781 * @return 1 if str pointing to non empty string otherwise 0.
782 */
783static ly_bool
784trg_charptr_has_data(const char *str)
785{
786 return (str) && (str[0] != '\0');
787}
788
789/**
aPiecek874ea4d2021-04-19 12:26:36 +0200790 * @brief Check if @p word in @p src is present where words are
791 * delimited by @p delim.
792 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100793 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200794 * @param[in] delim is delimiter between @p words in @p src.
795 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100796 */
797static ly_bool
798trg_word_is_present(const char *src, const char *word, char delim)
799{
800 const char *hit;
801
802 if ((!src) || (src[0] == '\0') || (!word)) {
803 return 0;
804 }
805
806 hit = strstr(src, word);
807
808 if (hit) {
809 /* word was founded at the begin of src
810 * OR it match somewhere after delim
811 */
812 if ((hit == src) || (hit[-1] == delim)) {
813 /* end of word was founded at the end of src
814 * OR end of word was match somewhere before delim
815 */
816 char delim_or_end = (hit + strlen(word))[0];
Michal Vasko26bbb272022-08-02 14:54:33 +0200817
aPiecek61d062b2020-11-02 11:05:09 +0100818 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
819 return 1;
820 }
821 }
822 /* after -> hit is just substr and it's not the whole word */
823 /* jump to the next word */
824 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
825 /* skip delim */
826 src = src[0] == '\0' ? src : src + 1;
827 /* continue with searching */
828 return trg_word_is_present(src, word, delim);
829 } else {
830 return 0;
831 }
832}
833
aPiecek874ea4d2021-04-19 12:26:36 +0200834/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100835 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200836 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100837
838/**
aPiecek01598c02021-04-23 14:18:24 +0200839 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100840 *
aPiecek874ea4d2021-04-19 12:26:36 +0200841 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100842 * @param[in] buf contains input characters
843 * @param[in] count is number of characters in buf.
844 * @return Number of printed bytes.
845 * @return Negative value in case of error.
846 */
847static ssize_t
848trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
849{
850 LY_ERR erc = LY_SUCCESS;
851 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
852
853 switch (data->mode) {
854 case TRD_PRINT:
855 erc = ly_write_(data->out, buf, count);
856 break;
857 case TRD_CHAR_COUNT:
858 data->counter = data->counter + count;
859 break;
860 default:
861 break;
862 }
863
864 if (erc != LY_SUCCESS) {
865 data->last_error = erc;
866 return -1;
867 } else {
868 return count;
869 }
870}
871
872/**
873 * @brief Check that indent in node can be considered as equivalent.
874 * @param[in] first is the first indent in node.
875 * @param[in] second is the second indent in node.
876 * @return 1 if indents are equivalent otherwise 0.
877 */
878static ly_bool
879trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
880{
881 const ly_bool a = first.type == second.type;
882 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
883 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
884 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
885
886 return a && b && c && d;
887}
888
889/**
aPiecek874ea4d2021-04-19 12:26:36 +0200890 * @brief Setting space character because node is last sibling.
891 * @param[in] wr is wrapper over which the shift operation
892 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100893 * @return New shifted wrapper.
894 */
895static struct trt_wrapper
896trp_wrapper_set_shift(struct trt_wrapper wr)
897{
898 assert(wr.actual_pos < 64);
899 /* +--<node>
900 * +--<node>
901 */
902 wr.actual_pos++;
903 return wr;
904}
905
906/**
aPiecek874ea4d2021-04-19 12:26:36 +0200907 * @brief Setting '|' symbol because node is divided or
908 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100909 * @param[in] wr is source of wrapper.
910 * @return New wrapper which is marked at actual position and shifted.
911 */
912static struct trt_wrapper
913trp_wrapper_set_mark(struct trt_wrapper wr)
914{
915 assert(wr.actual_pos < 64);
916 wr.bit_marks1 |= 1U << wr.actual_pos;
917 return trp_wrapper_set_shift(wr);
918}
919
920/**
ekinzie0ab8b302022-10-10 03:03:57 -0400921 * @brief Set '|' symbol to connect current level nodes in a module.
922 * This is only used to connect all top-level nodes in all modules under
923 * a schema mount point.
924 * @param[in] wr is the wrapper to be marked
925 * @return New wrapper which is marked at actual position.
926 */
927static struct trt_wrapper
928trp_wrapper_set_mark_top(struct trt_wrapper wr)
929{
930 wr.bit_marks1 |= 1U << wr.actual_pos;
931 return wr;
932}
933
934/**
aPiecek61d062b2020-11-02 11:05:09 +0100935 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
936 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200937 * @param[in] last_one is flag. Value 1 saying if the node is the last
938 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100939 * @return New wrapper for the actual node.
940 */
941static struct trt_wrapper
942trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
943{
944 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
945}
946
947/**
948 * @brief Test if the wrappers are equivalent.
949 * @param[in] first is the first wrapper.
950 * @param[in] second is the second wrapper.
951 * @return 1 if the wrappers are equivalent otherwise 0.
952 */
953static ly_bool
954trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
955{
956 const ly_bool a = first.type == second.type;
957 const ly_bool b = first.bit_marks1 == second.bit_marks1;
958 const ly_bool c = first.actual_pos == second.actual_pos;
959
960 return a && b && c;
961}
962
963/**
964 * @brief Print " | " sequence on line.
965 * @param[in] wr is wrapper to be printed.
966 * @param[in,out] out is output handler.
967 */
968static void
969trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
970{
971 uint32_t lb;
972
973 if (wr.type == TRD_WRAPPER_TOP) {
974 lb = TRD_INDENT_LINE_BEGIN;
975 } else if (wr.type == TRD_WRAPPER_BODY) {
976 lb = TRD_INDENT_LINE_BEGIN * 2;
977 } else {
978 lb = TRD_INDENT_LINE_BEGIN;
979 }
980
981 ly_print_(out, "%*c", lb, ' ');
982
983 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
984 return;
985 }
986
987 for (uint32_t i = 0; i < wr.actual_pos; i++) {
988 /** Test if the bit on the index is set. */
989 if ((wr.bit_marks1 >> i) & 1U) {
990 ly_print_(out, "|");
991 } else {
992 ly_print_(out, " ");
993 }
994
995 if (i != wr.actual_pos) {
996 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
997 }
998 }
999}
1000
1001/**
1002 * @brief Check if struct trt_node is empty.
1003 * @param[in] node is item to test.
1004 * @return 1 if node is considered empty otherwise 0.
1005 */
1006static ly_bool
1007trp_node_is_empty(struct trt_node node)
1008{
1009 const ly_bool a = !node.iffeatures;
1010 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
1011 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
1012 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
1013 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
1014
1015 return a && b && c && d && e;
1016}
1017
1018/**
aPiecek874ea4d2021-04-19 12:26:36 +02001019 * @brief Check if [\<keys\>], \<type\> and
1020 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +01001021 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +02001022 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
1023 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +01001024 */
1025static ly_bool
1026trp_node_body_is_empty(struct trt_node node)
1027{
1028 const ly_bool a = !node.iffeatures;
1029 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
aPiecekbca57772022-10-13 13:51:59 +02001030 const ly_bool c = !node.name.keys;
aPiecek61d062b2020-11-02 11:05:09 +01001031
1032 return a && b && c;
1033}
1034
1035/**
1036 * @brief Print \<status\> of the node.
1037 * @param[in] status_type is type of status.
1038 * @param[in,out] out is output handler.
1039 */
1040static void
1041trp_print_status(trt_status_type status_type, struct ly_out *out)
1042{
1043 switch (status_type) {
1044 case TRD_STATUS_TYPE_CURRENT:
1045 ly_print_(out, "%c", '+');
1046 break;
1047 case TRD_STATUS_TYPE_DEPRECATED:
1048 ly_print_(out, "%c", 'x');
1049 break;
1050 case TRD_STATUS_TYPE_OBSOLETE:
1051 ly_print_(out, "%c", 'o');
1052 break;
1053 default:
1054 break;
1055 }
1056}
1057
1058/**
1059 * @brief Print \<flags\>.
1060 * @param[in] flags_type is type of \<flags\>.
1061 * @param[in,out] out is output handler.
1062 */
1063static void
1064trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1065{
1066 switch (flags_type) {
1067 case TRD_FLAGS_TYPE_RW:
1068 ly_print_(out, "%s", "rw");
1069 break;
1070 case TRD_FLAGS_TYPE_RO:
1071 ly_print_(out, "%s", "ro");
1072 break;
1073 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1074 ly_print_(out, "%s", "-w");
1075 break;
1076 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1077 ly_print_(out, "%s", "-u");
1078 break;
1079 case TRD_FLAGS_TYPE_RPC:
1080 ly_print_(out, "%s", "-x");
1081 break;
1082 case TRD_FLAGS_TYPE_NOTIF:
1083 ly_print_(out, "%s", "-n");
1084 break;
1085 case TRD_FLAGS_TYPE_MOUNT_POINT:
1086 ly_print_(out, "%s", "mp");
1087 break;
1088 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001089 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001090 break;
1091 }
1092}
1093
1094/**
1095 * @brief Get size of the \<flags\>.
1096 * @param[in] flags_type is type of \<flags\>.
1097 * @return 0 if flags_type is not set otherwise 2.
1098 */
1099static size_t
1100trp_get_flags_strlen(trt_flags_type flags_type)
1101{
1102 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1103}
1104
1105/**
1106 * @brief Print entire struct trt_node_name structure.
1107 * @param[in] node_name is item to print.
1108 * @param[in,out] out is output handler.
1109 */
1110static void
1111trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1112{
1113 const char *mod_prefix;
1114 const char *colon;
1115 const char trd_node_name_suffix_choice[] = ")";
1116 const char trd_node_name_suffix_case[] = ")";
1117 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1118 const char trd_opts_container[] = "!"; /**< For a presence container. */
1119 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1120 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1121 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1122
1123 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1124 return;
1125 }
1126
1127 if (node_name.module_prefix) {
1128 mod_prefix = node_name.module_prefix;
1129 colon = ":";
1130 } else {
1131 mod_prefix = "";
1132 colon = "";
1133 }
1134
1135 switch (node_name.type) {
1136 case TRD_NODE_ELSE:
1137 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1138 break;
1139 case TRD_NODE_CASE:
1140 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1141 break;
1142 case TRD_NODE_CHOICE:
1143 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1144 break;
1145 case TRD_NODE_OPTIONAL_CHOICE:
1146 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);
1147 break;
1148 case TRD_NODE_OPTIONAL:
1149 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1150 break;
1151 case TRD_NODE_CONTAINER:
1152 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1153 break;
1154 case TRD_NODE_LISTLEAFLIST:
1155 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1156 break;
aPiecek61d062b2020-11-02 11:05:09 +01001157 case TRD_NODE_TOP_LEVEL1:
1158 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1159 break;
1160 case TRD_NODE_TOP_LEVEL2:
1161 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1162 break;
1163 case TRD_NODE_TRIPLE_DOT:
1164 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1165 break;
1166 default:
1167 break;
1168 }
1169}
1170
1171/**
aPiecek874ea4d2021-04-19 12:26:36 +02001172 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1173 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001174 * @param[in] node_name is structure containing the 'mark'.
1175 * @return 1 if contain otherwise 0.
1176 */
1177static ly_bool
1178trp_mark_is_used(struct trt_node_name node_name)
1179{
1180 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1181 return 0;
aPiecekbca57772022-10-13 13:51:59 +02001182 } else if (node_name.keys) {
1183 return 0;
aPiecek61d062b2020-11-02 11:05:09 +01001184 }
1185
1186 switch (node_name.type) {
1187 case TRD_NODE_ELSE:
1188 case TRD_NODE_CASE:
aPiecek61d062b2020-11-02 11:05:09 +01001189 return 0;
1190 default:
1191 return 1;
1192 }
1193}
1194
1195/**
1196 * @brief Print opts keys.
1197 * @param[in] node_name contains type of the node with his name.
1198 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001199 * @param[in] cf is basically a pointer to the function that prints
1200 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001201 * @param[in,out] out is output handler.
1202 */
1203static void
1204trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1205{
aPiecekbca57772022-10-13 13:51:59 +02001206 if (!node_name.keys) {
aPiecek61d062b2020-11-02 11:05:09 +01001207 return;
1208 }
1209
1210 /* <name><mark>___<keys>*/
1211 if (btw_name_opts > 0) {
1212 ly_print_(out, "%*c", btw_name_opts, ' ');
1213 }
1214 ly_print_(out, "[");
1215 cf.pf(cf.ctx, out);
1216 ly_print_(out, "]");
1217}
1218
1219/**
1220 * @brief Print entire struct trt_type structure.
1221 * @param[in] type is item to print.
1222 * @param[in,out] out is output handler.
1223 */
1224static void
1225trp_print_type(struct trt_type type, struct ly_out *out)
1226{
1227 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1228 return;
1229 }
1230
1231 switch (type.type) {
1232 case TRD_TYPE_NAME:
1233 ly_print_(out, "%s", type.str);
1234 break;
1235 case TRD_TYPE_TARGET:
1236 ly_print_(out, "-> %s", type.str);
1237 break;
1238 case TRD_TYPE_LEAFREF:
1239 ly_print_(out, "leafref");
1240 default:
1241 break;
1242 }
1243}
1244
1245/**
1246 * @brief Print all iffeatures of node
1247 *
1248 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001249 * @param[in] cf is basically a pointer to the function that prints
1250 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001251 * @param[in,out] out is output handler.
1252 */
1253static void
1254trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1255{
1256 if (iffeature_flag) {
1257 ly_print_(out, "{");
1258 cf.pf(cf.ctx, out);
1259 ly_print_(out, "}?");
1260 }
1261}
1262
1263/**
1264 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1265 * @param[in] node contains items to print.
1266 * @param[in] out is output handler.
1267 */
1268static void
1269trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1270{
1271 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1272 trp_print_node_name(node.name, out);
1273 return;
1274 }
1275 /* <status>--<flags> */
1276 trp_print_status(node.status, out);
1277 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001278 /* If the node is a case node, there is no space before the <name>
1279 * also case node has no flags.
1280 */
aPiecek61d062b2020-11-02 11:05:09 +01001281 if (node.name.type != TRD_NODE_CASE) {
1282 trp_print_flags(node.flags, out);
1283 ly_print_(out, " ");
1284 }
1285 /* <name> */
1286 trp_print_node_name(node.name, out);
1287}
1288
1289/**
aPiecek874ea4d2021-04-19 12:26:36 +02001290 * @brief Print alignment (spaces) instead of
1291 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001292 * @param[in] node contains items to print.
1293 * @param[in] out is output handler.
1294 */
1295static void
1296trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1297{
1298 uint32_t space = trp_get_flags_strlen(node.flags);
1299
1300 if (node.name.type == TRD_NODE_CASE) {
1301 /* :(<name> */
1302 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1303 } else if (node.name.type == TRD_NODE_CHOICE) {
1304 /* (<name> */
1305 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1306 } else {
1307 /* _<name> */
1308 space += strlen(" ");
1309 }
1310
1311 /* <name>
1312 * __
1313 */
1314 space += TRD_INDENT_LONG_LINE_BREAK;
1315
1316 ly_print_(out, "%*c", space, ' ');
1317}
1318
1319/**
1320 * @brief Print struct trt_node structure.
1321 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001322 * @param[in] pck package of functions for
1323 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001324 * @param[in] indent is the indent in node.
1325 * @param[in,out] out is output handler.
1326 */
1327static void
1328trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1329{
1330 ly_bool triple_dot;
1331 ly_bool divided;
1332 struct trt_cf_print cf_print_keys;
1333 struct trt_cf_print cf_print_iffeatures;
1334
1335 if (trp_node_is_empty(node)) {
1336 return;
1337 }
1338
1339 /* <status>--<flags> <name><opts> <type> <if-features> */
1340 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1341 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1342
1343 if (triple_dot) {
1344 trp_print_node_name(node.name, out);
1345 return;
1346 } else if (!divided) {
1347 trp_print_node_up_to_name(node, out);
1348 } else {
1349 trp_print_divided_node_up_to_name(node, out);
1350 }
1351
1352 /* <opts> */
1353 /* <name>___<opts>*/
1354 cf_print_keys.ctx = pck.tree_ctx;
1355 cf_print_keys.pf = pck.fps.print_keys;
1356
1357 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1358
1359 /* <opts>__<type> */
1360 if (indent.btw_opts_type > 0) {
1361 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1362 }
1363
1364 /* <type> */
1365 trp_print_type(node.type, out);
1366
1367 /* <type>__<iffeatures> */
1368 if (indent.btw_type_iffeatures > 0) {
1369 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1370 }
1371
1372 /* <iffeatures> */
1373 cf_print_iffeatures.ctx = pck.tree_ctx;
1374 cf_print_iffeatures.pf = pck.fps.print_features_names;
1375
1376 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1377}
1378
1379/**
aPiecek874ea4d2021-04-19 12:26:36 +02001380 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001381 * @param[in] ks is keyword statement to print.
1382 * @param[in,out] out is output handler
1383 */
1384static void
1385trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1386{
1387 switch (ks.type) {
1388 case TRD_KEYWORD_MODULE:
1389 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1390 return;
1391 case TRD_KEYWORD_SUBMODULE:
1392 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1393 return;
1394 default:
1395 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1396 switch (ks.type) {
1397 case TRD_KEYWORD_AUGMENT:
1398 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1399 break;
1400 case TRD_KEYWORD_RPC:
1401 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1402 break;
1403 case TRD_KEYWORD_NOTIF:
1404 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1405 break;
1406 case TRD_KEYWORD_GROUPING:
1407 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1408 break;
1409 case TRD_KEYWORD_YANG_DATA:
1410 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1411 break;
1412 default:
1413 break;
1414 }
1415 break;
1416 }
1417}
1418
1419/**
1420 * @brief Get string length of stored keyword.
1421 * @param[in] type is type of the keyword statement.
1422 * @return length of the keyword statement name.
1423 */
1424static size_t
1425trp_keyword_type_strlen(trt_keyword_type type)
1426{
1427 switch (type) {
1428 case TRD_KEYWORD_MODULE:
1429 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1430 case TRD_KEYWORD_SUBMODULE:
1431 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1432 case TRD_KEYWORD_AUGMENT:
1433 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1434 case TRD_KEYWORD_RPC:
1435 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1436 case TRD_KEYWORD_NOTIF:
1437 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1438 case TRD_KEYWORD_GROUPING:
1439 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1440 case TRD_KEYWORD_YANG_DATA:
1441 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1442 default:
1443 return 0;
1444 }
1445}
1446
1447/**
aPiecek874ea4d2021-04-19 12:26:36 +02001448 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001449 * @param[in] ks is keyword statement structure.
1450 * @param[in] mll is max line length.
1451 * @param[in,out] out is output handler.
1452 */
1453static void
1454trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1455{
1456 uint32_t ind_initial;
1457 uint32_t ind_divided;
1458 /* flag if path must be splitted to more lines */
1459 ly_bool linebreak_was_set;
1460 /* flag if at least one subpath was printed */
1461 ly_bool subpath_printed;
1462 /* the sum of the sizes of the substrings on the current line */
1463 uint32_t how_far;
1464 /* pointer to start of the subpath */
1465 const char *sub_ptr;
1466 /* size of subpath from sub_ptr */
1467 size_t sub_len;
1468
1469 if ((!ks.str) || (ks.str[0] == '\0')) {
1470 return;
1471 }
1472
1473 /* module name cannot be splitted */
1474 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1475 ly_print_(out, "%s", ks.str);
1476 return;
1477 }
1478
1479 /* after -> for trd_keyword_stmt_body do */
1480
1481 /* set begin indentation */
1482 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1483 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1484 linebreak_was_set = 0;
1485 subpath_printed = 0;
1486 how_far = 0;
1487 sub_ptr = ks.str;
1488 sub_len = 0;
1489
1490 while (sub_ptr[0] != '\0') {
1491 uint32_t ind;
1492 /* skip slash */
1493 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
Michal Vasko26bbb272022-08-02 14:54:33 +02001494
aPiecek61d062b2020-11-02 11:05:09 +01001495 /* get position of the end of substr */
1496 tmp = strchr(tmp, '/');
1497 /* set correct size if this is a last substring */
1498 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1499 /* actualize sum of the substring's sizes on the current line */
1500 how_far += sub_len;
1501 /* correction due to colon character if it this is last substring */
1502 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1503 /* choose indentation which depends on
1504 * whether the string is printed on multiple lines or not
1505 */
1506 ind = linebreak_was_set ? ind_divided : ind_initial;
1507 if (ind + how_far <= mll) {
1508 /* printing before max line length */
1509 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1510 subpath_printed = 1;
1511 } else {
1512 /* printing on new line */
1513 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001514 /* first subpath is too long
1515 * but print it at first line anyway
1516 */
aPiecek61d062b2020-11-02 11:05:09 +01001517 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1518 subpath_printed = 1;
1519 continue;
1520 }
1521 ly_print_(out, "\n");
1522 ly_print_(out, "%*c", ind_divided, ' ');
1523 linebreak_was_set = 1;
1524 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1525 how_far = sub_len;
1526 subpath_printed = 1;
1527 }
1528 }
1529}
1530
1531/**
aPiecek874ea4d2021-04-19 12:26:36 +02001532 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001533 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001534 * @param[in] grp_has_data is flag only for grouping section.
1535 * Set to 1 if grouping section has some nodes.
1536 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001537 * @param[in,out] out is output handler.
1538 */
1539static void
aPiecekdc8fd572021-04-19 10:47:23 +02001540trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001541{
1542 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001543 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1544 return;
1545 } else {
1546 ly_print_(out, ":");
1547 }
aPiecek61d062b2020-11-02 11:05:09 +01001548 }
1549}
1550
1551/**
1552 * @brief Print entire struct trt_keyword_stmt structure.
1553 * @param[in] ks is item to print.
1554 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001555 * @param[in] grp_has_data is flag only for grouping section.
1556 * Set to 1 if grouping section has some nodes.
1557 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001558 * @param[in,out] out is output handler.
1559 */
1560static void
aPiecek874ea4d2021-04-19 12:26:36 +02001561trp_print_keyword_stmt(struct trt_keyword_stmt ks, size_t mll, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001562{
1563 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1564 return;
1565 }
1566 trt_print_keyword_stmt_begin(ks, out);
1567 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001568 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001569}
1570
aPiecek874ea4d2021-04-19 12:26:36 +02001571/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001572 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001573 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001574
1575/**
aPiecek874ea4d2021-04-19 12:26:36 +02001576 * @brief Printing one line including wrapper and node
1577 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001578 * @param[in] node is \<node\> representation.
1579 * @param[in] pck contains special printing functions callback.
1580 * @param[in] indent contains wrapper and indent in node numbers.
1581 * @param[in,out] out is output handler.
1582 */
1583static void
1584trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1585{
1586 trp_print_wrapper(indent.wrapper, out);
1587 trp_print_node(node, pck, indent.in_node, out);
1588}
1589
1590/**
aPiecek874ea4d2021-04-19 12:26:36 +02001591 * @brief Printing one line including wrapper and
1592 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001593 * @param[in] node is \<node\> representation.
1594 * @param[in] wr is wrapper for printing indentation before node.
1595 * @param[in] out is output handler.
1596 */
1597static void
1598trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1599{
1600 trp_print_wrapper(wr, out);
1601 trp_print_node_up_to_name(node, out);
1602}
1603
1604/**
aPiecek874ea4d2021-04-19 12:26:36 +02001605 * @brief Check if leafref target must be change to string 'leafref'
1606 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001607 * @param[in] node containing leafref target.
1608 * @param[in] wr is wrapper for printing indentation before node.
1609 * @param[in] mll is max line length.
1610 * @param[in] out is output handler.
1611 * @return true if leafref must be changed to string 'leafref'.
1612 */
1613static ly_bool
1614trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1615{
1616 struct ly_out_clb_arg *data;
1617
1618 if (node.type.type != TRD_TYPE_TARGET) {
1619 return 0;
1620 }
1621
1622 /* set ly_out to counting characters */
1623 data = out->method.clb.arg;
1624
1625 data->counter = 0;
1626 data->mode = TRD_CHAR_COUNT;
1627 /* count number of printed bytes */
1628 trp_print_wrapper(wr, out);
1629 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1630 trp_print_divided_node_up_to_name(node, out);
1631 data->mode = TRD_PRINT;
1632
1633 return data->counter + strlen(node.type.str) > mll;
1634}
1635
1636/**
1637 * @brief Get default indent in node based on node values.
1638 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001639 * @return Default indent in node assuming that the node
1640 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001641 */
1642static struct trt_indent_in_node
1643trp_default_indent_in_node(struct trt_node node)
1644{
1645 struct trt_indent_in_node ret;
1646
1647 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1648
1649 /* btw_name_opts */
aPiecekbca57772022-10-13 13:51:59 +02001650 ret.btw_name_opts = node.name.keys ? TRD_INDENT_BEFORE_KEYS : 0;
aPiecek61d062b2020-11-02 11:05:09 +01001651
1652 /* btw_opts_type */
1653 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1654 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1655 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1656 TRD_INDENT_BEFORE_TYPE;
1657 } else {
1658 ret.btw_opts_type = 0;
1659 }
1660
1661 /* btw_type_iffeatures */
1662 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1663
1664 return ret;
1665}
1666
1667/**
1668 * @brief Setting linebreaks in trt_indent_in_node.
1669 *
1670 * The order where the linebreak tag can be placed is from the end.
1671 *
aPiecek874ea4d2021-04-19 12:26:36 +02001672 * @param[in] indent containing alignment lengths
1673 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001674 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001675 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1676 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001677 */
1678static struct trt_indent_in_node
1679trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1680{
1681 /* somewhere must be set a line break in node */
1682 struct trt_indent_in_node ret = indent;
1683
1684 /* gradually break the node from the end */
1685 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1686 ret.btw_type_iffeatures = TRD_LINEBREAK;
1687 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1688 ret.btw_opts_type = TRD_LINEBREAK;
1689 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1690 /* set line break between name and opts */
1691 ret.btw_name_opts = TRD_LINEBREAK;
1692 } else {
1693 /* it is not possible to place a more line breaks,
1694 * unfortunately the max_line_length constraint is violated
1695 */
1696 ret.type = TRD_INDENT_IN_NODE_FAILED;
1697 }
1698 return ret;
1699}
1700
1701/**
1702 * @brief Get the first half of the node based on the linebreak mark.
1703 *
1704 * Items in the second half of the node will be empty.
1705 *
1706 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001707 * @param[in] indent contains information in which part of the \<node\>
1708 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001709 * @return first half of the node, indent is unchanged.
1710 */
1711static struct trt_pair_indent_node
1712trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1713{
1714 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1715
1716 if (indent.btw_name_opts == TRD_LINEBREAK) {
aPiecekbca57772022-10-13 13:51:59 +02001717 ret.node.name.type = node.name.type;
aPiecek61d062b2020-11-02 11:05:09 +01001718 ret.node.type = TRP_EMPTY_TRT_TYPE;
1719 ret.node.iffeatures = 0;
1720 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1721 ret.node.type = TRP_EMPTY_TRT_TYPE;
1722 ret.node.iffeatures = 0;
1723 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1724 ret.node.iffeatures = 0;
1725 }
1726
1727 return ret;
1728}
1729
1730/**
1731 * @brief Get the second half of the node based on the linebreak mark.
1732 *
1733 * Items in the first half of the node will be empty.
1734 * Indentations belonging to the first node will be reset to zero.
1735 *
1736 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001737 * @param[in] indent contains information in which part of the \<node\>
1738 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001739 * @return second half of the node, indent is newly set.
1740 */
1741static struct trt_pair_indent_node
1742trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1743{
1744 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1745
1746 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001747 /* Logically, the information up to token <opts> should
1748 * be deleted, but the the trp_print_node function needs it to
1749 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001750 */
1751 ret.indent.btw_name_opts = 0;
1752 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1753 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1754 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
aPiecekbca57772022-10-13 13:51:59 +02001755 ret.node.name.type = node.name.type;
aPiecek61d062b2020-11-02 11:05:09 +01001756 ret.indent.btw_name_opts = 0;
1757 ret.indent.btw_opts_type = 0;
1758 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1759 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
aPiecekbca57772022-10-13 13:51:59 +02001760 ret.node.name.type = node.name.type;
aPiecek61d062b2020-11-02 11:05:09 +01001761 ret.node.type = TRP_EMPTY_TRT_TYPE;
1762 ret.indent.btw_name_opts = 0;
1763 ret.indent.btw_opts_type = 0;
1764 ret.indent.btw_type_iffeatures = 0;
1765 }
1766 return ret;
1767}
1768
1769/**
1770 * @brief Get the correct alignment for the node.
1771 *
aPiecek874ea4d2021-04-19 12:26:36 +02001772 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001773 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001774 *
1775 * @param[in] node is \<node\> representation.
1776 * @param[in] pck contains speciall callback functions for printing.
1777 * @param[in] indent contains wrapper and indent in node numbers.
1778 * @param[in] mll is max line length.
1779 * @param[in,out] cnt counting number of characters to print.
1780 * @param[in,out] out is output handler.
1781 * @return pair of node and indentation numbers of that node.
1782 */
1783static struct trt_pair_indent_node
1784trp_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)
1785{
1786 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1787
1788 trp_print_line(node, pck, indent, out);
1789
1790 if (*cnt <= mll) {
1791 /* success */
1792 return ret;
1793 } else {
1794 ret.indent = trp_indent_in_node_place_break(ret.indent);
1795 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1796 /* erase information in node due to line break */
1797 ret = trp_first_half_node(node, ret.indent);
1798 /* check if line fits, recursive call */
1799 *cnt = 0;
1800 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1801 /* make sure that the result will be with the status divided
1802 * or eventually with status failed */
1803 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1804 }
1805 return ret;
1806 }
1807}
1808
1809/**
1810 * @brief Get the correct alignment for the node.
1811 *
1812 * @param[in] node is \<node\> representation.
1813 * @param[in] pck contains speciall callback functions for printing.
1814 * @param[in] indent contains wrapper and indent in node numbers.
1815 * @param[in] mll is max line length.
1816 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001817 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1818 * line, some indent variable has negative value as a line break sign.
1819 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1820 * all indent variables values has non-negative number.
1821 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1822 * line, all indent variables has negative or zero values,
1823 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001824 */
1825static struct trt_pair_indent_node
1826trp_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)
1827{
1828 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1829 struct ly_out_clb_arg *data;
1830
1831 /* set ly_out to counting characters */
1832 data = out->method.clb.arg;
1833
1834 data->counter = 0;
1835 data->mode = TRD_CHAR_COUNT;
1836 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1837 data->mode = TRD_PRINT;
1838
1839 return ret;
1840}
1841
1842/**
aPiecek01598c02021-04-23 14:18:24 +02001843 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001844 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001845 * @param[in] node is node representation.
1846 * @param[in] ppck contains speciall callback functions for printing.
1847 * @param[in] ipck contains wrapper and indent in node numbers.
1848 * @param[in] mll is max line length.
1849 * @param[in,out] out is output handler.
1850 */
1851static void
1852trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1853{
1854 ly_bool entire_node_was_printed;
1855 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1856
1857 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1858 /* nothing can be done, continue as usual */
1859 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1860 }
1861
1862 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1863 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1864
1865 if (!entire_node_was_printed) {
1866 ly_print_(out, "\n");
1867 /* continue with second half node */
1868 ind_node = trp_second_half_node(node, ind_node.indent);
1869 /* continue with printing node */
1870 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1871 } else {
1872 return;
1873 }
1874}
1875
1876/**
aPiecek874ea4d2021-04-19 12:26:36 +02001877 * @brief Printing of the wrapper and the whole node,
1878 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001879 * @param[in] node is node representation.
1880 * @param[in] ppck contains speciall callback functions for printing.
1881 * @param[in] ipck contains wrapper and indent in node numbers.
1882 * @param[in] mll is max line length.
1883 * @param[in,out] out is output handler.
1884 */
1885static void
1886trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1887{
1888 struct trt_pair_indent_node ind_node1;
1889 struct trt_pair_indent_node ind_node2;
1890 struct trt_pck_indent tmp;
1891
1892 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1893 node.type.type = TRD_TYPE_LEAFREF;
1894 }
1895
1896 /* check if normal indent is possible */
1897 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1898
1899 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1900 /* node fits to one line */
1901 trp_print_line(node, ppck, ipck, out);
1902 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1903 /* node will be divided */
1904 /* print first half */
1905 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1906 /* pretend that this is normal node */
1907 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1908
1909 trp_print_line(ind_node1.node, ppck, tmp, out);
1910 ly_print_(out, "\n");
1911
1912 /* continue with second half on new line */
1913 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1914 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1915
1916 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1917 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1918 /* node name is too long */
1919 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1920
1921 if (trp_node_body_is_empty(node)) {
1922 return;
1923 } else {
1924 ly_print_(out, "\n");
1925
1926 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1927 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1928 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1929
1930 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1931 }
1932
1933 }
1934}
1935
aPiecek874ea4d2021-04-19 12:26:36 +02001936/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001937 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001938 *********************************************************************/
1939
1940/**
1941 * @brief Get nodetype.
1942 * @param[in] node is any lysp_node.
1943 */
1944static uint16_t
1945trop_nodetype(const void *node)
1946{
1947 return ((const struct lysp_node *)node)->nodetype;
1948}
1949
1950/**
1951 * @brief Get sibling.
1952 * @param[in] node is any lysp_node.
1953 */
1954static const void *
1955trop_next(const void *node)
1956{
1957 return ((const struct lysp_node *)node)->next;
1958}
1959
1960/**
1961 * @brief Get parent.
1962 * @param[in] node is any lysp_node.
1963 */
1964static const void *
1965trop_parent(const void *node)
1966{
1967 return ((const struct lysp_node *)node)->parent;
1968}
1969
1970/**
1971 * @brief Try to get child.
1972 * @param[in] node is any lysp_node.
1973 */
1974static const void *
1975trop_child(const void *node)
1976{
1977 return lysp_node_child(node);
1978}
1979
1980/**
1981 * @brief Try to get action.
1982 * @param[in] node is any lysp_node.
1983 */
1984static const void *
1985trop_actions(const void *node)
1986{
1987 return lysp_node_actions(node);
1988}
1989
1990/**
1991 * @brief Try to get action.
1992 * @param[in] node must be of type lysp_node_action.
1993 */
1994static const void *
1995trop_action_input(const void *node)
1996{
1997 return &((const struct lysp_node_action *)node)->input;
1998}
1999
2000/**
2001 * @brief Try to get action.
2002 * @param[in] node must be of type lysp_node_action.
2003 */
2004static const void *
2005trop_action_output(const void *node)
2006{
2007 return &((const struct lysp_node_action *)node)->output;
2008}
2009
2010/**
2011 * @brief Try to get action.
2012 * @param[in] node is any lysp_node.
2013 */
2014static const void *
2015trop_notifs(const void *node)
2016{
2017 return lysp_node_notifs(node);
2018}
2019
2020/**
aPiecek01598c02021-04-23 14:18:24 +02002021 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02002022 * which are adapted to lysp nodes.
2023 */
2024static struct tro_getters
2025trop_init_getters()
2026{
2027 return (struct tro_getters) {
2028 .nodetype = trop_nodetype,
2029 .next = trop_next,
2030 .parent = trop_parent,
2031 .child = trop_child,
2032 .actions = trop_actions,
2033 .action_input = trop_action_input,
2034 .action_output = trop_action_output,
2035 .notifs = trop_notifs
2036 };
2037}
2038
aPiecek3f247652021-04-19 13:40:25 +02002039/**
2040 * @brief Get nodetype.
2041 * @param[in] node is any lysc_node.
2042 */
2043static uint16_t
2044troc_nodetype(const void *node)
2045{
2046 return ((const struct lysc_node *)node)->nodetype;
2047}
2048
2049/**
2050 * @brief Get sibling.
2051 * @param[in] node is any lysc_node.
2052 */
2053static const void *
2054troc_next(const void *node)
2055{
2056 return ((const struct lysc_node *)node)->next;
2057}
2058
2059/**
2060 * @brief Get parent.
2061 * @param[in] node is any lysc_node.
2062 */
2063static const void *
2064troc_parent(const void *node)
2065{
2066 return ((const struct lysc_node *)node)->parent;
2067}
2068
2069/**
2070 * @brief Try to get child.
2071 * @param[in] node is any lysc_node.
2072 */
2073static const void *
2074troc_child(const void *node)
2075{
2076 return lysc_node_child(node);
2077}
2078
2079/**
2080 * @brief Try to get action.
2081 * @param[in] node is any lysc_node.
2082 */
2083static const void *
2084troc_actions(const void *node)
2085{
2086 return lysc_node_actions(node);
2087}
2088
2089/**
2090 * @brief Try to get action.
2091 * @param[in] node must be of type lysc_node_action.
2092 */
2093static const void *
2094troc_action_input(const void *node)
2095{
2096 return &((const struct lysc_node_action *)node)->input;
2097}
2098
2099/**
2100 * @brief Try to get action.
2101 * @param[in] node must be of type lysc_node_action.
2102 */
2103static const void *
2104troc_action_output(const void *node)
2105{
2106 return &((const struct lysc_node_action *)node)->output;
2107}
2108
2109/**
2110 * @brief Try to get action.
2111 * @param[in] node is any lysc_node.
2112 */
2113static const void *
2114troc_notifs(const void *node)
2115{
2116 return lysc_node_notifs(node);
2117}
2118
2119/**
aPiecek01598c02021-04-23 14:18:24 +02002120 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002121 * which are adapted to lysc nodes.
2122 */
2123static struct tro_getters
2124troc_init_getters()
2125{
2126 return (struct tro_getters) {
2127 .nodetype = troc_nodetype,
2128 .next = troc_next,
2129 .parent = troc_parent,
2130 .child = troc_child,
2131 .actions = troc_actions,
2132 .action_input = troc_action_input,
2133 .action_output = troc_action_output,
2134 .notifs = troc_notifs
2135 };
2136}
2137
aPiecekef1e58e2021-04-19 13:19:44 +02002138/**********************************************************************
2139 * tro functions
2140 *********************************************************************/
2141
2142/**
2143 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002144 *
2145 * This is a general algorithm that is able to
2146 * work with lysp_node or lysc_node.
2147 *
2148 * @param[in] node points to lysp_node or lysc_node.
2149 * @param[in] lysc_tree flag to determine what type the @p node is.
2150 * If set to true, then @p points to lysc_node otherwise lysp_node.
2151 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002152 */
2153static const void *
aPiecek3f247652021-04-19 13:40:25 +02002154tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002155{
2156 struct tro_getters get;
2157 const void *tmp, *parent;
2158 const void *ret;
2159
2160 assert(node);
2161
aPiecek3f247652021-04-19 13:40:25 +02002162 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002163
2164 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2165 if ((tmp = get.next(node))) {
2166 /* next action exists */
2167 ret = tmp;
2168 } else if ((parent = get.parent(node))) {
2169 /* maybe if notif exists as sibling */
2170 ret = get.notifs(parent);
2171 } else {
2172 ret = NULL;
2173 }
2174 } else if (get.nodetype(node) & LYS_INPUT) {
2175 if ((parent = get.parent(node))) {
2176 /* if output action has data */
2177 if (get.child(get.action_output(parent))) {
2178 /* then next sibling is output action */
2179 ret = get.action_output(parent);
2180 } else {
2181 /* input action cannot have siblings other
2182 * than output action.
2183 */
2184 ret = NULL;
2185 }
2186 } else {
2187 /* there is no way how to get output action */
2188 ret = NULL;
2189 }
2190 } else if (get.nodetype(node) & LYS_OUTPUT) {
2191 /* output action cannot have siblings */
2192 ret = NULL;
2193 } else if (get.nodetype(node) & LYS_NOTIF) {
2194 /* must have as a sibling only notif */
2195 ret = get.next(node);
2196 } else {
2197 /* for rest of nodes */
2198 if ((tmp = get.next(node))) {
2199 /* some sibling exists */
2200 ret = tmp;
2201 } else if ((parent = get.parent(node))) {
2202 /* Action and notif are siblings too.
2203 * They can be reached through parent.
2204 */
2205 if ((tmp = get.actions(parent))) {
2206 /* next sibling is action */
2207 ret = tmp;
2208 } else if ((tmp = get.notifs(parent))) {
2209 /* next sibling is notif */
2210 ret = tmp;
2211 } else {
2212 /* sibling not exists */
2213 ret = NULL;
2214 }
2215 } else {
2216 /* sibling not exists */
2217 ret = NULL;
2218 }
2219 }
2220
2221 return ret;
2222}
2223
2224/**
2225 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002226 *
2227 * This is a general algorithm that is able to
2228 * work with lysp_node or lysc_node.
2229 *
2230 * @param[in] node points to lysp_node or lysc_node.
2231 * @param[in] lysc_tree flag to determine what type the @p node is.
2232 * If set to true, then @p points to lysc_node otherwise lysp_node.
2233 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002234 */
2235static const void *
aPiecek3f247652021-04-19 13:40:25 +02002236tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002237{
2238 struct tro_getters get;
2239 const void *tmp;
2240 const void *ret;
2241
2242 assert(node);
2243
aPiecek3f247652021-04-19 13:40:25 +02002244 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002245
2246 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2247 if (get.child(get.action_input(node))) {
2248 /* go to LYS_INPUT */
2249 ret = get.action_input(node);
2250 } else if (get.child(get.action_output(node))) {
2251 /* go to LYS_OUTPUT */
2252 ret = get.action_output(node);
2253 } else {
2254 /* input action and output action have no data */
2255 ret = NULL;
2256 }
2257 } else {
2258 if ((tmp = get.child(node))) {
2259 ret = tmp;
2260 } else {
2261 /* current node can't have children or has no children */
2262 /* but maybe has some actions or notifs */
2263 if ((tmp = get.actions(node))) {
2264 ret = tmp;
2265 } else if ((tmp = get.notifs(node))) {
2266 ret = tmp;
2267 } else {
2268 ret = NULL;
2269 }
2270 }
2271 }
2272
2273 return ret;
2274}
2275
2276/**
aPiecek3f247652021-04-19 13:40:25 +02002277 * @brief Get new trt_parent_cache if we apply the transfer
2278 * to the child node in the tree.
2279 * @param[in] ca is parent cache for current node.
2280 * @param[in] tc contains current tree node.
2281 * @return Cache for the current node.
2282 */
2283static struct trt_parent_cache
2284tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2285{
2286 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2287
2288 if (!tc->lysc_tree) {
2289 const struct lysp_node *pn = tc->pn;
2290
2291 ret.ancestor =
2292 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2293 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2294 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2295 ca.ancestor;
2296
2297 ret.lys_status =
2298 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2299 ca.lys_status;
2300
2301 ret.lys_config =
2302 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2303 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2304 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2305 ca.lys_config;
2306
2307 ret.last_list =
2308 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2309 ca.last_list;
2310 }
2311
2312 return ret;
2313}
2314
2315/**
aPiecekef1e58e2021-04-19 13:19:44 +02002316 * @brief Transformation of the Schema nodes flags to
2317 * Tree diagram \<status\>.
2318 * @param[in] flags is node's flags obtained from the tree.
2319 */
2320static trt_status_type
2321tro_flags2status(uint16_t flags)
2322{
2323 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2324 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2325 TRD_STATUS_TYPE_CURRENT;
2326}
2327
2328/**
2329 * @brief Transformation of the Schema nodes flags to Tree diagram
2330 * \<flags\> but more specifically 'ro' or 'rw'.
2331 * @param[in] flags is node's flags obtained from the tree.
2332 */
2333static trt_flags_type
2334tro_flags2config(uint16_t flags)
2335{
2336 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2337 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2338 TRD_FLAGS_TYPE_EMPTY;
2339}
2340
2341/**
aPiecek3f247652021-04-19 13:40:25 +02002342 * @brief Print current node's iffeatures.
2343 * @param[in] tc is tree context.
2344 * @param[in,out] out is output handler.
2345 */
2346static void
2347tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2348{
2349 const struct lysp_qname *iffs;
2350
aPiecekbbc02932021-05-21 07:19:41 +02002351 if (tc->lysc_tree) {
2352 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2353 iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
2354 } else {
2355 iffs = tc->pn->iffeatures;
2356 }
aPiecek3f247652021-04-19 13:40:25 +02002357 LY_ARRAY_COUNT_TYPE i;
2358
2359 LY_ARRAY_FOR(iffs, i) {
2360 if (i == 0) {
2361 ly_print_(out, "%s", iffs[i].str);
2362 } else {
2363 ly_print_(out, ",%s", iffs[i].str);
2364 }
2365 }
2366
2367}
2368
2369/**
2370 * @brief Print current list's keys.
2371 *
2372 * Well, actually printing keys in the lysp_tree is trivial,
2373 * because char* points to all keys. However, special functions have
2374 * been reserved for this, because in principle the list of elements
2375 * can have more implementations.
2376 *
2377 * @param[in] tc is tree context.
2378 * @param[in,out] out is output handler.
2379 */
2380static void
2381tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2382{
2383 const struct lysp_node_list *list;
2384
aPiecekbbc02932021-05-21 07:19:41 +02002385 if (tc->lysc_tree) {
2386 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2387 list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
2388 } else {
2389 list = (const struct lysp_node_list *)tc->pn;
2390 }
aPiecek3f247652021-04-19 13:40:25 +02002391 assert(list->nodetype & LYS_LIST);
2392
2393 if (trg_charptr_has_data(list->key)) {
2394 ly_print_(out, "%s", list->key);
2395 }
2396}
2397
2398/**
2399 * @brief Get rpcs section if exists.
2400 * @param[in,out] tc is tree context.
2401 * @return Section representation if it exists. The @p tc is modified
2402 * and his pointer points to the first node in rpcs section.
2403 * @return Empty section representation otherwise.
2404 */
2405static struct trt_keyword_stmt
2406tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2407{
aPiecek9f792e52021-04-21 08:33:56 +02002408 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002409 const void *actions;
2410
2411 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002412 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002413 if (actions) {
2414 tc->cn = actions;
2415 }
2416 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002417 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002418 if (actions) {
2419 tc->pn = actions;
2420 tc->tpn = tc->pn;
2421 }
2422 }
2423
2424 if (actions) {
2425 tc->section = TRD_SECT_RPCS;
2426 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2427 } else {
2428 return TRP_EMPTY_KEYWORD_STMT;
2429 }
2430}
2431
2432/**
2433 * @brief Get notification section if exists
2434 * @param[in,out] tc is tree context.
2435 * @return Section representation if it exists.
2436 * The @p tc is modified and his pointer points to the
2437 * first node in notification section.
2438 * @return Empty section representation otherwise.
2439 */
2440static struct trt_keyword_stmt
2441tro_modi_get_notifications(struct trt_tree_ctx *tc)
2442{
aPiecek9f792e52021-04-21 08:33:56 +02002443 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002444 const void *notifs;
2445
2446 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002447 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002448 if (notifs) {
2449 tc->cn = notifs;
2450 }
2451 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002452 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002453 if (notifs) {
2454 tc->pn = notifs;
2455 tc->tpn = tc->pn;
2456 }
2457 }
2458
2459 if (notifs) {
2460 tc->section = TRD_SECT_NOTIF;
2461 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2462 } else {
2463 return TRP_EMPTY_KEYWORD_STMT;
2464 }
2465}
2466
2467/**
aPiecek96baa7f2021-04-23 12:32:00 +02002468 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002469 *
2470 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002471 * @param[in] u is index to the array of extensions (lysc_ext_instance
2472 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002473 * @return Section representation if it exists.
2474 * @return Empty section representation otherwise.
2475 */
2476static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002477tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002478{
aPiecek96baa7f2021-04-23 12:32:00 +02002479 assert(tc);
2480 const void *node;
2481 const char *yang_data_name;
2482
2483 if (tc->lysc_tree) {
2484 struct lysc_ext_instance *exts;
2485 struct lysc_ext_substmt *substmts;
2486
2487 exts = tc->cmod->exts;
2488 substmts = exts[u].substmts;
2489 if (!substmts) {
2490 return TRP_EMPTY_KEYWORD_STMT;
2491 }
2492 node = *(const struct lysc_node **)substmts->storage;
2493 yang_data_name = exts[u].argument;
2494 } else {
2495 struct lysp_ext_instance *exts;
2496
2497 exts = tc->pmod->exts;
2498 node = exts[u].parsed;
2499 yang_data_name = exts[u].argument;
2500 }
2501
2502 if (tc->lysc_tree) {
2503 tc->cn = node;
2504 } else {
2505 tc->tpn_ext = &tc->pmod->exts[u];
2506 tc->pn = node;
2507 }
2508
2509 if (node) {
2510 tc->section = TRD_SECT_YANG_DATA;
2511 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2512 } else {
2513 return TRP_EMPTY_KEYWORD_STMT;
2514 }
aPiecekef1e58e2021-04-19 13:19:44 +02002515}
2516
2517/**
2518 * @brief Get name of the module.
2519 * @param[in] tc is context of the tree.
2520 */
2521static struct trt_keyword_stmt
2522tro_read_module_name(const struct trt_tree_ctx *tc)
2523{
aPiecek9f792e52021-04-21 08:33:56 +02002524 assert(tc);
2525
2526 struct trt_keyword_stmt ret;
2527
2528 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2529 TRD_KEYWORD_SUBMODULE :
2530 TRD_KEYWORD_MODULE;
2531
2532 ret.str = !tc->lysc_tree ?
2533 LYSP_MODULE_NAME(tc->pmod) :
2534 tc->cmod->mod->name;
2535
2536 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002537}
2538
aPiecekb8d5a0a2021-05-20 08:20:24 +02002539/**
2540 * @brief Create implicit "case" node as parent of @p node.
2541 * @param[in] node child of implicit case node.
2542 * @return The case node ready to print.
2543 */
2544static struct trt_node
2545tro_create_implicit_case_node(struct trt_node node)
2546{
2547 struct trt_node ret;
2548
2549 ret.status = node.status;
2550 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2551 ret.name.type = TRD_NODE_CASE;
aPiecekbca57772022-10-13 13:51:59 +02002552 ret.name.keys = node.name.keys;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002553 ret.name.module_prefix = node.name.module_prefix;
2554 ret.name.str = node.name.str;
2555 ret.type = TRP_EMPTY_TRT_TYPE;
aPiecek7a28e2f2021-05-21 07:27:03 +02002556 ret.iffeatures = 0;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002557 ret.last_one = node.last_one;
aPiecek50b64e42022-10-10 10:00:12 +02002558 ret.mount = NULL;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002559
2560 return ret;
2561}
2562
aPiecekef1e58e2021-04-19 13:19:44 +02002563/**********************************************************************
2564 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002565 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002566
2567/**
aPiecek61d062b2020-11-02 11:05:09 +01002568 * @brief Check if list statement has keys.
2569 * @param[in] pn is pointer to the list.
2570 * @return 1 if has keys, otherwise 0.
2571 */
2572static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002573trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002574{
aPiecekef1e58e2021-04-19 13:19:44 +02002575 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002576}
2577
2578/**
2579 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002580 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002581 * @return 1 if has if-features, otherwise 0.
2582 */
2583static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002584trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002585{
2586 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002587 const struct lysp_qname *iffs;
2588
aPiecek61d062b2020-11-02 11:05:09 +01002589 ly_bool ret = 0;
2590
aPiecekef1e58e2021-04-19 13:19:44 +02002591 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002592 LY_ARRAY_FOR(iffs, u) {
2593 ret = 1;
2594 break;
2595 }
2596 return ret;
2597}
2598
2599/**
2600 * @brief Find out if leaf is also the key in last list.
2601 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002602 * @param[in] ca_last_list is pointer to last visited list.
2603 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002604 * @return 1 if leaf is also the key, otherwise 0.
2605 */
2606static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002607trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002608{
2609 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2610 const struct lysp_node_list *list = ca_last_list;
2611
2612 if (!list) {
2613 return 0;
2614 }
2615 return trg_charptr_has_data(list->key) ?
2616 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2617}
2618
2619/**
2620 * @brief Check if container's type is presence.
2621 * @param[in] pn is pointer to container.
2622 * @return 1 if container has presence statement, otherwise 0.
2623 */
2624static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002625trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002626{
2627 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2628}
2629
2630/**
aPiecek7ed8d032022-10-10 12:32:27 +02002631 * @brief Check if container has mount-point extension.
ekinzie0ab8b302022-10-10 03:03:57 -04002632 * @param[in] cn is pointer to container or list.
2633 * @param[out] mount is assigned a pointer to the extension instance, if found
aPiecek7ed8d032022-10-10 12:32:27 +02002634 * @return 1 if node has mount-point.
ekinzie0ab8b302022-10-10 03:03:57 -04002635 */
2636static ly_bool
2637troc_node_has_mount(const struct lysc_node *cn, struct lysc_ext_instance **mount)
2638{
2639 struct lysc_ext_instance *ext;
2640 uint64_t u;
2641
2642 /* The schema-mount extension plugin has already made sure that
2643 * there is only one mount-point here.
2644 */
2645 LY_ARRAY_FOR(cn->exts, u) {
2646 ext = &cn->exts[u];
2647 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") ||
2648 strcmp(ext->def->name, "mount-point")) {
2649 continue;
2650 }
2651 *mount = ext;
2652 return 1;
2653 }
2654 return 0;
2655}
2656
aPiecek7ed8d032022-10-10 12:32:27 +02002657/**
2658 * @brief Check if node has mount-point extension.
2659 *
2660 * @param[in] pn is pointer to container or list.
2661 * @return 1 if node has mount-point.
2662 */
ekinzie0ab8b302022-10-10 03:03:57 -04002663static ly_bool
2664trop_node_has_mount(const struct lysp_node *pn)
2665{
2666 struct lysp_ext_instance *ext;
2667 uint64_t u;
2668
2669 LY_ARRAY_FOR(pn->exts, u) {
2670 ext = &pn->exts[u];
2671 if (strcmp(ext->name, "yangmnt:mount-point")) {
2672 continue;
2673 }
2674 return 1;
2675 }
2676 return 0;
2677}
2678
2679/**
aPiecek61d062b2020-11-02 11:05:09 +01002680 * @brief Get leaflist's path without lysp_node type control.
2681 * @param[in] pn is pointer to the leaflist.
2682 */
2683static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002684trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002685{
2686 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2687
2688 return list->type.path ? list->type.path->expr : NULL;
2689}
2690
2691/**
2692 * @brief Get leaflist's type name without lysp_node type control.
2693 * @param[in] pn is pointer to the leaflist.
2694 */
2695static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002696trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002697{
2698 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2699
2700 return list->type.name;
2701}
2702
2703/**
2704 * @brief Get leaf's path without lysp_node type control.
2705 * @param[in] pn is pointer to the leaf node.
2706 */
2707static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002708trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002709{
2710 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2711
2712 return leaf->type.path ? leaf->type.path->expr : NULL;
2713}
2714
2715/**
2716 * @brief Get leaf's type name without lysp_node type control.
2717 * @param[in] pn is pointer to the leaf's type name.
2718 */
2719static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002720trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002721{
2722 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2723
2724 return leaf->type.name;
2725}
2726
2727/**
aPiecek874ea4d2021-04-19 12:26:36 +02002728 * @brief Get pointer to data using node type specification
2729 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002730 *
aPiecek874ea4d2021-04-19 12:26:36 +02002731 * @param[in] flags is node type specification.
2732 * If it is the correct node, the getter function is called.
2733 * @param[in] f is getter function which provides the desired
2734 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002735 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002736 * @return NULL if node has wrong type or getter function return
2737 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002738 * @return Pointer to desired char pointer obtained from the node.
2739 */
2740static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002741trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002742{
2743 if (pn->nodetype & flags) {
2744 const char *ret = f(pn);
Michal Vasko26bbb272022-08-02 14:54:33 +02002745
aPiecek61d062b2020-11-02 11:05:09 +01002746 return trg_charptr_has_data(ret) ? ret : NULL;
2747 } else {
2748 return NULL;
2749 }
2750}
2751
2752/**
aPiecek61d062b2020-11-02 11:05:09 +01002753 * @brief Resolve \<status\> of the current node.
2754 * @param[in] nodetype is node's type obtained from the tree.
2755 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002756 * @param[in] ca_lys_status is inherited status
2757 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002758 * @return The status type.
2759 */
2760static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002761trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002762{
2763 /* LYS_INPUT and LYS_OUTPUT is special case */
2764 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002765 return tro_flags2status(ca_lys_status);
2766 /* if ancestor's status is deprc or obslt
2767 * and also node's status is not set
2768 */
aPiecek61d062b2020-11-02 11:05:09 +01002769 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2770 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002771 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002772 } else {
2773 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002774 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002775 }
2776}
2777
2778/**
2779 * @brief Resolve \<flags\> of the current node.
2780 * @param[in] nodetype is node's type obtained from the tree.
2781 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002782 * @param[in] ca_ancestor is ancestor type obtained
2783 * from trt_parent_cache.
2784 * @param[in] ca_lys_config is inherited config item
2785 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002786 * @return The flags type.
2787 */
2788static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002789trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002790{
2791 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2792 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2793 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2794 return TRD_FLAGS_TYPE_RO;
2795 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2796 return TRD_FLAGS_TYPE_RO;
2797 } else if (nodetype & LYS_NOTIF) {
2798 return TRD_FLAGS_TYPE_NOTIF;
2799 } else if (nodetype & LYS_USES) {
2800 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2801 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2802 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002803 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002804 /* config is not set. Look at ancestor's config */
2805 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002806 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002807 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002808 }
2809}
2810
2811/**
2812 * @brief Resolve node type of the current node.
2813 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002814 * @param[in] ca_last_list is pointer to the last visited list.
2815 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002816 */
2817static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04002818trop_resolve_node_type(const struct trt_tree_ctx *tc, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002819{
ekinzie0ab8b302022-10-10 03:03:57 -04002820 const struct lysp_node *pn = tc->pn;
2821
aPiecek61d062b2020-11-02 11:05:09 +01002822 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2823 return TRD_NODE_ELSE;
2824 } else if (pn->nodetype & LYS_CASE) {
2825 return TRD_NODE_CASE;
2826 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2827 return TRD_NODE_OPTIONAL_CHOICE;
2828 } else if (pn->nodetype & LYS_CHOICE) {
2829 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04002830 } else if (tc->mounted && (tc->pn->parent == NULL)) {
2831 if (tc->parent_refs) {
2832 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
2833 if (!strcmp(tc->pmod->mod->ns, tc->parent_refs->snodes[v]->module->ns)) {
2834 return TRD_NODE_TOP_LEVEL2;
2835 }
2836 }
2837 }
2838 return TRD_NODE_TOP_LEVEL1;
aPiecekef1e58e2021-04-19 13:19:44 +02002839 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002840 return TRD_NODE_CONTAINER;
aPiecek61d062b2020-11-02 11:05:09 +01002841 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2842 return TRD_NODE_LISTLEAFLIST;
2843 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2844 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002845 } else if ((pn->nodetype & LYS_LEAF) && !(pn->flags & LYS_MAND_TRUE) && (!trop_leaf_is_key(pn, ca_last_list))) {
aPiecek61d062b2020-11-02 11:05:09 +01002846 return TRD_NODE_OPTIONAL;
2847 } else {
2848 return TRD_NODE_ELSE;
2849 }
2850}
2851
2852/**
aPiecekef1e58e2021-04-19 13:19:44 +02002853 * @brief Resolve \<type\> of the current node.
2854 * @param[in] pn is current node.
2855 */
2856static struct trt_type
2857trop_resolve_type(const struct lysp_node *pn)
2858{
2859 const char *tmp = NULL;
2860
2861 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2862 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2863 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2864 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2865 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2866 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2867 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2868 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2869 } else if (pn->nodetype == LYS_ANYDATA) {
2870 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2871 } else if (pn->nodetype & LYS_ANYXML) {
2872 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2873 } else {
2874 return TRP_EMPTY_TRT_TYPE;
2875 }
2876}
2877
2878/**
aPiecek61d062b2020-11-02 11:05:09 +01002879 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002880 * @param[in] ca contains stored important data
2881 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002882 * @param[in] tc is context of the tree.
2883 */
2884static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002885trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002886{
aPiecekef1e58e2021-04-19 13:19:44 +02002887 const struct lysp_node *pn;
2888 struct trt_node ret;
2889
aPiecek61d062b2020-11-02 11:05:09 +01002890 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002891
2892 pn = tc->pn;
2893 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002894
2895 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002896 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002897
aPiecek61d062b2020-11-02 11:05:09 +01002898 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04002899 if (trop_node_has_mount(pn)) {
2900 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
2901 } else {
2902 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
2903 }
aPiecek61d062b2020-11-02 11:05:09 +01002904
aPiecek61d062b2020-11-02 11:05:09 +01002905 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04002906 ret.name.type = trop_resolve_node_type(tc, ca.last_list);
aPiecekbca57772022-10-13 13:51:59 +02002907 ret.name.keys = (tc->pn->nodetype & LYS_LIST) && trop_list_has_keys(tc->pn);
aPiecek61d062b2020-11-02 11:05:09 +01002908
aPiecek34fa3772021-05-21 12:35:46 +02002909 /* The parsed tree is not compiled, so no node can be augmented
2910 * from another module. This means that nodes from the parsed tree
2911 * will never have the prefix.
2912 */
aPiecek61d062b2020-11-02 11:05:09 +01002913 ret.name.module_prefix = NULL;
2914
2915 /* set node's name */
2916 ret.name.str = pn->name;
2917
2918 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002919 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002920
2921 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002922 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002923
aPiecek3f247652021-04-19 13:40:25 +02002924 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002925
2926 return ret;
2927}
2928
aPiecekef1e58e2021-04-19 13:19:44 +02002929/**
2930 * @brief Find out if the current node has siblings.
2931 * @param[in] tc is context of the tree.
2932 * @return 1 if sibling exists otherwise 0.
2933 */
2934static ly_bool
2935trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2936{
aPiecek3f247652021-04-19 13:40:25 +02002937 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002938}
2939
aPiecek96baa7f2021-04-23 12:32:00 +02002940/**
2941 * @brief Print all yang-data sections and print three dots instead
2942 * of nodes.
2943 * @param[in] exts is array of YANG extension instances from parsed
2944 * module (@ref sizedarrays).
2945 * @param[in] mll is maximum number of characters that can be printed
2946 * on one line.
2947 * @param[in,out] out is output handler.
2948 */
2949static void
2950trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2951{
2952 struct trt_keyword_stmt ks;
2953 LY_ARRAY_COUNT_TYPE u;
2954 struct trt_wrapper wr;
2955
2956 if (!exts) {
2957 return;
2958 }
2959
2960 ly_print_(out, "\n");
2961 ks.type = TRD_KEYWORD_YANG_DATA;
2962 wr = TRP_INIT_WRAPPER_BODY;
2963
2964 LY_ARRAY_FOR(exts, u) {
2965 ly_print_(out, "\n");
2966
2967 /* yang-data <yang-data-name>: */
2968 ks.str = exts[u].argument;
2969 trp_print_keyword_stmt(ks, mll, 0, out);
2970 ly_print_(out, "\n");
2971
2972 /* ... */
2973 trp_print_wrapper(wr, out);
2974 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2975 }
2976}
2977
aPiecek874ea4d2021-04-19 12:26:36 +02002978/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002979 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002980 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002981
2982/**
aPiecek874ea4d2021-04-19 12:26:36 +02002983 * @brief Change current node pointer to its parent
2984 * but only if parent exists.
2985 * @param[in,out] tc is tree context.
2986 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002987 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002988 * @return 0 if the node did not have parents.
2989 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002990 */
2991static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002992trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002993{
2994 assert(tc && tc->pn);
2995 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002996 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002997 tc->pn = tc->pn->parent;
2998 return 1;
2999 } else {
3000 return 0;
3001 }
3002}
3003
3004/**
aPiecek874ea4d2021-04-19 12:26:36 +02003005 * @brief Change the current node pointer to its child
3006 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003007 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003008 * @param[in,out] tc is context of the tree.
3009 * Contains pointer to the current node.
3010 * @return Non-empty \<node\> representation of the current
3011 * node's child. The @p tc is modified.
3012 * @return Empty \<node\> representation if child don't exists.
3013 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003014 */
3015static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003016trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003017{
aPiecekef1e58e2021-04-19 13:19:44 +02003018 const struct lysp_node *tmp;
3019
aPiecek61d062b2020-11-02 11:05:09 +01003020 assert(tc && tc->pn);
3021
aPiecek3f247652021-04-19 13:40:25 +02003022 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02003023 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02003024 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01003025 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02003026 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01003027 }
3028}
3029
3030/**
aPiecek874ea4d2021-04-19 12:26:36 +02003031 * @brief Change the current node pointer to the first child of node's
3032 * parent. If current node is already first sibling/child then nothing
3033 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01003034 * @param[in,out] tc is tree context.
3035 */
3036static void
aPiecekef1e58e2021-04-19 13:19:44 +02003037trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003038{
aPiecek9f792e52021-04-21 08:33:56 +02003039 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01003040
aPiecekef1e58e2021-04-19 13:19:44 +02003041 if (trop_modi_parent(tc)) {
3042 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003043 } else {
3044 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01003045 switch (tc->section) {
3046 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003047 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02003048 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003049 break;
3050 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02003051 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02003052 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003053 break;
3054 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003055 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02003056 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003057 break;
3058 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003059 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02003060 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003061 break;
3062 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02003063 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02003064 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003065 break;
3066 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003067 /* tpn in this case is of type lysp_ext_instance */
3068 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01003069 break;
aPiecek96baa7f2021-04-23 12:32:00 +02003070 default:
3071 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01003072 }
aPiecek61d062b2020-11-02 11:05:09 +01003073 }
3074}
3075
3076/**
aPiecek874ea4d2021-04-19 12:26:36 +02003077 * @brief Change the pointer to the current node to its next sibling
3078 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003079 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003080 * @param[in,out] tc is tree context.
3081 * Contains pointer to the current node.
3082 * @return Non-empty \<node\> representation if sibling exists.
3083 * The @p tc is modified.
3084 * @return Empty \<node\> representation otherwise.
3085 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003086 */
3087static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003088trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003089{
aPiecekef1e58e2021-04-19 13:19:44 +02003090 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003091
3092 assert(tc && tc->pn);
3093
aPiecek3f247652021-04-19 13:40:25 +02003094 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01003095
aPiecekef1e58e2021-04-19 13:19:44 +02003096 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003097 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3098 tc->tpn = pn;
3099 }
aPiecekef1e58e2021-04-19 13:19:44 +02003100 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003101 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003102 } else {
3103 return TRP_EMPTY_NODE;
3104 }
3105}
3106
3107/**
3108 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003109 * @param[in,out] tc is tree context. It is modified and his current
3110 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003111 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003112 * @return Empty section structure otherwise.
3113 */
3114static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003115trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003116{
aPiecek9f792e52021-04-21 08:33:56 +02003117 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003118 const struct lysp_node_augment *augs;
3119
3120 /* if next_augment func was called for the first time */
3121 if (tc->section != TRD_SECT_AUGMENT) {
3122 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003123 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003124 } else {
3125 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003126 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003127 }
3128
aPiecekdc8fd572021-04-19 10:47:23 +02003129 if (augs) {
3130 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003131 tc->tpn = tc->pn;
3132 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3133 } else {
3134 return TRP_EMPTY_KEYWORD_STMT;
3135 }
3136}
3137
3138/**
aPiecek61d062b2020-11-02 11:05:09 +01003139 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003140 * @param[in,out] tc is tree context. It is modified and his current
3141 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003142 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003143 * @return Empty section representation otherwise.
3144 */
3145static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003146trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003147{
aPiecek9f792e52021-04-21 08:33:56 +02003148 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003149 const struct lysp_node_grp *grps;
3150
3151 if (tc->section != TRD_SECT_GROUPING) {
3152 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003153 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003154 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003155 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003156 }
3157
aPiecekdc8fd572021-04-19 10:47:23 +02003158 if (grps) {
3159 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003160 tc->tpn = tc->pn;
3161 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3162 } else {
3163 return TRP_EMPTY_KEYWORD_STMT;
3164 }
3165}
3166
aPiecek874ea4d2021-04-19 12:26:36 +02003167/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003168 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003169 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003170
3171/**
aPiecek3f247652021-04-19 13:40:25 +02003172 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003173 */
aPiecek3f247652021-04-19 13:40:25 +02003174static ly_bool
3175troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003176{
aPiecek3f247652021-04-19 13:40:25 +02003177 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3178}
aPiecek61d062b2020-11-02 11:05:09 +01003179
aPiecek3f247652021-04-19 13:40:25 +02003180/**
3181 * @brief Resolve \<flags\> of the current node.
3182 *
3183 * Use this function only if trt_tree_ctx.lysc_tree is true.
3184 *
3185 * @param[in] nodetype is current lysc_node.nodetype.
3186 * @param[in] flags is current lysc_node.flags.
3187 * @return The flags type.
3188 */
3189static trt_flags_type
3190troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3191{
3192 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3193 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3194 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3195 return TRD_FLAGS_TYPE_RO;
3196 } else if (nodetype & LYS_IS_NOTIF) {
3197 return TRD_FLAGS_TYPE_RO;
3198 } else if (nodetype & LYS_NOTIF) {
3199 return TRD_FLAGS_TYPE_NOTIF;
3200 } else if (nodetype & LYS_USES) {
3201 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3202 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3203 return TRD_FLAGS_TYPE_RPC;
3204 } else {
3205 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003206 }
aPiecek61d062b2020-11-02 11:05:09 +01003207}
3208
3209/**
aPiecek3f247652021-04-19 13:40:25 +02003210 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003211 *
aPiecek3f247652021-04-19 13:40:25 +02003212 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003213 *
aPiecek3f247652021-04-19 13:40:25 +02003214 * @param[in] nodetype is current lysc_node.nodetype.
3215 * @param[in] flags is current lysc_node.flags.
3216 */
3217static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04003218troc_resolve_node_type(const struct trt_tree_ctx *tc, uint16_t nodetype, uint16_t flags)
aPiecek3f247652021-04-19 13:40:25 +02003219{
3220 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3221 return TRD_NODE_ELSE;
3222 } else if (nodetype & LYS_CASE) {
3223 return TRD_NODE_CASE;
3224 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3225 return TRD_NODE_OPTIONAL_CHOICE;
3226 } else if (nodetype & LYS_CHOICE) {
3227 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04003228 } else if (tc->mounted && (tc->cn->parent == NULL)) {
3229 if (tc->parent_refs) {
3230 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
3231 if (!strcmp(tc->cn->module->ns, tc->parent_refs->snodes[v]->module->ns)) {
3232 return TRD_NODE_TOP_LEVEL2;
3233 }
3234 }
3235 }
3236 return TRD_NODE_TOP_LEVEL1;
aPiecek3f247652021-04-19 13:40:25 +02003237 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3238 return TRD_NODE_CONTAINER;
aPiecek3f247652021-04-19 13:40:25 +02003239 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3240 return TRD_NODE_LISTLEAFLIST;
3241 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3242 return TRD_NODE_OPTIONAL;
3243 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3244 return TRD_NODE_OPTIONAL;
3245 } else {
3246 return TRD_NODE_ELSE;
3247 }
3248}
3249
3250/**
aPiecek34fa3772021-05-21 12:35:46 +02003251 * @brief Resolve prefix (<prefix>:<name>) of node that has been
3252 * placed from another module via an augment statement.
3253 *
3254 * @param[in] cn is current compiled node.
3255 * @param[in] current_compiled_module is module whose nodes are
3256 * currently being printed.
3257 * @return Prefix of foreign module or NULL.
3258 */
3259static const char *
3260troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
3261{
3262 const struct lys_module *node_module;
3263 const char *ret = NULL;
3264
3265 node_module = cn->module;
3266 if (node_module->compiled != current_compiled_module) {
3267 ret = node_module->prefix;
3268 }
3269
3270 return ret;
3271}
3272
3273/**
aPiecek3f247652021-04-19 13:40:25 +02003274 * @brief Transformation of current lysc_node to struct trt_node.
3275 * @param[in] ca is not used.
3276 * @param[in] tc is context of the tree.
3277 */
3278static struct trt_node
3279troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3280{
3281 (void) ca;
3282 const struct lysc_node *cn;
3283 struct trt_node ret;
3284
aPiecek082c7dc2021-05-20 08:55:07 +02003285 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003286
3287 cn = tc->cn;
3288 ret = TRP_EMPTY_NODE;
3289
3290 /* <status> */
3291 ret.status = tro_flags2status(cn->flags);
3292
aPiecek3f247652021-04-19 13:40:25 +02003293 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04003294 if (troc_node_has_mount(cn, &ret.mount)) {
3295 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
3296 } else {
3297 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3298 }
aPiecek3f247652021-04-19 13:40:25 +02003299
aPiecek3f247652021-04-19 13:40:25 +02003300 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04003301 ret.name.type = troc_resolve_node_type(tc, cn->nodetype, cn->flags);
aPiecekbca57772022-10-13 13:51:59 +02003302 ret.name.keys = (cn->nodetype & LYS_LIST) && !(cn->flags & LYS_KEYLESS);
aPiecek3f247652021-04-19 13:40:25 +02003303
aPiecek34fa3772021-05-21 12:35:46 +02003304 /* <prefix> */
3305 ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
aPiecek3f247652021-04-19 13:40:25 +02003306
3307 /* set node's name */
3308 ret.name.str = cn->name;
3309
aPiecekbbc02932021-05-21 07:19:41 +02003310 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003311 /* <type> */
3312 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003313
aPiecek082c7dc2021-05-20 08:55:07 +02003314 /* <iffeature> */
3315 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3316 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003317 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003318 assert(tc->cn->nodetype & LYS_CASE);
3319
3320 /* <type> */
3321 ret.type = TRP_EMPTY_TRT_TYPE;
3322
aPiecek7a28e2f2021-05-21 07:27:03 +02003323 /* <iffeature> */
3324 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003325 }
aPiecek3f247652021-04-19 13:40:25 +02003326
3327 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3328
3329 return ret;
3330}
3331
3332/**********************************************************************
3333 * Modify troc getters
3334 *********************************************************************/
3335
3336/**
aPiecek01598c02021-04-23 14:18:24 +02003337 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003338 */
3339static ly_bool
3340troc_modi_parent(struct trt_tree_ctx *tc)
3341{
3342 assert(tc && tc->cn);
3343 /* If no parent exists, stay in actual node. */
3344 if (tc->cn->parent) {
3345 tc->cn = tc->cn->parent;
3346 return 1;
3347 } else {
3348 return 0;
3349 }
3350}
3351
3352/**
aPiecek01598c02021-04-23 14:18:24 +02003353 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003354 */
3355static struct trt_node
3356troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3357{
3358 const struct lysc_node *cn;
3359
3360 assert(tc && tc->cn);
3361
3362 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3363
3364 /* if next sibling exists */
3365 if (cn) {
3366 /* update trt_tree_ctx */
3367 tc->cn = cn;
3368 return troc_read_node(ca, tc);
3369 } else {
3370 return TRP_EMPTY_NODE;
3371 }
3372}
3373
3374/**
3375 * @copydoc trop_modi_next_child()
3376 */
3377static struct trt_node
3378troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3379{
3380 const struct lysc_node *tmp;
3381
3382 assert(tc && tc->cn);
3383
3384 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3385 tc->cn = tmp;
3386 return troc_read_node(ca, tc);
3387 } else {
3388 return TRP_EMPTY_NODE;
3389 }
3390}
3391
3392/**
aPiecek01598c02021-04-23 14:18:24 +02003393 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003394 */
3395static void
aPiecek3f247652021-04-19 13:40:25 +02003396troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003397{
aPiecek3f247652021-04-19 13:40:25 +02003398 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003399
aPiecek3f247652021-04-19 13:40:25 +02003400 if (troc_modi_parent(tc)) {
3401 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3402 } else {
3403 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003404 switch (tc->section) {
3405 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003406 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003407 break;
3408 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003409 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003410 break;
3411 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003412 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003413 break;
3414 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003415 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003416 break;
3417 default:
3418 assert(0);
3419 }
aPiecek61d062b2020-11-02 11:05:09 +01003420 }
3421}
3422
aPiecek874ea4d2021-04-19 12:26:36 +02003423/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003424 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003425 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003426
3427/**
3428 * @brief Get size of node name.
3429 * @param[in] name contains name and mark.
3430 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003431 * @return negative value as an indication that option mark
3432 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003433 */
3434static int32_t
3435trb_strlen_of_name_and_mark(struct trt_node_name name)
3436{
3437 size_t name_len = strlen(name.str);
3438
3439 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3440 /* counting also parentheses */
3441 name_len += 2;
3442 }
3443
3444 return trp_mark_is_used(name) ?
3445 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3446 (int32_t)name_len;
3447}
3448
3449/**
aPiecek874ea4d2021-04-19 12:26:36 +02003450 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3451 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003452 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003453 * @param[in] max_len4all is the maximum value of btw_opts_type
3454 * that it can have.
3455 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003456 */
3457static int16_t
3458trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3459{
3460 int32_t name_len;
3461 int16_t min_len;
3462 int16_t ret;
3463
3464 name_len = trb_strlen_of_name_and_mark(name);
3465
3466 /* negative value indicate that in name is some opt mark */
3467 min_len = name_len < 0 ?
3468 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3469 TRD_INDENT_BEFORE_TYPE;
3470 ret = abs(max_len4all) - abs(name_len);
3471
3472 /* correction -> negative indicate that name is too long. */
3473 return ret < 0 ? min_len : ret;
3474}
3475
3476/**
3477 * @brief Print node.
3478 *
aPiecek01598c02021-04-23 14:18:24 +02003479 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003480 * But difference is that take @p max_gap_before_type which will be
3481 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003482 *
aPiecek9bdd7592021-05-20 08:13:20 +02003483 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003484 * @param[in] max_gap_before_type is number of indent before \<type\>.
3485 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003486 * @param[in] pc contains mainly functions for printing.
3487 * @param[in] tc is tree context.
3488 */
3489static void
aPiecek9bdd7592021-05-20 08:13:20 +02003490trb_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)
aPiecek61d062b2020-11-02 11:05:09 +01003491{
aPiecek61d062b2020-11-02 11:05:09 +01003492 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3493
3494 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3495 /* print actual node with unified indent */
3496 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3497 }
3498 /* after -> print actual node with default indent */
3499 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3500 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
aPiecek7ed8d032022-10-10 12:32:27 +02003501 if ((node.flags == TRD_FLAGS_TYPE_MOUNT_POINT) && node.mount) {
ekinzie0ab8b302022-10-10 03:03:57 -04003502 struct trt_wrapper wr_mount;
3503 struct tro_getters get;
3504
3505 wr_mount = pc->fp.read.if_sibling_exists(tc) ?
3506 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3507
3508 get = tc->lysc_tree ? troc_init_getters() : trop_init_getters();
3509 if (get.child(tc->lysc_tree ? (void *)tc->cn : (void *)tc->pn)) {
3510 /* If this node has a child, we need to draw a vertical line
3511 * from the last mounted module to the first child
3512 */
3513 wr_mount = trp_wrapper_set_mark_top(wr_mount);
3514 }
3515
aPiecek40f22402022-10-14 10:48:08 +02003516 tc->last_error = trb_print_mount_point(node.mount, wr_mount, pc);
ekinzie0ab8b302022-10-10 03:03:57 -04003517 }
aPiecek61d062b2020-11-02 11:05:09 +01003518}
3519
3520/**
aPiecek874ea4d2021-04-19 12:26:36 +02003521 * @brief Check if parent of the current node is the last
3522 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003523 *
aPiecek874ea4d2021-04-19 12:26:36 +02003524 * To mantain stability use this function only if the current node is
3525 * the first of the siblings.
3526 * Side-effect -> current node is set to the first sibling
3527 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003528 *
aPiecek01598c02021-04-23 14:18:24 +02003529 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003530 * @param[in,out] tc is tree context.
3531 * @return 1 if parent is last sibling otherwise 0.
3532 */
3533static ly_bool
3534trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3535{
3536 if (fp.modify.parent(tc)) {
3537 ly_bool ret = fp.read.if_sibling_exists(tc);
Michal Vasko26bbb272022-08-02 14:54:33 +02003538
aPiecek61d062b2020-11-02 11:05:09 +01003539 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3540 return !ret;
3541 } else {
3542 return !fp.read.if_sibling_exists(tc);
3543 }
3544}
3545
3546/**
3547 * @brief Find sibling with the biggest node name and return that size.
3548 *
3549 * Side-effect -> Current node is set to the first sibling.
3550 *
3551 * @param[in] ca contains inherited data from ancestors.
3552 * @param[in] pc contains mainly functions for printing.
3553 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003554 * @return positive number as a sign that only the node name is
3555 * included in the size.
3556 * @return negative number sign that node name and his opt mark is
3557 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003558 */
3559static int32_t
3560trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3561{
3562 int32_t ret = 0;
3563
3564 pc->fp.modify.first_sibling(tc);
3565
3566 for (struct trt_node node = pc->fp.read.node(ca, tc);
3567 !trp_node_is_empty(node);
3568 node = pc->fp.modify.next_sibling(ca, tc)) {
3569 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
Michal Vasko26bbb272022-08-02 14:54:33 +02003570
aPiecek61d062b2020-11-02 11:05:09 +01003571 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3572 }
3573 pc->fp.modify.first_sibling(tc);
3574 return ret;
3575}
3576
3577/**
aPiecek874ea4d2021-04-19 12:26:36 +02003578 * @brief Find maximal indent between
3579 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003580 *
3581 * Side-effect -> Current node is set to the first sibling.
3582 *
3583 * @param[in] ca contains inherited data from ancestors.
3584 * @param[in] pc contains mainly functions for printing.
3585 * @param[in,out] tc is tree context.
3586 * @return max btw_opts_type value for rest of the siblings
3587 */
3588static int16_t
3589trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3590{
3591 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3592 int16_t ind_before_type = maxlen_node_name < 0 ?
3593 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3594 TRD_INDENT_BEFORE_TYPE;
3595
3596 return abs(maxlen_node_name) + ind_before_type;
3597}
3598
3599/**
aPiecek874ea4d2021-04-19 12:26:36 +02003600 * @brief Find out if it is possible to unify
3601 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003602 *
aPiecek874ea4d2021-04-19 12:26:36 +02003603 * The goal is for all node siblings to have the same alignment
3604 * for \<type\> as if they were in a column. All siblings who cannot
3605 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003606 * Side-effect -> Current node is set to the first sibling.
3607 *
3608 * @param[in] ca contains inherited data from ancestors.
3609 * @param[in] pc contains mainly functions for printing.
3610 * @param[in,out] tc is tree context.
3611 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003612 * @return positive number indicating the maximum number of spaces
3613 * before \<type\> if the length of the node name is 0. To calculate
3614 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003615 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003616*/
3617static uint32_t
3618trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3619{
3620 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3621}
3622
3623/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003624 * @brief Check if there is no case statement
3625 * under the choice statement.
3626 *
3627 * It can return true only if the Parsed schema tree
3628 * is used for browsing.
3629 *
3630 * @param[in] tc is tree context.
3631 * @return 1 if implicit case statement is present otherwise 0.
3632 */
3633static ly_bool
3634trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3635{
3636 return !tc->lysc_tree && tc->pn->parent &&
3637 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3638 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3639 LYS_LEAF | LYS_LEAFLIST));
3640}
3641
3642static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3643 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3644
3645/**
3646 * @brief Print implicit case node and his subtree.
3647 *
3648 * @param[in] node is child of implicit case.
3649 * @param[in] wr is wrapper for printing identation before node.
3650 * @param[in] ca contains inherited data from ancestors.
3651 * @param[in] pc contains mainly functions for printing.
3652 * @param[in] tc is tree context. Its settings should be the same as
3653 * before the function call.
3654 */
3655static void
3656trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3657 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3658{
3659 struct trt_node case_node;
3660 struct trt_wrapper wr_case_child;
3661
3662 case_node = tro_create_implicit_case_node(node);
3663 ly_print_(pc->out, "\n");
3664 trb_print_entire_node(case_node, 0, wr, pc, tc);
3665 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3666 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3667 ly_print_(pc->out, "\n");
3668 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3669}
3670
3671/**
aPiecek874ea4d2021-04-19 12:26:36 +02003672 * @brief For the current node: recursively print all of its child
3673 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003674 *
aPiecek01598c02021-04-23 14:18:24 +02003675 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003676 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003677 * Nodes are printed, including unified sibling node alignment
3678 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003679 * Side-effect -> current node is set to the last sibling.
3680 *
3681 * @param[in] wr is wrapper for printing identation before node.
3682 * @param[in] ca contains inherited data from ancestors.
3683 * @param[in] pc contains mainly functions for printing.
3684 * @param[in,out] tc is tree context.
3685 */
3686static void
3687trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3688{
3689 uint32_t max_gap_before_type;
3690 ly_bool sibling_flag = 0;
3691 ly_bool child_flag = 0;
3692
3693 /* if node is last sibling, then do not add '|' to wrapper */
3694 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3695 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3696
3697 /* try unified indentation in node */
3698 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3699
3700 /* print all siblings */
3701 do {
3702 struct trt_parent_cache new_ca;
3703 struct trt_node node;
Michal Vasko26bbb272022-08-02 14:54:33 +02003704
aPiecek9bdd7592021-05-20 08:13:20 +02003705 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003706
aPiecekb8d5a0a2021-05-20 08:20:24 +02003707 if (!trb_need_implicit_node_case(tc)) {
3708 /* normal behavior */
3709 ly_print_(pc->out, "\n");
3710 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3711 new_ca = tro_parent_cache_for_child(ca, tc);
3712 /* go to the actual node's child or stay in actual node */
3713 node = pc->fp.modify.next_child(ca, tc);
3714 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003715
aPiecekb8d5a0a2021-05-20 08:20:24 +02003716 if (child_flag) {
3717 /* print all childs - recursive call */
3718 trb_print_nodes(wr, new_ca, pc, tc);
3719 /* get back from child node to actual node */
3720 pc->fp.modify.parent(tc);
3721 }
3722 } else {
3723 /* The case statement is omitted (shorthand).
3724 * Print implicit case node and his subtree.
3725 */
3726 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003727 }
3728
3729 /* go to the actual node's sibling */
3730 node = pc->fp.modify.next_sibling(ca, tc);
3731 sibling_flag = !trp_node_is_empty(node);
3732
3733 /* go to the next sibling or stay in actual node */
3734 } while (sibling_flag);
3735}
3736
3737/**
aPiecek153b00f2021-04-20 13:52:57 +02003738 * @brief Calculate the wrapper about how deep in the tree the node is.
ekinzie0ab8b302022-10-10 03:03:57 -04003739 * @param[in] wr_in A wrapper to use as a starting point
aPiecek153b00f2021-04-20 13:52:57 +02003740 * @param[in] node from which to count.
3741 * @return wrapper for @p node.
3742 */
3743static struct trt_wrapper
ekinzie0ab8b302022-10-10 03:03:57 -04003744trb_count_depth(const struct trt_wrapper *wr_in, const struct lysc_node *node)
aPiecek153b00f2021-04-20 13:52:57 +02003745{
ekinzie0ab8b302022-10-10 03:03:57 -04003746 struct trt_wrapper wr = wr_in ? *wr_in : TRP_INIT_WRAPPER_TOP;
aPiecek153b00f2021-04-20 13:52:57 +02003747 const struct lysc_node *parent;
3748
3749 if (!node) {
3750 return wr;
3751 }
3752
3753 for (parent = node->parent; parent; parent = parent->parent) {
3754 wr = trp_wrapper_set_shift(wr);
3755 }
3756
3757 return wr;
3758}
3759
3760/**
3761 * @brief Print all parent nodes of @p node and the @p node itself.
3762 *
3763 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3764 *
3765 * @param[in] node on which the function is focused.
aPiecek7ed8d032022-10-10 12:32:27 +02003766 * @param[in] wr_in for printing identation before node.
aPiecek01598c02021-04-23 14:18:24 +02003767 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003768 * @param[in,out] tc is context of tree printer.
3769 * @return wrapper for @p node.
3770 */
3771static void
ekinzie0ab8b302022-10-10 03:03:57 -04003772trb_print_parents(const struct lysc_node *node, struct trt_wrapper *wr_in, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek153b00f2021-04-20 13:52:57 +02003773{
3774 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003775 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003776
3777 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3778
3779 /* stop recursion */
3780 if (!node) {
3781 return;
3782 }
ekinzie0ab8b302022-10-10 03:03:57 -04003783 trb_print_parents(node->parent, wr_in, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003784
3785 /* setup for printing */
3786 tc->cn = node;
ekinzie0ab8b302022-10-10 03:03:57 -04003787 wr = trb_count_depth(wr_in, node);
aPiecek153b00f2021-04-20 13:52:57 +02003788
3789 /* print node */
3790 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003791 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3792 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003793}
3794
3795/**
aPiecekdc8fd572021-04-19 10:47:23 +02003796 * @brief Get address of the current node.
3797 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003798 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003799 */
3800static const void *
3801trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3802{
aPiecek3f247652021-04-19 13:40:25 +02003803 return tc->lysc_tree ?
3804 (const void *)tc->cn :
3805 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003806}
3807
3808/**
3809 * @brief Get address of current node's child.
3810 * @param[in,out] tc contains current node.
3811 */
3812static const void *
3813trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3814{
3815 if (!trb_tree_ctx_get_node(tc)) {
3816 return NULL;
3817 }
3818
aPiecek3f247652021-04-19 13:40:25 +02003819 if (tc->lysc_tree) {
3820 return lysc_node_child(tc->cn);
3821 } else {
3822 return lysp_node_child(tc->pn);
3823 }
aPiecekdc8fd572021-04-19 10:47:23 +02003824}
3825
3826/**
3827 * @brief Set current node on its child.
3828 * @param[in,out] tc contains current node.
3829 */
3830static void
3831trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3832{
aPiecek3f247652021-04-19 13:40:25 +02003833 const void *node = trb_tree_ctx_get_child(tc);
3834
3835 if (tc->lysc_tree) {
3836 tc->cn = node;
3837 } else {
3838 tc->pn = node;
3839 }
aPiecekdc8fd572021-04-19 10:47:23 +02003840}
3841
3842/**
aPiecek61d062b2020-11-02 11:05:09 +01003843 * @brief Print subtree of nodes.
3844 *
3845 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003846 * Before root node is no linebreak printing. This must be addressed by
3847 * the caller. Root node will also be printed. Behind last printed node
3848 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003849 *
aPiecek9bdd7592021-05-20 08:13:20 +02003850 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003851 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003852 * ::trb_try_unified_indent() function for root node.
3853 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003854 * @param[in] wr is wrapper saying how deep in the whole tree
3855 * is the root of the subtree.
3856 * @param[in] ca is parent_cache from root's parent.
3857 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003858 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003859 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003860 */
3861static void
aPiecek9bdd7592021-05-20 08:13:20 +02003862trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3863 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003864{
3865 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003866
aPiecek9bdd7592021-05-20 08:13:20 +02003867 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
ekinzie0ab8b302022-10-10 03:03:57 -04003868
aPiecek61d062b2020-11-02 11:05:09 +01003869 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003870 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003871 node = pc->fp.modify.next_child(ca, tc);
3872
3873 if (!trp_node_is_empty(node)) {
3874 /* print root's nodes */
3875 trb_print_nodes(wr, new_ca, pc, tc);
3876 /* get back from child node to actual node */
3877 pc->fp.modify.parent(tc);
3878 }
3879}
3880
3881/**
3882 * @brief Get number of siblings.
3883 *
3884 * Side-effect -> current node is set to the first sibling.
3885 *
3886 * @param[in] fp contains callback functions which modify tree context
3887 * @param[in,out] tc is the tree context.
3888 * @return Number of siblings of the current node.
3889 */
3890static uint32_t
3891trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3892{
3893 uint32_t ret = 1;
3894 struct trt_node node = TRP_EMPTY_NODE;
3895
3896 /* including actual node */
3897 fp.first_sibling(tc);
3898 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3899 ret++;
3900 }
3901 fp.first_sibling(tc);
3902 return ret;
3903}
3904
3905/**
3906 * @brief Print all parents and their children.
3907 *
aPiecek874ea4d2021-04-19 12:26:36 +02003908 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003909 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003910 * for all top-level siblings. Use this function after 'module' keyword
3911 * or 'augment' and so. The nodes may not be exactly top-level in the
3912 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003913 *
aPiecek153b00f2021-04-20 13:52:57 +02003914 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3915 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003916 * @param[pc] pc contains mainly functions for printing.
3917 * @param[in,out] tc is tree context.
3918 */
3919static void
aPiecek153b00f2021-04-20 13:52:57 +02003920trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003921{
aPiecek61d062b2020-11-02 11:05:09 +01003922 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003923 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003924 uint32_t total_parents;
3925 uint32_t max_gap_before_type;
3926
aPiecekdc8fd572021-04-19 10:47:23 +02003927 if (!trb_tree_ctx_get_node(tc)) {
3928 return;
3929 }
3930
aPiecek61d062b2020-11-02 11:05:09 +01003931 ca = TRP_EMPTY_PARENT_CACHE;
3932 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3933 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3934
aPiecek3f247652021-04-19 13:40:25 +02003935 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003936 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3937 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003938 ca.lys_config = 0x0;
3939 }
aPiecekdc8fd572021-04-19 10:47:23 +02003940 }
3941
aPiecek61d062b2020-11-02 11:05:09 +01003942 for (uint32_t i = 0; i < total_parents; i++) {
3943 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003944 node = pc->fp.read.node(ca, tc);
3945 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003946 pc->fp.modify.next_sibling(ca, tc);
3947 }
3948}
3949
aPiecek7ed8d032022-10-10 12:32:27 +02003950/**
3951 * @brief Mounted module iterator.
3952 *
3953 * Omit internal modules, modules with no nodes (e.g., iana-if-types)
3954 * and modules that were loaded as the result of a parent-reference.
3955 *
3956 * @param[in] ext_ctx is special context of mount-point extension.
3957 * @param[in] parent_refs is set of parent-references. Can be NULL for case of 'inline' schema-ref.
3958 * @param[in,out] state of the iterator. Set the value to zero for initialization.
3959 * @return First/next mounted module or NULL.
3960 */
3961static const struct lys_module *
3962trb_mounted_module_iter(struct ly_ctx *ext_ctx, struct ly_set *parent_refs, uint32_t *state)
3963{
3964 const struct lys_module *mod = NULL;
3965 ly_bool from_parent_ref;
3966 uint32_t j;
3967
3968 if (!(*state)) {
3969 /* Get first non-internal module. */
3970 *state = ly_ctx_internal_modules_count(ext_ctx);
3971 }
3972
3973 while ((mod = ly_ctx_get_module_iter(ext_ctx, state))) {
3974 if (mod->compiled && !mod->compiled->data) {
3975 /* Compiled module with no data-nodes. */
3976 continue;
3977 } else if (mod->parsed && !mod->parsed->data) {
3978 /* Parsed module with no data-nodes. */
3979 continue;
3980 } else if (!parent_refs) {
3981 /* Mounting in 'inline' mode. Success. */
3982 break;
3983 }
3984
3985 /* Check if the module is not in parent-reference. */
3986 from_parent_ref = 0;
3987 for (j = 0; j < parent_refs->count; j++) {
3988 if (!strcmp(mod->ns, parent_refs->snodes[j]->module->ns)) {
3989 from_parent_ref = 1;
3990 break;
3991 }
3992 }
3993 if (!from_parent_ref) {
3994 /* Success. */
3995 break;
3996 }
3997 }
3998
3999 return mod;
4000}
4001
aPiecek874ea4d2021-04-19 12:26:36 +02004002/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004003 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02004004 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004005
4006/**
aPiecekdc8fd572021-04-19 10:47:23 +02004007 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01004008 *
aPiecekdc8fd572021-04-19 10:47:23 +02004009 * @param[in] module YANG schema tree structure representing
4010 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01004011 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02004012 * @param[in] max_line_length is the maximum line length limit
4013 * that should not be exceeded.
aPiecek7ed8d032022-10-10 12:32:27 +02004014 * @param[in] mounted context is used for printing the YANG Schema mount.
4015 * @param[in] parent_refs context is used for printing the YANG Schema mount and its parent-reference is set.
aPiecekdc8fd572021-04-19 10:47:23 +02004016 * @param[in,out] pc will be adapted to lysp_tree.
4017 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01004018 */
4019static void
aPiecek7ed8d032022-10-10 12:32:27 +02004020trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, ly_bool mounted,
4021 const struct ly_set *parent_refs, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01004022{
aPiecekdc8fd572021-04-19 10:47:23 +02004023 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02004024 .lysc_tree = 0,
aPiecek7ed8d032022-10-10 12:32:27 +02004025 .mounted = mounted || parent_refs,
aPiecekdc8fd572021-04-19 10:47:23 +02004026 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004027 .pmod = module->parsed,
4028 .cmod = NULL,
4029 .pn = module->parsed ? module->parsed->data : NULL,
4030 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek7ed8d032022-10-10 12:32:27 +02004031 .cn = NULL,
aPiecek40f22402022-10-14 10:48:08 +02004032 .parent_refs = parent_refs,
4033 .last_error = 0
aPiecekdc8fd572021-04-19 10:47:23 +02004034 };
aPiecek61d062b2020-11-02 11:05:09 +01004035
aPiecekdc8fd572021-04-19 10:47:23 +02004036 pc->out = out;
4037
4038 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02004039 .parent = trop_modi_parent,
4040 .first_sibling = trop_modi_first_sibling,
4041 .next_sibling = trop_modi_next_sibling,
4042 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01004043 };
4044
aPiecekdc8fd572021-04-19 10:47:23 +02004045 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01004046 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02004047 .node = trop_read_node,
4048 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01004049 };
4050
aPiecekdc8fd572021-04-19 10:47:23 +02004051 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02004052 .print_features_names = tro_print_features_names,
4053 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01004054 };
4055
aPiecekdc8fd572021-04-19 10:47:23 +02004056 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01004057}
4058
4059/**
aPiecek3f247652021-04-19 13:40:25 +02004060 * @brief Settings if lysc_node are used for browsing through the tree.
4061 *
4062 * Pointers to current nodes will be set to module data.
4063 *
4064 * @param[in] module YANG schema tree structure representing
4065 * YANG module.
4066 * @param[in] out is output handler.
4067 * @param[in] max_line_length is the maximum line length limit
4068 * that should not be exceeded.
aPiecek7ed8d032022-10-10 12:32:27 +02004069 * @param[in] mounted context is used for printing the YANG Schema mount.
4070 * @param[in] parent_refs context is used for printing the YANG Schema mount and its parent-reference is set.
aPiecek3f247652021-04-19 13:40:25 +02004071 * @param[in,out] pc will be adapted to lysc_tree.
4072 * @param[in,out] tc will be adapted to lysc_tree.
4073 */
4074static void
aPiecek7ed8d032022-10-10 12:32:27 +02004075trm_lysc_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, ly_bool mounted,
4076 const struct ly_set *parent_refs, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek3f247652021-04-19 13:40:25 +02004077{
4078 *tc = (struct trt_tree_ctx) {
4079 .lysc_tree = 1,
aPiecek7ed8d032022-10-10 12:32:27 +02004080 .mounted = mounted || parent_refs,
aPiecek3f247652021-04-19 13:40:25 +02004081 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004082 .pmod = module->parsed,
4083 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02004084 .tpn = NULL,
4085 .pn = NULL,
aPiecek7ed8d032022-10-10 12:32:27 +02004086 .cn = module->compiled->data,
aPiecek40f22402022-10-14 10:48:08 +02004087 .parent_refs = parent_refs,
4088 .last_error = 0
aPiecek3f247652021-04-19 13:40:25 +02004089 };
4090
4091 pc->out = out;
4092
4093 pc->fp.modify = (struct trt_fp_modify_ctx) {
4094 .parent = troc_modi_parent,
4095 .first_sibling = troc_modi_first_sibling,
4096 .next_sibling = troc_modi_next_sibling,
4097 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02004098 };
4099
4100 pc->fp.read = (struct trt_fp_read) {
4101 .module_name = tro_read_module_name,
4102 .node = troc_read_node,
4103 .if_sibling_exists = troc_read_if_sibling_exists
4104 };
4105
4106 pc->fp.print = (struct trt_fp_print) {
4107 .print_features_names = tro_print_features_names,
4108 .print_keys = tro_print_keys
4109 };
4110
4111 pc->max_line_length = max_line_length;
4112}
4113
4114/**
4115 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02004116 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02004117 * @param[in,out] tc resets to lysc browsing.
4118 */
4119static void
4120trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4121{
aPiecek40f22402022-10-14 10:48:08 +02004122 LY_ERR erc;
4123
4124 erc = tc->last_error;
aPiecek7ed8d032022-10-10 12:32:27 +02004125 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, tc->mounted, tc->parent_refs, pc, tc);
aPiecek40f22402022-10-14 10:48:08 +02004126 tc->last_error = erc;
aPiecek3f247652021-04-19 13:40:25 +02004127}
4128
4129/**
4130 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02004131 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02004132 * @param[in,out] tc resets to lysp browsing.
4133 */
4134static void
4135trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4136{
aPiecek40f22402022-10-14 10:48:08 +02004137 LY_ERR erc;
4138
4139 erc = tc->last_error;
aPiecek7ed8d032022-10-10 12:32:27 +02004140 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, tc->mounted, tc->parent_refs, pc, tc);
aPiecek40f22402022-10-14 10:48:08 +02004141 tc->last_error = erc;
aPiecek3f247652021-04-19 13:40:25 +02004142}
4143
4144/**
4145 * @brief If augment's target node is located on the current module.
4146 * @param[in] pn is examined augment.
4147 * @param[in] pmod is current module.
4148 * @return 1 if nodeid refers to the local node, otherwise 0.
4149 */
4150static ly_bool
4151trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
4152{
4153 const char *id, *prefix, *name;
4154 size_t prefix_len, name_len;
4155 const struct lys_module *mod;
4156 ly_bool ret = 0;
4157
4158 if (pn == NULL) {
4159 return ret;
4160 }
4161
4162 id = pn->nodeid;
4163 if (!id) {
4164 return ret;
4165 }
4166 /* only absolute-schema-nodeid is taken into account */
4167 assert(id[0] == '/');
4168 ++id;
4169
4170 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
4171 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02004172 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
Michal Vasko3003c9a2022-03-29 12:10:00 +02004173 ret = mod ? (mod->parsed == pmod) : 0;
aPiecek3f247652021-04-19 13:40:25 +02004174 } else {
4175 ret = 1;
4176 }
4177
4178 return ret;
4179}
4180
4181/**
aPiecek96baa7f2021-04-23 12:32:00 +02004182 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01004183 *
aPiecekdc8fd572021-04-19 10:47:23 +02004184 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02004185 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01004186 *
aPiecekdc8fd572021-04-19 10:47:23 +02004187 * @param[in] ks is section representation.
4188 * @param[in] pc contains mainly functions for printing.
4189 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01004190 */
4191static void
aPiecekdc8fd572021-04-19 10:47:23 +02004192trm_print_section_as_family_tree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01004193{
aPiecekdc8fd572021-04-19 10:47:23 +02004194 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4195 return;
4196 }
4197
4198 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
4199 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02004200 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004201 } else {
aPiecek153b00f2021-04-20 13:52:57 +02004202 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004203 }
4204}
4205
4206/**
aPiecek96baa7f2021-04-23 12:32:00 +02004207 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02004208 *
aPiecek96baa7f2021-04-23 12:32:00 +02004209 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02004210 *
4211 * @param[in] ks is section representation.
4212 * @param[in] pc contains mainly functions for printing.
4213 * @param[in,out] tc is the tree context.
4214 */
4215static void
4216trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4217{
4218 ly_bool grp_has_data = 0;
4219
4220 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4221 return;
4222 }
4223
4224 if (ks.type == TRD_KEYWORD_GROUPING) {
4225 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4226 }
4227
4228 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4229 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004230 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004231}
4232
4233/**
4234 * @brief Print 'module' keyword, its name and all nodes.
4235 * @param[in] pc contains mainly functions for printing.
4236 * @param[in,out] tc is the tree context.
4237 */
4238static void
4239trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4240{
4241 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4242}
4243
4244/**
4245 * @brief For all augment sections: print 'augment' keyword,
4246 * its target node and all nodes.
4247 * @param[in] pc contains mainly functions for printing.
4248 * @param[in,out] tc is the tree context.
4249 */
4250static void
4251trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4252{
4253 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004254 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004255
aPiecek3f247652021-04-19 13:40:25 +02004256 if (tc->lysc_tree) {
4257 origin_was_lysc_tree = 1;
4258 trm_reset_to_lysp_tree_ctx(pc, tc);
4259 }
4260
aPiecekdc8fd572021-04-19 10:47:23 +02004261 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004262 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004263 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004264 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004265
aPiecek3f247652021-04-19 13:40:25 +02004266 if (origin_was_lysc_tree) {
4267 /* if lysc tree is used, then only augments targeting
4268 * another module are printed
4269 */
aPiecek9f792e52021-04-21 08:33:56 +02004270 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004271 continue;
4272 }
4273 }
4274
aPiecekdc8fd572021-04-19 10:47:23 +02004275 if (once) {
4276 ly_print_(pc->out, "\n");
4277 ly_print_(pc->out, "\n");
4278 once = 0;
4279 } else {
4280 ly_print_(pc->out, "\n");
4281 }
4282
4283 trm_print_section_as_subtree(ks, pc, tc);
4284 }
aPiecek3f247652021-04-19 13:40:25 +02004285
4286 if (origin_was_lysc_tree) {
4287 trm_reset_to_lysc_tree_ctx(pc, tc);
4288 }
aPiecekdc8fd572021-04-19 10:47:23 +02004289}
4290
4291/**
4292 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4293 * @param[in] pc contains mainly functions for printing.
4294 * @param[in,out] tc is the tree context.
4295 */
4296static void
4297trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4298{
4299 struct trt_keyword_stmt rpc;
4300
aPiecek01598c02021-04-23 14:18:24 +02004301 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004302
4303 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4304 ly_print_(pc->out, "\n");
4305 ly_print_(pc->out, "\n");
4306 trm_print_section_as_family_tree(rpc, pc, tc);
4307 }
4308}
4309
4310/**
4311 * @brief For notifications section: print 'notifications' keyword
4312 * and all its nodes.
4313 * @param[in] pc contains mainly functions for printing.
4314 * @param[in,out] tc is the tree context.
4315 */
4316static void
4317trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4318{
4319 struct trt_keyword_stmt notifs;
4320
aPiecek01598c02021-04-23 14:18:24 +02004321 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004322
4323 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4324 ly_print_(pc->out, "\n");
4325 ly_print_(pc->out, "\n");
4326 trm_print_section_as_family_tree(notifs, pc, tc);
4327 }
4328}
4329
4330/**
4331 * @brief For all grouping sections: print 'grouping' keyword, its name
4332 * and all nodes.
4333 * @param[in] pc contains mainly functions for printing.
4334 * @param[in,out] tc is the tree context.
4335 */
4336static void
4337trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4338{
4339 ly_bool once;
4340
aPiecek01598c02021-04-23 14:18:24 +02004341 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004342 return;
4343 }
4344
4345 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004346 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004347 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004348 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004349 if (once) {
4350 ly_print_(pc->out, "\n");
4351 ly_print_(pc->out, "\n");
4352 once = 0;
4353 } else {
4354 ly_print_(pc->out, "\n");
4355 }
4356 trm_print_section_as_subtree(ks, pc, tc);
4357 }
4358}
4359
4360/**
4361 * @brief For all yang-data sections: print 'yang-data' keyword
4362 * and all its nodes.
4363 * @param[in] pc contains mainly functions for printing.
4364 * @param[in,out] tc is the tree context.
4365 */
4366static void
aPiecek96baa7f2021-04-23 12:32:00 +02004367trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004368{
aPiecek96baa7f2021-04-23 12:32:00 +02004369 ly_bool once;
4370 LY_ARRAY_COUNT_TYPE count;
4371
4372 count = LY_ARRAY_COUNT(tc->pmod->exts);
4373 if (count == 0) {
4374 return;
4375 }
4376
4377 once = 1;
4378 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4379 struct trt_keyword_stmt ks;
4380
aPiecek01598c02021-04-23 14:18:24 +02004381 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004382 * ::lysp_ext_instance.parsed.
4383 */
4384 if (!tc->pmod->exts[u].parsed) {
4385 /* print at least the yang-data names */
4386 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4387 continue;
4388 }
4389
4390 ks = tro_modi_next_yang_data(tc, u);
4391 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4392 break;
4393 }
4394
4395 if (once) {
4396 ly_print_(pc->out, "\n");
4397 ly_print_(pc->out, "\n");
4398 once = 0;
4399 } else {
4400 ly_print_(pc->out, "\n");
4401 }
4402
4403 trm_print_section_as_family_tree(ks, pc, tc);
4404 }
aPiecekdc8fd572021-04-19 10:47:23 +02004405}
4406
4407/**
4408 * @brief Print sections module, augment, rpcs, notifications,
4409 * grouping, yang-data.
4410 * @param[in] pc contains mainly functions for printing.
4411 * @param[in,out] tc is the tree context.
4412 */
4413static void
4414trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4415{
4416 trm_print_module_section(pc, tc);
4417 trm_print_augmentations(pc, tc);
4418 trm_print_rpcs(pc, tc);
4419 trm_print_notifications(pc, tc);
4420 trm_print_groupings(pc, tc);
4421 trm_print_yang_data(pc, tc);
4422 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004423}
4424
aPiecek40f22402022-10-14 10:48:08 +02004425static LY_ERR
4426tree_print_check_error(struct ly_out_clb_arg *out, struct trt_tree_ctx *tc)
4427{
4428 if (out->last_error) {
4429 return out->last_error;
4430 } else if (tc->last_error) {
4431 return tc->last_error;
4432 } else {
4433 return LY_SUCCESS;
4434 }
4435}
4436
aPiecek874ea4d2021-04-19 12:26:36 +02004437/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004438 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004439 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004440
4441LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004442tree_print_module(struct ly_out *out, const struct lys_module *module, uint32_t UNUSED(options), size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004443{
4444 struct trt_printer_ctx pc;
4445 struct trt_tree_ctx tc;
4446 struct ly_out *new_out;
4447 LY_ERR erc;
4448 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4449
aPiecekdc8fd572021-04-19 10:47:23 +02004450 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4451
aPiecek61d062b2020-11-02 11:05:09 +01004452 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4453 return erc;
4454 }
4455
4456 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004457 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
aPiecek7ed8d032022-10-10 12:32:27 +02004458 trm_lysc_tree_ctx(module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek3f247652021-04-19 13:40:25 +02004459 } else {
aPiecek7ed8d032022-10-10 12:32:27 +02004460 trm_lysp_tree_ctx(module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek3f247652021-04-19 13:40:25 +02004461 }
aPiecek61d062b2020-11-02 11:05:09 +01004462
4463 trm_print_sections(&pc, &tc);
aPiecek40f22402022-10-14 10:48:08 +02004464 erc = tree_print_check_error(&clb_arg, &tc);
aPiecek61d062b2020-11-02 11:05:09 +01004465
4466 ly_out_free(new_out, NULL, 1);
4467
aPiecekdc8fd572021-04-19 10:47:23 +02004468 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004469}
4470
4471LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004472tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options, size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004473{
aPiecek153b00f2021-04-20 13:52:57 +02004474 struct trt_printer_ctx pc;
4475 struct trt_tree_ctx tc;
4476 struct ly_out *new_out;
4477 struct trt_wrapper wr;
4478 LY_ERR erc;
4479 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4480
4481 assert(out && node);
4482
4483 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4484 return LY_EINVAL;
4485 }
4486
4487 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4488 return erc;
4489 }
4490
4491 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek7ed8d032022-10-10 12:32:27 +02004492 trm_lysc_tree_ctx(node->module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004493
4494 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
ekinzie0ab8b302022-10-10 03:03:57 -04004495 trb_print_parents(node, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004496
4497 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4498 tc.cn = lysc_node_child(node);
ekinzie0ab8b302022-10-10 03:03:57 -04004499 wr = trb_count_depth(NULL, tc.cn);
aPiecek153b00f2021-04-20 13:52:57 +02004500 trb_print_family_tree(wr, &pc, &tc);
4501 }
4502 ly_print_(out, "\n");
4503
aPiecek40f22402022-10-14 10:48:08 +02004504 erc = tree_print_check_error(&clb_arg, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004505 ly_out_free(new_out, NULL, 1);
4506
4507 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004508}
4509
4510LY_ERR
Michal Vasko6a914fb2022-01-17 10:36:14 +01004511tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
4512 size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004513{
aPiecek9f792e52021-04-21 08:33:56 +02004514 struct trt_printer_ctx pc;
4515 struct trt_tree_ctx tc;
4516 struct ly_out *new_out;
4517 LY_ERR erc;
4518 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4519
4520 assert(submodp);
4521 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4522
4523 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4524 return erc;
4525 }
4526
4527 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek7ed8d032022-10-10 12:32:27 +02004528 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek9f792e52021-04-21 08:33:56 +02004529 tc.pmod = (struct lysp_module *)submodp;
4530 tc.tpn = submodp->data;
4531 tc.pn = tc.tpn;
4532
4533 trm_print_sections(&pc, &tc);
aPiecek40f22402022-10-14 10:48:08 +02004534 erc = tree_print_check_error(&clb_arg, &tc);
aPiecek9f792e52021-04-21 08:33:56 +02004535
4536 ly_out_free(new_out, NULL, 1);
4537
4538 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004539}
ekinzie0ab8b302022-10-10 03:03:57 -04004540
aPiecek7ed8d032022-10-10 12:32:27 +02004541/**
4542 * @brief Print all mounted nodes ('/') and parent-referenced nodes ('@').
4543 *
4544 * @param[in] ext is mount-point extension.
4545 * @param[in] wr is wrapper to be printed.
4546 * @param[in] pc contains mainly functions for printing.
4547 * @return LY_ERR value.
4548 */
ekinzie0ab8b302022-10-10 03:03:57 -04004549static LY_ERR
aPiecek7ed8d032022-10-10 12:32:27 +02004550trb_print_mount_point(const struct lysc_ext_instance *ext, const struct trt_wrapper wr, struct trt_printer_ctx *pc)
ekinzie0ab8b302022-10-10 03:03:57 -04004551{
aPiecek7ed8d032022-10-10 12:32:27 +02004552 LY_ERR ret = LY_SUCCESS;
ekinzie0ab8b302022-10-10 03:03:57 -04004553 struct ly_ctx *ext_ctx = NULL;
aPiecek7ed8d032022-10-10 12:32:27 +02004554 const struct lys_module *mod, *last_mod;
ekinzie0ab8b302022-10-10 03:03:57 -04004555 struct trt_tree_ctx tmptc;
4556 struct trt_wrapper tmpwr;
4557 struct trt_printer_ctx tmppc;
ekinzie0ab8b302022-10-10 03:03:57 -04004558 struct ly_set *refs = NULL;
aPiecek7ed8d032022-10-10 12:32:27 +02004559 uint32_t i, iter_state;
aPiecek36f82232022-10-14 10:08:38 +02004560 ly_bool notif, rpc;
ekinzie0ab8b302022-10-10 03:03:57 -04004561
aPiecek7ed8d032022-10-10 12:32:27 +02004562 if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) {
ekinzie0ab8b302022-10-10 03:03:57 -04004563 /* Void mount point */
4564 return LY_SUCCESS;
4565 }
4566
aPiecek40f22402022-10-14 10:48:08 +02004567 ret = lyplg_ext_schema_mount_get_parent_ref(ext, &refs);
4568 LY_CHECK_GOTO(ret, cleanup);
aPiecek7ed8d032022-10-10 12:32:27 +02004569
4570 /* Get the last mounted module which will be printed. */
4571 iter_state = 0;
4572 while ((mod = trb_mounted_module_iter(ext_ctx, refs, &iter_state))) {
4573 last_mod = mod;
ekinzie0ab8b302022-10-10 03:03:57 -04004574 }
4575
ekinzie0ab8b302022-10-10 03:03:57 -04004576 tmppc = *pc;
aPiecek7ed8d032022-10-10 12:32:27 +02004577 iter_state = 0;
4578 while ((mod = trb_mounted_module_iter(ext_ctx, refs, &iter_state))) {
4579 /* Prepare printer tree context. */
ekinzie0ab8b302022-10-10 03:03:57 -04004580 if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) {
aPiecek7ed8d032022-10-10 12:32:27 +02004581 trm_lysc_tree_ctx(mod, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
aPiecek36f82232022-10-14 10:08:38 +02004582 notif = tmptc.cmod->notifs ? 1 : 0;
4583 rpc = tmptc.cmod->rpcs ? 1 : 0;
ekinzie0ab8b302022-10-10 03:03:57 -04004584 } else {
aPiecek7ed8d032022-10-10 12:32:27 +02004585 trm_lysp_tree_ctx(mod, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
aPiecek36f82232022-10-14 10:08:38 +02004586 notif = tmptc.pmod->notifs ? 1 : 0;
4587 rpc = tmptc.pmod->rpcs ? 1 : 0;
ekinzie0ab8b302022-10-10 03:03:57 -04004588 }
aPiecek36f82232022-10-14 10:08:38 +02004589
aPiecek7ed8d032022-10-10 12:32:27 +02004590 /* Decide whether to print the symbol '|'. */
aPiecek36f82232022-10-14 10:08:38 +02004591 tmpwr = (mod == last_mod) && !rpc && !notif && !refs ? wr : trp_wrapper_set_mark_top(wr);
aPiecek7ed8d032022-10-10 12:32:27 +02004592 /* Print top-level nodes of mounted module which are denoted by the symbol '/'. */
4593 trb_print_family_tree(tmpwr, &tmppc, &tmptc);
aPiecek36f82232022-10-14 10:08:38 +02004594
4595 /* Print top-level rpc nodes. */
4596 if (rpc) {
4597 tro_modi_get_rpcs(&tmptc);
4598 tmpwr = (mod == last_mod) && !notif && !refs ? wr : trp_wrapper_set_mark_top(wr);
4599 trb_print_family_tree(tmpwr, &tmppc, &tmptc);
4600 }
4601
4602 /* Print top-level notification nodes. */
4603 if (notif) {
4604 tro_modi_get_notifications(&tmptc);
4605 tmpwr = (mod == last_mod) && !refs ? wr : trp_wrapper_set_mark_top(wr);
4606 trb_print_family_tree(tmpwr, &tmppc, &tmptc);
4607 }
ekinzie0ab8b302022-10-10 03:03:57 -04004608 }
4609
aPiecek7ed8d032022-10-10 12:32:27 +02004610 /* Print parent-referenced nodes which are denoted by the symbol '@'. */
4611 for (i = 0; refs && i < refs->count; i++) {
4612 trm_lysc_tree_ctx(refs->snodes[i]->module, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
aPiecek42174592022-10-13 13:53:25 +02004613 tmpwr = ((i + 1) == refs->count) ? wr : trp_wrapper_set_mark_top(wr);
aPiecek7ed8d032022-10-10 12:32:27 +02004614 trb_print_parents(refs->snodes[i], &tmpwr, pc, &tmptc);
ekinzie0ab8b302022-10-10 03:03:57 -04004615 }
4616
aPiecek40f22402022-10-14 10:48:08 +02004617cleanup:
ekinzie0ab8b302022-10-10 03:03:57 -04004618 ly_set_free(refs, NULL);
ekinzie0ab8b302022-10-10 03:03:57 -04004619 ly_ctx_destroy(ext_ctx);
aPiecek7ed8d032022-10-10 12:32:27 +02004620
4621 return ret;
ekinzie0ab8b302022-10-10 03:03:57 -04004622}