blob: e6422576cdc9610a711d5282790dd348573bedaa [file] [log] [blame]
aPiecek61d062b2020-11-02 11:05:09 +01001/**
2 * @file printer_tree.c
3 * @author Adam Piecek <piecek@cesnet.cz>
4 * @brief RFC tree printer for libyang data structure
5 *
6 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
aPiecek874ea4d2021-04-19 12:26:36 +020013 *
14 * @section TRP_DESIGN Design
15 *
16 * @code
aPiecek61d062b2020-11-02 11:05:09 +010017 * +---------+ +---------+ +---------+
18 * output | trp | | trb | | tro |
19 * <---+ Print +<---+ Browse +<-->+ Obtain |
20 * | | | | | |
21 * +---------+ +----+----+ +---------+
22 * ^
23 * |
24 * +----+----+
25 * | trm |
26 * | Manager |
27 * | |
28 * +----+----+
29 * ^
30 * | input
31 * +
aPiecek874ea4d2021-04-19 12:26:36 +020032 * @endcode
aPiecek61d062b2020-11-02 11:05:09 +010033 *
aPiecek874ea4d2021-04-19 12:26:36 +020034 * @subsection TRP_GLOSSARY Glossary
aPiecek61d062b2020-11-02 11:05:09 +010035 *
aPiecek874ea4d2021-04-19 12:26:36 +020036 * @subsubsection TRP_trm trm
37 * Manager functions are at the peak of abstraction. They are
38 * able to print individual sections of the YANG tree diagram
39 * (eg module, notifications, rpcs ...) and they call
aPiecek01598c02021-04-23 14:18:24 +020040 * Browse functions (@ref TRP_trb).
aPiecek61d062b2020-11-02 11:05:09 +010041 *
aPiecek874ea4d2021-04-19 12:26:36 +020042 * @subsubsection TRP_trb trb
43 * Browse functions contain a general algorithm (Preorder DFS)
44 * for traversing the tree. It does not matter what data type
aPiecek01598c02021-04-23 14:18:24 +020045 * the tree contains (@ref lysc_node or @ref lysp_node), because it
aPiecek874ea4d2021-04-19 12:26:36 +020046 * requires a ready-made getter functions for traversing the tree
aPiecek01598c02021-04-23 14:18:24 +020047 * (@ref trt_fp_all) and transformation function to its own node
48 * data type (@ref trt_node). These getter functions are generally
49 * referred to as @ref TRP_tro. Browse functions can repeatedly
aPiecek874ea4d2021-04-19 12:26:36 +020050 * traverse nodes in the tree, for example, to calculate the alignment
51 * gap before the nodes \<type\> in the YANG Tree Diagram.
aPiecek01598c02021-04-23 14:18:24 +020052 * The obtained @ref trt_node is passed to the @ref TRP_trp functions
aPiecek874ea4d2021-04-19 12:26:36 +020053 * to print the Tree diagram.
54 *
55 * @subsubsection TRP_tro tro
56 * Functions that provide an extra wrapper for the libyang library.
aPiecekef1e58e2021-04-19 13:19:44 +020057 * The Obtain functions are further specialized according to whether
aPiecek01598c02021-04-23 14:18:24 +020058 * they operate on lysp_tree (@ref TRP_trop) or lysc_tree
59 * (@ref TRP_troc). If they are general algorithms, then they have the
aPiecek3f247652021-04-19 13:40:25 +020060 * prefix \b tro_. The Obtain functions provide information to
aPiecek01598c02021-04-23 14:18:24 +020061 * @ref TRP_trb functions for printing the Tree diagram.
aPiecekef1e58e2021-04-19 13:19:44 +020062 *
63 * @subsubsection TRP_trop trop
64 * Functions for Obtaining information from Parsed schema tree.
aPiecek874ea4d2021-04-19 12:26:36 +020065 *
aPiecek3f247652021-04-19 13:40:25 +020066 * @subsubsection TRP_troc troc
67 * Functions for Obtaining information from Compiled schema tree.
68 *
aPiecek874ea4d2021-04-19 12:26:36 +020069 * @subsubsection TRP_trp trp
70 * Print functions take care of the printing YANG diagram. They can
71 * also split one node into multiple lines if the node does not fit
72 * on one line.
73 *
74 * @subsubsection TRP_trt trt
75 * Data type marking in the printer_tree module.
76 *
77 * @subsubsection TRP_trg trg
78 * General functions.
79 *
80 * @subsection TRP_ADJUSTMENTS Adjustments
81 * It is assumed that the changes are likely to take place mainly for
aPiecek01598c02021-04-23 14:18:24 +020082 * @ref TRP_tro, @ref TRP_trop or @ref TRP_troc functions because
aPiecek3f247652021-04-19 13:40:25 +020083 * they are the only ones dependent on libyang implementation.
84 * In special cases, changes will also need to be made to the
aPiecek01598c02021-04-23 14:18:24 +020085 * @ref TRP_trp functions if a special algorithm is needed to print
aPiecek3f247652021-04-19 13:40:25 +020086 * (right now this is prepared for printing list's keys
87 * and if-features).
aPiecek61d062b2020-11-02 11:05:09 +010088 */
89
aPiecek874ea4d2021-04-19 12:26:36 +020090#include <assert.h>
91#include <string.h>
92
93#include "common.h"
94#include "compat.h"
95#include "out_internal.h"
ekinzie0ab8b302022-10-10 03:03:57 -040096#include "plugins_exts.h"
97#include "plugins_types.h"
aPiecek704f8e92021-08-25 13:35:05 +020098#include "printer_schema.h"
aPiecek874ea4d2021-04-19 12:26:36 +020099#include "tree_schema_internal.h"
100#include "xpath.h"
101
aPiecek61d062b2020-11-02 11:05:09 +0100102/**
103 * @brief List of available actions.
104 */
105typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200106 TRD_PRINT = 0, /**< Normal behavior. It just prints. */
107 TRD_CHAR_COUNT /**< Characters will be counted instead of printing. */
aPiecek61d062b2020-11-02 11:05:09 +0100108} trt_ly_out_clb_arg_flag;
109
110/**
aPiecek874ea4d2021-04-19 12:26:36 +0200111 * @brief Structure is passed as 'writeclb' argument
aPiecek01598c02021-04-23 14:18:24 +0200112 * to the ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100113 */
114struct ly_out_clb_arg {
aPiecek874ea4d2021-04-19 12:26:36 +0200115 trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */
116 struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */
117 size_t counter; /**< Counter of printed characters. */
118 LY_ERR last_error; /**< The last error that occurred. If no error has occurred, it will be ::LY_SUCCESS. */
aPiecek61d062b2020-11-02 11:05:09 +0100119};
120
121/**
122 * @brief Initialize struct ly_out_clb_arg with default settings.
123 */
124#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
aPiecek874ea4d2021-04-19 12:26:36 +0200125 (struct ly_out_clb_arg) { \
126 .mode = MODE, .out = OUT, \
127 .counter = COUNTER, .last_error = LAST_ERROR \
128 }
aPiecek61d062b2020-11-02 11:05:09 +0100129
aPiecek874ea4d2021-04-19 12:26:36 +0200130/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100131 * Print getters
aPiecek874ea4d2021-04-19 12:26:36 +0200132 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100133
134/**
135 * @brief Callback functions that prints special cases.
136 *
137 * It just groups together tree context with trt_fp_print.
138 */
139struct trt_cf_print {
aPiecek874ea4d2021-04-19 12:26:36 +0200140 const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200141
aPiecek874ea4d2021-04-19 12:26:36 +0200142 void (*pf)(const struct trt_tree_ctx *, struct ly_out *); /**< Pointing to function which printing list's keys or features. */
aPiecek61d062b2020-11-02 11:05:09 +0100143};
144
145/**
146 * @brief Callback functions for printing special cases.
147 *
aPiecek874ea4d2021-04-19 12:26:36 +0200148 * Functions with the suffix 'trp' can print most of the text on
149 * output, just by setting the pointer to the string. But in some
150 * cases, it's not that simple, because its entire string is fragmented
151 * in memory. For example, for printing list's keys or if-features.
aPiecek61d062b2020-11-02 11:05:09 +0100152 * However, this depends on how the libyang library is implemented.
aPiecek3f247652021-04-19 13:40:25 +0200153 * This implementation of the printer_tree module goes through
154 * a lysp tree, but if it goes through a lysc tree, these special cases
155 * would be different.
aPiecek61d062b2020-11-02 11:05:09 +0100156 * Functions must print including spaces or delimiters between names.
157 */
158struct trt_fp_print {
159 void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */
160 void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */
161};
162
163/**
164 * @brief Package which only groups getter function.
165 */
166struct trt_pck_print {
167 const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */
168 struct trt_fp_print fps; /**< Print function. */
169};
170
171/**
172 * @brief Initialize struct trt_pck_print by parameters.
173 */
174#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
aPiecek874ea4d2021-04-19 12:26:36 +0200175 (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT}
aPiecek61d062b2020-11-02 11:05:09 +0100176
aPiecek874ea4d2021-04-19 12:26:36 +0200177/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100178 * Indent
aPiecek874ea4d2021-04-19 12:26:36 +0200179 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100180
181/**
aPiecek874ea4d2021-04-19 12:26:36 +0200182 * @brief Constants which are defined in the RFC or are observable
183 * from the pyang tool.
aPiecek61d062b2020-11-02 11:05:09 +0100184 */
185typedef enum {
186 TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100187 TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with
188 a whitespace offset of at least two characters. */
189 TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */
190 TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */
191 TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */
192 TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */
aPiecek61d062b2020-11-02 11:05:09 +0100193 TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */
194} trt_cnf_indent;
195
196/**
197 * @brief Type of indent in node.
198 */
199typedef enum {
200 TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100201 TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */
aPiecek61d062b2020-11-02 11:05:09 +0100202 TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
203} trt_indent_in_node_type;
204
205/** Constant to indicate the need to break a line. */
206#define TRD_LINEBREAK -1
207
208/**
aPiecek874ea4d2021-04-19 12:26:36 +0200209 * @brief Records the alignment between the individual
210 * elements of the node.
aPiecek61d062b2020-11-02 11:05:09 +0100211 *
aPiecek874ea4d2021-04-19 12:26:36 +0200212 * @see trp_default_indent_in_node, trp_try_normal_indent_in_node
aPiecek61d062b2020-11-02 11:05:09 +0100213 */
214struct trt_indent_in_node {
215 trt_indent_in_node_type type; /**< Type of indent in node. */
aPiecek874ea4d2021-04-19 12:26:36 +0200216 int16_t btw_name_opts; /**< Indent between node name and \<opts\>. */
217 int16_t btw_opts_type; /**< Indent between \<opts\> and \<type\>. */
aPiecek61d062b2020-11-02 11:05:09 +0100218 int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */
219};
220
221/**
222 * @brief Type of wrappers to be printed.
223 */
224typedef enum {
225 TRD_WRAPPER_TOP = 0, /**< Related to the module. */
226 TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */
227} trd_wrapper_type;
228
229/**
230 * @brief For resolving sibling symbol ('|') placement.
231 *
232 * Bit indicates where the sibling symbol must be printed.
aPiecek874ea4d2021-04-19 12:26:36 +0200233 * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS.
aPiecek61d062b2020-11-02 11:05:09 +0100234 *
aPiecek874ea4d2021-04-19 12:26:36 +0200235 * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY,
236 * trp_wrapper_set_mark, trp_wrapper_set_shift,
237 * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper
aPiecek61d062b2020-11-02 11:05:09 +0100238 */
239struct trt_wrapper {
240 trd_wrapper_type type; /**< Location of the wrapper. */
241 uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed.
242 It follows that the maximum immersion of the printable node is 64. */
243 uint32_t actual_pos; /**< Actual position in bit_marks. */
244};
245
246/**
247 * @brief Get wrapper related to the module section.
248 *
249 * @code
250 * module: <module-name>
251 * +--<node>
252 * |
253 * @endcode
254 */
255#define TRP_INIT_WRAPPER_TOP \
aPiecek874ea4d2021-04-19 12:26:36 +0200256 (struct trt_wrapper) { \
257 .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \
258 }
aPiecek61d062b2020-11-02 11:05:09 +0100259
260/**
aPiecek874ea4d2021-04-19 12:26:36 +0200261 * @brief Get wrapper related to subsection
262 * e.g. Augmenations or Groupings.
aPiecek61d062b2020-11-02 11:05:09 +0100263 *
264 * @code
265 * module: <module-name>
266 * +--<node>
267 *
268 * augment <target-node>:
269 * +--<node>
270 * @endcode
271 */
272#define TRP_INIT_WRAPPER_BODY \
aPiecek874ea4d2021-04-19 12:26:36 +0200273 (struct trt_wrapper) { \
274 .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \
275 }
aPiecek61d062b2020-11-02 11:05:09 +0100276
277/**
278 * @brief Package which only groups wrapper and indent in node.
279 */
280struct trt_pck_indent {
281 struct trt_wrapper wrapper; /**< Coded " | | " sequence. */
282 struct trt_indent_in_node in_node; /**< Indent in node. */
283};
284
285/**
286 * @brief Initialize struct trt_pck_indent by parameters.
287 */
288#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200289 (struct trt_pck_indent){ \
290 .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \
291 }
aPiecek61d062b2020-11-02 11:05:09 +0100292
aPiecek874ea4d2021-04-19 12:26:36 +0200293/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100294 * status
aPiecek874ea4d2021-04-19 12:26:36 +0200295 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100296
297/**
298 * @brief Status of the node.
299 *
aPiecek874ea4d2021-04-19 12:26:36 +0200300 * @see trp_print_status
aPiecek61d062b2020-11-02 11:05:09 +0100301 */
302typedef enum {
303 TRD_STATUS_TYPE_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100304 TRD_STATUS_TYPE_CURRENT, /**< ::LYS_STATUS_CURR */
305 TRD_STATUS_TYPE_DEPRECATED, /**< ::LYS_STATUS_DEPRC */
aPiecek874ea4d2021-04-19 12:26:36 +0200306 TRD_STATUS_TYPE_OBSOLETE /**< ::LYS_STATUS_OBSLT */
aPiecek61d062b2020-11-02 11:05:09 +0100307} trt_status_type;
308
aPiecek874ea4d2021-04-19 12:26:36 +0200309/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100310 * flags
aPiecek874ea4d2021-04-19 12:26:36 +0200311 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100312
313/**
314 * @brief Flag of the node.
315 *
aPiecek874ea4d2021-04-19 12:26:36 +0200316 * @see trp_print_flags, trp_get_flags_strlen
aPiecek61d062b2020-11-02 11:05:09 +0100317 */
318typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200319 TRD_FLAGS_TYPE_EMPTY = 0, /**< -- */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100320 TRD_FLAGS_TYPE_RW, /**< rw */
321 TRD_FLAGS_TYPE_RO, /**< ro */
322 TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */
323 TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */
324 TRD_FLAGS_TYPE_RPC, /**< -x */
325 TRD_FLAGS_TYPE_NOTIF, /**< -n */
aPiecek61d062b2020-11-02 11:05:09 +0100326 TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */
327} trt_flags_type;
328
aPiecek874ea4d2021-04-19 12:26:36 +0200329/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100330 * node_name and opts
aPiecek874ea4d2021-04-19 12:26:36 +0200331 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100332
333#define TRD_NODE_NAME_PREFIX_CHOICE "("
334#define TRD_NODE_NAME_PREFIX_CASE ":("
335#define TRD_NODE_NAME_TRIPLE_DOT "..."
336
337/**
338 * @brief Type of the node.
339 *
aPiecek874ea4d2021-04-19 12:26:36 +0200340 * Used mainly to complete the correct \<opts\> next to or
341 * around the \<name\>.
aPiecek61d062b2020-11-02 11:05:09 +0100342 */
343typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200344 TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100345 TRD_NODE_CASE, /**< For case node. :(\<name\>) */
346 TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */
347 TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */
348 TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */
349 TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */
aPiecekbca57772022-10-13 13:51:59 +0200350 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list. \<name\>* */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100351 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
352 TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */
aPiecek874ea4d2021-04-19 12:26:36 +0200353 TRD_NODE_TRIPLE_DOT /**< For collapsed sibling nodes and their children. Special case which doesn't belong here very well. */
aPiecek61d062b2020-11-02 11:05:09 +0100354} trt_node_type;
355
356/**
357 * @brief Type of node and his name.
358 *
aPiecek874ea4d2021-04-19 12:26:36 +0200359 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
aPiecek61d062b2020-11-02 11:05:09 +0100360 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
361 */
362struct trt_node_name {
363 trt_node_type type; /**< Type of the node relevant for printing. */
aPiecekbca57772022-10-13 13:51:59 +0200364 ly_bool keys; /**< Set to 1 if [\<keys\>] are to be printed. Valid for some types only. */
aPiecek34fa3772021-05-21 12:35:46 +0200365 const char *module_prefix; /**< If the node is augmented into the tree from another module,
366 so this is the prefix of that module. */
aPiecek61d062b2020-11-02 11:05:09 +0100367 const char *str; /**< Name of the node. */
368};
369
370/**
371 * @brief Create struct trt_node_name as empty.
372 */
373#define TRP_EMPTY_NODE_NAME \
aPiecek874ea4d2021-04-19 12:26:36 +0200374 (struct trt_node_name) { \
aPiecekbca57772022-10-13 13:51:59 +0200375 .type = TRD_NODE_ELSE, .keys = 0, .module_prefix = NULL, .str = NULL \
aPiecek874ea4d2021-04-19 12:26:36 +0200376 }
aPiecek61d062b2020-11-02 11:05:09 +0100377
378/**
379 * @brief Check if struct trt_node_name is empty.
380 */
381#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
382 !NODE_NAME.str
383
aPiecek874ea4d2021-04-19 12:26:36 +0200384/**
385 * @brief Every \<opts\> mark except string of list's keys
386 * has a length of one.
387 */
aPiecek61d062b2020-11-02 11:05:09 +0100388#define TRD_OPTS_MARK_LENGTH 1
389
aPiecek874ea4d2021-04-19 12:26:36 +0200390/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100391 * type
aPiecek874ea4d2021-04-19 12:26:36 +0200392 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100393
394/**
395 * @brief Type of the \<type\>
396 */
397typedef enum {
398 TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100399 TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
400 TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm.
aPiecek874ea4d2021-04-19 12:26:36 +0200401 So set type as ::TRD_TYPE_TARGET. */
aPiecek61d062b2020-11-02 11:05:09 +0100402 TRD_TYPE_EMPTY /**< Type is not used at all. */
403} trt_type_type;
404
405/**
406 * @brief \<type\> in the \<node\>.
407 *
aPiecek874ea4d2021-04-19 12:26:36 +0200408 * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
aPiecek61d062b2020-11-02 11:05:09 +0100409 */
410struct trt_type {
411 trt_type_type type; /**< Type of the \<type\>. */
412 const char *str; /**< Path or name of the type. */
413};
414
415/**
416 * @brief Create empty struct trt_type.
417 */
418#define TRP_EMPTY_TRT_TYPE \
419 (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}
420
421/**
422 * @brief Check if struct trt_type is empty.
423 */
424#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
425 TYPE_OF_TYPE.type == TRD_TYPE_EMPTY
426
427/**
428 * @brief Initialize struct trt_type by parameters.
429 */
430#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
431 (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}
432
aPiecek874ea4d2021-04-19 12:26:36 +0200433/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100434 * node
aPiecek874ea4d2021-04-19 12:26:36 +0200435 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100436
437/**
438 * @brief \<node\> data for printing.
439 *
aPiecek874ea4d2021-04-19 12:26:36 +0200440 * It contains RFC's:
441 * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
aPiecek61d062b2020-11-02 11:05:09 +0100442 * Item \<opts\> is moved to part struct trt_node_name.
aPiecek874ea4d2021-04-19 12:26:36 +0200443 * For printing [\<keys\>] and if-features is required special
444 * functions which prints them.
aPiecek61d062b2020-11-02 11:05:09 +0100445 *
aPiecek874ea4d2021-04-19 12:26:36 +0200446 * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty,
447 * trp_print_node_up_to_name, trp_print_divided_node_up_to_name,
448 * trp_print_node
aPiecek61d062b2020-11-02 11:05:09 +0100449 */
450struct trt_node {
aPiecek7ed8d032022-10-10 12:32:27 +0200451 trt_status_type status; /**< \<status\>. */
452 trt_flags_type flags; /**< \<flags\>. */
453 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
454 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
455 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and
456 will be printed by trt_fp_print.print_features_names callback. */
457 ly_bool last_one; /**< Information about whether the node is the last. */
458 struct lysc_ext_instance *mount; /**< Mount-point extension if flags == TRD_FLAGS_TYPE_MOUNT_POINT */
aPiecek61d062b2020-11-02 11:05:09 +0100459};
460
461/**
462 * @brief Create struct trt_node as empty.
463 */
464#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200465 (struct trt_node) { \
466 .status = TRD_STATUS_TYPE_EMPTY, \
467 .flags = TRD_FLAGS_TYPE_EMPTY, \
468 .name = TRP_EMPTY_NODE_NAME, \
469 .type = TRP_EMPTY_TRT_TYPE, \
470 .iffeatures = 0, \
ekinzie0ab8b302022-10-10 03:03:57 -0400471 .last_one = 1, \
472 .mount = NULL \
aPiecek874ea4d2021-04-19 12:26:36 +0200473 }
aPiecek61d062b2020-11-02 11:05:09 +0100474
475/**
476 * @brief Package which only groups indent and node.
477 */
478struct trt_pair_indent_node {
479 struct trt_indent_in_node indent;
480 struct trt_node node;
481};
482
483/**
484 * @brief Initialize struct trt_pair_indent_node by parameters.
485 */
486#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200487 (struct trt_pair_indent_node) { \
488 .indent = INDENT_IN_NODE, .node = NODE \
489 }
aPiecek61d062b2020-11-02 11:05:09 +0100490
aPiecek874ea4d2021-04-19 12:26:36 +0200491/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100492 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200493 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100494
495#define TRD_TOP_KEYWORD_MODULE "module"
496#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
497
498#define TRD_BODY_KEYWORD_AUGMENT "augment"
499#define TRD_BODY_KEYWORD_RPC "rpcs"
500#define TRD_BODY_KEYWORD_NOTIF "notifications"
501#define TRD_BODY_KEYWORD_GROUPING "grouping"
502#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
503
504/**
505 * @brief Type of the trt_keyword.
506 */
507typedef enum {
508 TRD_KEYWORD_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100509 TRD_KEYWORD_MODULE,
510 TRD_KEYWORD_SUBMODULE,
511 TRD_KEYWORD_AUGMENT,
512 TRD_KEYWORD_RPC,
513 TRD_KEYWORD_NOTIF,
514 TRD_KEYWORD_GROUPING,
aPiecek61d062b2020-11-02 11:05:09 +0100515 TRD_KEYWORD_YANG_DATA
516} trt_keyword_type;
517
518/**
519 * @brief Main sign of the tree nodes.
520 *
aPiecek874ea4d2021-04-19 12:26:36 +0200521 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100522 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
523 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
524 * trp_keyword_type_strlen
525 *
526 */
527struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200528 trt_keyword_type type; /**< String containing some of the top or body keyword. */
529 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100530};
531
532/**
533 * @brief Create struct trt_keyword_stmt as empty.
534 */
535#define TRP_EMPTY_KEYWORD_STMT \
536 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
537
538/**
539 * @brief Check if struct trt_keyword_stmt is empty.
540 */
541#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
542 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
543
544/**
545 * @brief Initialize struct trt_keyword_stmt by parameters.
546 */
547#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
548 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
549
aPiecek874ea4d2021-04-19 12:26:36 +0200550/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100551 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200552 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100553
554struct trt_parent_cache;
555
556/**
557 * @brief Functions that change the state of the tree_ctx structure.
558 *
aPiecek3f247652021-04-19 13:40:25 +0200559 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200560 * for the 'trp' printing functions and are also called from the
561 * 'trb' browsing functions when walking through a tree. These callback
562 * functions need to be checked or reformulated if changes to the
563 * libyang library affect the printing tree. For all, if the value
564 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
565 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100566 */
567struct trt_fp_modify_ctx {
568 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
569 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
570 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
571 struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */
aPiecek61d062b2020-11-02 11:05:09 +0100572};
573
aPiecek874ea4d2021-04-19 12:26:36 +0200574/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100575 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200576 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100577
578/**
579 * @brief Functions that do not change the state of the tree_structure.
580 *
581 * For details see trt_fp_modify_ctx.
582 */
583struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200584 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
585 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
586 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100587};
588
aPiecek874ea4d2021-04-19 12:26:36 +0200589/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100590 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200591 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100592
593/**
aPiecek874ea4d2021-04-19 12:26:36 +0200594 * @brief A set of all necessary functions that must be provided
595 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100596 */
597struct trt_fp_all {
598 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
599 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
600 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
601};
602
aPiecek874ea4d2021-04-19 12:26:36 +0200603/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100604 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200605 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100606
607/**
aPiecek01598c02021-04-23 14:18:24 +0200608 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100609 */
610struct trt_printer_ctx {
611 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200612 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100613 size_t max_line_length; /**< The maximum number of characters that can be
614 printed on one line, including the last. */
615};
616
aPiecek874ea4d2021-04-19 12:26:36 +0200617/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100618 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200619 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100620
621/**
622 * @brief The name of the section to which the node belongs.
623 */
624typedef enum {
625 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100626 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
627 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
628 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
629 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
aPiecek61d062b2020-11-02 11:05:09 +0100630 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
631} trt_actual_section;
632
633/**
634 * @brief Types of nodes that have some effect on their children.
635 */
636typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200637 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100638 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
639 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
aPiecek874ea4d2021-04-19 12:26:36 +0200640 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100641} trt_ancestor_type;
642
643/**
644 * @brief Saved information when browsing the tree downwards.
645 *
aPiecek874ea4d2021-04-19 12:26:36 +0200646 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200647 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200648 * this structures during their recursive calls. This functions do not
649 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200650 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200651 * structure, can return a node with the correct data. The word
652 * \b parent is in the structure name, because this data refers to
653 * the last parent and at the same time the states of its
654 * ancestors data. Only the function jumping on the child
655 * (next_child(...)) creates this structure, because the pointer
656 * to the current node moves down the tree. It's like passing
657 * the genetic code to children. Some data must be inherited and
658 * there are two approaches to this problem. Either it will always
659 * be determined which inheritance states belong to the current node
660 * (which can lead to regular travel to the root node) or
661 * the inheritance states will be stored during the recursive calls.
662 * So the problem was solved by the second option. Why does
663 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200664 * the lysp tree. For walks through the lysc tree is trt_parent_cache
665 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100666 *
aPiecek874ea4d2021-04-19 12:26:36 +0200667 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100668 */
669struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200670 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
671 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
672 uint16_t lys_config; /**< Inherited config W or R. */
673 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100674};
675
676/**
677 * @brief Return trt_parent_cache filled with default values.
678 */
679#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200680 (struct trt_parent_cache) { \
681 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
682 .lys_config = LYS_CONFIG_W, .last_list = NULL \
683 }
aPiecek61d062b2020-11-02 11:05:09 +0100684
685/**
686 * @brief Main structure for browsing the libyang tree
687 */
688struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200689 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
690 It is assumed that once set, it does not change.
691 If it is true then trt_tree_ctx.pn and
692 trt_tree_ctx.tpn are not used.
693 If it is false then trt_tree_ctx.cn is not used. */
ekinzie0ab8b302022-10-10 03:03:57 -0400694 ly_bool mounted; /**< This tree is a mounted schema */
aPiecek96baa7f2021-04-23 12:32:00 +0200695 trt_actual_section section; /**< To which section pn points. */
696 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
697 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
698 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200699
aPiecek96baa7f2021-04-23 12:32:00 +0200700 union {
701 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
702 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
703 is set to TRD_SECT_YANG_DATA. */
704 };
705 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
ekinzie0ab8b302022-10-10 03:03:57 -0400706 const struct ly_set *parent_refs; /**< List of schema nodes for top-level nodes found in mount
707 point parent references */
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);
aPiecekbca57772022-10-13 13:51:59 +02001029 const ly_bool c = !node.name.keys;
aPiecek61d062b2020-11-02 11:05:09 +01001030
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;
aPiecek61d062b2020-11-02 11:05:09 +01001156 case TRD_NODE_TOP_LEVEL1:
1157 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1158 break;
1159 case TRD_NODE_TOP_LEVEL2:
1160 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1161 break;
1162 case TRD_NODE_TRIPLE_DOT:
1163 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1164 break;
1165 default:
1166 break;
1167 }
1168}
1169
1170/**
aPiecek874ea4d2021-04-19 12:26:36 +02001171 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1172 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001173 * @param[in] node_name is structure containing the 'mark'.
1174 * @return 1 if contain otherwise 0.
1175 */
1176static ly_bool
1177trp_mark_is_used(struct trt_node_name node_name)
1178{
1179 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1180 return 0;
aPiecekbca57772022-10-13 13:51:59 +02001181 } else if (node_name.keys) {
1182 return 0;
aPiecek61d062b2020-11-02 11:05:09 +01001183 }
1184
1185 switch (node_name.type) {
1186 case TRD_NODE_ELSE:
1187 case TRD_NODE_CASE:
aPiecek61d062b2020-11-02 11:05:09 +01001188 return 0;
1189 default:
1190 return 1;
1191 }
1192}
1193
1194/**
1195 * @brief Print opts keys.
1196 * @param[in] node_name contains type of the node with his name.
1197 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001198 * @param[in] cf is basically a pointer to the function that prints
1199 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001200 * @param[in,out] out is output handler.
1201 */
1202static void
1203trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1204{
aPiecekbca57772022-10-13 13:51:59 +02001205 if (!node_name.keys) {
aPiecek61d062b2020-11-02 11:05:09 +01001206 return;
1207 }
1208
1209 /* <name><mark>___<keys>*/
1210 if (btw_name_opts > 0) {
1211 ly_print_(out, "%*c", btw_name_opts, ' ');
1212 }
1213 ly_print_(out, "[");
1214 cf.pf(cf.ctx, out);
1215 ly_print_(out, "]");
1216}
1217
1218/**
1219 * @brief Print entire struct trt_type structure.
1220 * @param[in] type is item to print.
1221 * @param[in,out] out is output handler.
1222 */
1223static void
1224trp_print_type(struct trt_type type, struct ly_out *out)
1225{
1226 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1227 return;
1228 }
1229
1230 switch (type.type) {
1231 case TRD_TYPE_NAME:
1232 ly_print_(out, "%s", type.str);
1233 break;
1234 case TRD_TYPE_TARGET:
1235 ly_print_(out, "-> %s", type.str);
1236 break;
1237 case TRD_TYPE_LEAFREF:
1238 ly_print_(out, "leafref");
1239 default:
1240 break;
1241 }
1242}
1243
1244/**
1245 * @brief Print all iffeatures of node
1246 *
1247 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001248 * @param[in] cf is basically a pointer to the function that prints
1249 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001250 * @param[in,out] out is output handler.
1251 */
1252static void
1253trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1254{
1255 if (iffeature_flag) {
1256 ly_print_(out, "{");
1257 cf.pf(cf.ctx, out);
1258 ly_print_(out, "}?");
1259 }
1260}
1261
1262/**
1263 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1264 * @param[in] node contains items to print.
1265 * @param[in] out is output handler.
1266 */
1267static void
1268trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1269{
1270 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1271 trp_print_node_name(node.name, out);
1272 return;
1273 }
1274 /* <status>--<flags> */
1275 trp_print_status(node.status, out);
1276 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001277 /* If the node is a case node, there is no space before the <name>
1278 * also case node has no flags.
1279 */
aPiecek61d062b2020-11-02 11:05:09 +01001280 if (node.name.type != TRD_NODE_CASE) {
1281 trp_print_flags(node.flags, out);
1282 ly_print_(out, " ");
1283 }
1284 /* <name> */
1285 trp_print_node_name(node.name, out);
1286}
1287
1288/**
aPiecek874ea4d2021-04-19 12:26:36 +02001289 * @brief Print alignment (spaces) instead of
1290 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001291 * @param[in] node contains items to print.
1292 * @param[in] out is output handler.
1293 */
1294static void
1295trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1296{
1297 uint32_t space = trp_get_flags_strlen(node.flags);
1298
1299 if (node.name.type == TRD_NODE_CASE) {
1300 /* :(<name> */
1301 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1302 } else if (node.name.type == TRD_NODE_CHOICE) {
1303 /* (<name> */
1304 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1305 } else {
1306 /* _<name> */
1307 space += strlen(" ");
1308 }
1309
1310 /* <name>
1311 * __
1312 */
1313 space += TRD_INDENT_LONG_LINE_BREAK;
1314
1315 ly_print_(out, "%*c", space, ' ');
1316}
1317
1318/**
1319 * @brief Print struct trt_node structure.
1320 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001321 * @param[in] pck package of functions for
1322 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001323 * @param[in] indent is the indent in node.
1324 * @param[in,out] out is output handler.
1325 */
1326static void
1327trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1328{
1329 ly_bool triple_dot;
1330 ly_bool divided;
1331 struct trt_cf_print cf_print_keys;
1332 struct trt_cf_print cf_print_iffeatures;
1333
1334 if (trp_node_is_empty(node)) {
1335 return;
1336 }
1337
1338 /* <status>--<flags> <name><opts> <type> <if-features> */
1339 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1340 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1341
1342 if (triple_dot) {
1343 trp_print_node_name(node.name, out);
1344 return;
1345 } else if (!divided) {
1346 trp_print_node_up_to_name(node, out);
1347 } else {
1348 trp_print_divided_node_up_to_name(node, out);
1349 }
1350
1351 /* <opts> */
1352 /* <name>___<opts>*/
1353 cf_print_keys.ctx = pck.tree_ctx;
1354 cf_print_keys.pf = pck.fps.print_keys;
1355
1356 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1357
1358 /* <opts>__<type> */
1359 if (indent.btw_opts_type > 0) {
1360 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1361 }
1362
1363 /* <type> */
1364 trp_print_type(node.type, out);
1365
1366 /* <type>__<iffeatures> */
1367 if (indent.btw_type_iffeatures > 0) {
1368 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1369 }
1370
1371 /* <iffeatures> */
1372 cf_print_iffeatures.ctx = pck.tree_ctx;
1373 cf_print_iffeatures.pf = pck.fps.print_features_names;
1374
1375 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1376}
1377
1378/**
aPiecek874ea4d2021-04-19 12:26:36 +02001379 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001380 * @param[in] ks is keyword statement to print.
1381 * @param[in,out] out is output handler
1382 */
1383static void
1384trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1385{
1386 switch (ks.type) {
1387 case TRD_KEYWORD_MODULE:
1388 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1389 return;
1390 case TRD_KEYWORD_SUBMODULE:
1391 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1392 return;
1393 default:
1394 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1395 switch (ks.type) {
1396 case TRD_KEYWORD_AUGMENT:
1397 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1398 break;
1399 case TRD_KEYWORD_RPC:
1400 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1401 break;
1402 case TRD_KEYWORD_NOTIF:
1403 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1404 break;
1405 case TRD_KEYWORD_GROUPING:
1406 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1407 break;
1408 case TRD_KEYWORD_YANG_DATA:
1409 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1410 break;
1411 default:
1412 break;
1413 }
1414 break;
1415 }
1416}
1417
1418/**
1419 * @brief Get string length of stored keyword.
1420 * @param[in] type is type of the keyword statement.
1421 * @return length of the keyword statement name.
1422 */
1423static size_t
1424trp_keyword_type_strlen(trt_keyword_type type)
1425{
1426 switch (type) {
1427 case TRD_KEYWORD_MODULE:
1428 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1429 case TRD_KEYWORD_SUBMODULE:
1430 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1431 case TRD_KEYWORD_AUGMENT:
1432 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1433 case TRD_KEYWORD_RPC:
1434 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1435 case TRD_KEYWORD_NOTIF:
1436 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1437 case TRD_KEYWORD_GROUPING:
1438 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1439 case TRD_KEYWORD_YANG_DATA:
1440 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1441 default:
1442 return 0;
1443 }
1444}
1445
1446/**
aPiecek874ea4d2021-04-19 12:26:36 +02001447 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001448 * @param[in] ks is keyword statement structure.
1449 * @param[in] mll is max line length.
1450 * @param[in,out] out is output handler.
1451 */
1452static void
1453trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1454{
1455 uint32_t ind_initial;
1456 uint32_t ind_divided;
1457 /* flag if path must be splitted to more lines */
1458 ly_bool linebreak_was_set;
1459 /* flag if at least one subpath was printed */
1460 ly_bool subpath_printed;
1461 /* the sum of the sizes of the substrings on the current line */
1462 uint32_t how_far;
1463 /* pointer to start of the subpath */
1464 const char *sub_ptr;
1465 /* size of subpath from sub_ptr */
1466 size_t sub_len;
1467
1468 if ((!ks.str) || (ks.str[0] == '\0')) {
1469 return;
1470 }
1471
1472 /* module name cannot be splitted */
1473 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1474 ly_print_(out, "%s", ks.str);
1475 return;
1476 }
1477
1478 /* after -> for trd_keyword_stmt_body do */
1479
1480 /* set begin indentation */
1481 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1482 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1483 linebreak_was_set = 0;
1484 subpath_printed = 0;
1485 how_far = 0;
1486 sub_ptr = ks.str;
1487 sub_len = 0;
1488
1489 while (sub_ptr[0] != '\0') {
1490 uint32_t ind;
1491 /* skip slash */
1492 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
Michal Vasko26bbb272022-08-02 14:54:33 +02001493
aPiecek61d062b2020-11-02 11:05:09 +01001494 /* get position of the end of substr */
1495 tmp = strchr(tmp, '/');
1496 /* set correct size if this is a last substring */
1497 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1498 /* actualize sum of the substring's sizes on the current line */
1499 how_far += sub_len;
1500 /* correction due to colon character if it this is last substring */
1501 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1502 /* choose indentation which depends on
1503 * whether the string is printed on multiple lines or not
1504 */
1505 ind = linebreak_was_set ? ind_divided : ind_initial;
1506 if (ind + how_far <= mll) {
1507 /* printing before max line length */
1508 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1509 subpath_printed = 1;
1510 } else {
1511 /* printing on new line */
1512 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001513 /* first subpath is too long
1514 * but print it at first line anyway
1515 */
aPiecek61d062b2020-11-02 11:05:09 +01001516 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1517 subpath_printed = 1;
1518 continue;
1519 }
1520 ly_print_(out, "\n");
1521 ly_print_(out, "%*c", ind_divided, ' ');
1522 linebreak_was_set = 1;
1523 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1524 how_far = sub_len;
1525 subpath_printed = 1;
1526 }
1527 }
1528}
1529
1530/**
aPiecek874ea4d2021-04-19 12:26:36 +02001531 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001532 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001533 * @param[in] grp_has_data is flag only for grouping section.
1534 * Set to 1 if grouping section has some nodes.
1535 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001536 * @param[in,out] out is output handler.
1537 */
1538static void
aPiecekdc8fd572021-04-19 10:47:23 +02001539trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001540{
1541 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001542 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1543 return;
1544 } else {
1545 ly_print_(out, ":");
1546 }
aPiecek61d062b2020-11-02 11:05:09 +01001547 }
1548}
1549
1550/**
1551 * @brief Print entire struct trt_keyword_stmt structure.
1552 * @param[in] ks is item to print.
1553 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001554 * @param[in] grp_has_data is flag only for grouping section.
1555 * Set to 1 if grouping section has some nodes.
1556 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001557 * @param[in,out] out is output handler.
1558 */
1559static void
aPiecek874ea4d2021-04-19 12:26:36 +02001560trp_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 +01001561{
1562 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1563 return;
1564 }
1565 trt_print_keyword_stmt_begin(ks, out);
1566 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001567 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001568}
1569
aPiecek874ea4d2021-04-19 12:26:36 +02001570/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001571 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001572 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001573
1574/**
aPiecek874ea4d2021-04-19 12:26:36 +02001575 * @brief Printing one line including wrapper and node
1576 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001577 * @param[in] node is \<node\> representation.
1578 * @param[in] pck contains special printing functions callback.
1579 * @param[in] indent contains wrapper and indent in node numbers.
1580 * @param[in,out] out is output handler.
1581 */
1582static void
1583trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1584{
1585 trp_print_wrapper(indent.wrapper, out);
1586 trp_print_node(node, pck, indent.in_node, out);
1587}
1588
1589/**
aPiecek874ea4d2021-04-19 12:26:36 +02001590 * @brief Printing one line including wrapper and
1591 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001592 * @param[in] node is \<node\> representation.
1593 * @param[in] wr is wrapper for printing indentation before node.
1594 * @param[in] out is output handler.
1595 */
1596static void
1597trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1598{
1599 trp_print_wrapper(wr, out);
1600 trp_print_node_up_to_name(node, out);
1601}
1602
1603/**
aPiecek874ea4d2021-04-19 12:26:36 +02001604 * @brief Check if leafref target must be change to string 'leafref'
1605 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001606 * @param[in] node containing leafref target.
1607 * @param[in] wr is wrapper for printing indentation before node.
1608 * @param[in] mll is max line length.
1609 * @param[in] out is output handler.
1610 * @return true if leafref must be changed to string 'leafref'.
1611 */
1612static ly_bool
1613trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1614{
1615 struct ly_out_clb_arg *data;
1616
1617 if (node.type.type != TRD_TYPE_TARGET) {
1618 return 0;
1619 }
1620
1621 /* set ly_out to counting characters */
1622 data = out->method.clb.arg;
1623
1624 data->counter = 0;
1625 data->mode = TRD_CHAR_COUNT;
1626 /* count number of printed bytes */
1627 trp_print_wrapper(wr, out);
1628 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1629 trp_print_divided_node_up_to_name(node, out);
1630 data->mode = TRD_PRINT;
1631
1632 return data->counter + strlen(node.type.str) > mll;
1633}
1634
1635/**
1636 * @brief Get default indent in node based on node values.
1637 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001638 * @return Default indent in node assuming that the node
1639 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001640 */
1641static struct trt_indent_in_node
1642trp_default_indent_in_node(struct trt_node node)
1643{
1644 struct trt_indent_in_node ret;
1645
1646 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1647
1648 /* btw_name_opts */
aPiecekbca57772022-10-13 13:51:59 +02001649 ret.btw_name_opts = node.name.keys ? TRD_INDENT_BEFORE_KEYS : 0;
aPiecek61d062b2020-11-02 11:05:09 +01001650
1651 /* btw_opts_type */
1652 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1653 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1654 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1655 TRD_INDENT_BEFORE_TYPE;
1656 } else {
1657 ret.btw_opts_type = 0;
1658 }
1659
1660 /* btw_type_iffeatures */
1661 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1662
1663 return ret;
1664}
1665
1666/**
1667 * @brief Setting linebreaks in trt_indent_in_node.
1668 *
1669 * The order where the linebreak tag can be placed is from the end.
1670 *
aPiecek874ea4d2021-04-19 12:26:36 +02001671 * @param[in] indent containing alignment lengths
1672 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001673 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001674 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1675 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001676 */
1677static struct trt_indent_in_node
1678trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1679{
1680 /* somewhere must be set a line break in node */
1681 struct trt_indent_in_node ret = indent;
1682
1683 /* gradually break the node from the end */
1684 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1685 ret.btw_type_iffeatures = TRD_LINEBREAK;
1686 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1687 ret.btw_opts_type = TRD_LINEBREAK;
1688 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1689 /* set line break between name and opts */
1690 ret.btw_name_opts = TRD_LINEBREAK;
1691 } else {
1692 /* it is not possible to place a more line breaks,
1693 * unfortunately the max_line_length constraint is violated
1694 */
1695 ret.type = TRD_INDENT_IN_NODE_FAILED;
1696 }
1697 return ret;
1698}
1699
1700/**
1701 * @brief Get the first half of the node based on the linebreak mark.
1702 *
1703 * Items in the second half of the node will be empty.
1704 *
1705 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001706 * @param[in] indent contains information in which part of the \<node\>
1707 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001708 * @return first half of the node, indent is unchanged.
1709 */
1710static struct trt_pair_indent_node
1711trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1712{
1713 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1714
1715 if (indent.btw_name_opts == TRD_LINEBREAK) {
aPiecekbca57772022-10-13 13:51:59 +02001716 ret.node.name.type = node.name.type;
aPiecek61d062b2020-11-02 11:05:09 +01001717 ret.node.type = TRP_EMPTY_TRT_TYPE;
1718 ret.node.iffeatures = 0;
1719 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1720 ret.node.type = TRP_EMPTY_TRT_TYPE;
1721 ret.node.iffeatures = 0;
1722 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1723 ret.node.iffeatures = 0;
1724 }
1725
1726 return ret;
1727}
1728
1729/**
1730 * @brief Get the second half of the node based on the linebreak mark.
1731 *
1732 * Items in the first half of the node will be empty.
1733 * Indentations belonging to the first node will be reset to zero.
1734 *
1735 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001736 * @param[in] indent contains information in which part of the \<node\>
1737 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001738 * @return second half of the node, indent is newly set.
1739 */
1740static struct trt_pair_indent_node
1741trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1742{
1743 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1744
1745 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001746 /* Logically, the information up to token <opts> should
1747 * be deleted, but the the trp_print_node function needs it to
1748 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001749 */
1750 ret.indent.btw_name_opts = 0;
1751 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1752 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1753 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
aPiecekbca57772022-10-13 13:51:59 +02001754 ret.node.name.type = node.name.type;
aPiecek61d062b2020-11-02 11:05:09 +01001755 ret.indent.btw_name_opts = 0;
1756 ret.indent.btw_opts_type = 0;
1757 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1758 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
aPiecekbca57772022-10-13 13:51:59 +02001759 ret.node.name.type = node.name.type;
aPiecek61d062b2020-11-02 11:05:09 +01001760 ret.node.type = TRP_EMPTY_TRT_TYPE;
1761 ret.indent.btw_name_opts = 0;
1762 ret.indent.btw_opts_type = 0;
1763 ret.indent.btw_type_iffeatures = 0;
1764 }
1765 return ret;
1766}
1767
1768/**
1769 * @brief Get the correct alignment for the node.
1770 *
aPiecek874ea4d2021-04-19 12:26:36 +02001771 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001772 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001773 *
1774 * @param[in] node is \<node\> representation.
1775 * @param[in] pck contains speciall callback functions for printing.
1776 * @param[in] indent contains wrapper and indent in node numbers.
1777 * @param[in] mll is max line length.
1778 * @param[in,out] cnt counting number of characters to print.
1779 * @param[in,out] out is output handler.
1780 * @return pair of node and indentation numbers of that node.
1781 */
1782static struct trt_pair_indent_node
1783trp_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)
1784{
1785 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1786
1787 trp_print_line(node, pck, indent, out);
1788
1789 if (*cnt <= mll) {
1790 /* success */
1791 return ret;
1792 } else {
1793 ret.indent = trp_indent_in_node_place_break(ret.indent);
1794 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1795 /* erase information in node due to line break */
1796 ret = trp_first_half_node(node, ret.indent);
1797 /* check if line fits, recursive call */
1798 *cnt = 0;
1799 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1800 /* make sure that the result will be with the status divided
1801 * or eventually with status failed */
1802 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1803 }
1804 return ret;
1805 }
1806}
1807
1808/**
1809 * @brief Get the correct alignment for the node.
1810 *
1811 * @param[in] node is \<node\> representation.
1812 * @param[in] pck contains speciall callback functions for printing.
1813 * @param[in] indent contains wrapper and indent in node numbers.
1814 * @param[in] mll is max line length.
1815 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001816 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1817 * line, some indent variable has negative value as a line break sign.
1818 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1819 * all indent variables values has non-negative number.
1820 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1821 * line, all indent variables has negative or zero values,
1822 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001823 */
1824static struct trt_pair_indent_node
1825trp_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)
1826{
1827 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1828 struct ly_out_clb_arg *data;
1829
1830 /* set ly_out to counting characters */
1831 data = out->method.clb.arg;
1832
1833 data->counter = 0;
1834 data->mode = TRD_CHAR_COUNT;
1835 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1836 data->mode = TRD_PRINT;
1837
1838 return ret;
1839}
1840
1841/**
aPiecek01598c02021-04-23 14:18:24 +02001842 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001843 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001844 * @param[in] node is node representation.
1845 * @param[in] ppck contains speciall callback functions for printing.
1846 * @param[in] ipck contains wrapper and indent in node numbers.
1847 * @param[in] mll is max line length.
1848 * @param[in,out] out is output handler.
1849 */
1850static void
1851trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1852{
1853 ly_bool entire_node_was_printed;
1854 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1855
1856 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1857 /* nothing can be done, continue as usual */
1858 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1859 }
1860
1861 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1862 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1863
1864 if (!entire_node_was_printed) {
1865 ly_print_(out, "\n");
1866 /* continue with second half node */
1867 ind_node = trp_second_half_node(node, ind_node.indent);
1868 /* continue with printing node */
1869 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1870 } else {
1871 return;
1872 }
1873}
1874
1875/**
aPiecek874ea4d2021-04-19 12:26:36 +02001876 * @brief Printing of the wrapper and the whole node,
1877 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001878 * @param[in] node is node representation.
1879 * @param[in] ppck contains speciall callback functions for printing.
1880 * @param[in] ipck contains wrapper and indent in node numbers.
1881 * @param[in] mll is max line length.
1882 * @param[in,out] out is output handler.
1883 */
1884static void
1885trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1886{
1887 struct trt_pair_indent_node ind_node1;
1888 struct trt_pair_indent_node ind_node2;
1889 struct trt_pck_indent tmp;
1890
1891 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1892 node.type.type = TRD_TYPE_LEAFREF;
1893 }
1894
1895 /* check if normal indent is possible */
1896 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1897
1898 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1899 /* node fits to one line */
1900 trp_print_line(node, ppck, ipck, out);
1901 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1902 /* node will be divided */
1903 /* print first half */
1904 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1905 /* pretend that this is normal node */
1906 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1907
1908 trp_print_line(ind_node1.node, ppck, tmp, out);
1909 ly_print_(out, "\n");
1910
1911 /* continue with second half on new line */
1912 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1913 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1914
1915 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1916 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1917 /* node name is too long */
1918 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1919
1920 if (trp_node_body_is_empty(node)) {
1921 return;
1922 } else {
1923 ly_print_(out, "\n");
1924
1925 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1926 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1927 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1928
1929 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1930 }
1931
1932 }
1933}
1934
aPiecek874ea4d2021-04-19 12:26:36 +02001935/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001936 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001937 *********************************************************************/
1938
1939/**
1940 * @brief Get nodetype.
1941 * @param[in] node is any lysp_node.
1942 */
1943static uint16_t
1944trop_nodetype(const void *node)
1945{
1946 return ((const struct lysp_node *)node)->nodetype;
1947}
1948
1949/**
1950 * @brief Get sibling.
1951 * @param[in] node is any lysp_node.
1952 */
1953static const void *
1954trop_next(const void *node)
1955{
1956 return ((const struct lysp_node *)node)->next;
1957}
1958
1959/**
1960 * @brief Get parent.
1961 * @param[in] node is any lysp_node.
1962 */
1963static const void *
1964trop_parent(const void *node)
1965{
1966 return ((const struct lysp_node *)node)->parent;
1967}
1968
1969/**
1970 * @brief Try to get child.
1971 * @param[in] node is any lysp_node.
1972 */
1973static const void *
1974trop_child(const void *node)
1975{
1976 return lysp_node_child(node);
1977}
1978
1979/**
1980 * @brief Try to get action.
1981 * @param[in] node is any lysp_node.
1982 */
1983static const void *
1984trop_actions(const void *node)
1985{
1986 return lysp_node_actions(node);
1987}
1988
1989/**
1990 * @brief Try to get action.
1991 * @param[in] node must be of type lysp_node_action.
1992 */
1993static const void *
1994trop_action_input(const void *node)
1995{
1996 return &((const struct lysp_node_action *)node)->input;
1997}
1998
1999/**
2000 * @brief Try to get action.
2001 * @param[in] node must be of type lysp_node_action.
2002 */
2003static const void *
2004trop_action_output(const void *node)
2005{
2006 return &((const struct lysp_node_action *)node)->output;
2007}
2008
2009/**
2010 * @brief Try to get action.
2011 * @param[in] node is any lysp_node.
2012 */
2013static const void *
2014trop_notifs(const void *node)
2015{
2016 return lysp_node_notifs(node);
2017}
2018
2019/**
aPiecek01598c02021-04-23 14:18:24 +02002020 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02002021 * which are adapted to lysp nodes.
2022 */
2023static struct tro_getters
2024trop_init_getters()
2025{
2026 return (struct tro_getters) {
2027 .nodetype = trop_nodetype,
2028 .next = trop_next,
2029 .parent = trop_parent,
2030 .child = trop_child,
2031 .actions = trop_actions,
2032 .action_input = trop_action_input,
2033 .action_output = trop_action_output,
2034 .notifs = trop_notifs
2035 };
2036}
2037
aPiecek3f247652021-04-19 13:40:25 +02002038/**
2039 * @brief Get nodetype.
2040 * @param[in] node is any lysc_node.
2041 */
2042static uint16_t
2043troc_nodetype(const void *node)
2044{
2045 return ((const struct lysc_node *)node)->nodetype;
2046}
2047
2048/**
2049 * @brief Get sibling.
2050 * @param[in] node is any lysc_node.
2051 */
2052static const void *
2053troc_next(const void *node)
2054{
2055 return ((const struct lysc_node *)node)->next;
2056}
2057
2058/**
2059 * @brief Get parent.
2060 * @param[in] node is any lysc_node.
2061 */
2062static const void *
2063troc_parent(const void *node)
2064{
2065 return ((const struct lysc_node *)node)->parent;
2066}
2067
2068/**
2069 * @brief Try to get child.
2070 * @param[in] node is any lysc_node.
2071 */
2072static const void *
2073troc_child(const void *node)
2074{
2075 return lysc_node_child(node);
2076}
2077
2078/**
2079 * @brief Try to get action.
2080 * @param[in] node is any lysc_node.
2081 */
2082static const void *
2083troc_actions(const void *node)
2084{
2085 return lysc_node_actions(node);
2086}
2087
2088/**
2089 * @brief Try to get action.
2090 * @param[in] node must be of type lysc_node_action.
2091 */
2092static const void *
2093troc_action_input(const void *node)
2094{
2095 return &((const struct lysc_node_action *)node)->input;
2096}
2097
2098/**
2099 * @brief Try to get action.
2100 * @param[in] node must be of type lysc_node_action.
2101 */
2102static const void *
2103troc_action_output(const void *node)
2104{
2105 return &((const struct lysc_node_action *)node)->output;
2106}
2107
2108/**
2109 * @brief Try to get action.
2110 * @param[in] node is any lysc_node.
2111 */
2112static const void *
2113troc_notifs(const void *node)
2114{
2115 return lysc_node_notifs(node);
2116}
2117
2118/**
aPiecek01598c02021-04-23 14:18:24 +02002119 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002120 * which are adapted to lysc nodes.
2121 */
2122static struct tro_getters
2123troc_init_getters()
2124{
2125 return (struct tro_getters) {
2126 .nodetype = troc_nodetype,
2127 .next = troc_next,
2128 .parent = troc_parent,
2129 .child = troc_child,
2130 .actions = troc_actions,
2131 .action_input = troc_action_input,
2132 .action_output = troc_action_output,
2133 .notifs = troc_notifs
2134 };
2135}
2136
aPiecekef1e58e2021-04-19 13:19:44 +02002137/**********************************************************************
2138 * tro functions
2139 *********************************************************************/
2140
2141/**
2142 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002143 *
2144 * This is a general algorithm that is able to
2145 * work with lysp_node or lysc_node.
2146 *
2147 * @param[in] node points to lysp_node or lysc_node.
2148 * @param[in] lysc_tree flag to determine what type the @p node is.
2149 * If set to true, then @p points to lysc_node otherwise lysp_node.
2150 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002151 */
2152static const void *
aPiecek3f247652021-04-19 13:40:25 +02002153tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002154{
2155 struct tro_getters get;
2156 const void *tmp, *parent;
2157 const void *ret;
2158
2159 assert(node);
2160
aPiecek3f247652021-04-19 13:40:25 +02002161 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002162
2163 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2164 if ((tmp = get.next(node))) {
2165 /* next action exists */
2166 ret = tmp;
2167 } else if ((parent = get.parent(node))) {
2168 /* maybe if notif exists as sibling */
2169 ret = get.notifs(parent);
2170 } else {
2171 ret = NULL;
2172 }
2173 } else if (get.nodetype(node) & LYS_INPUT) {
2174 if ((parent = get.parent(node))) {
2175 /* if output action has data */
2176 if (get.child(get.action_output(parent))) {
2177 /* then next sibling is output action */
2178 ret = get.action_output(parent);
2179 } else {
2180 /* input action cannot have siblings other
2181 * than output action.
2182 */
2183 ret = NULL;
2184 }
2185 } else {
2186 /* there is no way how to get output action */
2187 ret = NULL;
2188 }
2189 } else if (get.nodetype(node) & LYS_OUTPUT) {
2190 /* output action cannot have siblings */
2191 ret = NULL;
2192 } else if (get.nodetype(node) & LYS_NOTIF) {
2193 /* must have as a sibling only notif */
2194 ret = get.next(node);
2195 } else {
2196 /* for rest of nodes */
2197 if ((tmp = get.next(node))) {
2198 /* some sibling exists */
2199 ret = tmp;
2200 } else if ((parent = get.parent(node))) {
2201 /* Action and notif are siblings too.
2202 * They can be reached through parent.
2203 */
2204 if ((tmp = get.actions(parent))) {
2205 /* next sibling is action */
2206 ret = tmp;
2207 } else if ((tmp = get.notifs(parent))) {
2208 /* next sibling is notif */
2209 ret = tmp;
2210 } else {
2211 /* sibling not exists */
2212 ret = NULL;
2213 }
2214 } else {
2215 /* sibling not exists */
2216 ret = NULL;
2217 }
2218 }
2219
2220 return ret;
2221}
2222
2223/**
2224 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002225 *
2226 * This is a general algorithm that is able to
2227 * work with lysp_node or lysc_node.
2228 *
2229 * @param[in] node points to lysp_node or lysc_node.
2230 * @param[in] lysc_tree flag to determine what type the @p node is.
2231 * If set to true, then @p points to lysc_node otherwise lysp_node.
2232 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002233 */
2234static const void *
aPiecek3f247652021-04-19 13:40:25 +02002235tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002236{
2237 struct tro_getters get;
2238 const void *tmp;
2239 const void *ret;
2240
2241 assert(node);
2242
aPiecek3f247652021-04-19 13:40:25 +02002243 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002244
2245 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2246 if (get.child(get.action_input(node))) {
2247 /* go to LYS_INPUT */
2248 ret = get.action_input(node);
2249 } else if (get.child(get.action_output(node))) {
2250 /* go to LYS_OUTPUT */
2251 ret = get.action_output(node);
2252 } else {
2253 /* input action and output action have no data */
2254 ret = NULL;
2255 }
2256 } else {
2257 if ((tmp = get.child(node))) {
2258 ret = tmp;
2259 } else {
2260 /* current node can't have children or has no children */
2261 /* but maybe has some actions or notifs */
2262 if ((tmp = get.actions(node))) {
2263 ret = tmp;
2264 } else if ((tmp = get.notifs(node))) {
2265 ret = tmp;
2266 } else {
2267 ret = NULL;
2268 }
2269 }
2270 }
2271
2272 return ret;
2273}
2274
2275/**
aPiecek3f247652021-04-19 13:40:25 +02002276 * @brief Get new trt_parent_cache if we apply the transfer
2277 * to the child node in the tree.
2278 * @param[in] ca is parent cache for current node.
2279 * @param[in] tc contains current tree node.
2280 * @return Cache for the current node.
2281 */
2282static struct trt_parent_cache
2283tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2284{
2285 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2286
2287 if (!tc->lysc_tree) {
2288 const struct lysp_node *pn = tc->pn;
2289
2290 ret.ancestor =
2291 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2292 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2293 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2294 ca.ancestor;
2295
2296 ret.lys_status =
2297 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2298 ca.lys_status;
2299
2300 ret.lys_config =
2301 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2302 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2303 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2304 ca.lys_config;
2305
2306 ret.last_list =
2307 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2308 ca.last_list;
2309 }
2310
2311 return ret;
2312}
2313
2314/**
aPiecekef1e58e2021-04-19 13:19:44 +02002315 * @brief Transformation of the Schema nodes flags to
2316 * Tree diagram \<status\>.
2317 * @param[in] flags is node's flags obtained from the tree.
2318 */
2319static trt_status_type
2320tro_flags2status(uint16_t flags)
2321{
2322 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2323 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2324 TRD_STATUS_TYPE_CURRENT;
2325}
2326
2327/**
2328 * @brief Transformation of the Schema nodes flags to Tree diagram
2329 * \<flags\> but more specifically 'ro' or 'rw'.
2330 * @param[in] flags is node's flags obtained from the tree.
2331 */
2332static trt_flags_type
2333tro_flags2config(uint16_t flags)
2334{
2335 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2336 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2337 TRD_FLAGS_TYPE_EMPTY;
2338}
2339
2340/**
aPiecek3f247652021-04-19 13:40:25 +02002341 * @brief Print current node's iffeatures.
2342 * @param[in] tc is tree context.
2343 * @param[in,out] out is output handler.
2344 */
2345static void
2346tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2347{
2348 const struct lysp_qname *iffs;
2349
aPiecekbbc02932021-05-21 07:19:41 +02002350 if (tc->lysc_tree) {
2351 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2352 iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
2353 } else {
2354 iffs = tc->pn->iffeatures;
2355 }
aPiecek3f247652021-04-19 13:40:25 +02002356 LY_ARRAY_COUNT_TYPE i;
2357
2358 LY_ARRAY_FOR(iffs, i) {
2359 if (i == 0) {
2360 ly_print_(out, "%s", iffs[i].str);
2361 } else {
2362 ly_print_(out, ",%s", iffs[i].str);
2363 }
2364 }
2365
2366}
2367
2368/**
2369 * @brief Print current list's keys.
2370 *
2371 * Well, actually printing keys in the lysp_tree is trivial,
2372 * because char* points to all keys. However, special functions have
2373 * been reserved for this, because in principle the list of elements
2374 * can have more implementations.
2375 *
2376 * @param[in] tc is tree context.
2377 * @param[in,out] out is output handler.
2378 */
2379static void
2380tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2381{
2382 const struct lysp_node_list *list;
2383
aPiecekbbc02932021-05-21 07:19:41 +02002384 if (tc->lysc_tree) {
2385 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2386 list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
2387 } else {
2388 list = (const struct lysp_node_list *)tc->pn;
2389 }
aPiecek3f247652021-04-19 13:40:25 +02002390 assert(list->nodetype & LYS_LIST);
2391
2392 if (trg_charptr_has_data(list->key)) {
2393 ly_print_(out, "%s", list->key);
2394 }
2395}
2396
2397/**
2398 * @brief Get rpcs section if exists.
2399 * @param[in,out] tc is tree context.
2400 * @return Section representation if it exists. The @p tc is modified
2401 * and his pointer points to the first node in rpcs section.
2402 * @return Empty section representation otherwise.
2403 */
2404static struct trt_keyword_stmt
2405tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2406{
aPiecek9f792e52021-04-21 08:33:56 +02002407 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002408 const void *actions;
2409
2410 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002411 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002412 if (actions) {
2413 tc->cn = actions;
2414 }
2415 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002416 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002417 if (actions) {
2418 tc->pn = actions;
2419 tc->tpn = tc->pn;
2420 }
2421 }
2422
2423 if (actions) {
2424 tc->section = TRD_SECT_RPCS;
2425 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2426 } else {
2427 return TRP_EMPTY_KEYWORD_STMT;
2428 }
2429}
2430
2431/**
2432 * @brief Get notification section if exists
2433 * @param[in,out] tc is tree context.
2434 * @return Section representation if it exists.
2435 * The @p tc is modified and his pointer points to the
2436 * first node in notification section.
2437 * @return Empty section representation otherwise.
2438 */
2439static struct trt_keyword_stmt
2440tro_modi_get_notifications(struct trt_tree_ctx *tc)
2441{
aPiecek9f792e52021-04-21 08:33:56 +02002442 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002443 const void *notifs;
2444
2445 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002446 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002447 if (notifs) {
2448 tc->cn = notifs;
2449 }
2450 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002451 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002452 if (notifs) {
2453 tc->pn = notifs;
2454 tc->tpn = tc->pn;
2455 }
2456 }
2457
2458 if (notifs) {
2459 tc->section = TRD_SECT_NOTIF;
2460 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2461 } else {
2462 return TRP_EMPTY_KEYWORD_STMT;
2463 }
2464}
2465
2466/**
aPiecek96baa7f2021-04-23 12:32:00 +02002467 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002468 *
2469 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002470 * @param[in] u is index to the array of extensions (lysc_ext_instance
2471 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002472 * @return Section representation if it exists.
2473 * @return Empty section representation otherwise.
2474 */
2475static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002476tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002477{
aPiecek96baa7f2021-04-23 12:32:00 +02002478 assert(tc);
2479 const void *node;
2480 const char *yang_data_name;
2481
2482 if (tc->lysc_tree) {
2483 struct lysc_ext_instance *exts;
2484 struct lysc_ext_substmt *substmts;
2485
2486 exts = tc->cmod->exts;
2487 substmts = exts[u].substmts;
2488 if (!substmts) {
2489 return TRP_EMPTY_KEYWORD_STMT;
2490 }
2491 node = *(const struct lysc_node **)substmts->storage;
2492 yang_data_name = exts[u].argument;
2493 } else {
2494 struct lysp_ext_instance *exts;
2495
2496 exts = tc->pmod->exts;
2497 node = exts[u].parsed;
2498 yang_data_name = exts[u].argument;
2499 }
2500
2501 if (tc->lysc_tree) {
2502 tc->cn = node;
2503 } else {
2504 tc->tpn_ext = &tc->pmod->exts[u];
2505 tc->pn = node;
2506 }
2507
2508 if (node) {
2509 tc->section = TRD_SECT_YANG_DATA;
2510 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2511 } else {
2512 return TRP_EMPTY_KEYWORD_STMT;
2513 }
aPiecekef1e58e2021-04-19 13:19:44 +02002514}
2515
2516/**
2517 * @brief Get name of the module.
2518 * @param[in] tc is context of the tree.
2519 */
2520static struct trt_keyword_stmt
2521tro_read_module_name(const struct trt_tree_ctx *tc)
2522{
aPiecek9f792e52021-04-21 08:33:56 +02002523 assert(tc);
2524
2525 struct trt_keyword_stmt ret;
2526
2527 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2528 TRD_KEYWORD_SUBMODULE :
2529 TRD_KEYWORD_MODULE;
2530
2531 ret.str = !tc->lysc_tree ?
2532 LYSP_MODULE_NAME(tc->pmod) :
2533 tc->cmod->mod->name;
2534
2535 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002536}
2537
aPiecekb8d5a0a2021-05-20 08:20:24 +02002538/**
2539 * @brief Create implicit "case" node as parent of @p node.
2540 * @param[in] node child of implicit case node.
2541 * @return The case node ready to print.
2542 */
2543static struct trt_node
2544tro_create_implicit_case_node(struct trt_node node)
2545{
2546 struct trt_node ret;
2547
2548 ret.status = node.status;
2549 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2550 ret.name.type = TRD_NODE_CASE;
aPiecekbca57772022-10-13 13:51:59 +02002551 ret.name.keys = node.name.keys;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002552 ret.name.module_prefix = node.name.module_prefix;
2553 ret.name.str = node.name.str;
2554 ret.type = TRP_EMPTY_TRT_TYPE;
aPiecek7a28e2f2021-05-21 07:27:03 +02002555 ret.iffeatures = 0;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002556 ret.last_one = node.last_one;
aPiecek50b64e42022-10-10 10:00:12 +02002557 ret.mount = NULL;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002558
2559 return ret;
2560}
2561
aPiecekef1e58e2021-04-19 13:19:44 +02002562/**********************************************************************
2563 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002564 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002565
2566/**
aPiecek61d062b2020-11-02 11:05:09 +01002567 * @brief Check if list statement has keys.
2568 * @param[in] pn is pointer to the list.
2569 * @return 1 if has keys, otherwise 0.
2570 */
2571static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002572trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002573{
aPiecekef1e58e2021-04-19 13:19:44 +02002574 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002575}
2576
2577/**
2578 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002579 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002580 * @return 1 if has if-features, otherwise 0.
2581 */
2582static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002583trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002584{
2585 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002586 const struct lysp_qname *iffs;
2587
aPiecek61d062b2020-11-02 11:05:09 +01002588 ly_bool ret = 0;
2589
aPiecekef1e58e2021-04-19 13:19:44 +02002590 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002591 LY_ARRAY_FOR(iffs, u) {
2592 ret = 1;
2593 break;
2594 }
2595 return ret;
2596}
2597
2598/**
2599 * @brief Find out if leaf is also the key in last list.
2600 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002601 * @param[in] ca_last_list is pointer to last visited list.
2602 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002603 * @return 1 if leaf is also the key, otherwise 0.
2604 */
2605static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002606trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002607{
2608 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2609 const struct lysp_node_list *list = ca_last_list;
2610
2611 if (!list) {
2612 return 0;
2613 }
2614 return trg_charptr_has_data(list->key) ?
2615 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2616}
2617
2618/**
2619 * @brief Check if container's type is presence.
2620 * @param[in] pn is pointer to container.
2621 * @return 1 if container has presence statement, otherwise 0.
2622 */
2623static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002624trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002625{
2626 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2627}
2628
2629/**
aPiecek7ed8d032022-10-10 12:32:27 +02002630 * @brief Check if container has mount-point extension.
ekinzie0ab8b302022-10-10 03:03:57 -04002631 * @param[in] cn is pointer to container or list.
2632 * @param[out] mount is assigned a pointer to the extension instance, if found
aPiecek7ed8d032022-10-10 12:32:27 +02002633 * @return 1 if node has mount-point.
ekinzie0ab8b302022-10-10 03:03:57 -04002634 */
2635static ly_bool
2636troc_node_has_mount(const struct lysc_node *cn, struct lysc_ext_instance **mount)
2637{
2638 struct lysc_ext_instance *ext;
2639 uint64_t u;
2640
2641 /* The schema-mount extension plugin has already made sure that
2642 * there is only one mount-point here.
2643 */
2644 LY_ARRAY_FOR(cn->exts, u) {
2645 ext = &cn->exts[u];
2646 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") ||
2647 strcmp(ext->def->name, "mount-point")) {
2648 continue;
2649 }
2650 *mount = ext;
2651 return 1;
2652 }
2653 return 0;
2654}
2655
aPiecek7ed8d032022-10-10 12:32:27 +02002656/**
2657 * @brief Check if node has mount-point extension.
2658 *
2659 * @param[in] pn is pointer to container or list.
2660 * @return 1 if node has mount-point.
2661 */
ekinzie0ab8b302022-10-10 03:03:57 -04002662static ly_bool
2663trop_node_has_mount(const struct lysp_node *pn)
2664{
2665 struct lysp_ext_instance *ext;
2666 uint64_t u;
2667
2668 LY_ARRAY_FOR(pn->exts, u) {
2669 ext = &pn->exts[u];
2670 if (strcmp(ext->name, "yangmnt:mount-point")) {
2671 continue;
2672 }
2673 return 1;
2674 }
2675 return 0;
2676}
2677
2678/**
aPiecek61d062b2020-11-02 11:05:09 +01002679 * @brief Get leaflist's path without lysp_node type control.
2680 * @param[in] pn is pointer to the leaflist.
2681 */
2682static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002683trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002684{
2685 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2686
2687 return list->type.path ? list->type.path->expr : NULL;
2688}
2689
2690/**
2691 * @brief Get leaflist's type name without lysp_node type control.
2692 * @param[in] pn is pointer to the leaflist.
2693 */
2694static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002695trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002696{
2697 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2698
2699 return list->type.name;
2700}
2701
2702/**
2703 * @brief Get leaf's path without lysp_node type control.
2704 * @param[in] pn is pointer to the leaf node.
2705 */
2706static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002707trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002708{
2709 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2710
2711 return leaf->type.path ? leaf->type.path->expr : NULL;
2712}
2713
2714/**
2715 * @brief Get leaf's type name without lysp_node type control.
2716 * @param[in] pn is pointer to the leaf's type name.
2717 */
2718static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002719trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002720{
2721 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2722
2723 return leaf->type.name;
2724}
2725
2726/**
aPiecek874ea4d2021-04-19 12:26:36 +02002727 * @brief Get pointer to data using node type specification
2728 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002729 *
aPiecek874ea4d2021-04-19 12:26:36 +02002730 * @param[in] flags is node type specification.
2731 * If it is the correct node, the getter function is called.
2732 * @param[in] f is getter function which provides the desired
2733 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002734 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002735 * @return NULL if node has wrong type or getter function return
2736 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002737 * @return Pointer to desired char pointer obtained from the node.
2738 */
2739static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002740trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002741{
2742 if (pn->nodetype & flags) {
2743 const char *ret = f(pn);
Michal Vasko26bbb272022-08-02 14:54:33 +02002744
aPiecek61d062b2020-11-02 11:05:09 +01002745 return trg_charptr_has_data(ret) ? ret : NULL;
2746 } else {
2747 return NULL;
2748 }
2749}
2750
2751/**
aPiecek61d062b2020-11-02 11:05:09 +01002752 * @brief Resolve \<status\> of the current node.
2753 * @param[in] nodetype is node's type obtained from the tree.
2754 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002755 * @param[in] ca_lys_status is inherited status
2756 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002757 * @return The status type.
2758 */
2759static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002760trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002761{
2762 /* LYS_INPUT and LYS_OUTPUT is special case */
2763 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002764 return tro_flags2status(ca_lys_status);
2765 /* if ancestor's status is deprc or obslt
2766 * and also node's status is not set
2767 */
aPiecek61d062b2020-11-02 11:05:09 +01002768 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2769 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002770 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002771 } else {
2772 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002773 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002774 }
2775}
2776
2777/**
2778 * @brief Resolve \<flags\> of the current node.
2779 * @param[in] nodetype is node's type obtained from the tree.
2780 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002781 * @param[in] ca_ancestor is ancestor type obtained
2782 * from trt_parent_cache.
2783 * @param[in] ca_lys_config is inherited config item
2784 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002785 * @return The flags type.
2786 */
2787static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002788trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002789{
2790 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2791 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2792 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2793 return TRD_FLAGS_TYPE_RO;
2794 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2795 return TRD_FLAGS_TYPE_RO;
2796 } else if (nodetype & LYS_NOTIF) {
2797 return TRD_FLAGS_TYPE_NOTIF;
2798 } else if (nodetype & LYS_USES) {
2799 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2800 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2801 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002802 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002803 /* config is not set. Look at ancestor's config */
2804 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002805 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002806 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002807 }
2808}
2809
2810/**
2811 * @brief Resolve node type of the current node.
2812 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002813 * @param[in] ca_last_list is pointer to the last visited list.
2814 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002815 */
2816static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04002817trop_resolve_node_type(const struct trt_tree_ctx *tc, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002818{
ekinzie0ab8b302022-10-10 03:03:57 -04002819 const struct lysp_node *pn = tc->pn;
2820
aPiecek61d062b2020-11-02 11:05:09 +01002821 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2822 return TRD_NODE_ELSE;
2823 } else if (pn->nodetype & LYS_CASE) {
2824 return TRD_NODE_CASE;
2825 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2826 return TRD_NODE_OPTIONAL_CHOICE;
2827 } else if (pn->nodetype & LYS_CHOICE) {
2828 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04002829 } else if (tc->mounted && (tc->pn->parent == NULL)) {
2830 if (tc->parent_refs) {
2831 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
2832 if (!strcmp(tc->pmod->mod->ns, tc->parent_refs->snodes[v]->module->ns)) {
2833 return TRD_NODE_TOP_LEVEL2;
2834 }
2835 }
2836 }
2837 return TRD_NODE_TOP_LEVEL1;
aPiecekef1e58e2021-04-19 13:19:44 +02002838 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002839 return TRD_NODE_CONTAINER;
aPiecek61d062b2020-11-02 11:05:09 +01002840 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2841 return TRD_NODE_LISTLEAFLIST;
2842 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2843 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002844 } 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 +01002845 return TRD_NODE_OPTIONAL;
2846 } else {
2847 return TRD_NODE_ELSE;
2848 }
2849}
2850
2851/**
aPiecekef1e58e2021-04-19 13:19:44 +02002852 * @brief Resolve \<type\> of the current node.
2853 * @param[in] pn is current node.
2854 */
2855static struct trt_type
2856trop_resolve_type(const struct lysp_node *pn)
2857{
2858 const char *tmp = NULL;
2859
2860 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2861 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2862 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2863 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2864 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2865 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2866 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2867 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2868 } else if (pn->nodetype == LYS_ANYDATA) {
2869 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2870 } else if (pn->nodetype & LYS_ANYXML) {
2871 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2872 } else {
2873 return TRP_EMPTY_TRT_TYPE;
2874 }
2875}
2876
2877/**
aPiecek61d062b2020-11-02 11:05:09 +01002878 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002879 * @param[in] ca contains stored important data
2880 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002881 * @param[in] tc is context of the tree.
2882 */
2883static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002884trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002885{
aPiecekef1e58e2021-04-19 13:19:44 +02002886 const struct lysp_node *pn;
2887 struct trt_node ret;
2888
aPiecek61d062b2020-11-02 11:05:09 +01002889 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002890
2891 pn = tc->pn;
2892 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002893
2894 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002895 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002896
aPiecek61d062b2020-11-02 11:05:09 +01002897 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04002898 if (trop_node_has_mount(pn)) {
2899 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
2900 } else {
2901 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
2902 }
aPiecek61d062b2020-11-02 11:05:09 +01002903
aPiecek61d062b2020-11-02 11:05:09 +01002904 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04002905 ret.name.type = trop_resolve_node_type(tc, ca.last_list);
aPiecekbca57772022-10-13 13:51:59 +02002906 ret.name.keys = (tc->pn->nodetype & LYS_LIST) && trop_list_has_keys(tc->pn);
aPiecek61d062b2020-11-02 11:05:09 +01002907
aPiecek34fa3772021-05-21 12:35:46 +02002908 /* The parsed tree is not compiled, so no node can be augmented
2909 * from another module. This means that nodes from the parsed tree
2910 * will never have the prefix.
2911 */
aPiecek61d062b2020-11-02 11:05:09 +01002912 ret.name.module_prefix = NULL;
2913
2914 /* set node's name */
2915 ret.name.str = pn->name;
2916
2917 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002918 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002919
2920 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002921 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002922
aPiecek3f247652021-04-19 13:40:25 +02002923 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002924
2925 return ret;
2926}
2927
aPiecekef1e58e2021-04-19 13:19:44 +02002928/**
2929 * @brief Find out if the current node has siblings.
2930 * @param[in] tc is context of the tree.
2931 * @return 1 if sibling exists otherwise 0.
2932 */
2933static ly_bool
2934trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2935{
aPiecek3f247652021-04-19 13:40:25 +02002936 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002937}
2938
aPiecek96baa7f2021-04-23 12:32:00 +02002939/**
2940 * @brief Print all yang-data sections and print three dots instead
2941 * of nodes.
2942 * @param[in] exts is array of YANG extension instances from parsed
2943 * module (@ref sizedarrays).
2944 * @param[in] mll is maximum number of characters that can be printed
2945 * on one line.
2946 * @param[in,out] out is output handler.
2947 */
2948static void
2949trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2950{
2951 struct trt_keyword_stmt ks;
2952 LY_ARRAY_COUNT_TYPE u;
2953 struct trt_wrapper wr;
2954
2955 if (!exts) {
2956 return;
2957 }
2958
2959 ly_print_(out, "\n");
2960 ks.type = TRD_KEYWORD_YANG_DATA;
2961 wr = TRP_INIT_WRAPPER_BODY;
2962
2963 LY_ARRAY_FOR(exts, u) {
2964 ly_print_(out, "\n");
2965
2966 /* yang-data <yang-data-name>: */
2967 ks.str = exts[u].argument;
2968 trp_print_keyword_stmt(ks, mll, 0, out);
2969 ly_print_(out, "\n");
2970
2971 /* ... */
2972 trp_print_wrapper(wr, out);
2973 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2974 }
2975}
2976
aPiecek874ea4d2021-04-19 12:26:36 +02002977/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002978 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002979 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002980
2981/**
aPiecek874ea4d2021-04-19 12:26:36 +02002982 * @brief Change current node pointer to its parent
2983 * but only if parent exists.
2984 * @param[in,out] tc is tree context.
2985 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002986 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002987 * @return 0 if the node did not have parents.
2988 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002989 */
2990static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002991trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002992{
2993 assert(tc && tc->pn);
2994 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002995 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002996 tc->pn = tc->pn->parent;
2997 return 1;
2998 } else {
2999 return 0;
3000 }
3001}
3002
3003/**
aPiecek874ea4d2021-04-19 12:26:36 +02003004 * @brief Change the current node pointer to its child
3005 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003006 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003007 * @param[in,out] tc is context of the tree.
3008 * Contains pointer to the current node.
3009 * @return Non-empty \<node\> representation of the current
3010 * node's child. The @p tc is modified.
3011 * @return Empty \<node\> representation if child don't exists.
3012 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003013 */
3014static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003015trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003016{
aPiecekef1e58e2021-04-19 13:19:44 +02003017 const struct lysp_node *tmp;
3018
aPiecek61d062b2020-11-02 11:05:09 +01003019 assert(tc && tc->pn);
3020
aPiecek3f247652021-04-19 13:40:25 +02003021 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02003022 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02003023 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01003024 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02003025 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01003026 }
3027}
3028
3029/**
aPiecek874ea4d2021-04-19 12:26:36 +02003030 * @brief Change the current node pointer to the first child of node's
3031 * parent. If current node is already first sibling/child then nothing
3032 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01003033 * @param[in,out] tc is tree context.
3034 */
3035static void
aPiecekef1e58e2021-04-19 13:19:44 +02003036trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003037{
aPiecek9f792e52021-04-21 08:33:56 +02003038 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01003039
aPiecekef1e58e2021-04-19 13:19:44 +02003040 if (trop_modi_parent(tc)) {
3041 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003042 } else {
3043 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01003044 switch (tc->section) {
3045 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003046 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02003047 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003048 break;
3049 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02003050 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02003051 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003052 break;
3053 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003054 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02003055 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003056 break;
3057 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003058 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02003059 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003060 break;
3061 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02003062 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02003063 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01003064 break;
3065 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003066 /* tpn in this case is of type lysp_ext_instance */
3067 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01003068 break;
aPiecek96baa7f2021-04-23 12:32:00 +02003069 default:
3070 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01003071 }
aPiecek61d062b2020-11-02 11:05:09 +01003072 }
3073}
3074
3075/**
aPiecek874ea4d2021-04-19 12:26:36 +02003076 * @brief Change the pointer to the current node to its next sibling
3077 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01003078 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02003079 * @param[in,out] tc is tree context.
3080 * Contains pointer to the current node.
3081 * @return Non-empty \<node\> representation if sibling exists.
3082 * The @p tc is modified.
3083 * @return Empty \<node\> representation otherwise.
3084 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01003085 */
3086static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003087trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003088{
aPiecekef1e58e2021-04-19 13:19:44 +02003089 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003090
3091 assert(tc && tc->pn);
3092
aPiecek3f247652021-04-19 13:40:25 +02003093 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01003094
aPiecekef1e58e2021-04-19 13:19:44 +02003095 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003096 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3097 tc->tpn = pn;
3098 }
aPiecekef1e58e2021-04-19 13:19:44 +02003099 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003100 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003101 } else {
3102 return TRP_EMPTY_NODE;
3103 }
3104}
3105
3106/**
3107 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003108 * @param[in,out] tc is tree context. It is modified and his current
3109 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003110 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003111 * @return Empty section structure otherwise.
3112 */
3113static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003114trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003115{
aPiecek9f792e52021-04-21 08:33:56 +02003116 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003117 const struct lysp_node_augment *augs;
3118
3119 /* if next_augment func was called for the first time */
3120 if (tc->section != TRD_SECT_AUGMENT) {
3121 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003122 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003123 } else {
3124 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003125 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003126 }
3127
aPiecekdc8fd572021-04-19 10:47:23 +02003128 if (augs) {
3129 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003130 tc->tpn = tc->pn;
3131 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3132 } else {
3133 return TRP_EMPTY_KEYWORD_STMT;
3134 }
3135}
3136
3137/**
aPiecek61d062b2020-11-02 11:05:09 +01003138 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003139 * @param[in,out] tc is tree context. It is modified and his current
3140 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003141 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003142 * @return Empty section representation otherwise.
3143 */
3144static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003145trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003146{
aPiecek9f792e52021-04-21 08:33:56 +02003147 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003148 const struct lysp_node_grp *grps;
3149
3150 if (tc->section != TRD_SECT_GROUPING) {
3151 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003152 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003153 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003154 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003155 }
3156
aPiecekdc8fd572021-04-19 10:47:23 +02003157 if (grps) {
3158 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003159 tc->tpn = tc->pn;
3160 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3161 } else {
3162 return TRP_EMPTY_KEYWORD_STMT;
3163 }
3164}
3165
aPiecek874ea4d2021-04-19 12:26:36 +02003166/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003167 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003168 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003169
3170/**
aPiecek3f247652021-04-19 13:40:25 +02003171 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003172 */
aPiecek3f247652021-04-19 13:40:25 +02003173static ly_bool
3174troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003175{
aPiecek3f247652021-04-19 13:40:25 +02003176 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3177}
aPiecek61d062b2020-11-02 11:05:09 +01003178
aPiecek3f247652021-04-19 13:40:25 +02003179/**
3180 * @brief Resolve \<flags\> of the current node.
3181 *
3182 * Use this function only if trt_tree_ctx.lysc_tree is true.
3183 *
3184 * @param[in] nodetype is current lysc_node.nodetype.
3185 * @param[in] flags is current lysc_node.flags.
3186 * @return The flags type.
3187 */
3188static trt_flags_type
3189troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3190{
3191 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3192 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3193 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3194 return TRD_FLAGS_TYPE_RO;
3195 } else if (nodetype & LYS_IS_NOTIF) {
3196 return TRD_FLAGS_TYPE_RO;
3197 } else if (nodetype & LYS_NOTIF) {
3198 return TRD_FLAGS_TYPE_NOTIF;
3199 } else if (nodetype & LYS_USES) {
3200 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3201 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3202 return TRD_FLAGS_TYPE_RPC;
3203 } else {
3204 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003205 }
aPiecek61d062b2020-11-02 11:05:09 +01003206}
3207
3208/**
aPiecek3f247652021-04-19 13:40:25 +02003209 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003210 *
aPiecek3f247652021-04-19 13:40:25 +02003211 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003212 *
aPiecek3f247652021-04-19 13:40:25 +02003213 * @param[in] nodetype is current lysc_node.nodetype.
3214 * @param[in] flags is current lysc_node.flags.
3215 */
3216static trt_node_type
ekinzie0ab8b302022-10-10 03:03:57 -04003217troc_resolve_node_type(const struct trt_tree_ctx *tc, uint16_t nodetype, uint16_t flags)
aPiecek3f247652021-04-19 13:40:25 +02003218{
3219 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3220 return TRD_NODE_ELSE;
3221 } else if (nodetype & LYS_CASE) {
3222 return TRD_NODE_CASE;
3223 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3224 return TRD_NODE_OPTIONAL_CHOICE;
3225 } else if (nodetype & LYS_CHOICE) {
3226 return TRD_NODE_CHOICE;
ekinzie0ab8b302022-10-10 03:03:57 -04003227 } else if (tc->mounted && (tc->cn->parent == NULL)) {
3228 if (tc->parent_refs) {
3229 for (uint32_t v = 0; v < tc->parent_refs->count; v++) {
3230 if (!strcmp(tc->cn->module->ns, tc->parent_refs->snodes[v]->module->ns)) {
3231 return TRD_NODE_TOP_LEVEL2;
3232 }
3233 }
3234 }
3235 return TRD_NODE_TOP_LEVEL1;
aPiecek3f247652021-04-19 13:40:25 +02003236 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3237 return TRD_NODE_CONTAINER;
aPiecek3f247652021-04-19 13:40:25 +02003238 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3239 return TRD_NODE_LISTLEAFLIST;
3240 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3241 return TRD_NODE_OPTIONAL;
3242 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3243 return TRD_NODE_OPTIONAL;
3244 } else {
3245 return TRD_NODE_ELSE;
3246 }
3247}
3248
3249/**
aPiecek34fa3772021-05-21 12:35:46 +02003250 * @brief Resolve prefix (<prefix>:<name>) of node that has been
3251 * placed from another module via an augment statement.
3252 *
3253 * @param[in] cn is current compiled node.
3254 * @param[in] current_compiled_module is module whose nodes are
3255 * currently being printed.
3256 * @return Prefix of foreign module or NULL.
3257 */
3258static const char *
3259troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
3260{
3261 const struct lys_module *node_module;
3262 const char *ret = NULL;
3263
3264 node_module = cn->module;
3265 if (node_module->compiled != current_compiled_module) {
3266 ret = node_module->prefix;
3267 }
3268
3269 return ret;
3270}
3271
3272/**
aPiecek3f247652021-04-19 13:40:25 +02003273 * @brief Transformation of current lysc_node to struct trt_node.
3274 * @param[in] ca is not used.
3275 * @param[in] tc is context of the tree.
3276 */
3277static struct trt_node
3278troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3279{
3280 (void) ca;
3281 const struct lysc_node *cn;
3282 struct trt_node ret;
3283
aPiecek082c7dc2021-05-20 08:55:07 +02003284 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003285
3286 cn = tc->cn;
3287 ret = TRP_EMPTY_NODE;
3288
3289 /* <status> */
3290 ret.status = tro_flags2status(cn->flags);
3291
aPiecek3f247652021-04-19 13:40:25 +02003292 /* <flags> */
ekinzie0ab8b302022-10-10 03:03:57 -04003293 if (troc_node_has_mount(cn, &ret.mount)) {
3294 ret.flags = TRD_FLAGS_TYPE_MOUNT_POINT;
3295 } else {
3296 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3297 }
aPiecek3f247652021-04-19 13:40:25 +02003298
aPiecek3f247652021-04-19 13:40:25 +02003299 /* set type of the node */
ekinzie0ab8b302022-10-10 03:03:57 -04003300 ret.name.type = troc_resolve_node_type(tc, cn->nodetype, cn->flags);
aPiecekbca57772022-10-13 13:51:59 +02003301 ret.name.keys = (cn->nodetype & LYS_LIST) && !(cn->flags & LYS_KEYLESS);
aPiecek3f247652021-04-19 13:40:25 +02003302
aPiecek34fa3772021-05-21 12:35:46 +02003303 /* <prefix> */
3304 ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
aPiecek3f247652021-04-19 13:40:25 +02003305
3306 /* set node's name */
3307 ret.name.str = cn->name;
3308
aPiecekbbc02932021-05-21 07:19:41 +02003309 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003310 /* <type> */
3311 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003312
aPiecek082c7dc2021-05-20 08:55:07 +02003313 /* <iffeature> */
3314 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3315 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003316 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003317 assert(tc->cn->nodetype & LYS_CASE);
3318
3319 /* <type> */
3320 ret.type = TRP_EMPTY_TRT_TYPE;
3321
aPiecek7a28e2f2021-05-21 07:27:03 +02003322 /* <iffeature> */
3323 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003324 }
aPiecek3f247652021-04-19 13:40:25 +02003325
3326 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3327
3328 return ret;
3329}
3330
3331/**********************************************************************
3332 * Modify troc getters
3333 *********************************************************************/
3334
3335/**
aPiecek01598c02021-04-23 14:18:24 +02003336 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003337 */
3338static ly_bool
3339troc_modi_parent(struct trt_tree_ctx *tc)
3340{
3341 assert(tc && tc->cn);
3342 /* If no parent exists, stay in actual node. */
3343 if (tc->cn->parent) {
3344 tc->cn = tc->cn->parent;
3345 return 1;
3346 } else {
3347 return 0;
3348 }
3349}
3350
3351/**
aPiecek01598c02021-04-23 14:18:24 +02003352 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003353 */
3354static struct trt_node
3355troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3356{
3357 const struct lysc_node *cn;
3358
3359 assert(tc && tc->cn);
3360
3361 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3362
3363 /* if next sibling exists */
3364 if (cn) {
3365 /* update trt_tree_ctx */
3366 tc->cn = cn;
3367 return troc_read_node(ca, tc);
3368 } else {
3369 return TRP_EMPTY_NODE;
3370 }
3371}
3372
3373/**
3374 * @copydoc trop_modi_next_child()
3375 */
3376static struct trt_node
3377troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3378{
3379 const struct lysc_node *tmp;
3380
3381 assert(tc && tc->cn);
3382
3383 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3384 tc->cn = tmp;
3385 return troc_read_node(ca, tc);
3386 } else {
3387 return TRP_EMPTY_NODE;
3388 }
3389}
3390
3391/**
aPiecek01598c02021-04-23 14:18:24 +02003392 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003393 */
3394static void
aPiecek3f247652021-04-19 13:40:25 +02003395troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003396{
aPiecek3f247652021-04-19 13:40:25 +02003397 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003398
aPiecek3f247652021-04-19 13:40:25 +02003399 if (troc_modi_parent(tc)) {
3400 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3401 } else {
3402 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003403 switch (tc->section) {
3404 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003405 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003406 break;
3407 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003408 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003409 break;
3410 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003411 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003412 break;
3413 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003414 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003415 break;
3416 default:
3417 assert(0);
3418 }
aPiecek61d062b2020-11-02 11:05:09 +01003419 }
3420}
3421
aPiecek874ea4d2021-04-19 12:26:36 +02003422/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003423 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003424 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003425
3426/**
3427 * @brief Get size of node name.
3428 * @param[in] name contains name and mark.
3429 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003430 * @return negative value as an indication that option mark
3431 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003432 */
3433static int32_t
3434trb_strlen_of_name_and_mark(struct trt_node_name name)
3435{
3436 size_t name_len = strlen(name.str);
3437
3438 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3439 /* counting also parentheses */
3440 name_len += 2;
3441 }
3442
3443 return trp_mark_is_used(name) ?
3444 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3445 (int32_t)name_len;
3446}
3447
3448/**
aPiecek874ea4d2021-04-19 12:26:36 +02003449 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3450 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003451 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003452 * @param[in] max_len4all is the maximum value of btw_opts_type
3453 * that it can have.
3454 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003455 */
3456static int16_t
3457trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3458{
3459 int32_t name_len;
3460 int16_t min_len;
3461 int16_t ret;
3462
3463 name_len = trb_strlen_of_name_and_mark(name);
3464
3465 /* negative value indicate that in name is some opt mark */
3466 min_len = name_len < 0 ?
3467 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3468 TRD_INDENT_BEFORE_TYPE;
3469 ret = abs(max_len4all) - abs(name_len);
3470
3471 /* correction -> negative indicate that name is too long. */
3472 return ret < 0 ? min_len : ret;
3473}
3474
3475/**
3476 * @brief Print node.
3477 *
aPiecek01598c02021-04-23 14:18:24 +02003478 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003479 * But difference is that take @p max_gap_before_type which will be
3480 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003481 *
aPiecek9bdd7592021-05-20 08:13:20 +02003482 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003483 * @param[in] max_gap_before_type is number of indent before \<type\>.
3484 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003485 * @param[in] pc contains mainly functions for printing.
3486 * @param[in] tc is tree context.
3487 */
3488static void
aPiecek9bdd7592021-05-20 08:13:20 +02003489trb_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 +01003490{
aPiecek61d062b2020-11-02 11:05:09 +01003491 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3492
3493 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3494 /* print actual node with unified indent */
3495 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3496 }
3497 /* after -> print actual node with default indent */
3498 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3499 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
aPiecek7ed8d032022-10-10 12:32:27 +02003500 if ((node.flags == TRD_FLAGS_TYPE_MOUNT_POINT) && node.mount) {
ekinzie0ab8b302022-10-10 03:03:57 -04003501 struct trt_wrapper wr_mount;
3502 struct tro_getters get;
3503
3504 wr_mount = pc->fp.read.if_sibling_exists(tc) ?
3505 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3506
3507 get = tc->lysc_tree ? troc_init_getters() : trop_init_getters();
3508 if (get.child(tc->lysc_tree ? (void *)tc->cn : (void *)tc->pn)) {
3509 /* If this node has a child, we need to draw a vertical line
3510 * from the last mounted module to the first child
3511 */
3512 wr_mount = trp_wrapper_set_mark_top(wr_mount);
3513 }
3514
aPiecek7ed8d032022-10-10 12:32:27 +02003515 trb_print_mount_point(node.mount, wr_mount, pc);
ekinzie0ab8b302022-10-10 03:03:57 -04003516 }
aPiecek61d062b2020-11-02 11:05:09 +01003517}
3518
3519/**
aPiecek874ea4d2021-04-19 12:26:36 +02003520 * @brief Check if parent of the current node is the last
3521 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003522 *
aPiecek874ea4d2021-04-19 12:26:36 +02003523 * To mantain stability use this function only if the current node is
3524 * the first of the siblings.
3525 * Side-effect -> current node is set to the first sibling
3526 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003527 *
aPiecek01598c02021-04-23 14:18:24 +02003528 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003529 * @param[in,out] tc is tree context.
3530 * @return 1 if parent is last sibling otherwise 0.
3531 */
3532static ly_bool
3533trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3534{
3535 if (fp.modify.parent(tc)) {
3536 ly_bool ret = fp.read.if_sibling_exists(tc);
Michal Vasko26bbb272022-08-02 14:54:33 +02003537
aPiecek61d062b2020-11-02 11:05:09 +01003538 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3539 return !ret;
3540 } else {
3541 return !fp.read.if_sibling_exists(tc);
3542 }
3543}
3544
3545/**
3546 * @brief Find sibling with the biggest node name and return that size.
3547 *
3548 * Side-effect -> Current node is set to the first sibling.
3549 *
3550 * @param[in] ca contains inherited data from ancestors.
3551 * @param[in] pc contains mainly functions for printing.
3552 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003553 * @return positive number as a sign that only the node name is
3554 * included in the size.
3555 * @return negative number sign that node name and his opt mark is
3556 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003557 */
3558static int32_t
3559trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3560{
3561 int32_t ret = 0;
3562
3563 pc->fp.modify.first_sibling(tc);
3564
3565 for (struct trt_node node = pc->fp.read.node(ca, tc);
3566 !trp_node_is_empty(node);
3567 node = pc->fp.modify.next_sibling(ca, tc)) {
3568 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
Michal Vasko26bbb272022-08-02 14:54:33 +02003569
aPiecek61d062b2020-11-02 11:05:09 +01003570 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3571 }
3572 pc->fp.modify.first_sibling(tc);
3573 return ret;
3574}
3575
3576/**
aPiecek874ea4d2021-04-19 12:26:36 +02003577 * @brief Find maximal indent between
3578 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003579 *
3580 * Side-effect -> Current node is set to the first sibling.
3581 *
3582 * @param[in] ca contains inherited data from ancestors.
3583 * @param[in] pc contains mainly functions for printing.
3584 * @param[in,out] tc is tree context.
3585 * @return max btw_opts_type value for rest of the siblings
3586 */
3587static int16_t
3588trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3589{
3590 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3591 int16_t ind_before_type = maxlen_node_name < 0 ?
3592 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3593 TRD_INDENT_BEFORE_TYPE;
3594
3595 return abs(maxlen_node_name) + ind_before_type;
3596}
3597
3598/**
aPiecek874ea4d2021-04-19 12:26:36 +02003599 * @brief Find out if it is possible to unify
3600 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003601 *
aPiecek874ea4d2021-04-19 12:26:36 +02003602 * The goal is for all node siblings to have the same alignment
3603 * for \<type\> as if they were in a column. All siblings who cannot
3604 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003605 * Side-effect -> Current node is set to the first sibling.
3606 *
3607 * @param[in] ca contains inherited data from ancestors.
3608 * @param[in] pc contains mainly functions for printing.
3609 * @param[in,out] tc is tree context.
3610 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003611 * @return positive number indicating the maximum number of spaces
3612 * before \<type\> if the length of the node name is 0. To calculate
3613 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003614 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003615*/
3616static uint32_t
3617trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3618{
3619 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3620}
3621
3622/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003623 * @brief Check if there is no case statement
3624 * under the choice statement.
3625 *
3626 * It can return true only if the Parsed schema tree
3627 * is used for browsing.
3628 *
3629 * @param[in] tc is tree context.
3630 * @return 1 if implicit case statement is present otherwise 0.
3631 */
3632static ly_bool
3633trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3634{
3635 return !tc->lysc_tree && tc->pn->parent &&
3636 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3637 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3638 LYS_LEAF | LYS_LEAFLIST));
3639}
3640
3641static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3642 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3643
3644/**
3645 * @brief Print implicit case node and his subtree.
3646 *
3647 * @param[in] node is child of implicit case.
3648 * @param[in] wr is wrapper for printing identation before node.
3649 * @param[in] ca contains inherited data from ancestors.
3650 * @param[in] pc contains mainly functions for printing.
3651 * @param[in] tc is tree context. Its settings should be the same as
3652 * before the function call.
3653 */
3654static void
3655trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3656 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3657{
3658 struct trt_node case_node;
3659 struct trt_wrapper wr_case_child;
3660
3661 case_node = tro_create_implicit_case_node(node);
3662 ly_print_(pc->out, "\n");
3663 trb_print_entire_node(case_node, 0, wr, pc, tc);
3664 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3665 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3666 ly_print_(pc->out, "\n");
3667 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3668}
3669
3670/**
aPiecek874ea4d2021-04-19 12:26:36 +02003671 * @brief For the current node: recursively print all of its child
3672 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003673 *
aPiecek01598c02021-04-23 14:18:24 +02003674 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003675 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003676 * Nodes are printed, including unified sibling node alignment
3677 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003678 * Side-effect -> current node is set to the last sibling.
3679 *
3680 * @param[in] wr is wrapper for printing identation before node.
3681 * @param[in] ca contains inherited data from ancestors.
3682 * @param[in] pc contains mainly functions for printing.
3683 * @param[in,out] tc is tree context.
3684 */
3685static void
3686trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3687{
3688 uint32_t max_gap_before_type;
3689 ly_bool sibling_flag = 0;
3690 ly_bool child_flag = 0;
3691
3692 /* if node is last sibling, then do not add '|' to wrapper */
3693 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3694 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3695
3696 /* try unified indentation in node */
3697 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3698
3699 /* print all siblings */
3700 do {
3701 struct trt_parent_cache new_ca;
3702 struct trt_node node;
Michal Vasko26bbb272022-08-02 14:54:33 +02003703
aPiecek9bdd7592021-05-20 08:13:20 +02003704 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003705
aPiecekb8d5a0a2021-05-20 08:20:24 +02003706 if (!trb_need_implicit_node_case(tc)) {
3707 /* normal behavior */
3708 ly_print_(pc->out, "\n");
3709 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3710 new_ca = tro_parent_cache_for_child(ca, tc);
3711 /* go to the actual node's child or stay in actual node */
3712 node = pc->fp.modify.next_child(ca, tc);
3713 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003714
aPiecekb8d5a0a2021-05-20 08:20:24 +02003715 if (child_flag) {
3716 /* print all childs - recursive call */
3717 trb_print_nodes(wr, new_ca, pc, tc);
3718 /* get back from child node to actual node */
3719 pc->fp.modify.parent(tc);
3720 }
3721 } else {
3722 /* The case statement is omitted (shorthand).
3723 * Print implicit case node and his subtree.
3724 */
3725 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003726 }
3727
3728 /* go to the actual node's sibling */
3729 node = pc->fp.modify.next_sibling(ca, tc);
3730 sibling_flag = !trp_node_is_empty(node);
3731
3732 /* go to the next sibling or stay in actual node */
3733 } while (sibling_flag);
3734}
3735
3736/**
aPiecek153b00f2021-04-20 13:52:57 +02003737 * @brief Calculate the wrapper about how deep in the tree the node is.
ekinzie0ab8b302022-10-10 03:03:57 -04003738 * @param[in] wr_in A wrapper to use as a starting point
aPiecek153b00f2021-04-20 13:52:57 +02003739 * @param[in] node from which to count.
3740 * @return wrapper for @p node.
3741 */
3742static struct trt_wrapper
ekinzie0ab8b302022-10-10 03:03:57 -04003743trb_count_depth(const struct trt_wrapper *wr_in, const struct lysc_node *node)
aPiecek153b00f2021-04-20 13:52:57 +02003744{
ekinzie0ab8b302022-10-10 03:03:57 -04003745 struct trt_wrapper wr = wr_in ? *wr_in : TRP_INIT_WRAPPER_TOP;
aPiecek153b00f2021-04-20 13:52:57 +02003746 const struct lysc_node *parent;
3747
3748 if (!node) {
3749 return wr;
3750 }
3751
3752 for (parent = node->parent; parent; parent = parent->parent) {
3753 wr = trp_wrapper_set_shift(wr);
3754 }
3755
3756 return wr;
3757}
3758
3759/**
3760 * @brief Print all parent nodes of @p node and the @p node itself.
3761 *
3762 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3763 *
3764 * @param[in] node on which the function is focused.
aPiecek7ed8d032022-10-10 12:32:27 +02003765 * @param[in] wr_in for printing identation before node.
aPiecek01598c02021-04-23 14:18:24 +02003766 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003767 * @param[in,out] tc is context of tree printer.
3768 * @return wrapper for @p node.
3769 */
3770static void
ekinzie0ab8b302022-10-10 03:03:57 -04003771trb_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 +02003772{
3773 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003774 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003775
3776 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3777
3778 /* stop recursion */
3779 if (!node) {
3780 return;
3781 }
ekinzie0ab8b302022-10-10 03:03:57 -04003782 trb_print_parents(node->parent, wr_in, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003783
3784 /* setup for printing */
3785 tc->cn = node;
ekinzie0ab8b302022-10-10 03:03:57 -04003786 wr = trb_count_depth(wr_in, node);
aPiecek153b00f2021-04-20 13:52:57 +02003787
3788 /* print node */
3789 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003790 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3791 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003792}
3793
3794/**
aPiecekdc8fd572021-04-19 10:47:23 +02003795 * @brief Get address of the current node.
3796 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003797 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003798 */
3799static const void *
3800trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3801{
aPiecek3f247652021-04-19 13:40:25 +02003802 return tc->lysc_tree ?
3803 (const void *)tc->cn :
3804 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003805}
3806
3807/**
3808 * @brief Get address of current node's child.
3809 * @param[in,out] tc contains current node.
3810 */
3811static const void *
3812trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3813{
3814 if (!trb_tree_ctx_get_node(tc)) {
3815 return NULL;
3816 }
3817
aPiecek3f247652021-04-19 13:40:25 +02003818 if (tc->lysc_tree) {
3819 return lysc_node_child(tc->cn);
3820 } else {
3821 return lysp_node_child(tc->pn);
3822 }
aPiecekdc8fd572021-04-19 10:47:23 +02003823}
3824
3825/**
3826 * @brief Set current node on its child.
3827 * @param[in,out] tc contains current node.
3828 */
3829static void
3830trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3831{
aPiecek3f247652021-04-19 13:40:25 +02003832 const void *node = trb_tree_ctx_get_child(tc);
3833
3834 if (tc->lysc_tree) {
3835 tc->cn = node;
3836 } else {
3837 tc->pn = node;
3838 }
aPiecekdc8fd572021-04-19 10:47:23 +02003839}
3840
3841/**
aPiecek61d062b2020-11-02 11:05:09 +01003842 * @brief Print subtree of nodes.
3843 *
3844 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003845 * Before root node is no linebreak printing. This must be addressed by
3846 * the caller. Root node will also be printed. Behind last printed node
3847 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003848 *
aPiecek9bdd7592021-05-20 08:13:20 +02003849 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003850 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003851 * ::trb_try_unified_indent() function for root node.
3852 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003853 * @param[in] wr is wrapper saying how deep in the whole tree
3854 * is the root of the subtree.
3855 * @param[in] ca is parent_cache from root's parent.
3856 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003857 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003858 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003859 */
3860static void
aPiecek9bdd7592021-05-20 08:13:20 +02003861trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3862 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003863{
3864 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003865
aPiecek9bdd7592021-05-20 08:13:20 +02003866 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
ekinzie0ab8b302022-10-10 03:03:57 -04003867
aPiecek61d062b2020-11-02 11:05:09 +01003868 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003869 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003870 node = pc->fp.modify.next_child(ca, tc);
3871
3872 if (!trp_node_is_empty(node)) {
3873 /* print root's nodes */
3874 trb_print_nodes(wr, new_ca, pc, tc);
3875 /* get back from child node to actual node */
3876 pc->fp.modify.parent(tc);
3877 }
3878}
3879
3880/**
3881 * @brief Get number of siblings.
3882 *
3883 * Side-effect -> current node is set to the first sibling.
3884 *
3885 * @param[in] fp contains callback functions which modify tree context
3886 * @param[in,out] tc is the tree context.
3887 * @return Number of siblings of the current node.
3888 */
3889static uint32_t
3890trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3891{
3892 uint32_t ret = 1;
3893 struct trt_node node = TRP_EMPTY_NODE;
3894
3895 /* including actual node */
3896 fp.first_sibling(tc);
3897 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3898 ret++;
3899 }
3900 fp.first_sibling(tc);
3901 return ret;
3902}
3903
3904/**
3905 * @brief Print all parents and their children.
3906 *
aPiecek874ea4d2021-04-19 12:26:36 +02003907 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003908 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003909 * for all top-level siblings. Use this function after 'module' keyword
3910 * or 'augment' and so. The nodes may not be exactly top-level in the
3911 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003912 *
aPiecek153b00f2021-04-20 13:52:57 +02003913 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3914 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003915 * @param[pc] pc contains mainly functions for printing.
3916 * @param[in,out] tc is tree context.
3917 */
3918static void
aPiecek153b00f2021-04-20 13:52:57 +02003919trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003920{
aPiecek61d062b2020-11-02 11:05:09 +01003921 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003922 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003923 uint32_t total_parents;
3924 uint32_t max_gap_before_type;
3925
aPiecekdc8fd572021-04-19 10:47:23 +02003926 if (!trb_tree_ctx_get_node(tc)) {
3927 return;
3928 }
3929
aPiecek61d062b2020-11-02 11:05:09 +01003930 ca = TRP_EMPTY_PARENT_CACHE;
3931 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3932 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3933
aPiecek3f247652021-04-19 13:40:25 +02003934 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003935 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3936 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003937 ca.lys_config = 0x0;
3938 }
aPiecekdc8fd572021-04-19 10:47:23 +02003939 }
3940
aPiecek61d062b2020-11-02 11:05:09 +01003941 for (uint32_t i = 0; i < total_parents; i++) {
3942 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003943 node = pc->fp.read.node(ca, tc);
3944 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003945 pc->fp.modify.next_sibling(ca, tc);
3946 }
3947}
3948
aPiecek7ed8d032022-10-10 12:32:27 +02003949/**
3950 * @brief Mounted module iterator.
3951 *
3952 * Omit internal modules, modules with no nodes (e.g., iana-if-types)
3953 * and modules that were loaded as the result of a parent-reference.
3954 *
3955 * @param[in] ext_ctx is special context of mount-point extension.
3956 * @param[in] parent_refs is set of parent-references. Can be NULL for case of 'inline' schema-ref.
3957 * @param[in,out] state of the iterator. Set the value to zero for initialization.
3958 * @return First/next mounted module or NULL.
3959 */
3960static const struct lys_module *
3961trb_mounted_module_iter(struct ly_ctx *ext_ctx, struct ly_set *parent_refs, uint32_t *state)
3962{
3963 const struct lys_module *mod = NULL;
3964 ly_bool from_parent_ref;
3965 uint32_t j;
3966
3967 if (!(*state)) {
3968 /* Get first non-internal module. */
3969 *state = ly_ctx_internal_modules_count(ext_ctx);
3970 }
3971
3972 while ((mod = ly_ctx_get_module_iter(ext_ctx, state))) {
3973 if (mod->compiled && !mod->compiled->data) {
3974 /* Compiled module with no data-nodes. */
3975 continue;
3976 } else if (mod->parsed && !mod->parsed->data) {
3977 /* Parsed module with no data-nodes. */
3978 continue;
3979 } else if (!parent_refs) {
3980 /* Mounting in 'inline' mode. Success. */
3981 break;
3982 }
3983
3984 /* Check if the module is not in parent-reference. */
3985 from_parent_ref = 0;
3986 for (j = 0; j < parent_refs->count; j++) {
3987 if (!strcmp(mod->ns, parent_refs->snodes[j]->module->ns)) {
3988 from_parent_ref = 1;
3989 break;
3990 }
3991 }
3992 if (!from_parent_ref) {
3993 /* Success. */
3994 break;
3995 }
3996 }
3997
3998 return mod;
3999}
4000
aPiecek874ea4d2021-04-19 12:26:36 +02004001/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004002 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02004003 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004004
4005/**
aPiecekdc8fd572021-04-19 10:47:23 +02004006 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01004007 *
aPiecekdc8fd572021-04-19 10:47:23 +02004008 * @param[in] module YANG schema tree structure representing
4009 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01004010 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02004011 * @param[in] max_line_length is the maximum line length limit
4012 * that should not be exceeded.
aPiecek7ed8d032022-10-10 12:32:27 +02004013 * @param[in] mounted context is used for printing the YANG Schema mount.
4014 * @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 +02004015 * @param[in,out] pc will be adapted to lysp_tree.
4016 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01004017 */
4018static void
aPiecek7ed8d032022-10-10 12:32:27 +02004019trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, ly_bool mounted,
4020 const struct ly_set *parent_refs, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01004021{
aPiecekdc8fd572021-04-19 10:47:23 +02004022 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02004023 .lysc_tree = 0,
aPiecek7ed8d032022-10-10 12:32:27 +02004024 .mounted = mounted || parent_refs,
aPiecekdc8fd572021-04-19 10:47:23 +02004025 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004026 .pmod = module->parsed,
4027 .cmod = NULL,
4028 .pn = module->parsed ? module->parsed->data : NULL,
4029 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek7ed8d032022-10-10 12:32:27 +02004030 .cn = NULL,
4031 .parent_refs = parent_refs
aPiecekdc8fd572021-04-19 10:47:23 +02004032 };
aPiecek61d062b2020-11-02 11:05:09 +01004033
aPiecekdc8fd572021-04-19 10:47:23 +02004034 pc->out = out;
4035
4036 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02004037 .parent = trop_modi_parent,
4038 .first_sibling = trop_modi_first_sibling,
4039 .next_sibling = trop_modi_next_sibling,
4040 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01004041 };
4042
aPiecekdc8fd572021-04-19 10:47:23 +02004043 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01004044 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02004045 .node = trop_read_node,
4046 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01004047 };
4048
aPiecekdc8fd572021-04-19 10:47:23 +02004049 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02004050 .print_features_names = tro_print_features_names,
4051 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01004052 };
4053
aPiecekdc8fd572021-04-19 10:47:23 +02004054 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01004055}
4056
4057/**
aPiecek3f247652021-04-19 13:40:25 +02004058 * @brief Settings if lysc_node are used for browsing through the tree.
4059 *
4060 * Pointers to current nodes will be set to module data.
4061 *
4062 * @param[in] module YANG schema tree structure representing
4063 * YANG module.
4064 * @param[in] out is output handler.
4065 * @param[in] max_line_length is the maximum line length limit
4066 * that should not be exceeded.
aPiecek7ed8d032022-10-10 12:32:27 +02004067 * @param[in] mounted context is used for printing the YANG Schema mount.
4068 * @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 +02004069 * @param[in,out] pc will be adapted to lysc_tree.
4070 * @param[in,out] tc will be adapted to lysc_tree.
4071 */
4072static void
aPiecek7ed8d032022-10-10 12:32:27 +02004073trm_lysc_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, ly_bool mounted,
4074 const struct ly_set *parent_refs, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek3f247652021-04-19 13:40:25 +02004075{
4076 *tc = (struct trt_tree_ctx) {
4077 .lysc_tree = 1,
aPiecek7ed8d032022-10-10 12:32:27 +02004078 .mounted = mounted || parent_refs,
aPiecek3f247652021-04-19 13:40:25 +02004079 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02004080 .pmod = module->parsed,
4081 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02004082 .tpn = NULL,
4083 .pn = NULL,
aPiecek7ed8d032022-10-10 12:32:27 +02004084 .cn = module->compiled->data,
4085 .parent_refs = parent_refs
aPiecek3f247652021-04-19 13:40:25 +02004086 };
4087
4088 pc->out = out;
4089
4090 pc->fp.modify = (struct trt_fp_modify_ctx) {
4091 .parent = troc_modi_parent,
4092 .first_sibling = troc_modi_first_sibling,
4093 .next_sibling = troc_modi_next_sibling,
4094 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02004095 };
4096
4097 pc->fp.read = (struct trt_fp_read) {
4098 .module_name = tro_read_module_name,
4099 .node = troc_read_node,
4100 .if_sibling_exists = troc_read_if_sibling_exists
4101 };
4102
4103 pc->fp.print = (struct trt_fp_print) {
4104 .print_features_names = tro_print_features_names,
4105 .print_keys = tro_print_keys
4106 };
4107
4108 pc->max_line_length = max_line_length;
4109}
4110
4111/**
4112 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02004113 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02004114 * @param[in,out] tc resets to lysc browsing.
4115 */
4116static void
4117trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4118{
aPiecek7ed8d032022-10-10 12:32:27 +02004119 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 +02004120}
4121
4122/**
4123 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02004124 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02004125 * @param[in,out] tc resets to lysp browsing.
4126 */
4127static void
4128trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4129{
aPiecek7ed8d032022-10-10 12:32:27 +02004130 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 +02004131}
4132
4133/**
4134 * @brief If augment's target node is located on the current module.
4135 * @param[in] pn is examined augment.
4136 * @param[in] pmod is current module.
4137 * @return 1 if nodeid refers to the local node, otherwise 0.
4138 */
4139static ly_bool
4140trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
4141{
4142 const char *id, *prefix, *name;
4143 size_t prefix_len, name_len;
4144 const struct lys_module *mod;
4145 ly_bool ret = 0;
4146
4147 if (pn == NULL) {
4148 return ret;
4149 }
4150
4151 id = pn->nodeid;
4152 if (!id) {
4153 return ret;
4154 }
4155 /* only absolute-schema-nodeid is taken into account */
4156 assert(id[0] == '/');
4157 ++id;
4158
4159 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
4160 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02004161 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
Michal Vasko3003c9a2022-03-29 12:10:00 +02004162 ret = mod ? (mod->parsed == pmod) : 0;
aPiecek3f247652021-04-19 13:40:25 +02004163 } else {
4164 ret = 1;
4165 }
4166
4167 return ret;
4168}
4169
4170/**
aPiecek96baa7f2021-04-23 12:32:00 +02004171 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01004172 *
aPiecekdc8fd572021-04-19 10:47:23 +02004173 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02004174 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01004175 *
aPiecekdc8fd572021-04-19 10:47:23 +02004176 * @param[in] ks is section representation.
4177 * @param[in] pc contains mainly functions for printing.
4178 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01004179 */
4180static void
aPiecekdc8fd572021-04-19 10:47:23 +02004181trm_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 +01004182{
aPiecekdc8fd572021-04-19 10:47:23 +02004183 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4184 return;
4185 }
4186
4187 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
4188 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02004189 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004190 } else {
aPiecek153b00f2021-04-20 13:52:57 +02004191 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004192 }
4193}
4194
4195/**
aPiecek96baa7f2021-04-23 12:32:00 +02004196 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02004197 *
aPiecek96baa7f2021-04-23 12:32:00 +02004198 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02004199 *
4200 * @param[in] ks is section representation.
4201 * @param[in] pc contains mainly functions for printing.
4202 * @param[in,out] tc is the tree context.
4203 */
4204static void
4205trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4206{
4207 ly_bool grp_has_data = 0;
4208
4209 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4210 return;
4211 }
4212
4213 if (ks.type == TRD_KEYWORD_GROUPING) {
4214 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4215 }
4216
4217 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4218 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004219 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004220}
4221
4222/**
4223 * @brief Print 'module' keyword, its name and all nodes.
4224 * @param[in] pc contains mainly functions for printing.
4225 * @param[in,out] tc is the tree context.
4226 */
4227static void
4228trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4229{
4230 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4231}
4232
4233/**
4234 * @brief For all augment sections: print 'augment' keyword,
4235 * its target node and all nodes.
4236 * @param[in] pc contains mainly functions for printing.
4237 * @param[in,out] tc is the tree context.
4238 */
4239static void
4240trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4241{
4242 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004243 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004244
aPiecek3f247652021-04-19 13:40:25 +02004245 if (tc->lysc_tree) {
4246 origin_was_lysc_tree = 1;
4247 trm_reset_to_lysp_tree_ctx(pc, tc);
4248 }
4249
aPiecekdc8fd572021-04-19 10:47:23 +02004250 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004251 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004252 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004253 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004254
aPiecek3f247652021-04-19 13:40:25 +02004255 if (origin_was_lysc_tree) {
4256 /* if lysc tree is used, then only augments targeting
4257 * another module are printed
4258 */
aPiecek9f792e52021-04-21 08:33:56 +02004259 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004260 continue;
4261 }
4262 }
4263
aPiecekdc8fd572021-04-19 10:47:23 +02004264 if (once) {
4265 ly_print_(pc->out, "\n");
4266 ly_print_(pc->out, "\n");
4267 once = 0;
4268 } else {
4269 ly_print_(pc->out, "\n");
4270 }
4271
4272 trm_print_section_as_subtree(ks, pc, tc);
4273 }
aPiecek3f247652021-04-19 13:40:25 +02004274
4275 if (origin_was_lysc_tree) {
4276 trm_reset_to_lysc_tree_ctx(pc, tc);
4277 }
aPiecekdc8fd572021-04-19 10:47:23 +02004278}
4279
4280/**
4281 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4282 * @param[in] pc contains mainly functions for printing.
4283 * @param[in,out] tc is the tree context.
4284 */
4285static void
4286trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4287{
4288 struct trt_keyword_stmt rpc;
4289
aPiecek01598c02021-04-23 14:18:24 +02004290 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004291
4292 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4293 ly_print_(pc->out, "\n");
4294 ly_print_(pc->out, "\n");
4295 trm_print_section_as_family_tree(rpc, pc, tc);
4296 }
4297}
4298
4299/**
4300 * @brief For notifications section: print 'notifications' keyword
4301 * and all its nodes.
4302 * @param[in] pc contains mainly functions for printing.
4303 * @param[in,out] tc is the tree context.
4304 */
4305static void
4306trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4307{
4308 struct trt_keyword_stmt notifs;
4309
aPiecek01598c02021-04-23 14:18:24 +02004310 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004311
4312 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4313 ly_print_(pc->out, "\n");
4314 ly_print_(pc->out, "\n");
4315 trm_print_section_as_family_tree(notifs, pc, tc);
4316 }
4317}
4318
4319/**
4320 * @brief For all grouping sections: print 'grouping' keyword, its name
4321 * and all nodes.
4322 * @param[in] pc contains mainly functions for printing.
4323 * @param[in,out] tc is the tree context.
4324 */
4325static void
4326trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4327{
4328 ly_bool once;
4329
aPiecek01598c02021-04-23 14:18:24 +02004330 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004331 return;
4332 }
4333
4334 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004335 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004336 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004337 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004338 if (once) {
4339 ly_print_(pc->out, "\n");
4340 ly_print_(pc->out, "\n");
4341 once = 0;
4342 } else {
4343 ly_print_(pc->out, "\n");
4344 }
4345 trm_print_section_as_subtree(ks, pc, tc);
4346 }
4347}
4348
4349/**
4350 * @brief For all yang-data sections: print 'yang-data' keyword
4351 * and all its nodes.
4352 * @param[in] pc contains mainly functions for printing.
4353 * @param[in,out] tc is the tree context.
4354 */
4355static void
aPiecek96baa7f2021-04-23 12:32:00 +02004356trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004357{
aPiecek96baa7f2021-04-23 12:32:00 +02004358 ly_bool once;
4359 LY_ARRAY_COUNT_TYPE count;
4360
4361 count = LY_ARRAY_COUNT(tc->pmod->exts);
4362 if (count == 0) {
4363 return;
4364 }
4365
4366 once = 1;
4367 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4368 struct trt_keyword_stmt ks;
4369
aPiecek01598c02021-04-23 14:18:24 +02004370 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004371 * ::lysp_ext_instance.parsed.
4372 */
4373 if (!tc->pmod->exts[u].parsed) {
4374 /* print at least the yang-data names */
4375 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4376 continue;
4377 }
4378
4379 ks = tro_modi_next_yang_data(tc, u);
4380 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4381 break;
4382 }
4383
4384 if (once) {
4385 ly_print_(pc->out, "\n");
4386 ly_print_(pc->out, "\n");
4387 once = 0;
4388 } else {
4389 ly_print_(pc->out, "\n");
4390 }
4391
4392 trm_print_section_as_family_tree(ks, pc, tc);
4393 }
aPiecekdc8fd572021-04-19 10:47:23 +02004394}
4395
4396/**
4397 * @brief Print sections module, augment, rpcs, notifications,
4398 * grouping, yang-data.
4399 * @param[in] pc contains mainly functions for printing.
4400 * @param[in,out] tc is the tree context.
4401 */
4402static void
4403trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4404{
4405 trm_print_module_section(pc, tc);
4406 trm_print_augmentations(pc, tc);
4407 trm_print_rpcs(pc, tc);
4408 trm_print_notifications(pc, tc);
4409 trm_print_groupings(pc, tc);
4410 trm_print_yang_data(pc, tc);
4411 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004412}
4413
aPiecek874ea4d2021-04-19 12:26:36 +02004414/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004415 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004416 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004417
4418LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004419tree_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 +01004420{
4421 struct trt_printer_ctx pc;
4422 struct trt_tree_ctx tc;
4423 struct ly_out *new_out;
4424 LY_ERR erc;
4425 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4426
aPiecekdc8fd572021-04-19 10:47:23 +02004427 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4428
aPiecek61d062b2020-11-02 11:05:09 +01004429 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4430 return erc;
4431 }
4432
4433 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004434 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
aPiecek7ed8d032022-10-10 12:32:27 +02004435 trm_lysc_tree_ctx(module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek3f247652021-04-19 13:40:25 +02004436 } else {
aPiecek7ed8d032022-10-10 12:32:27 +02004437 trm_lysp_tree_ctx(module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek3f247652021-04-19 13:40:25 +02004438 }
aPiecek61d062b2020-11-02 11:05:09 +01004439
4440 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004441 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004442
4443 ly_out_free(new_out, NULL, 1);
4444
aPiecekdc8fd572021-04-19 10:47:23 +02004445 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004446}
4447
4448LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004449tree_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 +01004450{
aPiecek153b00f2021-04-20 13:52:57 +02004451 struct trt_printer_ctx pc;
4452 struct trt_tree_ctx tc;
4453 struct ly_out *new_out;
4454 struct trt_wrapper wr;
4455 LY_ERR erc;
4456 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4457
4458 assert(out && node);
4459
4460 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4461 return LY_EINVAL;
4462 }
4463
4464 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4465 return erc;
4466 }
4467
4468 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek7ed8d032022-10-10 12:32:27 +02004469 trm_lysc_tree_ctx(node->module, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004470
4471 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
ekinzie0ab8b302022-10-10 03:03:57 -04004472 trb_print_parents(node, NULL, &pc, &tc);
aPiecek153b00f2021-04-20 13:52:57 +02004473
4474 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4475 tc.cn = lysc_node_child(node);
ekinzie0ab8b302022-10-10 03:03:57 -04004476 wr = trb_count_depth(NULL, tc.cn);
aPiecek153b00f2021-04-20 13:52:57 +02004477 trb_print_family_tree(wr, &pc, &tc);
4478 }
4479 ly_print_(out, "\n");
4480
4481 erc = clb_arg.last_error;
4482 ly_out_free(new_out, NULL, 1);
4483
4484 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004485}
4486
4487LY_ERR
Michal Vasko6a914fb2022-01-17 10:36:14 +01004488tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
4489 size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004490{
aPiecek9f792e52021-04-21 08:33:56 +02004491 struct trt_printer_ctx pc;
4492 struct trt_tree_ctx tc;
4493 struct ly_out *new_out;
4494 LY_ERR erc;
4495 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4496
4497 assert(submodp);
4498 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4499
4500 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4501 return erc;
4502 }
4503
4504 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek7ed8d032022-10-10 12:32:27 +02004505 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, 0, NULL, &pc, &tc);
aPiecek9f792e52021-04-21 08:33:56 +02004506 tc.pmod = (struct lysp_module *)submodp;
4507 tc.tpn = submodp->data;
4508 tc.pn = tc.tpn;
4509
4510 trm_print_sections(&pc, &tc);
4511 erc = clb_arg.last_error;
4512
4513 ly_out_free(new_out, NULL, 1);
4514
4515 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004516}
ekinzie0ab8b302022-10-10 03:03:57 -04004517
aPiecek7ed8d032022-10-10 12:32:27 +02004518/**
4519 * @brief Print all mounted nodes ('/') and parent-referenced nodes ('@').
4520 *
4521 * @param[in] ext is mount-point extension.
4522 * @param[in] wr is wrapper to be printed.
4523 * @param[in] pc contains mainly functions for printing.
4524 * @return LY_ERR value.
4525 */
ekinzie0ab8b302022-10-10 03:03:57 -04004526static LY_ERR
aPiecek7ed8d032022-10-10 12:32:27 +02004527trb_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 -04004528{
aPiecek7ed8d032022-10-10 12:32:27 +02004529 LY_ERR ret = LY_SUCCESS;
ekinzie0ab8b302022-10-10 03:03:57 -04004530 struct ly_ctx *ext_ctx = NULL;
aPiecek7ed8d032022-10-10 12:32:27 +02004531 const struct lys_module *mod, *last_mod;
ekinzie0ab8b302022-10-10 03:03:57 -04004532 struct trt_tree_ctx tmptc;
4533 struct trt_wrapper tmpwr;
4534 struct trt_printer_ctx tmppc;
ekinzie0ab8b302022-10-10 03:03:57 -04004535 struct ly_set *refs = NULL;
aPiecek7ed8d032022-10-10 12:32:27 +02004536 uint32_t i, iter_state;
aPiecek36f82232022-10-14 10:08:38 +02004537 ly_bool notif, rpc;
ekinzie0ab8b302022-10-10 03:03:57 -04004538
aPiecek7ed8d032022-10-10 12:32:27 +02004539 if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) {
ekinzie0ab8b302022-10-10 03:03:57 -04004540 /* Void mount point */
4541 return LY_SUCCESS;
4542 }
4543
aPiecek7ed8d032022-10-10 12:32:27 +02004544 lyplg_ext_schema_mount_get_parent_ref(ext, &refs);
4545
4546 /* Get the last mounted module which will be printed. */
4547 iter_state = 0;
4548 while ((mod = trb_mounted_module_iter(ext_ctx, refs, &iter_state))) {
4549 last_mod = mod;
ekinzie0ab8b302022-10-10 03:03:57 -04004550 }
4551
ekinzie0ab8b302022-10-10 03:03:57 -04004552 tmppc = *pc;
aPiecek7ed8d032022-10-10 12:32:27 +02004553 iter_state = 0;
4554 while ((mod = trb_mounted_module_iter(ext_ctx, refs, &iter_state))) {
4555 /* Prepare printer tree context. */
ekinzie0ab8b302022-10-10 03:03:57 -04004556 if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) {
aPiecek7ed8d032022-10-10 12:32:27 +02004557 trm_lysc_tree_ctx(mod, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
aPiecek36f82232022-10-14 10:08:38 +02004558 notif = tmptc.cmod->notifs ? 1 : 0;
4559 rpc = tmptc.cmod->rpcs ? 1 : 0;
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);
aPiecek36f82232022-10-14 10:08:38 +02004562 notif = tmptc.pmod->notifs ? 1 : 0;
4563 rpc = tmptc.pmod->rpcs ? 1 : 0;
ekinzie0ab8b302022-10-10 03:03:57 -04004564 }
aPiecek36f82232022-10-14 10:08:38 +02004565
aPiecek7ed8d032022-10-10 12:32:27 +02004566 /* Decide whether to print the symbol '|'. */
aPiecek36f82232022-10-14 10:08:38 +02004567 tmpwr = (mod == last_mod) && !rpc && !notif && !refs ? wr : trp_wrapper_set_mark_top(wr);
aPiecek7ed8d032022-10-10 12:32:27 +02004568 /* Print top-level nodes of mounted module which are denoted by the symbol '/'. */
4569 trb_print_family_tree(tmpwr, &tmppc, &tmptc);
aPiecek36f82232022-10-14 10:08:38 +02004570
4571 /* Print top-level rpc nodes. */
4572 if (rpc) {
4573 tro_modi_get_rpcs(&tmptc);
4574 tmpwr = (mod == last_mod) && !notif && !refs ? wr : trp_wrapper_set_mark_top(wr);
4575 trb_print_family_tree(tmpwr, &tmppc, &tmptc);
4576 }
4577
4578 /* Print top-level notification nodes. */
4579 if (notif) {
4580 tro_modi_get_notifications(&tmptc);
4581 tmpwr = (mod == last_mod) && !refs ? wr : trp_wrapper_set_mark_top(wr);
4582 trb_print_family_tree(tmpwr, &tmppc, &tmptc);
4583 }
ekinzie0ab8b302022-10-10 03:03:57 -04004584 }
4585
aPiecek7ed8d032022-10-10 12:32:27 +02004586 /* Print parent-referenced nodes which are denoted by the symbol '@'. */
4587 for (i = 0; refs && i < refs->count; i++) {
4588 trm_lysc_tree_ctx(refs->snodes[i]->module, pc->out, pc->max_line_length, 1, refs, &tmppc, &tmptc);
aPiecek42174592022-10-13 13:53:25 +02004589 tmpwr = ((i + 1) == refs->count) ? wr : trp_wrapper_set_mark_top(wr);
aPiecek7ed8d032022-10-10 12:32:27 +02004590 trb_print_parents(refs->snodes[i], &tmpwr, pc, &tmptc);
ekinzie0ab8b302022-10-10 03:03:57 -04004591 }
4592
ekinzie0ab8b302022-10-10 03:03:57 -04004593 ly_set_free(refs, NULL);
ekinzie0ab8b302022-10-10 03:03:57 -04004594 ly_ctx_destroy(ext_ctx);
aPiecek7ed8d032022-10-10 12:32:27 +02004595
4596 return ret;
ekinzie0ab8b302022-10-10 03:03:57 -04004597}