blob: 963dc4c8e08e9e2c91a3ab81467db9f42238b025 [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"
aPiecek704f8e92021-08-25 13:35:05 +020096#include "printer_schema.h"
aPiecek874ea4d2021-04-19 12:26:36 +020097#include "tree_schema_internal.h"
98#include "xpath.h"
99
aPiecek61d062b2020-11-02 11:05:09 +0100100/**
101 * @brief List of available actions.
102 */
103typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200104 TRD_PRINT = 0, /**< Normal behavior. It just prints. */
105 TRD_CHAR_COUNT /**< Characters will be counted instead of printing. */
aPiecek61d062b2020-11-02 11:05:09 +0100106} trt_ly_out_clb_arg_flag;
107
108/**
aPiecek874ea4d2021-04-19 12:26:36 +0200109 * @brief Structure is passed as 'writeclb' argument
aPiecek01598c02021-04-23 14:18:24 +0200110 * to the ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100111 */
112struct ly_out_clb_arg {
aPiecek874ea4d2021-04-19 12:26:36 +0200113 trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */
114 struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */
115 size_t counter; /**< Counter of printed characters. */
116 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 +0100117};
118
119/**
120 * @brief Initialize struct ly_out_clb_arg with default settings.
121 */
122#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
aPiecek874ea4d2021-04-19 12:26:36 +0200123 (struct ly_out_clb_arg) { \
124 .mode = MODE, .out = OUT, \
125 .counter = COUNTER, .last_error = LAST_ERROR \
126 }
aPiecek61d062b2020-11-02 11:05:09 +0100127
aPiecek874ea4d2021-04-19 12:26:36 +0200128/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100129 * Print getters
aPiecek874ea4d2021-04-19 12:26:36 +0200130 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100131
132/**
133 * @brief Callback functions that prints special cases.
134 *
135 * It just groups together tree context with trt_fp_print.
136 */
137struct trt_cf_print {
aPiecek874ea4d2021-04-19 12:26:36 +0200138 const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200139
aPiecek874ea4d2021-04-19 12:26:36 +0200140 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 +0100141};
142
143/**
144 * @brief Callback functions for printing special cases.
145 *
aPiecek874ea4d2021-04-19 12:26:36 +0200146 * Functions with the suffix 'trp' can print most of the text on
147 * output, just by setting the pointer to the string. But in some
148 * cases, it's not that simple, because its entire string is fragmented
149 * in memory. For example, for printing list's keys or if-features.
aPiecek61d062b2020-11-02 11:05:09 +0100150 * However, this depends on how the libyang library is implemented.
aPiecek3f247652021-04-19 13:40:25 +0200151 * This implementation of the printer_tree module goes through
152 * a lysp tree, but if it goes through a lysc tree, these special cases
153 * would be different.
aPiecek61d062b2020-11-02 11:05:09 +0100154 * Functions must print including spaces or delimiters between names.
155 */
156struct trt_fp_print {
157 void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */
158 void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */
159};
160
161/**
162 * @brief Package which only groups getter function.
163 */
164struct trt_pck_print {
165 const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */
166 struct trt_fp_print fps; /**< Print function. */
167};
168
169/**
170 * @brief Initialize struct trt_pck_print by parameters.
171 */
172#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
aPiecek874ea4d2021-04-19 12:26:36 +0200173 (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT}
aPiecek61d062b2020-11-02 11:05:09 +0100174
aPiecek874ea4d2021-04-19 12:26:36 +0200175/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100176 * Indent
aPiecek874ea4d2021-04-19 12:26:36 +0200177 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100178
179/**
aPiecek874ea4d2021-04-19 12:26:36 +0200180 * @brief Constants which are defined in the RFC or are observable
181 * from the pyang tool.
aPiecek61d062b2020-11-02 11:05:09 +0100182 */
183typedef enum {
184 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 +0100185 TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with
186 a whitespace offset of at least two characters. */
187 TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */
188 TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */
189 TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */
190 TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */
aPiecek61d062b2020-11-02 11:05:09 +0100191 TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */
192} trt_cnf_indent;
193
194/**
195 * @brief Type of indent in node.
196 */
197typedef enum {
198 TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100199 TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */
aPiecek61d062b2020-11-02 11:05:09 +0100200 TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
201} trt_indent_in_node_type;
202
203/** Constant to indicate the need to break a line. */
204#define TRD_LINEBREAK -1
205
206/**
aPiecek874ea4d2021-04-19 12:26:36 +0200207 * @brief Records the alignment between the individual
208 * elements of the node.
aPiecek61d062b2020-11-02 11:05:09 +0100209 *
aPiecek874ea4d2021-04-19 12:26:36 +0200210 * @see trp_default_indent_in_node, trp_try_normal_indent_in_node
aPiecek61d062b2020-11-02 11:05:09 +0100211 */
212struct trt_indent_in_node {
213 trt_indent_in_node_type type; /**< Type of indent in node. */
aPiecek874ea4d2021-04-19 12:26:36 +0200214 int16_t btw_name_opts; /**< Indent between node name and \<opts\>. */
215 int16_t btw_opts_type; /**< Indent between \<opts\> and \<type\>. */
aPiecek61d062b2020-11-02 11:05:09 +0100216 int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */
217};
218
219/**
220 * @brief Type of wrappers to be printed.
221 */
222typedef enum {
223 TRD_WRAPPER_TOP = 0, /**< Related to the module. */
224 TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */
225} trd_wrapper_type;
226
227/**
228 * @brief For resolving sibling symbol ('|') placement.
229 *
230 * Bit indicates where the sibling symbol must be printed.
aPiecek874ea4d2021-04-19 12:26:36 +0200231 * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS.
aPiecek61d062b2020-11-02 11:05:09 +0100232 *
aPiecek874ea4d2021-04-19 12:26:36 +0200233 * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY,
234 * trp_wrapper_set_mark, trp_wrapper_set_shift,
235 * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper
aPiecek61d062b2020-11-02 11:05:09 +0100236 */
237struct trt_wrapper {
238 trd_wrapper_type type; /**< Location of the wrapper. */
239 uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed.
240 It follows that the maximum immersion of the printable node is 64. */
241 uint32_t actual_pos; /**< Actual position in bit_marks. */
242};
243
244/**
245 * @brief Get wrapper related to the module section.
246 *
247 * @code
248 * module: <module-name>
249 * +--<node>
250 * |
251 * @endcode
252 */
253#define TRP_INIT_WRAPPER_TOP \
aPiecek874ea4d2021-04-19 12:26:36 +0200254 (struct trt_wrapper) { \
255 .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \
256 }
aPiecek61d062b2020-11-02 11:05:09 +0100257
258/**
aPiecek874ea4d2021-04-19 12:26:36 +0200259 * @brief Get wrapper related to subsection
260 * e.g. Augmenations or Groupings.
aPiecek61d062b2020-11-02 11:05:09 +0100261 *
262 * @code
263 * module: <module-name>
264 * +--<node>
265 *
266 * augment <target-node>:
267 * +--<node>
268 * @endcode
269 */
270#define TRP_INIT_WRAPPER_BODY \
aPiecek874ea4d2021-04-19 12:26:36 +0200271 (struct trt_wrapper) { \
272 .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \
273 }
aPiecek61d062b2020-11-02 11:05:09 +0100274
275/**
276 * @brief Package which only groups wrapper and indent in node.
277 */
278struct trt_pck_indent {
279 struct trt_wrapper wrapper; /**< Coded " | | " sequence. */
280 struct trt_indent_in_node in_node; /**< Indent in node. */
281};
282
283/**
284 * @brief Initialize struct trt_pck_indent by parameters.
285 */
286#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200287 (struct trt_pck_indent){ \
288 .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \
289 }
aPiecek61d062b2020-11-02 11:05:09 +0100290
aPiecek874ea4d2021-04-19 12:26:36 +0200291/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100292 * status
aPiecek874ea4d2021-04-19 12:26:36 +0200293 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100294
295/**
296 * @brief Status of the node.
297 *
aPiecek874ea4d2021-04-19 12:26:36 +0200298 * @see trp_print_status
aPiecek61d062b2020-11-02 11:05:09 +0100299 */
300typedef enum {
301 TRD_STATUS_TYPE_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100302 TRD_STATUS_TYPE_CURRENT, /**< ::LYS_STATUS_CURR */
303 TRD_STATUS_TYPE_DEPRECATED, /**< ::LYS_STATUS_DEPRC */
aPiecek874ea4d2021-04-19 12:26:36 +0200304 TRD_STATUS_TYPE_OBSOLETE /**< ::LYS_STATUS_OBSLT */
aPiecek61d062b2020-11-02 11:05:09 +0100305} trt_status_type;
306
aPiecek874ea4d2021-04-19 12:26:36 +0200307/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100308 * flags
aPiecek874ea4d2021-04-19 12:26:36 +0200309 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100310
311/**
312 * @brief Flag of the node.
313 *
aPiecek874ea4d2021-04-19 12:26:36 +0200314 * @see trp_print_flags, trp_get_flags_strlen
aPiecek61d062b2020-11-02 11:05:09 +0100315 */
316typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200317 TRD_FLAGS_TYPE_EMPTY = 0, /**< -- */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100318 TRD_FLAGS_TYPE_RW, /**< rw */
319 TRD_FLAGS_TYPE_RO, /**< ro */
320 TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */
321 TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */
322 TRD_FLAGS_TYPE_RPC, /**< -x */
323 TRD_FLAGS_TYPE_NOTIF, /**< -n */
aPiecek61d062b2020-11-02 11:05:09 +0100324 TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */
325} trt_flags_type;
326
aPiecek874ea4d2021-04-19 12:26:36 +0200327/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100328 * node_name and opts
aPiecek874ea4d2021-04-19 12:26:36 +0200329 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100330
331#define TRD_NODE_NAME_PREFIX_CHOICE "("
332#define TRD_NODE_NAME_PREFIX_CASE ":("
333#define TRD_NODE_NAME_TRIPLE_DOT "..."
334
335/**
336 * @brief Type of the node.
337 *
aPiecek874ea4d2021-04-19 12:26:36 +0200338 * Used mainly to complete the correct \<opts\> next to or
339 * around the \<name\>.
aPiecek61d062b2020-11-02 11:05:09 +0100340 */
341typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200342 TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100343 TRD_NODE_CASE, /**< For case node. :(\<name\>) */
344 TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */
345 TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */
346 TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */
347 TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */
348 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list (without keys). \<name\>* */
349 TRD_NODE_KEYS, /**< For a list's keys. \<name\>* [\<keys\>] */
350 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
351 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 +0200352 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 +0100353} trt_node_type;
354
355/**
356 * @brief Type of node and his name.
357 *
aPiecek874ea4d2021-04-19 12:26:36 +0200358 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
aPiecek61d062b2020-11-02 11:05:09 +0100359 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
360 */
361struct trt_node_name {
362 trt_node_type type; /**< Type of the node relevant for printing. */
aPiecek34fa3772021-05-21 12:35:46 +0200363 const char *module_prefix; /**< If the node is augmented into the tree from another module,
364 so this is the prefix of that module. */
aPiecek61d062b2020-11-02 11:05:09 +0100365 const char *str; /**< Name of the node. */
366};
367
368/**
369 * @brief Create struct trt_node_name as empty.
370 */
371#define TRP_EMPTY_NODE_NAME \
aPiecek874ea4d2021-04-19 12:26:36 +0200372 (struct trt_node_name) { \
373 .type = TRD_NODE_ELSE, .module_prefix = NULL, .str = NULL \
374 }
aPiecek61d062b2020-11-02 11:05:09 +0100375
376/**
377 * @brief Check if struct trt_node_name is empty.
378 */
379#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
380 !NODE_NAME.str
381
aPiecek874ea4d2021-04-19 12:26:36 +0200382/**
383 * @brief Every \<opts\> mark except string of list's keys
384 * has a length of one.
385 */
aPiecek61d062b2020-11-02 11:05:09 +0100386#define TRD_OPTS_MARK_LENGTH 1
387
aPiecek874ea4d2021-04-19 12:26:36 +0200388/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100389 * type
aPiecek874ea4d2021-04-19 12:26:36 +0200390 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100391
392/**
393 * @brief Type of the \<type\>
394 */
395typedef enum {
396 TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100397 TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
398 TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm.
aPiecek874ea4d2021-04-19 12:26:36 +0200399 So set type as ::TRD_TYPE_TARGET. */
aPiecek61d062b2020-11-02 11:05:09 +0100400 TRD_TYPE_EMPTY /**< Type is not used at all. */
401} trt_type_type;
402
403/**
404 * @brief \<type\> in the \<node\>.
405 *
aPiecek874ea4d2021-04-19 12:26:36 +0200406 * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
aPiecek61d062b2020-11-02 11:05:09 +0100407 */
408struct trt_type {
409 trt_type_type type; /**< Type of the \<type\>. */
410 const char *str; /**< Path or name of the type. */
411};
412
413/**
414 * @brief Create empty struct trt_type.
415 */
416#define TRP_EMPTY_TRT_TYPE \
417 (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}
418
419/**
420 * @brief Check if struct trt_type is empty.
421 */
422#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
423 TYPE_OF_TYPE.type == TRD_TYPE_EMPTY
424
425/**
426 * @brief Initialize struct trt_type by parameters.
427 */
428#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
429 (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}
430
aPiecek874ea4d2021-04-19 12:26:36 +0200431/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100432 * node
aPiecek874ea4d2021-04-19 12:26:36 +0200433 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100434
435/**
436 * @brief \<node\> data for printing.
437 *
aPiecek874ea4d2021-04-19 12:26:36 +0200438 * It contains RFC's:
439 * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
aPiecek61d062b2020-11-02 11:05:09 +0100440 * Item \<opts\> is moved to part struct trt_node_name.
aPiecek874ea4d2021-04-19 12:26:36 +0200441 * For printing [\<keys\>] and if-features is required special
442 * functions which prints them.
aPiecek61d062b2020-11-02 11:05:09 +0100443 *
aPiecek874ea4d2021-04-19 12:26:36 +0200444 * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty,
445 * trp_print_node_up_to_name, trp_print_divided_node_up_to_name,
446 * trp_print_node
aPiecek61d062b2020-11-02 11:05:09 +0100447 */
448struct trt_node {
aPiecek874ea4d2021-04-19 12:26:36 +0200449 trt_status_type status; /**< \<status\>. */
450 trt_flags_type flags; /**< \<flags\>. */
451 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
452 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
453 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and
454 will be printed by trt_fp_print.print_features_names callback. */
455 ly_bool last_one; /**< Information about whether the node is the last. */
aPiecek61d062b2020-11-02 11:05:09 +0100456};
457
458/**
459 * @brief Create struct trt_node as empty.
460 */
461#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200462 (struct trt_node) { \
463 .status = TRD_STATUS_TYPE_EMPTY, \
464 .flags = TRD_FLAGS_TYPE_EMPTY, \
465 .name = TRP_EMPTY_NODE_NAME, \
466 .type = TRP_EMPTY_TRT_TYPE, \
467 .iffeatures = 0, \
468 .last_one = 1 \
469 }
aPiecek61d062b2020-11-02 11:05:09 +0100470
471/**
472 * @brief Package which only groups indent and node.
473 */
474struct trt_pair_indent_node {
475 struct trt_indent_in_node indent;
476 struct trt_node node;
477};
478
479/**
480 * @brief Initialize struct trt_pair_indent_node by parameters.
481 */
482#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200483 (struct trt_pair_indent_node) { \
484 .indent = INDENT_IN_NODE, .node = NODE \
485 }
aPiecek61d062b2020-11-02 11:05:09 +0100486
aPiecek874ea4d2021-04-19 12:26:36 +0200487/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100488 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200489 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100490
491#define TRD_TOP_KEYWORD_MODULE "module"
492#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
493
494#define TRD_BODY_KEYWORD_AUGMENT "augment"
495#define TRD_BODY_KEYWORD_RPC "rpcs"
496#define TRD_BODY_KEYWORD_NOTIF "notifications"
497#define TRD_BODY_KEYWORD_GROUPING "grouping"
498#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
499
500/**
501 * @brief Type of the trt_keyword.
502 */
503typedef enum {
504 TRD_KEYWORD_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100505 TRD_KEYWORD_MODULE,
506 TRD_KEYWORD_SUBMODULE,
507 TRD_KEYWORD_AUGMENT,
508 TRD_KEYWORD_RPC,
509 TRD_KEYWORD_NOTIF,
510 TRD_KEYWORD_GROUPING,
aPiecek61d062b2020-11-02 11:05:09 +0100511 TRD_KEYWORD_YANG_DATA
512} trt_keyword_type;
513
514/**
515 * @brief Main sign of the tree nodes.
516 *
aPiecek874ea4d2021-04-19 12:26:36 +0200517 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100518 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
519 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
520 * trp_keyword_type_strlen
521 *
522 */
523struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200524 trt_keyword_type type; /**< String containing some of the top or body keyword. */
525 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100526};
527
528/**
529 * @brief Create struct trt_keyword_stmt as empty.
530 */
531#define TRP_EMPTY_KEYWORD_STMT \
532 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
533
534/**
535 * @brief Check if struct trt_keyword_stmt is empty.
536 */
537#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
538 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
539
540/**
541 * @brief Initialize struct trt_keyword_stmt by parameters.
542 */
543#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
544 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
545
aPiecek874ea4d2021-04-19 12:26:36 +0200546/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100547 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200548 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100549
550struct trt_parent_cache;
551
552/**
553 * @brief Functions that change the state of the tree_ctx structure.
554 *
aPiecek3f247652021-04-19 13:40:25 +0200555 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200556 * for the 'trp' printing functions and are also called from the
557 * 'trb' browsing functions when walking through a tree. These callback
558 * functions need to be checked or reformulated if changes to the
559 * libyang library affect the printing tree. For all, if the value
560 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
561 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100562 */
563struct trt_fp_modify_ctx {
564 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
565 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
566 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
567 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 +0100568};
569
aPiecek874ea4d2021-04-19 12:26:36 +0200570/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100571 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200572 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100573
574/**
575 * @brief Functions that do not change the state of the tree_structure.
576 *
577 * For details see trt_fp_modify_ctx.
578 */
579struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200580 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
581 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
582 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100583};
584
aPiecek874ea4d2021-04-19 12:26:36 +0200585/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100586 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200587 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100588
589/**
aPiecek874ea4d2021-04-19 12:26:36 +0200590 * @brief A set of all necessary functions that must be provided
591 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100592 */
593struct trt_fp_all {
594 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
595 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
596 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
597};
598
aPiecek874ea4d2021-04-19 12:26:36 +0200599/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100600 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200601 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100602
603/**
aPiecek01598c02021-04-23 14:18:24 +0200604 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100605 */
606struct trt_printer_ctx {
607 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200608 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100609 size_t max_line_length; /**< The maximum number of characters that can be
610 printed on one line, including the last. */
611};
612
aPiecek874ea4d2021-04-19 12:26:36 +0200613/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100614 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200615 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100616
617/**
618 * @brief The name of the section to which the node belongs.
619 */
620typedef enum {
621 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100622 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
623 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
624 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
625 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
aPiecek61d062b2020-11-02 11:05:09 +0100626 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
627} trt_actual_section;
628
629/**
630 * @brief Types of nodes that have some effect on their children.
631 */
632typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200633 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100634 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
635 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
aPiecek874ea4d2021-04-19 12:26:36 +0200636 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100637} trt_ancestor_type;
638
639/**
640 * @brief Saved information when browsing the tree downwards.
641 *
aPiecek874ea4d2021-04-19 12:26:36 +0200642 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200643 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200644 * this structures during their recursive calls. This functions do not
645 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200646 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200647 * structure, can return a node with the correct data. The word
648 * \b parent is in the structure name, because this data refers to
649 * the last parent and at the same time the states of its
650 * ancestors data. Only the function jumping on the child
651 * (next_child(...)) creates this structure, because the pointer
652 * to the current node moves down the tree. It's like passing
653 * the genetic code to children. Some data must be inherited and
654 * there are two approaches to this problem. Either it will always
655 * be determined which inheritance states belong to the current node
656 * (which can lead to regular travel to the root node) or
657 * the inheritance states will be stored during the recursive calls.
658 * So the problem was solved by the second option. Why does
659 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200660 * the lysp tree. For walks through the lysc tree is trt_parent_cache
661 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100662 *
aPiecek874ea4d2021-04-19 12:26:36 +0200663 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100664 */
665struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200666 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
667 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
668 uint16_t lys_config; /**< Inherited config W or R. */
669 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100670};
671
672/**
673 * @brief Return trt_parent_cache filled with default values.
674 */
675#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200676 (struct trt_parent_cache) { \
677 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
678 .lys_config = LYS_CONFIG_W, .last_list = NULL \
679 }
aPiecek61d062b2020-11-02 11:05:09 +0100680
681/**
682 * @brief Main structure for browsing the libyang tree
683 */
684struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200685 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
686 It is assumed that once set, it does not change.
687 If it is true then trt_tree_ctx.pn and
688 trt_tree_ctx.tpn are not used.
689 If it is false then trt_tree_ctx.cn is not used. */
690 trt_actual_section section; /**< To which section pn points. */
691 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
692 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
693 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
Michal Vasko26bbb272022-08-02 14:54:33 +0200694
aPiecek96baa7f2021-04-23 12:32:00 +0200695 union {
696 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
697 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
698 is set to TRD_SECT_YANG_DATA. */
699 };
700 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
aPiecek61d062b2020-11-02 11:05:09 +0100701};
702
aPiecek3f247652021-04-19 13:40:25 +0200703/**
aPiecekbbc02932021-05-21 07:19:41 +0200704 * @brief Check if lysp node is available from
705 * the current compiled node.
706 *
707 * Use only if trt_tree_ctx.lysc_tree is set to true.
708 */
709#define TRP_TREE_CTX_LYSP_NODE_PRESENT(CN) \
710 (CN->priv)
711
712/**
aPiecek3f247652021-04-19 13:40:25 +0200713 * @brief Get lysp_node from trt_tree_ctx.cn.
aPiecekbbc02932021-05-21 07:19:41 +0200714 *
715 * Use only if :TRP_TREE_CTX_LYSP_NODE_PRESENT returns true
716 * for that node.
aPiecek3f247652021-04-19 13:40:25 +0200717 */
718#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
719 ((const struct lysp_node *)CN->priv)
720
aPiecek01598c02021-04-23 14:18:24 +0200721/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100722typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
723
aPiecekef1e58e2021-04-19 13:19:44 +0200724/**
725 * @brief Simple getter functions for lysp and lysc nodes.
726 *
727 * This structure is useful if we have a general algorithm
728 * (tro function) that can be used for both lysc and lysp nodes.
729 * Thanks to this structure, we prevent code redundancy.
730 * We don't have to write basically the same algorithm twice
731 * for lysp and lysc trees.
732 */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100733struct tro_getters {
aPiecekef1e58e2021-04-19 13:19:44 +0200734 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
735 const void *(*next)(const void *); /**< Get sibling. */
736 const void *(*parent)(const void *); /**< Get parent. */
737 const void *(*child)(const void *); /**< Get child. */
738 const void *(*actions)(const void *); /**< Get actions. */
739 const void *(*action_input)(const void *); /**< Get input action from action node. */
740 const void *(*action_output)(const void *); /**< Get output action from action node. */
741 const void *(*notifs)(const void *); /**< Get notifs. */
742};
743
aPiecek874ea4d2021-04-19 12:26:36 +0200744/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100745 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200746 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100747
748/**
749 * @brief Print a substring but limited to the maximum length.
750 * @param[in] str is pointer to source.
751 * @param[in] len is number of characters to be printed.
752 * @param[in,out] out is output handler.
753 * @return str parameter shifted by len.
754 */
755static const char *
756trg_print_substr(const char *str, size_t len, struct ly_out *out)
757{
758 for (size_t i = 0; i < len; i++) {
759 ly_print_(out, "%c", str[0]);
760 str++;
761 }
762 return str;
763}
764
765/**
766 * @brief Pointer is not NULL and does not point to an empty string.
767 * @param[in] str is pointer to string to be checked.
768 * @return 1 if str pointing to non empty string otherwise 0.
769 */
770static ly_bool
771trg_charptr_has_data(const char *str)
772{
773 return (str) && (str[0] != '\0');
774}
775
776/**
aPiecek874ea4d2021-04-19 12:26:36 +0200777 * @brief Check if @p word in @p src is present where words are
778 * delimited by @p delim.
779 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100780 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200781 * @param[in] delim is delimiter between @p words in @p src.
782 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100783 */
784static ly_bool
785trg_word_is_present(const char *src, const char *word, char delim)
786{
787 const char *hit;
788
789 if ((!src) || (src[0] == '\0') || (!word)) {
790 return 0;
791 }
792
793 hit = strstr(src, word);
794
795 if (hit) {
796 /* word was founded at the begin of src
797 * OR it match somewhere after delim
798 */
799 if ((hit == src) || (hit[-1] == delim)) {
800 /* end of word was founded at the end of src
801 * OR end of word was match somewhere before delim
802 */
803 char delim_or_end = (hit + strlen(word))[0];
Michal Vasko26bbb272022-08-02 14:54:33 +0200804
aPiecek61d062b2020-11-02 11:05:09 +0100805 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
806 return 1;
807 }
808 }
809 /* after -> hit is just substr and it's not the whole word */
810 /* jump to the next word */
811 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
812 /* skip delim */
813 src = src[0] == '\0' ? src : src + 1;
814 /* continue with searching */
815 return trg_word_is_present(src, word, delim);
816 } else {
817 return 0;
818 }
819}
820
aPiecek874ea4d2021-04-19 12:26:36 +0200821/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100822 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200823 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100824
825/**
aPiecek01598c02021-04-23 14:18:24 +0200826 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100827 *
aPiecek874ea4d2021-04-19 12:26:36 +0200828 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100829 * @param[in] buf contains input characters
830 * @param[in] count is number of characters in buf.
831 * @return Number of printed bytes.
832 * @return Negative value in case of error.
833 */
834static ssize_t
835trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
836{
837 LY_ERR erc = LY_SUCCESS;
838 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
839
840 switch (data->mode) {
841 case TRD_PRINT:
842 erc = ly_write_(data->out, buf, count);
843 break;
844 case TRD_CHAR_COUNT:
845 data->counter = data->counter + count;
846 break;
847 default:
848 break;
849 }
850
851 if (erc != LY_SUCCESS) {
852 data->last_error = erc;
853 return -1;
854 } else {
855 return count;
856 }
857}
858
859/**
860 * @brief Check that indent in node can be considered as equivalent.
861 * @param[in] first is the first indent in node.
862 * @param[in] second is the second indent in node.
863 * @return 1 if indents are equivalent otherwise 0.
864 */
865static ly_bool
866trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
867{
868 const ly_bool a = first.type == second.type;
869 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
870 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
871 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
872
873 return a && b && c && d;
874}
875
876/**
aPiecek874ea4d2021-04-19 12:26:36 +0200877 * @brief Setting space character because node is last sibling.
878 * @param[in] wr is wrapper over which the shift operation
879 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100880 * @return New shifted wrapper.
881 */
882static struct trt_wrapper
883trp_wrapper_set_shift(struct trt_wrapper wr)
884{
885 assert(wr.actual_pos < 64);
886 /* +--<node>
887 * +--<node>
888 */
889 wr.actual_pos++;
890 return wr;
891}
892
893/**
aPiecek874ea4d2021-04-19 12:26:36 +0200894 * @brief Setting '|' symbol because node is divided or
895 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100896 * @param[in] wr is source of wrapper.
897 * @return New wrapper which is marked at actual position and shifted.
898 */
899static struct trt_wrapper
900trp_wrapper_set_mark(struct trt_wrapper wr)
901{
902 assert(wr.actual_pos < 64);
903 wr.bit_marks1 |= 1U << wr.actual_pos;
904 return trp_wrapper_set_shift(wr);
905}
906
907/**
908 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
909 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200910 * @param[in] last_one is flag. Value 1 saying if the node is the last
911 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100912 * @return New wrapper for the actual node.
913 */
914static struct trt_wrapper
915trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
916{
917 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
918}
919
920/**
921 * @brief Test if the wrappers are equivalent.
922 * @param[in] first is the first wrapper.
923 * @param[in] second is the second wrapper.
924 * @return 1 if the wrappers are equivalent otherwise 0.
925 */
926static ly_bool
927trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
928{
929 const ly_bool a = first.type == second.type;
930 const ly_bool b = first.bit_marks1 == second.bit_marks1;
931 const ly_bool c = first.actual_pos == second.actual_pos;
932
933 return a && b && c;
934}
935
936/**
937 * @brief Print " | " sequence on line.
938 * @param[in] wr is wrapper to be printed.
939 * @param[in,out] out is output handler.
940 */
941static void
942trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
943{
944 uint32_t lb;
945
946 if (wr.type == TRD_WRAPPER_TOP) {
947 lb = TRD_INDENT_LINE_BEGIN;
948 } else if (wr.type == TRD_WRAPPER_BODY) {
949 lb = TRD_INDENT_LINE_BEGIN * 2;
950 } else {
951 lb = TRD_INDENT_LINE_BEGIN;
952 }
953
954 ly_print_(out, "%*c", lb, ' ');
955
956 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
957 return;
958 }
959
960 for (uint32_t i = 0; i < wr.actual_pos; i++) {
961 /** Test if the bit on the index is set. */
962 if ((wr.bit_marks1 >> i) & 1U) {
963 ly_print_(out, "|");
964 } else {
965 ly_print_(out, " ");
966 }
967
968 if (i != wr.actual_pos) {
969 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
970 }
971 }
972}
973
974/**
975 * @brief Check if struct trt_node is empty.
976 * @param[in] node is item to test.
977 * @return 1 if node is considered empty otherwise 0.
978 */
979static ly_bool
980trp_node_is_empty(struct trt_node node)
981{
982 const ly_bool a = !node.iffeatures;
983 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
984 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
985 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
986 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
987
988 return a && b && c && d && e;
989}
990
991/**
aPiecek874ea4d2021-04-19 12:26:36 +0200992 * @brief Check if [\<keys\>], \<type\> and
993 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +0100994 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +0200995 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
996 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100997 */
998static ly_bool
999trp_node_body_is_empty(struct trt_node node)
1000{
1001 const ly_bool a = !node.iffeatures;
1002 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
1003 const ly_bool c = node.name.type != TRD_NODE_KEYS;
1004
1005 return a && b && c;
1006}
1007
1008/**
1009 * @brief Print \<status\> of the node.
1010 * @param[in] status_type is type of status.
1011 * @param[in,out] out is output handler.
1012 */
1013static void
1014trp_print_status(trt_status_type status_type, struct ly_out *out)
1015{
1016 switch (status_type) {
1017 case TRD_STATUS_TYPE_CURRENT:
1018 ly_print_(out, "%c", '+');
1019 break;
1020 case TRD_STATUS_TYPE_DEPRECATED:
1021 ly_print_(out, "%c", 'x');
1022 break;
1023 case TRD_STATUS_TYPE_OBSOLETE:
1024 ly_print_(out, "%c", 'o');
1025 break;
1026 default:
1027 break;
1028 }
1029}
1030
1031/**
1032 * @brief Print \<flags\>.
1033 * @param[in] flags_type is type of \<flags\>.
1034 * @param[in,out] out is output handler.
1035 */
1036static void
1037trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1038{
1039 switch (flags_type) {
1040 case TRD_FLAGS_TYPE_RW:
1041 ly_print_(out, "%s", "rw");
1042 break;
1043 case TRD_FLAGS_TYPE_RO:
1044 ly_print_(out, "%s", "ro");
1045 break;
1046 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1047 ly_print_(out, "%s", "-w");
1048 break;
1049 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1050 ly_print_(out, "%s", "-u");
1051 break;
1052 case TRD_FLAGS_TYPE_RPC:
1053 ly_print_(out, "%s", "-x");
1054 break;
1055 case TRD_FLAGS_TYPE_NOTIF:
1056 ly_print_(out, "%s", "-n");
1057 break;
1058 case TRD_FLAGS_TYPE_MOUNT_POINT:
1059 ly_print_(out, "%s", "mp");
1060 break;
1061 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001062 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001063 break;
1064 }
1065}
1066
1067/**
1068 * @brief Get size of the \<flags\>.
1069 * @param[in] flags_type is type of \<flags\>.
1070 * @return 0 if flags_type is not set otherwise 2.
1071 */
1072static size_t
1073trp_get_flags_strlen(trt_flags_type flags_type)
1074{
1075 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1076}
1077
1078/**
1079 * @brief Print entire struct trt_node_name structure.
1080 * @param[in] node_name is item to print.
1081 * @param[in,out] out is output handler.
1082 */
1083static void
1084trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1085{
1086 const char *mod_prefix;
1087 const char *colon;
1088 const char trd_node_name_suffix_choice[] = ")";
1089 const char trd_node_name_suffix_case[] = ")";
1090 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1091 const char trd_opts_container[] = "!"; /**< For a presence container. */
1092 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1093 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1094 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1095
1096 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1097 return;
1098 }
1099
1100 if (node_name.module_prefix) {
1101 mod_prefix = node_name.module_prefix;
1102 colon = ":";
1103 } else {
1104 mod_prefix = "";
1105 colon = "";
1106 }
1107
1108 switch (node_name.type) {
1109 case TRD_NODE_ELSE:
1110 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1111 break;
1112 case TRD_NODE_CASE:
1113 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1114 break;
1115 case TRD_NODE_CHOICE:
1116 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1117 break;
1118 case TRD_NODE_OPTIONAL_CHOICE:
1119 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);
1120 break;
1121 case TRD_NODE_OPTIONAL:
1122 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1123 break;
1124 case TRD_NODE_CONTAINER:
1125 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1126 break;
1127 case TRD_NODE_LISTLEAFLIST:
1128 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1129 break;
1130 case TRD_NODE_KEYS:
1131 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1132 break;
1133 case TRD_NODE_TOP_LEVEL1:
1134 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1135 break;
1136 case TRD_NODE_TOP_LEVEL2:
1137 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1138 break;
1139 case TRD_NODE_TRIPLE_DOT:
1140 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1141 break;
1142 default:
1143 break;
1144 }
1145}
1146
1147/**
aPiecek874ea4d2021-04-19 12:26:36 +02001148 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1149 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001150 * @param[in] node_name is structure containing the 'mark'.
1151 * @return 1 if contain otherwise 0.
1152 */
1153static ly_bool
1154trp_mark_is_used(struct trt_node_name node_name)
1155{
1156 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1157 return 0;
1158 }
1159
1160 switch (node_name.type) {
1161 case TRD_NODE_ELSE:
1162 case TRD_NODE_CASE:
1163 case TRD_NODE_KEYS:
1164 return 0;
1165 default:
1166 return 1;
1167 }
1168}
1169
1170/**
1171 * @brief Print opts keys.
1172 * @param[in] node_name contains type of the node with his name.
1173 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001174 * @param[in] cf is basically a pointer to the function that prints
1175 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001176 * @param[in,out] out is output handler.
1177 */
1178static void
1179trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1180{
1181 if (node_name.type != TRD_NODE_KEYS) {
1182 return;
1183 }
1184
1185 /* <name><mark>___<keys>*/
1186 if (btw_name_opts > 0) {
1187 ly_print_(out, "%*c", btw_name_opts, ' ');
1188 }
1189 ly_print_(out, "[");
1190 cf.pf(cf.ctx, out);
1191 ly_print_(out, "]");
1192}
1193
1194/**
1195 * @brief Print entire struct trt_type structure.
1196 * @param[in] type is item to print.
1197 * @param[in,out] out is output handler.
1198 */
1199static void
1200trp_print_type(struct trt_type type, struct ly_out *out)
1201{
1202 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1203 return;
1204 }
1205
1206 switch (type.type) {
1207 case TRD_TYPE_NAME:
1208 ly_print_(out, "%s", type.str);
1209 break;
1210 case TRD_TYPE_TARGET:
1211 ly_print_(out, "-> %s", type.str);
1212 break;
1213 case TRD_TYPE_LEAFREF:
1214 ly_print_(out, "leafref");
1215 default:
1216 break;
1217 }
1218}
1219
1220/**
1221 * @brief Print all iffeatures of node
1222 *
1223 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001224 * @param[in] cf is basically a pointer to the function that prints
1225 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001226 * @param[in,out] out is output handler.
1227 */
1228static void
1229trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1230{
1231 if (iffeature_flag) {
1232 ly_print_(out, "{");
1233 cf.pf(cf.ctx, out);
1234 ly_print_(out, "}?");
1235 }
1236}
1237
1238/**
1239 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1240 * @param[in] node contains items to print.
1241 * @param[in] out is output handler.
1242 */
1243static void
1244trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1245{
1246 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1247 trp_print_node_name(node.name, out);
1248 return;
1249 }
1250 /* <status>--<flags> */
1251 trp_print_status(node.status, out);
1252 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001253 /* If the node is a case node, there is no space before the <name>
1254 * also case node has no flags.
1255 */
aPiecek61d062b2020-11-02 11:05:09 +01001256 if (node.name.type != TRD_NODE_CASE) {
1257 trp_print_flags(node.flags, out);
1258 ly_print_(out, " ");
1259 }
1260 /* <name> */
1261 trp_print_node_name(node.name, out);
1262}
1263
1264/**
aPiecek874ea4d2021-04-19 12:26:36 +02001265 * @brief Print alignment (spaces) instead of
1266 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001267 * @param[in] node contains items to print.
1268 * @param[in] out is output handler.
1269 */
1270static void
1271trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1272{
1273 uint32_t space = trp_get_flags_strlen(node.flags);
1274
1275 if (node.name.type == TRD_NODE_CASE) {
1276 /* :(<name> */
1277 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1278 } else if (node.name.type == TRD_NODE_CHOICE) {
1279 /* (<name> */
1280 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1281 } else {
1282 /* _<name> */
1283 space += strlen(" ");
1284 }
1285
1286 /* <name>
1287 * __
1288 */
1289 space += TRD_INDENT_LONG_LINE_BREAK;
1290
1291 ly_print_(out, "%*c", space, ' ');
1292}
1293
1294/**
1295 * @brief Print struct trt_node structure.
1296 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001297 * @param[in] pck package of functions for
1298 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001299 * @param[in] indent is the indent in node.
1300 * @param[in,out] out is output handler.
1301 */
1302static void
1303trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1304{
1305 ly_bool triple_dot;
1306 ly_bool divided;
1307 struct trt_cf_print cf_print_keys;
1308 struct trt_cf_print cf_print_iffeatures;
1309
1310 if (trp_node_is_empty(node)) {
1311 return;
1312 }
1313
1314 /* <status>--<flags> <name><opts> <type> <if-features> */
1315 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1316 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1317
1318 if (triple_dot) {
1319 trp_print_node_name(node.name, out);
1320 return;
1321 } else if (!divided) {
1322 trp_print_node_up_to_name(node, out);
1323 } else {
1324 trp_print_divided_node_up_to_name(node, out);
1325 }
1326
1327 /* <opts> */
1328 /* <name>___<opts>*/
1329 cf_print_keys.ctx = pck.tree_ctx;
1330 cf_print_keys.pf = pck.fps.print_keys;
1331
1332 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1333
1334 /* <opts>__<type> */
1335 if (indent.btw_opts_type > 0) {
1336 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1337 }
1338
1339 /* <type> */
1340 trp_print_type(node.type, out);
1341
1342 /* <type>__<iffeatures> */
1343 if (indent.btw_type_iffeatures > 0) {
1344 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1345 }
1346
1347 /* <iffeatures> */
1348 cf_print_iffeatures.ctx = pck.tree_ctx;
1349 cf_print_iffeatures.pf = pck.fps.print_features_names;
1350
1351 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1352}
1353
1354/**
aPiecek874ea4d2021-04-19 12:26:36 +02001355 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001356 * @param[in] ks is keyword statement to print.
1357 * @param[in,out] out is output handler
1358 */
1359static void
1360trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1361{
1362 switch (ks.type) {
1363 case TRD_KEYWORD_MODULE:
1364 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1365 return;
1366 case TRD_KEYWORD_SUBMODULE:
1367 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1368 return;
1369 default:
1370 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1371 switch (ks.type) {
1372 case TRD_KEYWORD_AUGMENT:
1373 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1374 break;
1375 case TRD_KEYWORD_RPC:
1376 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1377 break;
1378 case TRD_KEYWORD_NOTIF:
1379 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1380 break;
1381 case TRD_KEYWORD_GROUPING:
1382 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1383 break;
1384 case TRD_KEYWORD_YANG_DATA:
1385 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1386 break;
1387 default:
1388 break;
1389 }
1390 break;
1391 }
1392}
1393
1394/**
1395 * @brief Get string length of stored keyword.
1396 * @param[in] type is type of the keyword statement.
1397 * @return length of the keyword statement name.
1398 */
1399static size_t
1400trp_keyword_type_strlen(trt_keyword_type type)
1401{
1402 switch (type) {
1403 case TRD_KEYWORD_MODULE:
1404 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1405 case TRD_KEYWORD_SUBMODULE:
1406 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1407 case TRD_KEYWORD_AUGMENT:
1408 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1409 case TRD_KEYWORD_RPC:
1410 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1411 case TRD_KEYWORD_NOTIF:
1412 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1413 case TRD_KEYWORD_GROUPING:
1414 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1415 case TRD_KEYWORD_YANG_DATA:
1416 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1417 default:
1418 return 0;
1419 }
1420}
1421
1422/**
aPiecek874ea4d2021-04-19 12:26:36 +02001423 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001424 * @param[in] ks is keyword statement structure.
1425 * @param[in] mll is max line length.
1426 * @param[in,out] out is output handler.
1427 */
1428static void
1429trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1430{
1431 uint32_t ind_initial;
1432 uint32_t ind_divided;
1433 /* flag if path must be splitted to more lines */
1434 ly_bool linebreak_was_set;
1435 /* flag if at least one subpath was printed */
1436 ly_bool subpath_printed;
1437 /* the sum of the sizes of the substrings on the current line */
1438 uint32_t how_far;
1439 /* pointer to start of the subpath */
1440 const char *sub_ptr;
1441 /* size of subpath from sub_ptr */
1442 size_t sub_len;
1443
1444 if ((!ks.str) || (ks.str[0] == '\0')) {
1445 return;
1446 }
1447
1448 /* module name cannot be splitted */
1449 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1450 ly_print_(out, "%s", ks.str);
1451 return;
1452 }
1453
1454 /* after -> for trd_keyword_stmt_body do */
1455
1456 /* set begin indentation */
1457 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1458 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1459 linebreak_was_set = 0;
1460 subpath_printed = 0;
1461 how_far = 0;
1462 sub_ptr = ks.str;
1463 sub_len = 0;
1464
1465 while (sub_ptr[0] != '\0') {
1466 uint32_t ind;
1467 /* skip slash */
1468 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
Michal Vasko26bbb272022-08-02 14:54:33 +02001469
aPiecek61d062b2020-11-02 11:05:09 +01001470 /* get position of the end of substr */
1471 tmp = strchr(tmp, '/');
1472 /* set correct size if this is a last substring */
1473 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1474 /* actualize sum of the substring's sizes on the current line */
1475 how_far += sub_len;
1476 /* correction due to colon character if it this is last substring */
1477 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1478 /* choose indentation which depends on
1479 * whether the string is printed on multiple lines or not
1480 */
1481 ind = linebreak_was_set ? ind_divided : ind_initial;
1482 if (ind + how_far <= mll) {
1483 /* printing before max line length */
1484 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1485 subpath_printed = 1;
1486 } else {
1487 /* printing on new line */
1488 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001489 /* first subpath is too long
1490 * but print it at first line anyway
1491 */
aPiecek61d062b2020-11-02 11:05:09 +01001492 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1493 subpath_printed = 1;
1494 continue;
1495 }
1496 ly_print_(out, "\n");
1497 ly_print_(out, "%*c", ind_divided, ' ');
1498 linebreak_was_set = 1;
1499 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1500 how_far = sub_len;
1501 subpath_printed = 1;
1502 }
1503 }
1504}
1505
1506/**
aPiecek874ea4d2021-04-19 12:26:36 +02001507 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001508 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001509 * @param[in] grp_has_data is flag only for grouping section.
1510 * Set to 1 if grouping section has some nodes.
1511 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001512 * @param[in,out] out is output handler.
1513 */
1514static void
aPiecekdc8fd572021-04-19 10:47:23 +02001515trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001516{
1517 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001518 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1519 return;
1520 } else {
1521 ly_print_(out, ":");
1522 }
aPiecek61d062b2020-11-02 11:05:09 +01001523 }
1524}
1525
1526/**
1527 * @brief Print entire struct trt_keyword_stmt structure.
1528 * @param[in] ks is item to print.
1529 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001530 * @param[in] grp_has_data is flag only for grouping section.
1531 * Set to 1 if grouping section has some nodes.
1532 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001533 * @param[in,out] out is output handler.
1534 */
1535static void
aPiecek874ea4d2021-04-19 12:26:36 +02001536trp_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 +01001537{
1538 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1539 return;
1540 }
1541 trt_print_keyword_stmt_begin(ks, out);
1542 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001543 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001544}
1545
aPiecek874ea4d2021-04-19 12:26:36 +02001546/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001547 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001548 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001549
1550/**
aPiecek874ea4d2021-04-19 12:26:36 +02001551 * @brief Printing one line including wrapper and node
1552 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001553 * @param[in] node is \<node\> representation.
1554 * @param[in] pck contains special printing functions callback.
1555 * @param[in] indent contains wrapper and indent in node numbers.
1556 * @param[in,out] out is output handler.
1557 */
1558static void
1559trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1560{
1561 trp_print_wrapper(indent.wrapper, out);
1562 trp_print_node(node, pck, indent.in_node, out);
1563}
1564
1565/**
aPiecek874ea4d2021-04-19 12:26:36 +02001566 * @brief Printing one line including wrapper and
1567 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001568 * @param[in] node is \<node\> representation.
1569 * @param[in] wr is wrapper for printing indentation before node.
1570 * @param[in] out is output handler.
1571 */
1572static void
1573trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1574{
1575 trp_print_wrapper(wr, out);
1576 trp_print_node_up_to_name(node, out);
1577}
1578
1579/**
aPiecek874ea4d2021-04-19 12:26:36 +02001580 * @brief Check if leafref target must be change to string 'leafref'
1581 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001582 * @param[in] node containing leafref target.
1583 * @param[in] wr is wrapper for printing indentation before node.
1584 * @param[in] mll is max line length.
1585 * @param[in] out is output handler.
1586 * @return true if leafref must be changed to string 'leafref'.
1587 */
1588static ly_bool
1589trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1590{
1591 struct ly_out_clb_arg *data;
1592
1593 if (node.type.type != TRD_TYPE_TARGET) {
1594 return 0;
1595 }
1596
1597 /* set ly_out to counting characters */
1598 data = out->method.clb.arg;
1599
1600 data->counter = 0;
1601 data->mode = TRD_CHAR_COUNT;
1602 /* count number of printed bytes */
1603 trp_print_wrapper(wr, out);
1604 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1605 trp_print_divided_node_up_to_name(node, out);
1606 data->mode = TRD_PRINT;
1607
1608 return data->counter + strlen(node.type.str) > mll;
1609}
1610
1611/**
1612 * @brief Get default indent in node based on node values.
1613 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001614 * @return Default indent in node assuming that the node
1615 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001616 */
1617static struct trt_indent_in_node
1618trp_default_indent_in_node(struct trt_node node)
1619{
1620 struct trt_indent_in_node ret;
1621
1622 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1623
1624 /* btw_name_opts */
1625 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1626
1627 /* btw_opts_type */
1628 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1629 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1630 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1631 TRD_INDENT_BEFORE_TYPE;
1632 } else {
1633 ret.btw_opts_type = 0;
1634 }
1635
1636 /* btw_type_iffeatures */
1637 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1638
1639 return ret;
1640}
1641
1642/**
1643 * @brief Setting linebreaks in trt_indent_in_node.
1644 *
1645 * The order where the linebreak tag can be placed is from the end.
1646 *
aPiecek874ea4d2021-04-19 12:26:36 +02001647 * @param[in] indent containing alignment lengths
1648 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001649 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001650 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1651 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001652 */
1653static struct trt_indent_in_node
1654trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1655{
1656 /* somewhere must be set a line break in node */
1657 struct trt_indent_in_node ret = indent;
1658
1659 /* gradually break the node from the end */
1660 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1661 ret.btw_type_iffeatures = TRD_LINEBREAK;
1662 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1663 ret.btw_opts_type = TRD_LINEBREAK;
1664 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1665 /* set line break between name and opts */
1666 ret.btw_name_opts = TRD_LINEBREAK;
1667 } else {
1668 /* it is not possible to place a more line breaks,
1669 * unfortunately the max_line_length constraint is violated
1670 */
1671 ret.type = TRD_INDENT_IN_NODE_FAILED;
1672 }
1673 return ret;
1674}
1675
1676/**
1677 * @brief Get the first half of the node based on the linebreak mark.
1678 *
1679 * Items in the second half of the node will be empty.
1680 *
1681 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001682 * @param[in] indent contains information in which part of the \<node\>
1683 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001684 * @return first half of the node, indent is unchanged.
1685 */
1686static struct trt_pair_indent_node
1687trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1688{
1689 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1690
1691 if (indent.btw_name_opts == TRD_LINEBREAK) {
1692 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1693 ret.node.type = TRP_EMPTY_TRT_TYPE;
1694 ret.node.iffeatures = 0;
1695 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1696 ret.node.type = TRP_EMPTY_TRT_TYPE;
1697 ret.node.iffeatures = 0;
1698 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1699 ret.node.iffeatures = 0;
1700 }
1701
1702 return ret;
1703}
1704
1705/**
1706 * @brief Get the second half of the node based on the linebreak mark.
1707 *
1708 * Items in the first half of the node will be empty.
1709 * Indentations belonging to the first node will be reset to zero.
1710 *
1711 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001712 * @param[in] indent contains information in which part of the \<node\>
1713 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001714 * @return second half of the node, indent is newly set.
1715 */
1716static struct trt_pair_indent_node
1717trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1718{
1719 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1720
1721 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001722 /* Logically, the information up to token <opts> should
1723 * be deleted, but the the trp_print_node function needs it to
1724 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001725 */
1726 ret.indent.btw_name_opts = 0;
1727 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1728 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1729 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1730 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1731 ret.indent.btw_name_opts = 0;
1732 ret.indent.btw_opts_type = 0;
1733 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1734 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1735 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1736 ret.node.type = TRP_EMPTY_TRT_TYPE;
1737 ret.indent.btw_name_opts = 0;
1738 ret.indent.btw_opts_type = 0;
1739 ret.indent.btw_type_iffeatures = 0;
1740 }
1741 return ret;
1742}
1743
1744/**
1745 * @brief Get the correct alignment for the node.
1746 *
aPiecek874ea4d2021-04-19 12:26:36 +02001747 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001748 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001749 *
1750 * @param[in] node is \<node\> representation.
1751 * @param[in] pck contains speciall callback functions for printing.
1752 * @param[in] indent contains wrapper and indent in node numbers.
1753 * @param[in] mll is max line length.
1754 * @param[in,out] cnt counting number of characters to print.
1755 * @param[in,out] out is output handler.
1756 * @return pair of node and indentation numbers of that node.
1757 */
1758static struct trt_pair_indent_node
1759trp_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)
1760{
1761 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1762
1763 trp_print_line(node, pck, indent, out);
1764
1765 if (*cnt <= mll) {
1766 /* success */
1767 return ret;
1768 } else {
1769 ret.indent = trp_indent_in_node_place_break(ret.indent);
1770 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1771 /* erase information in node due to line break */
1772 ret = trp_first_half_node(node, ret.indent);
1773 /* check if line fits, recursive call */
1774 *cnt = 0;
1775 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1776 /* make sure that the result will be with the status divided
1777 * or eventually with status failed */
1778 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1779 }
1780 return ret;
1781 }
1782}
1783
1784/**
1785 * @brief Get the correct alignment for the node.
1786 *
1787 * @param[in] node is \<node\> representation.
1788 * @param[in] pck contains speciall callback functions for printing.
1789 * @param[in] indent contains wrapper and indent in node numbers.
1790 * @param[in] mll is max line length.
1791 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001792 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1793 * line, some indent variable has negative value as a line break sign.
1794 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1795 * all indent variables values has non-negative number.
1796 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1797 * line, all indent variables has negative or zero values,
1798 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001799 */
1800static struct trt_pair_indent_node
1801trp_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)
1802{
1803 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1804 struct ly_out_clb_arg *data;
1805
1806 /* set ly_out to counting characters */
1807 data = out->method.clb.arg;
1808
1809 data->counter = 0;
1810 data->mode = TRD_CHAR_COUNT;
1811 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1812 data->mode = TRD_PRINT;
1813
1814 return ret;
1815}
1816
1817/**
aPiecek01598c02021-04-23 14:18:24 +02001818 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001819 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001820 * @param[in] node is node representation.
1821 * @param[in] ppck contains speciall callback functions for printing.
1822 * @param[in] ipck contains wrapper and indent in node numbers.
1823 * @param[in] mll is max line length.
1824 * @param[in,out] out is output handler.
1825 */
1826static void
1827trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1828{
1829 ly_bool entire_node_was_printed;
1830 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1831
1832 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1833 /* nothing can be done, continue as usual */
1834 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1835 }
1836
1837 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1838 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1839
1840 if (!entire_node_was_printed) {
1841 ly_print_(out, "\n");
1842 /* continue with second half node */
1843 ind_node = trp_second_half_node(node, ind_node.indent);
1844 /* continue with printing node */
1845 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1846 } else {
1847 return;
1848 }
1849}
1850
1851/**
aPiecek874ea4d2021-04-19 12:26:36 +02001852 * @brief Printing of the wrapper and the whole node,
1853 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001854 * @param[in] node is node representation.
1855 * @param[in] ppck contains speciall callback functions for printing.
1856 * @param[in] ipck contains wrapper and indent in node numbers.
1857 * @param[in] mll is max line length.
1858 * @param[in,out] out is output handler.
1859 */
1860static void
1861trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1862{
1863 struct trt_pair_indent_node ind_node1;
1864 struct trt_pair_indent_node ind_node2;
1865 struct trt_pck_indent tmp;
1866
1867 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1868 node.type.type = TRD_TYPE_LEAFREF;
1869 }
1870
1871 /* check if normal indent is possible */
1872 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1873
1874 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1875 /* node fits to one line */
1876 trp_print_line(node, ppck, ipck, out);
1877 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1878 /* node will be divided */
1879 /* print first half */
1880 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1881 /* pretend that this is normal node */
1882 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1883
1884 trp_print_line(ind_node1.node, ppck, tmp, out);
1885 ly_print_(out, "\n");
1886
1887 /* continue with second half on new line */
1888 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1889 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1890
1891 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1892 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1893 /* node name is too long */
1894 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1895
1896 if (trp_node_body_is_empty(node)) {
1897 return;
1898 } else {
1899 ly_print_(out, "\n");
1900
1901 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1902 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1903 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1904
1905 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1906 }
1907
1908 }
1909}
1910
aPiecek874ea4d2021-04-19 12:26:36 +02001911/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001912 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001913 *********************************************************************/
1914
1915/**
1916 * @brief Get nodetype.
1917 * @param[in] node is any lysp_node.
1918 */
1919static uint16_t
1920trop_nodetype(const void *node)
1921{
1922 return ((const struct lysp_node *)node)->nodetype;
1923}
1924
1925/**
1926 * @brief Get sibling.
1927 * @param[in] node is any lysp_node.
1928 */
1929static const void *
1930trop_next(const void *node)
1931{
1932 return ((const struct lysp_node *)node)->next;
1933}
1934
1935/**
1936 * @brief Get parent.
1937 * @param[in] node is any lysp_node.
1938 */
1939static const void *
1940trop_parent(const void *node)
1941{
1942 return ((const struct lysp_node *)node)->parent;
1943}
1944
1945/**
1946 * @brief Try to get child.
1947 * @param[in] node is any lysp_node.
1948 */
1949static const void *
1950trop_child(const void *node)
1951{
1952 return lysp_node_child(node);
1953}
1954
1955/**
1956 * @brief Try to get action.
1957 * @param[in] node is any lysp_node.
1958 */
1959static const void *
1960trop_actions(const void *node)
1961{
1962 return lysp_node_actions(node);
1963}
1964
1965/**
1966 * @brief Try to get action.
1967 * @param[in] node must be of type lysp_node_action.
1968 */
1969static const void *
1970trop_action_input(const void *node)
1971{
1972 return &((const struct lysp_node_action *)node)->input;
1973}
1974
1975/**
1976 * @brief Try to get action.
1977 * @param[in] node must be of type lysp_node_action.
1978 */
1979static const void *
1980trop_action_output(const void *node)
1981{
1982 return &((const struct lysp_node_action *)node)->output;
1983}
1984
1985/**
1986 * @brief Try to get action.
1987 * @param[in] node is any lysp_node.
1988 */
1989static const void *
1990trop_notifs(const void *node)
1991{
1992 return lysp_node_notifs(node);
1993}
1994
1995/**
aPiecek01598c02021-04-23 14:18:24 +02001996 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02001997 * which are adapted to lysp nodes.
1998 */
1999static struct tro_getters
2000trop_init_getters()
2001{
2002 return (struct tro_getters) {
2003 .nodetype = trop_nodetype,
2004 .next = trop_next,
2005 .parent = trop_parent,
2006 .child = trop_child,
2007 .actions = trop_actions,
2008 .action_input = trop_action_input,
2009 .action_output = trop_action_output,
2010 .notifs = trop_notifs
2011 };
2012}
2013
aPiecek3f247652021-04-19 13:40:25 +02002014/**
2015 * @brief Get nodetype.
2016 * @param[in] node is any lysc_node.
2017 */
2018static uint16_t
2019troc_nodetype(const void *node)
2020{
2021 return ((const struct lysc_node *)node)->nodetype;
2022}
2023
2024/**
2025 * @brief Get sibling.
2026 * @param[in] node is any lysc_node.
2027 */
2028static const void *
2029troc_next(const void *node)
2030{
2031 return ((const struct lysc_node *)node)->next;
2032}
2033
2034/**
2035 * @brief Get parent.
2036 * @param[in] node is any lysc_node.
2037 */
2038static const void *
2039troc_parent(const void *node)
2040{
2041 return ((const struct lysc_node *)node)->parent;
2042}
2043
2044/**
2045 * @brief Try to get child.
2046 * @param[in] node is any lysc_node.
2047 */
2048static const void *
2049troc_child(const void *node)
2050{
2051 return lysc_node_child(node);
2052}
2053
2054/**
2055 * @brief Try to get action.
2056 * @param[in] node is any lysc_node.
2057 */
2058static const void *
2059troc_actions(const void *node)
2060{
2061 return lysc_node_actions(node);
2062}
2063
2064/**
2065 * @brief Try to get action.
2066 * @param[in] node must be of type lysc_node_action.
2067 */
2068static const void *
2069troc_action_input(const void *node)
2070{
2071 return &((const struct lysc_node_action *)node)->input;
2072}
2073
2074/**
2075 * @brief Try to get action.
2076 * @param[in] node must be of type lysc_node_action.
2077 */
2078static const void *
2079troc_action_output(const void *node)
2080{
2081 return &((const struct lysc_node_action *)node)->output;
2082}
2083
2084/**
2085 * @brief Try to get action.
2086 * @param[in] node is any lysc_node.
2087 */
2088static const void *
2089troc_notifs(const void *node)
2090{
2091 return lysc_node_notifs(node);
2092}
2093
2094/**
aPiecek01598c02021-04-23 14:18:24 +02002095 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002096 * which are adapted to lysc nodes.
2097 */
2098static struct tro_getters
2099troc_init_getters()
2100{
2101 return (struct tro_getters) {
2102 .nodetype = troc_nodetype,
2103 .next = troc_next,
2104 .parent = troc_parent,
2105 .child = troc_child,
2106 .actions = troc_actions,
2107 .action_input = troc_action_input,
2108 .action_output = troc_action_output,
2109 .notifs = troc_notifs
2110 };
2111}
2112
aPiecekef1e58e2021-04-19 13:19:44 +02002113/**********************************************************************
2114 * tro functions
2115 *********************************************************************/
2116
2117/**
2118 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002119 *
2120 * This is a general algorithm that is able to
2121 * work with lysp_node or lysc_node.
2122 *
2123 * @param[in] node points to lysp_node or lysc_node.
2124 * @param[in] lysc_tree flag to determine what type the @p node is.
2125 * If set to true, then @p points to lysc_node otherwise lysp_node.
2126 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002127 */
2128static const void *
aPiecek3f247652021-04-19 13:40:25 +02002129tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002130{
2131 struct tro_getters get;
2132 const void *tmp, *parent;
2133 const void *ret;
2134
2135 assert(node);
2136
aPiecek3f247652021-04-19 13:40:25 +02002137 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002138
2139 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2140 if ((tmp = get.next(node))) {
2141 /* next action exists */
2142 ret = tmp;
2143 } else if ((parent = get.parent(node))) {
2144 /* maybe if notif exists as sibling */
2145 ret = get.notifs(parent);
2146 } else {
2147 ret = NULL;
2148 }
2149 } else if (get.nodetype(node) & LYS_INPUT) {
2150 if ((parent = get.parent(node))) {
2151 /* if output action has data */
2152 if (get.child(get.action_output(parent))) {
2153 /* then next sibling is output action */
2154 ret = get.action_output(parent);
2155 } else {
2156 /* input action cannot have siblings other
2157 * than output action.
2158 */
2159 ret = NULL;
2160 }
2161 } else {
2162 /* there is no way how to get output action */
2163 ret = NULL;
2164 }
2165 } else if (get.nodetype(node) & LYS_OUTPUT) {
2166 /* output action cannot have siblings */
2167 ret = NULL;
2168 } else if (get.nodetype(node) & LYS_NOTIF) {
2169 /* must have as a sibling only notif */
2170 ret = get.next(node);
2171 } else {
2172 /* for rest of nodes */
2173 if ((tmp = get.next(node))) {
2174 /* some sibling exists */
2175 ret = tmp;
2176 } else if ((parent = get.parent(node))) {
2177 /* Action and notif are siblings too.
2178 * They can be reached through parent.
2179 */
2180 if ((tmp = get.actions(parent))) {
2181 /* next sibling is action */
2182 ret = tmp;
2183 } else if ((tmp = get.notifs(parent))) {
2184 /* next sibling is notif */
2185 ret = tmp;
2186 } else {
2187 /* sibling not exists */
2188 ret = NULL;
2189 }
2190 } else {
2191 /* sibling not exists */
2192 ret = NULL;
2193 }
2194 }
2195
2196 return ret;
2197}
2198
2199/**
2200 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002201 *
2202 * This is a general algorithm that is able to
2203 * work with lysp_node or lysc_node.
2204 *
2205 * @param[in] node points to lysp_node or lysc_node.
2206 * @param[in] lysc_tree flag to determine what type the @p node is.
2207 * If set to true, then @p points to lysc_node otherwise lysp_node.
2208 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002209 */
2210static const void *
aPiecek3f247652021-04-19 13:40:25 +02002211tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002212{
2213 struct tro_getters get;
2214 const void *tmp;
2215 const void *ret;
2216
2217 assert(node);
2218
aPiecek3f247652021-04-19 13:40:25 +02002219 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002220
2221 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2222 if (get.child(get.action_input(node))) {
2223 /* go to LYS_INPUT */
2224 ret = get.action_input(node);
2225 } else if (get.child(get.action_output(node))) {
2226 /* go to LYS_OUTPUT */
2227 ret = get.action_output(node);
2228 } else {
2229 /* input action and output action have no data */
2230 ret = NULL;
2231 }
2232 } else {
2233 if ((tmp = get.child(node))) {
2234 ret = tmp;
2235 } else {
2236 /* current node can't have children or has no children */
2237 /* but maybe has some actions or notifs */
2238 if ((tmp = get.actions(node))) {
2239 ret = tmp;
2240 } else if ((tmp = get.notifs(node))) {
2241 ret = tmp;
2242 } else {
2243 ret = NULL;
2244 }
2245 }
2246 }
2247
2248 return ret;
2249}
2250
2251/**
aPiecek3f247652021-04-19 13:40:25 +02002252 * @brief Get new trt_parent_cache if we apply the transfer
2253 * to the child node in the tree.
2254 * @param[in] ca is parent cache for current node.
2255 * @param[in] tc contains current tree node.
2256 * @return Cache for the current node.
2257 */
2258static struct trt_parent_cache
2259tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2260{
2261 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2262
2263 if (!tc->lysc_tree) {
2264 const struct lysp_node *pn = tc->pn;
2265
2266 ret.ancestor =
2267 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2268 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2269 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2270 ca.ancestor;
2271
2272 ret.lys_status =
2273 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2274 ca.lys_status;
2275
2276 ret.lys_config =
2277 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2278 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2279 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2280 ca.lys_config;
2281
2282 ret.last_list =
2283 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2284 ca.last_list;
2285 }
2286
2287 return ret;
2288}
2289
2290/**
aPiecekef1e58e2021-04-19 13:19:44 +02002291 * @brief Transformation of the Schema nodes flags to
2292 * Tree diagram \<status\>.
2293 * @param[in] flags is node's flags obtained from the tree.
2294 */
2295static trt_status_type
2296tro_flags2status(uint16_t flags)
2297{
2298 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2299 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2300 TRD_STATUS_TYPE_CURRENT;
2301}
2302
2303/**
2304 * @brief Transformation of the Schema nodes flags to Tree diagram
2305 * \<flags\> but more specifically 'ro' or 'rw'.
2306 * @param[in] flags is node's flags obtained from the tree.
2307 */
2308static trt_flags_type
2309tro_flags2config(uint16_t flags)
2310{
2311 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2312 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2313 TRD_FLAGS_TYPE_EMPTY;
2314}
2315
2316/**
aPiecek3f247652021-04-19 13:40:25 +02002317 * @brief Print current node's iffeatures.
2318 * @param[in] tc is tree context.
2319 * @param[in,out] out is output handler.
2320 */
2321static void
2322tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2323{
2324 const struct lysp_qname *iffs;
2325
aPiecekbbc02932021-05-21 07:19:41 +02002326 if (tc->lysc_tree) {
2327 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2328 iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
2329 } else {
2330 iffs = tc->pn->iffeatures;
2331 }
aPiecek3f247652021-04-19 13:40:25 +02002332 LY_ARRAY_COUNT_TYPE i;
2333
2334 LY_ARRAY_FOR(iffs, i) {
2335 if (i == 0) {
2336 ly_print_(out, "%s", iffs[i].str);
2337 } else {
2338 ly_print_(out, ",%s", iffs[i].str);
2339 }
2340 }
2341
2342}
2343
2344/**
2345 * @brief Print current list's keys.
2346 *
2347 * Well, actually printing keys in the lysp_tree is trivial,
2348 * because char* points to all keys. However, special functions have
2349 * been reserved for this, because in principle the list of elements
2350 * can have more implementations.
2351 *
2352 * @param[in] tc is tree context.
2353 * @param[in,out] out is output handler.
2354 */
2355static void
2356tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2357{
2358 const struct lysp_node_list *list;
2359
aPiecekbbc02932021-05-21 07:19:41 +02002360 if (tc->lysc_tree) {
2361 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2362 list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
2363 } else {
2364 list = (const struct lysp_node_list *)tc->pn;
2365 }
aPiecek3f247652021-04-19 13:40:25 +02002366 assert(list->nodetype & LYS_LIST);
2367
2368 if (trg_charptr_has_data(list->key)) {
2369 ly_print_(out, "%s", list->key);
2370 }
2371}
2372
2373/**
2374 * @brief Get rpcs section if exists.
2375 * @param[in,out] tc is tree context.
2376 * @return Section representation if it exists. The @p tc is modified
2377 * and his pointer points to the first node in rpcs section.
2378 * @return Empty section representation otherwise.
2379 */
2380static struct trt_keyword_stmt
2381tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2382{
aPiecek9f792e52021-04-21 08:33:56 +02002383 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002384 const void *actions;
2385
2386 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002387 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002388 if (actions) {
2389 tc->cn = actions;
2390 }
2391 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002392 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002393 if (actions) {
2394 tc->pn = actions;
2395 tc->tpn = tc->pn;
2396 }
2397 }
2398
2399 if (actions) {
2400 tc->section = TRD_SECT_RPCS;
2401 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2402 } else {
2403 return TRP_EMPTY_KEYWORD_STMT;
2404 }
2405}
2406
2407/**
2408 * @brief Get notification section if exists
2409 * @param[in,out] tc is tree context.
2410 * @return Section representation if it exists.
2411 * The @p tc is modified and his pointer points to the
2412 * first node in notification section.
2413 * @return Empty section representation otherwise.
2414 */
2415static struct trt_keyword_stmt
2416tro_modi_get_notifications(struct trt_tree_ctx *tc)
2417{
aPiecek9f792e52021-04-21 08:33:56 +02002418 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002419 const void *notifs;
2420
2421 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002422 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002423 if (notifs) {
2424 tc->cn = notifs;
2425 }
2426 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002427 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002428 if (notifs) {
2429 tc->pn = notifs;
2430 tc->tpn = tc->pn;
2431 }
2432 }
2433
2434 if (notifs) {
2435 tc->section = TRD_SECT_NOTIF;
2436 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2437 } else {
2438 return TRP_EMPTY_KEYWORD_STMT;
2439 }
2440}
2441
2442/**
aPiecek96baa7f2021-04-23 12:32:00 +02002443 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002444 *
2445 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002446 * @param[in] u is index to the array of extensions (lysc_ext_instance
2447 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002448 * @return Section representation if it exists.
2449 * @return Empty section representation otherwise.
2450 */
2451static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002452tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002453{
aPiecek96baa7f2021-04-23 12:32:00 +02002454 assert(tc);
2455 const void *node;
2456 const char *yang_data_name;
2457
2458 if (tc->lysc_tree) {
2459 struct lysc_ext_instance *exts;
2460 struct lysc_ext_substmt *substmts;
2461
2462 exts = tc->cmod->exts;
2463 substmts = exts[u].substmts;
2464 if (!substmts) {
2465 return TRP_EMPTY_KEYWORD_STMT;
2466 }
2467 node = *(const struct lysc_node **)substmts->storage;
2468 yang_data_name = exts[u].argument;
2469 } else {
2470 struct lysp_ext_instance *exts;
2471
2472 exts = tc->pmod->exts;
2473 node = exts[u].parsed;
2474 yang_data_name = exts[u].argument;
2475 }
2476
2477 if (tc->lysc_tree) {
2478 tc->cn = node;
2479 } else {
2480 tc->tpn_ext = &tc->pmod->exts[u];
2481 tc->pn = node;
2482 }
2483
2484 if (node) {
2485 tc->section = TRD_SECT_YANG_DATA;
2486 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2487 } else {
2488 return TRP_EMPTY_KEYWORD_STMT;
2489 }
aPiecekef1e58e2021-04-19 13:19:44 +02002490}
2491
2492/**
2493 * @brief Get name of the module.
2494 * @param[in] tc is context of the tree.
2495 */
2496static struct trt_keyword_stmt
2497tro_read_module_name(const struct trt_tree_ctx *tc)
2498{
aPiecek9f792e52021-04-21 08:33:56 +02002499 assert(tc);
2500
2501 struct trt_keyword_stmt ret;
2502
2503 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2504 TRD_KEYWORD_SUBMODULE :
2505 TRD_KEYWORD_MODULE;
2506
2507 ret.str = !tc->lysc_tree ?
2508 LYSP_MODULE_NAME(tc->pmod) :
2509 tc->cmod->mod->name;
2510
2511 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002512}
2513
aPiecekb8d5a0a2021-05-20 08:20:24 +02002514/**
2515 * @brief Create implicit "case" node as parent of @p node.
2516 * @param[in] node child of implicit case node.
2517 * @return The case node ready to print.
2518 */
2519static struct trt_node
2520tro_create_implicit_case_node(struct trt_node node)
2521{
2522 struct trt_node ret;
2523
2524 ret.status = node.status;
2525 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2526 ret.name.type = TRD_NODE_CASE;
2527 ret.name.module_prefix = node.name.module_prefix;
2528 ret.name.str = node.name.str;
2529 ret.type = TRP_EMPTY_TRT_TYPE;
aPiecek7a28e2f2021-05-21 07:27:03 +02002530 ret.iffeatures = 0;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002531 ret.last_one = node.last_one;
2532
2533 return ret;
2534}
2535
aPiecekef1e58e2021-04-19 13:19:44 +02002536/**********************************************************************
2537 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002538 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002539
2540/**
aPiecek61d062b2020-11-02 11:05:09 +01002541 * @brief Check if list statement has keys.
2542 * @param[in] pn is pointer to the list.
2543 * @return 1 if has keys, otherwise 0.
2544 */
2545static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002546trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002547{
aPiecekef1e58e2021-04-19 13:19:44 +02002548 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002549}
2550
2551/**
2552 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002553 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002554 * @return 1 if has if-features, otherwise 0.
2555 */
2556static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002557trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002558{
2559 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002560 const struct lysp_qname *iffs;
2561
aPiecek61d062b2020-11-02 11:05:09 +01002562 ly_bool ret = 0;
2563
aPiecekef1e58e2021-04-19 13:19:44 +02002564 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002565 LY_ARRAY_FOR(iffs, u) {
2566 ret = 1;
2567 break;
2568 }
2569 return ret;
2570}
2571
2572/**
2573 * @brief Find out if leaf is also the key in last list.
2574 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002575 * @param[in] ca_last_list is pointer to last visited list.
2576 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002577 * @return 1 if leaf is also the key, otherwise 0.
2578 */
2579static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002580trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002581{
2582 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2583 const struct lysp_node_list *list = ca_last_list;
2584
2585 if (!list) {
2586 return 0;
2587 }
2588 return trg_charptr_has_data(list->key) ?
2589 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2590}
2591
2592/**
2593 * @brief Check if container's type is presence.
2594 * @param[in] pn is pointer to container.
2595 * @return 1 if container has presence statement, otherwise 0.
2596 */
2597static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002598trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002599{
2600 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2601}
2602
2603/**
2604 * @brief Get leaflist's path without lysp_node type control.
2605 * @param[in] pn is pointer to the leaflist.
2606 */
2607static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002608trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002609{
2610 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2611
2612 return list->type.path ? list->type.path->expr : NULL;
2613}
2614
2615/**
2616 * @brief Get leaflist's type name without lysp_node type control.
2617 * @param[in] pn is pointer to the leaflist.
2618 */
2619static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002620trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002621{
2622 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2623
2624 return list->type.name;
2625}
2626
2627/**
2628 * @brief Get leaf's path without lysp_node type control.
2629 * @param[in] pn is pointer to the leaf node.
2630 */
2631static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002632trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002633{
2634 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2635
2636 return leaf->type.path ? leaf->type.path->expr : NULL;
2637}
2638
2639/**
2640 * @brief Get leaf's type name without lysp_node type control.
2641 * @param[in] pn is pointer to the leaf's type name.
2642 */
2643static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002644trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002645{
2646 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2647
2648 return leaf->type.name;
2649}
2650
2651/**
aPiecek874ea4d2021-04-19 12:26:36 +02002652 * @brief Get pointer to data using node type specification
2653 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002654 *
aPiecek874ea4d2021-04-19 12:26:36 +02002655 * @param[in] flags is node type specification.
2656 * If it is the correct node, the getter function is called.
2657 * @param[in] f is getter function which provides the desired
2658 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002659 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002660 * @return NULL if node has wrong type or getter function return
2661 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002662 * @return Pointer to desired char pointer obtained from the node.
2663 */
2664static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002665trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002666{
2667 if (pn->nodetype & flags) {
2668 const char *ret = f(pn);
Michal Vasko26bbb272022-08-02 14:54:33 +02002669
aPiecek61d062b2020-11-02 11:05:09 +01002670 return trg_charptr_has_data(ret) ? ret : NULL;
2671 } else {
2672 return NULL;
2673 }
2674}
2675
2676/**
aPiecek61d062b2020-11-02 11:05:09 +01002677 * @brief Resolve \<status\> of the current node.
2678 * @param[in] nodetype is node's type obtained from the tree.
2679 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002680 * @param[in] ca_lys_status is inherited status
2681 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002682 * @return The status type.
2683 */
2684static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002685trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002686{
2687 /* LYS_INPUT and LYS_OUTPUT is special case */
2688 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002689 return tro_flags2status(ca_lys_status);
2690 /* if ancestor's status is deprc or obslt
2691 * and also node's status is not set
2692 */
aPiecek61d062b2020-11-02 11:05:09 +01002693 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2694 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002695 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002696 } else {
2697 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002698 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002699 }
2700}
2701
2702/**
2703 * @brief Resolve \<flags\> of the current node.
2704 * @param[in] nodetype is node's type obtained from the tree.
2705 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002706 * @param[in] ca_ancestor is ancestor type obtained
2707 * from trt_parent_cache.
2708 * @param[in] ca_lys_config is inherited config item
2709 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002710 * @return The flags type.
2711 */
2712static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002713trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002714{
2715 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2716 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2717 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2718 return TRD_FLAGS_TYPE_RO;
2719 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2720 return TRD_FLAGS_TYPE_RO;
2721 } else if (nodetype & LYS_NOTIF) {
2722 return TRD_FLAGS_TYPE_NOTIF;
2723 } else if (nodetype & LYS_USES) {
2724 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2725 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2726 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002727 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002728 /* config is not set. Look at ancestor's config */
2729 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002730 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002731 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002732 }
2733}
2734
2735/**
2736 * @brief Resolve node type of the current node.
2737 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002738 * @param[in] ca_last_list is pointer to the last visited list.
2739 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002740 */
2741static trt_node_type
aPiecekef1e58e2021-04-19 13:19:44 +02002742trop_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002743{
2744 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2745 return TRD_NODE_ELSE;
2746 } else if (pn->nodetype & LYS_CASE) {
2747 return TRD_NODE_CASE;
2748 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2749 return TRD_NODE_OPTIONAL_CHOICE;
2750 } else if (pn->nodetype & LYS_CHOICE) {
2751 return TRD_NODE_CHOICE;
aPiecekef1e58e2021-04-19 13:19:44 +02002752 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002753 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002754 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002755 return TRD_NODE_KEYS;
2756 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2757 return TRD_NODE_LISTLEAFLIST;
2758 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2759 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002760 } 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 +01002761 return TRD_NODE_OPTIONAL;
2762 } else {
2763 return TRD_NODE_ELSE;
2764 }
2765}
2766
2767/**
aPiecekef1e58e2021-04-19 13:19:44 +02002768 * @brief Resolve \<type\> of the current node.
2769 * @param[in] pn is current node.
2770 */
2771static struct trt_type
2772trop_resolve_type(const struct lysp_node *pn)
2773{
2774 const char *tmp = NULL;
2775
2776 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2777 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2778 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2779 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2780 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2781 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2782 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2783 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2784 } else if (pn->nodetype == LYS_ANYDATA) {
2785 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2786 } else if (pn->nodetype & LYS_ANYXML) {
2787 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2788 } else {
2789 return TRP_EMPTY_TRT_TYPE;
2790 }
2791}
2792
2793/**
aPiecek61d062b2020-11-02 11:05:09 +01002794 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002795 * @param[in] ca contains stored important data
2796 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002797 * @param[in] tc is context of the tree.
2798 */
2799static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002800trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002801{
aPiecekef1e58e2021-04-19 13:19:44 +02002802 const struct lysp_node *pn;
2803 struct trt_node ret;
2804
aPiecek61d062b2020-11-02 11:05:09 +01002805 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002806
2807 pn = tc->pn;
2808 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002809
2810 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002811 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002812
2813 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2814 /* <flags> */
aPiecekef1e58e2021-04-19 13:19:44 +02002815 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002816
2817 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2818 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2819 /* set type of the node */
aPiecekef1e58e2021-04-19 13:19:44 +02002820 ret.name.type = trop_resolve_node_type(pn, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002821
aPiecek34fa3772021-05-21 12:35:46 +02002822 /* The parsed tree is not compiled, so no node can be augmented
2823 * from another module. This means that nodes from the parsed tree
2824 * will never have the prefix.
2825 */
aPiecek61d062b2020-11-02 11:05:09 +01002826 ret.name.module_prefix = NULL;
2827
2828 /* set node's name */
2829 ret.name.str = pn->name;
2830
2831 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002832 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002833
2834 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002835 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002836
aPiecek3f247652021-04-19 13:40:25 +02002837 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002838
2839 return ret;
2840}
2841
aPiecekef1e58e2021-04-19 13:19:44 +02002842/**
2843 * @brief Find out if the current node has siblings.
2844 * @param[in] tc is context of the tree.
2845 * @return 1 if sibling exists otherwise 0.
2846 */
2847static ly_bool
2848trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2849{
aPiecek3f247652021-04-19 13:40:25 +02002850 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002851}
2852
aPiecek96baa7f2021-04-23 12:32:00 +02002853/**
2854 * @brief Print all yang-data sections and print three dots instead
2855 * of nodes.
2856 * @param[in] exts is array of YANG extension instances from parsed
2857 * module (@ref sizedarrays).
2858 * @param[in] mll is maximum number of characters that can be printed
2859 * on one line.
2860 * @param[in,out] out is output handler.
2861 */
2862static void
2863trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2864{
2865 struct trt_keyword_stmt ks;
2866 LY_ARRAY_COUNT_TYPE u;
2867 struct trt_wrapper wr;
2868
2869 if (!exts) {
2870 return;
2871 }
2872
2873 ly_print_(out, "\n");
2874 ks.type = TRD_KEYWORD_YANG_DATA;
2875 wr = TRP_INIT_WRAPPER_BODY;
2876
2877 LY_ARRAY_FOR(exts, u) {
2878 ly_print_(out, "\n");
2879
2880 /* yang-data <yang-data-name>: */
2881 ks.str = exts[u].argument;
2882 trp_print_keyword_stmt(ks, mll, 0, out);
2883 ly_print_(out, "\n");
2884
2885 /* ... */
2886 trp_print_wrapper(wr, out);
2887 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2888 }
2889}
2890
aPiecek874ea4d2021-04-19 12:26:36 +02002891/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002892 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002893 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002894
2895/**
aPiecek874ea4d2021-04-19 12:26:36 +02002896 * @brief Change current node pointer to its parent
2897 * but only if parent exists.
2898 * @param[in,out] tc is tree context.
2899 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002900 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002901 * @return 0 if the node did not have parents.
2902 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002903 */
2904static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002905trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002906{
2907 assert(tc && tc->pn);
2908 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002909 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002910 tc->pn = tc->pn->parent;
2911 return 1;
2912 } else {
2913 return 0;
2914 }
2915}
2916
2917/**
aPiecek874ea4d2021-04-19 12:26:36 +02002918 * @brief Change the current node pointer to its child
2919 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002920 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002921 * @param[in,out] tc is context of the tree.
2922 * Contains pointer to the current node.
2923 * @return Non-empty \<node\> representation of the current
2924 * node's child. The @p tc is modified.
2925 * @return Empty \<node\> representation if child don't exists.
2926 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002927 */
2928static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002929trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002930{
aPiecekef1e58e2021-04-19 13:19:44 +02002931 const struct lysp_node *tmp;
2932
aPiecek61d062b2020-11-02 11:05:09 +01002933 assert(tc && tc->pn);
2934
aPiecek3f247652021-04-19 13:40:25 +02002935 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002936 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02002937 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01002938 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002939 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002940 }
2941}
2942
2943/**
aPiecek874ea4d2021-04-19 12:26:36 +02002944 * @brief Change the current node pointer to the first child of node's
2945 * parent. If current node is already first sibling/child then nothing
2946 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01002947 * @param[in,out] tc is tree context.
2948 */
2949static void
aPiecekef1e58e2021-04-19 13:19:44 +02002950trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002951{
aPiecek9f792e52021-04-21 08:33:56 +02002952 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01002953
aPiecekef1e58e2021-04-19 13:19:44 +02002954 if (trop_modi_parent(tc)) {
2955 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002956 } else {
2957 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01002958 switch (tc->section) {
2959 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02002960 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02002961 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002962 break;
2963 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02002964 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02002965 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002966 break;
2967 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02002968 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02002969 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002970 break;
2971 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02002972 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02002973 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002974 break;
2975 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02002976 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02002977 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002978 break;
2979 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02002980 /* tpn in this case is of type lysp_ext_instance */
2981 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01002982 break;
aPiecek96baa7f2021-04-23 12:32:00 +02002983 default:
2984 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01002985 }
aPiecek61d062b2020-11-02 11:05:09 +01002986 }
2987}
2988
2989/**
aPiecek874ea4d2021-04-19 12:26:36 +02002990 * @brief Change the pointer to the current node to its next sibling
2991 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002992 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002993 * @param[in,out] tc is tree context.
2994 * Contains pointer to the current node.
2995 * @return Non-empty \<node\> representation if sibling exists.
2996 * The @p tc is modified.
2997 * @return Empty \<node\> representation otherwise.
2998 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002999 */
3000static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02003001trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003002{
aPiecekef1e58e2021-04-19 13:19:44 +02003003 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003004
3005 assert(tc && tc->pn);
3006
aPiecek3f247652021-04-19 13:40:25 +02003007 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01003008
aPiecekef1e58e2021-04-19 13:19:44 +02003009 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003010 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3011 tc->tpn = pn;
3012 }
aPiecekef1e58e2021-04-19 13:19:44 +02003013 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003014 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003015 } else {
3016 return TRP_EMPTY_NODE;
3017 }
3018}
3019
3020/**
3021 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003022 * @param[in,out] tc is tree context. It is modified and his current
3023 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003024 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003025 * @return Empty section structure otherwise.
3026 */
3027static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003028trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003029{
aPiecek9f792e52021-04-21 08:33:56 +02003030 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003031 const struct lysp_node_augment *augs;
3032
3033 /* if next_augment func was called for the first time */
3034 if (tc->section != TRD_SECT_AUGMENT) {
3035 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003036 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003037 } else {
3038 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003039 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003040 }
3041
aPiecekdc8fd572021-04-19 10:47:23 +02003042 if (augs) {
3043 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003044 tc->tpn = tc->pn;
3045 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3046 } else {
3047 return TRP_EMPTY_KEYWORD_STMT;
3048 }
3049}
3050
3051/**
aPiecek61d062b2020-11-02 11:05:09 +01003052 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003053 * @param[in,out] tc is tree context. It is modified and his current
3054 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003055 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003056 * @return Empty section representation otherwise.
3057 */
3058static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003059trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003060{
aPiecek9f792e52021-04-21 08:33:56 +02003061 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003062 const struct lysp_node_grp *grps;
3063
3064 if (tc->section != TRD_SECT_GROUPING) {
3065 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003066 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003067 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003068 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003069 }
3070
aPiecekdc8fd572021-04-19 10:47:23 +02003071 if (grps) {
3072 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003073 tc->tpn = tc->pn;
3074 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3075 } else {
3076 return TRP_EMPTY_KEYWORD_STMT;
3077 }
3078}
3079
aPiecek874ea4d2021-04-19 12:26:36 +02003080/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003081 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003082 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003083
3084/**
aPiecek3f247652021-04-19 13:40:25 +02003085 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003086 */
aPiecek3f247652021-04-19 13:40:25 +02003087static ly_bool
3088troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003089{
aPiecek3f247652021-04-19 13:40:25 +02003090 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3091}
aPiecek61d062b2020-11-02 11:05:09 +01003092
aPiecek3f247652021-04-19 13:40:25 +02003093/**
3094 * @brief Resolve \<flags\> of the current node.
3095 *
3096 * Use this function only if trt_tree_ctx.lysc_tree is true.
3097 *
3098 * @param[in] nodetype is current lysc_node.nodetype.
3099 * @param[in] flags is current lysc_node.flags.
3100 * @return The flags type.
3101 */
3102static trt_flags_type
3103troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3104{
3105 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3106 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3107 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3108 return TRD_FLAGS_TYPE_RO;
3109 } else if (nodetype & LYS_IS_NOTIF) {
3110 return TRD_FLAGS_TYPE_RO;
3111 } else if (nodetype & LYS_NOTIF) {
3112 return TRD_FLAGS_TYPE_NOTIF;
3113 } else if (nodetype & LYS_USES) {
3114 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3115 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3116 return TRD_FLAGS_TYPE_RPC;
3117 } else {
3118 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003119 }
aPiecek61d062b2020-11-02 11:05:09 +01003120}
3121
3122/**
aPiecek3f247652021-04-19 13:40:25 +02003123 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003124 *
aPiecek3f247652021-04-19 13:40:25 +02003125 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003126 *
aPiecek3f247652021-04-19 13:40:25 +02003127 * @param[in] nodetype is current lysc_node.nodetype.
3128 * @param[in] flags is current lysc_node.flags.
3129 */
3130static trt_node_type
3131troc_resolve_node_type(uint16_t nodetype, uint16_t flags)
3132{
3133 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3134 return TRD_NODE_ELSE;
3135 } else if (nodetype & LYS_CASE) {
3136 return TRD_NODE_CASE;
3137 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3138 return TRD_NODE_OPTIONAL_CHOICE;
3139 } else if (nodetype & LYS_CHOICE) {
3140 return TRD_NODE_CHOICE;
3141 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3142 return TRD_NODE_CONTAINER;
3143 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3144 return TRD_NODE_KEYS;
3145 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3146 return TRD_NODE_LISTLEAFLIST;
3147 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3148 return TRD_NODE_OPTIONAL;
3149 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3150 return TRD_NODE_OPTIONAL;
3151 } else {
3152 return TRD_NODE_ELSE;
3153 }
3154}
3155
3156/**
aPiecek34fa3772021-05-21 12:35:46 +02003157 * @brief Resolve prefix (<prefix>:<name>) of node that has been
3158 * placed from another module via an augment statement.
3159 *
3160 * @param[in] cn is current compiled node.
3161 * @param[in] current_compiled_module is module whose nodes are
3162 * currently being printed.
3163 * @return Prefix of foreign module or NULL.
3164 */
3165static const char *
3166troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
3167{
3168 const struct lys_module *node_module;
3169 const char *ret = NULL;
3170
3171 node_module = cn->module;
3172 if (node_module->compiled != current_compiled_module) {
3173 ret = node_module->prefix;
3174 }
3175
3176 return ret;
3177}
3178
3179/**
aPiecek3f247652021-04-19 13:40:25 +02003180 * @brief Transformation of current lysc_node to struct trt_node.
3181 * @param[in] ca is not used.
3182 * @param[in] tc is context of the tree.
3183 */
3184static struct trt_node
3185troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3186{
3187 (void) ca;
3188 const struct lysc_node *cn;
3189 struct trt_node ret;
3190
aPiecek082c7dc2021-05-20 08:55:07 +02003191 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003192
3193 cn = tc->cn;
3194 ret = TRP_EMPTY_NODE;
3195
3196 /* <status> */
3197 ret.status = tro_flags2status(cn->flags);
3198
3199 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
3200 /* <flags> */
3201 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3202
3203 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
3204 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
3205 /* set type of the node */
3206 ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
3207
aPiecek34fa3772021-05-21 12:35:46 +02003208 /* <prefix> */
3209 ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
aPiecek3f247652021-04-19 13:40:25 +02003210
3211 /* set node's name */
3212 ret.name.str = cn->name;
3213
aPiecekbbc02932021-05-21 07:19:41 +02003214 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003215 /* <type> */
3216 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003217
aPiecek082c7dc2021-05-20 08:55:07 +02003218 /* <iffeature> */
3219 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3220 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003221 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003222 assert(tc->cn->nodetype & LYS_CASE);
3223
3224 /* <type> */
3225 ret.type = TRP_EMPTY_TRT_TYPE;
3226
aPiecek7a28e2f2021-05-21 07:27:03 +02003227 /* <iffeature> */
3228 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003229 }
aPiecek3f247652021-04-19 13:40:25 +02003230
3231 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3232
3233 return ret;
3234}
3235
3236/**********************************************************************
3237 * Modify troc getters
3238 *********************************************************************/
3239
3240/**
aPiecek01598c02021-04-23 14:18:24 +02003241 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003242 */
3243static ly_bool
3244troc_modi_parent(struct trt_tree_ctx *tc)
3245{
3246 assert(tc && tc->cn);
3247 /* If no parent exists, stay in actual node. */
3248 if (tc->cn->parent) {
3249 tc->cn = tc->cn->parent;
3250 return 1;
3251 } else {
3252 return 0;
3253 }
3254}
3255
3256/**
aPiecek01598c02021-04-23 14:18:24 +02003257 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003258 */
3259static struct trt_node
3260troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3261{
3262 const struct lysc_node *cn;
3263
3264 assert(tc && tc->cn);
3265
3266 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3267
3268 /* if next sibling exists */
3269 if (cn) {
3270 /* update trt_tree_ctx */
3271 tc->cn = cn;
3272 return troc_read_node(ca, tc);
3273 } else {
3274 return TRP_EMPTY_NODE;
3275 }
3276}
3277
3278/**
3279 * @copydoc trop_modi_next_child()
3280 */
3281static struct trt_node
3282troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3283{
3284 const struct lysc_node *tmp;
3285
3286 assert(tc && tc->cn);
3287
3288 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3289 tc->cn = tmp;
3290 return troc_read_node(ca, tc);
3291 } else {
3292 return TRP_EMPTY_NODE;
3293 }
3294}
3295
3296/**
aPiecek01598c02021-04-23 14:18:24 +02003297 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003298 */
3299static void
aPiecek3f247652021-04-19 13:40:25 +02003300troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003301{
aPiecek3f247652021-04-19 13:40:25 +02003302 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003303
aPiecek3f247652021-04-19 13:40:25 +02003304 if (troc_modi_parent(tc)) {
3305 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3306 } else {
3307 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003308 switch (tc->section) {
3309 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003310 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003311 break;
3312 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003313 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003314 break;
3315 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003316 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003317 break;
3318 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003319 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003320 break;
3321 default:
3322 assert(0);
3323 }
aPiecek61d062b2020-11-02 11:05:09 +01003324 }
3325}
3326
aPiecek874ea4d2021-04-19 12:26:36 +02003327/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003328 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003329 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003330
3331/**
3332 * @brief Get size of node name.
3333 * @param[in] name contains name and mark.
3334 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003335 * @return negative value as an indication that option mark
3336 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003337 */
3338static int32_t
3339trb_strlen_of_name_and_mark(struct trt_node_name name)
3340{
3341 size_t name_len = strlen(name.str);
3342
3343 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3344 /* counting also parentheses */
3345 name_len += 2;
3346 }
3347
3348 return trp_mark_is_used(name) ?
3349 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3350 (int32_t)name_len;
3351}
3352
3353/**
aPiecek874ea4d2021-04-19 12:26:36 +02003354 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3355 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003356 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003357 * @param[in] max_len4all is the maximum value of btw_opts_type
3358 * that it can have.
3359 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003360 */
3361static int16_t
3362trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3363{
3364 int32_t name_len;
3365 int16_t min_len;
3366 int16_t ret;
3367
3368 name_len = trb_strlen_of_name_and_mark(name);
3369
3370 /* negative value indicate that in name is some opt mark */
3371 min_len = name_len < 0 ?
3372 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3373 TRD_INDENT_BEFORE_TYPE;
3374 ret = abs(max_len4all) - abs(name_len);
3375
3376 /* correction -> negative indicate that name is too long. */
3377 return ret < 0 ? min_len : ret;
3378}
3379
3380/**
3381 * @brief Print node.
3382 *
aPiecek01598c02021-04-23 14:18:24 +02003383 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003384 * But difference is that take @p max_gap_before_type which will be
3385 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003386 *
aPiecek9bdd7592021-05-20 08:13:20 +02003387 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003388 * @param[in] max_gap_before_type is number of indent before \<type\>.
3389 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003390 * @param[in] pc contains mainly functions for printing.
3391 * @param[in] tc is tree context.
3392 */
3393static void
aPiecek9bdd7592021-05-20 08:13:20 +02003394trb_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 +01003395{
aPiecek61d062b2020-11-02 11:05:09 +01003396 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3397
3398 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3399 /* print actual node with unified indent */
3400 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3401 }
3402 /* after -> print actual node with default indent */
3403 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3404 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
3405}
3406
3407/**
aPiecek874ea4d2021-04-19 12:26:36 +02003408 * @brief Check if parent of the current node is the last
3409 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003410 *
aPiecek874ea4d2021-04-19 12:26:36 +02003411 * To mantain stability use this function only if the current node is
3412 * the first of the siblings.
3413 * Side-effect -> current node is set to the first sibling
3414 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003415 *
aPiecek01598c02021-04-23 14:18:24 +02003416 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003417 * @param[in,out] tc is tree context.
3418 * @return 1 if parent is last sibling otherwise 0.
3419 */
3420static ly_bool
3421trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3422{
3423 if (fp.modify.parent(tc)) {
3424 ly_bool ret = fp.read.if_sibling_exists(tc);
Michal Vasko26bbb272022-08-02 14:54:33 +02003425
aPiecek61d062b2020-11-02 11:05:09 +01003426 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3427 return !ret;
3428 } else {
3429 return !fp.read.if_sibling_exists(tc);
3430 }
3431}
3432
3433/**
3434 * @brief Find sibling with the biggest node name and return that size.
3435 *
3436 * Side-effect -> Current node is set to the first sibling.
3437 *
3438 * @param[in] ca contains inherited data from ancestors.
3439 * @param[in] pc contains mainly functions for printing.
3440 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003441 * @return positive number as a sign that only the node name is
3442 * included in the size.
3443 * @return negative number sign that node name and his opt mark is
3444 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003445 */
3446static int32_t
3447trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3448{
3449 int32_t ret = 0;
3450
3451 pc->fp.modify.first_sibling(tc);
3452
3453 for (struct trt_node node = pc->fp.read.node(ca, tc);
3454 !trp_node_is_empty(node);
3455 node = pc->fp.modify.next_sibling(ca, tc)) {
3456 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
Michal Vasko26bbb272022-08-02 14:54:33 +02003457
aPiecek61d062b2020-11-02 11:05:09 +01003458 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3459 }
3460 pc->fp.modify.first_sibling(tc);
3461 return ret;
3462}
3463
3464/**
aPiecek874ea4d2021-04-19 12:26:36 +02003465 * @brief Find maximal indent between
3466 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003467 *
3468 * Side-effect -> Current node is set to the first sibling.
3469 *
3470 * @param[in] ca contains inherited data from ancestors.
3471 * @param[in] pc contains mainly functions for printing.
3472 * @param[in,out] tc is tree context.
3473 * @return max btw_opts_type value for rest of the siblings
3474 */
3475static int16_t
3476trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3477{
3478 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3479 int16_t ind_before_type = maxlen_node_name < 0 ?
3480 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3481 TRD_INDENT_BEFORE_TYPE;
3482
3483 return abs(maxlen_node_name) + ind_before_type;
3484}
3485
3486/**
aPiecek874ea4d2021-04-19 12:26:36 +02003487 * @brief Find out if it is possible to unify
3488 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003489 *
aPiecek874ea4d2021-04-19 12:26:36 +02003490 * The goal is for all node siblings to have the same alignment
3491 * for \<type\> as if they were in a column. All siblings who cannot
3492 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003493 * Side-effect -> Current node is set to the first sibling.
3494 *
3495 * @param[in] ca contains inherited data from ancestors.
3496 * @param[in] pc contains mainly functions for printing.
3497 * @param[in,out] tc is tree context.
3498 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003499 * @return positive number indicating the maximum number of spaces
3500 * before \<type\> if the length of the node name is 0. To calculate
3501 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003502 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003503*/
3504static uint32_t
3505trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3506{
3507 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3508}
3509
3510/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003511 * @brief Check if there is no case statement
3512 * under the choice statement.
3513 *
3514 * It can return true only if the Parsed schema tree
3515 * is used for browsing.
3516 *
3517 * @param[in] tc is tree context.
3518 * @return 1 if implicit case statement is present otherwise 0.
3519 */
3520static ly_bool
3521trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3522{
3523 return !tc->lysc_tree && tc->pn->parent &&
3524 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3525 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3526 LYS_LEAF | LYS_LEAFLIST));
3527}
3528
3529static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3530 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3531
3532/**
3533 * @brief Print implicit case node and his subtree.
3534 *
3535 * @param[in] node is child of implicit case.
3536 * @param[in] wr is wrapper for printing identation before node.
3537 * @param[in] ca contains inherited data from ancestors.
3538 * @param[in] pc contains mainly functions for printing.
3539 * @param[in] tc is tree context. Its settings should be the same as
3540 * before the function call.
3541 */
3542static void
3543trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3544 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3545{
3546 struct trt_node case_node;
3547 struct trt_wrapper wr_case_child;
3548
3549 case_node = tro_create_implicit_case_node(node);
3550 ly_print_(pc->out, "\n");
3551 trb_print_entire_node(case_node, 0, wr, pc, tc);
3552 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3553 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3554 ly_print_(pc->out, "\n");
3555 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3556}
3557
3558/**
aPiecek874ea4d2021-04-19 12:26:36 +02003559 * @brief For the current node: recursively print all of its child
3560 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003561 *
aPiecek01598c02021-04-23 14:18:24 +02003562 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003563 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003564 * Nodes are printed, including unified sibling node alignment
3565 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003566 * Side-effect -> current node is set to the last sibling.
3567 *
3568 * @param[in] wr is wrapper for printing identation before node.
3569 * @param[in] ca contains inherited data from ancestors.
3570 * @param[in] pc contains mainly functions for printing.
3571 * @param[in,out] tc is tree context.
3572 */
3573static void
3574trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3575{
3576 uint32_t max_gap_before_type;
3577 ly_bool sibling_flag = 0;
3578 ly_bool child_flag = 0;
3579
3580 /* if node is last sibling, then do not add '|' to wrapper */
3581 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3582 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3583
3584 /* try unified indentation in node */
3585 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3586
3587 /* print all siblings */
3588 do {
3589 struct trt_parent_cache new_ca;
3590 struct trt_node node;
Michal Vasko26bbb272022-08-02 14:54:33 +02003591
aPiecek9bdd7592021-05-20 08:13:20 +02003592 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003593
aPiecekb8d5a0a2021-05-20 08:20:24 +02003594 if (!trb_need_implicit_node_case(tc)) {
3595 /* normal behavior */
3596 ly_print_(pc->out, "\n");
3597 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3598 new_ca = tro_parent_cache_for_child(ca, tc);
3599 /* go to the actual node's child or stay in actual node */
3600 node = pc->fp.modify.next_child(ca, tc);
3601 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003602
aPiecekb8d5a0a2021-05-20 08:20:24 +02003603 if (child_flag) {
3604 /* print all childs - recursive call */
3605 trb_print_nodes(wr, new_ca, pc, tc);
3606 /* get back from child node to actual node */
3607 pc->fp.modify.parent(tc);
3608 }
3609 } else {
3610 /* The case statement is omitted (shorthand).
3611 * Print implicit case node and his subtree.
3612 */
3613 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003614 }
3615
3616 /* go to the actual node's sibling */
3617 node = pc->fp.modify.next_sibling(ca, tc);
3618 sibling_flag = !trp_node_is_empty(node);
3619
3620 /* go to the next sibling or stay in actual node */
3621 } while (sibling_flag);
3622}
3623
3624/**
aPiecek153b00f2021-04-20 13:52:57 +02003625 * @brief Calculate the wrapper about how deep in the tree the node is.
3626 * @param[in] node from which to count.
3627 * @return wrapper for @p node.
3628 */
3629static struct trt_wrapper
3630trb_count_depth(const struct lysc_node *node)
3631{
3632 struct trt_wrapper wr = TRP_INIT_WRAPPER_TOP;
3633 const struct lysc_node *parent;
3634
3635 if (!node) {
3636 return wr;
3637 }
3638
3639 for (parent = node->parent; parent; parent = parent->parent) {
3640 wr = trp_wrapper_set_shift(wr);
3641 }
3642
3643 return wr;
3644}
3645
3646/**
3647 * @brief Print all parent nodes of @p node and the @p node itself.
3648 *
3649 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3650 *
3651 * @param[in] node on which the function is focused.
aPiecek01598c02021-04-23 14:18:24 +02003652 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003653 * @param[in,out] tc is context of tree printer.
3654 * @return wrapper for @p node.
3655 */
3656static void
3657trb_print_parents(const struct lysc_node *node, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3658{
3659 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003660 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003661
3662 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3663
3664 /* stop recursion */
3665 if (!node) {
3666 return;
3667 }
3668 trb_print_parents(node->parent, pc, tc);
3669
3670 /* setup for printing */
3671 tc->cn = node;
3672 wr = trb_count_depth(node);
3673
3674 /* print node */
3675 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003676 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3677 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003678}
3679
3680/**
aPiecekdc8fd572021-04-19 10:47:23 +02003681 * @brief Get address of the current node.
3682 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003683 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003684 */
3685static const void *
3686trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3687{
aPiecek3f247652021-04-19 13:40:25 +02003688 return tc->lysc_tree ?
3689 (const void *)tc->cn :
3690 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003691}
3692
3693/**
3694 * @brief Get address of current node's child.
3695 * @param[in,out] tc contains current node.
3696 */
3697static const void *
3698trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3699{
3700 if (!trb_tree_ctx_get_node(tc)) {
3701 return NULL;
3702 }
3703
aPiecek3f247652021-04-19 13:40:25 +02003704 if (tc->lysc_tree) {
3705 return lysc_node_child(tc->cn);
3706 } else {
3707 return lysp_node_child(tc->pn);
3708 }
aPiecekdc8fd572021-04-19 10:47:23 +02003709}
3710
3711/**
3712 * @brief Set current node on its child.
3713 * @param[in,out] tc contains current node.
3714 */
3715static void
3716trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3717{
aPiecek3f247652021-04-19 13:40:25 +02003718 const void *node = trb_tree_ctx_get_child(tc);
3719
3720 if (tc->lysc_tree) {
3721 tc->cn = node;
3722 } else {
3723 tc->pn = node;
3724 }
aPiecekdc8fd572021-04-19 10:47:23 +02003725}
3726
3727/**
aPiecek61d062b2020-11-02 11:05:09 +01003728 * @brief Print subtree of nodes.
3729 *
3730 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003731 * Before root node is no linebreak printing. This must be addressed by
3732 * the caller. Root node will also be printed. Behind last printed node
3733 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003734 *
aPiecek9bdd7592021-05-20 08:13:20 +02003735 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003736 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003737 * ::trb_try_unified_indent() function for root node.
3738 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003739 * @param[in] wr is wrapper saying how deep in the whole tree
3740 * is the root of the subtree.
3741 * @param[in] ca is parent_cache from root's parent.
3742 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003743 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003744 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003745 */
3746static void
aPiecek9bdd7592021-05-20 08:13:20 +02003747trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3748 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003749{
3750 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003751
aPiecek9bdd7592021-05-20 08:13:20 +02003752 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003753 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003754 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003755 node = pc->fp.modify.next_child(ca, tc);
3756
3757 if (!trp_node_is_empty(node)) {
3758 /* print root's nodes */
3759 trb_print_nodes(wr, new_ca, pc, tc);
3760 /* get back from child node to actual node */
3761 pc->fp.modify.parent(tc);
3762 }
3763}
3764
3765/**
3766 * @brief Get number of siblings.
3767 *
3768 * Side-effect -> current node is set to the first sibling.
3769 *
3770 * @param[in] fp contains callback functions which modify tree context
3771 * @param[in,out] tc is the tree context.
3772 * @return Number of siblings of the current node.
3773 */
3774static uint32_t
3775trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3776{
3777 uint32_t ret = 1;
3778 struct trt_node node = TRP_EMPTY_NODE;
3779
3780 /* including actual node */
3781 fp.first_sibling(tc);
3782 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3783 ret++;
3784 }
3785 fp.first_sibling(tc);
3786 return ret;
3787}
3788
3789/**
3790 * @brief Print all parents and their children.
3791 *
aPiecek874ea4d2021-04-19 12:26:36 +02003792 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003793 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003794 * for all top-level siblings. Use this function after 'module' keyword
3795 * or 'augment' and so. The nodes may not be exactly top-level in the
3796 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003797 *
aPiecek153b00f2021-04-20 13:52:57 +02003798 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3799 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003800 * @param[pc] pc contains mainly functions for printing.
3801 * @param[in,out] tc is tree context.
3802 */
3803static void
aPiecek153b00f2021-04-20 13:52:57 +02003804trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003805{
aPiecek61d062b2020-11-02 11:05:09 +01003806 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003807 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003808 uint32_t total_parents;
3809 uint32_t max_gap_before_type;
3810
aPiecekdc8fd572021-04-19 10:47:23 +02003811 if (!trb_tree_ctx_get_node(tc)) {
3812 return;
3813 }
3814
aPiecek61d062b2020-11-02 11:05:09 +01003815 ca = TRP_EMPTY_PARENT_CACHE;
3816 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3817 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3818
aPiecek3f247652021-04-19 13:40:25 +02003819 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003820 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3821 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003822 ca.lys_config = 0x0;
3823 }
aPiecekdc8fd572021-04-19 10:47:23 +02003824 }
3825
aPiecek61d062b2020-11-02 11:05:09 +01003826 for (uint32_t i = 0; i < total_parents; i++) {
3827 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003828 node = pc->fp.read.node(ca, tc);
3829 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003830 pc->fp.modify.next_sibling(ca, tc);
3831 }
3832}
3833
aPiecek874ea4d2021-04-19 12:26:36 +02003834/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003835 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003836 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003837
3838/**
aPiecekdc8fd572021-04-19 10:47:23 +02003839 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003840 *
aPiecekdc8fd572021-04-19 10:47:23 +02003841 * @param[in] module YANG schema tree structure representing
3842 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003843 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003844 * @param[in] max_line_length is the maximum line length limit
3845 * that should not be exceeded.
3846 * @param[in,out] pc will be adapted to lysp_tree.
3847 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003848 */
3849static void
aPiecekdc8fd572021-04-19 10:47:23 +02003850trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003851{
aPiecekdc8fd572021-04-19 10:47:23 +02003852 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003853 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003854 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003855 .pmod = module->parsed,
3856 .cmod = NULL,
3857 .pn = module->parsed ? module->parsed->data : NULL,
3858 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003859 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003860 };
aPiecek61d062b2020-11-02 11:05:09 +01003861
aPiecekdc8fd572021-04-19 10:47:23 +02003862 pc->out = out;
3863
3864 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003865 .parent = trop_modi_parent,
3866 .first_sibling = trop_modi_first_sibling,
3867 .next_sibling = trop_modi_next_sibling,
3868 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01003869 };
3870
aPiecekdc8fd572021-04-19 10:47:23 +02003871 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003872 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003873 .node = trop_read_node,
3874 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003875 };
3876
aPiecekdc8fd572021-04-19 10:47:23 +02003877 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003878 .print_features_names = tro_print_features_names,
3879 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01003880 };
3881
aPiecekdc8fd572021-04-19 10:47:23 +02003882 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01003883}
3884
3885/**
aPiecek3f247652021-04-19 13:40:25 +02003886 * @brief Settings if lysc_node are used for browsing through the tree.
3887 *
3888 * Pointers to current nodes will be set to module data.
3889 *
3890 * @param[in] module YANG schema tree structure representing
3891 * YANG module.
3892 * @param[in] out is output handler.
3893 * @param[in] max_line_length is the maximum line length limit
3894 * that should not be exceeded.
3895 * @param[in,out] pc will be adapted to lysc_tree.
3896 * @param[in,out] tc will be adapted to lysc_tree.
3897 */
3898static void
3899trm_lysc_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3900{
3901 *tc = (struct trt_tree_ctx) {
3902 .lysc_tree = 1,
3903 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003904 .pmod = module->parsed,
3905 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02003906 .tpn = NULL,
3907 .pn = NULL,
3908 .cn = module->compiled->data
3909 };
3910
3911 pc->out = out;
3912
3913 pc->fp.modify = (struct trt_fp_modify_ctx) {
3914 .parent = troc_modi_parent,
3915 .first_sibling = troc_modi_first_sibling,
3916 .next_sibling = troc_modi_next_sibling,
3917 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02003918 };
3919
3920 pc->fp.read = (struct trt_fp_read) {
3921 .module_name = tro_read_module_name,
3922 .node = troc_read_node,
3923 .if_sibling_exists = troc_read_if_sibling_exists
3924 };
3925
3926 pc->fp.print = (struct trt_fp_print) {
3927 .print_features_names = tro_print_features_names,
3928 .print_keys = tro_print_keys
3929 };
3930
3931 pc->max_line_length = max_line_length;
3932}
3933
3934/**
3935 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02003936 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02003937 * @param[in,out] tc resets to lysc browsing.
3938 */
3939static void
3940trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3941{
aPiecek9f792e52021-04-21 08:33:56 +02003942 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003943}
3944
3945/**
3946 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02003947 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02003948 * @param[in,out] tc resets to lysp browsing.
3949 */
3950static void
3951trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3952{
aPiecek9f792e52021-04-21 08:33:56 +02003953 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003954}
3955
3956/**
3957 * @brief If augment's target node is located on the current module.
3958 * @param[in] pn is examined augment.
3959 * @param[in] pmod is current module.
3960 * @return 1 if nodeid refers to the local node, otherwise 0.
3961 */
3962static ly_bool
3963trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
3964{
3965 const char *id, *prefix, *name;
3966 size_t prefix_len, name_len;
3967 const struct lys_module *mod;
3968 ly_bool ret = 0;
3969
3970 if (pn == NULL) {
3971 return ret;
3972 }
3973
3974 id = pn->nodeid;
3975 if (!id) {
3976 return ret;
3977 }
3978 /* only absolute-schema-nodeid is taken into account */
3979 assert(id[0] == '/');
3980 ++id;
3981
3982 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3983 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003984 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
Michal Vasko3003c9a2022-03-29 12:10:00 +02003985 ret = mod ? (mod->parsed == pmod) : 0;
aPiecek3f247652021-04-19 13:40:25 +02003986 } else {
3987 ret = 1;
3988 }
3989
3990 return ret;
3991}
3992
3993/**
aPiecek96baa7f2021-04-23 12:32:00 +02003994 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01003995 *
aPiecekdc8fd572021-04-19 10:47:23 +02003996 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02003997 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01003998 *
aPiecekdc8fd572021-04-19 10:47:23 +02003999 * @param[in] ks is section representation.
4000 * @param[in] pc contains mainly functions for printing.
4001 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01004002 */
4003static void
aPiecekdc8fd572021-04-19 10:47:23 +02004004trm_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 +01004005{
aPiecekdc8fd572021-04-19 10:47:23 +02004006 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4007 return;
4008 }
4009
4010 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
4011 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02004012 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004013 } else {
aPiecek153b00f2021-04-20 13:52:57 +02004014 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004015 }
4016}
4017
4018/**
aPiecek96baa7f2021-04-23 12:32:00 +02004019 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02004020 *
aPiecek96baa7f2021-04-23 12:32:00 +02004021 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02004022 *
4023 * @param[in] ks is section representation.
4024 * @param[in] pc contains mainly functions for printing.
4025 * @param[in,out] tc is the tree context.
4026 */
4027static void
4028trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4029{
4030 ly_bool grp_has_data = 0;
4031
4032 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4033 return;
4034 }
4035
4036 if (ks.type == TRD_KEYWORD_GROUPING) {
4037 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4038 }
4039
4040 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4041 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004042 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004043}
4044
4045/**
4046 * @brief Print 'module' keyword, its name and all nodes.
4047 * @param[in] pc contains mainly functions for printing.
4048 * @param[in,out] tc is the tree context.
4049 */
4050static void
4051trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4052{
4053 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4054}
4055
4056/**
4057 * @brief For all augment sections: print 'augment' keyword,
4058 * its target node and all nodes.
4059 * @param[in] pc contains mainly functions for printing.
4060 * @param[in,out] tc is the tree context.
4061 */
4062static void
4063trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4064{
4065 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004066 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004067
aPiecek3f247652021-04-19 13:40:25 +02004068 if (tc->lysc_tree) {
4069 origin_was_lysc_tree = 1;
4070 trm_reset_to_lysp_tree_ctx(pc, tc);
4071 }
4072
aPiecekdc8fd572021-04-19 10:47:23 +02004073 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004074 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004075 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004076 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004077
aPiecek3f247652021-04-19 13:40:25 +02004078 if (origin_was_lysc_tree) {
4079 /* if lysc tree is used, then only augments targeting
4080 * another module are printed
4081 */
aPiecek9f792e52021-04-21 08:33:56 +02004082 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004083 continue;
4084 }
4085 }
4086
aPiecekdc8fd572021-04-19 10:47:23 +02004087 if (once) {
4088 ly_print_(pc->out, "\n");
4089 ly_print_(pc->out, "\n");
4090 once = 0;
4091 } else {
4092 ly_print_(pc->out, "\n");
4093 }
4094
4095 trm_print_section_as_subtree(ks, pc, tc);
4096 }
aPiecek3f247652021-04-19 13:40:25 +02004097
4098 if (origin_was_lysc_tree) {
4099 trm_reset_to_lysc_tree_ctx(pc, tc);
4100 }
aPiecekdc8fd572021-04-19 10:47:23 +02004101}
4102
4103/**
4104 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4105 * @param[in] pc contains mainly functions for printing.
4106 * @param[in,out] tc is the tree context.
4107 */
4108static void
4109trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4110{
4111 struct trt_keyword_stmt rpc;
4112
aPiecek01598c02021-04-23 14:18:24 +02004113 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004114
4115 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4116 ly_print_(pc->out, "\n");
4117 ly_print_(pc->out, "\n");
4118 trm_print_section_as_family_tree(rpc, pc, tc);
4119 }
4120}
4121
4122/**
4123 * @brief For notifications section: print 'notifications' keyword
4124 * and all its nodes.
4125 * @param[in] pc contains mainly functions for printing.
4126 * @param[in,out] tc is the tree context.
4127 */
4128static void
4129trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4130{
4131 struct trt_keyword_stmt notifs;
4132
aPiecek01598c02021-04-23 14:18:24 +02004133 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004134
4135 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4136 ly_print_(pc->out, "\n");
4137 ly_print_(pc->out, "\n");
4138 trm_print_section_as_family_tree(notifs, pc, tc);
4139 }
4140}
4141
4142/**
4143 * @brief For all grouping sections: print 'grouping' keyword, its name
4144 * and all nodes.
4145 * @param[in] pc contains mainly functions for printing.
4146 * @param[in,out] tc is the tree context.
4147 */
4148static void
4149trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4150{
4151 ly_bool once;
4152
aPiecek01598c02021-04-23 14:18:24 +02004153 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004154 return;
4155 }
4156
4157 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004158 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004159 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004160 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004161 if (once) {
4162 ly_print_(pc->out, "\n");
4163 ly_print_(pc->out, "\n");
4164 once = 0;
4165 } else {
4166 ly_print_(pc->out, "\n");
4167 }
4168 trm_print_section_as_subtree(ks, pc, tc);
4169 }
4170}
4171
4172/**
4173 * @brief For all yang-data sections: print 'yang-data' keyword
4174 * and all its nodes.
4175 * @param[in] pc contains mainly functions for printing.
4176 * @param[in,out] tc is the tree context.
4177 */
4178static void
aPiecek96baa7f2021-04-23 12:32:00 +02004179trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004180{
aPiecek96baa7f2021-04-23 12:32:00 +02004181 ly_bool once;
4182 LY_ARRAY_COUNT_TYPE count;
4183
4184 count = LY_ARRAY_COUNT(tc->pmod->exts);
4185 if (count == 0) {
4186 return;
4187 }
4188
4189 once = 1;
4190 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4191 struct trt_keyword_stmt ks;
4192
aPiecek01598c02021-04-23 14:18:24 +02004193 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004194 * ::lysp_ext_instance.parsed.
4195 */
4196 if (!tc->pmod->exts[u].parsed) {
4197 /* print at least the yang-data names */
4198 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4199 continue;
4200 }
4201
4202 ks = tro_modi_next_yang_data(tc, u);
4203 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4204 break;
4205 }
4206
4207 if (once) {
4208 ly_print_(pc->out, "\n");
4209 ly_print_(pc->out, "\n");
4210 once = 0;
4211 } else {
4212 ly_print_(pc->out, "\n");
4213 }
4214
4215 trm_print_section_as_family_tree(ks, pc, tc);
4216 }
aPiecekdc8fd572021-04-19 10:47:23 +02004217}
4218
4219/**
4220 * @brief Print sections module, augment, rpcs, notifications,
4221 * grouping, yang-data.
4222 * @param[in] pc contains mainly functions for printing.
4223 * @param[in,out] tc is the tree context.
4224 */
4225static void
4226trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4227{
4228 trm_print_module_section(pc, tc);
4229 trm_print_augmentations(pc, tc);
4230 trm_print_rpcs(pc, tc);
4231 trm_print_notifications(pc, tc);
4232 trm_print_groupings(pc, tc);
4233 trm_print_yang_data(pc, tc);
4234 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004235}
4236
aPiecek874ea4d2021-04-19 12:26:36 +02004237/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004238 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004239 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004240
4241LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004242tree_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 +01004243{
4244 struct trt_printer_ctx pc;
4245 struct trt_tree_ctx tc;
4246 struct ly_out *new_out;
4247 LY_ERR erc;
4248 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4249
aPiecekdc8fd572021-04-19 10:47:23 +02004250 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4251
aPiecek61d062b2020-11-02 11:05:09 +01004252 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4253 return erc;
4254 }
4255
4256 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004257 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4258 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4259 } else {
4260 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4261 }
aPiecek61d062b2020-11-02 11:05:09 +01004262
4263 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004264 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004265
4266 ly_out_free(new_out, NULL, 1);
4267
aPiecekdc8fd572021-04-19 10:47:23 +02004268 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004269}
4270
4271LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004272tree_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 +01004273{
aPiecek153b00f2021-04-20 13:52:57 +02004274 struct trt_printer_ctx pc;
4275 struct trt_tree_ctx tc;
4276 struct ly_out *new_out;
4277 struct trt_wrapper wr;
4278 LY_ERR erc;
4279 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4280
4281 assert(out && node);
4282
4283 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4284 return LY_EINVAL;
4285 }
4286
4287 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4288 return erc;
4289 }
4290
4291 line_length = line_length == 0 ? SIZE_MAX : line_length;
4292 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4293
4294 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
4295 trb_print_parents(node, &pc, &tc);
4296
4297 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4298 tc.cn = lysc_node_child(node);
4299 wr = trb_count_depth(tc.cn);
4300 trb_print_family_tree(wr, &pc, &tc);
4301 }
4302 ly_print_(out, "\n");
4303
4304 erc = clb_arg.last_error;
4305 ly_out_free(new_out, NULL, 1);
4306
4307 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004308}
4309
4310LY_ERR
Michal Vasko6a914fb2022-01-17 10:36:14 +01004311tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
4312 size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004313{
aPiecek9f792e52021-04-21 08:33:56 +02004314 struct trt_printer_ctx pc;
4315 struct trt_tree_ctx tc;
4316 struct ly_out *new_out;
4317 LY_ERR erc;
4318 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4319
4320 assert(submodp);
4321 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4322
4323 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4324 return erc;
4325 }
4326
4327 line_length = line_length == 0 ? SIZE_MAX : line_length;
4328 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4329 tc.pmod = (struct lysp_module *)submodp;
4330 tc.tpn = submodp->data;
4331 tc.pn = tc.tpn;
4332
4333 trm_print_sections(&pc, &tc);
4334 erc = clb_arg.last_error;
4335
4336 ly_out_free(new_out, NULL, 1);
4337
4338 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004339}