blob: d0d10244098328b1196e3b3a720613182acf7a9e [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;
2569
2570 return ret;
2571}
2572
aPiecekef1e58e2021-04-19 13:19:44 +02002573/**********************************************************************
2574 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002575 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002576
2577/**
aPiecek61d062b2020-11-02 11:05:09 +01002578 * @brief Check if list statement has keys.
2579 * @param[in] pn is pointer to the list.
2580 * @return 1 if has keys, otherwise 0.
2581 */
2582static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002583trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002584{
aPiecekef1e58e2021-04-19 13:19:44 +02002585 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002586}
2587
2588/**
2589 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002590 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002591 * @return 1 if has if-features, otherwise 0.
2592 */
2593static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002594trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002595{
2596 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002597 const struct lysp_qname *iffs;
2598
aPiecek61d062b2020-11-02 11:05:09 +01002599 ly_bool ret = 0;
2600
aPiecekef1e58e2021-04-19 13:19:44 +02002601 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002602 LY_ARRAY_FOR(iffs, u) {
2603 ret = 1;
2604 break;
2605 }
2606 return ret;
2607}
2608
2609/**
2610 * @brief Find out if leaf is also the key in last list.
2611 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002612 * @param[in] ca_last_list is pointer to last visited list.
2613 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002614 * @return 1 if leaf is also the key, otherwise 0.
2615 */
2616static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002617trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002618{
2619 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2620 const struct lysp_node_list *list = ca_last_list;
2621
2622 if (!list) {
2623 return 0;
2624 }
2625 return trg_charptr_has_data(list->key) ?
2626 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2627}
2628
2629/**
2630 * @brief Check if container's type is presence.
2631 * @param[in] pn is pointer to container.
2632 * @return 1 if container has presence statement, otherwise 0.
2633 */
2634static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002635trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002636{
2637 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2638}
2639
2640/**
ekinzie0ab8b302022-10-10 03:03:57 -04002641 * @brief Check if container has yangmt:mount-point extension.
2642 * @param[in] cn is pointer to container or list.
2643 * @param[out] mount is assigned a pointer to the extension instance, if found
2644 * @return 1 if container has mount-point.
2645 */
2646static ly_bool
2647troc_node_has_mount(const struct lysc_node *cn, struct lysc_ext_instance **mount)
2648{
2649 struct lysc_ext_instance *ext;
2650 uint64_t u;
2651
2652 /* The schema-mount extension plugin has already made sure that
2653 * there is only one mount-point here.
2654 */
2655 LY_ARRAY_FOR(cn->exts, u) {
2656 ext = &cn->exts[u];
2657 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") ||
2658 strcmp(ext->def->name, "mount-point")) {
2659 continue;
2660 }
2661 *mount = ext;
2662 return 1;
2663 }
2664 return 0;
2665}
2666
2667static ly_bool
2668trop_node_has_mount(const struct lysp_node *pn)
2669{
2670 struct lysp_ext_instance *ext;
2671 uint64_t u;
2672
2673 LY_ARRAY_FOR(pn->exts, u) {
2674 ext = &pn->exts[u];
2675 if (strcmp(ext->name, "yangmnt:mount-point")) {
2676 continue;
2677 }
2678 return 1;
2679 }
2680 return 0;
2681}
2682
2683/**
aPiecek61d062b2020-11-02 11:05:09 +01002684 * @brief Get leaflist's path without lysp_node type control.
2685 * @param[in] pn is pointer to the leaflist.
2686 */
2687static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002688trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002689{
2690 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2691
2692 return list->type.path ? list->type.path->expr : NULL;
2693}
2694
2695/**
2696 * @brief Get leaflist's type name without lysp_node type control.
2697 * @param[in] pn is pointer to the leaflist.
2698 */
2699static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002700trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002701{
2702 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2703
2704 return list->type.name;
2705}
2706
2707/**
2708 * @brief Get leaf's path without lysp_node type control.
2709 * @param[in] pn is pointer to the leaf node.
2710 */
2711static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002712trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002713{
2714 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2715
2716 return leaf->type.path ? leaf->type.path->expr : NULL;
2717}
2718
2719/**
2720 * @brief Get leaf's type name without lysp_node type control.
2721 * @param[in] pn is pointer to the leaf's type name.
2722 */
2723static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002724trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002725{
2726 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2727
2728 return leaf->type.name;
2729}
2730
2731/**
aPiecek874ea4d2021-04-19 12:26:36 +02002732 * @brief Get pointer to data using node type specification
2733 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002734 *
aPiecek874ea4d2021-04-19 12:26:36 +02002735 * @param[in] flags is node type specification.
2736 * If it is the correct node, the getter function is called.
2737 * @param[in] f is getter function which provides the desired
2738 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002739 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002740 * @return NULL if node has wrong type or getter function return
2741 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002742 * @return Pointer to desired char pointer obtained from the node.
2743 */
2744static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002745trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002746{
2747 if (pn->nodetype & flags) {
2748 const char *ret = f(pn);
Michal Vasko26bbb272022-08-02 14:54:33 +02002749
aPiecek61d062b2020-11-02 11:05:09 +01002750 return trg_charptr_has_data(ret) ? ret : NULL;
2751 } else {
2752 return NULL;
2753 }
2754}
2755
2756/**
aPiecek61d062b2020-11-02 11:05:09 +01002757 * @brief Resolve \<status\> of the current node.
2758 * @param[in] nodetype is node's type obtained from the tree.
2759 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002760 * @param[in] ca_lys_status is inherited status
2761 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002762 * @return The status type.
2763 */
2764static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002765trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002766{
2767 /* LYS_INPUT and LYS_OUTPUT is special case */
2768 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002769 return tro_flags2status(ca_lys_status);
2770 /* if ancestor's status is deprc or obslt
2771 * and also node's status is not set
2772 */
aPiecek61d062b2020-11-02 11:05:09 +01002773 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2774 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002775 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002776 } else {
2777 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002778 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002779 }
2780}
2781
2782/**
2783 * @brief Resolve \<flags\> of the current node.
2784 * @param[in] nodetype is node's type obtained from the tree.
2785 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002786 * @param[in] ca_ancestor is ancestor type obtained
2787 * from trt_parent_cache.
2788 * @param[in] ca_lys_config is inherited config item
2789 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002790 * @return The flags type.
2791 */
2792static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002793trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002794{
2795 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2796 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2797 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2798 return TRD_FLAGS_TYPE_RO;
2799 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2800 return TRD_FLAGS_TYPE_RO;
2801 } else if (nodetype & LYS_NOTIF) {
2802 return TRD_FLAGS_TYPE_NOTIF;
2803 } else if (nodetype & LYS_USES) {
2804 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2805 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2806 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002807 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002808 /* config is not set. Look at ancestor's config */
2809 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002810 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002811 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002812 }
2813}
2814
2815/**
2816 * @brief Resolve node type of the current node.
2817 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002818 * @param[in] ca_last_list is pointer to the last visited list.
2819 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002820 */
2821static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04002822trop_resolve_node_type(const struct trt_tree_ctx *tc, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002823{
ekinzie0ab8b302022-10-10 03:03:57 -04002824 const struct lysp_node *pn = tc->pn;
2825
aPiecek61d062b2020-11-02 11:05:09 +01002826 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2827 return TRD_NODE_ELSE;
2828 } else if (pn->nodetype & LYS_CASE) {
2829 return TRD_NODE_CASE;
2830 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2831 return TRD_NODE_OPTIONAL_CHOICE;
2832 } else if (pn->nodetype & LYS_CHOICE) {
2833 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04002834 } else if (tc->mounted && (tc->pn->parent == NULL)) {
2835 if (tc->parent_refs) {
2836 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
2837 if (!strcmp(tc->pmod->mod->ns, tc->parent_refs->snodes[v]->module->ns)) {
2838 return TRD_NODE_TOP_LEVEL2;
2839 }
2840 }
2841 }
2842 return TRD_NODE_TOP_LEVEL1;
aPiecekef1e58e2021-04-19 13:19:44 +02002843 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002844 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002845 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002846 return TRD_NODE_KEYS;
2847 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2848 return TRD_NODE_LISTLEAFLIST;
2849 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2850 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002851 } 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 +01002852 return TRD_NODE_OPTIONAL;
2853 } else {
2854 return TRD_NODE_ELSE;
2855 }
2856}
2857
2858/**
aPiecekef1e58e2021-04-19 13:19:44 +02002859 * @brief Resolve \<type\> of the current node.
2860 * @param[in] pn is current node.
2861 */
2862static struct trt_type
2863trop_resolve_type(const struct lysp_node *pn)
2864{
2865 const char *tmp = NULL;
2866
2867 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2868 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2869 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2870 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2871 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2872 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2873 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2874 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2875 } else if (pn->nodetype == LYS_ANYDATA) {
2876 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2877 } else if (pn->nodetype & LYS_ANYXML) {
2878 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2879 } else {
2880 return TRP_EMPTY_TRT_TYPE;
2881 }
2882}
2883
2884/**
aPiecek61d062b2020-11-02 11:05:09 +01002885 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002886 * @param[in] ca contains stored important data
2887 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002888 * @param[in] tc is context of the tree.
2889 */
2890static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002891trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002892{
aPiecekef1e58e2021-04-19 13:19:44 +02002893 const struct lysp_node *pn;
2894 struct trt_node ret;
2895
aPiecek61d062b2020-11-02 11:05:09 +01002896 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002897
2898 pn = tc->pn;
2899 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002900
2901 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002902 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002903
aPiecek61d062b2020-11-02 11:05:09 +01002904 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04002905 if (trop_node_has_mount(pn)) {
2906 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
2907 } else {
2908 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
2909 }
aPiecek61d062b2020-11-02 11:05:09 +01002910
aPiecek61d062b2020-11-02 11:05:09 +01002911 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04002912 ret.name.type = trop_resolve_node_type(tc, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002913
aPiecek34fa3772021-05-21 12:35:46 +02002914 /* The parsed tree is not compiled, so no node can be augmented
2915 * from another module. This means that nodes from the parsed tree
2916 * will never have the prefix.
2917 */
aPiecek61d062b2020-11-02 11:05:09 +01002918 ret.name.module_prefix = NULL;
2919
2920 /* set node's name */
2921 ret.name.str = pn->name;
2922
2923 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002924 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002925
2926 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002927 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002928
aPiecek3f247652021-04-19 13:40:25 +02002929 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002930
2931 return ret;
2932}
2933
aPiecekef1e58e2021-04-19 13:19:44 +02002934/**
2935 * @brief Find out if the current node has siblings.
2936 * @param[in] tc is context of the tree.
2937 * @return 1 if sibling exists otherwise 0.
2938 */
2939static ly_bool
2940trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2941{
aPiecek3f247652021-04-19 13:40:25 +02002942 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002943}
2944
aPiecek96baa7f2021-04-23 12:32:00 +02002945/**
2946 * @brief Print all yang-data sections and print three dots instead
2947 * of nodes.
2948 * @param[in] exts is array of YANG extension instances from parsed
2949 * module (@ref sizedarrays).
2950 * @param[in] mll is maximum number of characters that can be printed
2951 * on one line.
2952 * @param[in,out] out is output handler.
2953 */
2954static void
2955trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2956{
2957 struct trt_keyword_stmt ks;
2958 LY_ARRAY_COUNT_TYPE u;
2959 struct trt_wrapper wr;
2960
2961 if (!exts) {
2962 return;
2963 }
2964
2965 ly_print_(out, "\n");
2966 ks.type = TRD_KEYWORD_YANG_DATA;
2967 wr = TRP_INIT_WRAPPER_BODY;
2968
2969 LY_ARRAY_FOR(exts, u) {
2970 ly_print_(out, "\n");
2971
2972 /* yang-data <yang-data-name>: */
2973 ks.str = exts[u].argument;
2974 trp_print_keyword_stmt(ks, mll, 0, out);
2975 ly_print_(out, "\n");
2976
2977 /* ... */
2978 trp_print_wrapper(wr, out);
2979 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2980 }
2981}
2982
aPiecek874ea4d2021-04-19 12:26:36 +02002983/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002984 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002985 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002986
2987/**
aPiecek874ea4d2021-04-19 12:26:36 +02002988 * @brief Change current node pointer to its parent
2989 * but only if parent exists.
2990 * @param[in,out] tc is tree context.
2991 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002992 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002993 * @return 0 if the node did not have parents.
2994 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002995 */
2996static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002997trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002998{
2999 assert(tc && tc->pn);
3000 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02003001 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01003002 tc->pn = tc->pn->parent;
3003 return 1;
3004 } else {
3005 return 0;
3006 }
3007}
3008
3009/**
aPiecek874ea4d2021-04-19 12:26:36 +02003010 * @brief Change the current node pointer to its child
3011 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003012 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003013 * @param[in,out] tc is context of the tree.
3014 * Contains pointer to the current node.
3015 * @return Non-empty \<node\> representation of the current
3016 * node's child. The @p tc is modified.
3017 * @return Empty \<node\> representation if child don't exists.
3018 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003019 */
3020static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003021trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003022{
aPiecekef1e58e2021-04-19 13:19:44 +02003023 const struct lysp_node *tmp;
3024
aPiecek61d062b2020-11-02 11:05:09 +01003025 assert(tc && tc->pn);
3026
aPiecek3f247652021-04-19 13:40:25 +02003027 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02003028 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02003029 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01003030 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02003031 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01003032 }
3033}
3034
3035/**
aPiecek874ea4d2021-04-19 12:26:36 +02003036 * @brief Change the current node pointer to the first child of node's
3037 * parent. If current node is already first sibling/child then nothing
3038 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01003039 * @param[in,out] tc is tree context.
3040 */
3041static void
aPiecekef1e58e2021-04-19 13:19:44 +02003042trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003043{
aPiecek9f792e52021-04-21 08:33:56 +02003044 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01003045
aPiecekef1e58e2021-04-19 13:19:44 +02003046 if (trop_modi_parent(tc)) {
3047 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003048 } else {
3049 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01003050 switch (tc->section) {
3051 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003052 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02003053 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003054 break;
3055 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02003056 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02003057 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003058 break;
3059 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003060 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02003061 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003062 break;
3063 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003064 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02003065 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003066 break;
3067 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02003068 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02003069 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003070 break;
3071 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003072 /* tpn in this case is of type lysp_ext_instance */
3073 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01003074 break;
aPiecek96baa7f2021-04-23 12:32:00 +02003075 default:
3076 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01003077 }
aPiecek61d062b2020-11-02 11:05:09 +01003078 }
3079}
3080
3081/**
aPiecek874ea4d2021-04-19 12:26:36 +02003082 * @brief Change the pointer to the current node to its next sibling
3083 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003084 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003085 * @param[in,out] tc is tree context.
3086 * Contains pointer to the current node.
3087 * @return Non-empty \<node\> representation if sibling exists.
3088 * The @p tc is modified.
3089 * @return Empty \<node\> representation otherwise.
3090 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003091 */
3092static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003093trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003094{
aPiecekef1e58e2021-04-19 13:19:44 +02003095 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003096
3097 assert(tc && tc->pn);
3098
aPiecek3f247652021-04-19 13:40:25 +02003099 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01003100
aPiecekef1e58e2021-04-19 13:19:44 +02003101 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003102 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3103 tc->tpn = pn;
3104 }
aPiecekef1e58e2021-04-19 13:19:44 +02003105 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003106 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003107 } else {
3108 return TRP_EMPTY_NODE;
3109 }
3110}
3111
3112/**
3113 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003114 * @param[in,out] tc is tree context. It is modified and his current
3115 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003116 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003117 * @return Empty section structure otherwise.
3118 */
3119static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003120trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003121{
aPiecek9f792e52021-04-21 08:33:56 +02003122 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003123 const struct lysp_node_augment *augs;
3124
3125 /* if next_augment func was called for the first time */
3126 if (tc->section != TRD_SECT_AUGMENT) {
3127 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003128 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003129 } else {
3130 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003131 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003132 }
3133
aPiecekdc8fd572021-04-19 10:47:23 +02003134 if (augs) {
3135 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003136 tc->tpn = tc->pn;
3137 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3138 } else {
3139 return TRP_EMPTY_KEYWORD_STMT;
3140 }
3141}
3142
3143/**
aPiecek61d062b2020-11-02 11:05:09 +01003144 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003145 * @param[in,out] tc is tree context. It is modified and his current
3146 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003147 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003148 * @return Empty section representation otherwise.
3149 */
3150static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003151trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003152{
aPiecek9f792e52021-04-21 08:33:56 +02003153 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003154 const struct lysp_node_grp *grps;
3155
3156 if (tc->section != TRD_SECT_GROUPING) {
3157 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003158 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003159 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003160 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003161 }
3162
aPiecekdc8fd572021-04-19 10:47:23 +02003163 if (grps) {
3164 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003165 tc->tpn = tc->pn;
3166 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3167 } else {
3168 return TRP_EMPTY_KEYWORD_STMT;
3169 }
3170}
3171
aPiecek874ea4d2021-04-19 12:26:36 +02003172/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003173 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003174 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003175
3176/**
aPiecek3f247652021-04-19 13:40:25 +02003177 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003178 */
aPiecek3f247652021-04-19 13:40:25 +02003179static ly_bool
3180troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003181{
aPiecek3f247652021-04-19 13:40:25 +02003182 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3183}
aPiecek61d062b2020-11-02 11:05:09 +01003184
aPiecek3f247652021-04-19 13:40:25 +02003185/**
3186 * @brief Resolve \<flags\> of the current node.
3187 *
3188 * Use this function only if trt_tree_ctx.lysc_tree is true.
3189 *
3190 * @param[in] nodetype is current lysc_node.nodetype.
3191 * @param[in] flags is current lysc_node.flags.
3192 * @return The flags type.
3193 */
3194static trt_flags_type
3195troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3196{
3197 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3198 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3199 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3200 return TRD_FLAGS_TYPE_RO;
3201 } else if (nodetype & LYS_IS_NOTIF) {
3202 return TRD_FLAGS_TYPE_RO;
3203 } else if (nodetype & LYS_NOTIF) {
3204 return TRD_FLAGS_TYPE_NOTIF;
3205 } else if (nodetype & LYS_USES) {
3206 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3207 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3208 return TRD_FLAGS_TYPE_RPC;
3209 } else {
3210 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003211 }
aPiecek61d062b2020-11-02 11:05:09 +01003212}
3213
3214/**
aPiecek3f247652021-04-19 13:40:25 +02003215 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003216 *
aPiecek3f247652021-04-19 13:40:25 +02003217 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003218 *
aPiecek3f247652021-04-19 13:40:25 +02003219 * @param[in] nodetype is current lysc_node.nodetype.
3220 * @param[in] flags is current lysc_node.flags.
3221 */
3222static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04003223troc_resolve_node_type(const struct trt_tree_ctx *tc, uint16_t nodetype, uint16_t flags)
aPiecek3f247652021-04-19 13:40:25 +02003224{
3225 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3226 return TRD_NODE_ELSE;
3227 } else if (nodetype & LYS_CASE) {
3228 return TRD_NODE_CASE;
3229 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3230 return TRD_NODE_OPTIONAL_CHOICE;
3231 } else if (nodetype & LYS_CHOICE) {
3232 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04003233 } else if (tc->mounted && (tc->cn->parent == NULL)) {
3234 if (tc->parent_refs) {
3235 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
3236 if (!strcmp(tc->cn->module->ns, tc->parent_refs->snodes[v]->module->ns)) {
3237 return TRD_NODE_TOP_LEVEL2;
3238 }
3239 }
3240 }
3241 return TRD_NODE_TOP_LEVEL1;
aPiecek3f247652021-04-19 13:40:25 +02003242 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3243 return TRD_NODE_CONTAINER;
3244 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3245 return TRD_NODE_KEYS;
3246 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3247 return TRD_NODE_LISTLEAFLIST;
3248 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3249 return TRD_NODE_OPTIONAL;
3250 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3251 return TRD_NODE_OPTIONAL;
3252 } else {
3253 return TRD_NODE_ELSE;
3254 }
3255}
3256
3257/**
aPiecek34fa3772021-05-21 12:35:46 +02003258 * @brief Resolve prefix (<prefix>:<name>) of node that has been
3259 * placed from another module via an augment statement.
3260 *
3261 * @param[in] cn is current compiled node.
3262 * @param[in] current_compiled_module is module whose nodes are
3263 * currently being printed.
3264 * @return Prefix of foreign module or NULL.
3265 */
3266static const char *
3267troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
3268{
3269 const struct lys_module *node_module;
3270 const char *ret = NULL;
3271
3272 node_module = cn->module;
3273 if (node_module->compiled != current_compiled_module) {
3274 ret = node_module->prefix;
3275 }
3276
3277 return ret;
3278}
3279
3280/**
aPiecek3f247652021-04-19 13:40:25 +02003281 * @brief Transformation of current lysc_node to struct trt_node.
3282 * @param[in] ca is not used.
3283 * @param[in] tc is context of the tree.
3284 */
3285static struct trt_node
3286troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3287{
3288 (void) ca;
3289 const struct lysc_node *cn;
3290 struct trt_node ret;
3291
aPiecek082c7dc2021-05-20 08:55:07 +02003292 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003293
3294 cn = tc->cn;
3295 ret = TRP_EMPTY_NODE;
3296
3297 /* <status> */
3298 ret.status = tro_flags2status(cn->flags);
3299
aPiecek3f247652021-04-19 13:40:25 +02003300 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04003301 if (troc_node_has_mount(cn, &ret.mount)) {
3302 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
3303 } else {
3304 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3305 }
aPiecek3f247652021-04-19 13:40:25 +02003306
aPiecek3f247652021-04-19 13:40:25 +02003307 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04003308 ret.name.type = troc_resolve_node_type(tc, cn->nodetype, cn->flags);
aPiecek3f247652021-04-19 13:40:25 +02003309
aPiecek34fa3772021-05-21 12:35:46 +02003310 /* <prefix> */
3311 ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
aPiecek3f247652021-04-19 13:40:25 +02003312
3313 /* set node's name */
3314 ret.name.str = cn->name;
3315
aPiecekbbc02932021-05-21 07:19:41 +02003316 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003317 /* <type> */
3318 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003319
aPiecek082c7dc2021-05-20 08:55:07 +02003320 /* <iffeature> */
3321 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3322 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003323 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003324 assert(tc->cn->nodetype & LYS_CASE);
3325
3326 /* <type> */
3327 ret.type = TRP_EMPTY_TRT_TYPE;
3328
aPiecek7a28e2f2021-05-21 07:27:03 +02003329 /* <iffeature> */
3330 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003331 }
aPiecek3f247652021-04-19 13:40:25 +02003332
3333 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3334
3335 return ret;
3336}
3337
3338/**********************************************************************
3339 * Modify troc getters
3340 *********************************************************************/
3341
3342/**
aPiecek01598c02021-04-23 14:18:24 +02003343 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003344 */
3345static ly_bool
3346troc_modi_parent(struct trt_tree_ctx *tc)
3347{
3348 assert(tc && tc->cn);
3349 /* If no parent exists, stay in actual node. */
3350 if (tc->cn->parent) {
3351 tc->cn = tc->cn->parent;
3352 return 1;
3353 } else {
3354 return 0;
3355 }
3356}
3357
3358/**
aPiecek01598c02021-04-23 14:18:24 +02003359 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003360 */
3361static struct trt_node
3362troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3363{
3364 const struct lysc_node *cn;
3365
3366 assert(tc && tc->cn);
3367
3368 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3369
3370 /* if next sibling exists */
3371 if (cn) {
3372 /* update trt_tree_ctx */
3373 tc->cn = cn;
3374 return troc_read_node(ca, tc);
3375 } else {
3376 return TRP_EMPTY_NODE;
3377 }
3378}
3379
3380/**
3381 * @copydoc trop_modi_next_child()
3382 */
3383static struct trt_node
3384troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3385{
3386 const struct lysc_node *tmp;
3387
3388 assert(tc && tc->cn);
3389
3390 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3391 tc->cn = tmp;
3392 return troc_read_node(ca, tc);
3393 } else {
3394 return TRP_EMPTY_NODE;
3395 }
3396}
3397
3398/**
aPiecek01598c02021-04-23 14:18:24 +02003399 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003400 */
3401static void
aPiecek3f247652021-04-19 13:40:25 +02003402troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003403{
aPiecek3f247652021-04-19 13:40:25 +02003404 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003405
aPiecek3f247652021-04-19 13:40:25 +02003406 if (troc_modi_parent(tc)) {
3407 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3408 } else {
3409 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003410 switch (tc->section) {
3411 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003412 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003413 break;
3414 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003415 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003416 break;
3417 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003418 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003419 break;
3420 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003421 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003422 break;
3423 default:
3424 assert(0);
3425 }
aPiecek61d062b2020-11-02 11:05:09 +01003426 }
3427}
3428
aPiecek874ea4d2021-04-19 12:26:36 +02003429/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003430 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003431 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003432
3433/**
3434 * @brief Get size of node name.
3435 * @param[in] name contains name and mark.
3436 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003437 * @return negative value as an indication that option mark
3438 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003439 */
3440static int32_t
3441trb_strlen_of_name_and_mark(struct trt_node_name name)
3442{
3443 size_t name_len = strlen(name.str);
3444
3445 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3446 /* counting also parentheses */
3447 name_len += 2;
3448 }
3449
3450 return trp_mark_is_used(name) ?
3451 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3452 (int32_t)name_len;
3453}
3454
3455/**
aPiecek874ea4d2021-04-19 12:26:36 +02003456 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3457 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003458 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003459 * @param[in] max_len4all is the maximum value of btw_opts_type
3460 * that it can have.
3461 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003462 */
3463static int16_t
3464trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3465{
3466 int32_t name_len;
3467 int16_t min_len;
3468 int16_t ret;
3469
3470 name_len = trb_strlen_of_name_and_mark(name);
3471
3472 /* negative value indicate that in name is some opt mark */
3473 min_len = name_len < 0 ?
3474 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3475 TRD_INDENT_BEFORE_TYPE;
3476 ret = abs(max_len4all) - abs(name_len);
3477
3478 /* correction -> negative indicate that name is too long. */
3479 return ret < 0 ? min_len : ret;
3480}
3481
3482/**
3483 * @brief Print node.
3484 *
aPiecek01598c02021-04-23 14:18:24 +02003485 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003486 * But difference is that take @p max_gap_before_type which will be
3487 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003488 *
aPiecek9bdd7592021-05-20 08:13:20 +02003489 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003490 * @param[in] max_gap_before_type is number of indent before \<type\>.
3491 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003492 * @param[in] pc contains mainly functions for printing.
3493 * @param[in] tc is tree context.
3494 */
3495static void
aPiecek9bdd7592021-05-20 08:13:20 +02003496trb_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 +01003497{
aPiecek61d062b2020-11-02 11:05:09 +01003498 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3499
3500 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3501 /* print actual node with unified indent */
3502 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3503 }
3504 /* after -> print actual node with default indent */
3505 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3506 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
ekinzie0ab8b302022-10-10 03:03:57 -04003507 if (node.flags == TRD_FLAGS_TYPE_MOUNT_POINT) {
3508 struct trt_wrapper wr_mount;
3509 struct tro_getters get;
3510
3511 wr_mount = pc->fp.read.if_sibling_exists(tc) ?
3512 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3513
3514 get = tc->lysc_tree ? troc_init_getters() : trop_init_getters();
3515 if (get.child(tc->lysc_tree ? (void *)tc->cn : (void *)tc->pn)) {
3516 /* If this node has a child, we need to draw a vertical line
3517 * from the last mounted module to the first child
3518 */
3519 wr_mount = trp_wrapper_set_mark_top(wr_mount);
3520 }
3521
3522 trb_print_mount_point(&node, wr_mount, pc, tc);
3523 }
aPiecek61d062b2020-11-02 11:05:09 +01003524}
3525
3526/**
aPiecek874ea4d2021-04-19 12:26:36 +02003527 * @brief Check if parent of the current node is the last
3528 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003529 *
aPiecek874ea4d2021-04-19 12:26:36 +02003530 * To mantain stability use this function only if the current node is
3531 * the first of the siblings.
3532 * Side-effect -> current node is set to the first sibling
3533 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003534 *
aPiecek01598c02021-04-23 14:18:24 +02003535 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003536 * @param[in,out] tc is tree context.
3537 * @return 1 if parent is last sibling otherwise 0.
3538 */
3539static ly_bool
3540trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3541{
3542 if (fp.modify.parent(tc)) {
3543 ly_bool ret = fp.read.if_sibling_exists(tc);
Michal Vasko26bbb272022-08-02 14:54:33 +02003544
aPiecek61d062b2020-11-02 11:05:09 +01003545 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3546 return !ret;
3547 } else {
3548 return !fp.read.if_sibling_exists(tc);
3549 }
3550}
3551
3552/**
3553 * @brief Find sibling with the biggest node name and return that size.
3554 *
3555 * Side-effect -> Current node is set to the first sibling.
3556 *
3557 * @param[in] ca contains inherited data from ancestors.
3558 * @param[in] pc contains mainly functions for printing.
3559 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003560 * @return positive number as a sign that only the node name is
3561 * included in the size.
3562 * @return negative number sign that node name and his opt mark is
3563 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003564 */
3565static int32_t
3566trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3567{
3568 int32_t ret = 0;
3569
3570 pc->fp.modify.first_sibling(tc);
3571
3572 for (struct trt_node node = pc->fp.read.node(ca, tc);
3573 !trp_node_is_empty(node);
3574 node = pc->fp.modify.next_sibling(ca, tc)) {
3575 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
Michal Vasko26bbb272022-08-02 14:54:33 +02003576
aPiecek61d062b2020-11-02 11:05:09 +01003577 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3578 }
3579 pc->fp.modify.first_sibling(tc);
3580 return ret;
3581}
3582
3583/**
aPiecek874ea4d2021-04-19 12:26:36 +02003584 * @brief Find maximal indent between
3585 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003586 *
3587 * Side-effect -> Current node is set to the first sibling.
3588 *
3589 * @param[in] ca contains inherited data from ancestors.
3590 * @param[in] pc contains mainly functions for printing.
3591 * @param[in,out] tc is tree context.
3592 * @return max btw_opts_type value for rest of the siblings
3593 */
3594static int16_t
3595trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3596{
3597 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3598 int16_t ind_before_type = maxlen_node_name < 0 ?
3599 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3600 TRD_INDENT_BEFORE_TYPE;
3601
3602 return abs(maxlen_node_name) + ind_before_type;
3603}
3604
3605/**
aPiecek874ea4d2021-04-19 12:26:36 +02003606 * @brief Find out if it is possible to unify
3607 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003608 *
aPiecek874ea4d2021-04-19 12:26:36 +02003609 * The goal is for all node siblings to have the same alignment
3610 * for \<type\> as if they were in a column. All siblings who cannot
3611 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003612 * Side-effect -> Current node is set to the first sibling.
3613 *
3614 * @param[in] ca contains inherited data from ancestors.
3615 * @param[in] pc contains mainly functions for printing.
3616 * @param[in,out] tc is tree context.
3617 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003618 * @return positive number indicating the maximum number of spaces
3619 * before \<type\> if the length of the node name is 0. To calculate
3620 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003621 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003622*/
3623static uint32_t
3624trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3625{
3626 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3627}
3628
3629/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003630 * @brief Check if there is no case statement
3631 * under the choice statement.
3632 *
3633 * It can return true only if the Parsed schema tree
3634 * is used for browsing.
3635 *
3636 * @param[in] tc is tree context.
3637 * @return 1 if implicit case statement is present otherwise 0.
3638 */
3639static ly_bool
3640trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3641{
3642 return !tc->lysc_tree && tc->pn->parent &&
3643 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3644 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3645 LYS_LEAF | LYS_LEAFLIST));
3646}
3647
3648static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3649 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3650
3651/**
3652 * @brief Print implicit case node and his subtree.
3653 *
3654 * @param[in] node is child of implicit case.
3655 * @param[in] wr is wrapper for printing identation before node.
3656 * @param[in] ca contains inherited data from ancestors.
3657 * @param[in] pc contains mainly functions for printing.
3658 * @param[in] tc is tree context. Its settings should be the same as
3659 * before the function call.
3660 */
3661static void
3662trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3663 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3664{
3665 struct trt_node case_node;
3666 struct trt_wrapper wr_case_child;
3667
3668 case_node = tro_create_implicit_case_node(node);
3669 ly_print_(pc->out, "\n");
3670 trb_print_entire_node(case_node, 0, wr, pc, tc);
3671 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3672 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3673 ly_print_(pc->out, "\n");
3674 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3675}
3676
3677/**
aPiecek874ea4d2021-04-19 12:26:36 +02003678 * @brief For the current node: recursively print all of its child
3679 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003680 *
aPiecek01598c02021-04-23 14:18:24 +02003681 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003682 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003683 * Nodes are printed, including unified sibling node alignment
3684 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003685 * Side-effect -> current node is set to the last sibling.
3686 *
3687 * @param[in] wr is wrapper for printing identation before node.
3688 * @param[in] ca contains inherited data from ancestors.
3689 * @param[in] pc contains mainly functions for printing.
3690 * @param[in,out] tc is tree context.
3691 */
3692static void
3693trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3694{
3695 uint32_t max_gap_before_type;
3696 ly_bool sibling_flag = 0;
3697 ly_bool child_flag = 0;
3698
3699 /* if node is last sibling, then do not add '|' to wrapper */
3700 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3701 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3702
3703 /* try unified indentation in node */
3704 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3705
3706 /* print all siblings */
3707 do {
3708 struct trt_parent_cache new_ca;
3709 struct trt_node node;
Michal Vasko26bbb272022-08-02 14:54:33 +02003710
aPiecek9bdd7592021-05-20 08:13:20 +02003711 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003712
aPiecekb8d5a0a2021-05-20 08:20:24 +02003713 if (!trb_need_implicit_node_case(tc)) {
3714 /* normal behavior */
3715 ly_print_(pc->out, "\n");
3716 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3717 new_ca = tro_parent_cache_for_child(ca, tc);
3718 /* go to the actual node's child or stay in actual node */
3719 node = pc->fp.modify.next_child(ca, tc);
3720 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003721
aPiecekb8d5a0a2021-05-20 08:20:24 +02003722 if (child_flag) {
3723 /* print all childs - recursive call */
3724 trb_print_nodes(wr, new_ca, pc, tc);
3725 /* get back from child node to actual node */
3726 pc->fp.modify.parent(tc);
3727 }
3728 } else {
3729 /* The case statement is omitted (shorthand).
3730 * Print implicit case node and his subtree.
3731 */
3732 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003733 }
3734
3735 /* go to the actual node's sibling */
3736 node = pc->fp.modify.next_sibling(ca, tc);
3737 sibling_flag = !trp_node_is_empty(node);
3738
3739 /* go to the next sibling or stay in actual node */
3740 } while (sibling_flag);
3741}
3742
3743/**
aPiecek153b00f2021-04-20 13:52:57 +02003744 * @brief Calculate the wrapper about how deep in the tree the node is.
ekinzie0ab8b302022-10-10 03:03:57 -04003745 * @param[in] wr_in A wrapper to use as a starting point
aPiecek153b00f2021-04-20 13:52:57 +02003746 * @param[in] node from which to count.
3747 * @return wrapper for @p node.
3748 */
3749static struct trt_wrapper
ekinzie0ab8b302022-10-10 03:03:57 -04003750trb_count_depth(const struct trt_wrapper *wr_in, const struct lysc_node *node)
aPiecek153b00f2021-04-20 13:52:57 +02003751{
ekinzie0ab8b302022-10-10 03:03:57 -04003752 struct trt_wrapper wr = wr_in ? *wr_in : TRP_INIT_WRAPPER_TOP;
aPiecek153b00f2021-04-20 13:52:57 +02003753 const struct lysc_node *parent;
3754
3755 if (!node) {
3756 return wr;
3757 }
3758
3759 for (parent = node->parent; parent; parent = parent->parent) {
3760 wr = trp_wrapper_set_shift(wr);
3761 }
3762
3763 return wr;
3764}
3765
3766/**
3767 * @brief Print all parent nodes of @p node and the @p node itself.
3768 *
3769 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3770 *
3771 * @param[in] node on which the function is focused.
aPiecek01598c02021-04-23 14:18:24 +02003772 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003773 * @param[in,out] tc is context of tree printer.
3774 * @return wrapper for @p node.
3775 */
3776static void
ekinzie0ab8b302022-10-10 03:03:57 -04003777trb_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 +02003778{
3779 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003780 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003781
3782 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3783
3784 /* stop recursion */
3785 if (!node) {
3786 return;
3787 }
ekinzie0ab8b302022-10-10 03:03:57 -04003788 trb_print_parents(node->parent, wr_in, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003789
3790 /* setup for printing */
3791 tc->cn = node;
ekinzie0ab8b302022-10-10 03:03:57 -04003792 wr = trb_count_depth(wr_in, node);
aPiecek153b00f2021-04-20 13:52:57 +02003793
3794 /* print node */
3795 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003796 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3797 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003798}
3799
3800/**
aPiecekdc8fd572021-04-19 10:47:23 +02003801 * @brief Get address of the current node.
3802 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003803 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003804 */
3805static const void *
3806trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3807{
aPiecek3f247652021-04-19 13:40:25 +02003808 return tc->lysc_tree ?
3809 (const void *)tc->cn :
3810 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003811}
3812
3813/**
3814 * @brief Get address of current node's child.
3815 * @param[in,out] tc contains current node.
3816 */
3817static const void *
3818trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3819{
3820 if (!trb_tree_ctx_get_node(tc)) {
3821 return NULL;
3822 }
3823
aPiecek3f247652021-04-19 13:40:25 +02003824 if (tc->lysc_tree) {
3825 return lysc_node_child(tc->cn);
3826 } else {
3827 return lysp_node_child(tc->pn);
3828 }
aPiecekdc8fd572021-04-19 10:47:23 +02003829}
3830
3831/**
3832 * @brief Set current node on its child.
3833 * @param[in,out] tc contains current node.
3834 */
3835static void
3836trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3837{
aPiecek3f247652021-04-19 13:40:25 +02003838 const void *node = trb_tree_ctx_get_child(tc);
3839
3840 if (tc->lysc_tree) {
3841 tc->cn = node;
3842 } else {
3843 tc->pn = node;
3844 }
aPiecekdc8fd572021-04-19 10:47:23 +02003845}
3846
3847/**
aPiecek61d062b2020-11-02 11:05:09 +01003848 * @brief Print subtree of nodes.
3849 *
3850 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003851 * Before root node is no linebreak printing. This must be addressed by
3852 * the caller. Root node will also be printed. Behind last printed node
3853 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003854 *
aPiecek9bdd7592021-05-20 08:13:20 +02003855 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003856 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003857 * ::trb_try_unified_indent() function for root node.
3858 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003859 * @param[in] wr is wrapper saying how deep in the whole tree
3860 * is the root of the subtree.
3861 * @param[in] ca is parent_cache from root's parent.
3862 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003863 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003864 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003865 */
3866static void
aPiecek9bdd7592021-05-20 08:13:20 +02003867trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3868 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003869{
3870 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003871
aPiecek9bdd7592021-05-20 08:13:20 +02003872 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
ekinzie0ab8b302022-10-10 03:03:57 -04003873
aPiecek61d062b2020-11-02 11:05:09 +01003874 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003875 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003876 node = pc->fp.modify.next_child(ca, tc);
3877
3878 if (!trp_node_is_empty(node)) {
3879 /* print root's nodes */
3880 trb_print_nodes(wr, new_ca, pc, tc);
3881 /* get back from child node to actual node */
3882 pc->fp.modify.parent(tc);
3883 }
3884}
3885
3886/**
3887 * @brief Get number of siblings.
3888 *
3889 * Side-effect -> current node is set to the first sibling.
3890 *
3891 * @param[in] fp contains callback functions which modify tree context
3892 * @param[in,out] tc is the tree context.
3893 * @return Number of siblings of the current node.
3894 */
3895static uint32_t
3896trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3897{
3898 uint32_t ret = 1;
3899 struct trt_node node = TRP_EMPTY_NODE;
3900
3901 /* including actual node */
3902 fp.first_sibling(tc);
3903 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3904 ret++;
3905 }
3906 fp.first_sibling(tc);
3907 return ret;
3908}
3909
3910/**
3911 * @brief Print all parents and their children.
3912 *
aPiecek874ea4d2021-04-19 12:26:36 +02003913 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003914 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003915 * for all top-level siblings. Use this function after 'module' keyword
3916 * or 'augment' and so. The nodes may not be exactly top-level in the
3917 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003918 *
aPiecek153b00f2021-04-20 13:52:57 +02003919 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3920 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003921 * @param[pc] pc contains mainly functions for printing.
3922 * @param[in,out] tc is tree context.
3923 */
3924static void
aPiecek153b00f2021-04-20 13:52:57 +02003925trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003926{
aPiecek61d062b2020-11-02 11:05:09 +01003927 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003928 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003929 uint32_t total_parents;
3930 uint32_t max_gap_before_type;
3931
aPiecekdc8fd572021-04-19 10:47:23 +02003932 if (!trb_tree_ctx_get_node(tc)) {
3933 return;
3934 }
3935
aPiecek61d062b2020-11-02 11:05:09 +01003936 ca = TRP_EMPTY_PARENT_CACHE;
3937 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3938 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3939
aPiecek3f247652021-04-19 13:40:25 +02003940 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003941 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3942 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003943 ca.lys_config = 0x0;
3944 }
aPiecekdc8fd572021-04-19 10:47:23 +02003945 }
3946
aPiecek61d062b2020-11-02 11:05:09 +01003947 for (uint32_t i = 0; i < total_parents; i++) {
3948 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003949 node = pc->fp.read.node(ca, tc);
3950 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003951 pc->fp.modify.next_sibling(ca, tc);
3952 }
3953}
3954
aPiecek874ea4d2021-04-19 12:26:36 +02003955/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003956 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003957 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003958
3959/**
aPiecekdc8fd572021-04-19 10:47:23 +02003960 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003961 *
aPiecekdc8fd572021-04-19 10:47:23 +02003962 * @param[in] module YANG schema tree structure representing
3963 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003964 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003965 * @param[in] max_line_length is the maximum line length limit
3966 * that should not be exceeded.
3967 * @param[in,out] pc will be adapted to lysp_tree.
3968 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003969 */
3970static void
aPiecekdc8fd572021-04-19 10:47:23 +02003971trm_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 +01003972{
aPiecekdc8fd572021-04-19 10:47:23 +02003973 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003974 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003975 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003976 .pmod = module->parsed,
3977 .cmod = NULL,
3978 .pn = module->parsed ? module->parsed->data : NULL,
3979 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003980 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003981 };
aPiecek61d062b2020-11-02 11:05:09 +01003982
aPiecekdc8fd572021-04-19 10:47:23 +02003983 pc->out = out;
3984
3985 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003986 .parent = trop_modi_parent,
3987 .first_sibling = trop_modi_first_sibling,
3988 .next_sibling = trop_modi_next_sibling,
3989 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01003990 };
3991
aPiecekdc8fd572021-04-19 10:47:23 +02003992 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003993 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003994 .node = trop_read_node,
3995 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003996 };
3997
aPiecekdc8fd572021-04-19 10:47:23 +02003998 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003999 .print_features_names = tro_print_features_names,
4000 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01004001 };
4002
aPiecekdc8fd572021-04-19 10:47:23 +02004003 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01004004}
4005
4006/**
aPiecek3f247652021-04-19 13:40:25 +02004007 * @brief Settings if lysc_node are used for browsing through the tree.
4008 *
4009 * Pointers to current nodes will be set to module data.
4010 *
4011 * @param[in] module YANG schema tree structure representing
4012 * YANG module.
4013 * @param[in] out is output handler.
4014 * @param[in] max_line_length is the maximum line length limit
4015 * that should not be exceeded.
4016 * @param[in,out] pc will be adapted to lysc_tree.
4017 * @param[in,out] tc will be adapted to lysc_tree.
4018 */
4019static void
4020trm_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)
4021{
4022 *tc = (struct trt_tree_ctx) {
4023 .lysc_tree = 1,
4024 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004025 .pmod = module->parsed,
4026 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02004027 .tpn = NULL,
4028 .pn = NULL,
4029 .cn = module->compiled->data
4030 };
4031
4032 pc->out = out;
4033
4034 pc->fp.modify = (struct trt_fp_modify_ctx) {
4035 .parent = troc_modi_parent,
4036 .first_sibling = troc_modi_first_sibling,
4037 .next_sibling = troc_modi_next_sibling,
4038 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02004039 };
4040
4041 pc->fp.read = (struct trt_fp_read) {
4042 .module_name = tro_read_module_name,
4043 .node = troc_read_node,
4044 .if_sibling_exists = troc_read_if_sibling_exists
4045 };
4046
4047 pc->fp.print = (struct trt_fp_print) {
4048 .print_features_names = tro_print_features_names,
4049 .print_keys = tro_print_keys
4050 };
4051
4052 pc->max_line_length = max_line_length;
4053}
4054
4055/**
4056 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02004057 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02004058 * @param[in,out] tc resets to lysc browsing.
4059 */
4060static void
4061trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4062{
aPiecek9f792e52021-04-21 08:33:56 +02004063 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02004064}
4065
4066/**
4067 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02004068 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02004069 * @param[in,out] tc resets to lysp browsing.
4070 */
4071static void
4072trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4073{
aPiecek9f792e52021-04-21 08:33:56 +02004074 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02004075}
4076
4077/**
4078 * @brief If augment's target node is located on the current module.
4079 * @param[in] pn is examined augment.
4080 * @param[in] pmod is current module.
4081 * @return 1 if nodeid refers to the local node, otherwise 0.
4082 */
4083static ly_bool
4084trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
4085{
4086 const char *id, *prefix, *name;
4087 size_t prefix_len, name_len;
4088 const struct lys_module *mod;
4089 ly_bool ret = 0;
4090
4091 if (pn == NULL) {
4092 return ret;
4093 }
4094
4095 id = pn->nodeid;
4096 if (!id) {
4097 return ret;
4098 }
4099 /* only absolute-schema-nodeid is taken into account */
4100 assert(id[0] == '/');
4101 ++id;
4102
4103 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
4104 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02004105 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
Michal Vasko3003c9a2022-03-29 12:10:00 +02004106 ret = mod ? (mod->parsed == pmod) : 0;
aPiecek3f247652021-04-19 13:40:25 +02004107 } else {
4108 ret = 1;
4109 }
4110
4111 return ret;
4112}
4113
4114/**
aPiecek96baa7f2021-04-23 12:32:00 +02004115 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01004116 *
aPiecekdc8fd572021-04-19 10:47:23 +02004117 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02004118 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01004119 *
aPiecekdc8fd572021-04-19 10:47:23 +02004120 * @param[in] ks is section representation.
4121 * @param[in] pc contains mainly functions for printing.
4122 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01004123 */
4124static void
aPiecekdc8fd572021-04-19 10:47:23 +02004125trm_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 +01004126{
aPiecekdc8fd572021-04-19 10:47:23 +02004127 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4128 return;
4129 }
4130
4131 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
4132 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02004133 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004134 } else {
aPiecek153b00f2021-04-20 13:52:57 +02004135 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004136 }
4137}
4138
4139/**
aPiecek96baa7f2021-04-23 12:32:00 +02004140 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02004141 *
aPiecek96baa7f2021-04-23 12:32:00 +02004142 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02004143 *
4144 * @param[in] ks is section representation.
4145 * @param[in] pc contains mainly functions for printing.
4146 * @param[in,out] tc is the tree context.
4147 */
4148static void
4149trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4150{
4151 ly_bool grp_has_data = 0;
4152
4153 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4154 return;
4155 }
4156
4157 if (ks.type == TRD_KEYWORD_GROUPING) {
4158 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4159 }
4160
4161 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4162 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004163 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004164}
4165
4166/**
4167 * @brief Print 'module' keyword, its name and all nodes.
4168 * @param[in] pc contains mainly functions for printing.
4169 * @param[in,out] tc is the tree context.
4170 */
4171static void
4172trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4173{
4174 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4175}
4176
4177/**
4178 * @brief For all augment sections: print 'augment' keyword,
4179 * its target node and all nodes.
4180 * @param[in] pc contains mainly functions for printing.
4181 * @param[in,out] tc is the tree context.
4182 */
4183static void
4184trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4185{
4186 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004187 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004188
aPiecek3f247652021-04-19 13:40:25 +02004189 if (tc->lysc_tree) {
4190 origin_was_lysc_tree = 1;
4191 trm_reset_to_lysp_tree_ctx(pc, tc);
4192 }
4193
aPiecekdc8fd572021-04-19 10:47:23 +02004194 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004195 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004196 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004197 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004198
aPiecek3f247652021-04-19 13:40:25 +02004199 if (origin_was_lysc_tree) {
4200 /* if lysc tree is used, then only augments targeting
4201 * another module are printed
4202 */
aPiecek9f792e52021-04-21 08:33:56 +02004203 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004204 continue;
4205 }
4206 }
4207
aPiecekdc8fd572021-04-19 10:47:23 +02004208 if (once) {
4209 ly_print_(pc->out, "\n");
4210 ly_print_(pc->out, "\n");
4211 once = 0;
4212 } else {
4213 ly_print_(pc->out, "\n");
4214 }
4215
4216 trm_print_section_as_subtree(ks, pc, tc);
4217 }
aPiecek3f247652021-04-19 13:40:25 +02004218
4219 if (origin_was_lysc_tree) {
4220 trm_reset_to_lysc_tree_ctx(pc, tc);
4221 }
aPiecekdc8fd572021-04-19 10:47:23 +02004222}
4223
4224/**
4225 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4226 * @param[in] pc contains mainly functions for printing.
4227 * @param[in,out] tc is the tree context.
4228 */
4229static void
4230trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4231{
4232 struct trt_keyword_stmt rpc;
4233
aPiecek01598c02021-04-23 14:18:24 +02004234 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004235
4236 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4237 ly_print_(pc->out, "\n");
4238 ly_print_(pc->out, "\n");
4239 trm_print_section_as_family_tree(rpc, pc, tc);
4240 }
4241}
4242
4243/**
4244 * @brief For notifications section: print 'notifications' keyword
4245 * and all its nodes.
4246 * @param[in] pc contains mainly functions for printing.
4247 * @param[in,out] tc is the tree context.
4248 */
4249static void
4250trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4251{
4252 struct trt_keyword_stmt notifs;
4253
aPiecek01598c02021-04-23 14:18:24 +02004254 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004255
4256 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4257 ly_print_(pc->out, "\n");
4258 ly_print_(pc->out, "\n");
4259 trm_print_section_as_family_tree(notifs, pc, tc);
4260 }
4261}
4262
4263/**
4264 * @brief For all grouping sections: print 'grouping' keyword, its name
4265 * and all nodes.
4266 * @param[in] pc contains mainly functions for printing.
4267 * @param[in,out] tc is the tree context.
4268 */
4269static void
4270trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4271{
4272 ly_bool once;
4273
aPiecek01598c02021-04-23 14:18:24 +02004274 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004275 return;
4276 }
4277
4278 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004279 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004280 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004281 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004282 if (once) {
4283 ly_print_(pc->out, "\n");
4284 ly_print_(pc->out, "\n");
4285 once = 0;
4286 } else {
4287 ly_print_(pc->out, "\n");
4288 }
4289 trm_print_section_as_subtree(ks, pc, tc);
4290 }
4291}
4292
4293/**
4294 * @brief For all yang-data sections: print 'yang-data' keyword
4295 * and all its nodes.
4296 * @param[in] pc contains mainly functions for printing.
4297 * @param[in,out] tc is the tree context.
4298 */
4299static void
aPiecek96baa7f2021-04-23 12:32:00 +02004300trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004301{
aPiecek96baa7f2021-04-23 12:32:00 +02004302 ly_bool once;
4303 LY_ARRAY_COUNT_TYPE count;
4304
4305 count = LY_ARRAY_COUNT(tc->pmod->exts);
4306 if (count == 0) {
4307 return;
4308 }
4309
4310 once = 1;
4311 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4312 struct trt_keyword_stmt ks;
4313
aPiecek01598c02021-04-23 14:18:24 +02004314 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004315 * ::lysp_ext_instance.parsed.
4316 */
4317 if (!tc->pmod->exts[u].parsed) {
4318 /* print at least the yang-data names */
4319 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4320 continue;
4321 }
4322
4323 ks = tro_modi_next_yang_data(tc, u);
4324 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4325 break;
4326 }
4327
4328 if (once) {
4329 ly_print_(pc->out, "\n");
4330 ly_print_(pc->out, "\n");
4331 once = 0;
4332 } else {
4333 ly_print_(pc->out, "\n");
4334 }
4335
4336 trm_print_section_as_family_tree(ks, pc, tc);
4337 }
aPiecekdc8fd572021-04-19 10:47:23 +02004338}
4339
4340/**
4341 * @brief Print sections module, augment, rpcs, notifications,
4342 * grouping, yang-data.
4343 * @param[in] pc contains mainly functions for printing.
4344 * @param[in,out] tc is the tree context.
4345 */
4346static void
4347trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4348{
4349 trm_print_module_section(pc, tc);
4350 trm_print_augmentations(pc, tc);
4351 trm_print_rpcs(pc, tc);
4352 trm_print_notifications(pc, tc);
4353 trm_print_groupings(pc, tc);
4354 trm_print_yang_data(pc, tc);
4355 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004356}
4357
aPiecek874ea4d2021-04-19 12:26:36 +02004358/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004359 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004360 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004361
4362LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004363tree_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 +01004364{
4365 struct trt_printer_ctx pc;
4366 struct trt_tree_ctx tc;
4367 struct ly_out *new_out;
4368 LY_ERR erc;
4369 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4370
aPiecekdc8fd572021-04-19 10:47:23 +02004371 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4372
aPiecek61d062b2020-11-02 11:05:09 +01004373 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4374 return erc;
4375 }
4376
4377 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004378 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4379 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4380 } else {
4381 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4382 }
aPiecek61d062b2020-11-02 11:05:09 +01004383
4384 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004385 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004386
4387 ly_out_free(new_out, NULL, 1);
4388
aPiecekdc8fd572021-04-19 10:47:23 +02004389 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004390}
4391
4392LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004393tree_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 +01004394{
aPiecek153b00f2021-04-20 13:52:57 +02004395 struct trt_printer_ctx pc;
4396 struct trt_tree_ctx tc;
4397 struct ly_out *new_out;
4398 struct trt_wrapper wr;
4399 LY_ERR erc;
4400 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4401
4402 assert(out && node);
4403
4404 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4405 return LY_EINVAL;
4406 }
4407
4408 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4409 return erc;
4410 }
4411
4412 line_length = line_length == 0 ? SIZE_MAX : line_length;
4413 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4414
4415 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
ekinzie0ab8b302022-10-10 03:03:57 -04004416 trb_print_parents(node, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004417
4418 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4419 tc.cn = lysc_node_child(node);
ekinzie0ab8b302022-10-10 03:03:57 -04004420 wr = trb_count_depth(NULL, tc.cn);
aPiecek153b00f2021-04-20 13:52:57 +02004421 trb_print_family_tree(wr, &pc, &tc);
4422 }
4423 ly_print_(out, "\n");
4424
4425 erc = clb_arg.last_error;
4426 ly_out_free(new_out, NULL, 1);
4427
4428 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004429}
4430
4431LY_ERR
Michal Vasko6a914fb2022-01-17 10:36:14 +01004432tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
4433 size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004434{
aPiecek9f792e52021-04-21 08:33:56 +02004435 struct trt_printer_ctx pc;
4436 struct trt_tree_ctx tc;
4437 struct ly_out *new_out;
4438 LY_ERR erc;
4439 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4440
4441 assert(submodp);
4442 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4443
4444 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4445 return erc;
4446 }
4447
4448 line_length = line_length == 0 ? SIZE_MAX : line_length;
4449 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4450 tc.pmod = (struct lysp_module *)submodp;
4451 tc.tpn = submodp->data;
4452 tc.pn = tc.tpn;
4453
4454 trm_print_sections(&pc, &tc);
4455 erc = clb_arg.last_error;
4456
4457 ly_out_free(new_out, NULL, 1);
4458
4459 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004460}
ekinzie0ab8b302022-10-10 03:03:57 -04004461
4462static LY_ERR
4463trb_print_mount_point(struct trt_node *node, struct trt_wrapper wr, struct trt_printer_ctx *pc,
4464 struct trt_tree_ctx *UNUSED(tc))
4465{
4466 struct ly_ctx *ext_ctx = NULL;
4467 const struct lys_module *mod;
4468 struct trt_tree_ctx tmptc;
4469 struct trt_wrapper tmpwr;
4470 struct trt_printer_ctx tmppc;
4471 struct trt_mount_ctx *mc;
4472 struct ly_set *modules;
4473 struct ly_set *refs = NULL;
4474 LY_ERR err = LY_SUCCESS;
4475
4476 if (!node->mount) {
4477 /* modules are parsed only */
4478 return LY_SUCCESS;
4479 }
4480
4481 if (lyplg_ext_schema_mount_create_context(node->mount, &ext_ctx)) {
4482 /* Void mount point */
4483 return LY_SUCCESS;
4484 }
4485
4486 if (ly_set_new(&modules)) {
4487 err = LY_SUCCESS;
4488 goto out_ctx;
4489 }
4490
4491 lyplg_ext_schema_mount_get_parent_ref(node->mount, &refs);
4492
4493 /* build new list of modules to print. This list will omit internal
4494 * modules, modules with no nodes (e.g., iana-if-types) and modules
4495 * that were loaded as the result of a parent-reference.
4496 */
4497 uint32_t v = ly_ctx_internal_modules_count(ext_ctx);
4498
4499 tmppc = *pc;
4500 while ((mod = ly_ctx_get_module_iter(ext_ctx, &v))) {
4501 uint32_t siblings;
4502 ly_bool from_parent_ref = 0;
4503
4504 for (uint32_t pr = 0; refs && pr < refs->count; pr++) {
4505 if (!strcmp(mod->ns, refs->snodes[pr]->module->ns)) {
4506 from_parent_ref = 1;
4507 break;
4508 }
4509 }
4510 if (from_parent_ref) {
4511 continue;
4512 }
4513
4514 if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) {
4515 trm_lysc_tree_ctx(mod, pc->out, pc->max_line_length, &tmppc, &tmptc);
4516 } else {
4517 trm_lysp_tree_ctx(mod, pc->out, pc->max_line_length, &tmppc, &tmptc);
4518 }
4519
4520 siblings = (tmptc.pn || tmptc.cn) ?
4521 trb_get_number_of_siblings(tmppc.fp.modify, &tmptc) : 0;
4522 if (siblings == 0) {
4523 continue;
4524 }
4525 tmptc.mounted = 1;
4526 tmptc.parent_refs = refs;
4527
4528 mc = malloc(sizeof *mc);
4529 if (mc == NULL) {
4530 err = LY_EMEM;
4531 goto out;
4532 }
4533
4534 mc->tc = tmptc;
4535 mc->pc = tmppc;
4536 ly_set_add(modules, (void *)mc, 1, NULL);
4537 }
4538
4539 for (uint32_t v = 0; v < modules->count; ++v) {
4540 mc = modules->objs[v];
4541 tmpwr = (v == modules->count - 1) ? wr : trp_wrapper_set_mark_top(wr);
4542 trb_print_family_tree(tmpwr, &mc->pc, &mc->tc);
4543 }
4544
4545 for (uint32_t pr = 0; refs && pr < refs->count; pr++) {
4546 trm_lysc_tree_ctx(refs->snodes[pr]->module, pc->out, pc->max_line_length, &tmppc, &tmptc);
4547 tmptc.mounted = 1;
4548 tmptc.parent_refs = refs;
4549 trb_print_parents(refs->snodes[pr], &tmpwr, pc, &tmptc);
4550 }
4551
4552out:
4553 ly_set_free(modules, free);
4554 ly_set_free(refs, NULL);
4555out_ctx:
4556 ly_ctx_destroy(ext_ctx);
4557 return err;
4558}