blob: 2a03c0464fdf86435cdac0e2a84fe9cef79f960d [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\>! */
350 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list (without keys). \<name\>* */
351 TRD_NODE_KEYS, /**< For a list's keys. \<name\>* [\<keys\>] */
352 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
353 TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */
aPiecek874ea4d2021-04-19 12:26:36 +0200354 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 +0100355} trt_node_type;
356
357/**
358 * @brief Type of node and his name.
359 *
aPiecek874ea4d2021-04-19 12:26:36 +0200360 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
aPiecek61d062b2020-11-02 11:05:09 +0100361 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
362 */
363struct trt_node_name {
364 trt_node_type type; /**< Type of the node relevant for printing. */
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) { \
375 .type = TRD_NODE_ELSE, .module_prefix = NULL, .str = NULL \
376 }
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 {
aPiecek874ea4d2021-04-19 12:26:36 +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. */
ekinzie0ab8b302022-10-10 03:03:57 -0400458 struct lysc_ext_instance
459 *mount; /**< Mount-point extension if flags == TRD_FLAGS_TYPE_MOUNT_POINT */
aPiecek61d062b2020-11-02 11:05:09 +0100460};
461
462/**
463 * @brief Create struct trt_node as empty.
464 */
465#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200466 (struct trt_node) { \
467 .status = TRD_STATUS_TYPE_EMPTY, \
468 .flags = TRD_FLAGS_TYPE_EMPTY, \
469 .name = TRP_EMPTY_NODE_NAME, \
470 .type = TRP_EMPTY_TRT_TYPE, \
471 .iffeatures = 0, \
ekinzie0ab8b302022-10-10 03:03:57 -0400472 .last_one = 1, \
473 .mount = NULL \
aPiecek874ea4d2021-04-19 12:26:36 +0200474 }
aPiecek61d062b2020-11-02 11:05:09 +0100475
476/**
477 * @brief Package which only groups indent and node.
478 */
479struct trt_pair_indent_node {
480 struct trt_indent_in_node indent;
481 struct trt_node node;
482};
483
484/**
485 * @brief Initialize struct trt_pair_indent_node by parameters.
486 */
487#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200488 (struct trt_pair_indent_node) { \
489 .indent = INDENT_IN_NODE, .node = NODE \
490 }
aPiecek61d062b2020-11-02 11:05:09 +0100491
aPiecek874ea4d2021-04-19 12:26:36 +0200492/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100493 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200494 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100495
496#define TRD_TOP_KEYWORD_MODULE "module"
497#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
498
499#define TRD_BODY_KEYWORD_AUGMENT "augment"
500#define TRD_BODY_KEYWORD_RPC "rpcs"
501#define TRD_BODY_KEYWORD_NOTIF "notifications"
502#define TRD_BODY_KEYWORD_GROUPING "grouping"
503#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
504
505/**
506 * @brief Type of the trt_keyword.
507 */
508typedef enum {
509 TRD_KEYWORD_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100510 TRD_KEYWORD_MODULE,
511 TRD_KEYWORD_SUBMODULE,
512 TRD_KEYWORD_AUGMENT,
513 TRD_KEYWORD_RPC,
514 TRD_KEYWORD_NOTIF,
515 TRD_KEYWORD_GROUPING,
aPiecek61d062b2020-11-02 11:05:09 +0100516 TRD_KEYWORD_YANG_DATA
517} trt_keyword_type;
518
519/**
520 * @brief Main sign of the tree nodes.
521 *
aPiecek874ea4d2021-04-19 12:26:36 +0200522 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100523 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
524 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
525 * trp_keyword_type_strlen
526 *
527 */
528struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200529 trt_keyword_type type; /**< String containing some of the top or body keyword. */
530 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100531};
532
533/**
534 * @brief Create struct trt_keyword_stmt as empty.
535 */
536#define TRP_EMPTY_KEYWORD_STMT \
537 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
538
539/**
540 * @brief Check if struct trt_keyword_stmt is empty.
541 */
542#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
543 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
544
545/**
546 * @brief Initialize struct trt_keyword_stmt by parameters.
547 */
548#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
549 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
550
aPiecek874ea4d2021-04-19 12:26:36 +0200551/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100552 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200553 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100554
555struct trt_parent_cache;
556
557/**
558 * @brief Functions that change the state of the tree_ctx structure.
559 *
aPiecek3f247652021-04-19 13:40:25 +0200560 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200561 * for the 'trp' printing functions and are also called from the
562 * 'trb' browsing functions when walking through a tree. These callback
563 * functions need to be checked or reformulated if changes to the
564 * libyang library affect the printing tree. For all, if the value
565 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
566 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100567 */
568struct trt_fp_modify_ctx {
569 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
570 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
571 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
572 struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */
aPiecek61d062b2020-11-02 11:05:09 +0100573};
574
aPiecek874ea4d2021-04-19 12:26:36 +0200575/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100576 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200577 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100578
579/**
580 * @brief Functions that do not change the state of the tree_structure.
581 *
582 * For details see trt_fp_modify_ctx.
583 */
584struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200585 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
586 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
587 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100588};
589
aPiecek874ea4d2021-04-19 12:26:36 +0200590/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100591 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200592 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100593
594/**
aPiecek874ea4d2021-04-19 12:26:36 +0200595 * @brief A set of all necessary functions that must be provided
596 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100597 */
598struct trt_fp_all {
599 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
600 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
601 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
602};
603
aPiecek874ea4d2021-04-19 12:26:36 +0200604/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100605 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200606 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100607
608/**
aPiecek01598c02021-04-23 14:18:24 +0200609 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100610 */
611struct trt_printer_ctx {
612 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200613 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100614 size_t max_line_length; /**< The maximum number of characters that can be
615 printed on one line, including the last. */
616};
617
aPiecek874ea4d2021-04-19 12:26:36 +0200618/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100619 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200620 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100621
622/**
623 * @brief The name of the section to which the node belongs.
624 */
625typedef enum {
626 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100627 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
628 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
629 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
630 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
aPiecek61d062b2020-11-02 11:05:09 +0100631 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
632} trt_actual_section;
633
634/**
635 * @brief Types of nodes that have some effect on their children.
636 */
637typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200638 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100639 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
640 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
aPiecek874ea4d2021-04-19 12:26:36 +0200641 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100642} trt_ancestor_type;
643
644/**
645 * @brief Saved information when browsing the tree downwards.
646 *
aPiecek874ea4d2021-04-19 12:26:36 +0200647 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200648 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200649 * this structures during their recursive calls. This functions do not
650 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200651 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200652 * structure, can return a node with the correct data. The word
653 * \b parent is in the structure name, because this data refers to
654 * the last parent and at the same time the states of its
655 * ancestors data. Only the function jumping on the child
656 * (next_child(...)) creates this structure, because the pointer
657 * to the current node moves down the tree. It's like passing
658 * the genetic code to children. Some data must be inherited and
659 * there are two approaches to this problem. Either it will always
660 * be determined which inheritance states belong to the current node
661 * (which can lead to regular travel to the root node) or
662 * the inheritance states will be stored during the recursive calls.
663 * So the problem was solved by the second option. Why does
664 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200665 * the lysp tree. For walks through the lysc tree is trt_parent_cache
666 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100667 *
aPiecek874ea4d2021-04-19 12:26:36 +0200668 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100669 */
670struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200671 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
672 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
673 uint16_t lys_config; /**< Inherited config W or R. */
674 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100675};
676
677/**
678 * @brief Return trt_parent_cache filled with default values.
679 */
680#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200681 (struct trt_parent_cache) { \
682 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
683 .lys_config = LYS_CONFIG_W, .last_list = NULL \
684 }
aPiecek61d062b2020-11-02 11:05:09 +0100685
686/**
687 * @brief Main structure for browsing the libyang tree
688 */
689struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200690 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
691 It is assumed that once set, it does not change.
692 If it is true then trt_tree_ctx.pn and
693 trt_tree_ctx.tpn are not used.
694 If it is false then trt_tree_ctx.cn is not used. */
ekinzie0ab8b302022-10-10 03:03:57 -0400695 ly_bool mounted; /**< This tree is a mounted schema */
aPiecek96baa7f2021-04-23 12:32:00 +0200696 trt_actual_section section; /**< To which section pn points. */
697 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
698 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
699 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200700
aPiecek96baa7f2021-04-23 12:32:00 +0200701 union {
702 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
703 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
704 is set to TRD_SECT_YANG_DATA. */
705 };
706 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
ekinzie0ab8b302022-10-10 03:03:57 -0400707 const struct ly_set *parent_refs; /**< List of schema nodes for top-level nodes found in mount
708 point parent references */
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
ekinzie0ab8b302022-10-10 03:03:57 -0400729/**
730 * @brief Context for mounted module
731 *
732 */
733struct trt_mount_ctx {
734 struct trt_printer_ctx pc;
735 struct trt_tree_ctx tc;
736};
737
aPiecek01598c02021-04-23 14:18:24 +0200738/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100739typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
740
aPiecekef1e58e2021-04-19 13:19:44 +0200741/**
742 * @brief Simple getter functions for lysp and lysc nodes.
743 *
744 * This structure is useful if we have a general algorithm
745 * (tro function) that can be used for both lysc and lysp nodes.
746 * Thanks to this structure, we prevent code redundancy.
747 * We don't have to write basically the same algorithm twice
748 * for lysp and lysc trees.
749 */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100750struct tro_getters {
aPiecekef1e58e2021-04-19 13:19:44 +0200751 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
752 const void *(*next)(const void *); /**< Get sibling. */
753 const void *(*parent)(const void *); /**< Get parent. */
754 const void *(*child)(const void *); /**< Get child. */
755 const void *(*actions)(const void *); /**< Get actions. */
756 const void *(*action_input)(const void *); /**< Get input action from action node. */
757 const void *(*action_output)(const void *); /**< Get output action from action node. */
758 const void *(*notifs)(const void *); /**< Get notifs. */
759};
760
aPiecek874ea4d2021-04-19 12:26:36 +0200761/**********************************************************************
ekinzie0ab8b302022-10-10 03:03:57 -0400762 * Forward declarations
763 *********************************************************************/
764static LY_ERR trb_print_mount_point(struct trt_node *node, struct trt_wrapper wr,
765 struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
766
767/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100768 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200769 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100770
771/**
772 * @brief Print a substring but limited to the maximum length.
773 * @param[in] str is pointer to source.
774 * @param[in] len is number of characters to be printed.
775 * @param[in,out] out is output handler.
776 * @return str parameter shifted by len.
777 */
778static const char *
779trg_print_substr(const char *str, size_t len, struct ly_out *out)
780{
781 for (size_t i = 0; i < len; i++) {
782 ly_print_(out, "%c", str[0]);
783 str++;
784 }
785 return str;
786}
787
788/**
789 * @brief Pointer is not NULL and does not point to an empty string.
790 * @param[in] str is pointer to string to be checked.
791 * @return 1 if str pointing to non empty string otherwise 0.
792 */
793static ly_bool
794trg_charptr_has_data(const char *str)
795{
796 return (str) && (str[0] != '\0');
797}
798
799/**
aPiecek874ea4d2021-04-19 12:26:36 +0200800 * @brief Check if @p word in @p src is present where words are
801 * delimited by @p delim.
802 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100803 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200804 * @param[in] delim is delimiter between @p words in @p src.
805 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100806 */
807static ly_bool
808trg_word_is_present(const char *src, const char *word, char delim)
809{
810 const char *hit;
811
812 if ((!src) || (src[0] == '\0') || (!word)) {
813 return 0;
814 }
815
816 hit = strstr(src, word);
817
818 if (hit) {
819 /* word was founded at the begin of src
820 * OR it match somewhere after delim
821 */
822 if ((hit == src) || (hit[-1] == delim)) {
823 /* end of word was founded at the end of src
824 * OR end of word was match somewhere before delim
825 */
826 char delim_or_end = (hit + strlen(word))[0];
Michal Vasko26bbb272022-08-02 14:54:33 +0200827
aPiecek61d062b2020-11-02 11:05:09 +0100828 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
829 return 1;
830 }
831 }
832 /* after -> hit is just substr and it's not the whole word */
833 /* jump to the next word */
834 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
835 /* skip delim */
836 src = src[0] == '\0' ? src : src + 1;
837 /* continue with searching */
838 return trg_word_is_present(src, word, delim);
839 } else {
840 return 0;
841 }
842}
843
aPiecek874ea4d2021-04-19 12:26:36 +0200844/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100845 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200846 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100847
848/**
aPiecek01598c02021-04-23 14:18:24 +0200849 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100850 *
aPiecek874ea4d2021-04-19 12:26:36 +0200851 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100852 * @param[in] buf contains input characters
853 * @param[in] count is number of characters in buf.
854 * @return Number of printed bytes.
855 * @return Negative value in case of error.
856 */
857static ssize_t
858trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
859{
860 LY_ERR erc = LY_SUCCESS;
861 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
862
863 switch (data->mode) {
864 case TRD_PRINT:
865 erc = ly_write_(data->out, buf, count);
866 break;
867 case TRD_CHAR_COUNT:
868 data->counter = data->counter + count;
869 break;
870 default:
871 break;
872 }
873
874 if (erc != LY_SUCCESS) {
875 data->last_error = erc;
876 return -1;
877 } else {
878 return count;
879 }
880}
881
882/**
883 * @brief Check that indent in node can be considered as equivalent.
884 * @param[in] first is the first indent in node.
885 * @param[in] second is the second indent in node.
886 * @return 1 if indents are equivalent otherwise 0.
887 */
888static ly_bool
889trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
890{
891 const ly_bool a = first.type == second.type;
892 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
893 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
894 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
895
896 return a && b && c && d;
897}
898
899/**
aPiecek874ea4d2021-04-19 12:26:36 +0200900 * @brief Setting space character because node is last sibling.
901 * @param[in] wr is wrapper over which the shift operation
902 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100903 * @return New shifted wrapper.
904 */
905static struct trt_wrapper
906trp_wrapper_set_shift(struct trt_wrapper wr)
907{
908 assert(wr.actual_pos < 64);
909 /* +--<node>
910 * +--<node>
911 */
912 wr.actual_pos++;
913 return wr;
914}
915
916/**
aPiecek874ea4d2021-04-19 12:26:36 +0200917 * @brief Setting '|' symbol because node is divided or
918 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100919 * @param[in] wr is source of wrapper.
920 * @return New wrapper which is marked at actual position and shifted.
921 */
922static struct trt_wrapper
923trp_wrapper_set_mark(struct trt_wrapper wr)
924{
925 assert(wr.actual_pos < 64);
926 wr.bit_marks1 |= 1U << wr.actual_pos;
927 return trp_wrapper_set_shift(wr);
928}
929
930/**
ekinzie0ab8b302022-10-10 03:03:57 -0400931 * @brief Set '|' symbol to connect current level nodes in a module.
932 * This is only used to connect all top-level nodes in all modules under
933 * a schema mount point.
934 * @param[in] wr is the wrapper to be marked
935 * @return New wrapper which is marked at actual position.
936 */
937static struct trt_wrapper
938trp_wrapper_set_mark_top(struct trt_wrapper wr)
939{
940 wr.bit_marks1 |= 1U << wr.actual_pos;
941 return wr;
942}
943
944/**
aPiecek61d062b2020-11-02 11:05:09 +0100945 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
946 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200947 * @param[in] last_one is flag. Value 1 saying if the node is the last
948 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100949 * @return New wrapper for the actual node.
950 */
951static struct trt_wrapper
952trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
953{
954 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
955}
956
957/**
958 * @brief Test if the wrappers are equivalent.
959 * @param[in] first is the first wrapper.
960 * @param[in] second is the second wrapper.
961 * @return 1 if the wrappers are equivalent otherwise 0.
962 */
963static ly_bool
964trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
965{
966 const ly_bool a = first.type == second.type;
967 const ly_bool b = first.bit_marks1 == second.bit_marks1;
968 const ly_bool c = first.actual_pos == second.actual_pos;
969
970 return a && b && c;
971}
972
973/**
974 * @brief Print " | " sequence on line.
975 * @param[in] wr is wrapper to be printed.
976 * @param[in,out] out is output handler.
977 */
978static void
979trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
980{
981 uint32_t lb;
982
983 if (wr.type == TRD_WRAPPER_TOP) {
984 lb = TRD_INDENT_LINE_BEGIN;
985 } else if (wr.type == TRD_WRAPPER_BODY) {
986 lb = TRD_INDENT_LINE_BEGIN * 2;
987 } else {
988 lb = TRD_INDENT_LINE_BEGIN;
989 }
990
991 ly_print_(out, "%*c", lb, ' ');
992
993 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
994 return;
995 }
996
997 for (uint32_t i = 0; i < wr.actual_pos; i++) {
998 /** Test if the bit on the index is set. */
999 if ((wr.bit_marks1 >> i) & 1U) {
1000 ly_print_(out, "|");
1001 } else {
1002 ly_print_(out, " ");
1003 }
1004
1005 if (i != wr.actual_pos) {
1006 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1007 }
1008 }
1009}
1010
1011/**
1012 * @brief Check if struct trt_node is empty.
1013 * @param[in] node is item to test.
1014 * @return 1 if node is considered empty otherwise 0.
1015 */
1016static ly_bool
1017trp_node_is_empty(struct trt_node node)
1018{
1019 const ly_bool a = !node.iffeatures;
1020 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
1021 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
1022 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
1023 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
1024
1025 return a && b && c && d && e;
1026}
1027
1028/**
aPiecek874ea4d2021-04-19 12:26:36 +02001029 * @brief Check if [\<keys\>], \<type\> and
1030 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +01001031 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +02001032 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
1033 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +01001034 */
1035static ly_bool
1036trp_node_body_is_empty(struct trt_node node)
1037{
1038 const ly_bool a = !node.iffeatures;
1039 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
1040 const ly_bool c = node.name.type != TRD_NODE_KEYS;
1041
1042 return a && b && c;
1043}
1044
1045/**
1046 * @brief Print \<status\> of the node.
1047 * @param[in] status_type is type of status.
1048 * @param[in,out] out is output handler.
1049 */
1050static void
1051trp_print_status(trt_status_type status_type, struct ly_out *out)
1052{
1053 switch (status_type) {
1054 case TRD_STATUS_TYPE_CURRENT:
1055 ly_print_(out, "%c", '+');
1056 break;
1057 case TRD_STATUS_TYPE_DEPRECATED:
1058 ly_print_(out, "%c", 'x');
1059 break;
1060 case TRD_STATUS_TYPE_OBSOLETE:
1061 ly_print_(out, "%c", 'o');
1062 break;
1063 default:
1064 break;
1065 }
1066}
1067
1068/**
1069 * @brief Print \<flags\>.
1070 * @param[in] flags_type is type of \<flags\>.
1071 * @param[in,out] out is output handler.
1072 */
1073static void
1074trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1075{
1076 switch (flags_type) {
1077 case TRD_FLAGS_TYPE_RW:
1078 ly_print_(out, "%s", "rw");
1079 break;
1080 case TRD_FLAGS_TYPE_RO:
1081 ly_print_(out, "%s", "ro");
1082 break;
1083 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1084 ly_print_(out, "%s", "-w");
1085 break;
1086 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1087 ly_print_(out, "%s", "-u");
1088 break;
1089 case TRD_FLAGS_TYPE_RPC:
1090 ly_print_(out, "%s", "-x");
1091 break;
1092 case TRD_FLAGS_TYPE_NOTIF:
1093 ly_print_(out, "%s", "-n");
1094 break;
1095 case TRD_FLAGS_TYPE_MOUNT_POINT:
1096 ly_print_(out, "%s", "mp");
1097 break;
1098 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001099 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001100 break;
1101 }
1102}
1103
1104/**
1105 * @brief Get size of the \<flags\>.
1106 * @param[in] flags_type is type of \<flags\>.
1107 * @return 0 if flags_type is not set otherwise 2.
1108 */
1109static size_t
1110trp_get_flags_strlen(trt_flags_type flags_type)
1111{
1112 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1113}
1114
1115/**
1116 * @brief Print entire struct trt_node_name structure.
1117 * @param[in] node_name is item to print.
1118 * @param[in,out] out is output handler.
1119 */
1120static void
1121trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1122{
1123 const char *mod_prefix;
1124 const char *colon;
1125 const char trd_node_name_suffix_choice[] = ")";
1126 const char trd_node_name_suffix_case[] = ")";
1127 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1128 const char trd_opts_container[] = "!"; /**< For a presence container. */
1129 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1130 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1131 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1132
1133 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1134 return;
1135 }
1136
1137 if (node_name.module_prefix) {
1138 mod_prefix = node_name.module_prefix;
1139 colon = ":";
1140 } else {
1141 mod_prefix = "";
1142 colon = "";
1143 }
1144
1145 switch (node_name.type) {
1146 case TRD_NODE_ELSE:
1147 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1148 break;
1149 case TRD_NODE_CASE:
1150 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1151 break;
1152 case TRD_NODE_CHOICE:
1153 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1154 break;
1155 case TRD_NODE_OPTIONAL_CHOICE:
1156 ly_print_(out, "%s%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice, trd_opts_optional);
1157 break;
1158 case TRD_NODE_OPTIONAL:
1159 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1160 break;
1161 case TRD_NODE_CONTAINER:
1162 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1163 break;
1164 case TRD_NODE_LISTLEAFLIST:
1165 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1166 break;
1167 case TRD_NODE_KEYS:
1168 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1169 break;
1170 case TRD_NODE_TOP_LEVEL1:
1171 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1172 break;
1173 case TRD_NODE_TOP_LEVEL2:
1174 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1175 break;
1176 case TRD_NODE_TRIPLE_DOT:
1177 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1178 break;
1179 default:
1180 break;
1181 }
1182}
1183
1184/**
aPiecek874ea4d2021-04-19 12:26:36 +02001185 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1186 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001187 * @param[in] node_name is structure containing the 'mark'.
1188 * @return 1 if contain otherwise 0.
1189 */
1190static ly_bool
1191trp_mark_is_used(struct trt_node_name node_name)
1192{
1193 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1194 return 0;
1195 }
1196
1197 switch (node_name.type) {
1198 case TRD_NODE_ELSE:
1199 case TRD_NODE_CASE:
1200 case TRD_NODE_KEYS:
1201 return 0;
1202 default:
1203 return 1;
1204 }
1205}
1206
1207/**
1208 * @brief Print opts keys.
1209 * @param[in] node_name contains type of the node with his name.
1210 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001211 * @param[in] cf is basically a pointer to the function that prints
1212 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001213 * @param[in,out] out is output handler.
1214 */
1215static void
1216trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1217{
1218 if (node_name.type != TRD_NODE_KEYS) {
1219 return;
1220 }
1221
1222 /* <name><mark>___<keys>*/
1223 if (btw_name_opts > 0) {
1224 ly_print_(out, "%*c", btw_name_opts, ' ');
1225 }
1226 ly_print_(out, "[");
1227 cf.pf(cf.ctx, out);
1228 ly_print_(out, "]");
1229}
1230
1231/**
1232 * @brief Print entire struct trt_type structure.
1233 * @param[in] type is item to print.
1234 * @param[in,out] out is output handler.
1235 */
1236static void
1237trp_print_type(struct trt_type type, struct ly_out *out)
1238{
1239 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1240 return;
1241 }
1242
1243 switch (type.type) {
1244 case TRD_TYPE_NAME:
1245 ly_print_(out, "%s", type.str);
1246 break;
1247 case TRD_TYPE_TARGET:
1248 ly_print_(out, "-> %s", type.str);
1249 break;
1250 case TRD_TYPE_LEAFREF:
1251 ly_print_(out, "leafref");
1252 default:
1253 break;
1254 }
1255}
1256
1257/**
1258 * @brief Print all iffeatures of node
1259 *
1260 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001261 * @param[in] cf is basically a pointer to the function that prints
1262 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001263 * @param[in,out] out is output handler.
1264 */
1265static void
1266trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1267{
1268 if (iffeature_flag) {
1269 ly_print_(out, "{");
1270 cf.pf(cf.ctx, out);
1271 ly_print_(out, "}?");
1272 }
1273}
1274
1275/**
1276 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1277 * @param[in] node contains items to print.
1278 * @param[in] out is output handler.
1279 */
1280static void
1281trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1282{
1283 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1284 trp_print_node_name(node.name, out);
1285 return;
1286 }
1287 /* <status>--<flags> */
1288 trp_print_status(node.status, out);
1289 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001290 /* If the node is a case node, there is no space before the <name>
1291 * also case node has no flags.
1292 */
aPiecek61d062b2020-11-02 11:05:09 +01001293 if (node.name.type != TRD_NODE_CASE) {
1294 trp_print_flags(node.flags, out);
1295 ly_print_(out, " ");
1296 }
1297 /* <name> */
1298 trp_print_node_name(node.name, out);
1299}
1300
1301/**
aPiecek874ea4d2021-04-19 12:26:36 +02001302 * @brief Print alignment (spaces) instead of
1303 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001304 * @param[in] node contains items to print.
1305 * @param[in] out is output handler.
1306 */
1307static void
1308trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1309{
1310 uint32_t space = trp_get_flags_strlen(node.flags);
1311
1312 if (node.name.type == TRD_NODE_CASE) {
1313 /* :(<name> */
1314 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1315 } else if (node.name.type == TRD_NODE_CHOICE) {
1316 /* (<name> */
1317 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1318 } else {
1319 /* _<name> */
1320 space += strlen(" ");
1321 }
1322
1323 /* <name>
1324 * __
1325 */
1326 space += TRD_INDENT_LONG_LINE_BREAK;
1327
1328 ly_print_(out, "%*c", space, ' ');
1329}
1330
1331/**
1332 * @brief Print struct trt_node structure.
1333 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001334 * @param[in] pck package of functions for
1335 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001336 * @param[in] indent is the indent in node.
1337 * @param[in,out] out is output handler.
1338 */
1339static void
1340trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1341{
1342 ly_bool triple_dot;
1343 ly_bool divided;
1344 struct trt_cf_print cf_print_keys;
1345 struct trt_cf_print cf_print_iffeatures;
1346
1347 if (trp_node_is_empty(node)) {
1348 return;
1349 }
1350
1351 /* <status>--<flags> <name><opts> <type> <if-features> */
1352 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1353 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1354
1355 if (triple_dot) {
1356 trp_print_node_name(node.name, out);
1357 return;
1358 } else if (!divided) {
1359 trp_print_node_up_to_name(node, out);
1360 } else {
1361 trp_print_divided_node_up_to_name(node, out);
1362 }
1363
1364 /* <opts> */
1365 /* <name>___<opts>*/
1366 cf_print_keys.ctx = pck.tree_ctx;
1367 cf_print_keys.pf = pck.fps.print_keys;
1368
1369 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1370
1371 /* <opts>__<type> */
1372 if (indent.btw_opts_type > 0) {
1373 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1374 }
1375
1376 /* <type> */
1377 trp_print_type(node.type, out);
1378
1379 /* <type>__<iffeatures> */
1380 if (indent.btw_type_iffeatures > 0) {
1381 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1382 }
1383
1384 /* <iffeatures> */
1385 cf_print_iffeatures.ctx = pck.tree_ctx;
1386 cf_print_iffeatures.pf = pck.fps.print_features_names;
1387
1388 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1389}
1390
1391/**
aPiecek874ea4d2021-04-19 12:26:36 +02001392 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001393 * @param[in] ks is keyword statement to print.
1394 * @param[in,out] out is output handler
1395 */
1396static void
1397trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1398{
1399 switch (ks.type) {
1400 case TRD_KEYWORD_MODULE:
1401 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1402 return;
1403 case TRD_KEYWORD_SUBMODULE:
1404 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1405 return;
1406 default:
1407 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1408 switch (ks.type) {
1409 case TRD_KEYWORD_AUGMENT:
1410 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1411 break;
1412 case TRD_KEYWORD_RPC:
1413 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1414 break;
1415 case TRD_KEYWORD_NOTIF:
1416 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1417 break;
1418 case TRD_KEYWORD_GROUPING:
1419 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1420 break;
1421 case TRD_KEYWORD_YANG_DATA:
1422 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1423 break;
1424 default:
1425 break;
1426 }
1427 break;
1428 }
1429}
1430
1431/**
1432 * @brief Get string length of stored keyword.
1433 * @param[in] type is type of the keyword statement.
1434 * @return length of the keyword statement name.
1435 */
1436static size_t
1437trp_keyword_type_strlen(trt_keyword_type type)
1438{
1439 switch (type) {
1440 case TRD_KEYWORD_MODULE:
1441 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1442 case TRD_KEYWORD_SUBMODULE:
1443 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1444 case TRD_KEYWORD_AUGMENT:
1445 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1446 case TRD_KEYWORD_RPC:
1447 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1448 case TRD_KEYWORD_NOTIF:
1449 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1450 case TRD_KEYWORD_GROUPING:
1451 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1452 case TRD_KEYWORD_YANG_DATA:
1453 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1454 default:
1455 return 0;
1456 }
1457}
1458
1459/**
aPiecek874ea4d2021-04-19 12:26:36 +02001460 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001461 * @param[in] ks is keyword statement structure.
1462 * @param[in] mll is max line length.
1463 * @param[in,out] out is output handler.
1464 */
1465static void
1466trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1467{
1468 uint32_t ind_initial;
1469 uint32_t ind_divided;
1470 /* flag if path must be splitted to more lines */
1471 ly_bool linebreak_was_set;
1472 /* flag if at least one subpath was printed */
1473 ly_bool subpath_printed;
1474 /* the sum of the sizes of the substrings on the current line */
1475 uint32_t how_far;
1476 /* pointer to start of the subpath */
1477 const char *sub_ptr;
1478 /* size of subpath from sub_ptr */
1479 size_t sub_len;
1480
1481 if ((!ks.str) || (ks.str[0] == '\0')) {
1482 return;
1483 }
1484
1485 /* module name cannot be splitted */
1486 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1487 ly_print_(out, "%s", ks.str);
1488 return;
1489 }
1490
1491 /* after -> for trd_keyword_stmt_body do */
1492
1493 /* set begin indentation */
1494 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1495 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1496 linebreak_was_set = 0;
1497 subpath_printed = 0;
1498 how_far = 0;
1499 sub_ptr = ks.str;
1500 sub_len = 0;
1501
1502 while (sub_ptr[0] != '\0') {
1503 uint32_t ind;
1504 /* skip slash */
1505 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
Michal Vasko26bbb272022-08-02 14:54:33 +02001506
aPiecek61d062b2020-11-02 11:05:09 +01001507 /* get position of the end of substr */
1508 tmp = strchr(tmp, '/');
1509 /* set correct size if this is a last substring */
1510 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1511 /* actualize sum of the substring's sizes on the current line */
1512 how_far += sub_len;
1513 /* correction due to colon character if it this is last substring */
1514 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1515 /* choose indentation which depends on
1516 * whether the string is printed on multiple lines or not
1517 */
1518 ind = linebreak_was_set ? ind_divided : ind_initial;
1519 if (ind + how_far <= mll) {
1520 /* printing before max line length */
1521 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1522 subpath_printed = 1;
1523 } else {
1524 /* printing on new line */
1525 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001526 /* first subpath is too long
1527 * but print it at first line anyway
1528 */
aPiecek61d062b2020-11-02 11:05:09 +01001529 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1530 subpath_printed = 1;
1531 continue;
1532 }
1533 ly_print_(out, "\n");
1534 ly_print_(out, "%*c", ind_divided, ' ');
1535 linebreak_was_set = 1;
1536 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1537 how_far = sub_len;
1538 subpath_printed = 1;
1539 }
1540 }
1541}
1542
1543/**
aPiecek874ea4d2021-04-19 12:26:36 +02001544 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001545 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001546 * @param[in] grp_has_data is flag only for grouping section.
1547 * Set to 1 if grouping section has some nodes.
1548 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001549 * @param[in,out] out is output handler.
1550 */
1551static void
aPiecekdc8fd572021-04-19 10:47:23 +02001552trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001553{
1554 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001555 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1556 return;
1557 } else {
1558 ly_print_(out, ":");
1559 }
aPiecek61d062b2020-11-02 11:05:09 +01001560 }
1561}
1562
1563/**
1564 * @brief Print entire struct trt_keyword_stmt structure.
1565 * @param[in] ks is item to print.
1566 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001567 * @param[in] grp_has_data is flag only for grouping section.
1568 * Set to 1 if grouping section has some nodes.
1569 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001570 * @param[in,out] out is output handler.
1571 */
1572static void
aPiecek874ea4d2021-04-19 12:26:36 +02001573trp_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 +01001574{
1575 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1576 return;
1577 }
1578 trt_print_keyword_stmt_begin(ks, out);
1579 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001580 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001581}
1582
aPiecek874ea4d2021-04-19 12:26:36 +02001583/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001584 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001585 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001586
1587/**
aPiecek874ea4d2021-04-19 12:26:36 +02001588 * @brief Printing one line including wrapper and node
1589 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001590 * @param[in] node is \<node\> representation.
1591 * @param[in] pck contains special printing functions callback.
1592 * @param[in] indent contains wrapper and indent in node numbers.
1593 * @param[in,out] out is output handler.
1594 */
1595static void
1596trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1597{
1598 trp_print_wrapper(indent.wrapper, out);
1599 trp_print_node(node, pck, indent.in_node, out);
1600}
1601
1602/**
aPiecek874ea4d2021-04-19 12:26:36 +02001603 * @brief Printing one line including wrapper and
1604 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001605 * @param[in] node is \<node\> representation.
1606 * @param[in] wr is wrapper for printing indentation before node.
1607 * @param[in] out is output handler.
1608 */
1609static void
1610trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1611{
1612 trp_print_wrapper(wr, out);
1613 trp_print_node_up_to_name(node, out);
1614}
1615
1616/**
aPiecek874ea4d2021-04-19 12:26:36 +02001617 * @brief Check if leafref target must be change to string 'leafref'
1618 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001619 * @param[in] node containing leafref target.
1620 * @param[in] wr is wrapper for printing indentation before node.
1621 * @param[in] mll is max line length.
1622 * @param[in] out is output handler.
1623 * @return true if leafref must be changed to string 'leafref'.
1624 */
1625static ly_bool
1626trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1627{
1628 struct ly_out_clb_arg *data;
1629
1630 if (node.type.type != TRD_TYPE_TARGET) {
1631 return 0;
1632 }
1633
1634 /* set ly_out to counting characters */
1635 data = out->method.clb.arg;
1636
1637 data->counter = 0;
1638 data->mode = TRD_CHAR_COUNT;
1639 /* count number of printed bytes */
1640 trp_print_wrapper(wr, out);
1641 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1642 trp_print_divided_node_up_to_name(node, out);
1643 data->mode = TRD_PRINT;
1644
1645 return data->counter + strlen(node.type.str) > mll;
1646}
1647
1648/**
1649 * @brief Get default indent in node based on node values.
1650 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001651 * @return Default indent in node assuming that the node
1652 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001653 */
1654static struct trt_indent_in_node
1655trp_default_indent_in_node(struct trt_node node)
1656{
1657 struct trt_indent_in_node ret;
1658
1659 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1660
1661 /* btw_name_opts */
1662 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1663
1664 /* btw_opts_type */
1665 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1666 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1667 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1668 TRD_INDENT_BEFORE_TYPE;
1669 } else {
1670 ret.btw_opts_type = 0;
1671 }
1672
1673 /* btw_type_iffeatures */
1674 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1675
1676 return ret;
1677}
1678
1679/**
1680 * @brief Setting linebreaks in trt_indent_in_node.
1681 *
1682 * The order where the linebreak tag can be placed is from the end.
1683 *
aPiecek874ea4d2021-04-19 12:26:36 +02001684 * @param[in] indent containing alignment lengths
1685 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001686 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001687 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1688 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001689 */
1690static struct trt_indent_in_node
1691trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1692{
1693 /* somewhere must be set a line break in node */
1694 struct trt_indent_in_node ret = indent;
1695
1696 /* gradually break the node from the end */
1697 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1698 ret.btw_type_iffeatures = TRD_LINEBREAK;
1699 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1700 ret.btw_opts_type = TRD_LINEBREAK;
1701 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1702 /* set line break between name and opts */
1703 ret.btw_name_opts = TRD_LINEBREAK;
1704 } else {
1705 /* it is not possible to place a more line breaks,
1706 * unfortunately the max_line_length constraint is violated
1707 */
1708 ret.type = TRD_INDENT_IN_NODE_FAILED;
1709 }
1710 return ret;
1711}
1712
1713/**
1714 * @brief Get the first half of the node based on the linebreak mark.
1715 *
1716 * Items in the second half of the node will be empty.
1717 *
1718 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001719 * @param[in] indent contains information in which part of the \<node\>
1720 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001721 * @return first half of the node, indent is unchanged.
1722 */
1723static struct trt_pair_indent_node
1724trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1725{
1726 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1727
1728 if (indent.btw_name_opts == TRD_LINEBREAK) {
1729 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1730 ret.node.type = TRP_EMPTY_TRT_TYPE;
1731 ret.node.iffeatures = 0;
1732 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1733 ret.node.type = TRP_EMPTY_TRT_TYPE;
1734 ret.node.iffeatures = 0;
1735 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1736 ret.node.iffeatures = 0;
1737 }
1738
1739 return ret;
1740}
1741
1742/**
1743 * @brief Get the second half of the node based on the linebreak mark.
1744 *
1745 * Items in the first half of the node will be empty.
1746 * Indentations belonging to the first node will be reset to zero.
1747 *
1748 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001749 * @param[in] indent contains information in which part of the \<node\>
1750 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001751 * @return second half of the node, indent is newly set.
1752 */
1753static struct trt_pair_indent_node
1754trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1755{
1756 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1757
1758 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001759 /* Logically, the information up to token <opts> should
1760 * be deleted, but the the trp_print_node function needs it to
1761 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001762 */
1763 ret.indent.btw_name_opts = 0;
1764 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1765 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1766 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1767 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1768 ret.indent.btw_name_opts = 0;
1769 ret.indent.btw_opts_type = 0;
1770 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1771 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1772 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1773 ret.node.type = TRP_EMPTY_TRT_TYPE;
1774 ret.indent.btw_name_opts = 0;
1775 ret.indent.btw_opts_type = 0;
1776 ret.indent.btw_type_iffeatures = 0;
1777 }
1778 return ret;
1779}
1780
1781/**
1782 * @brief Get the correct alignment for the node.
1783 *
aPiecek874ea4d2021-04-19 12:26:36 +02001784 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001785 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001786 *
1787 * @param[in] node is \<node\> representation.
1788 * @param[in] pck contains speciall callback functions for printing.
1789 * @param[in] indent contains wrapper and indent in node numbers.
1790 * @param[in] mll is max line length.
1791 * @param[in,out] cnt counting number of characters to print.
1792 * @param[in,out] out is output handler.
1793 * @return pair of node and indentation numbers of that node.
1794 */
1795static struct trt_pair_indent_node
1796trp_try_normal_indent_in_node_(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, size_t *cnt, struct ly_out *out)
1797{
1798 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1799
1800 trp_print_line(node, pck, indent, out);
1801
1802 if (*cnt <= mll) {
1803 /* success */
1804 return ret;
1805 } else {
1806 ret.indent = trp_indent_in_node_place_break(ret.indent);
1807 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1808 /* erase information in node due to line break */
1809 ret = trp_first_half_node(node, ret.indent);
1810 /* check if line fits, recursive call */
1811 *cnt = 0;
1812 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1813 /* make sure that the result will be with the status divided
1814 * or eventually with status failed */
1815 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1816 }
1817 return ret;
1818 }
1819}
1820
1821/**
1822 * @brief Get the correct alignment for the node.
1823 *
1824 * @param[in] node is \<node\> representation.
1825 * @param[in] pck contains speciall callback functions for printing.
1826 * @param[in] indent contains wrapper and indent in node numbers.
1827 * @param[in] mll is max line length.
1828 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001829 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1830 * line, some indent variable has negative value as a line break sign.
1831 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1832 * all indent variables values has non-negative number.
1833 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1834 * line, all indent variables has negative or zero values,
1835 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001836 */
1837static struct trt_pair_indent_node
1838trp_try_normal_indent_in_node(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, struct ly_out *out)
1839{
1840 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1841 struct ly_out_clb_arg *data;
1842
1843 /* set ly_out to counting characters */
1844 data = out->method.clb.arg;
1845
1846 data->counter = 0;
1847 data->mode = TRD_CHAR_COUNT;
1848 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1849 data->mode = TRD_PRINT;
1850
1851 return ret;
1852}
1853
1854/**
aPiecek01598c02021-04-23 14:18:24 +02001855 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001856 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001857 * @param[in] node is node representation.
1858 * @param[in] ppck contains speciall callback functions for printing.
1859 * @param[in] ipck contains wrapper and indent in node numbers.
1860 * @param[in] mll is max line length.
1861 * @param[in,out] out is output handler.
1862 */
1863static void
1864trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1865{
1866 ly_bool entire_node_was_printed;
1867 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1868
1869 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1870 /* nothing can be done, continue as usual */
1871 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1872 }
1873
1874 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1875 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1876
1877 if (!entire_node_was_printed) {
1878 ly_print_(out, "\n");
1879 /* continue with second half node */
1880 ind_node = trp_second_half_node(node, ind_node.indent);
1881 /* continue with printing node */
1882 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1883 } else {
1884 return;
1885 }
1886}
1887
1888/**
aPiecek874ea4d2021-04-19 12:26:36 +02001889 * @brief Printing of the wrapper and the whole node,
1890 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001891 * @param[in] node is node representation.
1892 * @param[in] ppck contains speciall callback functions for printing.
1893 * @param[in] ipck contains wrapper and indent in node numbers.
1894 * @param[in] mll is max line length.
1895 * @param[in,out] out is output handler.
1896 */
1897static void
1898trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1899{
1900 struct trt_pair_indent_node ind_node1;
1901 struct trt_pair_indent_node ind_node2;
1902 struct trt_pck_indent tmp;
1903
1904 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1905 node.type.type = TRD_TYPE_LEAFREF;
1906 }
1907
1908 /* check if normal indent is possible */
1909 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1910
1911 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1912 /* node fits to one line */
1913 trp_print_line(node, ppck, ipck, out);
1914 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1915 /* node will be divided */
1916 /* print first half */
1917 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1918 /* pretend that this is normal node */
1919 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1920
1921 trp_print_line(ind_node1.node, ppck, tmp, out);
1922 ly_print_(out, "\n");
1923
1924 /* continue with second half on new line */
1925 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1926 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1927
1928 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1929 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1930 /* node name is too long */
1931 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1932
1933 if (trp_node_body_is_empty(node)) {
1934 return;
1935 } else {
1936 ly_print_(out, "\n");
1937
1938 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1939 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1940 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1941
1942 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1943 }
1944
1945 }
1946}
1947
aPiecek874ea4d2021-04-19 12:26:36 +02001948/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001949 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001950 *********************************************************************/
1951
1952/**
1953 * @brief Get nodetype.
1954 * @param[in] node is any lysp_node.
1955 */
1956static uint16_t
1957trop_nodetype(const void *node)
1958{
1959 return ((const struct lysp_node *)node)->nodetype;
1960}
1961
1962/**
1963 * @brief Get sibling.
1964 * @param[in] node is any lysp_node.
1965 */
1966static const void *
1967trop_next(const void *node)
1968{
1969 return ((const struct lysp_node *)node)->next;
1970}
1971
1972/**
1973 * @brief Get parent.
1974 * @param[in] node is any lysp_node.
1975 */
1976static const void *
1977trop_parent(const void *node)
1978{
1979 return ((const struct lysp_node *)node)->parent;
1980}
1981
1982/**
1983 * @brief Try to get child.
1984 * @param[in] node is any lysp_node.
1985 */
1986static const void *
1987trop_child(const void *node)
1988{
1989 return lysp_node_child(node);
1990}
1991
1992/**
1993 * @brief Try to get action.
1994 * @param[in] node is any lysp_node.
1995 */
1996static const void *
1997trop_actions(const void *node)
1998{
1999 return lysp_node_actions(node);
2000}
2001
2002/**
2003 * @brief Try to get action.
2004 * @param[in] node must be of type lysp_node_action.
2005 */
2006static const void *
2007trop_action_input(const void *node)
2008{
2009 return &((const struct lysp_node_action *)node)->input;
2010}
2011
2012/**
2013 * @brief Try to get action.
2014 * @param[in] node must be of type lysp_node_action.
2015 */
2016static const void *
2017trop_action_output(const void *node)
2018{
2019 return &((const struct lysp_node_action *)node)->output;
2020}
2021
2022/**
2023 * @brief Try to get action.
2024 * @param[in] node is any lysp_node.
2025 */
2026static const void *
2027trop_notifs(const void *node)
2028{
2029 return lysp_node_notifs(node);
2030}
2031
2032/**
aPiecek01598c02021-04-23 14:18:24 +02002033 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02002034 * which are adapted to lysp nodes.
2035 */
2036static struct tro_getters
2037trop_init_getters()
2038{
2039 return (struct tro_getters) {
2040 .nodetype = trop_nodetype,
2041 .next = trop_next,
2042 .parent = trop_parent,
2043 .child = trop_child,
2044 .actions = trop_actions,
2045 .action_input = trop_action_input,
2046 .action_output = trop_action_output,
2047 .notifs = trop_notifs
2048 };
2049}
2050
aPiecek3f247652021-04-19 13:40:25 +02002051/**
2052 * @brief Get nodetype.
2053 * @param[in] node is any lysc_node.
2054 */
2055static uint16_t
2056troc_nodetype(const void *node)
2057{
2058 return ((const struct lysc_node *)node)->nodetype;
2059}
2060
2061/**
2062 * @brief Get sibling.
2063 * @param[in] node is any lysc_node.
2064 */
2065static const void *
2066troc_next(const void *node)
2067{
2068 return ((const struct lysc_node *)node)->next;
2069}
2070
2071/**
2072 * @brief Get parent.
2073 * @param[in] node is any lysc_node.
2074 */
2075static const void *
2076troc_parent(const void *node)
2077{
2078 return ((const struct lysc_node *)node)->parent;
2079}
2080
2081/**
2082 * @brief Try to get child.
2083 * @param[in] node is any lysc_node.
2084 */
2085static const void *
2086troc_child(const void *node)
2087{
2088 return lysc_node_child(node);
2089}
2090
2091/**
2092 * @brief Try to get action.
2093 * @param[in] node is any lysc_node.
2094 */
2095static const void *
2096troc_actions(const void *node)
2097{
2098 return lysc_node_actions(node);
2099}
2100
2101/**
2102 * @brief Try to get action.
2103 * @param[in] node must be of type lysc_node_action.
2104 */
2105static const void *
2106troc_action_input(const void *node)
2107{
2108 return &((const struct lysc_node_action *)node)->input;
2109}
2110
2111/**
2112 * @brief Try to get action.
2113 * @param[in] node must be of type lysc_node_action.
2114 */
2115static const void *
2116troc_action_output(const void *node)
2117{
2118 return &((const struct lysc_node_action *)node)->output;
2119}
2120
2121/**
2122 * @brief Try to get action.
2123 * @param[in] node is any lysc_node.
2124 */
2125static const void *
2126troc_notifs(const void *node)
2127{
2128 return lysc_node_notifs(node);
2129}
2130
2131/**
aPiecek01598c02021-04-23 14:18:24 +02002132 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002133 * which are adapted to lysc nodes.
2134 */
2135static struct tro_getters
2136troc_init_getters()
2137{
2138 return (struct tro_getters) {
2139 .nodetype = troc_nodetype,
2140 .next = troc_next,
2141 .parent = troc_parent,
2142 .child = troc_child,
2143 .actions = troc_actions,
2144 .action_input = troc_action_input,
2145 .action_output = troc_action_output,
2146 .notifs = troc_notifs
2147 };
2148}
2149
aPiecekef1e58e2021-04-19 13:19:44 +02002150/**********************************************************************
2151 * tro functions
2152 *********************************************************************/
2153
2154/**
2155 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002156 *
2157 * This is a general algorithm that is able to
2158 * work with lysp_node or lysc_node.
2159 *
2160 * @param[in] node points to lysp_node or lysc_node.
2161 * @param[in] lysc_tree flag to determine what type the @p node is.
2162 * If set to true, then @p points to lysc_node otherwise lysp_node.
2163 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002164 */
2165static const void *
aPiecek3f247652021-04-19 13:40:25 +02002166tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002167{
2168 struct tro_getters get;
2169 const void *tmp, *parent;
2170 const void *ret;
2171
2172 assert(node);
2173
aPiecek3f247652021-04-19 13:40:25 +02002174 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002175
2176 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2177 if ((tmp = get.next(node))) {
2178 /* next action exists */
2179 ret = tmp;
2180 } else if ((parent = get.parent(node))) {
2181 /* maybe if notif exists as sibling */
2182 ret = get.notifs(parent);
2183 } else {
2184 ret = NULL;
2185 }
2186 } else if (get.nodetype(node) & LYS_INPUT) {
2187 if ((parent = get.parent(node))) {
2188 /* if output action has data */
2189 if (get.child(get.action_output(parent))) {
2190 /* then next sibling is output action */
2191 ret = get.action_output(parent);
2192 } else {
2193 /* input action cannot have siblings other
2194 * than output action.
2195 */
2196 ret = NULL;
2197 }
2198 } else {
2199 /* there is no way how to get output action */
2200 ret = NULL;
2201 }
2202 } else if (get.nodetype(node) & LYS_OUTPUT) {
2203 /* output action cannot have siblings */
2204 ret = NULL;
2205 } else if (get.nodetype(node) & LYS_NOTIF) {
2206 /* must have as a sibling only notif */
2207 ret = get.next(node);
2208 } else {
2209 /* for rest of nodes */
2210 if ((tmp = get.next(node))) {
2211 /* some sibling exists */
2212 ret = tmp;
2213 } else if ((parent = get.parent(node))) {
2214 /* Action and notif are siblings too.
2215 * They can be reached through parent.
2216 */
2217 if ((tmp = get.actions(parent))) {
2218 /* next sibling is action */
2219 ret = tmp;
2220 } else if ((tmp = get.notifs(parent))) {
2221 /* next sibling is notif */
2222 ret = tmp;
2223 } else {
2224 /* sibling not exists */
2225 ret = NULL;
2226 }
2227 } else {
2228 /* sibling not exists */
2229 ret = NULL;
2230 }
2231 }
2232
2233 return ret;
2234}
2235
2236/**
2237 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002238 *
2239 * This is a general algorithm that is able to
2240 * work with lysp_node or lysc_node.
2241 *
2242 * @param[in] node points to lysp_node or lysc_node.
2243 * @param[in] lysc_tree flag to determine what type the @p node is.
2244 * If set to true, then @p points to lysc_node otherwise lysp_node.
2245 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002246 */
2247static const void *
aPiecek3f247652021-04-19 13:40:25 +02002248tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002249{
2250 struct tro_getters get;
2251 const void *tmp;
2252 const void *ret;
2253
2254 assert(node);
2255
aPiecek3f247652021-04-19 13:40:25 +02002256 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002257
2258 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2259 if (get.child(get.action_input(node))) {
2260 /* go to LYS_INPUT */
2261 ret = get.action_input(node);
2262 } else if (get.child(get.action_output(node))) {
2263 /* go to LYS_OUTPUT */
2264 ret = get.action_output(node);
2265 } else {
2266 /* input action and output action have no data */
2267 ret = NULL;
2268 }
2269 } else {
2270 if ((tmp = get.child(node))) {
2271 ret = tmp;
2272 } else {
2273 /* current node can't have children or has no children */
2274 /* but maybe has some actions or notifs */
2275 if ((tmp = get.actions(node))) {
2276 ret = tmp;
2277 } else if ((tmp = get.notifs(node))) {
2278 ret = tmp;
2279 } else {
2280 ret = NULL;
2281 }
2282 }
2283 }
2284
2285 return ret;
2286}
2287
2288/**
aPiecek3f247652021-04-19 13:40:25 +02002289 * @brief Get new trt_parent_cache if we apply the transfer
2290 * to the child node in the tree.
2291 * @param[in] ca is parent cache for current node.
2292 * @param[in] tc contains current tree node.
2293 * @return Cache for the current node.
2294 */
2295static struct trt_parent_cache
2296tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2297{
2298 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2299
2300 if (!tc->lysc_tree) {
2301 const struct lysp_node *pn = tc->pn;
2302
2303 ret.ancestor =
2304 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2305 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2306 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2307 ca.ancestor;
2308
2309 ret.lys_status =
2310 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2311 ca.lys_status;
2312
2313 ret.lys_config =
2314 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2315 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2316 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2317 ca.lys_config;
2318
2319 ret.last_list =
2320 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2321 ca.last_list;
2322 }
2323
2324 return ret;
2325}
2326
2327/**
aPiecekef1e58e2021-04-19 13:19:44 +02002328 * @brief Transformation of the Schema nodes flags to
2329 * Tree diagram \<status\>.
2330 * @param[in] flags is node's flags obtained from the tree.
2331 */
2332static trt_status_type
2333tro_flags2status(uint16_t flags)
2334{
2335 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2336 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2337 TRD_STATUS_TYPE_CURRENT;
2338}
2339
2340/**
2341 * @brief Transformation of the Schema nodes flags to Tree diagram
2342 * \<flags\> but more specifically 'ro' or 'rw'.
2343 * @param[in] flags is node's flags obtained from the tree.
2344 */
2345static trt_flags_type
2346tro_flags2config(uint16_t flags)
2347{
2348 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2349 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2350 TRD_FLAGS_TYPE_EMPTY;
2351}
2352
2353/**
aPiecek3f247652021-04-19 13:40:25 +02002354 * @brief Print current node's iffeatures.
2355 * @param[in] tc is tree context.
2356 * @param[in,out] out is output handler.
2357 */
2358static void
2359tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2360{
2361 const struct lysp_qname *iffs;
2362
aPiecekbbc02932021-05-21 07:19:41 +02002363 if (tc->lysc_tree) {
2364 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2365 iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
2366 } else {
2367 iffs = tc->pn->iffeatures;
2368 }
aPiecek3f247652021-04-19 13:40:25 +02002369 LY_ARRAY_COUNT_TYPE i;
2370
2371 LY_ARRAY_FOR(iffs, i) {
2372 if (i == 0) {
2373 ly_print_(out, "%s", iffs[i].str);
2374 } else {
2375 ly_print_(out, ",%s", iffs[i].str);
2376 }
2377 }
2378
2379}
2380
2381/**
2382 * @brief Print current list's keys.
2383 *
2384 * Well, actually printing keys in the lysp_tree is trivial,
2385 * because char* points to all keys. However, special functions have
2386 * been reserved for this, because in principle the list of elements
2387 * can have more implementations.
2388 *
2389 * @param[in] tc is tree context.
2390 * @param[in,out] out is output handler.
2391 */
2392static void
2393tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2394{
2395 const struct lysp_node_list *list;
2396
aPiecekbbc02932021-05-21 07:19:41 +02002397 if (tc->lysc_tree) {
2398 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2399 list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
2400 } else {
2401 list = (const struct lysp_node_list *)tc->pn;
2402 }
aPiecek3f247652021-04-19 13:40:25 +02002403 assert(list->nodetype & LYS_LIST);
2404
2405 if (trg_charptr_has_data(list->key)) {
2406 ly_print_(out, "%s", list->key);
2407 }
2408}
2409
2410/**
2411 * @brief Get rpcs section if exists.
2412 * @param[in,out] tc is tree context.
2413 * @return Section representation if it exists. The @p tc is modified
2414 * and his pointer points to the first node in rpcs section.
2415 * @return Empty section representation otherwise.
2416 */
2417static struct trt_keyword_stmt
2418tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2419{
aPiecek9f792e52021-04-21 08:33:56 +02002420 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002421 const void *actions;
2422
2423 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002424 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002425 if (actions) {
2426 tc->cn = actions;
2427 }
2428 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002429 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002430 if (actions) {
2431 tc->pn = actions;
2432 tc->tpn = tc->pn;
2433 }
2434 }
2435
2436 if (actions) {
2437 tc->section = TRD_SECT_RPCS;
2438 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2439 } else {
2440 return TRP_EMPTY_KEYWORD_STMT;
2441 }
2442}
2443
2444/**
2445 * @brief Get notification section if exists
2446 * @param[in,out] tc is tree context.
2447 * @return Section representation if it exists.
2448 * The @p tc is modified and his pointer points to the
2449 * first node in notification section.
2450 * @return Empty section representation otherwise.
2451 */
2452static struct trt_keyword_stmt
2453tro_modi_get_notifications(struct trt_tree_ctx *tc)
2454{
aPiecek9f792e52021-04-21 08:33:56 +02002455 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002456 const void *notifs;
2457
2458 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002459 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002460 if (notifs) {
2461 tc->cn = notifs;
2462 }
2463 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002464 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002465 if (notifs) {
2466 tc->pn = notifs;
2467 tc->tpn = tc->pn;
2468 }
2469 }
2470
2471 if (notifs) {
2472 tc->section = TRD_SECT_NOTIF;
2473 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2474 } else {
2475 return TRP_EMPTY_KEYWORD_STMT;
2476 }
2477}
2478
2479/**
aPiecek96baa7f2021-04-23 12:32:00 +02002480 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002481 *
2482 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002483 * @param[in] u is index to the array of extensions (lysc_ext_instance
2484 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002485 * @return Section representation if it exists.
2486 * @return Empty section representation otherwise.
2487 */
2488static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002489tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002490{
aPiecek96baa7f2021-04-23 12:32:00 +02002491 assert(tc);
2492 const void *node;
2493 const char *yang_data_name;
2494
2495 if (tc->lysc_tree) {
2496 struct lysc_ext_instance *exts;
2497 struct lysc_ext_substmt *substmts;
2498
2499 exts = tc->cmod->exts;
2500 substmts = exts[u].substmts;
2501 if (!substmts) {
2502 return TRP_EMPTY_KEYWORD_STMT;
2503 }
2504 node = *(const struct lysc_node **)substmts->storage;
2505 yang_data_name = exts[u].argument;
2506 } else {
2507 struct lysp_ext_instance *exts;
2508
2509 exts = tc->pmod->exts;
2510 node = exts[u].parsed;
2511 yang_data_name = exts[u].argument;
2512 }
2513
2514 if (tc->lysc_tree) {
2515 tc->cn = node;
2516 } else {
2517 tc->tpn_ext = &tc->pmod->exts[u];
2518 tc->pn = node;
2519 }
2520
2521 if (node) {
2522 tc->section = TRD_SECT_YANG_DATA;
2523 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2524 } else {
2525 return TRP_EMPTY_KEYWORD_STMT;
2526 }
aPiecekef1e58e2021-04-19 13:19:44 +02002527}
2528
2529/**
2530 * @brief Get name of the module.
2531 * @param[in] tc is context of the tree.
2532 */
2533static struct trt_keyword_stmt
2534tro_read_module_name(const struct trt_tree_ctx *tc)
2535{
aPiecek9f792e52021-04-21 08:33:56 +02002536 assert(tc);
2537
2538 struct trt_keyword_stmt ret;
2539
2540 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2541 TRD_KEYWORD_SUBMODULE :
2542 TRD_KEYWORD_MODULE;
2543
2544 ret.str = !tc->lysc_tree ?
2545 LYSP_MODULE_NAME(tc->pmod) :
2546 tc->cmod->mod->name;
2547
2548 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002549}
2550
aPiecekb8d5a0a2021-05-20 08:20:24 +02002551/**
2552 * @brief Create implicit "case" node as parent of @p node.
2553 * @param[in] node child of implicit case node.
2554 * @return The case node ready to print.
2555 */
2556static struct trt_node
2557tro_create_implicit_case_node(struct trt_node node)
2558{
2559 struct trt_node ret;
2560
2561 ret.status = node.status;
2562 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2563 ret.name.type = TRD_NODE_CASE;
2564 ret.name.module_prefix = node.name.module_prefix;
2565 ret.name.str = node.name.str;
2566 ret.type = TRP_EMPTY_TRT_TYPE;
aPiecek7a28e2f2021-05-21 07:27:03 +02002567 ret.iffeatures = 0;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002568 ret.last_one = node.last_one;
aPiecek50b64e42022-10-10 10:00:12 +02002569 ret.mount = NULL;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002570
2571 return ret;
2572}
2573
aPiecekef1e58e2021-04-19 13:19:44 +02002574/**********************************************************************
2575 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002576 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002577
2578/**
aPiecek61d062b2020-11-02 11:05:09 +01002579 * @brief Check if list statement has keys.
2580 * @param[in] pn is pointer to the list.
2581 * @return 1 if has keys, otherwise 0.
2582 */
2583static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002584trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002585{
aPiecekef1e58e2021-04-19 13:19:44 +02002586 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002587}
2588
2589/**
2590 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002591 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002592 * @return 1 if has if-features, otherwise 0.
2593 */
2594static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002595trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002596{
2597 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002598 const struct lysp_qname *iffs;
2599
aPiecek61d062b2020-11-02 11:05:09 +01002600 ly_bool ret = 0;
2601
aPiecekef1e58e2021-04-19 13:19:44 +02002602 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002603 LY_ARRAY_FOR(iffs, u) {
2604 ret = 1;
2605 break;
2606 }
2607 return ret;
2608}
2609
2610/**
2611 * @brief Find out if leaf is also the key in last list.
2612 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002613 * @param[in] ca_last_list is pointer to last visited list.
2614 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002615 * @return 1 if leaf is also the key, otherwise 0.
2616 */
2617static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002618trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002619{
2620 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2621 const struct lysp_node_list *list = ca_last_list;
2622
2623 if (!list) {
2624 return 0;
2625 }
2626 return trg_charptr_has_data(list->key) ?
2627 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2628}
2629
2630/**
2631 * @brief Check if container's type is presence.
2632 * @param[in] pn is pointer to container.
2633 * @return 1 if container has presence statement, otherwise 0.
2634 */
2635static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002636trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002637{
2638 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2639}
2640
2641/**
ekinzie0ab8b302022-10-10 03:03:57 -04002642 * @brief Check if container has yangmt:mount-point extension.
2643 * @param[in] cn is pointer to container or list.
2644 * @param[out] mount is assigned a pointer to the extension instance, if found
2645 * @return 1 if container has mount-point.
2646 */
2647static ly_bool
2648troc_node_has_mount(const struct lysc_node *cn, struct lysc_ext_instance **mount)
2649{
2650 struct lysc_ext_instance *ext;
2651 uint64_t u;
2652
2653 /* The schema-mount extension plugin has already made sure that
2654 * there is only one mount-point here.
2655 */
2656 LY_ARRAY_FOR(cn->exts, u) {
2657 ext = &cn->exts[u];
2658 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") ||
2659 strcmp(ext->def->name, "mount-point")) {
2660 continue;
2661 }
2662 *mount = ext;
2663 return 1;
2664 }
2665 return 0;
2666}
2667
2668static ly_bool
2669trop_node_has_mount(const struct lysp_node *pn)
2670{
2671 struct lysp_ext_instance *ext;
2672 uint64_t u;
2673
2674 LY_ARRAY_FOR(pn->exts, u) {
2675 ext = &pn->exts[u];
2676 if (strcmp(ext->name, "yangmnt:mount-point")) {
2677 continue;
2678 }
2679 return 1;
2680 }
2681 return 0;
2682}
2683
2684/**
aPiecek61d062b2020-11-02 11:05:09 +01002685 * @brief Get leaflist's path without lysp_node type control.
2686 * @param[in] pn is pointer to the leaflist.
2687 */
2688static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002689trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002690{
2691 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2692
2693 return list->type.path ? list->type.path->expr : NULL;
2694}
2695
2696/**
2697 * @brief Get leaflist's type name without lysp_node type control.
2698 * @param[in] pn is pointer to the leaflist.
2699 */
2700static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002701trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002702{
2703 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2704
2705 return list->type.name;
2706}
2707
2708/**
2709 * @brief Get leaf's path without lysp_node type control.
2710 * @param[in] pn is pointer to the leaf node.
2711 */
2712static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002713trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002714{
2715 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2716
2717 return leaf->type.path ? leaf->type.path->expr : NULL;
2718}
2719
2720/**
2721 * @brief Get leaf's type name without lysp_node type control.
2722 * @param[in] pn is pointer to the leaf's type name.
2723 */
2724static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002725trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002726{
2727 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2728
2729 return leaf->type.name;
2730}
2731
2732/**
aPiecek874ea4d2021-04-19 12:26:36 +02002733 * @brief Get pointer to data using node type specification
2734 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002735 *
aPiecek874ea4d2021-04-19 12:26:36 +02002736 * @param[in] flags is node type specification.
2737 * If it is the correct node, the getter function is called.
2738 * @param[in] f is getter function which provides the desired
2739 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002740 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002741 * @return NULL if node has wrong type or getter function return
2742 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002743 * @return Pointer to desired char pointer obtained from the node.
2744 */
2745static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002746trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002747{
2748 if (pn->nodetype & flags) {
2749 const char *ret = f(pn);
Michal Vasko26bbb272022-08-02 14:54:33 +02002750
aPiecek61d062b2020-11-02 11:05:09 +01002751 return trg_charptr_has_data(ret) ? ret : NULL;
2752 } else {
2753 return NULL;
2754 }
2755}
2756
2757/**
aPiecek61d062b2020-11-02 11:05:09 +01002758 * @brief Resolve \<status\> of the current node.
2759 * @param[in] nodetype is node's type obtained from the tree.
2760 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002761 * @param[in] ca_lys_status is inherited status
2762 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002763 * @return The status type.
2764 */
2765static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002766trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002767{
2768 /* LYS_INPUT and LYS_OUTPUT is special case */
2769 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002770 return tro_flags2status(ca_lys_status);
2771 /* if ancestor's status is deprc or obslt
2772 * and also node's status is not set
2773 */
aPiecek61d062b2020-11-02 11:05:09 +01002774 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2775 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002776 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002777 } else {
2778 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002779 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002780 }
2781}
2782
2783/**
2784 * @brief Resolve \<flags\> of the current node.
2785 * @param[in] nodetype is node's type obtained from the tree.
2786 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002787 * @param[in] ca_ancestor is ancestor type obtained
2788 * from trt_parent_cache.
2789 * @param[in] ca_lys_config is inherited config item
2790 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002791 * @return The flags type.
2792 */
2793static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002794trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002795{
2796 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2797 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2798 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2799 return TRD_FLAGS_TYPE_RO;
2800 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2801 return TRD_FLAGS_TYPE_RO;
2802 } else if (nodetype & LYS_NOTIF) {
2803 return TRD_FLAGS_TYPE_NOTIF;
2804 } else if (nodetype & LYS_USES) {
2805 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2806 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2807 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002808 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002809 /* config is not set. Look at ancestor's config */
2810 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002811 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002812 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002813 }
2814}
2815
2816/**
2817 * @brief Resolve node type of the current node.
2818 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002819 * @param[in] ca_last_list is pointer to the last visited list.
2820 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002821 */
2822static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04002823trop_resolve_node_type(const struct trt_tree_ctx *tc, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002824{
ekinzie0ab8b302022-10-10 03:03:57 -04002825 const struct lysp_node *pn = tc->pn;
2826
aPiecek61d062b2020-11-02 11:05:09 +01002827 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2828 return TRD_NODE_ELSE;
2829 } else if (pn->nodetype & LYS_CASE) {
2830 return TRD_NODE_CASE;
2831 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2832 return TRD_NODE_OPTIONAL_CHOICE;
2833 } else if (pn->nodetype & LYS_CHOICE) {
2834 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04002835 } else if (tc->mounted && (tc->pn->parent == NULL)) {
2836 if (tc->parent_refs) {
2837 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
2838 if (!strcmp(tc->pmod->mod->ns, tc->parent_refs->snodes[v]->module->ns)) {
2839 return TRD_NODE_TOP_LEVEL2;
2840 }
2841 }
2842 }
2843 return TRD_NODE_TOP_LEVEL1;
aPiecekef1e58e2021-04-19 13:19:44 +02002844 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002845 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002846 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002847 return TRD_NODE_KEYS;
2848 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2849 return TRD_NODE_LISTLEAFLIST;
2850 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2851 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002852 } 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 +01002853 return TRD_NODE_OPTIONAL;
2854 } else {
2855 return TRD_NODE_ELSE;
2856 }
2857}
2858
2859/**
aPiecekef1e58e2021-04-19 13:19:44 +02002860 * @brief Resolve \<type\> of the current node.
2861 * @param[in] pn is current node.
2862 */
2863static struct trt_type
2864trop_resolve_type(const struct lysp_node *pn)
2865{
2866 const char *tmp = NULL;
2867
2868 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2869 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2870 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2871 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2872 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2873 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2874 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2875 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2876 } else if (pn->nodetype == LYS_ANYDATA) {
2877 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2878 } else if (pn->nodetype & LYS_ANYXML) {
2879 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2880 } else {
2881 return TRP_EMPTY_TRT_TYPE;
2882 }
2883}
2884
2885/**
aPiecek61d062b2020-11-02 11:05:09 +01002886 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002887 * @param[in] ca contains stored important data
2888 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002889 * @param[in] tc is context of the tree.
2890 */
2891static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002892trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002893{
aPiecekef1e58e2021-04-19 13:19:44 +02002894 const struct lysp_node *pn;
2895 struct trt_node ret;
2896
aPiecek61d062b2020-11-02 11:05:09 +01002897 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002898
2899 pn = tc->pn;
2900 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002901
2902 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002903 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002904
aPiecek61d062b2020-11-02 11:05:09 +01002905 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04002906 if (trop_node_has_mount(pn)) {
2907 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
2908 } else {
2909 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
2910 }
aPiecek61d062b2020-11-02 11:05:09 +01002911
aPiecek61d062b2020-11-02 11:05:09 +01002912 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04002913 ret.name.type = trop_resolve_node_type(tc, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002914
aPiecek34fa3772021-05-21 12:35:46 +02002915 /* The parsed tree is not compiled, so no node can be augmented
2916 * from another module. This means that nodes from the parsed tree
2917 * will never have the prefix.
2918 */
aPiecek61d062b2020-11-02 11:05:09 +01002919 ret.name.module_prefix = NULL;
2920
2921 /* set node's name */
2922 ret.name.str = pn->name;
2923
2924 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002925 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002926
2927 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002928 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002929
aPiecek3f247652021-04-19 13:40:25 +02002930 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002931
2932 return ret;
2933}
2934
aPiecekef1e58e2021-04-19 13:19:44 +02002935/**
2936 * @brief Find out if the current node has siblings.
2937 * @param[in] tc is context of the tree.
2938 * @return 1 if sibling exists otherwise 0.
2939 */
2940static ly_bool
2941trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2942{
aPiecek3f247652021-04-19 13:40:25 +02002943 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002944}
2945
aPiecek96baa7f2021-04-23 12:32:00 +02002946/**
2947 * @brief Print all yang-data sections and print three dots instead
2948 * of nodes.
2949 * @param[in] exts is array of YANG extension instances from parsed
2950 * module (@ref sizedarrays).
2951 * @param[in] mll is maximum number of characters that can be printed
2952 * on one line.
2953 * @param[in,out] out is output handler.
2954 */
2955static void
2956trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2957{
2958 struct trt_keyword_stmt ks;
2959 LY_ARRAY_COUNT_TYPE u;
2960 struct trt_wrapper wr;
2961
2962 if (!exts) {
2963 return;
2964 }
2965
2966 ly_print_(out, "\n");
2967 ks.type = TRD_KEYWORD_YANG_DATA;
2968 wr = TRP_INIT_WRAPPER_BODY;
2969
2970 LY_ARRAY_FOR(exts, u) {
2971 ly_print_(out, "\n");
2972
2973 /* yang-data <yang-data-name>: */
2974 ks.str = exts[u].argument;
2975 trp_print_keyword_stmt(ks, mll, 0, out);
2976 ly_print_(out, "\n");
2977
2978 /* ... */
2979 trp_print_wrapper(wr, out);
2980 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2981 }
2982}
2983
aPiecek874ea4d2021-04-19 12:26:36 +02002984/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002985 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002986 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002987
2988/**
aPiecek874ea4d2021-04-19 12:26:36 +02002989 * @brief Change current node pointer to its parent
2990 * but only if parent exists.
2991 * @param[in,out] tc is tree context.
2992 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002993 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002994 * @return 0 if the node did not have parents.
2995 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002996 */
2997static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002998trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002999{
3000 assert(tc && tc->pn);
3001 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02003002 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01003003 tc->pn = tc->pn->parent;
3004 return 1;
3005 } else {
3006 return 0;
3007 }
3008}
3009
3010/**
aPiecek874ea4d2021-04-19 12:26:36 +02003011 * @brief Change the current node pointer to its child
3012 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003013 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003014 * @param[in,out] tc is context of the tree.
3015 * Contains pointer to the current node.
3016 * @return Non-empty \<node\> representation of the current
3017 * node's child. The @p tc is modified.
3018 * @return Empty \<node\> representation if child don't exists.
3019 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003020 */
3021static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003022trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003023{
aPiecekef1e58e2021-04-19 13:19:44 +02003024 const struct lysp_node *tmp;
3025
aPiecek61d062b2020-11-02 11:05:09 +01003026 assert(tc && tc->pn);
3027
aPiecek3f247652021-04-19 13:40:25 +02003028 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02003029 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02003030 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01003031 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02003032 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01003033 }
3034}
3035
3036/**
aPiecek874ea4d2021-04-19 12:26:36 +02003037 * @brief Change the current node pointer to the first child of node's
3038 * parent. If current node is already first sibling/child then nothing
3039 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01003040 * @param[in,out] tc is tree context.
3041 */
3042static void
aPiecekef1e58e2021-04-19 13:19:44 +02003043trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003044{
aPiecek9f792e52021-04-21 08:33:56 +02003045 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01003046
aPiecekef1e58e2021-04-19 13:19:44 +02003047 if (trop_modi_parent(tc)) {
3048 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003049 } else {
3050 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01003051 switch (tc->section) {
3052 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003053 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02003054 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003055 break;
3056 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02003057 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02003058 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003059 break;
3060 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003061 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02003062 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003063 break;
3064 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003065 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02003066 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003067 break;
3068 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02003069 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02003070 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003071 break;
3072 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003073 /* tpn in this case is of type lysp_ext_instance */
3074 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01003075 break;
aPiecek96baa7f2021-04-23 12:32:00 +02003076 default:
3077 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01003078 }
aPiecek61d062b2020-11-02 11:05:09 +01003079 }
3080}
3081
3082/**
aPiecek874ea4d2021-04-19 12:26:36 +02003083 * @brief Change the pointer to the current node to its next sibling
3084 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003085 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003086 * @param[in,out] tc is tree context.
3087 * Contains pointer to the current node.
3088 * @return Non-empty \<node\> representation if sibling exists.
3089 * The @p tc is modified.
3090 * @return Empty \<node\> representation otherwise.
3091 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003092 */
3093static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003094trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003095{
aPiecekef1e58e2021-04-19 13:19:44 +02003096 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003097
3098 assert(tc && tc->pn);
3099
aPiecek3f247652021-04-19 13:40:25 +02003100 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01003101
aPiecekef1e58e2021-04-19 13:19:44 +02003102 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003103 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3104 tc->tpn = pn;
3105 }
aPiecekef1e58e2021-04-19 13:19:44 +02003106 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003107 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003108 } else {
3109 return TRP_EMPTY_NODE;
3110 }
3111}
3112
3113/**
3114 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003115 * @param[in,out] tc is tree context. It is modified and his current
3116 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003117 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003118 * @return Empty section structure otherwise.
3119 */
3120static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003121trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003122{
aPiecek9f792e52021-04-21 08:33:56 +02003123 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003124 const struct lysp_node_augment *augs;
3125
3126 /* if next_augment func was called for the first time */
3127 if (tc->section != TRD_SECT_AUGMENT) {
3128 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003129 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003130 } else {
3131 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003132 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003133 }
3134
aPiecekdc8fd572021-04-19 10:47:23 +02003135 if (augs) {
3136 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003137 tc->tpn = tc->pn;
3138 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3139 } else {
3140 return TRP_EMPTY_KEYWORD_STMT;
3141 }
3142}
3143
3144/**
aPiecek61d062b2020-11-02 11:05:09 +01003145 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003146 * @param[in,out] tc is tree context. It is modified and his current
3147 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003148 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003149 * @return Empty section representation otherwise.
3150 */
3151static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003152trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003153{
aPiecek9f792e52021-04-21 08:33:56 +02003154 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003155 const struct lysp_node_grp *grps;
3156
3157 if (tc->section != TRD_SECT_GROUPING) {
3158 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003159 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003160 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003161 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003162 }
3163
aPiecekdc8fd572021-04-19 10:47:23 +02003164 if (grps) {
3165 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003166 tc->tpn = tc->pn;
3167 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3168 } else {
3169 return TRP_EMPTY_KEYWORD_STMT;
3170 }
3171}
3172
aPiecek874ea4d2021-04-19 12:26:36 +02003173/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003174 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003175 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003176
3177/**
aPiecek3f247652021-04-19 13:40:25 +02003178 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003179 */
aPiecek3f247652021-04-19 13:40:25 +02003180static ly_bool
3181troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003182{
aPiecek3f247652021-04-19 13:40:25 +02003183 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3184}
aPiecek61d062b2020-11-02 11:05:09 +01003185
aPiecek3f247652021-04-19 13:40:25 +02003186/**
3187 * @brief Resolve \<flags\> of the current node.
3188 *
3189 * Use this function only if trt_tree_ctx.lysc_tree is true.
3190 *
3191 * @param[in] nodetype is current lysc_node.nodetype.
3192 * @param[in] flags is current lysc_node.flags.
3193 * @return The flags type.
3194 */
3195static trt_flags_type
3196troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3197{
3198 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3199 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3200 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3201 return TRD_FLAGS_TYPE_RO;
3202 } else if (nodetype & LYS_IS_NOTIF) {
3203 return TRD_FLAGS_TYPE_RO;
3204 } else if (nodetype & LYS_NOTIF) {
3205 return TRD_FLAGS_TYPE_NOTIF;
3206 } else if (nodetype & LYS_USES) {
3207 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3208 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3209 return TRD_FLAGS_TYPE_RPC;
3210 } else {
3211 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003212 }
aPiecek61d062b2020-11-02 11:05:09 +01003213}
3214
3215/**
aPiecek3f247652021-04-19 13:40:25 +02003216 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003217 *
aPiecek3f247652021-04-19 13:40:25 +02003218 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003219 *
aPiecek3f247652021-04-19 13:40:25 +02003220 * @param[in] nodetype is current lysc_node.nodetype.
3221 * @param[in] flags is current lysc_node.flags.
3222 */
3223static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04003224troc_resolve_node_type(const struct trt_tree_ctx *tc, uint16_t nodetype, uint16_t flags)
aPiecek3f247652021-04-19 13:40:25 +02003225{
3226 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3227 return TRD_NODE_ELSE;
3228 } else if (nodetype & LYS_CASE) {
3229 return TRD_NODE_CASE;
3230 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3231 return TRD_NODE_OPTIONAL_CHOICE;
3232 } else if (nodetype & LYS_CHOICE) {
3233 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04003234 } else if (tc->mounted && (tc->cn->parent == NULL)) {
3235 if (tc->parent_refs) {
3236 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
3237 if (!strcmp(tc->cn->module->ns, tc->parent_refs->snodes[v]->module->ns)) {
3238 return TRD_NODE_TOP_LEVEL2;
3239 }
3240 }
3241 }
3242 return TRD_NODE_TOP_LEVEL1;
aPiecek3f247652021-04-19 13:40:25 +02003243 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3244 return TRD_NODE_CONTAINER;
3245 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3246 return TRD_NODE_KEYS;
3247 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3248 return TRD_NODE_LISTLEAFLIST;
3249 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3250 return TRD_NODE_OPTIONAL;
3251 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3252 return TRD_NODE_OPTIONAL;
3253 } else {
3254 return TRD_NODE_ELSE;
3255 }
3256}
3257
3258/**
aPiecek34fa3772021-05-21 12:35:46 +02003259 * @brief Resolve prefix (<prefix>:<name>) of node that has been
3260 * placed from another module via an augment statement.
3261 *
3262 * @param[in] cn is current compiled node.
3263 * @param[in] current_compiled_module is module whose nodes are
3264 * currently being printed.
3265 * @return Prefix of foreign module or NULL.
3266 */
3267static const char *
3268troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
3269{
3270 const struct lys_module *node_module;
3271 const char *ret = NULL;
3272
3273 node_module = cn->module;
3274 if (node_module->compiled != current_compiled_module) {
3275 ret = node_module->prefix;
3276 }
3277
3278 return ret;
3279}
3280
3281/**
aPiecek3f247652021-04-19 13:40:25 +02003282 * @brief Transformation of current lysc_node to struct trt_node.
3283 * @param[in] ca is not used.
3284 * @param[in] tc is context of the tree.
3285 */
3286static struct trt_node
3287troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3288{
3289 (void) ca;
3290 const struct lysc_node *cn;
3291 struct trt_node ret;
3292
aPiecek082c7dc2021-05-20 08:55:07 +02003293 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003294
3295 cn = tc->cn;
3296 ret = TRP_EMPTY_NODE;
3297
3298 /* <status> */
3299 ret.status = tro_flags2status(cn->flags);
3300
aPiecek3f247652021-04-19 13:40:25 +02003301 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04003302 if (troc_node_has_mount(cn, &ret.mount)) {
3303 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
3304 } else {
3305 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3306 }
aPiecek3f247652021-04-19 13:40:25 +02003307
aPiecek3f247652021-04-19 13:40:25 +02003308 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04003309 ret.name.type = troc_resolve_node_type(tc, cn->nodetype, cn->flags);
aPiecek3f247652021-04-19 13:40:25 +02003310
aPiecek34fa3772021-05-21 12:35:46 +02003311 /* <prefix> */
3312 ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
aPiecek3f247652021-04-19 13:40:25 +02003313
3314 /* set node's name */
3315 ret.name.str = cn->name;
3316
aPiecekbbc02932021-05-21 07:19:41 +02003317 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003318 /* <type> */
3319 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003320
aPiecek082c7dc2021-05-20 08:55:07 +02003321 /* <iffeature> */
3322 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3323 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003324 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003325 assert(tc->cn->nodetype & LYS_CASE);
3326
3327 /* <type> */
3328 ret.type = TRP_EMPTY_TRT_TYPE;
3329
aPiecek7a28e2f2021-05-21 07:27:03 +02003330 /* <iffeature> */
3331 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003332 }
aPiecek3f247652021-04-19 13:40:25 +02003333
3334 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3335
3336 return ret;
3337}
3338
3339/**********************************************************************
3340 * Modify troc getters
3341 *********************************************************************/
3342
3343/**
aPiecek01598c02021-04-23 14:18:24 +02003344 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003345 */
3346static ly_bool
3347troc_modi_parent(struct trt_tree_ctx *tc)
3348{
3349 assert(tc && tc->cn);
3350 /* If no parent exists, stay in actual node. */
3351 if (tc->cn->parent) {
3352 tc->cn = tc->cn->parent;
3353 return 1;
3354 } else {
3355 return 0;
3356 }
3357}
3358
3359/**
aPiecek01598c02021-04-23 14:18:24 +02003360 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003361 */
3362static struct trt_node
3363troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3364{
3365 const struct lysc_node *cn;
3366
3367 assert(tc && tc->cn);
3368
3369 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3370
3371 /* if next sibling exists */
3372 if (cn) {
3373 /* update trt_tree_ctx */
3374 tc->cn = cn;
3375 return troc_read_node(ca, tc);
3376 } else {
3377 return TRP_EMPTY_NODE;
3378 }
3379}
3380
3381/**
3382 * @copydoc trop_modi_next_child()
3383 */
3384static struct trt_node
3385troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3386{
3387 const struct lysc_node *tmp;
3388
3389 assert(tc && tc->cn);
3390
3391 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3392 tc->cn = tmp;
3393 return troc_read_node(ca, tc);
3394 } else {
3395 return TRP_EMPTY_NODE;
3396 }
3397}
3398
3399/**
aPiecek01598c02021-04-23 14:18:24 +02003400 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003401 */
3402static void
aPiecek3f247652021-04-19 13:40:25 +02003403troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003404{
aPiecek3f247652021-04-19 13:40:25 +02003405 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003406
aPiecek3f247652021-04-19 13:40:25 +02003407 if (troc_modi_parent(tc)) {
3408 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3409 } else {
3410 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003411 switch (tc->section) {
3412 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003413 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003414 break;
3415 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003416 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003417 break;
3418 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003419 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003420 break;
3421 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003422 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003423 break;
3424 default:
3425 assert(0);
3426 }
aPiecek61d062b2020-11-02 11:05:09 +01003427 }
3428}
3429
aPiecek874ea4d2021-04-19 12:26:36 +02003430/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003431 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003432 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003433
3434/**
3435 * @brief Get size of node name.
3436 * @param[in] name contains name and mark.
3437 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003438 * @return negative value as an indication that option mark
3439 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003440 */
3441static int32_t
3442trb_strlen_of_name_and_mark(struct trt_node_name name)
3443{
3444 size_t name_len = strlen(name.str);
3445
3446 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3447 /* counting also parentheses */
3448 name_len += 2;
3449 }
3450
3451 return trp_mark_is_used(name) ?
3452 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3453 (int32_t)name_len;
3454}
3455
3456/**
aPiecek874ea4d2021-04-19 12:26:36 +02003457 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3458 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003459 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003460 * @param[in] max_len4all is the maximum value of btw_opts_type
3461 * that it can have.
3462 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003463 */
3464static int16_t
3465trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3466{
3467 int32_t name_len;
3468 int16_t min_len;
3469 int16_t ret;
3470
3471 name_len = trb_strlen_of_name_and_mark(name);
3472
3473 /* negative value indicate that in name is some opt mark */
3474 min_len = name_len < 0 ?
3475 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3476 TRD_INDENT_BEFORE_TYPE;
3477 ret = abs(max_len4all) - abs(name_len);
3478
3479 /* correction -> negative indicate that name is too long. */
3480 return ret < 0 ? min_len : ret;
3481}
3482
3483/**
3484 * @brief Print node.
3485 *
aPiecek01598c02021-04-23 14:18:24 +02003486 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003487 * But difference is that take @p max_gap_before_type which will be
3488 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003489 *
aPiecek9bdd7592021-05-20 08:13:20 +02003490 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003491 * @param[in] max_gap_before_type is number of indent before \<type\>.
3492 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003493 * @param[in] pc contains mainly functions for printing.
3494 * @param[in] tc is tree context.
3495 */
3496static void
aPiecek9bdd7592021-05-20 08:13:20 +02003497trb_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 +01003498{
aPiecek61d062b2020-11-02 11:05:09 +01003499 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3500
3501 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3502 /* print actual node with unified indent */
3503 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3504 }
3505 /* after -> print actual node with default indent */
3506 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3507 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
ekinzie0ab8b302022-10-10 03:03:57 -04003508 if (node.flags == TRD_FLAGS_TYPE_MOUNT_POINT) {
3509 struct trt_wrapper wr_mount;
3510 struct tro_getters get;
3511
3512 wr_mount = pc->fp.read.if_sibling_exists(tc) ?
3513 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3514
3515 get = tc->lysc_tree ? troc_init_getters() : trop_init_getters();
3516 if (get.child(tc->lysc_tree ? (void *)tc->cn : (void *)tc->pn)) {
3517 /* If this node has a child, we need to draw a vertical line
3518 * from the last mounted module to the first child
3519 */
3520 wr_mount = trp_wrapper_set_mark_top(wr_mount);
3521 }
3522
3523 trb_print_mount_point(&node, wr_mount, pc, tc);
3524 }
aPiecek61d062b2020-11-02 11:05:09 +01003525}
3526
3527/**
aPiecek874ea4d2021-04-19 12:26:36 +02003528 * @brief Check if parent of the current node is the last
3529 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003530 *
aPiecek874ea4d2021-04-19 12:26:36 +02003531 * To mantain stability use this function only if the current node is
3532 * the first of the siblings.
3533 * Side-effect -> current node is set to the first sibling
3534 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003535 *
aPiecek01598c02021-04-23 14:18:24 +02003536 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003537 * @param[in,out] tc is tree context.
3538 * @return 1 if parent is last sibling otherwise 0.
3539 */
3540static ly_bool
3541trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3542{
3543 if (fp.modify.parent(tc)) {
3544 ly_bool ret = fp.read.if_sibling_exists(tc);
Michal Vasko26bbb272022-08-02 14:54:33 +02003545
aPiecek61d062b2020-11-02 11:05:09 +01003546 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3547 return !ret;
3548 } else {
3549 return !fp.read.if_sibling_exists(tc);
3550 }
3551}
3552
3553/**
3554 * @brief Find sibling with the biggest node name and return that size.
3555 *
3556 * Side-effect -> Current node is set to the first sibling.
3557 *
3558 * @param[in] ca contains inherited data from ancestors.
3559 * @param[in] pc contains mainly functions for printing.
3560 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003561 * @return positive number as a sign that only the node name is
3562 * included in the size.
3563 * @return negative number sign that node name and his opt mark is
3564 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003565 */
3566static int32_t
3567trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3568{
3569 int32_t ret = 0;
3570
3571 pc->fp.modify.first_sibling(tc);
3572
3573 for (struct trt_node node = pc->fp.read.node(ca, tc);
3574 !trp_node_is_empty(node);
3575 node = pc->fp.modify.next_sibling(ca, tc)) {
3576 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
Michal Vasko26bbb272022-08-02 14:54:33 +02003577
aPiecek61d062b2020-11-02 11:05:09 +01003578 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3579 }
3580 pc->fp.modify.first_sibling(tc);
3581 return ret;
3582}
3583
3584/**
aPiecek874ea4d2021-04-19 12:26:36 +02003585 * @brief Find maximal indent between
3586 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003587 *
3588 * Side-effect -> Current node is set to the first sibling.
3589 *
3590 * @param[in] ca contains inherited data from ancestors.
3591 * @param[in] pc contains mainly functions for printing.
3592 * @param[in,out] tc is tree context.
3593 * @return max btw_opts_type value for rest of the siblings
3594 */
3595static int16_t
3596trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3597{
3598 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3599 int16_t ind_before_type = maxlen_node_name < 0 ?
3600 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3601 TRD_INDENT_BEFORE_TYPE;
3602
3603 return abs(maxlen_node_name) + ind_before_type;
3604}
3605
3606/**
aPiecek874ea4d2021-04-19 12:26:36 +02003607 * @brief Find out if it is possible to unify
3608 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003609 *
aPiecek874ea4d2021-04-19 12:26:36 +02003610 * The goal is for all node siblings to have the same alignment
3611 * for \<type\> as if they were in a column. All siblings who cannot
3612 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003613 * Side-effect -> Current node is set to the first sibling.
3614 *
3615 * @param[in] ca contains inherited data from ancestors.
3616 * @param[in] pc contains mainly functions for printing.
3617 * @param[in,out] tc is tree context.
3618 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003619 * @return positive number indicating the maximum number of spaces
3620 * before \<type\> if the length of the node name is 0. To calculate
3621 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003622 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003623*/
3624static uint32_t
3625trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3626{
3627 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3628}
3629
3630/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003631 * @brief Check if there is no case statement
3632 * under the choice statement.
3633 *
3634 * It can return true only if the Parsed schema tree
3635 * is used for browsing.
3636 *
3637 * @param[in] tc is tree context.
3638 * @return 1 if implicit case statement is present otherwise 0.
3639 */
3640static ly_bool
3641trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3642{
3643 return !tc->lysc_tree && tc->pn->parent &&
3644 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3645 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3646 LYS_LEAF | LYS_LEAFLIST));
3647}
3648
3649static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3650 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3651
3652/**
3653 * @brief Print implicit case node and his subtree.
3654 *
3655 * @param[in] node is child of implicit case.
3656 * @param[in] wr is wrapper for printing identation before node.
3657 * @param[in] ca contains inherited data from ancestors.
3658 * @param[in] pc contains mainly functions for printing.
3659 * @param[in] tc is tree context. Its settings should be the same as
3660 * before the function call.
3661 */
3662static void
3663trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3664 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3665{
3666 struct trt_node case_node;
3667 struct trt_wrapper wr_case_child;
3668
3669 case_node = tro_create_implicit_case_node(node);
3670 ly_print_(pc->out, "\n");
3671 trb_print_entire_node(case_node, 0, wr, pc, tc);
3672 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3673 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3674 ly_print_(pc->out, "\n");
3675 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3676}
3677
3678/**
aPiecek874ea4d2021-04-19 12:26:36 +02003679 * @brief For the current node: recursively print all of its child
3680 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003681 *
aPiecek01598c02021-04-23 14:18:24 +02003682 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003683 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003684 * Nodes are printed, including unified sibling node alignment
3685 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003686 * Side-effect -> current node is set to the last sibling.
3687 *
3688 * @param[in] wr is wrapper for printing identation before node.
3689 * @param[in] ca contains inherited data from ancestors.
3690 * @param[in] pc contains mainly functions for printing.
3691 * @param[in,out] tc is tree context.
3692 */
3693static void
3694trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3695{
3696 uint32_t max_gap_before_type;
3697 ly_bool sibling_flag = 0;
3698 ly_bool child_flag = 0;
3699
3700 /* if node is last sibling, then do not add '|' to wrapper */
3701 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3702 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3703
3704 /* try unified indentation in node */
3705 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3706
3707 /* print all siblings */
3708 do {
3709 struct trt_parent_cache new_ca;
3710 struct trt_node node;
Michal Vasko26bbb272022-08-02 14:54:33 +02003711
aPiecek9bdd7592021-05-20 08:13:20 +02003712 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003713
aPiecekb8d5a0a2021-05-20 08:20:24 +02003714 if (!trb_need_implicit_node_case(tc)) {
3715 /* normal behavior */
3716 ly_print_(pc->out, "\n");
3717 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3718 new_ca = tro_parent_cache_for_child(ca, tc);
3719 /* go to the actual node's child or stay in actual node */
3720 node = pc->fp.modify.next_child(ca, tc);
3721 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003722
aPiecekb8d5a0a2021-05-20 08:20:24 +02003723 if (child_flag) {
3724 /* print all childs - recursive call */
3725 trb_print_nodes(wr, new_ca, pc, tc);
3726 /* get back from child node to actual node */
3727 pc->fp.modify.parent(tc);
3728 }
3729 } else {
3730 /* The case statement is omitted (shorthand).
3731 * Print implicit case node and his subtree.
3732 */
3733 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003734 }
3735
3736 /* go to the actual node's sibling */
3737 node = pc->fp.modify.next_sibling(ca, tc);
3738 sibling_flag = !trp_node_is_empty(node);
3739
3740 /* go to the next sibling or stay in actual node */
3741 } while (sibling_flag);
3742}
3743
3744/**
aPiecek153b00f2021-04-20 13:52:57 +02003745 * @brief Calculate the wrapper about how deep in the tree the node is.
ekinzie0ab8b302022-10-10 03:03:57 -04003746 * @param[in] wr_in A wrapper to use as a starting point
aPiecek153b00f2021-04-20 13:52:57 +02003747 * @param[in] node from which to count.
3748 * @return wrapper for @p node.
3749 */
3750static struct trt_wrapper
ekinzie0ab8b302022-10-10 03:03:57 -04003751trb_count_depth(const struct trt_wrapper *wr_in, const struct lysc_node *node)
aPiecek153b00f2021-04-20 13:52:57 +02003752{
ekinzie0ab8b302022-10-10 03:03:57 -04003753 struct trt_wrapper wr = wr_in ? *wr_in : TRP_INIT_WRAPPER_TOP;
aPiecek153b00f2021-04-20 13:52:57 +02003754 const struct lysc_node *parent;
3755
3756 if (!node) {
3757 return wr;
3758 }
3759
3760 for (parent = node->parent; parent; parent = parent->parent) {
3761 wr = trp_wrapper_set_shift(wr);
3762 }
3763
3764 return wr;
3765}
3766
3767/**
3768 * @brief Print all parent nodes of @p node and the @p node itself.
3769 *
3770 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3771 *
3772 * @param[in] node on which the function is focused.
aPiecek01598c02021-04-23 14:18:24 +02003773 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003774 * @param[in,out] tc is context of tree printer.
3775 * @return wrapper for @p node.
3776 */
3777static void
ekinzie0ab8b302022-10-10 03:03:57 -04003778trb_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 +02003779{
3780 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003781 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003782
3783 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3784
3785 /* stop recursion */
3786 if (!node) {
3787 return;
3788 }
ekinzie0ab8b302022-10-10 03:03:57 -04003789 trb_print_parents(node->parent, wr_in, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003790
3791 /* setup for printing */
3792 tc->cn = node;
ekinzie0ab8b302022-10-10 03:03:57 -04003793 wr = trb_count_depth(wr_in, node);
aPiecek153b00f2021-04-20 13:52:57 +02003794
3795 /* print node */
3796 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003797 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3798 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003799}
3800
3801/**
aPiecekdc8fd572021-04-19 10:47:23 +02003802 * @brief Get address of the current node.
3803 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003804 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003805 */
3806static const void *
3807trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3808{
aPiecek3f247652021-04-19 13:40:25 +02003809 return tc->lysc_tree ?
3810 (const void *)tc->cn :
3811 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003812}
3813
3814/**
3815 * @brief Get address of current node's child.
3816 * @param[in,out] tc contains current node.
3817 */
3818static const void *
3819trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3820{
3821 if (!trb_tree_ctx_get_node(tc)) {
3822 return NULL;
3823 }
3824
aPiecek3f247652021-04-19 13:40:25 +02003825 if (tc->lysc_tree) {
3826 return lysc_node_child(tc->cn);
3827 } else {
3828 return lysp_node_child(tc->pn);
3829 }
aPiecekdc8fd572021-04-19 10:47:23 +02003830}
3831
3832/**
3833 * @brief Set current node on its child.
3834 * @param[in,out] tc contains current node.
3835 */
3836static void
3837trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3838{
aPiecek3f247652021-04-19 13:40:25 +02003839 const void *node = trb_tree_ctx_get_child(tc);
3840
3841 if (tc->lysc_tree) {
3842 tc->cn = node;
3843 } else {
3844 tc->pn = node;
3845 }
aPiecekdc8fd572021-04-19 10:47:23 +02003846}
3847
3848/**
aPiecek61d062b2020-11-02 11:05:09 +01003849 * @brief Print subtree of nodes.
3850 *
3851 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003852 * Before root node is no linebreak printing. This must be addressed by
3853 * the caller. Root node will also be printed. Behind last printed node
3854 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003855 *
aPiecek9bdd7592021-05-20 08:13:20 +02003856 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003857 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003858 * ::trb_try_unified_indent() function for root node.
3859 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003860 * @param[in] wr is wrapper saying how deep in the whole tree
3861 * is the root of the subtree.
3862 * @param[in] ca is parent_cache from root's parent.
3863 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003864 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003865 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003866 */
3867static void
aPiecek9bdd7592021-05-20 08:13:20 +02003868trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3869 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003870{
3871 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003872
aPiecek9bdd7592021-05-20 08:13:20 +02003873 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
ekinzie0ab8b302022-10-10 03:03:57 -04003874
aPiecek61d062b2020-11-02 11:05:09 +01003875 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003876 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003877 node = pc->fp.modify.next_child(ca, tc);
3878
3879 if (!trp_node_is_empty(node)) {
3880 /* print root's nodes */
3881 trb_print_nodes(wr, new_ca, pc, tc);
3882 /* get back from child node to actual node */
3883 pc->fp.modify.parent(tc);
3884 }
3885}
3886
3887/**
3888 * @brief Get number of siblings.
3889 *
3890 * Side-effect -> current node is set to the first sibling.
3891 *
3892 * @param[in] fp contains callback functions which modify tree context
3893 * @param[in,out] tc is the tree context.
3894 * @return Number of siblings of the current node.
3895 */
3896static uint32_t
3897trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3898{
3899 uint32_t ret = 1;
3900 struct trt_node node = TRP_EMPTY_NODE;
3901
3902 /* including actual node */
3903 fp.first_sibling(tc);
3904 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3905 ret++;
3906 }
3907 fp.first_sibling(tc);
3908 return ret;
3909}
3910
3911/**
3912 * @brief Print all parents and their children.
3913 *
aPiecek874ea4d2021-04-19 12:26:36 +02003914 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003915 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003916 * for all top-level siblings. Use this function after 'module' keyword
3917 * or 'augment' and so. The nodes may not be exactly top-level in the
3918 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003919 *
aPiecek153b00f2021-04-20 13:52:57 +02003920 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3921 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003922 * @param[pc] pc contains mainly functions for printing.
3923 * @param[in,out] tc is tree context.
3924 */
3925static void
aPiecek153b00f2021-04-20 13:52:57 +02003926trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003927{
aPiecek61d062b2020-11-02 11:05:09 +01003928 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003929 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003930 uint32_t total_parents;
3931 uint32_t max_gap_before_type;
3932
aPiecekdc8fd572021-04-19 10:47:23 +02003933 if (!trb_tree_ctx_get_node(tc)) {
3934 return;
3935 }
3936
aPiecek61d062b2020-11-02 11:05:09 +01003937 ca = TRP_EMPTY_PARENT_CACHE;
3938 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3939 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3940
aPiecek3f247652021-04-19 13:40:25 +02003941 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003942 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3943 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003944 ca.lys_config = 0x0;
3945 }
aPiecekdc8fd572021-04-19 10:47:23 +02003946 }
3947
aPiecek61d062b2020-11-02 11:05:09 +01003948 for (uint32_t i = 0; i < total_parents; i++) {
3949 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003950 node = pc->fp.read.node(ca, tc);
3951 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003952 pc->fp.modify.next_sibling(ca, tc);
3953 }
3954}
3955
aPiecek874ea4d2021-04-19 12:26:36 +02003956/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003957 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003958 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003959
3960/**
aPiecekdc8fd572021-04-19 10:47:23 +02003961 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003962 *
aPiecekdc8fd572021-04-19 10:47:23 +02003963 * @param[in] module YANG schema tree structure representing
3964 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003965 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003966 * @param[in] max_line_length is the maximum line length limit
3967 * that should not be exceeded.
3968 * @param[in,out] pc will be adapted to lysp_tree.
3969 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003970 */
3971static void
aPiecekdc8fd572021-04-19 10:47:23 +02003972trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003973{
aPiecekdc8fd572021-04-19 10:47:23 +02003974 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003975 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003976 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003977 .pmod = module->parsed,
3978 .cmod = NULL,
3979 .pn = module->parsed ? module->parsed->data : NULL,
3980 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003981 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003982 };
aPiecek61d062b2020-11-02 11:05:09 +01003983
aPiecekdc8fd572021-04-19 10:47:23 +02003984 pc->out = out;
3985
3986 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003987 .parent = trop_modi_parent,
3988 .first_sibling = trop_modi_first_sibling,
3989 .next_sibling = trop_modi_next_sibling,
3990 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01003991 };
3992
aPiecekdc8fd572021-04-19 10:47:23 +02003993 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003994 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003995 .node = trop_read_node,
3996 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003997 };
3998
aPiecekdc8fd572021-04-19 10:47:23 +02003999 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02004000 .print_features_names = tro_print_features_names,
4001 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01004002 };
4003
aPiecekdc8fd572021-04-19 10:47:23 +02004004 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01004005}
4006
4007/**
aPiecek3f247652021-04-19 13:40:25 +02004008 * @brief Settings if lysc_node are used for browsing through the tree.
4009 *
4010 * Pointers to current nodes will be set to module data.
4011 *
4012 * @param[in] module YANG schema tree structure representing
4013 * YANG module.
4014 * @param[in] out is output handler.
4015 * @param[in] max_line_length is the maximum line length limit
4016 * that should not be exceeded.
4017 * @param[in,out] pc will be adapted to lysc_tree.
4018 * @param[in,out] tc will be adapted to lysc_tree.
4019 */
4020static void
4021trm_lysc_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4022{
4023 *tc = (struct trt_tree_ctx) {
4024 .lysc_tree = 1,
4025 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004026 .pmod = module->parsed,
4027 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02004028 .tpn = NULL,
4029 .pn = NULL,
4030 .cn = module->compiled->data
4031 };
4032
4033 pc->out = out;
4034
4035 pc->fp.modify = (struct trt_fp_modify_ctx) {
4036 .parent = troc_modi_parent,
4037 .first_sibling = troc_modi_first_sibling,
4038 .next_sibling = troc_modi_next_sibling,
4039 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02004040 };
4041
4042 pc->fp.read = (struct trt_fp_read) {
4043 .module_name = tro_read_module_name,
4044 .node = troc_read_node,
4045 .if_sibling_exists = troc_read_if_sibling_exists
4046 };
4047
4048 pc->fp.print = (struct trt_fp_print) {
4049 .print_features_names = tro_print_features_names,
4050 .print_keys = tro_print_keys
4051 };
4052
4053 pc->max_line_length = max_line_length;
4054}
4055
4056/**
4057 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02004058 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02004059 * @param[in,out] tc resets to lysc browsing.
4060 */
4061static void
4062trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4063{
aPiecek9f792e52021-04-21 08:33:56 +02004064 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02004065}
4066
4067/**
4068 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02004069 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02004070 * @param[in,out] tc resets to lysp browsing.
4071 */
4072static void
4073trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4074{
aPiecek9f792e52021-04-21 08:33:56 +02004075 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02004076}
4077
4078/**
4079 * @brief If augment's target node is located on the current module.
4080 * @param[in] pn is examined augment.
4081 * @param[in] pmod is current module.
4082 * @return 1 if nodeid refers to the local node, otherwise 0.
4083 */
4084static ly_bool
4085trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
4086{
4087 const char *id, *prefix, *name;
4088 size_t prefix_len, name_len;
4089 const struct lys_module *mod;
4090 ly_bool ret = 0;
4091
4092 if (pn == NULL) {
4093 return ret;
4094 }
4095
4096 id = pn->nodeid;
4097 if (!id) {
4098 return ret;
4099 }
4100 /* only absolute-schema-nodeid is taken into account */
4101 assert(id[0] == '/');
4102 ++id;
4103
4104 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
4105 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02004106 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
Michal Vasko3003c9a2022-03-29 12:10:00 +02004107 ret = mod ? (mod->parsed == pmod) : 0;
aPiecek3f247652021-04-19 13:40:25 +02004108 } else {
4109 ret = 1;
4110 }
4111
4112 return ret;
4113}
4114
4115/**
aPiecek96baa7f2021-04-23 12:32:00 +02004116 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01004117 *
aPiecekdc8fd572021-04-19 10:47:23 +02004118 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02004119 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01004120 *
aPiecekdc8fd572021-04-19 10:47:23 +02004121 * @param[in] ks is section representation.
4122 * @param[in] pc contains mainly functions for printing.
4123 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01004124 */
4125static void
aPiecekdc8fd572021-04-19 10:47:23 +02004126trm_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 +01004127{
aPiecekdc8fd572021-04-19 10:47:23 +02004128 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4129 return;
4130 }
4131
4132 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
4133 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02004134 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004135 } else {
aPiecek153b00f2021-04-20 13:52:57 +02004136 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004137 }
4138}
4139
4140/**
aPiecek96baa7f2021-04-23 12:32:00 +02004141 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02004142 *
aPiecek96baa7f2021-04-23 12:32:00 +02004143 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02004144 *
4145 * @param[in] ks is section representation.
4146 * @param[in] pc contains mainly functions for printing.
4147 * @param[in,out] tc is the tree context.
4148 */
4149static void
4150trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4151{
4152 ly_bool grp_has_data = 0;
4153
4154 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4155 return;
4156 }
4157
4158 if (ks.type == TRD_KEYWORD_GROUPING) {
4159 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4160 }
4161
4162 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4163 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004164 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004165}
4166
4167/**
4168 * @brief Print 'module' keyword, its name and all nodes.
4169 * @param[in] pc contains mainly functions for printing.
4170 * @param[in,out] tc is the tree context.
4171 */
4172static void
4173trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4174{
4175 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4176}
4177
4178/**
4179 * @brief For all augment sections: print 'augment' keyword,
4180 * its target node and all nodes.
4181 * @param[in] pc contains mainly functions for printing.
4182 * @param[in,out] tc is the tree context.
4183 */
4184static void
4185trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4186{
4187 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004188 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004189
aPiecek3f247652021-04-19 13:40:25 +02004190 if (tc->lysc_tree) {
4191 origin_was_lysc_tree = 1;
4192 trm_reset_to_lysp_tree_ctx(pc, tc);
4193 }
4194
aPiecekdc8fd572021-04-19 10:47:23 +02004195 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004196 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004197 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004198 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004199
aPiecek3f247652021-04-19 13:40:25 +02004200 if (origin_was_lysc_tree) {
4201 /* if lysc tree is used, then only augments targeting
4202 * another module are printed
4203 */
aPiecek9f792e52021-04-21 08:33:56 +02004204 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004205 continue;
4206 }
4207 }
4208
aPiecekdc8fd572021-04-19 10:47:23 +02004209 if (once) {
4210 ly_print_(pc->out, "\n");
4211 ly_print_(pc->out, "\n");
4212 once = 0;
4213 } else {
4214 ly_print_(pc->out, "\n");
4215 }
4216
4217 trm_print_section_as_subtree(ks, pc, tc);
4218 }
aPiecek3f247652021-04-19 13:40:25 +02004219
4220 if (origin_was_lysc_tree) {
4221 trm_reset_to_lysc_tree_ctx(pc, tc);
4222 }
aPiecekdc8fd572021-04-19 10:47:23 +02004223}
4224
4225/**
4226 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4227 * @param[in] pc contains mainly functions for printing.
4228 * @param[in,out] tc is the tree context.
4229 */
4230static void
4231trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4232{
4233 struct trt_keyword_stmt rpc;
4234
aPiecek01598c02021-04-23 14:18:24 +02004235 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004236
4237 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4238 ly_print_(pc->out, "\n");
4239 ly_print_(pc->out, "\n");
4240 trm_print_section_as_family_tree(rpc, pc, tc);
4241 }
4242}
4243
4244/**
4245 * @brief For notifications section: print 'notifications' keyword
4246 * and all its nodes.
4247 * @param[in] pc contains mainly functions for printing.
4248 * @param[in,out] tc is the tree context.
4249 */
4250static void
4251trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4252{
4253 struct trt_keyword_stmt notifs;
4254
aPiecek01598c02021-04-23 14:18:24 +02004255 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004256
4257 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4258 ly_print_(pc->out, "\n");
4259 ly_print_(pc->out, "\n");
4260 trm_print_section_as_family_tree(notifs, pc, tc);
4261 }
4262}
4263
4264/**
4265 * @brief For all grouping sections: print 'grouping' keyword, its name
4266 * and all nodes.
4267 * @param[in] pc contains mainly functions for printing.
4268 * @param[in,out] tc is the tree context.
4269 */
4270static void
4271trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4272{
4273 ly_bool once;
4274
aPiecek01598c02021-04-23 14:18:24 +02004275 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004276 return;
4277 }
4278
4279 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004280 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004281 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004282 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004283 if (once) {
4284 ly_print_(pc->out, "\n");
4285 ly_print_(pc->out, "\n");
4286 once = 0;
4287 } else {
4288 ly_print_(pc->out, "\n");
4289 }
4290 trm_print_section_as_subtree(ks, pc, tc);
4291 }
4292}
4293
4294/**
4295 * @brief For all yang-data sections: print 'yang-data' keyword
4296 * and all its nodes.
4297 * @param[in] pc contains mainly functions for printing.
4298 * @param[in,out] tc is the tree context.
4299 */
4300static void
aPiecek96baa7f2021-04-23 12:32:00 +02004301trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004302{
aPiecek96baa7f2021-04-23 12:32:00 +02004303 ly_bool once;
4304 LY_ARRAY_COUNT_TYPE count;
4305
4306 count = LY_ARRAY_COUNT(tc->pmod->exts);
4307 if (count == 0) {
4308 return;
4309 }
4310
4311 once = 1;
4312 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4313 struct trt_keyword_stmt ks;
4314
aPiecek01598c02021-04-23 14:18:24 +02004315 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004316 * ::lysp_ext_instance.parsed.
4317 */
4318 if (!tc->pmod->exts[u].parsed) {
4319 /* print at least the yang-data names */
4320 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4321 continue;
4322 }
4323
4324 ks = tro_modi_next_yang_data(tc, u);
4325 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4326 break;
4327 }
4328
4329 if (once) {
4330 ly_print_(pc->out, "\n");
4331 ly_print_(pc->out, "\n");
4332 once = 0;
4333 } else {
4334 ly_print_(pc->out, "\n");
4335 }
4336
4337 trm_print_section_as_family_tree(ks, pc, tc);
4338 }
aPiecekdc8fd572021-04-19 10:47:23 +02004339}
4340
4341/**
4342 * @brief Print sections module, augment, rpcs, notifications,
4343 * grouping, yang-data.
4344 * @param[in] pc contains mainly functions for printing.
4345 * @param[in,out] tc is the tree context.
4346 */
4347static void
4348trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4349{
4350 trm_print_module_section(pc, tc);
4351 trm_print_augmentations(pc, tc);
4352 trm_print_rpcs(pc, tc);
4353 trm_print_notifications(pc, tc);
4354 trm_print_groupings(pc, tc);
4355 trm_print_yang_data(pc, tc);
4356 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004357}
4358
aPiecek874ea4d2021-04-19 12:26:36 +02004359/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004360 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004361 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004362
4363LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004364tree_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 +01004365{
4366 struct trt_printer_ctx pc;
4367 struct trt_tree_ctx tc;
4368 struct ly_out *new_out;
4369 LY_ERR erc;
4370 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4371
aPiecekdc8fd572021-04-19 10:47:23 +02004372 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4373
aPiecek61d062b2020-11-02 11:05:09 +01004374 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4375 return erc;
4376 }
4377
4378 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004379 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4380 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4381 } else {
4382 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4383 }
aPiecek61d062b2020-11-02 11:05:09 +01004384
4385 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004386 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004387
4388 ly_out_free(new_out, NULL, 1);
4389
aPiecekdc8fd572021-04-19 10:47:23 +02004390 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004391}
4392
4393LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004394tree_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 +01004395{
aPiecek153b00f2021-04-20 13:52:57 +02004396 struct trt_printer_ctx pc;
4397 struct trt_tree_ctx tc;
4398 struct ly_out *new_out;
4399 struct trt_wrapper wr;
4400 LY_ERR erc;
4401 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4402
4403 assert(out && node);
4404
4405 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4406 return LY_EINVAL;
4407 }
4408
4409 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4410 return erc;
4411 }
4412
4413 line_length = line_length == 0 ? SIZE_MAX : line_length;
4414 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4415
4416 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
ekinzie0ab8b302022-10-10 03:03:57 -04004417 trb_print_parents(node, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004418
4419 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4420 tc.cn = lysc_node_child(node);
ekinzie0ab8b302022-10-10 03:03:57 -04004421 wr = trb_count_depth(NULL, tc.cn);
aPiecek153b00f2021-04-20 13:52:57 +02004422 trb_print_family_tree(wr, &pc, &tc);
4423 }
4424 ly_print_(out, "\n");
4425
4426 erc = clb_arg.last_error;
4427 ly_out_free(new_out, NULL, 1);
4428
4429 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004430}
4431
4432LY_ERR
Michal Vasko6a914fb2022-01-17 10:36:14 +01004433tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
4434 size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004435{
aPiecek9f792e52021-04-21 08:33:56 +02004436 struct trt_printer_ctx pc;
4437 struct trt_tree_ctx tc;
4438 struct ly_out *new_out;
4439 LY_ERR erc;
4440 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4441
4442 assert(submodp);
4443 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4444
4445 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4446 return erc;
4447 }
4448
4449 line_length = line_length == 0 ? SIZE_MAX : line_length;
4450 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4451 tc.pmod = (struct lysp_module *)submodp;
4452 tc.tpn = submodp->data;
4453 tc.pn = tc.tpn;
4454
4455 trm_print_sections(&pc, &tc);
4456 erc = clb_arg.last_error;
4457
4458 ly_out_free(new_out, NULL, 1);
4459
4460 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004461}
ekinzie0ab8b302022-10-10 03:03:57 -04004462
4463static LY_ERR
4464trb_print_mount_point(struct trt_node *node, struct trt_wrapper wr, struct trt_printer_ctx *pc,
4465 struct trt_tree_ctx *UNUSED(tc))
4466{
4467 struct ly_ctx *ext_ctx = NULL;
4468 const struct lys_module *mod;
4469 struct trt_tree_ctx tmptc;
4470 struct trt_wrapper tmpwr;
4471 struct trt_printer_ctx tmppc;
4472 struct trt_mount_ctx *mc;
4473 struct ly_set *modules;
4474 struct ly_set *refs = NULL;
4475 LY_ERR err = LY_SUCCESS;
4476
4477 if (!node->mount) {
4478 /* modules are parsed only */
4479 return LY_SUCCESS;
4480 }
4481
4482 if (lyplg_ext_schema_mount_create_context(node->mount, &ext_ctx)) {
4483 /* Void mount point */
4484 return LY_SUCCESS;
4485 }
4486
4487 if (ly_set_new(&modules)) {
4488 err = LY_SUCCESS;
4489 goto out_ctx;
4490 }
4491
4492 lyplg_ext_schema_mount_get_parent_ref(node->mount, &refs);
4493
4494 /* build new list of modules to print. This list will omit internal
4495 * modules, modules with no nodes (e.g., iana-if-types) and modules
4496 * that were loaded as the result of a parent-reference.
4497 */
4498 uint32_t v = ly_ctx_internal_modules_count(ext_ctx);
4499
4500 tmppc = *pc;
4501 while ((mod = ly_ctx_get_module_iter(ext_ctx, &v))) {
4502 uint32_t siblings;
4503 ly_bool from_parent_ref = 0;
4504
4505 for (uint32_t pr = 0; refs && pr < refs->count; pr++) {
4506 if (!strcmp(mod->ns, refs->snodes[pr]->module->ns)) {
4507 from_parent_ref = 1;
4508 break;
4509 }
4510 }
4511 if (from_parent_ref) {
4512 continue;
4513 }
4514
4515 if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) {
4516 trm_lysc_tree_ctx(mod, pc->out, pc->max_line_length, &tmppc, &tmptc);
4517 } else {
4518 trm_lysp_tree_ctx(mod, pc->out, pc->max_line_length, &tmppc, &tmptc);
4519 }
4520
4521 siblings = (tmptc.pn || tmptc.cn) ?
4522 trb_get_number_of_siblings(tmppc.fp.modify, &tmptc) : 0;
4523 if (siblings == 0) {
4524 continue;
4525 }
4526 tmptc.mounted = 1;
4527 tmptc.parent_refs = refs;
4528
4529 mc = malloc(sizeof *mc);
4530 if (mc == NULL) {
4531 err = LY_EMEM;
4532 goto out;
4533 }
4534
4535 mc->tc = tmptc;
4536 mc->pc = tmppc;
4537 ly_set_add(modules, (void *)mc, 1, NULL);
4538 }
4539
4540 for (uint32_t v = 0; v < modules->count; ++v) {
4541 mc = modules->objs[v];
4542 tmpwr = (v == modules->count - 1) ? wr : trp_wrapper_set_mark_top(wr);
4543 trb_print_family_tree(tmpwr, &mc->pc, &mc->tc);
4544 }
4545
4546 for (uint32_t pr = 0; refs && pr < refs->count; pr++) {
4547 trm_lysc_tree_ctx(refs->snodes[pr]->module, pc->out, pc->max_line_length, &tmppc, &tmptc);
4548 tmptc.mounted = 1;
4549 tmptc.parent_refs = refs;
4550 trb_print_parents(refs->snodes[pr], &tmpwr, pc, &tmptc);
4551 }
4552
4553out:
4554 ly_set_free(modules, free);
4555 ly_set_free(refs, NULL);
4556out_ctx:
4557 ly_ctx_destroy(ext_ctx);
4558 return err;
4559}