blob: f275d07e1992a0ba59686ffe747a36bede21fc06 [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 {
aPiecek7ed8d032022-10-10 12:32:27 +0200451 trt_status_type status; /**< \<status\>. */
452 trt_flags_type flags; /**< \<flags\>. */
453 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
454 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
455 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and
456 will be printed by trt_fp_print.print_features_names callback. */
457 ly_bool last_one; /**< Information about whether the node is the last. */
458 struct lysc_ext_instance *mount; /**< Mount-point extension if flags == TRD_FLAGS_TYPE_MOUNT_POINT */
aPiecek61d062b2020-11-02 11:05:09 +0100459};
460
461/**
462 * @brief Create struct trt_node as empty.
463 */
464#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200465 (struct trt_node) { \
466 .status = TRD_STATUS_TYPE_EMPTY, \
467 .flags = TRD_FLAGS_TYPE_EMPTY, \
468 .name = TRP_EMPTY_NODE_NAME, \
469 .type = TRP_EMPTY_TRT_TYPE, \
470 .iffeatures = 0, \
ekinzie0ab8b302022-10-10 03:03:57 -0400471 .last_one = 1, \
472 .mount = NULL \
aPiecek874ea4d2021-04-19 12:26:36 +0200473 }
aPiecek61d062b2020-11-02 11:05:09 +0100474
475/**
476 * @brief Package which only groups indent and node.
477 */
478struct trt_pair_indent_node {
479 struct trt_indent_in_node indent;
480 struct trt_node node;
481};
482
483/**
484 * @brief Initialize struct trt_pair_indent_node by parameters.
485 */
486#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200487 (struct trt_pair_indent_node) { \
488 .indent = INDENT_IN_NODE, .node = NODE \
489 }
aPiecek61d062b2020-11-02 11:05:09 +0100490
aPiecek874ea4d2021-04-19 12:26:36 +0200491/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100492 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200493 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100494
495#define TRD_TOP_KEYWORD_MODULE "module"
496#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
497
498#define TRD_BODY_KEYWORD_AUGMENT "augment"
499#define TRD_BODY_KEYWORD_RPC "rpcs"
500#define TRD_BODY_KEYWORD_NOTIF "notifications"
501#define TRD_BODY_KEYWORD_GROUPING "grouping"
502#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
503
504/**
505 * @brief Type of the trt_keyword.
506 */
507typedef enum {
508 TRD_KEYWORD_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100509 TRD_KEYWORD_MODULE,
510 TRD_KEYWORD_SUBMODULE,
511 TRD_KEYWORD_AUGMENT,
512 TRD_KEYWORD_RPC,
513 TRD_KEYWORD_NOTIF,
514 TRD_KEYWORD_GROUPING,
aPiecek61d062b2020-11-02 11:05:09 +0100515 TRD_KEYWORD_YANG_DATA
516} trt_keyword_type;
517
518/**
519 * @brief Main sign of the tree nodes.
520 *
aPiecek874ea4d2021-04-19 12:26:36 +0200521 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100522 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
523 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
524 * trp_keyword_type_strlen
525 *
526 */
527struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200528 trt_keyword_type type; /**< String containing some of the top or body keyword. */
529 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100530};
531
532/**
533 * @brief Create struct trt_keyword_stmt as empty.
534 */
535#define TRP_EMPTY_KEYWORD_STMT \
536 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
537
538/**
539 * @brief Check if struct trt_keyword_stmt is empty.
540 */
541#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
542 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
543
544/**
545 * @brief Initialize struct trt_keyword_stmt by parameters.
546 */
547#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
548 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
549
aPiecek874ea4d2021-04-19 12:26:36 +0200550/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100551 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200552 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100553
554struct trt_parent_cache;
555
556/**
557 * @brief Functions that change the state of the tree_ctx structure.
558 *
aPiecek3f247652021-04-19 13:40:25 +0200559 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200560 * for the 'trp' printing functions and are also called from the
561 * 'trb' browsing functions when walking through a tree. These callback
562 * functions need to be checked or reformulated if changes to the
563 * libyang library affect the printing tree. For all, if the value
564 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
565 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100566 */
567struct trt_fp_modify_ctx {
568 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
569 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
570 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
571 struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */
aPiecek61d062b2020-11-02 11:05:09 +0100572};
573
aPiecek874ea4d2021-04-19 12:26:36 +0200574/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100575 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200576 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100577
578/**
579 * @brief Functions that do not change the state of the tree_structure.
580 *
581 * For details see trt_fp_modify_ctx.
582 */
583struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200584 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
585 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
586 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100587};
588
aPiecek874ea4d2021-04-19 12:26:36 +0200589/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100590 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200591 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100592
593/**
aPiecek874ea4d2021-04-19 12:26:36 +0200594 * @brief A set of all necessary functions that must be provided
595 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100596 */
597struct trt_fp_all {
598 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
599 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
600 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
601};
602
aPiecek874ea4d2021-04-19 12:26:36 +0200603/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100604 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200605 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100606
607/**
aPiecek01598c02021-04-23 14:18:24 +0200608 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100609 */
610struct trt_printer_ctx {
611 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200612 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100613 size_t max_line_length; /**< The maximum number of characters that can be
614 printed on one line, including the last. */
615};
616
aPiecek874ea4d2021-04-19 12:26:36 +0200617/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100618 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200619 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100620
621/**
622 * @brief The name of the section to which the node belongs.
623 */
624typedef enum {
625 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100626 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
627 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
628 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
629 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
aPiecek61d062b2020-11-02 11:05:09 +0100630 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
631} trt_actual_section;
632
633/**
634 * @brief Types of nodes that have some effect on their children.
635 */
636typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200637 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100638 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
639 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
aPiecek874ea4d2021-04-19 12:26:36 +0200640 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100641} trt_ancestor_type;
642
643/**
644 * @brief Saved information when browsing the tree downwards.
645 *
aPiecek874ea4d2021-04-19 12:26:36 +0200646 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200647 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200648 * this structures during their recursive calls. This functions do not
649 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200650 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200651 * structure, can return a node with the correct data. The word
652 * \b parent is in the structure name, because this data refers to
653 * the last parent and at the same time the states of its
654 * ancestors data. Only the function jumping on the child
655 * (next_child(...)) creates this structure, because the pointer
656 * to the current node moves down the tree. It's like passing
657 * the genetic code to children. Some data must be inherited and
658 * there are two approaches to this problem. Either it will always
659 * be determined which inheritance states belong to the current node
660 * (which can lead to regular travel to the root node) or
661 * the inheritance states will be stored during the recursive calls.
662 * So the problem was solved by the second option. Why does
663 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200664 * the lysp tree. For walks through the lysc tree is trt_parent_cache
665 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100666 *
aPiecek874ea4d2021-04-19 12:26:36 +0200667 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100668 */
669struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200670 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
671 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
672 uint16_t lys_config; /**< Inherited config W or R. */
673 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100674};
675
676/**
677 * @brief Return trt_parent_cache filled with default values.
678 */
679#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200680 (struct trt_parent_cache) { \
681 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
682 .lys_config = LYS_CONFIG_W, .last_list = NULL \
683 }
aPiecek61d062b2020-11-02 11:05:09 +0100684
685/**
686 * @brief Main structure for browsing the libyang tree
687 */
688struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200689 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
690 It is assumed that once set, it does not change.
691 If it is true then trt_tree_ctx.pn and
692 trt_tree_ctx.tpn are not used.
693 If it is false then trt_tree_ctx.cn is not used. */
ekinzie0ab8b302022-10-10 03:03:57 -0400694 ly_bool mounted; /**< This tree is a mounted schema */
aPiecek96baa7f2021-04-23 12:32:00 +0200695 trt_actual_section section; /**< To which section pn points. */
696 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
697 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
698 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200699
aPiecek96baa7f2021-04-23 12:32:00 +0200700 union {
701 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
702 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
703 is set to TRD_SECT_YANG_DATA. */
704 };
705 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
ekinzie0ab8b302022-10-10 03:03:57 -0400706 const struct ly_set *parent_refs; /**< List of schema nodes for top-level nodes found in mount
707 point parent references */
aPiecek61d062b2020-11-02 11:05:09 +0100708};
709
aPiecek3f247652021-04-19 13:40:25 +0200710/**
aPiecekbbc02932021-05-21 07:19:41 +0200711 * @brief Check if lysp node is available from
712 * the current compiled node.
713 *
714 * Use only if trt_tree_ctx.lysc_tree is set to true.
715 */
716#define TRP_TREE_CTX_LYSP_NODE_PRESENT(CN) \
717 (CN->priv)
718
719/**
aPiecek3f247652021-04-19 13:40:25 +0200720 * @brief Get lysp_node from trt_tree_ctx.cn.
aPiecekbbc02932021-05-21 07:19:41 +0200721 *
722 * Use only if :TRP_TREE_CTX_LYSP_NODE_PRESENT returns true
723 * for that node.
aPiecek3f247652021-04-19 13:40:25 +0200724 */
725#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
726 ((const struct lysp_node *)CN->priv)
727
aPiecek01598c02021-04-23 14:18:24 +0200728/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100729typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
730
aPiecekef1e58e2021-04-19 13:19:44 +0200731/**
732 * @brief Simple getter functions for lysp and lysc nodes.
733 *
734 * This structure is useful if we have a general algorithm
735 * (tro function) that can be used for both lysc and lysp nodes.
736 * Thanks to this structure, we prevent code redundancy.
737 * We don't have to write basically the same algorithm twice
738 * for lysp and lysc trees.
739 */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100740struct tro_getters {
aPiecekef1e58e2021-04-19 13:19:44 +0200741 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
742 const void *(*next)(const void *); /**< Get sibling. */
743 const void *(*parent)(const void *); /**< Get parent. */
744 const void *(*child)(const void *); /**< Get child. */
745 const void *(*actions)(const void *); /**< Get actions. */
746 const void *(*action_input)(const void *); /**< Get input action from action node. */
747 const void *(*action_output)(const void *); /**< Get output action from action node. */
748 const void *(*notifs)(const void *); /**< Get notifs. */
749};
750
aPiecek874ea4d2021-04-19 12:26:36 +0200751/**********************************************************************
ekinzie0ab8b302022-10-10 03:03:57 -0400752 * Forward declarations
753 *********************************************************************/
aPiecek7ed8d032022-10-10 12:32:27 +0200754static LY_ERR trb_print_mount_point(const struct lysc_ext_instance *ext, const struct trt_wrapper wr, struct trt_printer_ctx *pc);
ekinzie0ab8b302022-10-10 03:03:57 -0400755
756/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100757 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200758 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100759
760/**
761 * @brief Print a substring but limited to the maximum length.
762 * @param[in] str is pointer to source.
763 * @param[in] len is number of characters to be printed.
764 * @param[in,out] out is output handler.
765 * @return str parameter shifted by len.
766 */
767static const char *
768trg_print_substr(const char *str, size_t len, struct ly_out *out)
769{
770 for (size_t i = 0; i < len; i++) {
771 ly_print_(out, "%c", str[0]);
772 str++;
773 }
774 return str;
775}
776
777/**
778 * @brief Pointer is not NULL and does not point to an empty string.
779 * @param[in] str is pointer to string to be checked.
780 * @return 1 if str pointing to non empty string otherwise 0.
781 */
782static ly_bool
783trg_charptr_has_data(const char *str)
784{
785 return (str) && (str[0] != '\0');
786}
787
788/**
aPiecek874ea4d2021-04-19 12:26:36 +0200789 * @brief Check if @p word in @p src is present where words are
790 * delimited by @p delim.
791 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100792 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200793 * @param[in] delim is delimiter between @p words in @p src.
794 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100795 */
796static ly_bool
797trg_word_is_present(const char *src, const char *word, char delim)
798{
799 const char *hit;
800
801 if ((!src) || (src[0] == '\0') || (!word)) {
802 return 0;
803 }
804
805 hit = strstr(src, word);
806
807 if (hit) {
808 /* word was founded at the begin of src
809 * OR it match somewhere after delim
810 */
811 if ((hit == src) || (hit[-1] == delim)) {
812 /* end of word was founded at the end of src
813 * OR end of word was match somewhere before delim
814 */
815 char delim_or_end = (hit + strlen(word))[0];
Michal Vasko26bbb272022-08-02 14:54:33 +0200816
aPiecek61d062b2020-11-02 11:05:09 +0100817 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
818 return 1;
819 }
820 }
821 /* after -> hit is just substr and it's not the whole word */
822 /* jump to the next word */
823 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
824 /* skip delim */
825 src = src[0] == '\0' ? src : src + 1;
826 /* continue with searching */
827 return trg_word_is_present(src, word, delim);
828 } else {
829 return 0;
830 }
831}
832
aPiecek874ea4d2021-04-19 12:26:36 +0200833/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100834 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200835 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100836
837/**
aPiecek01598c02021-04-23 14:18:24 +0200838 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100839 *
aPiecek874ea4d2021-04-19 12:26:36 +0200840 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100841 * @param[in] buf contains input characters
842 * @param[in] count is number of characters in buf.
843 * @return Number of printed bytes.
844 * @return Negative value in case of error.
845 */
846static ssize_t
847trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
848{
849 LY_ERR erc = LY_SUCCESS;
850 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
851
852 switch (data->mode) {
853 case TRD_PRINT:
854 erc = ly_write_(data->out, buf, count);
855 break;
856 case TRD_CHAR_COUNT:
857 data->counter = data->counter + count;
858 break;
859 default:
860 break;
861 }
862
863 if (erc != LY_SUCCESS) {
864 data->last_error = erc;
865 return -1;
866 } else {
867 return count;
868 }
869}
870
871/**
872 * @brief Check that indent in node can be considered as equivalent.
873 * @param[in] first is the first indent in node.
874 * @param[in] second is the second indent in node.
875 * @return 1 if indents are equivalent otherwise 0.
876 */
877static ly_bool
878trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
879{
880 const ly_bool a = first.type == second.type;
881 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
882 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
883 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
884
885 return a && b && c && d;
886}
887
888/**
aPiecek874ea4d2021-04-19 12:26:36 +0200889 * @brief Setting space character because node is last sibling.
890 * @param[in] wr is wrapper over which the shift operation
891 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100892 * @return New shifted wrapper.
893 */
894static struct trt_wrapper
895trp_wrapper_set_shift(struct trt_wrapper wr)
896{
897 assert(wr.actual_pos < 64);
898 /* +--<node>
899 * +--<node>
900 */
901 wr.actual_pos++;
902 return wr;
903}
904
905/**
aPiecek874ea4d2021-04-19 12:26:36 +0200906 * @brief Setting '|' symbol because node is divided or
907 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100908 * @param[in] wr is source of wrapper.
909 * @return New wrapper which is marked at actual position and shifted.
910 */
911static struct trt_wrapper
912trp_wrapper_set_mark(struct trt_wrapper wr)
913{
914 assert(wr.actual_pos < 64);
915 wr.bit_marks1 |= 1U << wr.actual_pos;
916 return trp_wrapper_set_shift(wr);
917}
918
919/**
ekinzie0ab8b302022-10-10 03:03:57 -0400920 * @brief Set '|' symbol to connect current level nodes in a module.
921 * This is only used to connect all top-level nodes in all modules under
922 * a schema mount point.
923 * @param[in] wr is the wrapper to be marked
924 * @return New wrapper which is marked at actual position.
925 */
926static struct trt_wrapper
927trp_wrapper_set_mark_top(struct trt_wrapper wr)
928{
929 wr.bit_marks1 |= 1U << wr.actual_pos;
930 return wr;
931}
932
933/**
aPiecek61d062b2020-11-02 11:05:09 +0100934 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
935 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200936 * @param[in] last_one is flag. Value 1 saying if the node is the last
937 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100938 * @return New wrapper for the actual node.
939 */
940static struct trt_wrapper
941trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
942{
943 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
944}
945
946/**
947 * @brief Test if the wrappers are equivalent.
948 * @param[in] first is the first wrapper.
949 * @param[in] second is the second wrapper.
950 * @return 1 if the wrappers are equivalent otherwise 0.
951 */
952static ly_bool
953trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
954{
955 const ly_bool a = first.type == second.type;
956 const ly_bool b = first.bit_marks1 == second.bit_marks1;
957 const ly_bool c = first.actual_pos == second.actual_pos;
958
959 return a && b && c;
960}
961
962/**
963 * @brief Print " | " sequence on line.
964 * @param[in] wr is wrapper to be printed.
965 * @param[in,out] out is output handler.
966 */
967static void
968trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
969{
970 uint32_t lb;
971
972 if (wr.type == TRD_WRAPPER_TOP) {
973 lb = TRD_INDENT_LINE_BEGIN;
974 } else if (wr.type == TRD_WRAPPER_BODY) {
975 lb = TRD_INDENT_LINE_BEGIN * 2;
976 } else {
977 lb = TRD_INDENT_LINE_BEGIN;
978 }
979
980 ly_print_(out, "%*c", lb, ' ');
981
982 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
983 return;
984 }
985
986 for (uint32_t i = 0; i < wr.actual_pos; i++) {
987 /** Test if the bit on the index is set. */
988 if ((wr.bit_marks1 >> i) & 1U) {
989 ly_print_(out, "|");
990 } else {
991 ly_print_(out, " ");
992 }
993
994 if (i != wr.actual_pos) {
995 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
996 }
997 }
998}
999
1000/**
1001 * @brief Check if struct trt_node is empty.
1002 * @param[in] node is item to test.
1003 * @return 1 if node is considered empty otherwise 0.
1004 */
1005static ly_bool
1006trp_node_is_empty(struct trt_node node)
1007{
1008 const ly_bool a = !node.iffeatures;
1009 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
1010 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
1011 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
1012 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
1013
1014 return a && b && c && d && e;
1015}
1016
1017/**
aPiecek874ea4d2021-04-19 12:26:36 +02001018 * @brief Check if [\<keys\>], \<type\> and
1019 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +01001020 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +02001021 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
1022 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +01001023 */
1024static ly_bool
1025trp_node_body_is_empty(struct trt_node node)
1026{
1027 const ly_bool a = !node.iffeatures;
1028 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
1029 const ly_bool c = node.name.type != TRD_NODE_KEYS;
1030
1031 return a && b && c;
1032}
1033
1034/**
1035 * @brief Print \<status\> of the node.
1036 * @param[in] status_type is type of status.
1037 * @param[in,out] out is output handler.
1038 */
1039static void
1040trp_print_status(trt_status_type status_type, struct ly_out *out)
1041{
1042 switch (status_type) {
1043 case TRD_STATUS_TYPE_CURRENT:
1044 ly_print_(out, "%c", '+');
1045 break;
1046 case TRD_STATUS_TYPE_DEPRECATED:
1047 ly_print_(out, "%c", 'x');
1048 break;
1049 case TRD_STATUS_TYPE_OBSOLETE:
1050 ly_print_(out, "%c", 'o');
1051 break;
1052 default:
1053 break;
1054 }
1055}
1056
1057/**
1058 * @brief Print \<flags\>.
1059 * @param[in] flags_type is type of \<flags\>.
1060 * @param[in,out] out is output handler.
1061 */
1062static void
1063trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1064{
1065 switch (flags_type) {
1066 case TRD_FLAGS_TYPE_RW:
1067 ly_print_(out, "%s", "rw");
1068 break;
1069 case TRD_FLAGS_TYPE_RO:
1070 ly_print_(out, "%s", "ro");
1071 break;
1072 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1073 ly_print_(out, "%s", "-w");
1074 break;
1075 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1076 ly_print_(out, "%s", "-u");
1077 break;
1078 case TRD_FLAGS_TYPE_RPC:
1079 ly_print_(out, "%s", "-x");
1080 break;
1081 case TRD_FLAGS_TYPE_NOTIF:
1082 ly_print_(out, "%s", "-n");
1083 break;
1084 case TRD_FLAGS_TYPE_MOUNT_POINT:
1085 ly_print_(out, "%s", "mp");
1086 break;
1087 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001088 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001089 break;
1090 }
1091}
1092
1093/**
1094 * @brief Get size of the \<flags\>.
1095 * @param[in] flags_type is type of \<flags\>.
1096 * @return 0 if flags_type is not set otherwise 2.
1097 */
1098static size_t
1099trp_get_flags_strlen(trt_flags_type flags_type)
1100{
1101 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1102}
1103
1104/**
1105 * @brief Print entire struct trt_node_name structure.
1106 * @param[in] node_name is item to print.
1107 * @param[in,out] out is output handler.
1108 */
1109static void
1110trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1111{
1112 const char *mod_prefix;
1113 const char *colon;
1114 const char trd_node_name_suffix_choice[] = ")";
1115 const char trd_node_name_suffix_case[] = ")";
1116 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1117 const char trd_opts_container[] = "!"; /**< For a presence container. */
1118 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1119 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1120 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1121
1122 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1123 return;
1124 }
1125
1126 if (node_name.module_prefix) {
1127 mod_prefix = node_name.module_prefix;
1128 colon = ":";
1129 } else {
1130 mod_prefix = "";
1131 colon = "";
1132 }
1133
1134 switch (node_name.type) {
1135 case TRD_NODE_ELSE:
1136 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1137 break;
1138 case TRD_NODE_CASE:
1139 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1140 break;
1141 case TRD_NODE_CHOICE:
1142 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1143 break;
1144 case TRD_NODE_OPTIONAL_CHOICE:
1145 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);
1146 break;
1147 case TRD_NODE_OPTIONAL:
1148 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1149 break;
1150 case TRD_NODE_CONTAINER:
1151 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1152 break;
1153 case TRD_NODE_LISTLEAFLIST:
1154 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1155 break;
1156 case TRD_NODE_KEYS:
1157 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1158 break;
1159 case TRD_NODE_TOP_LEVEL1:
1160 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1161 break;
1162 case TRD_NODE_TOP_LEVEL2:
1163 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1164 break;
1165 case TRD_NODE_TRIPLE_DOT:
1166 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1167 break;
1168 default:
1169 break;
1170 }
1171}
1172
1173/**
aPiecek874ea4d2021-04-19 12:26:36 +02001174 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1175 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001176 * @param[in] node_name is structure containing the 'mark'.
1177 * @return 1 if contain otherwise 0.
1178 */
1179static ly_bool
1180trp_mark_is_used(struct trt_node_name node_name)
1181{
1182 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1183 return 0;
1184 }
1185
1186 switch (node_name.type) {
1187 case TRD_NODE_ELSE:
1188 case TRD_NODE_CASE:
1189 case TRD_NODE_KEYS:
1190 return 0;
1191 default:
1192 return 1;
1193 }
1194}
1195
1196/**
1197 * @brief Print opts keys.
1198 * @param[in] node_name contains type of the node with his name.
1199 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001200 * @param[in] cf is basically a pointer to the function that prints
1201 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001202 * @param[in,out] out is output handler.
1203 */
1204static void
1205trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1206{
1207 if (node_name.type != TRD_NODE_KEYS) {
1208 return;
1209 }
1210
1211 /* <name><mark>___<keys>*/
1212 if (btw_name_opts > 0) {
1213 ly_print_(out, "%*c", btw_name_opts, ' ');
1214 }
1215 ly_print_(out, "[");
1216 cf.pf(cf.ctx, out);
1217 ly_print_(out, "]");
1218}
1219
1220/**
1221 * @brief Print entire struct trt_type structure.
1222 * @param[in] type is item to print.
1223 * @param[in,out] out is output handler.
1224 */
1225static void
1226trp_print_type(struct trt_type type, struct ly_out *out)
1227{
1228 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1229 return;
1230 }
1231
1232 switch (type.type) {
1233 case TRD_TYPE_NAME:
1234 ly_print_(out, "%s", type.str);
1235 break;
1236 case TRD_TYPE_TARGET:
1237 ly_print_(out, "-> %s", type.str);
1238 break;
1239 case TRD_TYPE_LEAFREF:
1240 ly_print_(out, "leafref");
1241 default:
1242 break;
1243 }
1244}
1245
1246/**
1247 * @brief Print all iffeatures of node
1248 *
1249 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001250 * @param[in] cf is basically a pointer to the function that prints
1251 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001252 * @param[in,out] out is output handler.
1253 */
1254static void
1255trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1256{
1257 if (iffeature_flag) {
1258 ly_print_(out, "{");
1259 cf.pf(cf.ctx, out);
1260 ly_print_(out, "}?");
1261 }
1262}
1263
1264/**
1265 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1266 * @param[in] node contains items to print.
1267 * @param[in] out is output handler.
1268 */
1269static void
1270trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1271{
1272 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1273 trp_print_node_name(node.name, out);
1274 return;
1275 }
1276 /* <status>--<flags> */
1277 trp_print_status(node.status, out);
1278 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001279 /* If the node is a case node, there is no space before the <name>
1280 * also case node has no flags.
1281 */
aPiecek61d062b2020-11-02 11:05:09 +01001282 if (node.name.type != TRD_NODE_CASE) {
1283 trp_print_flags(node.flags, out);
1284 ly_print_(out, " ");
1285 }
1286 /* <name> */
1287 trp_print_node_name(node.name, out);
1288}
1289
1290/**
aPiecek874ea4d2021-04-19 12:26:36 +02001291 * @brief Print alignment (spaces) instead of
1292 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001293 * @param[in] node contains items to print.
1294 * @param[in] out is output handler.
1295 */
1296static void
1297trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1298{
1299 uint32_t space = trp_get_flags_strlen(node.flags);
1300
1301 if (node.name.type == TRD_NODE_CASE) {
1302 /* :(<name> */
1303 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1304 } else if (node.name.type == TRD_NODE_CHOICE) {
1305 /* (<name> */
1306 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1307 } else {
1308 /* _<name> */
1309 space += strlen(" ");
1310 }
1311
1312 /* <name>
1313 * __
1314 */
1315 space += TRD_INDENT_LONG_LINE_BREAK;
1316
1317 ly_print_(out, "%*c", space, ' ');
1318}
1319
1320/**
1321 * @brief Print struct trt_node structure.
1322 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001323 * @param[in] pck package of functions for
1324 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001325 * @param[in] indent is the indent in node.
1326 * @param[in,out] out is output handler.
1327 */
1328static void
1329trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1330{
1331 ly_bool triple_dot;
1332 ly_bool divided;
1333 struct trt_cf_print cf_print_keys;
1334 struct trt_cf_print cf_print_iffeatures;
1335
1336 if (trp_node_is_empty(node)) {
1337 return;
1338 }
1339
1340 /* <status>--<flags> <name><opts> <type> <if-features> */
1341 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1342 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1343
1344 if (triple_dot) {
1345 trp_print_node_name(node.name, out);
1346 return;
1347 } else if (!divided) {
1348 trp_print_node_up_to_name(node, out);
1349 } else {
1350 trp_print_divided_node_up_to_name(node, out);
1351 }
1352
1353 /* <opts> */
1354 /* <name>___<opts>*/
1355 cf_print_keys.ctx = pck.tree_ctx;
1356 cf_print_keys.pf = pck.fps.print_keys;
1357
1358 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1359
1360 /* <opts>__<type> */
1361 if (indent.btw_opts_type > 0) {
1362 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1363 }
1364
1365 /* <type> */
1366 trp_print_type(node.type, out);
1367
1368 /* <type>__<iffeatures> */
1369 if (indent.btw_type_iffeatures > 0) {
1370 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1371 }
1372
1373 /* <iffeatures> */
1374 cf_print_iffeatures.ctx = pck.tree_ctx;
1375 cf_print_iffeatures.pf = pck.fps.print_features_names;
1376
1377 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1378}
1379
1380/**
aPiecek874ea4d2021-04-19 12:26:36 +02001381 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001382 * @param[in] ks is keyword statement to print.
1383 * @param[in,out] out is output handler
1384 */
1385static void
1386trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1387{
1388 switch (ks.type) {
1389 case TRD_KEYWORD_MODULE:
1390 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1391 return;
1392 case TRD_KEYWORD_SUBMODULE:
1393 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1394 return;
1395 default:
1396 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1397 switch (ks.type) {
1398 case TRD_KEYWORD_AUGMENT:
1399 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1400 break;
1401 case TRD_KEYWORD_RPC:
1402 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1403 break;
1404 case TRD_KEYWORD_NOTIF:
1405 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1406 break;
1407 case TRD_KEYWORD_GROUPING:
1408 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1409 break;
1410 case TRD_KEYWORD_YANG_DATA:
1411 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1412 break;
1413 default:
1414 break;
1415 }
1416 break;
1417 }
1418}
1419
1420/**
1421 * @brief Get string length of stored keyword.
1422 * @param[in] type is type of the keyword statement.
1423 * @return length of the keyword statement name.
1424 */
1425static size_t
1426trp_keyword_type_strlen(trt_keyword_type type)
1427{
1428 switch (type) {
1429 case TRD_KEYWORD_MODULE:
1430 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1431 case TRD_KEYWORD_SUBMODULE:
1432 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1433 case TRD_KEYWORD_AUGMENT:
1434 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1435 case TRD_KEYWORD_RPC:
1436 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1437 case TRD_KEYWORD_NOTIF:
1438 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1439 case TRD_KEYWORD_GROUPING:
1440 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1441 case TRD_KEYWORD_YANG_DATA:
1442 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1443 default:
1444 return 0;
1445 }
1446}
1447
1448/**
aPiecek874ea4d2021-04-19 12:26:36 +02001449 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001450 * @param[in] ks is keyword statement structure.
1451 * @param[in] mll is max line length.
1452 * @param[in,out] out is output handler.
1453 */
1454static void
1455trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1456{
1457 uint32_t ind_initial;
1458 uint32_t ind_divided;
1459 /* flag if path must be splitted to more lines */
1460 ly_bool linebreak_was_set;
1461 /* flag if at least one subpath was printed */
1462 ly_bool subpath_printed;
1463 /* the sum of the sizes of the substrings on the current line */
1464 uint32_t how_far;
1465 /* pointer to start of the subpath */
1466 const char *sub_ptr;
1467 /* size of subpath from sub_ptr */
1468 size_t sub_len;
1469
1470 if ((!ks.str) || (ks.str[0] == '\0')) {
1471 return;
1472 }
1473
1474 /* module name cannot be splitted */
1475 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1476 ly_print_(out, "%s", ks.str);
1477 return;
1478 }
1479
1480 /* after -> for trd_keyword_stmt_body do */
1481
1482 /* set begin indentation */
1483 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1484 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1485 linebreak_was_set = 0;
1486 subpath_printed = 0;
1487 how_far = 0;
1488 sub_ptr = ks.str;
1489 sub_len = 0;
1490
1491 while (sub_ptr[0] != '\0') {
1492 uint32_t ind;
1493 /* skip slash */
1494 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
Michal Vasko26bbb272022-08-02 14:54:33 +02001495
aPiecek61d062b2020-11-02 11:05:09 +01001496 /* get position of the end of substr */
1497 tmp = strchr(tmp, '/');
1498 /* set correct size if this is a last substring */
1499 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1500 /* actualize sum of the substring's sizes on the current line */
1501 how_far += sub_len;
1502 /* correction due to colon character if it this is last substring */
1503 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1504 /* choose indentation which depends on
1505 * whether the string is printed on multiple lines or not
1506 */
1507 ind = linebreak_was_set ? ind_divided : ind_initial;
1508 if (ind + how_far <= mll) {
1509 /* printing before max line length */
1510 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1511 subpath_printed = 1;
1512 } else {
1513 /* printing on new line */
1514 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001515 /* first subpath is too long
1516 * but print it at first line anyway
1517 */
aPiecek61d062b2020-11-02 11:05:09 +01001518 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1519 subpath_printed = 1;
1520 continue;
1521 }
1522 ly_print_(out, "\n");
1523 ly_print_(out, "%*c", ind_divided, ' ');
1524 linebreak_was_set = 1;
1525 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1526 how_far = sub_len;
1527 subpath_printed = 1;
1528 }
1529 }
1530}
1531
1532/**
aPiecek874ea4d2021-04-19 12:26:36 +02001533 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001534 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001535 * @param[in] grp_has_data is flag only for grouping section.
1536 * Set to 1 if grouping section has some nodes.
1537 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001538 * @param[in,out] out is output handler.
1539 */
1540static void
aPiecekdc8fd572021-04-19 10:47:23 +02001541trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001542{
1543 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001544 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1545 return;
1546 } else {
1547 ly_print_(out, ":");
1548 }
aPiecek61d062b2020-11-02 11:05:09 +01001549 }
1550}
1551
1552/**
1553 * @brief Print entire struct trt_keyword_stmt structure.
1554 * @param[in] ks is item to print.
1555 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001556 * @param[in] grp_has_data is flag only for grouping section.
1557 * Set to 1 if grouping section has some nodes.
1558 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001559 * @param[in,out] out is output handler.
1560 */
1561static void
aPiecek874ea4d2021-04-19 12:26:36 +02001562trp_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 +01001563{
1564 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1565 return;
1566 }
1567 trt_print_keyword_stmt_begin(ks, out);
1568 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001569 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001570}
1571
aPiecek874ea4d2021-04-19 12:26:36 +02001572/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001573 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001574 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001575
1576/**
aPiecek874ea4d2021-04-19 12:26:36 +02001577 * @brief Printing one line including wrapper and node
1578 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001579 * @param[in] node is \<node\> representation.
1580 * @param[in] pck contains special printing functions callback.
1581 * @param[in] indent contains wrapper and indent in node numbers.
1582 * @param[in,out] out is output handler.
1583 */
1584static void
1585trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1586{
1587 trp_print_wrapper(indent.wrapper, out);
1588 trp_print_node(node, pck, indent.in_node, out);
1589}
1590
1591/**
aPiecek874ea4d2021-04-19 12:26:36 +02001592 * @brief Printing one line including wrapper and
1593 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001594 * @param[in] node is \<node\> representation.
1595 * @param[in] wr is wrapper for printing indentation before node.
1596 * @param[in] out is output handler.
1597 */
1598static void
1599trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1600{
1601 trp_print_wrapper(wr, out);
1602 trp_print_node_up_to_name(node, out);
1603}
1604
1605/**
aPiecek874ea4d2021-04-19 12:26:36 +02001606 * @brief Check if leafref target must be change to string 'leafref'
1607 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001608 * @param[in] node containing leafref target.
1609 * @param[in] wr is wrapper for printing indentation before node.
1610 * @param[in] mll is max line length.
1611 * @param[in] out is output handler.
1612 * @return true if leafref must be changed to string 'leafref'.
1613 */
1614static ly_bool
1615trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1616{
1617 struct ly_out_clb_arg *data;
1618
1619 if (node.type.type != TRD_TYPE_TARGET) {
1620 return 0;
1621 }
1622
1623 /* set ly_out to counting characters */
1624 data = out->method.clb.arg;
1625
1626 data->counter = 0;
1627 data->mode = TRD_CHAR_COUNT;
1628 /* count number of printed bytes */
1629 trp_print_wrapper(wr, out);
1630 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1631 trp_print_divided_node_up_to_name(node, out);
1632 data->mode = TRD_PRINT;
1633
1634 return data->counter + strlen(node.type.str) > mll;
1635}
1636
1637/**
1638 * @brief Get default indent in node based on node values.
1639 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001640 * @return Default indent in node assuming that the node
1641 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001642 */
1643static struct trt_indent_in_node
1644trp_default_indent_in_node(struct trt_node node)
1645{
1646 struct trt_indent_in_node ret;
1647
1648 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1649
1650 /* btw_name_opts */
1651 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1652
1653 /* btw_opts_type */
1654 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1655 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1656 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1657 TRD_INDENT_BEFORE_TYPE;
1658 } else {
1659 ret.btw_opts_type = 0;
1660 }
1661
1662 /* btw_type_iffeatures */
1663 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1664
1665 return ret;
1666}
1667
1668/**
1669 * @brief Setting linebreaks in trt_indent_in_node.
1670 *
1671 * The order where the linebreak tag can be placed is from the end.
1672 *
aPiecek874ea4d2021-04-19 12:26:36 +02001673 * @param[in] indent containing alignment lengths
1674 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001675 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001676 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1677 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001678 */
1679static struct trt_indent_in_node
1680trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1681{
1682 /* somewhere must be set a line break in node */
1683 struct trt_indent_in_node ret = indent;
1684
1685 /* gradually break the node from the end */
1686 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1687 ret.btw_type_iffeatures = TRD_LINEBREAK;
1688 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1689 ret.btw_opts_type = TRD_LINEBREAK;
1690 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1691 /* set line break between name and opts */
1692 ret.btw_name_opts = TRD_LINEBREAK;
1693 } else {
1694 /* it is not possible to place a more line breaks,
1695 * unfortunately the max_line_length constraint is violated
1696 */
1697 ret.type = TRD_INDENT_IN_NODE_FAILED;
1698 }
1699 return ret;
1700}
1701
1702/**
1703 * @brief Get the first half of the node based on the linebreak mark.
1704 *
1705 * Items in the second half of the node will be empty.
1706 *
1707 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001708 * @param[in] indent contains information in which part of the \<node\>
1709 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001710 * @return first half of the node, indent is unchanged.
1711 */
1712static struct trt_pair_indent_node
1713trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1714{
1715 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1716
1717 if (indent.btw_name_opts == TRD_LINEBREAK) {
1718 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1719 ret.node.type = TRP_EMPTY_TRT_TYPE;
1720 ret.node.iffeatures = 0;
1721 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1722 ret.node.type = TRP_EMPTY_TRT_TYPE;
1723 ret.node.iffeatures = 0;
1724 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1725 ret.node.iffeatures = 0;
1726 }
1727
1728 return ret;
1729}
1730
1731/**
1732 * @brief Get the second half of the node based on the linebreak mark.
1733 *
1734 * Items in the first half of the node will be empty.
1735 * Indentations belonging to the first node will be reset to zero.
1736 *
1737 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001738 * @param[in] indent contains information in which part of the \<node\>
1739 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001740 * @return second half of the node, indent is newly set.
1741 */
1742static struct trt_pair_indent_node
1743trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1744{
1745 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1746
1747 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001748 /* Logically, the information up to token <opts> should
1749 * be deleted, but the the trp_print_node function needs it to
1750 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001751 */
1752 ret.indent.btw_name_opts = 0;
1753 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1754 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1755 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1756 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1757 ret.indent.btw_name_opts = 0;
1758 ret.indent.btw_opts_type = 0;
1759 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1760 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1761 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1762 ret.node.type = TRP_EMPTY_TRT_TYPE;
1763 ret.indent.btw_name_opts = 0;
1764 ret.indent.btw_opts_type = 0;
1765 ret.indent.btw_type_iffeatures = 0;
1766 }
1767 return ret;
1768}
1769
1770/**
1771 * @brief Get the correct alignment for the node.
1772 *
aPiecek874ea4d2021-04-19 12:26:36 +02001773 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001774 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001775 *
1776 * @param[in] node is \<node\> representation.
1777 * @param[in] pck contains speciall callback functions for printing.
1778 * @param[in] indent contains wrapper and indent in node numbers.
1779 * @param[in] mll is max line length.
1780 * @param[in,out] cnt counting number of characters to print.
1781 * @param[in,out] out is output handler.
1782 * @return pair of node and indentation numbers of that node.
1783 */
1784static struct trt_pair_indent_node
1785trp_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)
1786{
1787 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1788
1789 trp_print_line(node, pck, indent, out);
1790
1791 if (*cnt <= mll) {
1792 /* success */
1793 return ret;
1794 } else {
1795 ret.indent = trp_indent_in_node_place_break(ret.indent);
1796 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1797 /* erase information in node due to line break */
1798 ret = trp_first_half_node(node, ret.indent);
1799 /* check if line fits, recursive call */
1800 *cnt = 0;
1801 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1802 /* make sure that the result will be with the status divided
1803 * or eventually with status failed */
1804 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1805 }
1806 return ret;
1807 }
1808}
1809
1810/**
1811 * @brief Get the correct alignment for the node.
1812 *
1813 * @param[in] node is \<node\> representation.
1814 * @param[in] pck contains speciall callback functions for printing.
1815 * @param[in] indent contains wrapper and indent in node numbers.
1816 * @param[in] mll is max line length.
1817 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001818 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1819 * line, some indent variable has negative value as a line break sign.
1820 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1821 * all indent variables values has non-negative number.
1822 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1823 * line, all indent variables has negative or zero values,
1824 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001825 */
1826static struct trt_pair_indent_node
1827trp_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)
1828{
1829 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1830 struct ly_out_clb_arg *data;
1831
1832 /* set ly_out to counting characters */
1833 data = out->method.clb.arg;
1834
1835 data->counter = 0;
1836 data->mode = TRD_CHAR_COUNT;
1837 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1838 data->mode = TRD_PRINT;
1839
1840 return ret;
1841}
1842
1843/**
aPiecek01598c02021-04-23 14:18:24 +02001844 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001845 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001846 * @param[in] node is node representation.
1847 * @param[in] ppck contains speciall callback functions for printing.
1848 * @param[in] ipck contains wrapper and indent in node numbers.
1849 * @param[in] mll is max line length.
1850 * @param[in,out] out is output handler.
1851 */
1852static void
1853trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1854{
1855 ly_bool entire_node_was_printed;
1856 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1857
1858 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1859 /* nothing can be done, continue as usual */
1860 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1861 }
1862
1863 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1864 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1865
1866 if (!entire_node_was_printed) {
1867 ly_print_(out, "\n");
1868 /* continue with second half node */
1869 ind_node = trp_second_half_node(node, ind_node.indent);
1870 /* continue with printing node */
1871 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1872 } else {
1873 return;
1874 }
1875}
1876
1877/**
aPiecek874ea4d2021-04-19 12:26:36 +02001878 * @brief Printing of the wrapper and the whole node,
1879 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001880 * @param[in] node is node representation.
1881 * @param[in] ppck contains speciall callback functions for printing.
1882 * @param[in] ipck contains wrapper and indent in node numbers.
1883 * @param[in] mll is max line length.
1884 * @param[in,out] out is output handler.
1885 */
1886static void
1887trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1888{
1889 struct trt_pair_indent_node ind_node1;
1890 struct trt_pair_indent_node ind_node2;
1891 struct trt_pck_indent tmp;
1892
1893 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1894 node.type.type = TRD_TYPE_LEAFREF;
1895 }
1896
1897 /* check if normal indent is possible */
1898 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1899
1900 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1901 /* node fits to one line */
1902 trp_print_line(node, ppck, ipck, out);
1903 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1904 /* node will be divided */
1905 /* print first half */
1906 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1907 /* pretend that this is normal node */
1908 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1909
1910 trp_print_line(ind_node1.node, ppck, tmp, out);
1911 ly_print_(out, "\n");
1912
1913 /* continue with second half on new line */
1914 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1915 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1916
1917 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1918 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1919 /* node name is too long */
1920 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1921
1922 if (trp_node_body_is_empty(node)) {
1923 return;
1924 } else {
1925 ly_print_(out, "\n");
1926
1927 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1928 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1929 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1930
1931 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1932 }
1933
1934 }
1935}
1936
aPiecek874ea4d2021-04-19 12:26:36 +02001937/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001938 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001939 *********************************************************************/
1940
1941/**
1942 * @brief Get nodetype.
1943 * @param[in] node is any lysp_node.
1944 */
1945static uint16_t
1946trop_nodetype(const void *node)
1947{
1948 return ((const struct lysp_node *)node)->nodetype;
1949}
1950
1951/**
1952 * @brief Get sibling.
1953 * @param[in] node is any lysp_node.
1954 */
1955static const void *
1956trop_next(const void *node)
1957{
1958 return ((const struct lysp_node *)node)->next;
1959}
1960
1961/**
1962 * @brief Get parent.
1963 * @param[in] node is any lysp_node.
1964 */
1965static const void *
1966trop_parent(const void *node)
1967{
1968 return ((const struct lysp_node *)node)->parent;
1969}
1970
1971/**
1972 * @brief Try to get child.
1973 * @param[in] node is any lysp_node.
1974 */
1975static const void *
1976trop_child(const void *node)
1977{
1978 return lysp_node_child(node);
1979}
1980
1981/**
1982 * @brief Try to get action.
1983 * @param[in] node is any lysp_node.
1984 */
1985static const void *
1986trop_actions(const void *node)
1987{
1988 return lysp_node_actions(node);
1989}
1990
1991/**
1992 * @brief Try to get action.
1993 * @param[in] node must be of type lysp_node_action.
1994 */
1995static const void *
1996trop_action_input(const void *node)
1997{
1998 return &((const struct lysp_node_action *)node)->input;
1999}
2000
2001/**
2002 * @brief Try to get action.
2003 * @param[in] node must be of type lysp_node_action.
2004 */
2005static const void *
2006trop_action_output(const void *node)
2007{
2008 return &((const struct lysp_node_action *)node)->output;
2009}
2010
2011/**
2012 * @brief Try to get action.
2013 * @param[in] node is any lysp_node.
2014 */
2015static const void *
2016trop_notifs(const void *node)
2017{
2018 return lysp_node_notifs(node);
2019}
2020
2021/**
aPiecek01598c02021-04-23 14:18:24 +02002022 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02002023 * which are adapted to lysp nodes.
2024 */
2025static struct tro_getters
2026trop_init_getters()
2027{
2028 return (struct tro_getters) {
2029 .nodetype = trop_nodetype,
2030 .next = trop_next,
2031 .parent = trop_parent,
2032 .child = trop_child,
2033 .actions = trop_actions,
2034 .action_input = trop_action_input,
2035 .action_output = trop_action_output,
2036 .notifs = trop_notifs
2037 };
2038}
2039
aPiecek3f247652021-04-19 13:40:25 +02002040/**
2041 * @brief Get nodetype.
2042 * @param[in] node is any lysc_node.
2043 */
2044static uint16_t
2045troc_nodetype(const void *node)
2046{
2047 return ((const struct lysc_node *)node)->nodetype;
2048}
2049
2050/**
2051 * @brief Get sibling.
2052 * @param[in] node is any lysc_node.
2053 */
2054static const void *
2055troc_next(const void *node)
2056{
2057 return ((const struct lysc_node *)node)->next;
2058}
2059
2060/**
2061 * @brief Get parent.
2062 * @param[in] node is any lysc_node.
2063 */
2064static const void *
2065troc_parent(const void *node)
2066{
2067 return ((const struct lysc_node *)node)->parent;
2068}
2069
2070/**
2071 * @brief Try to get child.
2072 * @param[in] node is any lysc_node.
2073 */
2074static const void *
2075troc_child(const void *node)
2076{
2077 return lysc_node_child(node);
2078}
2079
2080/**
2081 * @brief Try to get action.
2082 * @param[in] node is any lysc_node.
2083 */
2084static const void *
2085troc_actions(const void *node)
2086{
2087 return lysc_node_actions(node);
2088}
2089
2090/**
2091 * @brief Try to get action.
2092 * @param[in] node must be of type lysc_node_action.
2093 */
2094static const void *
2095troc_action_input(const void *node)
2096{
2097 return &((const struct lysc_node_action *)node)->input;
2098}
2099
2100/**
2101 * @brief Try to get action.
2102 * @param[in] node must be of type lysc_node_action.
2103 */
2104static const void *
2105troc_action_output(const void *node)
2106{
2107 return &((const struct lysc_node_action *)node)->output;
2108}
2109
2110/**
2111 * @brief Try to get action.
2112 * @param[in] node is any lysc_node.
2113 */
2114static const void *
2115troc_notifs(const void *node)
2116{
2117 return lysc_node_notifs(node);
2118}
2119
2120/**
aPiecek01598c02021-04-23 14:18:24 +02002121 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002122 * which are adapted to lysc nodes.
2123 */
2124static struct tro_getters
2125troc_init_getters()
2126{
2127 return (struct tro_getters) {
2128 .nodetype = troc_nodetype,
2129 .next = troc_next,
2130 .parent = troc_parent,
2131 .child = troc_child,
2132 .actions = troc_actions,
2133 .action_input = troc_action_input,
2134 .action_output = troc_action_output,
2135 .notifs = troc_notifs
2136 };
2137}
2138
aPiecekef1e58e2021-04-19 13:19:44 +02002139/**********************************************************************
2140 * tro functions
2141 *********************************************************************/
2142
2143/**
2144 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002145 *
2146 * This is a general algorithm that is able to
2147 * work with lysp_node or lysc_node.
2148 *
2149 * @param[in] node points to lysp_node or lysc_node.
2150 * @param[in] lysc_tree flag to determine what type the @p node is.
2151 * If set to true, then @p points to lysc_node otherwise lysp_node.
2152 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002153 */
2154static const void *
aPiecek3f247652021-04-19 13:40:25 +02002155tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002156{
2157 struct tro_getters get;
2158 const void *tmp, *parent;
2159 const void *ret;
2160
2161 assert(node);
2162
aPiecek3f247652021-04-19 13:40:25 +02002163 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002164
2165 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2166 if ((tmp = get.next(node))) {
2167 /* next action exists */
2168 ret = tmp;
2169 } else if ((parent = get.parent(node))) {
2170 /* maybe if notif exists as sibling */
2171 ret = get.notifs(parent);
2172 } else {
2173 ret = NULL;
2174 }
2175 } else if (get.nodetype(node) & LYS_INPUT) {
2176 if ((parent = get.parent(node))) {
2177 /* if output action has data */
2178 if (get.child(get.action_output(parent))) {
2179 /* then next sibling is output action */
2180 ret = get.action_output(parent);
2181 } else {
2182 /* input action cannot have siblings other
2183 * than output action.
2184 */
2185 ret = NULL;
2186 }
2187 } else {
2188 /* there is no way how to get output action */
2189 ret = NULL;
2190 }
2191 } else if (get.nodetype(node) & LYS_OUTPUT) {
2192 /* output action cannot have siblings */
2193 ret = NULL;
2194 } else if (get.nodetype(node) & LYS_NOTIF) {
2195 /* must have as a sibling only notif */
2196 ret = get.next(node);
2197 } else {
2198 /* for rest of nodes */
2199 if ((tmp = get.next(node))) {
2200 /* some sibling exists */
2201 ret = tmp;
2202 } else if ((parent = get.parent(node))) {
2203 /* Action and notif are siblings too.
2204 * They can be reached through parent.
2205 */
2206 if ((tmp = get.actions(parent))) {
2207 /* next sibling is action */
2208 ret = tmp;
2209 } else if ((tmp = get.notifs(parent))) {
2210 /* next sibling is notif */
2211 ret = tmp;
2212 } else {
2213 /* sibling not exists */
2214 ret = NULL;
2215 }
2216 } else {
2217 /* sibling not exists */
2218 ret = NULL;
2219 }
2220 }
2221
2222 return ret;
2223}
2224
2225/**
2226 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002227 *
2228 * This is a general algorithm that is able to
2229 * work with lysp_node or lysc_node.
2230 *
2231 * @param[in] node points to lysp_node or lysc_node.
2232 * @param[in] lysc_tree flag to determine what type the @p node is.
2233 * If set to true, then @p points to lysc_node otherwise lysp_node.
2234 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002235 */
2236static const void *
aPiecek3f247652021-04-19 13:40:25 +02002237tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002238{
2239 struct tro_getters get;
2240 const void *tmp;
2241 const void *ret;
2242
2243 assert(node);
2244
aPiecek3f247652021-04-19 13:40:25 +02002245 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002246
2247 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2248 if (get.child(get.action_input(node))) {
2249 /* go to LYS_INPUT */
2250 ret = get.action_input(node);
2251 } else if (get.child(get.action_output(node))) {
2252 /* go to LYS_OUTPUT */
2253 ret = get.action_output(node);
2254 } else {
2255 /* input action and output action have no data */
2256 ret = NULL;
2257 }
2258 } else {
2259 if ((tmp = get.child(node))) {
2260 ret = tmp;
2261 } else {
2262 /* current node can't have children or has no children */
2263 /* but maybe has some actions or notifs */
2264 if ((tmp = get.actions(node))) {
2265 ret = tmp;
2266 } else if ((tmp = get.notifs(node))) {
2267 ret = tmp;
2268 } else {
2269 ret = NULL;
2270 }
2271 }
2272 }
2273
2274 return ret;
2275}
2276
2277/**
aPiecek3f247652021-04-19 13:40:25 +02002278 * @brief Get new trt_parent_cache if we apply the transfer
2279 * to the child node in the tree.
2280 * @param[in] ca is parent cache for current node.
2281 * @param[in] tc contains current tree node.
2282 * @return Cache for the current node.
2283 */
2284static struct trt_parent_cache
2285tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2286{
2287 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2288
2289 if (!tc->lysc_tree) {
2290 const struct lysp_node *pn = tc->pn;
2291
2292 ret.ancestor =
2293 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2294 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2295 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2296 ca.ancestor;
2297
2298 ret.lys_status =
2299 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2300 ca.lys_status;
2301
2302 ret.lys_config =
2303 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2304 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2305 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2306 ca.lys_config;
2307
2308 ret.last_list =
2309 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2310 ca.last_list;
2311 }
2312
2313 return ret;
2314}
2315
2316/**
aPiecekef1e58e2021-04-19 13:19:44 +02002317 * @brief Transformation of the Schema nodes flags to
2318 * Tree diagram \<status\>.
2319 * @param[in] flags is node's flags obtained from the tree.
2320 */
2321static trt_status_type
2322tro_flags2status(uint16_t flags)
2323{
2324 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2325 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2326 TRD_STATUS_TYPE_CURRENT;
2327}
2328
2329/**
2330 * @brief Transformation of the Schema nodes flags to Tree diagram
2331 * \<flags\> but more specifically 'ro' or 'rw'.
2332 * @param[in] flags is node's flags obtained from the tree.
2333 */
2334static trt_flags_type
2335tro_flags2config(uint16_t flags)
2336{
2337 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2338 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2339 TRD_FLAGS_TYPE_EMPTY;
2340}
2341
2342/**
aPiecek3f247652021-04-19 13:40:25 +02002343 * @brief Print current node's iffeatures.
2344 * @param[in] tc is tree context.
2345 * @param[in,out] out is output handler.
2346 */
2347static void
2348tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2349{
2350 const struct lysp_qname *iffs;
2351
aPiecekbbc02932021-05-21 07:19:41 +02002352 if (tc->lysc_tree) {
2353 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2354 iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
2355 } else {
2356 iffs = tc->pn->iffeatures;
2357 }
aPiecek3f247652021-04-19 13:40:25 +02002358 LY_ARRAY_COUNT_TYPE i;
2359
2360 LY_ARRAY_FOR(iffs, i) {
2361 if (i == 0) {
2362 ly_print_(out, "%s", iffs[i].str);
2363 } else {
2364 ly_print_(out, ",%s", iffs[i].str);
2365 }
2366 }
2367
2368}
2369
2370/**
2371 * @brief Print current list's keys.
2372 *
2373 * Well, actually printing keys in the lysp_tree is trivial,
2374 * because char* points to all keys. However, special functions have
2375 * been reserved for this, because in principle the list of elements
2376 * can have more implementations.
2377 *
2378 * @param[in] tc is tree context.
2379 * @param[in,out] out is output handler.
2380 */
2381static void
2382tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2383{
2384 const struct lysp_node_list *list;
2385
aPiecekbbc02932021-05-21 07:19:41 +02002386 if (tc->lysc_tree) {
2387 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2388 list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
2389 } else {
2390 list = (const struct lysp_node_list *)tc->pn;
2391 }
aPiecek3f247652021-04-19 13:40:25 +02002392 assert(list->nodetype & LYS_LIST);
2393
2394 if (trg_charptr_has_data(list->key)) {
2395 ly_print_(out, "%s", list->key);
2396 }
2397}
2398
2399/**
2400 * @brief Get rpcs section if exists.
2401 * @param[in,out] tc is tree context.
2402 * @return Section representation if it exists. The @p tc is modified
2403 * and his pointer points to the first node in rpcs section.
2404 * @return Empty section representation otherwise.
2405 */
2406static struct trt_keyword_stmt
2407tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2408{
aPiecek9f792e52021-04-21 08:33:56 +02002409 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002410 const void *actions;
2411
2412 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002413 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002414 if (actions) {
2415 tc->cn = actions;
2416 }
2417 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002418 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002419 if (actions) {
2420 tc->pn = actions;
2421 tc->tpn = tc->pn;
2422 }
2423 }
2424
2425 if (actions) {
2426 tc->section = TRD_SECT_RPCS;
2427 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2428 } else {
2429 return TRP_EMPTY_KEYWORD_STMT;
2430 }
2431}
2432
2433/**
2434 * @brief Get notification section if exists
2435 * @param[in,out] tc is tree context.
2436 * @return Section representation if it exists.
2437 * The @p tc is modified and his pointer points to the
2438 * first node in notification section.
2439 * @return Empty section representation otherwise.
2440 */
2441static struct trt_keyword_stmt
2442tro_modi_get_notifications(struct trt_tree_ctx *tc)
2443{
aPiecek9f792e52021-04-21 08:33:56 +02002444 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002445 const void *notifs;
2446
2447 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002448 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002449 if (notifs) {
2450 tc->cn = notifs;
2451 }
2452 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002453 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002454 if (notifs) {
2455 tc->pn = notifs;
2456 tc->tpn = tc->pn;
2457 }
2458 }
2459
2460 if (notifs) {
2461 tc->section = TRD_SECT_NOTIF;
2462 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2463 } else {
2464 return TRP_EMPTY_KEYWORD_STMT;
2465 }
2466}
2467
2468/**
aPiecek96baa7f2021-04-23 12:32:00 +02002469 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002470 *
2471 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002472 * @param[in] u is index to the array of extensions (lysc_ext_instance
2473 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002474 * @return Section representation if it exists.
2475 * @return Empty section representation otherwise.
2476 */
2477static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002478tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002479{
aPiecek96baa7f2021-04-23 12:32:00 +02002480 assert(tc);
2481 const void *node;
2482 const char *yang_data_name;
2483
2484 if (tc->lysc_tree) {
2485 struct lysc_ext_instance *exts;
2486 struct lysc_ext_substmt *substmts;
2487
2488 exts = tc->cmod->exts;
2489 substmts = exts[u].substmts;
2490 if (!substmts) {
2491 return TRP_EMPTY_KEYWORD_STMT;
2492 }
2493 node = *(const struct lysc_node **)substmts->storage;
2494 yang_data_name = exts[u].argument;
2495 } else {
2496 struct lysp_ext_instance *exts;
2497
2498 exts = tc->pmod->exts;
2499 node = exts[u].parsed;
2500 yang_data_name = exts[u].argument;
2501 }
2502
2503 if (tc->lysc_tree) {
2504 tc->cn = node;
2505 } else {
2506 tc->tpn_ext = &tc->pmod->exts[u];
2507 tc->pn = node;
2508 }
2509
2510 if (node) {
2511 tc->section = TRD_SECT_YANG_DATA;
2512 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2513 } else {
2514 return TRP_EMPTY_KEYWORD_STMT;
2515 }
aPiecekef1e58e2021-04-19 13:19:44 +02002516}
2517
2518/**
2519 * @brief Get name of the module.
2520 * @param[in] tc is context of the tree.
2521 */
2522static struct trt_keyword_stmt
2523tro_read_module_name(const struct trt_tree_ctx *tc)
2524{
aPiecek9f792e52021-04-21 08:33:56 +02002525 assert(tc);
2526
2527 struct trt_keyword_stmt ret;
2528
2529 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2530 TRD_KEYWORD_SUBMODULE :
2531 TRD_KEYWORD_MODULE;
2532
2533 ret.str = !tc->lysc_tree ?
2534 LYSP_MODULE_NAME(tc->pmod) :
2535 tc->cmod->mod->name;
2536
2537 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002538}
2539
aPiecekb8d5a0a2021-05-20 08:20:24 +02002540/**
2541 * @brief Create implicit "case" node as parent of @p node.
2542 * @param[in] node child of implicit case node.
2543 * @return The case node ready to print.
2544 */
2545static struct trt_node
2546tro_create_implicit_case_node(struct trt_node node)
2547{
2548 struct trt_node ret;
2549
2550 ret.status = node.status;
2551 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2552 ret.name.type = TRD_NODE_CASE;
2553 ret.name.module_prefix = node.name.module_prefix;
2554 ret.name.str = node.name.str;
2555 ret.type = TRP_EMPTY_TRT_TYPE;
aPiecek7a28e2f2021-05-21 07:27:03 +02002556 ret.iffeatures = 0;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002557 ret.last_one = node.last_one;
aPiecek50b64e42022-10-10 10:00:12 +02002558 ret.mount = NULL;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002559
2560 return ret;
2561}
2562
aPiecekef1e58e2021-04-19 13:19:44 +02002563/**********************************************************************
2564 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002565 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002566
2567/**
aPiecek61d062b2020-11-02 11:05:09 +01002568 * @brief Check if list statement has keys.
2569 * @param[in] pn is pointer to the list.
2570 * @return 1 if has keys, otherwise 0.
2571 */
2572static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002573trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002574{
aPiecekef1e58e2021-04-19 13:19:44 +02002575 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002576}
2577
2578/**
2579 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002580 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002581 * @return 1 if has if-features, otherwise 0.
2582 */
2583static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002584trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002585{
2586 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002587 const struct lysp_qname *iffs;
2588
aPiecek61d062b2020-11-02 11:05:09 +01002589 ly_bool ret = 0;
2590
aPiecekef1e58e2021-04-19 13:19:44 +02002591 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002592 LY_ARRAY_FOR(iffs, u) {
2593 ret = 1;
2594 break;
2595 }
2596 return ret;
2597}
2598
2599/**
2600 * @brief Find out if leaf is also the key in last list.
2601 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002602 * @param[in] ca_last_list is pointer to last visited list.
2603 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002604 * @return 1 if leaf is also the key, otherwise 0.
2605 */
2606static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002607trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002608{
2609 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2610 const struct lysp_node_list *list = ca_last_list;
2611
2612 if (!list) {
2613 return 0;
2614 }
2615 return trg_charptr_has_data(list->key) ?
2616 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2617}
2618
2619/**
2620 * @brief Check if container's type is presence.
2621 * @param[in] pn is pointer to container.
2622 * @return 1 if container has presence statement, otherwise 0.
2623 */
2624static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002625trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002626{
2627 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2628}
2629
2630/**
aPiecek7ed8d032022-10-10 12:32:27 +02002631 * @brief Check if container has mount-point extension.
ekinzie0ab8b302022-10-10 03:03:57 -04002632 * @param[in] cn is pointer to container or list.
2633 * @param[out] mount is assigned a pointer to the extension instance, if found
aPiecek7ed8d032022-10-10 12:32:27 +02002634 * @return 1 if node has mount-point.
ekinzie0ab8b302022-10-10 03:03:57 -04002635 */
2636static ly_bool
2637troc_node_has_mount(const struct lysc_node *cn, struct lysc_ext_instance **mount)
2638{
2639 struct lysc_ext_instance *ext;
2640 uint64_t u;
2641
2642 /* The schema-mount extension plugin has already made sure that
2643 * there is only one mount-point here.
2644 */
2645 LY_ARRAY_FOR(cn->exts, u) {
2646 ext = &cn->exts[u];
2647 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") ||
2648 strcmp(ext->def->name, "mount-point")) {
2649 continue;
2650 }
2651 *mount = ext;
2652 return 1;
2653 }
2654 return 0;
2655}
2656
aPiecek7ed8d032022-10-10 12:32:27 +02002657/**
2658 * @brief Check if node has mount-point extension.
2659 *
2660 * @param[in] pn is pointer to container or list.
2661 * @return 1 if node has mount-point.
2662 */
ekinzie0ab8b302022-10-10 03:03:57 -04002663static ly_bool
2664trop_node_has_mount(const struct lysp_node *pn)
2665{
2666 struct lysp_ext_instance *ext;
2667 uint64_t u;
2668
2669 LY_ARRAY_FOR(pn->exts, u) {
2670 ext = &pn->exts[u];
2671 if (strcmp(ext->name, "yangmnt:mount-point")) {
2672 continue;
2673 }
2674 return 1;
2675 }
2676 return 0;
2677}
2678
2679/**
aPiecek61d062b2020-11-02 11:05:09 +01002680 * @brief Get leaflist's path without lysp_node type control.
2681 * @param[in] pn is pointer to the leaflist.
2682 */
2683static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002684trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002685{
2686 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2687
2688 return list->type.path ? list->type.path->expr : NULL;
2689}
2690
2691/**
2692 * @brief Get leaflist's type name without lysp_node type control.
2693 * @param[in] pn is pointer to the leaflist.
2694 */
2695static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002696trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002697{
2698 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2699
2700 return list->type.name;
2701}
2702
2703/**
2704 * @brief Get leaf's path without lysp_node type control.
2705 * @param[in] pn is pointer to the leaf node.
2706 */
2707static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002708trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002709{
2710 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2711
2712 return leaf->type.path ? leaf->type.path->expr : NULL;
2713}
2714
2715/**
2716 * @brief Get leaf's type name without lysp_node type control.
2717 * @param[in] pn is pointer to the leaf's type name.
2718 */
2719static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002720trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002721{
2722 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2723
2724 return leaf->type.name;
2725}
2726
2727/**
aPiecek874ea4d2021-04-19 12:26:36 +02002728 * @brief Get pointer to data using node type specification
2729 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002730 *
aPiecek874ea4d2021-04-19 12:26:36 +02002731 * @param[in] flags is node type specification.
2732 * If it is the correct node, the getter function is called.
2733 * @param[in] f is getter function which provides the desired
2734 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002735 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002736 * @return NULL if node has wrong type or getter function return
2737 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002738 * @return Pointer to desired char pointer obtained from the node.
2739 */
2740static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002741trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002742{
2743 if (pn->nodetype & flags) {
2744 const char *ret = f(pn);
Michal Vasko26bbb272022-08-02 14:54:33 +02002745
aPiecek61d062b2020-11-02 11:05:09 +01002746 return trg_charptr_has_data(ret) ? ret : NULL;
2747 } else {
2748 return NULL;
2749 }
2750}
2751
2752/**
aPiecek61d062b2020-11-02 11:05:09 +01002753 * @brief Resolve \<status\> of the current node.
2754 * @param[in] nodetype is node's type obtained from the tree.
2755 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002756 * @param[in] ca_lys_status is inherited status
2757 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002758 * @return The status type.
2759 */
2760static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002761trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002762{
2763 /* LYS_INPUT and LYS_OUTPUT is special case */
2764 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002765 return tro_flags2status(ca_lys_status);
2766 /* if ancestor's status is deprc or obslt
2767 * and also node's status is not set
2768 */
aPiecek61d062b2020-11-02 11:05:09 +01002769 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2770 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002771 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002772 } else {
2773 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002774 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002775 }
2776}
2777
2778/**
2779 * @brief Resolve \<flags\> of the current node.
2780 * @param[in] nodetype is node's type obtained from the tree.
2781 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002782 * @param[in] ca_ancestor is ancestor type obtained
2783 * from trt_parent_cache.
2784 * @param[in] ca_lys_config is inherited config item
2785 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002786 * @return The flags type.
2787 */
2788static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002789trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002790{
2791 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2792 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2793 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2794 return TRD_FLAGS_TYPE_RO;
2795 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2796 return TRD_FLAGS_TYPE_RO;
2797 } else if (nodetype & LYS_NOTIF) {
2798 return TRD_FLAGS_TYPE_NOTIF;
2799 } else if (nodetype & LYS_USES) {
2800 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2801 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2802 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002803 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002804 /* config is not set. Look at ancestor's config */
2805 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002806 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002807 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002808 }
2809}
2810
2811/**
2812 * @brief Resolve node type of the current node.
2813 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002814 * @param[in] ca_last_list is pointer to the last visited list.
2815 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002816 */
2817static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04002818trop_resolve_node_type(const struct trt_tree_ctx *tc, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002819{
ekinzie0ab8b302022-10-10 03:03:57 -04002820 const struct lysp_node *pn = tc->pn;
2821
aPiecek61d062b2020-11-02 11:05:09 +01002822 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2823 return TRD_NODE_ELSE;
2824 } else if (pn->nodetype & LYS_CASE) {
2825 return TRD_NODE_CASE;
2826 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2827 return TRD_NODE_OPTIONAL_CHOICE;
2828 } else if (pn->nodetype & LYS_CHOICE) {
2829 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04002830 } else if (tc->mounted && (tc->pn->parent == NULL)) {
2831 if (tc->parent_refs) {
2832 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
2833 if (!strcmp(tc->pmod->mod->ns, tc->parent_refs->snodes[v]->module->ns)) {
2834 return TRD_NODE_TOP_LEVEL2;
2835 }
2836 }
2837 }
2838 return TRD_NODE_TOP_LEVEL1;
aPiecekef1e58e2021-04-19 13:19:44 +02002839 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002840 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002841 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002842 return TRD_NODE_KEYS;
2843 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2844 return TRD_NODE_LISTLEAFLIST;
2845 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2846 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002847 } 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 +01002848 return TRD_NODE_OPTIONAL;
2849 } else {
2850 return TRD_NODE_ELSE;
2851 }
2852}
2853
2854/**
aPiecekef1e58e2021-04-19 13:19:44 +02002855 * @brief Resolve \<type\> of the current node.
2856 * @param[in] pn is current node.
2857 */
2858static struct trt_type
2859trop_resolve_type(const struct lysp_node *pn)
2860{
2861 const char *tmp = NULL;
2862
2863 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2864 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2865 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2866 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2867 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2868 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2869 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2870 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2871 } else if (pn->nodetype == LYS_ANYDATA) {
2872 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2873 } else if (pn->nodetype & LYS_ANYXML) {
2874 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2875 } else {
2876 return TRP_EMPTY_TRT_TYPE;
2877 }
2878}
2879
2880/**
aPiecek61d062b2020-11-02 11:05:09 +01002881 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002882 * @param[in] ca contains stored important data
2883 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002884 * @param[in] tc is context of the tree.
2885 */
2886static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002887trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002888{
aPiecekef1e58e2021-04-19 13:19:44 +02002889 const struct lysp_node *pn;
2890 struct trt_node ret;
2891
aPiecek61d062b2020-11-02 11:05:09 +01002892 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002893
2894 pn = tc->pn;
2895 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002896
2897 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002898 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002899
aPiecek61d062b2020-11-02 11:05:09 +01002900 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04002901 if (trop_node_has_mount(pn)) {
2902 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
2903 } else {
2904 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
2905 }
aPiecek61d062b2020-11-02 11:05:09 +01002906
aPiecek61d062b2020-11-02 11:05:09 +01002907 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04002908 ret.name.type = trop_resolve_node_type(tc, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002909
aPiecek34fa3772021-05-21 12:35:46 +02002910 /* The parsed tree is not compiled, so no node can be augmented
2911 * from another module. This means that nodes from the parsed tree
2912 * will never have the prefix.
2913 */
aPiecek61d062b2020-11-02 11:05:09 +01002914 ret.name.module_prefix = NULL;
2915
2916 /* set node's name */
2917 ret.name.str = pn->name;
2918
2919 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002920 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002921
2922 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002923 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002924
aPiecek3f247652021-04-19 13:40:25 +02002925 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002926
2927 return ret;
2928}
2929
aPiecekef1e58e2021-04-19 13:19:44 +02002930/**
2931 * @brief Find out if the current node has siblings.
2932 * @param[in] tc is context of the tree.
2933 * @return 1 if sibling exists otherwise 0.
2934 */
2935static ly_bool
2936trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2937{
aPiecek3f247652021-04-19 13:40:25 +02002938 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002939}
2940
aPiecek96baa7f2021-04-23 12:32:00 +02002941/**
2942 * @brief Print all yang-data sections and print three dots instead
2943 * of nodes.
2944 * @param[in] exts is array of YANG extension instances from parsed
2945 * module (@ref sizedarrays).
2946 * @param[in] mll is maximum number of characters that can be printed
2947 * on one line.
2948 * @param[in,out] out is output handler.
2949 */
2950static void
2951trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2952{
2953 struct trt_keyword_stmt ks;
2954 LY_ARRAY_COUNT_TYPE u;
2955 struct trt_wrapper wr;
2956
2957 if (!exts) {
2958 return;
2959 }
2960
2961 ly_print_(out, "\n");
2962 ks.type = TRD_KEYWORD_YANG_DATA;
2963 wr = TRP_INIT_WRAPPER_BODY;
2964
2965 LY_ARRAY_FOR(exts, u) {
2966 ly_print_(out, "\n");
2967
2968 /* yang-data <yang-data-name>: */
2969 ks.str = exts[u].argument;
2970 trp_print_keyword_stmt(ks, mll, 0, out);
2971 ly_print_(out, "\n");
2972
2973 /* ... */
2974 trp_print_wrapper(wr, out);
2975 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2976 }
2977}
2978
aPiecek874ea4d2021-04-19 12:26:36 +02002979/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002980 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002981 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002982
2983/**
aPiecek874ea4d2021-04-19 12:26:36 +02002984 * @brief Change current node pointer to its parent
2985 * but only if parent exists.
2986 * @param[in,out] tc is tree context.
2987 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002988 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002989 * @return 0 if the node did not have parents.
2990 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002991 */
2992static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002993trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002994{
2995 assert(tc && tc->pn);
2996 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002997 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002998 tc->pn = tc->pn->parent;
2999 return 1;
3000 } else {
3001 return 0;
3002 }
3003}
3004
3005/**
aPiecek874ea4d2021-04-19 12:26:36 +02003006 * @brief Change the current node pointer to its child
3007 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003008 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003009 * @param[in,out] tc is context of the tree.
3010 * Contains pointer to the current node.
3011 * @return Non-empty \<node\> representation of the current
3012 * node's child. The @p tc is modified.
3013 * @return Empty \<node\> representation if child don't exists.
3014 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003015 */
3016static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003017trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003018{
aPiecekef1e58e2021-04-19 13:19:44 +02003019 const struct lysp_node *tmp;
3020
aPiecek61d062b2020-11-02 11:05:09 +01003021 assert(tc && tc->pn);
3022
aPiecek3f247652021-04-19 13:40:25 +02003023 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02003024 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02003025 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01003026 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02003027 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01003028 }
3029}
3030
3031/**
aPiecek874ea4d2021-04-19 12:26:36 +02003032 * @brief Change the current node pointer to the first child of node's
3033 * parent. If current node is already first sibling/child then nothing
3034 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01003035 * @param[in,out] tc is tree context.
3036 */
3037static void
aPiecekef1e58e2021-04-19 13:19:44 +02003038trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003039{
aPiecek9f792e52021-04-21 08:33:56 +02003040 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01003041
aPiecekef1e58e2021-04-19 13:19:44 +02003042 if (trop_modi_parent(tc)) {
3043 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003044 } else {
3045 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01003046 switch (tc->section) {
3047 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003048 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02003049 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003050 break;
3051 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02003052 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02003053 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003054 break;
3055 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003056 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02003057 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003058 break;
3059 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003060 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02003061 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003062 break;
3063 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02003064 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02003065 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003066 break;
3067 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003068 /* tpn in this case is of type lysp_ext_instance */
3069 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01003070 break;
aPiecek96baa7f2021-04-23 12:32:00 +02003071 default:
3072 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01003073 }
aPiecek61d062b2020-11-02 11:05:09 +01003074 }
3075}
3076
3077/**
aPiecek874ea4d2021-04-19 12:26:36 +02003078 * @brief Change the pointer to the current node to its next sibling
3079 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003080 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003081 * @param[in,out] tc is tree context.
3082 * Contains pointer to the current node.
3083 * @return Non-empty \<node\> representation if sibling exists.
3084 * The @p tc is modified.
3085 * @return Empty \<node\> representation otherwise.
3086 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003087 */
3088static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003089trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003090{
aPiecekef1e58e2021-04-19 13:19:44 +02003091 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003092
3093 assert(tc && tc->pn);
3094
aPiecek3f247652021-04-19 13:40:25 +02003095 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01003096
aPiecekef1e58e2021-04-19 13:19:44 +02003097 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003098 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3099 tc->tpn = pn;
3100 }
aPiecekef1e58e2021-04-19 13:19:44 +02003101 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003102 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003103 } else {
3104 return TRP_EMPTY_NODE;
3105 }
3106}
3107
3108/**
3109 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003110 * @param[in,out] tc is tree context. It is modified and his current
3111 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003112 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003113 * @return Empty section structure otherwise.
3114 */
3115static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003116trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003117{
aPiecek9f792e52021-04-21 08:33:56 +02003118 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003119 const struct lysp_node_augment *augs;
3120
3121 /* if next_augment func was called for the first time */
3122 if (tc->section != TRD_SECT_AUGMENT) {
3123 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003124 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003125 } else {
3126 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003127 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003128 }
3129
aPiecekdc8fd572021-04-19 10:47:23 +02003130 if (augs) {
3131 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003132 tc->tpn = tc->pn;
3133 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3134 } else {
3135 return TRP_EMPTY_KEYWORD_STMT;
3136 }
3137}
3138
3139/**
aPiecek61d062b2020-11-02 11:05:09 +01003140 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003141 * @param[in,out] tc is tree context. It is modified and his current
3142 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003143 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003144 * @return Empty section representation otherwise.
3145 */
3146static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003147trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003148{
aPiecek9f792e52021-04-21 08:33:56 +02003149 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003150 const struct lysp_node_grp *grps;
3151
3152 if (tc->section != TRD_SECT_GROUPING) {
3153 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003154 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003155 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003156 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003157 }
3158
aPiecekdc8fd572021-04-19 10:47:23 +02003159 if (grps) {
3160 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003161 tc->tpn = tc->pn;
3162 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3163 } else {
3164 return TRP_EMPTY_KEYWORD_STMT;
3165 }
3166}
3167
aPiecek874ea4d2021-04-19 12:26:36 +02003168/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003169 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003170 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003171
3172/**
aPiecek3f247652021-04-19 13:40:25 +02003173 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003174 */
aPiecek3f247652021-04-19 13:40:25 +02003175static ly_bool
3176troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003177{
aPiecek3f247652021-04-19 13:40:25 +02003178 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3179}
aPiecek61d062b2020-11-02 11:05:09 +01003180
aPiecek3f247652021-04-19 13:40:25 +02003181/**
3182 * @brief Resolve \<flags\> of the current node.
3183 *
3184 * Use this function only if trt_tree_ctx.lysc_tree is true.
3185 *
3186 * @param[in] nodetype is current lysc_node.nodetype.
3187 * @param[in] flags is current lysc_node.flags.
3188 * @return The flags type.
3189 */
3190static trt_flags_type
3191troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3192{
3193 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3194 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3195 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3196 return TRD_FLAGS_TYPE_RO;
3197 } else if (nodetype & LYS_IS_NOTIF) {
3198 return TRD_FLAGS_TYPE_RO;
3199 } else if (nodetype & LYS_NOTIF) {
3200 return TRD_FLAGS_TYPE_NOTIF;
3201 } else if (nodetype & LYS_USES) {
3202 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3203 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3204 return TRD_FLAGS_TYPE_RPC;
3205 } else {
3206 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003207 }
aPiecek61d062b2020-11-02 11:05:09 +01003208}
3209
3210/**
aPiecek3f247652021-04-19 13:40:25 +02003211 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003212 *
aPiecek3f247652021-04-19 13:40:25 +02003213 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003214 *
aPiecek3f247652021-04-19 13:40:25 +02003215 * @param[in] nodetype is current lysc_node.nodetype.
3216 * @param[in] flags is current lysc_node.flags.
3217 */
3218static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04003219troc_resolve_node_type(const struct trt_tree_ctx *tc, uint16_t nodetype, uint16_t flags)
aPiecek3f247652021-04-19 13:40:25 +02003220{
3221 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3222 return TRD_NODE_ELSE;
3223 } else if (nodetype & LYS_CASE) {
3224 return TRD_NODE_CASE;
3225 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3226 return TRD_NODE_OPTIONAL_CHOICE;
3227 } else if (nodetype & LYS_CHOICE) {
3228 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04003229 } else if (tc->mounted && (tc->cn->parent == NULL)) {
3230 if (tc->parent_refs) {
3231 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
3232 if (!strcmp(tc->cn->module->ns, tc->parent_refs->snodes[v]->module->ns)) {
3233 return TRD_NODE_TOP_LEVEL2;
3234 }
3235 }
3236 }
3237 return TRD_NODE_TOP_LEVEL1;
aPiecek3f247652021-04-19 13:40:25 +02003238 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3239 return TRD_NODE_CONTAINER;
3240 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3241 return TRD_NODE_KEYS;
3242 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3243 return TRD_NODE_LISTLEAFLIST;
3244 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3245 return TRD_NODE_OPTIONAL;
3246 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3247 return TRD_NODE_OPTIONAL;
3248 } else {
3249 return TRD_NODE_ELSE;
3250 }
3251}
3252
3253/**
aPiecek34fa3772021-05-21 12:35:46 +02003254 * @brief Resolve prefix (<prefix>:<name>) of node that has been
3255 * placed from another module via an augment statement.
3256 *
3257 * @param[in] cn is current compiled node.
3258 * @param[in] current_compiled_module is module whose nodes are
3259 * currently being printed.
3260 * @return Prefix of foreign module or NULL.
3261 */
3262static const char *
3263troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
3264{
3265 const struct lys_module *node_module;
3266 const char *ret = NULL;
3267
3268 node_module = cn->module;
3269 if (node_module->compiled != current_compiled_module) {
3270 ret = node_module->prefix;
3271 }
3272
3273 return ret;
3274}
3275
3276/**
aPiecek3f247652021-04-19 13:40:25 +02003277 * @brief Transformation of current lysc_node to struct trt_node.
3278 * @param[in] ca is not used.
3279 * @param[in] tc is context of the tree.
3280 */
3281static struct trt_node
3282troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3283{
3284 (void) ca;
3285 const struct lysc_node *cn;
3286 struct trt_node ret;
3287
aPiecek082c7dc2021-05-20 08:55:07 +02003288 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003289
3290 cn = tc->cn;
3291 ret = TRP_EMPTY_NODE;
3292
3293 /* <status> */
3294 ret.status = tro_flags2status(cn->flags);
3295
aPiecek3f247652021-04-19 13:40:25 +02003296 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04003297 if (troc_node_has_mount(cn, &ret.mount)) {
3298 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
3299 } else {
3300 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3301 }
aPiecek3f247652021-04-19 13:40:25 +02003302
aPiecek3f247652021-04-19 13:40:25 +02003303 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04003304 ret.name.type = troc_resolve_node_type(tc, cn->nodetype, cn->flags);
aPiecek3f247652021-04-19 13:40:25 +02003305
aPiecek34fa3772021-05-21 12:35:46 +02003306 /* <prefix> */
3307 ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
aPiecek3f247652021-04-19 13:40:25 +02003308
3309 /* set node's name */
3310 ret.name.str = cn->name;
3311
aPiecekbbc02932021-05-21 07:19:41 +02003312 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003313 /* <type> */
3314 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003315
aPiecek082c7dc2021-05-20 08:55:07 +02003316 /* <iffeature> */
3317 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3318 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003319 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003320 assert(tc->cn->nodetype & LYS_CASE);
3321
3322 /* <type> */
3323 ret.type = TRP_EMPTY_TRT_TYPE;
3324
aPiecek7a28e2f2021-05-21 07:27:03 +02003325 /* <iffeature> */
3326 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003327 }
aPiecek3f247652021-04-19 13:40:25 +02003328
3329 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3330
3331 return ret;
3332}
3333
3334/**********************************************************************
3335 * Modify troc getters
3336 *********************************************************************/
3337
3338/**
aPiecek01598c02021-04-23 14:18:24 +02003339 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003340 */
3341static ly_bool
3342troc_modi_parent(struct trt_tree_ctx *tc)
3343{
3344 assert(tc && tc->cn);
3345 /* If no parent exists, stay in actual node. */
3346 if (tc->cn->parent) {
3347 tc->cn = tc->cn->parent;
3348 return 1;
3349 } else {
3350 return 0;
3351 }
3352}
3353
3354/**
aPiecek01598c02021-04-23 14:18:24 +02003355 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003356 */
3357static struct trt_node
3358troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3359{
3360 const struct lysc_node *cn;
3361
3362 assert(tc && tc->cn);
3363
3364 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3365
3366 /* if next sibling exists */
3367 if (cn) {
3368 /* update trt_tree_ctx */
3369 tc->cn = cn;
3370 return troc_read_node(ca, tc);
3371 } else {
3372 return TRP_EMPTY_NODE;
3373 }
3374}
3375
3376/**
3377 * @copydoc trop_modi_next_child()
3378 */
3379static struct trt_node
3380troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3381{
3382 const struct lysc_node *tmp;
3383
3384 assert(tc && tc->cn);
3385
3386 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3387 tc->cn = tmp;
3388 return troc_read_node(ca, tc);
3389 } else {
3390 return TRP_EMPTY_NODE;
3391 }
3392}
3393
3394/**
aPiecek01598c02021-04-23 14:18:24 +02003395 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003396 */
3397static void
aPiecek3f247652021-04-19 13:40:25 +02003398troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003399{
aPiecek3f247652021-04-19 13:40:25 +02003400 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003401
aPiecek3f247652021-04-19 13:40:25 +02003402 if (troc_modi_parent(tc)) {
3403 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3404 } else {
3405 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003406 switch (tc->section) {
3407 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003408 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003409 break;
3410 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003411 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003412 break;
3413 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003414 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003415 break;
3416 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003417 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003418 break;
3419 default:
3420 assert(0);
3421 }
aPiecek61d062b2020-11-02 11:05:09 +01003422 }
3423}
3424
aPiecek874ea4d2021-04-19 12:26:36 +02003425/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003426 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003427 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003428
3429/**
3430 * @brief Get size of node name.
3431 * @param[in] name contains name and mark.
3432 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003433 * @return negative value as an indication that option mark
3434 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003435 */
3436static int32_t
3437trb_strlen_of_name_and_mark(struct trt_node_name name)
3438{
3439 size_t name_len = strlen(name.str);
3440
3441 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3442 /* counting also parentheses */
3443 name_len += 2;
3444 }
3445
3446 return trp_mark_is_used(name) ?
3447 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3448 (int32_t)name_len;
3449}
3450
3451/**
aPiecek874ea4d2021-04-19 12:26:36 +02003452 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3453 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003454 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003455 * @param[in] max_len4all is the maximum value of btw_opts_type
3456 * that it can have.
3457 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003458 */
3459static int16_t
3460trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3461{
3462 int32_t name_len;
3463 int16_t min_len;
3464 int16_t ret;
3465
3466 name_len = trb_strlen_of_name_and_mark(name);
3467
3468 /* negative value indicate that in name is some opt mark */
3469 min_len = name_len < 0 ?
3470 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3471 TRD_INDENT_BEFORE_TYPE;
3472 ret = abs(max_len4all) - abs(name_len);
3473
3474 /* correction -> negative indicate that name is too long. */
3475 return ret < 0 ? min_len : ret;
3476}
3477
3478/**
3479 * @brief Print node.
3480 *
aPiecek01598c02021-04-23 14:18:24 +02003481 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003482 * But difference is that take @p max_gap_before_type which will be
3483 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003484 *
aPiecek9bdd7592021-05-20 08:13:20 +02003485 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003486 * @param[in] max_gap_before_type is number of indent before \<type\>.
3487 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003488 * @param[in] pc contains mainly functions for printing.
3489 * @param[in] tc is tree context.
3490 */
3491static void
aPiecek9bdd7592021-05-20 08:13:20 +02003492trb_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 +01003493{
aPiecek61d062b2020-11-02 11:05:09 +01003494 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3495
3496 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3497 /* print actual node with unified indent */
3498 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3499 }
3500 /* after -> print actual node with default indent */
3501 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3502 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
aPiecek7ed8d032022-10-10 12:32:27 +02003503 if ((node.flags == TRD_FLAGS_TYPE_MOUNT_POINT) && node.mount) {
ekinzie0ab8b302022-10-10 03:03:57 -04003504 struct trt_wrapper wr_mount;
3505 struct tro_getters get;
3506
3507 wr_mount = pc->fp.read.if_sibling_exists(tc) ?
3508 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3509
3510 get = tc->lysc_tree ? troc_init_getters() : trop_init_getters();
3511 if (get.child(tc->lysc_tree ? (void *)tc->cn : (void *)tc->pn)) {
3512 /* If this node has a child, we need to draw a vertical line
3513 * from the last mounted module to the first child
3514 */
3515 wr_mount = trp_wrapper_set_mark_top(wr_mount);
3516 }
3517
aPiecek7ed8d032022-10-10 12:32:27 +02003518 trb_print_mount_point(node.mount, wr_mount, pc);
ekinzie0ab8b302022-10-10 03:03:57 -04003519 }
aPiecek61d062b2020-11-02 11:05:09 +01003520}
3521
3522/**
aPiecek874ea4d2021-04-19 12:26:36 +02003523 * @brief Check if parent of the current node is the last
3524 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003525 *
aPiecek874ea4d2021-04-19 12:26:36 +02003526 * To mantain stability use this function only if the current node is
3527 * the first of the siblings.
3528 * Side-effect -> current node is set to the first sibling
3529 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003530 *
aPiecek01598c02021-04-23 14:18:24 +02003531 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003532 * @param[in,out] tc is tree context.
3533 * @return 1 if parent is last sibling otherwise 0.
3534 */
3535static ly_bool
3536trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3537{
3538 if (fp.modify.parent(tc)) {
3539 ly_bool ret = fp.read.if_sibling_exists(tc);
Michal Vasko26bbb272022-08-02 14:54:33 +02003540
aPiecek61d062b2020-11-02 11:05:09 +01003541 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3542 return !ret;
3543 } else {
3544 return !fp.read.if_sibling_exists(tc);
3545 }
3546}
3547
3548/**
3549 * @brief Find sibling with the biggest node name and return that size.
3550 *
3551 * Side-effect -> Current node is set to the first sibling.
3552 *
3553 * @param[in] ca contains inherited data from ancestors.
3554 * @param[in] pc contains mainly functions for printing.
3555 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003556 * @return positive number as a sign that only the node name is
3557 * included in the size.
3558 * @return negative number sign that node name and his opt mark is
3559 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003560 */
3561static int32_t
3562trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3563{
3564 int32_t ret = 0;
3565
3566 pc->fp.modify.first_sibling(tc);
3567
3568 for (struct trt_node node = pc->fp.read.node(ca, tc);
3569 !trp_node_is_empty(node);
3570 node = pc->fp.modify.next_sibling(ca, tc)) {
3571 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
Michal Vasko26bbb272022-08-02 14:54:33 +02003572
aPiecek61d062b2020-11-02 11:05:09 +01003573 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3574 }
3575 pc->fp.modify.first_sibling(tc);
3576 return ret;
3577}
3578
3579/**
aPiecek874ea4d2021-04-19 12:26:36 +02003580 * @brief Find maximal indent between
3581 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003582 *
3583 * Side-effect -> Current node is set to the first sibling.
3584 *
3585 * @param[in] ca contains inherited data from ancestors.
3586 * @param[in] pc contains mainly functions for printing.
3587 * @param[in,out] tc is tree context.
3588 * @return max btw_opts_type value for rest of the siblings
3589 */
3590static int16_t
3591trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3592{
3593 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3594 int16_t ind_before_type = maxlen_node_name < 0 ?
3595 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3596 TRD_INDENT_BEFORE_TYPE;
3597
3598 return abs(maxlen_node_name) + ind_before_type;
3599}
3600
3601/**
aPiecek874ea4d2021-04-19 12:26:36 +02003602 * @brief Find out if it is possible to unify
3603 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003604 *
aPiecek874ea4d2021-04-19 12:26:36 +02003605 * The goal is for all node siblings to have the same alignment
3606 * for \<type\> as if they were in a column. All siblings who cannot
3607 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003608 * Side-effect -> Current node is set to the first sibling.
3609 *
3610 * @param[in] ca contains inherited data from ancestors.
3611 * @param[in] pc contains mainly functions for printing.
3612 * @param[in,out] tc is tree context.
3613 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003614 * @return positive number indicating the maximum number of spaces
3615 * before \<type\> if the length of the node name is 0. To calculate
3616 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003617 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003618*/
3619static uint32_t
3620trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3621{
3622 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3623}
3624
3625/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003626 * @brief Check if there is no case statement
3627 * under the choice statement.
3628 *
3629 * It can return true only if the Parsed schema tree
3630 * is used for browsing.
3631 *
3632 * @param[in] tc is tree context.
3633 * @return 1 if implicit case statement is present otherwise 0.
3634 */
3635static ly_bool
3636trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3637{
3638 return !tc->lysc_tree && tc->pn->parent &&
3639 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3640 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3641 LYS_LEAF | LYS_LEAFLIST));
3642}
3643
3644static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3645 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3646
3647/**
3648 * @brief Print implicit case node and his subtree.
3649 *
3650 * @param[in] node is child of implicit case.
3651 * @param[in] wr is wrapper for printing identation before node.
3652 * @param[in] ca contains inherited data from ancestors.
3653 * @param[in] pc contains mainly functions for printing.
3654 * @param[in] tc is tree context. Its settings should be the same as
3655 * before the function call.
3656 */
3657static void
3658trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3659 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3660{
3661 struct trt_node case_node;
3662 struct trt_wrapper wr_case_child;
3663
3664 case_node = tro_create_implicit_case_node(node);
3665 ly_print_(pc->out, "\n");
3666 trb_print_entire_node(case_node, 0, wr, pc, tc);
3667 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3668 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3669 ly_print_(pc->out, "\n");
3670 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3671}
3672
3673/**
aPiecek874ea4d2021-04-19 12:26:36 +02003674 * @brief For the current node: recursively print all of its child
3675 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003676 *
aPiecek01598c02021-04-23 14:18:24 +02003677 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003678 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003679 * Nodes are printed, including unified sibling node alignment
3680 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003681 * Side-effect -> current node is set to the last sibling.
3682 *
3683 * @param[in] wr is wrapper for printing identation before node.
3684 * @param[in] ca contains inherited data from ancestors.
3685 * @param[in] pc contains mainly functions for printing.
3686 * @param[in,out] tc is tree context.
3687 */
3688static void
3689trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3690{
3691 uint32_t max_gap_before_type;
3692 ly_bool sibling_flag = 0;
3693 ly_bool child_flag = 0;
3694
3695 /* if node is last sibling, then do not add '|' to wrapper */
3696 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3697 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3698
3699 /* try unified indentation in node */
3700 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3701
3702 /* print all siblings */
3703 do {
3704 struct trt_parent_cache new_ca;
3705 struct trt_node node;
Michal Vasko26bbb272022-08-02 14:54:33 +02003706
aPiecek9bdd7592021-05-20 08:13:20 +02003707 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003708
aPiecekb8d5a0a2021-05-20 08:20:24 +02003709 if (!trb_need_implicit_node_case(tc)) {
3710 /* normal behavior */
3711 ly_print_(pc->out, "\n");
3712 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3713 new_ca = tro_parent_cache_for_child(ca, tc);
3714 /* go to the actual node's child or stay in actual node */
3715 node = pc->fp.modify.next_child(ca, tc);
3716 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003717
aPiecekb8d5a0a2021-05-20 08:20:24 +02003718 if (child_flag) {
3719 /* print all childs - recursive call */
3720 trb_print_nodes(wr, new_ca, pc, tc);
3721 /* get back from child node to actual node */
3722 pc->fp.modify.parent(tc);
3723 }
3724 } else {
3725 /* The case statement is omitted (shorthand).
3726 * Print implicit case node and his subtree.
3727 */
3728 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003729 }
3730
3731 /* go to the actual node's sibling */
3732 node = pc->fp.modify.next_sibling(ca, tc);
3733 sibling_flag = !trp_node_is_empty(node);
3734
3735 /* go to the next sibling or stay in actual node */
3736 } while (sibling_flag);
3737}
3738
3739/**
aPiecek153b00f2021-04-20 13:52:57 +02003740 * @brief Calculate the wrapper about how deep in the tree the node is.
ekinzie0ab8b302022-10-10 03:03:57 -04003741 * @param[in] wr_in A wrapper to use as a starting point
aPiecek153b00f2021-04-20 13:52:57 +02003742 * @param[in] node from which to count.
3743 * @return wrapper for @p node.
3744 */
3745static struct trt_wrapper
ekinzie0ab8b302022-10-10 03:03:57 -04003746trb_count_depth(const struct trt_wrapper *wr_in, const struct lysc_node *node)
aPiecek153b00f2021-04-20 13:52:57 +02003747{
ekinzie0ab8b302022-10-10 03:03:57 -04003748 struct trt_wrapper wr = wr_in ? *wr_in : TRP_INIT_WRAPPER_TOP;
aPiecek153b00f2021-04-20 13:52:57 +02003749 const struct lysc_node *parent;
3750
3751 if (!node) {
3752 return wr;
3753 }
3754
3755 for (parent = node->parent; parent; parent = parent->parent) {
3756 wr = trp_wrapper_set_shift(wr);
3757 }
3758
3759 return wr;
3760}
3761
3762/**
3763 * @brief Print all parent nodes of @p node and the @p node itself.
3764 *
3765 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3766 *
3767 * @param[in] node on which the function is focused.
aPiecek7ed8d032022-10-10 12:32:27 +02003768 * @param[in] wr_in for printing identation before node.
aPiecek01598c02021-04-23 14:18:24 +02003769 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003770 * @param[in,out] tc is context of tree printer.
3771 * @return wrapper for @p node.
3772 */
3773static void
ekinzie0ab8b302022-10-10 03:03:57 -04003774trb_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 +02003775{
3776 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003777 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003778
3779 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3780
3781 /* stop recursion */
3782 if (!node) {
3783 return;
3784 }
ekinzie0ab8b302022-10-10 03:03:57 -04003785 trb_print_parents(node->parent, wr_in, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003786
3787 /* setup for printing */
3788 tc->cn = node;
ekinzie0ab8b302022-10-10 03:03:57 -04003789 wr = trb_count_depth(wr_in, node);
aPiecek153b00f2021-04-20 13:52:57 +02003790
3791 /* print node */
3792 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003793 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3794 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003795}
3796
3797/**
aPiecekdc8fd572021-04-19 10:47:23 +02003798 * @brief Get address of the current node.
3799 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003800 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003801 */
3802static const void *
3803trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3804{
aPiecek3f247652021-04-19 13:40:25 +02003805 return tc->lysc_tree ?
3806 (const void *)tc->cn :
3807 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003808}
3809
3810/**
3811 * @brief Get address of current node's child.
3812 * @param[in,out] tc contains current node.
3813 */
3814static const void *
3815trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3816{
3817 if (!trb_tree_ctx_get_node(tc)) {
3818 return NULL;
3819 }
3820
aPiecek3f247652021-04-19 13:40:25 +02003821 if (tc->lysc_tree) {
3822 return lysc_node_child(tc->cn);
3823 } else {
3824 return lysp_node_child(tc->pn);
3825 }
aPiecekdc8fd572021-04-19 10:47:23 +02003826}
3827
3828/**
3829 * @brief Set current node on its child.
3830 * @param[in,out] tc contains current node.
3831 */
3832static void
3833trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3834{
aPiecek3f247652021-04-19 13:40:25 +02003835 const void *node = trb_tree_ctx_get_child(tc);
3836
3837 if (tc->lysc_tree) {
3838 tc->cn = node;
3839 } else {
3840 tc->pn = node;
3841 }
aPiecekdc8fd572021-04-19 10:47:23 +02003842}
3843
3844/**
aPiecek61d062b2020-11-02 11:05:09 +01003845 * @brief Print subtree of nodes.
3846 *
3847 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003848 * Before root node is no linebreak printing. This must be addressed by
3849 * the caller. Root node will also be printed. Behind last printed node
3850 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003851 *
aPiecek9bdd7592021-05-20 08:13:20 +02003852 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003853 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003854 * ::trb_try_unified_indent() function for root node.
3855 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003856 * @param[in] wr is wrapper saying how deep in the whole tree
3857 * is the root of the subtree.
3858 * @param[in] ca is parent_cache from root's parent.
3859 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003860 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003861 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003862 */
3863static void
aPiecek9bdd7592021-05-20 08:13:20 +02003864trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3865 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003866{
3867 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003868
aPiecek9bdd7592021-05-20 08:13:20 +02003869 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
ekinzie0ab8b302022-10-10 03:03:57 -04003870
aPiecek61d062b2020-11-02 11:05:09 +01003871 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003872 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003873 node = pc->fp.modify.next_child(ca, tc);
3874
3875 if (!trp_node_is_empty(node)) {
3876 /* print root's nodes */
3877 trb_print_nodes(wr, new_ca, pc, tc);
3878 /* get back from child node to actual node */
3879 pc->fp.modify.parent(tc);
3880 }
3881}
3882
3883/**
3884 * @brief Get number of siblings.
3885 *
3886 * Side-effect -> current node is set to the first sibling.
3887 *
3888 * @param[in] fp contains callback functions which modify tree context
3889 * @param[in,out] tc is the tree context.
3890 * @return Number of siblings of the current node.
3891 */
3892static uint32_t
3893trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3894{
3895 uint32_t ret = 1;
3896 struct trt_node node = TRP_EMPTY_NODE;
3897
3898 /* including actual node */
3899 fp.first_sibling(tc);
3900 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3901 ret++;
3902 }
3903 fp.first_sibling(tc);
3904 return ret;
3905}
3906
3907/**
3908 * @brief Print all parents and their children.
3909 *
aPiecek874ea4d2021-04-19 12:26:36 +02003910 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003911 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003912 * for all top-level siblings. Use this function after 'module' keyword
3913 * or 'augment' and so. The nodes may not be exactly top-level in the
3914 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003915 *
aPiecek153b00f2021-04-20 13:52:57 +02003916 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3917 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003918 * @param[pc] pc contains mainly functions for printing.
3919 * @param[in,out] tc is tree context.
3920 */
3921static void
aPiecek153b00f2021-04-20 13:52:57 +02003922trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003923{
aPiecek61d062b2020-11-02 11:05:09 +01003924 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003925 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003926 uint32_t total_parents;
3927 uint32_t max_gap_before_type;
3928
aPiecekdc8fd572021-04-19 10:47:23 +02003929 if (!trb_tree_ctx_get_node(tc)) {
3930 return;
3931 }
3932
aPiecek61d062b2020-11-02 11:05:09 +01003933 ca = TRP_EMPTY_PARENT_CACHE;
3934 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3935 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3936
aPiecek3f247652021-04-19 13:40:25 +02003937 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003938 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3939 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003940 ca.lys_config = 0x0;
3941 }
aPiecekdc8fd572021-04-19 10:47:23 +02003942 }
3943
aPiecek61d062b2020-11-02 11:05:09 +01003944 for (uint32_t i = 0; i < total_parents; i++) {
3945 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003946 node = pc->fp.read.node(ca, tc);
3947 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003948 pc->fp.modify.next_sibling(ca, tc);
3949 }
3950}
3951
aPiecek7ed8d032022-10-10 12:32:27 +02003952/**
3953 * @brief Mounted module iterator.
3954 *
3955 * Omit internal modules, modules with no nodes (e.g., iana-if-types)
3956 * and modules that were loaded as the result of a parent-reference.
3957 *
3958 * @param[in] ext_ctx is special context of mount-point extension.
3959 * @param[in] parent_refs is set of parent-references. Can be NULL for case of 'inline' schema-ref.
3960 * @param[in,out] state of the iterator. Set the value to zero for initialization.
3961 * @return First/next mounted module or NULL.
3962 */
3963static const struct lys_module *
3964trb_mounted_module_iter(struct ly_ctx *ext_ctx, struct ly_set *parent_refs, uint32_t *state)
3965{
3966 const struct lys_module *mod = NULL;
3967 ly_bool from_parent_ref;
3968 uint32_t j;
3969
3970 if (!(*state)) {
3971 /* Get first non-internal module. */
3972 *state = ly_ctx_internal_modules_count(ext_ctx);
3973 }
3974
3975 while ((mod = ly_ctx_get_module_iter(ext_ctx, state))) {
3976 if (mod->compiled && !mod->compiled->data) {
3977 /* Compiled module with no data-nodes. */
3978 continue;
3979 } else if (mod->parsed && !mod->parsed->data) {
3980 /* Parsed module with no data-nodes. */
3981 continue;
3982 } else if (!parent_refs) {
3983 /* Mounting in 'inline' mode. Success. */
3984 break;
3985 }
3986
3987 /* Check if the module is not in parent-reference. */
3988 from_parent_ref = 0;
3989 for (j = 0; j < parent_refs->count; j++) {
3990 if (!strcmp(mod->ns, parent_refs->snodes[j]->module->ns)) {
3991 from_parent_ref = 1;
3992 break;
3993 }
3994 }
3995 if (!from_parent_ref) {
3996 /* Success. */
3997 break;
3998 }
3999 }
4000
4001 return mod;
4002}
4003
aPiecek874ea4d2021-04-19 12:26:36 +02004004/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004005 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02004006 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004007
4008/**
aPiecekdc8fd572021-04-19 10:47:23 +02004009 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01004010 *
aPiecekdc8fd572021-04-19 10:47:23 +02004011 * @param[in] module YANG schema tree structure representing
4012 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01004013 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02004014 * @param[in] max_line_length is the maximum line length limit
4015 * that should not be exceeded.
aPiecek7ed8d032022-10-10 12:32:27 +02004016 * @param[in] mounted context is used for printing the YANG Schema mount.
4017 * @param[in] parent_refs context is used for printing the YANG Schema mount and its parent-reference is set.
aPiecekdc8fd572021-04-19 10:47:23 +02004018 * @param[in,out] pc will be adapted to lysp_tree.
4019 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01004020 */
4021static void
aPiecek7ed8d032022-10-10 12:32:27 +02004022trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, ly_bool mounted,
4023 const struct ly_set *parent_refs, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01004024{
aPiecekdc8fd572021-04-19 10:47:23 +02004025 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02004026 .lysc_tree = 0,
aPiecek7ed8d032022-10-10 12:32:27 +02004027 .mounted = mounted || parent_refs,
aPiecekdc8fd572021-04-19 10:47:23 +02004028 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004029 .pmod = module->parsed,
4030 .cmod = NULL,
4031 .pn = module->parsed ? module->parsed->data : NULL,
4032 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek7ed8d032022-10-10 12:32:27 +02004033 .cn = NULL,
4034 .parent_refs = parent_refs
aPiecekdc8fd572021-04-19 10:47:23 +02004035 };
aPiecek61d062b2020-11-02 11:05:09 +01004036
aPiecekdc8fd572021-04-19 10:47:23 +02004037 pc->out = out;
4038
4039 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02004040 .parent = trop_modi_parent,
4041 .first_sibling = trop_modi_first_sibling,
4042 .next_sibling = trop_modi_next_sibling,
4043 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01004044 };
4045
aPiecekdc8fd572021-04-19 10:47:23 +02004046 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01004047 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02004048 .node = trop_read_node,
4049 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01004050 };
4051
aPiecekdc8fd572021-04-19 10:47:23 +02004052 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02004053 .print_features_names = tro_print_features_names,
4054 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01004055 };
4056
aPiecekdc8fd572021-04-19 10:47:23 +02004057 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01004058}
4059
4060/**
aPiecek3f247652021-04-19 13:40:25 +02004061 * @brief Settings if lysc_node are used for browsing through the tree.
4062 *
4063 * Pointers to current nodes will be set to module data.
4064 *
4065 * @param[in] module YANG schema tree structure representing
4066 * YANG module.
4067 * @param[in] out is output handler.
4068 * @param[in] max_line_length is the maximum line length limit
4069 * that should not be exceeded.
aPiecek7ed8d032022-10-10 12:32:27 +02004070 * @param[in] mounted context is used for printing the YANG Schema mount.
4071 * @param[in] parent_refs context is used for printing the YANG Schema mount and its parent-reference is set.
aPiecek3f247652021-04-19 13:40:25 +02004072 * @param[in,out] pc will be adapted to lysc_tree.
4073 * @param[in,out] tc will be adapted to lysc_tree.
4074 */
4075static void
aPiecek7ed8d032022-10-10 12:32:27 +02004076trm_lysc_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, ly_bool mounted,
4077 const struct ly_set *parent_refs, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek3f247652021-04-19 13:40:25 +02004078{
4079 *tc = (struct trt_tree_ctx) {
4080 .lysc_tree = 1,
aPiecek7ed8d032022-10-10 12:32:27 +02004081 .mounted = mounted || parent_refs,
aPiecek3f247652021-04-19 13:40:25 +02004082 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004083 .pmod = module->parsed,
4084 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02004085 .tpn = NULL,
4086 .pn = NULL,
aPiecek7ed8d032022-10-10 12:32:27 +02004087 .cn = module->compiled->data,
4088 .parent_refs = parent_refs
aPiecek3f247652021-04-19 13:40:25 +02004089 };
4090
4091 pc->out = out;
4092
4093 pc->fp.modify = (struct trt_fp_modify_ctx) {
4094 .parent = troc_modi_parent,
4095 .first_sibling = troc_modi_first_sibling,
4096 .next_sibling = troc_modi_next_sibling,
4097 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02004098 };
4099
4100 pc->fp.read = (struct trt_fp_read) {
4101 .module_name = tro_read_module_name,
4102 .node = troc_read_node,
4103 .if_sibling_exists = troc_read_if_sibling_exists
4104 };
4105
4106 pc->fp.print = (struct trt_fp_print) {
4107 .print_features_names = tro_print_features_names,
4108 .print_keys = tro_print_keys
4109 };
4110
4111 pc->max_line_length = max_line_length;
4112}
4113
4114/**
4115 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02004116 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02004117 * @param[in,out] tc resets to lysc browsing.
4118 */
4119static void
4120trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4121{
aPiecek7ed8d032022-10-10 12:32:27 +02004122 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, tc->mounted, tc->parent_refs, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02004123}
4124
4125/**
4126 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02004127 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02004128 * @param[in,out] tc resets to lysp browsing.
4129 */
4130static void
4131trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4132{
aPiecek7ed8d032022-10-10 12:32:27 +02004133 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, tc->mounted, tc->parent_refs, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02004134}
4135
4136/**
4137 * @brief If augment's target node is located on the current module.
4138 * @param[in] pn is examined augment.
4139 * @param[in] pmod is current module.
4140 * @return 1 if nodeid refers to the local node, otherwise 0.
4141 */
4142static ly_bool
4143trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
4144{
4145 const char *id, *prefix, *name;
4146 size_t prefix_len, name_len;
4147 const struct lys_module *mod;
4148 ly_bool ret = 0;
4149
4150 if (pn == NULL) {
4151 return ret;
4152 }
4153
4154 id = pn->nodeid;
4155 if (!id) {
4156 return ret;
4157 }
4158 /* only absolute-schema-nodeid is taken into account */
4159 assert(id[0] == '/');
4160 ++id;
4161
4162 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
4163 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02004164 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
Michal Vasko3003c9a2022-03-29 12:10:00 +02004165 ret = mod ? (mod->parsed == pmod) : 0;
aPiecek3f247652021-04-19 13:40:25 +02004166 } else {
4167 ret = 1;
4168 }
4169
4170 return ret;
4171}
4172
4173/**
aPiecek96baa7f2021-04-23 12:32:00 +02004174 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01004175 *
aPiecekdc8fd572021-04-19 10:47:23 +02004176 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02004177 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01004178 *
aPiecekdc8fd572021-04-19 10:47:23 +02004179 * @param[in] ks is section representation.
4180 * @param[in] pc contains mainly functions for printing.
4181 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01004182 */
4183static void
aPiecekdc8fd572021-04-19 10:47:23 +02004184trm_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 +01004185{
aPiecekdc8fd572021-04-19 10:47:23 +02004186 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4187 return;
4188 }
4189
4190 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
4191 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02004192 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004193 } else {
aPiecek153b00f2021-04-20 13:52:57 +02004194 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004195 }
4196}
4197
4198/**
aPiecek96baa7f2021-04-23 12:32:00 +02004199 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02004200 *
aPiecek96baa7f2021-04-23 12:32:00 +02004201 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02004202 *
4203 * @param[in] ks is section representation.
4204 * @param[in] pc contains mainly functions for printing.
4205 * @param[in,out] tc is the tree context.
4206 */
4207static void
4208trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4209{
4210 ly_bool grp_has_data = 0;
4211
4212 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4213 return;
4214 }
4215
4216 if (ks.type == TRD_KEYWORD_GROUPING) {
4217 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4218 }
4219
4220 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4221 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004222 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004223}
4224
4225/**
4226 * @brief Print 'module' keyword, its name and all nodes.
4227 * @param[in] pc contains mainly functions for printing.
4228 * @param[in,out] tc is the tree context.
4229 */
4230static void
4231trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4232{
4233 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4234}
4235
4236/**
4237 * @brief For all augment sections: print 'augment' keyword,
4238 * its target node and all nodes.
4239 * @param[in] pc contains mainly functions for printing.
4240 * @param[in,out] tc is the tree context.
4241 */
4242static void
4243trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4244{
4245 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004246 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004247
aPiecek3f247652021-04-19 13:40:25 +02004248 if (tc->lysc_tree) {
4249 origin_was_lysc_tree = 1;
4250 trm_reset_to_lysp_tree_ctx(pc, tc);
4251 }
4252
aPiecekdc8fd572021-04-19 10:47:23 +02004253 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004254 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004255 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004256 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004257
aPiecek3f247652021-04-19 13:40:25 +02004258 if (origin_was_lysc_tree) {
4259 /* if lysc tree is used, then only augments targeting
4260 * another module are printed
4261 */
aPiecek9f792e52021-04-21 08:33:56 +02004262 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004263 continue;
4264 }
4265 }
4266
aPiecekdc8fd572021-04-19 10:47:23 +02004267 if (once) {
4268 ly_print_(pc->out, "\n");
4269 ly_print_(pc->out, "\n");
4270 once = 0;
4271 } else {
4272 ly_print_(pc->out, "\n");
4273 }
4274
4275 trm_print_section_as_subtree(ks, pc, tc);
4276 }
aPiecek3f247652021-04-19 13:40:25 +02004277
4278 if (origin_was_lysc_tree) {
4279 trm_reset_to_lysc_tree_ctx(pc, tc);
4280 }
aPiecekdc8fd572021-04-19 10:47:23 +02004281}
4282
4283/**
4284 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4285 * @param[in] pc contains mainly functions for printing.
4286 * @param[in,out] tc is the tree context.
4287 */
4288static void
4289trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4290{
4291 struct trt_keyword_stmt rpc;
4292
aPiecek01598c02021-04-23 14:18:24 +02004293 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004294
4295 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4296 ly_print_(pc->out, "\n");
4297 ly_print_(pc->out, "\n");
4298 trm_print_section_as_family_tree(rpc, pc, tc);
4299 }
4300}
4301
4302/**
4303 * @brief For notifications section: print 'notifications' keyword
4304 * and all its nodes.
4305 * @param[in] pc contains mainly functions for printing.
4306 * @param[in,out] tc is the tree context.
4307 */
4308static void
4309trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4310{
4311 struct trt_keyword_stmt notifs;
4312
aPiecek01598c02021-04-23 14:18:24 +02004313 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004314
4315 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4316 ly_print_(pc->out, "\n");
4317 ly_print_(pc->out, "\n");
4318 trm_print_section_as_family_tree(notifs, pc, tc);
4319 }
4320}
4321
4322/**
4323 * @brief For all grouping sections: print 'grouping' keyword, its name
4324 * and all nodes.
4325 * @param[in] pc contains mainly functions for printing.
4326 * @param[in,out] tc is the tree context.
4327 */
4328static void
4329trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4330{
4331 ly_bool once;
4332
aPiecek01598c02021-04-23 14:18:24 +02004333 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004334 return;
4335 }
4336
4337 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004338 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004339 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004340 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004341 if (once) {
4342 ly_print_(pc->out, "\n");
4343 ly_print_(pc->out, "\n");
4344 once = 0;
4345 } else {
4346 ly_print_(pc->out, "\n");
4347 }
4348 trm_print_section_as_subtree(ks, pc, tc);
4349 }
4350}
4351
4352/**
4353 * @brief For all yang-data sections: print 'yang-data' keyword
4354 * and all its nodes.
4355 * @param[in] pc contains mainly functions for printing.
4356 * @param[in,out] tc is the tree context.
4357 */
4358static void
aPiecek96baa7f2021-04-23 12:32:00 +02004359trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004360{
aPiecek96baa7f2021-04-23 12:32:00 +02004361 ly_bool once;
4362 LY_ARRAY_COUNT_TYPE count;
4363
4364 count = LY_ARRAY_COUNT(tc->pmod->exts);
4365 if (count == 0) {
4366 return;
4367 }
4368
4369 once = 1;
4370 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4371 struct trt_keyword_stmt ks;
4372
aPiecek01598c02021-04-23 14:18:24 +02004373 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004374 * ::lysp_ext_instance.parsed.
4375 */
4376 if (!tc->pmod->exts[u].parsed) {
4377 /* print at least the yang-data names */
4378 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4379 continue;
4380 }
4381
4382 ks = tro_modi_next_yang_data(tc, u);
4383 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4384 break;
4385 }
4386
4387 if (once) {
4388 ly_print_(pc->out, "\n");
4389 ly_print_(pc->out, "\n");
4390 once = 0;
4391 } else {
4392 ly_print_(pc->out, "\n");
4393 }
4394
4395 trm_print_section_as_family_tree(ks, pc, tc);
4396 }
aPiecekdc8fd572021-04-19 10:47:23 +02004397}
4398
4399/**
4400 * @brief Print sections module, augment, rpcs, notifications,
4401 * grouping, yang-data.
4402 * @param[in] pc contains mainly functions for printing.
4403 * @param[in,out] tc is the tree context.
4404 */
4405static void
4406trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4407{
4408 trm_print_module_section(pc, tc);
4409 trm_print_augmentations(pc, tc);
4410 trm_print_rpcs(pc, tc);
4411 trm_print_notifications(pc, tc);
4412 trm_print_groupings(pc, tc);
4413 trm_print_yang_data(pc, tc);
4414 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004415}
4416
aPiecek874ea4d2021-04-19 12:26:36 +02004417/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004418 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004419 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004420
4421LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004422tree_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 +01004423{
4424 struct trt_printer_ctx pc;
4425 struct trt_tree_ctx tc;
4426 struct ly_out *new_out;
4427 LY_ERR erc;
4428 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4429
aPiecekdc8fd572021-04-19 10:47:23 +02004430 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4431
aPiecek61d062b2020-11-02 11:05:09 +01004432 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4433 return erc;
4434 }
4435
4436 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004437 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
aPiecek7ed8d032022-10-10 12:32:27 +02004438 trm_lysc_tree_ctx(module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek3f247652021-04-19 13:40:25 +02004439 } else {
aPiecek7ed8d032022-10-10 12:32:27 +02004440 trm_lysp_tree_ctx(module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek3f247652021-04-19 13:40:25 +02004441 }
aPiecek61d062b2020-11-02 11:05:09 +01004442
4443 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004444 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004445
4446 ly_out_free(new_out, NULL, 1);
4447
aPiecekdc8fd572021-04-19 10:47:23 +02004448 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004449}
4450
4451LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004452tree_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 +01004453{
aPiecek153b00f2021-04-20 13:52:57 +02004454 struct trt_printer_ctx pc;
4455 struct trt_tree_ctx tc;
4456 struct ly_out *new_out;
4457 struct trt_wrapper wr;
4458 LY_ERR erc;
4459 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4460
4461 assert(out && node);
4462
4463 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4464 return LY_EINVAL;
4465 }
4466
4467 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4468 return erc;
4469 }
4470
4471 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek7ed8d032022-10-10 12:32:27 +02004472 trm_lysc_tree_ctx(node->module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004473
4474 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
ekinzie0ab8b302022-10-10 03:03:57 -04004475 trb_print_parents(node, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004476
4477 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4478 tc.cn = lysc_node_child(node);
ekinzie0ab8b302022-10-10 03:03:57 -04004479 wr = trb_count_depth(NULL, tc.cn);
aPiecek153b00f2021-04-20 13:52:57 +02004480 trb_print_family_tree(wr, &pc, &tc);
4481 }
4482 ly_print_(out, "\n");
4483
4484 erc = clb_arg.last_error;
4485 ly_out_free(new_out, NULL, 1);
4486
4487 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004488}
4489
4490LY_ERR
Michal Vasko6a914fb2022-01-17 10:36:14 +01004491tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
4492 size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004493{
aPiecek9f792e52021-04-21 08:33:56 +02004494 struct trt_printer_ctx pc;
4495 struct trt_tree_ctx tc;
4496 struct ly_out *new_out;
4497 LY_ERR erc;
4498 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4499
4500 assert(submodp);
4501 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4502
4503 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4504 return erc;
4505 }
4506
4507 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek7ed8d032022-10-10 12:32:27 +02004508 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek9f792e52021-04-21 08:33:56 +02004509 tc.pmod = (struct lysp_module *)submodp;
4510 tc.tpn = submodp->data;
4511 tc.pn = tc.tpn;
4512
4513 trm_print_sections(&pc, &tc);
4514 erc = clb_arg.last_error;
4515
4516 ly_out_free(new_out, NULL, 1);
4517
4518 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004519}
ekinzie0ab8b302022-10-10 03:03:57 -04004520
aPiecek7ed8d032022-10-10 12:32:27 +02004521/**
4522 * @brief Print all mounted nodes ('/') and parent-referenced nodes ('@').
4523 *
4524 * @param[in] ext is mount-point extension.
4525 * @param[in] wr is wrapper to be printed.
4526 * @param[in] pc contains mainly functions for printing.
4527 * @return LY_ERR value.
4528 */
ekinzie0ab8b302022-10-10 03:03:57 -04004529static LY_ERR
aPiecek7ed8d032022-10-10 12:32:27 +02004530trb_print_mount_point(const struct lysc_ext_instance *ext, const struct trt_wrapper wr, struct trt_printer_ctx *pc)
ekinzie0ab8b302022-10-10 03:03:57 -04004531{
aPiecek7ed8d032022-10-10 12:32:27 +02004532 LY_ERR ret = LY_SUCCESS;
ekinzie0ab8b302022-10-10 03:03:57 -04004533 struct ly_ctx *ext_ctx = NULL;
aPiecek7ed8d032022-10-10 12:32:27 +02004534 const struct lys_module *mod, *last_mod;
ekinzie0ab8b302022-10-10 03:03:57 -04004535 struct trt_tree_ctx tmptc;
4536 struct trt_wrapper tmpwr;
4537 struct trt_printer_ctx tmppc;
ekinzie0ab8b302022-10-10 03:03:57 -04004538 struct ly_set *refs = NULL;
aPiecek7ed8d032022-10-10 12:32:27 +02004539 uint32_t i, iter_state;
ekinzie0ab8b302022-10-10 03:03:57 -04004540
aPiecek7ed8d032022-10-10 12:32:27 +02004541 if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) {
ekinzie0ab8b302022-10-10 03:03:57 -04004542 /* Void mount point */
4543 return LY_SUCCESS;
4544 }
4545
aPiecek7ed8d032022-10-10 12:32:27 +02004546 lyplg_ext_schema_mount_get_parent_ref(ext, &refs);
4547
4548 /* Get the last mounted module which will be printed. */
4549 iter_state = 0;
4550 while ((mod = trb_mounted_module_iter(ext_ctx, refs, &iter_state))) {
4551 last_mod = mod;
ekinzie0ab8b302022-10-10 03:03:57 -04004552 }
4553
ekinzie0ab8b302022-10-10 03:03:57 -04004554 tmppc = *pc;
aPiecek7ed8d032022-10-10 12:32:27 +02004555 iter_state = 0;
4556 while ((mod = trb_mounted_module_iter(ext_ctx, refs, &iter_state))) {
4557 /* Prepare printer tree context. */
ekinzie0ab8b302022-10-10 03:03:57 -04004558 if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) {
aPiecek7ed8d032022-10-10 12:32:27 +02004559 trm_lysc_tree_ctx(mod, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
ekinzie0ab8b302022-10-10 03:03:57 -04004560 } else {
aPiecek7ed8d032022-10-10 12:32:27 +02004561 trm_lysp_tree_ctx(mod, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
ekinzie0ab8b302022-10-10 03:03:57 -04004562 }
aPiecek7ed8d032022-10-10 12:32:27 +02004563 /* Decide whether to print the symbol '|'. */
4564 tmpwr = (mod == last_mod) ? wr : trp_wrapper_set_mark_top(wr);
4565 /* Print top-level nodes of mounted module which are denoted by the symbol '/'. */
4566 trb_print_family_tree(tmpwr, &tmppc, &tmptc);
ekinzie0ab8b302022-10-10 03:03:57 -04004567 }
4568
aPiecek7ed8d032022-10-10 12:32:27 +02004569 /* Print parent-referenced nodes which are denoted by the symbol '@'. */
4570 for (i = 0; refs && i < refs->count; i++) {
4571 trm_lysc_tree_ctx(refs->snodes[i]->module, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
4572 trb_print_parents(refs->snodes[i], &tmpwr, pc, &tmptc);
ekinzie0ab8b302022-10-10 03:03:57 -04004573 }
4574
ekinzie0ab8b302022-10-10 03:03:57 -04004575 ly_set_free(refs, NULL);
ekinzie0ab8b302022-10-10 03:03:57 -04004576 ly_ctx_destroy(ext_ctx);
aPiecek7ed8d032022-10-10 12:32:27 +02004577
4578 return ret;
ekinzie0ab8b302022-10-10 03:03:57 -04004579}