blob: d8fcc0554b2677b8c2e045232459a5b19db94663 [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"
96#include "tree_schema_internal.h"
97#include "xpath.h"
98
aPiecek61d062b2020-11-02 11:05:09 +010099/**
100 * @brief List of available actions.
101 */
102typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200103 TRD_PRINT = 0, /**< Normal behavior. It just prints. */
104 TRD_CHAR_COUNT /**< Characters will be counted instead of printing. */
aPiecek61d062b2020-11-02 11:05:09 +0100105} trt_ly_out_clb_arg_flag;
106
107/**
aPiecek874ea4d2021-04-19 12:26:36 +0200108 * @brief Structure is passed as 'writeclb' argument
aPiecek01598c02021-04-23 14:18:24 +0200109 * to the ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100110 */
111struct ly_out_clb_arg {
aPiecek874ea4d2021-04-19 12:26:36 +0200112 trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */
113 struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */
114 size_t counter; /**< Counter of printed characters. */
115 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 +0100116};
117
118/**
119 * @brief Initialize struct ly_out_clb_arg with default settings.
120 */
121#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
aPiecek874ea4d2021-04-19 12:26:36 +0200122 (struct ly_out_clb_arg) { \
123 .mode = MODE, .out = OUT, \
124 .counter = COUNTER, .last_error = LAST_ERROR \
125 }
aPiecek61d062b2020-11-02 11:05:09 +0100126
aPiecek874ea4d2021-04-19 12:26:36 +0200127/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100128 * Print getters
aPiecek874ea4d2021-04-19 12:26:36 +0200129 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100130
131/**
132 * @brief Callback functions that prints special cases.
133 *
134 * It just groups together tree context with trt_fp_print.
135 */
136struct trt_cf_print {
aPiecek874ea4d2021-04-19 12:26:36 +0200137 const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */
138 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 +0100139};
140
141/**
142 * @brief Callback functions for printing special cases.
143 *
aPiecek874ea4d2021-04-19 12:26:36 +0200144 * Functions with the suffix 'trp' can print most of the text on
145 * output, just by setting the pointer to the string. But in some
146 * cases, it's not that simple, because its entire string is fragmented
147 * in memory. For example, for printing list's keys or if-features.
aPiecek61d062b2020-11-02 11:05:09 +0100148 * However, this depends on how the libyang library is implemented.
aPiecek3f247652021-04-19 13:40:25 +0200149 * This implementation of the printer_tree module goes through
150 * a lysp tree, but if it goes through a lysc tree, these special cases
151 * would be different.
aPiecek61d062b2020-11-02 11:05:09 +0100152 * Functions must print including spaces or delimiters between names.
153 */
154struct trt_fp_print {
155 void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */
156 void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */
157};
158
159/**
160 * @brief Package which only groups getter function.
161 */
162struct trt_pck_print {
163 const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */
164 struct trt_fp_print fps; /**< Print function. */
165};
166
167/**
168 * @brief Initialize struct trt_pck_print by parameters.
169 */
170#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
aPiecek874ea4d2021-04-19 12:26:36 +0200171 (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT}
aPiecek61d062b2020-11-02 11:05:09 +0100172
aPiecek874ea4d2021-04-19 12:26:36 +0200173/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100174 * Indent
aPiecek874ea4d2021-04-19 12:26:36 +0200175 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100176
177/**
aPiecek874ea4d2021-04-19 12:26:36 +0200178 * @brief Constants which are defined in the RFC or are observable
179 * from the pyang tool.
aPiecek61d062b2020-11-02 11:05:09 +0100180 */
181typedef enum {
182 TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */
183 TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with a whitespace offset of at least two characters. */
184 TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */
185 TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */
186 TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */
187 TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */
188 TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */
189} trt_cnf_indent;
190
191/**
192 * @brief Type of indent in node.
193 */
194typedef enum {
195 TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */
196 TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */
197 TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
198} trt_indent_in_node_type;
199
200/** Constant to indicate the need to break a line. */
201#define TRD_LINEBREAK -1
202
203/**
aPiecek874ea4d2021-04-19 12:26:36 +0200204 * @brief Records the alignment between the individual
205 * elements of the node.
aPiecek61d062b2020-11-02 11:05:09 +0100206 *
aPiecek874ea4d2021-04-19 12:26:36 +0200207 * @see trp_default_indent_in_node, trp_try_normal_indent_in_node
aPiecek61d062b2020-11-02 11:05:09 +0100208 */
209struct trt_indent_in_node {
210 trt_indent_in_node_type type; /**< Type of indent in node. */
aPiecek874ea4d2021-04-19 12:26:36 +0200211 int16_t btw_name_opts; /**< Indent between node name and \<opts\>. */
212 int16_t btw_opts_type; /**< Indent between \<opts\> and \<type\>. */
aPiecek61d062b2020-11-02 11:05:09 +0100213 int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */
214};
215
216/**
217 * @brief Type of wrappers to be printed.
218 */
219typedef enum {
220 TRD_WRAPPER_TOP = 0, /**< Related to the module. */
221 TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */
222} trd_wrapper_type;
223
224/**
225 * @brief For resolving sibling symbol ('|') placement.
226 *
227 * Bit indicates where the sibling symbol must be printed.
aPiecek874ea4d2021-04-19 12:26:36 +0200228 * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS.
aPiecek61d062b2020-11-02 11:05:09 +0100229 *
aPiecek874ea4d2021-04-19 12:26:36 +0200230 * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY,
231 * trp_wrapper_set_mark, trp_wrapper_set_shift,
232 * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper
aPiecek61d062b2020-11-02 11:05:09 +0100233 */
234struct trt_wrapper {
235 trd_wrapper_type type; /**< Location of the wrapper. */
236 uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed.
237 It follows that the maximum immersion of the printable node is 64. */
238 uint32_t actual_pos; /**< Actual position in bit_marks. */
239};
240
241/**
242 * @brief Get wrapper related to the module section.
243 *
244 * @code
245 * module: <module-name>
246 * +--<node>
247 * |
248 * @endcode
249 */
250#define TRP_INIT_WRAPPER_TOP \
aPiecek874ea4d2021-04-19 12:26:36 +0200251 (struct trt_wrapper) { \
252 .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \
253 }
aPiecek61d062b2020-11-02 11:05:09 +0100254
255/**
aPiecek874ea4d2021-04-19 12:26:36 +0200256 * @brief Get wrapper related to subsection
257 * e.g. Augmenations or Groupings.
aPiecek61d062b2020-11-02 11:05:09 +0100258 *
259 * @code
260 * module: <module-name>
261 * +--<node>
262 *
263 * augment <target-node>:
264 * +--<node>
265 * @endcode
266 */
267#define TRP_INIT_WRAPPER_BODY \
aPiecek874ea4d2021-04-19 12:26:36 +0200268 (struct trt_wrapper) { \
269 .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \
270 }
aPiecek61d062b2020-11-02 11:05:09 +0100271
272/**
273 * @brief Package which only groups wrapper and indent in node.
274 */
275struct trt_pck_indent {
276 struct trt_wrapper wrapper; /**< Coded " | | " sequence. */
277 struct trt_indent_in_node in_node; /**< Indent in node. */
278};
279
280/**
281 * @brief Initialize struct trt_pck_indent by parameters.
282 */
283#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200284 (struct trt_pck_indent){ \
285 .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \
286 }
aPiecek61d062b2020-11-02 11:05:09 +0100287
aPiecek874ea4d2021-04-19 12:26:36 +0200288/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100289 * status
aPiecek874ea4d2021-04-19 12:26:36 +0200290 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100291
292/**
293 * @brief Status of the node.
294 *
aPiecek874ea4d2021-04-19 12:26:36 +0200295 * @see trp_print_status
aPiecek61d062b2020-11-02 11:05:09 +0100296 */
297typedef enum {
298 TRD_STATUS_TYPE_EMPTY = 0,
aPiecek874ea4d2021-04-19 12:26:36 +0200299 TRD_STATUS_TYPE_CURRENT, /**< ::LYS_STATUS_CURR */
300 TRD_STATUS_TYPE_DEPRECATED, /**< ::LYS_STATUS_DEPRC */
301 TRD_STATUS_TYPE_OBSOLETE /**< ::LYS_STATUS_OBSLT */
aPiecek61d062b2020-11-02 11:05:09 +0100302} trt_status_type;
303
aPiecek874ea4d2021-04-19 12:26:36 +0200304/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100305 * flags
aPiecek874ea4d2021-04-19 12:26:36 +0200306 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100307
308/**
309 * @brief Flag of the node.
310 *
aPiecek874ea4d2021-04-19 12:26:36 +0200311 * @see trp_print_flags, trp_get_flags_strlen
aPiecek61d062b2020-11-02 11:05:09 +0100312 */
313typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200314 TRD_FLAGS_TYPE_EMPTY = 0, /**< -- */
aPiecek61d062b2020-11-02 11:05:09 +0100315 TRD_FLAGS_TYPE_RW, /**< rw */
316 TRD_FLAGS_TYPE_RO, /**< ro */
317 TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */
318 TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */
319 TRD_FLAGS_TYPE_RPC, /**< -x */
320 TRD_FLAGS_TYPE_NOTIF, /**< -n */
321 TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */
322} trt_flags_type;
323
aPiecek874ea4d2021-04-19 12:26:36 +0200324/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100325 * node_name and opts
aPiecek874ea4d2021-04-19 12:26:36 +0200326 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100327
328#define TRD_NODE_NAME_PREFIX_CHOICE "("
329#define TRD_NODE_NAME_PREFIX_CASE ":("
330#define TRD_NODE_NAME_TRIPLE_DOT "..."
331
332/**
333 * @brief Type of the node.
334 *
aPiecek874ea4d2021-04-19 12:26:36 +0200335 * Used mainly to complete the correct \<opts\> next to or
336 * around the \<name\>.
aPiecek61d062b2020-11-02 11:05:09 +0100337 */
338typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200339 TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */
340 TRD_NODE_CASE, /**< For case node. :(\<name\>) */
341 TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */
342 TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */
343 TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */
344 TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */
345 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list (without keys). \<name\>* */
346 TRD_NODE_KEYS, /**< For a list's keys. \<name\>* [\<keys\>] */
347 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
348 TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */
349 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 +0100350} trt_node_type;
351
352/**
353 * @brief Type of node and his name.
354 *
aPiecek874ea4d2021-04-19 12:26:36 +0200355 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
aPiecek61d062b2020-11-02 11:05:09 +0100356 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
357 */
358struct trt_node_name {
359 trt_node_type type; /**< Type of the node relevant for printing. */
360 const char *module_prefix; /**< Prefix defined in the module where the node is defined. */
361 const char *str; /**< Name of the node. */
362};
363
364/**
365 * @brief Create struct trt_node_name as empty.
366 */
367#define TRP_EMPTY_NODE_NAME \
aPiecek874ea4d2021-04-19 12:26:36 +0200368 (struct trt_node_name) { \
369 .type = TRD_NODE_ELSE, .module_prefix = NULL, .str = NULL \
370 }
aPiecek61d062b2020-11-02 11:05:09 +0100371
372/**
373 * @brief Check if struct trt_node_name is empty.
374 */
375#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
376 !NODE_NAME.str
377
aPiecek874ea4d2021-04-19 12:26:36 +0200378/**
379 * @brief Every \<opts\> mark except string of list's keys
380 * has a length of one.
381 */
aPiecek61d062b2020-11-02 11:05:09 +0100382#define TRD_OPTS_MARK_LENGTH 1
383
aPiecek874ea4d2021-04-19 12:26:36 +0200384/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100385 * type
aPiecek874ea4d2021-04-19 12:26:36 +0200386 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100387
388/**
389 * @brief Type of the \<type\>
390 */
391typedef enum {
392 TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */
393 TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
aPiecek874ea4d2021-04-19 12:26:36 +0200394 TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm.
395 So set type as ::TRD_TYPE_TARGET. */
aPiecek61d062b2020-11-02 11:05:09 +0100396 TRD_TYPE_EMPTY /**< Type is not used at all. */
397} trt_type_type;
398
399/**
400 * @brief \<type\> in the \<node\>.
401 *
aPiecek874ea4d2021-04-19 12:26:36 +0200402 * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
aPiecek61d062b2020-11-02 11:05:09 +0100403 */
404struct trt_type {
405 trt_type_type type; /**< Type of the \<type\>. */
406 const char *str; /**< Path or name of the type. */
407};
408
409/**
410 * @brief Create empty struct trt_type.
411 */
412#define TRP_EMPTY_TRT_TYPE \
413 (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}
414
415/**
416 * @brief Check if struct trt_type is empty.
417 */
418#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
419 TYPE_OF_TYPE.type == TRD_TYPE_EMPTY
420
421/**
422 * @brief Initialize struct trt_type by parameters.
423 */
424#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
425 (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}
426
aPiecek874ea4d2021-04-19 12:26:36 +0200427/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100428 * node
aPiecek874ea4d2021-04-19 12:26:36 +0200429 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100430
431/**
432 * @brief \<node\> data for printing.
433 *
aPiecek874ea4d2021-04-19 12:26:36 +0200434 * It contains RFC's:
435 * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
aPiecek61d062b2020-11-02 11:05:09 +0100436 * Item \<opts\> is moved to part struct trt_node_name.
aPiecek874ea4d2021-04-19 12:26:36 +0200437 * For printing [\<keys\>] and if-features is required special
438 * functions which prints them.
aPiecek61d062b2020-11-02 11:05:09 +0100439 *
aPiecek874ea4d2021-04-19 12:26:36 +0200440 * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty,
441 * trp_print_node_up_to_name, trp_print_divided_node_up_to_name,
442 * trp_print_node
aPiecek61d062b2020-11-02 11:05:09 +0100443 */
444struct trt_node {
aPiecek874ea4d2021-04-19 12:26:36 +0200445 trt_status_type status; /**< \<status\>. */
446 trt_flags_type flags; /**< \<flags\>. */
447 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
448 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
449 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and
450 will be printed by trt_fp_print.print_features_names callback. */
451 ly_bool last_one; /**< Information about whether the node is the last. */
aPiecek61d062b2020-11-02 11:05:09 +0100452};
453
454/**
455 * @brief Create struct trt_node as empty.
456 */
457#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200458 (struct trt_node) { \
459 .status = TRD_STATUS_TYPE_EMPTY, \
460 .flags = TRD_FLAGS_TYPE_EMPTY, \
461 .name = TRP_EMPTY_NODE_NAME, \
462 .type = TRP_EMPTY_TRT_TYPE, \
463 .iffeatures = 0, \
464 .last_one = 1 \
465 }
aPiecek61d062b2020-11-02 11:05:09 +0100466
467/**
468 * @brief Package which only groups indent and node.
469 */
470struct trt_pair_indent_node {
471 struct trt_indent_in_node indent;
472 struct trt_node node;
473};
474
475/**
476 * @brief Initialize struct trt_pair_indent_node by parameters.
477 */
478#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200479 (struct trt_pair_indent_node) { \
480 .indent = INDENT_IN_NODE, .node = NODE \
481 }
aPiecek61d062b2020-11-02 11:05:09 +0100482
aPiecek874ea4d2021-04-19 12:26:36 +0200483/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100484 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200485 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100486
487#define TRD_TOP_KEYWORD_MODULE "module"
488#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
489
490#define TRD_BODY_KEYWORD_AUGMENT "augment"
491#define TRD_BODY_KEYWORD_RPC "rpcs"
492#define TRD_BODY_KEYWORD_NOTIF "notifications"
493#define TRD_BODY_KEYWORD_GROUPING "grouping"
494#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
495
496/**
497 * @brief Type of the trt_keyword.
498 */
499typedef enum {
500 TRD_KEYWORD_EMPTY = 0,
501 TRD_KEYWORD_MODULE,
502 TRD_KEYWORD_SUBMODULE,
503 TRD_KEYWORD_AUGMENT,
504 TRD_KEYWORD_RPC,
505 TRD_KEYWORD_NOTIF,
506 TRD_KEYWORD_GROUPING,
507 TRD_KEYWORD_YANG_DATA
508} trt_keyword_type;
509
510/**
511 * @brief Main sign of the tree nodes.
512 *
aPiecek874ea4d2021-04-19 12:26:36 +0200513 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100514 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
515 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
516 * trp_keyword_type_strlen
517 *
518 */
519struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200520 trt_keyword_type type; /**< String containing some of the top or body keyword. */
521 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100522};
523
524/**
525 * @brief Create struct trt_keyword_stmt as empty.
526 */
527#define TRP_EMPTY_KEYWORD_STMT \
528 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
529
530/**
531 * @brief Check if struct trt_keyword_stmt is empty.
532 */
533#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
534 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
535
536/**
537 * @brief Initialize struct trt_keyword_stmt by parameters.
538 */
539#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
540 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
541
aPiecek874ea4d2021-04-19 12:26:36 +0200542/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100543 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200544 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100545
546struct trt_parent_cache;
547
548/**
549 * @brief Functions that change the state of the tree_ctx structure.
550 *
aPiecek3f247652021-04-19 13:40:25 +0200551 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200552 * for the 'trp' printing functions and are also called from the
553 * 'trb' browsing functions when walking through a tree. These callback
554 * functions need to be checked or reformulated if changes to the
555 * libyang library affect the printing tree. For all, if the value
556 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
557 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100558 */
559struct trt_fp_modify_ctx {
560 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
561 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
562 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
563 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 +0100564};
565
aPiecek874ea4d2021-04-19 12:26:36 +0200566/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100567 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200568 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100569
570/**
571 * @brief Functions that do not change the state of the tree_structure.
572 *
573 * For details see trt_fp_modify_ctx.
574 */
575struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200576 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
577 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
578 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100579};
580
aPiecek874ea4d2021-04-19 12:26:36 +0200581/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100582 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200583 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100584
585/**
aPiecek874ea4d2021-04-19 12:26:36 +0200586 * @brief A set of all necessary functions that must be provided
587 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100588 */
589struct trt_fp_all {
590 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
591 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
592 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
593};
594
aPiecek874ea4d2021-04-19 12:26:36 +0200595/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100596 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200597 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100598
599/**
aPiecek01598c02021-04-23 14:18:24 +0200600 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100601 */
602struct trt_printer_ctx {
603 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200604 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100605 size_t max_line_length; /**< The maximum number of characters that can be
606 printed on one line, including the last. */
607};
608
aPiecek874ea4d2021-04-19 12:26:36 +0200609/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100610 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200611 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100612
613/**
614 * @brief The name of the section to which the node belongs.
615 */
616typedef enum {
617 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
618 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
619 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
620 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
621 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
622 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
623} trt_actual_section;
624
625/**
626 * @brief Types of nodes that have some effect on their children.
627 */
628typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200629 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
630 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
631 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
632 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100633} trt_ancestor_type;
634
635/**
636 * @brief Saved information when browsing the tree downwards.
637 *
aPiecek874ea4d2021-04-19 12:26:36 +0200638 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200639 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200640 * this structures during their recursive calls. This functions do not
641 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200642 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200643 * structure, can return a node with the correct data. The word
644 * \b parent is in the structure name, because this data refers to
645 * the last parent and at the same time the states of its
646 * ancestors data. Only the function jumping on the child
647 * (next_child(...)) creates this structure, because the pointer
648 * to the current node moves down the tree. It's like passing
649 * the genetic code to children. Some data must be inherited and
650 * there are two approaches to this problem. Either it will always
651 * be determined which inheritance states belong to the current node
652 * (which can lead to regular travel to the root node) or
653 * the inheritance states will be stored during the recursive calls.
654 * So the problem was solved by the second option. Why does
655 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200656 * the lysp tree. For walks through the lysc tree is trt_parent_cache
657 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100658 *
aPiecek874ea4d2021-04-19 12:26:36 +0200659 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100660 */
661struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200662 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
663 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
664 uint16_t lys_config; /**< Inherited config W or R. */
665 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100666};
667
668/**
669 * @brief Return trt_parent_cache filled with default values.
670 */
671#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200672 (struct trt_parent_cache) { \
673 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
674 .lys_config = LYS_CONFIG_W, .last_list = NULL \
675 }
aPiecek61d062b2020-11-02 11:05:09 +0100676
677/**
678 * @brief Main structure for browsing the libyang tree
679 */
680struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200681 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
682 It is assumed that once set, it does not change.
683 If it is true then trt_tree_ctx.pn and
684 trt_tree_ctx.tpn are not used.
685 If it is false then trt_tree_ctx.cn is not used. */
686 trt_actual_section section; /**< To which section pn points. */
687 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
688 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
689 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
690 union {
691 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
692 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
693 is set to TRD_SECT_YANG_DATA. */
694 };
695 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
aPiecek61d062b2020-11-02 11:05:09 +0100696};
697
aPiecek3f247652021-04-19 13:40:25 +0200698/**
699 * @brief Get lysp_node from trt_tree_ctx.cn.
700 */
701#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
702 ((const struct lysp_node *)CN->priv)
703
aPiecek01598c02021-04-23 14:18:24 +0200704/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100705typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
706
aPiecekef1e58e2021-04-19 13:19:44 +0200707/**
708 * @brief Simple getter functions for lysp and lysc nodes.
709 *
710 * This structure is useful if we have a general algorithm
711 * (tro function) that can be used for both lysc and lysp nodes.
712 * Thanks to this structure, we prevent code redundancy.
713 * We don't have to write basically the same algorithm twice
714 * for lysp and lysc trees.
715 */
716struct tro_getters
717{
718 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
719 const void *(*next)(const void *); /**< Get sibling. */
720 const void *(*parent)(const void *); /**< Get parent. */
721 const void *(*child)(const void *); /**< Get child. */
722 const void *(*actions)(const void *); /**< Get actions. */
723 const void *(*action_input)(const void *); /**< Get input action from action node. */
724 const void *(*action_output)(const void *); /**< Get output action from action node. */
725 const void *(*notifs)(const void *); /**< Get notifs. */
726};
727
aPiecek874ea4d2021-04-19 12:26:36 +0200728/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100729 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200730 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100731
732/**
733 * @brief Print a substring but limited to the maximum length.
734 * @param[in] str is pointer to source.
735 * @param[in] len is number of characters to be printed.
736 * @param[in,out] out is output handler.
737 * @return str parameter shifted by len.
738 */
739static const char *
740trg_print_substr(const char *str, size_t len, struct ly_out *out)
741{
742 for (size_t i = 0; i < len; i++) {
743 ly_print_(out, "%c", str[0]);
744 str++;
745 }
746 return str;
747}
748
749/**
750 * @brief Pointer is not NULL and does not point to an empty string.
751 * @param[in] str is pointer to string to be checked.
752 * @return 1 if str pointing to non empty string otherwise 0.
753 */
754static ly_bool
755trg_charptr_has_data(const char *str)
756{
757 return (str) && (str[0] != '\0');
758}
759
760/**
aPiecek874ea4d2021-04-19 12:26:36 +0200761 * @brief Check if @p word in @p src is present where words are
762 * delimited by @p delim.
763 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100764 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200765 * @param[in] delim is delimiter between @p words in @p src.
766 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100767 */
768static ly_bool
769trg_word_is_present(const char *src, const char *word, char delim)
770{
771 const char *hit;
772
773 if ((!src) || (src[0] == '\0') || (!word)) {
774 return 0;
775 }
776
777 hit = strstr(src, word);
778
779 if (hit) {
780 /* word was founded at the begin of src
781 * OR it match somewhere after delim
782 */
783 if ((hit == src) || (hit[-1] == delim)) {
784 /* end of word was founded at the end of src
785 * OR end of word was match somewhere before delim
786 */
787 char delim_or_end = (hit + strlen(word))[0];
788 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
789 return 1;
790 }
791 }
792 /* after -> hit is just substr and it's not the whole word */
793 /* jump to the next word */
794 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
795 /* skip delim */
796 src = src[0] == '\0' ? src : src + 1;
797 /* continue with searching */
798 return trg_word_is_present(src, word, delim);
799 } else {
800 return 0;
801 }
802}
803
aPiecek874ea4d2021-04-19 12:26:36 +0200804/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100805 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200806 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100807
808/**
aPiecek01598c02021-04-23 14:18:24 +0200809 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100810 *
aPiecek874ea4d2021-04-19 12:26:36 +0200811 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100812 * @param[in] buf contains input characters
813 * @param[in] count is number of characters in buf.
814 * @return Number of printed bytes.
815 * @return Negative value in case of error.
816 */
817static ssize_t
818trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
819{
820 LY_ERR erc = LY_SUCCESS;
821 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
822
823 switch (data->mode) {
824 case TRD_PRINT:
825 erc = ly_write_(data->out, buf, count);
826 break;
827 case TRD_CHAR_COUNT:
828 data->counter = data->counter + count;
829 break;
830 default:
831 break;
832 }
833
834 if (erc != LY_SUCCESS) {
835 data->last_error = erc;
836 return -1;
837 } else {
838 return count;
839 }
840}
841
842/**
843 * @brief Check that indent in node can be considered as equivalent.
844 * @param[in] first is the first indent in node.
845 * @param[in] second is the second indent in node.
846 * @return 1 if indents are equivalent otherwise 0.
847 */
848static ly_bool
849trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
850{
851 const ly_bool a = first.type == second.type;
852 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
853 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
854 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
855
856 return a && b && c && d;
857}
858
859/**
aPiecek874ea4d2021-04-19 12:26:36 +0200860 * @brief Setting space character because node is last sibling.
861 * @param[in] wr is wrapper over which the shift operation
862 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100863 * @return New shifted wrapper.
864 */
865static struct trt_wrapper
866trp_wrapper_set_shift(struct trt_wrapper wr)
867{
868 assert(wr.actual_pos < 64);
869 /* +--<node>
870 * +--<node>
871 */
872 wr.actual_pos++;
873 return wr;
874}
875
876/**
aPiecek874ea4d2021-04-19 12:26:36 +0200877 * @brief Setting '|' symbol because node is divided or
878 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100879 * @param[in] wr is source of wrapper.
880 * @return New wrapper which is marked at actual position and shifted.
881 */
882static struct trt_wrapper
883trp_wrapper_set_mark(struct trt_wrapper wr)
884{
885 assert(wr.actual_pos < 64);
886 wr.bit_marks1 |= 1U << wr.actual_pos;
887 return trp_wrapper_set_shift(wr);
888}
889
890/**
891 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
892 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200893 * @param[in] last_one is flag. Value 1 saying if the node is the last
894 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100895 * @return New wrapper for the actual node.
896 */
897static struct trt_wrapper
898trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
899{
900 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
901}
902
903/**
904 * @brief Test if the wrappers are equivalent.
905 * @param[in] first is the first wrapper.
906 * @param[in] second is the second wrapper.
907 * @return 1 if the wrappers are equivalent otherwise 0.
908 */
909static ly_bool
910trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
911{
912 const ly_bool a = first.type == second.type;
913 const ly_bool b = first.bit_marks1 == second.bit_marks1;
914 const ly_bool c = first.actual_pos == second.actual_pos;
915
916 return a && b && c;
917}
918
919/**
920 * @brief Print " | " sequence on line.
921 * @param[in] wr is wrapper to be printed.
922 * @param[in,out] out is output handler.
923 */
924static void
925trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
926{
927 uint32_t lb;
928
929 if (wr.type == TRD_WRAPPER_TOP) {
930 lb = TRD_INDENT_LINE_BEGIN;
931 } else if (wr.type == TRD_WRAPPER_BODY) {
932 lb = TRD_INDENT_LINE_BEGIN * 2;
933 } else {
934 lb = TRD_INDENT_LINE_BEGIN;
935 }
936
937 ly_print_(out, "%*c", lb, ' ');
938
939 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
940 return;
941 }
942
943 for (uint32_t i = 0; i < wr.actual_pos; i++) {
944 /** Test if the bit on the index is set. */
945 if ((wr.bit_marks1 >> i) & 1U) {
946 ly_print_(out, "|");
947 } else {
948 ly_print_(out, " ");
949 }
950
951 if (i != wr.actual_pos) {
952 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
953 }
954 }
955}
956
957/**
958 * @brief Check if struct trt_node is empty.
959 * @param[in] node is item to test.
960 * @return 1 if node is considered empty otherwise 0.
961 */
962static ly_bool
963trp_node_is_empty(struct trt_node node)
964{
965 const ly_bool a = !node.iffeatures;
966 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
967 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
968 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
969 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
970
971 return a && b && c && d && e;
972}
973
974/**
aPiecek874ea4d2021-04-19 12:26:36 +0200975 * @brief Check if [\<keys\>], \<type\> and
976 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +0100977 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +0200978 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
979 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100980 */
981static ly_bool
982trp_node_body_is_empty(struct trt_node node)
983{
984 const ly_bool a = !node.iffeatures;
985 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
986 const ly_bool c = node.name.type != TRD_NODE_KEYS;
987
988 return a && b && c;
989}
990
991/**
992 * @brief Print \<status\> of the node.
993 * @param[in] status_type is type of status.
994 * @param[in,out] out is output handler.
995 */
996static void
997trp_print_status(trt_status_type status_type, struct ly_out *out)
998{
999 switch (status_type) {
1000 case TRD_STATUS_TYPE_CURRENT:
1001 ly_print_(out, "%c", '+');
1002 break;
1003 case TRD_STATUS_TYPE_DEPRECATED:
1004 ly_print_(out, "%c", 'x');
1005 break;
1006 case TRD_STATUS_TYPE_OBSOLETE:
1007 ly_print_(out, "%c", 'o');
1008 break;
1009 default:
1010 break;
1011 }
1012}
1013
1014/**
1015 * @brief Print \<flags\>.
1016 * @param[in] flags_type is type of \<flags\>.
1017 * @param[in,out] out is output handler.
1018 */
1019static void
1020trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1021{
1022 switch (flags_type) {
1023 case TRD_FLAGS_TYPE_RW:
1024 ly_print_(out, "%s", "rw");
1025 break;
1026 case TRD_FLAGS_TYPE_RO:
1027 ly_print_(out, "%s", "ro");
1028 break;
1029 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1030 ly_print_(out, "%s", "-w");
1031 break;
1032 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1033 ly_print_(out, "%s", "-u");
1034 break;
1035 case TRD_FLAGS_TYPE_RPC:
1036 ly_print_(out, "%s", "-x");
1037 break;
1038 case TRD_FLAGS_TYPE_NOTIF:
1039 ly_print_(out, "%s", "-n");
1040 break;
1041 case TRD_FLAGS_TYPE_MOUNT_POINT:
1042 ly_print_(out, "%s", "mp");
1043 break;
1044 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001045 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001046 break;
1047 }
1048}
1049
1050/**
1051 * @brief Get size of the \<flags\>.
1052 * @param[in] flags_type is type of \<flags\>.
1053 * @return 0 if flags_type is not set otherwise 2.
1054 */
1055static size_t
1056trp_get_flags_strlen(trt_flags_type flags_type)
1057{
1058 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1059}
1060
1061/**
1062 * @brief Print entire struct trt_node_name structure.
1063 * @param[in] node_name is item to print.
1064 * @param[in,out] out is output handler.
1065 */
1066static void
1067trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1068{
1069 const char *mod_prefix;
1070 const char *colon;
1071 const char trd_node_name_suffix_choice[] = ")";
1072 const char trd_node_name_suffix_case[] = ")";
1073 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1074 const char trd_opts_container[] = "!"; /**< For a presence container. */
1075 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1076 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1077 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1078
1079 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1080 return;
1081 }
1082
1083 if (node_name.module_prefix) {
1084 mod_prefix = node_name.module_prefix;
1085 colon = ":";
1086 } else {
1087 mod_prefix = "";
1088 colon = "";
1089 }
1090
1091 switch (node_name.type) {
1092 case TRD_NODE_ELSE:
1093 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1094 break;
1095 case TRD_NODE_CASE:
1096 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1097 break;
1098 case TRD_NODE_CHOICE:
1099 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1100 break;
1101 case TRD_NODE_OPTIONAL_CHOICE:
1102 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);
1103 break;
1104 case TRD_NODE_OPTIONAL:
1105 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1106 break;
1107 case TRD_NODE_CONTAINER:
1108 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1109 break;
1110 case TRD_NODE_LISTLEAFLIST:
1111 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1112 break;
1113 case TRD_NODE_KEYS:
1114 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1115 break;
1116 case TRD_NODE_TOP_LEVEL1:
1117 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1118 break;
1119 case TRD_NODE_TOP_LEVEL2:
1120 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1121 break;
1122 case TRD_NODE_TRIPLE_DOT:
1123 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1124 break;
1125 default:
1126 break;
1127 }
1128}
1129
1130/**
aPiecek874ea4d2021-04-19 12:26:36 +02001131 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1132 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001133 * @param[in] node_name is structure containing the 'mark'.
1134 * @return 1 if contain otherwise 0.
1135 */
1136static ly_bool
1137trp_mark_is_used(struct trt_node_name node_name)
1138{
1139 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1140 return 0;
1141 }
1142
1143 switch (node_name.type) {
1144 case TRD_NODE_ELSE:
1145 case TRD_NODE_CASE:
1146 case TRD_NODE_KEYS:
1147 return 0;
1148 default:
1149 return 1;
1150 }
1151}
1152
1153/**
1154 * @brief Print opts keys.
1155 * @param[in] node_name contains type of the node with his name.
1156 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001157 * @param[in] cf is basically a pointer to the function that prints
1158 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001159 * @param[in,out] out is output handler.
1160 */
1161static void
1162trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1163{
1164 if (node_name.type != TRD_NODE_KEYS) {
1165 return;
1166 }
1167
1168 /* <name><mark>___<keys>*/
1169 if (btw_name_opts > 0) {
1170 ly_print_(out, "%*c", btw_name_opts, ' ');
1171 }
1172 ly_print_(out, "[");
1173 cf.pf(cf.ctx, out);
1174 ly_print_(out, "]");
1175}
1176
1177/**
1178 * @brief Print entire struct trt_type structure.
1179 * @param[in] type is item to print.
1180 * @param[in,out] out is output handler.
1181 */
1182static void
1183trp_print_type(struct trt_type type, struct ly_out *out)
1184{
1185 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1186 return;
1187 }
1188
1189 switch (type.type) {
1190 case TRD_TYPE_NAME:
1191 ly_print_(out, "%s", type.str);
1192 break;
1193 case TRD_TYPE_TARGET:
1194 ly_print_(out, "-> %s", type.str);
1195 break;
1196 case TRD_TYPE_LEAFREF:
1197 ly_print_(out, "leafref");
1198 default:
1199 break;
1200 }
1201}
1202
1203/**
1204 * @brief Print all iffeatures of node
1205 *
1206 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001207 * @param[in] cf is basically a pointer to the function that prints
1208 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001209 * @param[in,out] out is output handler.
1210 */
1211static void
1212trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1213{
1214 if (iffeature_flag) {
1215 ly_print_(out, "{");
1216 cf.pf(cf.ctx, out);
1217 ly_print_(out, "}?");
1218 }
1219}
1220
1221/**
1222 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1223 * @param[in] node contains items to print.
1224 * @param[in] out is output handler.
1225 */
1226static void
1227trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1228{
1229 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1230 trp_print_node_name(node.name, out);
1231 return;
1232 }
1233 /* <status>--<flags> */
1234 trp_print_status(node.status, out);
1235 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001236 /* If the node is a case node, there is no space before the <name>
1237 * also case node has no flags.
1238 */
aPiecek61d062b2020-11-02 11:05:09 +01001239 if (node.name.type != TRD_NODE_CASE) {
1240 trp_print_flags(node.flags, out);
1241 ly_print_(out, " ");
1242 }
1243 /* <name> */
1244 trp_print_node_name(node.name, out);
1245}
1246
1247/**
aPiecek874ea4d2021-04-19 12:26:36 +02001248 * @brief Print alignment (spaces) instead of
1249 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001250 * @param[in] node contains items to print.
1251 * @param[in] out is output handler.
1252 */
1253static void
1254trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1255{
1256 uint32_t space = trp_get_flags_strlen(node.flags);
1257
1258 if (node.name.type == TRD_NODE_CASE) {
1259 /* :(<name> */
1260 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1261 } else if (node.name.type == TRD_NODE_CHOICE) {
1262 /* (<name> */
1263 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1264 } else {
1265 /* _<name> */
1266 space += strlen(" ");
1267 }
1268
1269 /* <name>
1270 * __
1271 */
1272 space += TRD_INDENT_LONG_LINE_BREAK;
1273
1274 ly_print_(out, "%*c", space, ' ');
1275}
1276
1277/**
1278 * @brief Print struct trt_node structure.
1279 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001280 * @param[in] pck package of functions for
1281 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001282 * @param[in] indent is the indent in node.
1283 * @param[in,out] out is output handler.
1284 */
1285static void
1286trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1287{
1288 ly_bool triple_dot;
1289 ly_bool divided;
1290 struct trt_cf_print cf_print_keys;
1291 struct trt_cf_print cf_print_iffeatures;
1292
1293 if (trp_node_is_empty(node)) {
1294 return;
1295 }
1296
1297 /* <status>--<flags> <name><opts> <type> <if-features> */
1298 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1299 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1300
1301 if (triple_dot) {
1302 trp_print_node_name(node.name, out);
1303 return;
1304 } else if (!divided) {
1305 trp_print_node_up_to_name(node, out);
1306 } else {
1307 trp_print_divided_node_up_to_name(node, out);
1308 }
1309
1310 /* <opts> */
1311 /* <name>___<opts>*/
1312 cf_print_keys.ctx = pck.tree_ctx;
1313 cf_print_keys.pf = pck.fps.print_keys;
1314
1315 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1316
1317 /* <opts>__<type> */
1318 if (indent.btw_opts_type > 0) {
1319 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1320 }
1321
1322 /* <type> */
1323 trp_print_type(node.type, out);
1324
1325 /* <type>__<iffeatures> */
1326 if (indent.btw_type_iffeatures > 0) {
1327 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1328 }
1329
1330 /* <iffeatures> */
1331 cf_print_iffeatures.ctx = pck.tree_ctx;
1332 cf_print_iffeatures.pf = pck.fps.print_features_names;
1333
1334 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1335}
1336
1337/**
aPiecek874ea4d2021-04-19 12:26:36 +02001338 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001339 * @param[in] ks is keyword statement to print.
1340 * @param[in,out] out is output handler
1341 */
1342static void
1343trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1344{
1345 switch (ks.type) {
1346 case TRD_KEYWORD_MODULE:
1347 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1348 return;
1349 case TRD_KEYWORD_SUBMODULE:
1350 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1351 return;
1352 default:
1353 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1354 switch (ks.type) {
1355 case TRD_KEYWORD_AUGMENT:
1356 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1357 break;
1358 case TRD_KEYWORD_RPC:
1359 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1360 break;
1361 case TRD_KEYWORD_NOTIF:
1362 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1363 break;
1364 case TRD_KEYWORD_GROUPING:
1365 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1366 break;
1367 case TRD_KEYWORD_YANG_DATA:
1368 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1369 break;
1370 default:
1371 break;
1372 }
1373 break;
1374 }
1375}
1376
1377/**
1378 * @brief Get string length of stored keyword.
1379 * @param[in] type is type of the keyword statement.
1380 * @return length of the keyword statement name.
1381 */
1382static size_t
1383trp_keyword_type_strlen(trt_keyword_type type)
1384{
1385 switch (type) {
1386 case TRD_KEYWORD_MODULE:
1387 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1388 case TRD_KEYWORD_SUBMODULE:
1389 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1390 case TRD_KEYWORD_AUGMENT:
1391 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1392 case TRD_KEYWORD_RPC:
1393 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1394 case TRD_KEYWORD_NOTIF:
1395 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1396 case TRD_KEYWORD_GROUPING:
1397 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1398 case TRD_KEYWORD_YANG_DATA:
1399 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1400 default:
1401 return 0;
1402 }
1403}
1404
1405/**
aPiecek874ea4d2021-04-19 12:26:36 +02001406 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001407 * @param[in] ks is keyword statement structure.
1408 * @param[in] mll is max line length.
1409 * @param[in,out] out is output handler.
1410 */
1411static void
1412trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1413{
1414 uint32_t ind_initial;
1415 uint32_t ind_divided;
1416 /* flag if path must be splitted to more lines */
1417 ly_bool linebreak_was_set;
1418 /* flag if at least one subpath was printed */
1419 ly_bool subpath_printed;
1420 /* the sum of the sizes of the substrings on the current line */
1421 uint32_t how_far;
1422 /* pointer to start of the subpath */
1423 const char *sub_ptr;
1424 /* size of subpath from sub_ptr */
1425 size_t sub_len;
1426
1427 if ((!ks.str) || (ks.str[0] == '\0')) {
1428 return;
1429 }
1430
1431 /* module name cannot be splitted */
1432 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1433 ly_print_(out, "%s", ks.str);
1434 return;
1435 }
1436
1437 /* after -> for trd_keyword_stmt_body do */
1438
1439 /* set begin indentation */
1440 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1441 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1442 linebreak_was_set = 0;
1443 subpath_printed = 0;
1444 how_far = 0;
1445 sub_ptr = ks.str;
1446 sub_len = 0;
1447
1448 while (sub_ptr[0] != '\0') {
1449 uint32_t ind;
1450 /* skip slash */
1451 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
1452 /* get position of the end of substr */
1453 tmp = strchr(tmp, '/');
1454 /* set correct size if this is a last substring */
1455 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1456 /* actualize sum of the substring's sizes on the current line */
1457 how_far += sub_len;
1458 /* correction due to colon character if it this is last substring */
1459 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1460 /* choose indentation which depends on
1461 * whether the string is printed on multiple lines or not
1462 */
1463 ind = linebreak_was_set ? ind_divided : ind_initial;
1464 if (ind + how_far <= mll) {
1465 /* printing before max line length */
1466 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1467 subpath_printed = 1;
1468 } else {
1469 /* printing on new line */
1470 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001471 /* first subpath is too long
1472 * but print it at first line anyway
1473 */
aPiecek61d062b2020-11-02 11:05:09 +01001474 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1475 subpath_printed = 1;
1476 continue;
1477 }
1478 ly_print_(out, "\n");
1479 ly_print_(out, "%*c", ind_divided, ' ');
1480 linebreak_was_set = 1;
1481 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1482 how_far = sub_len;
1483 subpath_printed = 1;
1484 }
1485 }
1486}
1487
1488/**
aPiecek874ea4d2021-04-19 12:26:36 +02001489 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001490 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001491 * @param[in] grp_has_data is flag only for grouping section.
1492 * Set to 1 if grouping section has some nodes.
1493 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001494 * @param[in,out] out is output handler.
1495 */
1496static void
aPiecekdc8fd572021-04-19 10:47:23 +02001497trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001498{
1499 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001500 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1501 return;
1502 } else {
1503 ly_print_(out, ":");
1504 }
aPiecek61d062b2020-11-02 11:05:09 +01001505 }
1506}
1507
1508/**
1509 * @brief Print entire struct trt_keyword_stmt structure.
1510 * @param[in] ks is item to print.
1511 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001512 * @param[in] grp_has_data is flag only for grouping section.
1513 * Set to 1 if grouping section has some nodes.
1514 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001515 * @param[in,out] out is output handler.
1516 */
1517static void
aPiecek874ea4d2021-04-19 12:26:36 +02001518trp_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 +01001519{
1520 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1521 return;
1522 }
1523 trt_print_keyword_stmt_begin(ks, out);
1524 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001525 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001526}
1527
aPiecek874ea4d2021-04-19 12:26:36 +02001528/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001529 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001530 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001531
1532/**
aPiecek874ea4d2021-04-19 12:26:36 +02001533 * @brief Printing one line including wrapper and node
1534 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001535 * @param[in] node is \<node\> representation.
1536 * @param[in] pck contains special printing functions callback.
1537 * @param[in] indent contains wrapper and indent in node numbers.
1538 * @param[in,out] out is output handler.
1539 */
1540static void
1541trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1542{
1543 trp_print_wrapper(indent.wrapper, out);
1544 trp_print_node(node, pck, indent.in_node, out);
1545}
1546
1547/**
aPiecek874ea4d2021-04-19 12:26:36 +02001548 * @brief Printing one line including wrapper and
1549 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001550 * @param[in] node is \<node\> representation.
1551 * @param[in] wr is wrapper for printing indentation before node.
1552 * @param[in] out is output handler.
1553 */
1554static void
1555trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1556{
1557 trp_print_wrapper(wr, out);
1558 trp_print_node_up_to_name(node, out);
1559}
1560
1561/**
aPiecek874ea4d2021-04-19 12:26:36 +02001562 * @brief Check if leafref target must be change to string 'leafref'
1563 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001564 * @param[in] node containing leafref target.
1565 * @param[in] wr is wrapper for printing indentation before node.
1566 * @param[in] mll is max line length.
1567 * @param[in] out is output handler.
1568 * @return true if leafref must be changed to string 'leafref'.
1569 */
1570static ly_bool
1571trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1572{
1573 struct ly_out_clb_arg *data;
1574
1575 if (node.type.type != TRD_TYPE_TARGET) {
1576 return 0;
1577 }
1578
1579 /* set ly_out to counting characters */
1580 data = out->method.clb.arg;
1581
1582 data->counter = 0;
1583 data->mode = TRD_CHAR_COUNT;
1584 /* count number of printed bytes */
1585 trp_print_wrapper(wr, out);
1586 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1587 trp_print_divided_node_up_to_name(node, out);
1588 data->mode = TRD_PRINT;
1589
1590 return data->counter + strlen(node.type.str) > mll;
1591}
1592
1593/**
1594 * @brief Get default indent in node based on node values.
1595 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001596 * @return Default indent in node assuming that the node
1597 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001598 */
1599static struct trt_indent_in_node
1600trp_default_indent_in_node(struct trt_node node)
1601{
1602 struct trt_indent_in_node ret;
1603
1604 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1605
1606 /* btw_name_opts */
1607 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1608
1609 /* btw_opts_type */
1610 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1611 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1612 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1613 TRD_INDENT_BEFORE_TYPE;
1614 } else {
1615 ret.btw_opts_type = 0;
1616 }
1617
1618 /* btw_type_iffeatures */
1619 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1620
1621 return ret;
1622}
1623
1624/**
1625 * @brief Setting linebreaks in trt_indent_in_node.
1626 *
1627 * The order where the linebreak tag can be placed is from the end.
1628 *
aPiecek874ea4d2021-04-19 12:26:36 +02001629 * @param[in] indent containing alignment lengths
1630 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001631 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001632 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1633 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001634 */
1635static struct trt_indent_in_node
1636trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1637{
1638 /* somewhere must be set a line break in node */
1639 struct trt_indent_in_node ret = indent;
1640
1641 /* gradually break the node from the end */
1642 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1643 ret.btw_type_iffeatures = TRD_LINEBREAK;
1644 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1645 ret.btw_opts_type = TRD_LINEBREAK;
1646 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1647 /* set line break between name and opts */
1648 ret.btw_name_opts = TRD_LINEBREAK;
1649 } else {
1650 /* it is not possible to place a more line breaks,
1651 * unfortunately the max_line_length constraint is violated
1652 */
1653 ret.type = TRD_INDENT_IN_NODE_FAILED;
1654 }
1655 return ret;
1656}
1657
1658/**
1659 * @brief Get the first half of the node based on the linebreak mark.
1660 *
1661 * Items in the second half of the node will be empty.
1662 *
1663 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001664 * @param[in] indent contains information in which part of the \<node\>
1665 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001666 * @return first half of the node, indent is unchanged.
1667 */
1668static struct trt_pair_indent_node
1669trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1670{
1671 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1672
1673 if (indent.btw_name_opts == TRD_LINEBREAK) {
1674 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1675 ret.node.type = TRP_EMPTY_TRT_TYPE;
1676 ret.node.iffeatures = 0;
1677 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1678 ret.node.type = TRP_EMPTY_TRT_TYPE;
1679 ret.node.iffeatures = 0;
1680 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1681 ret.node.iffeatures = 0;
1682 }
1683
1684 return ret;
1685}
1686
1687/**
1688 * @brief Get the second half of the node based on the linebreak mark.
1689 *
1690 * Items in the first half of the node will be empty.
1691 * Indentations belonging to the first node will be reset to zero.
1692 *
1693 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001694 * @param[in] indent contains information in which part of the \<node\>
1695 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001696 * @return second half of the node, indent is newly set.
1697 */
1698static struct trt_pair_indent_node
1699trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1700{
1701 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1702
1703 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001704 /* Logically, the information up to token <opts> should
1705 * be deleted, but the the trp_print_node function needs it to
1706 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001707 */
1708 ret.indent.btw_name_opts = 0;
1709 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1710 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1711 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1712 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1713 ret.indent.btw_name_opts = 0;
1714 ret.indent.btw_opts_type = 0;
1715 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1716 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1717 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1718 ret.node.type = TRP_EMPTY_TRT_TYPE;
1719 ret.indent.btw_name_opts = 0;
1720 ret.indent.btw_opts_type = 0;
1721 ret.indent.btw_type_iffeatures = 0;
1722 }
1723 return ret;
1724}
1725
1726/**
1727 * @brief Get the correct alignment for the node.
1728 *
aPiecek874ea4d2021-04-19 12:26:36 +02001729 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001730 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001731 *
1732 * @param[in] node is \<node\> representation.
1733 * @param[in] pck contains speciall callback functions for printing.
1734 * @param[in] indent contains wrapper and indent in node numbers.
1735 * @param[in] mll is max line length.
1736 * @param[in,out] cnt counting number of characters to print.
1737 * @param[in,out] out is output handler.
1738 * @return pair of node and indentation numbers of that node.
1739 */
1740static struct trt_pair_indent_node
1741trp_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)
1742{
1743 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1744
1745 trp_print_line(node, pck, indent, out);
1746
1747 if (*cnt <= mll) {
1748 /* success */
1749 return ret;
1750 } else {
1751 ret.indent = trp_indent_in_node_place_break(ret.indent);
1752 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1753 /* erase information in node due to line break */
1754 ret = trp_first_half_node(node, ret.indent);
1755 /* check if line fits, recursive call */
1756 *cnt = 0;
1757 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1758 /* make sure that the result will be with the status divided
1759 * or eventually with status failed */
1760 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1761 }
1762 return ret;
1763 }
1764}
1765
1766/**
1767 * @brief Get the correct alignment for the node.
1768 *
1769 * @param[in] node is \<node\> representation.
1770 * @param[in] pck contains speciall callback functions for printing.
1771 * @param[in] indent contains wrapper and indent in node numbers.
1772 * @param[in] mll is max line length.
1773 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001774 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1775 * line, some indent variable has negative value as a line break sign.
1776 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1777 * all indent variables values has non-negative number.
1778 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1779 * line, all indent variables has negative or zero values,
1780 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001781 */
1782static struct trt_pair_indent_node
1783trp_try_normal_indent_in_node(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, struct ly_out *out)
1784{
1785 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1786 struct ly_out_clb_arg *data;
1787
1788 /* set ly_out to counting characters */
1789 data = out->method.clb.arg;
1790
1791 data->counter = 0;
1792 data->mode = TRD_CHAR_COUNT;
1793 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1794 data->mode = TRD_PRINT;
1795
1796 return ret;
1797}
1798
1799/**
aPiecek01598c02021-04-23 14:18:24 +02001800 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001801 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001802 * @param[in] node is node representation.
1803 * @param[in] ppck contains speciall callback functions for printing.
1804 * @param[in] ipck contains wrapper and indent in node numbers.
1805 * @param[in] mll is max line length.
1806 * @param[in,out] out is output handler.
1807 */
1808static void
1809trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1810{
1811 ly_bool entire_node_was_printed;
1812 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1813
1814 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1815 /* nothing can be done, continue as usual */
1816 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1817 }
1818
1819 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1820 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1821
1822 if (!entire_node_was_printed) {
1823 ly_print_(out, "\n");
1824 /* continue with second half node */
1825 ind_node = trp_second_half_node(node, ind_node.indent);
1826 /* continue with printing node */
1827 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1828 } else {
1829 return;
1830 }
1831}
1832
1833/**
aPiecek874ea4d2021-04-19 12:26:36 +02001834 * @brief Printing of the wrapper and the whole node,
1835 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001836 * @param[in] node is node representation.
1837 * @param[in] ppck contains speciall callback functions for printing.
1838 * @param[in] ipck contains wrapper and indent in node numbers.
1839 * @param[in] mll is max line length.
1840 * @param[in,out] out is output handler.
1841 */
1842static void
1843trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1844{
1845 struct trt_pair_indent_node ind_node1;
1846 struct trt_pair_indent_node ind_node2;
1847 struct trt_pck_indent tmp;
1848
1849 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1850 node.type.type = TRD_TYPE_LEAFREF;
1851 }
1852
1853 /* check if normal indent is possible */
1854 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1855
1856 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1857 /* node fits to one line */
1858 trp_print_line(node, ppck, ipck, out);
1859 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1860 /* node will be divided */
1861 /* print first half */
1862 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1863 /* pretend that this is normal node */
1864 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1865
1866 trp_print_line(ind_node1.node, ppck, tmp, out);
1867 ly_print_(out, "\n");
1868
1869 /* continue with second half on new line */
1870 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1871 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1872
1873 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1874 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1875 /* node name is too long */
1876 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1877
1878 if (trp_node_body_is_empty(node)) {
1879 return;
1880 } else {
1881 ly_print_(out, "\n");
1882
1883 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1884 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1885 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1886
1887 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1888 }
1889
1890 }
1891}
1892
aPiecek874ea4d2021-04-19 12:26:36 +02001893/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001894 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001895 *********************************************************************/
1896
1897/**
1898 * @brief Get nodetype.
1899 * @param[in] node is any lysp_node.
1900 */
1901static uint16_t
1902trop_nodetype(const void *node)
1903{
1904 return ((const struct lysp_node *)node)->nodetype;
1905}
1906
1907/**
1908 * @brief Get sibling.
1909 * @param[in] node is any lysp_node.
1910 */
1911static const void *
1912trop_next(const void *node)
1913{
1914 return ((const struct lysp_node *)node)->next;
1915}
1916
1917/**
1918 * @brief Get parent.
1919 * @param[in] node is any lysp_node.
1920 */
1921static const void *
1922trop_parent(const void *node)
1923{
1924 return ((const struct lysp_node *)node)->parent;
1925}
1926
1927/**
1928 * @brief Try to get child.
1929 * @param[in] node is any lysp_node.
1930 */
1931static const void *
1932trop_child(const void *node)
1933{
1934 return lysp_node_child(node);
1935}
1936
1937/**
1938 * @brief Try to get action.
1939 * @param[in] node is any lysp_node.
1940 */
1941static const void *
1942trop_actions(const void *node)
1943{
1944 return lysp_node_actions(node);
1945}
1946
1947/**
1948 * @brief Try to get action.
1949 * @param[in] node must be of type lysp_node_action.
1950 */
1951static const void *
1952trop_action_input(const void *node)
1953{
1954 return &((const struct lysp_node_action *)node)->input;
1955}
1956
1957/**
1958 * @brief Try to get action.
1959 * @param[in] node must be of type lysp_node_action.
1960 */
1961static const void *
1962trop_action_output(const void *node)
1963{
1964 return &((const struct lysp_node_action *)node)->output;
1965}
1966
1967/**
1968 * @brief Try to get action.
1969 * @param[in] node is any lysp_node.
1970 */
1971static const void *
1972trop_notifs(const void *node)
1973{
1974 return lysp_node_notifs(node);
1975}
1976
1977/**
aPiecek01598c02021-04-23 14:18:24 +02001978 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02001979 * which are adapted to lysp nodes.
1980 */
1981static struct tro_getters
1982trop_init_getters()
1983{
1984 return (struct tro_getters) {
1985 .nodetype = trop_nodetype,
1986 .next = trop_next,
1987 .parent = trop_parent,
1988 .child = trop_child,
1989 .actions = trop_actions,
1990 .action_input = trop_action_input,
1991 .action_output = trop_action_output,
1992 .notifs = trop_notifs
1993 };
1994}
1995
aPiecek3f247652021-04-19 13:40:25 +02001996/**
1997 * @brief Get nodetype.
1998 * @param[in] node is any lysc_node.
1999 */
2000static uint16_t
2001troc_nodetype(const void *node)
2002{
2003 return ((const struct lysc_node *)node)->nodetype;
2004}
2005
2006/**
2007 * @brief Get sibling.
2008 * @param[in] node is any lysc_node.
2009 */
2010static const void *
2011troc_next(const void *node)
2012{
2013 return ((const struct lysc_node *)node)->next;
2014}
2015
2016/**
2017 * @brief Get parent.
2018 * @param[in] node is any lysc_node.
2019 */
2020static const void *
2021troc_parent(const void *node)
2022{
2023 return ((const struct lysc_node *)node)->parent;
2024}
2025
2026/**
2027 * @brief Try to get child.
2028 * @param[in] node is any lysc_node.
2029 */
2030static const void *
2031troc_child(const void *node)
2032{
2033 return lysc_node_child(node);
2034}
2035
2036/**
2037 * @brief Try to get action.
2038 * @param[in] node is any lysc_node.
2039 */
2040static const void *
2041troc_actions(const void *node)
2042{
2043 return lysc_node_actions(node);
2044}
2045
2046/**
2047 * @brief Try to get action.
2048 * @param[in] node must be of type lysc_node_action.
2049 */
2050static const void *
2051troc_action_input(const void *node)
2052{
2053 return &((const struct lysc_node_action *)node)->input;
2054}
2055
2056/**
2057 * @brief Try to get action.
2058 * @param[in] node must be of type lysc_node_action.
2059 */
2060static const void *
2061troc_action_output(const void *node)
2062{
2063 return &((const struct lysc_node_action *)node)->output;
2064}
2065
2066/**
2067 * @brief Try to get action.
2068 * @param[in] node is any lysc_node.
2069 */
2070static const void *
2071troc_notifs(const void *node)
2072{
2073 return lysc_node_notifs(node);
2074}
2075
2076/**
aPiecek01598c02021-04-23 14:18:24 +02002077 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002078 * which are adapted to lysc nodes.
2079 */
2080static struct tro_getters
2081troc_init_getters()
2082{
2083 return (struct tro_getters) {
2084 .nodetype = troc_nodetype,
2085 .next = troc_next,
2086 .parent = troc_parent,
2087 .child = troc_child,
2088 .actions = troc_actions,
2089 .action_input = troc_action_input,
2090 .action_output = troc_action_output,
2091 .notifs = troc_notifs
2092 };
2093}
2094
aPiecekef1e58e2021-04-19 13:19:44 +02002095/**********************************************************************
2096 * tro functions
2097 *********************************************************************/
2098
2099/**
2100 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002101 *
2102 * This is a general algorithm that is able to
2103 * work with lysp_node or lysc_node.
2104 *
2105 * @param[in] node points to lysp_node or lysc_node.
2106 * @param[in] lysc_tree flag to determine what type the @p node is.
2107 * If set to true, then @p points to lysc_node otherwise lysp_node.
2108 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002109 */
2110static const void *
aPiecek3f247652021-04-19 13:40:25 +02002111tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002112{
2113 struct tro_getters get;
2114 const void *tmp, *parent;
2115 const void *ret;
2116
2117 assert(node);
2118
aPiecek3f247652021-04-19 13:40:25 +02002119 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002120
2121 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2122 if ((tmp = get.next(node))) {
2123 /* next action exists */
2124 ret = tmp;
2125 } else if ((parent = get.parent(node))) {
2126 /* maybe if notif exists as sibling */
2127 ret = get.notifs(parent);
2128 } else {
2129 ret = NULL;
2130 }
2131 } else if (get.nodetype(node) & LYS_INPUT) {
2132 if ((parent = get.parent(node))) {
2133 /* if output action has data */
2134 if (get.child(get.action_output(parent))) {
2135 /* then next sibling is output action */
2136 ret = get.action_output(parent);
2137 } else {
2138 /* input action cannot have siblings other
2139 * than output action.
2140 */
2141 ret = NULL;
2142 }
2143 } else {
2144 /* there is no way how to get output action */
2145 ret = NULL;
2146 }
2147 } else if (get.nodetype(node) & LYS_OUTPUT) {
2148 /* output action cannot have siblings */
2149 ret = NULL;
2150 } else if (get.nodetype(node) & LYS_NOTIF) {
2151 /* must have as a sibling only notif */
2152 ret = get.next(node);
2153 } else {
2154 /* for rest of nodes */
2155 if ((tmp = get.next(node))) {
2156 /* some sibling exists */
2157 ret = tmp;
2158 } else if ((parent = get.parent(node))) {
2159 /* Action and notif are siblings too.
2160 * They can be reached through parent.
2161 */
2162 if ((tmp = get.actions(parent))) {
2163 /* next sibling is action */
2164 ret = tmp;
2165 } else if ((tmp = get.notifs(parent))) {
2166 /* next sibling is notif */
2167 ret = tmp;
2168 } else {
2169 /* sibling not exists */
2170 ret = NULL;
2171 }
2172 } else {
2173 /* sibling not exists */
2174 ret = NULL;
2175 }
2176 }
2177
2178 return ret;
2179}
2180
2181/**
2182 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002183 *
2184 * This is a general algorithm that is able to
2185 * work with lysp_node or lysc_node.
2186 *
2187 * @param[in] node points to lysp_node or lysc_node.
2188 * @param[in] lysc_tree flag to determine what type the @p node is.
2189 * If set to true, then @p points to lysc_node otherwise lysp_node.
2190 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002191 */
2192static const void *
aPiecek3f247652021-04-19 13:40:25 +02002193tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002194{
2195 struct tro_getters get;
2196 const void *tmp;
2197 const void *ret;
2198
2199 assert(node);
2200
aPiecek3f247652021-04-19 13:40:25 +02002201 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002202
2203 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2204 if (get.child(get.action_input(node))) {
2205 /* go to LYS_INPUT */
2206 ret = get.action_input(node);
2207 } else if (get.child(get.action_output(node))) {
2208 /* go to LYS_OUTPUT */
2209 ret = get.action_output(node);
2210 } else {
2211 /* input action and output action have no data */
2212 ret = NULL;
2213 }
2214 } else {
2215 if ((tmp = get.child(node))) {
2216 ret = tmp;
2217 } else {
2218 /* current node can't have children or has no children */
2219 /* but maybe has some actions or notifs */
2220 if ((tmp = get.actions(node))) {
2221 ret = tmp;
2222 } else if ((tmp = get.notifs(node))) {
2223 ret = tmp;
2224 } else {
2225 ret = NULL;
2226 }
2227 }
2228 }
2229
2230 return ret;
2231}
2232
2233/**
aPiecek3f247652021-04-19 13:40:25 +02002234 * @brief Get new trt_parent_cache if we apply the transfer
2235 * to the child node in the tree.
2236 * @param[in] ca is parent cache for current node.
2237 * @param[in] tc contains current tree node.
2238 * @return Cache for the current node.
2239 */
2240static struct trt_parent_cache
2241tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2242{
2243 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2244
2245 if (!tc->lysc_tree) {
2246 const struct lysp_node *pn = tc->pn;
2247
2248 ret.ancestor =
2249 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2250 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2251 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2252 ca.ancestor;
2253
2254 ret.lys_status =
2255 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2256 ca.lys_status;
2257
2258 ret.lys_config =
2259 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2260 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2261 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2262 ca.lys_config;
2263
2264 ret.last_list =
2265 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2266 ca.last_list;
2267 }
2268
2269 return ret;
2270}
2271
2272/**
aPiecekef1e58e2021-04-19 13:19:44 +02002273 * @brief Transformation of the Schema nodes flags to
2274 * Tree diagram \<status\>.
2275 * @param[in] flags is node's flags obtained from the tree.
2276 */
2277static trt_status_type
2278tro_flags2status(uint16_t flags)
2279{
2280 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2281 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2282 TRD_STATUS_TYPE_CURRENT;
2283}
2284
2285/**
2286 * @brief Transformation of the Schema nodes flags to Tree diagram
2287 * \<flags\> but more specifically 'ro' or 'rw'.
2288 * @param[in] flags is node's flags obtained from the tree.
2289 */
2290static trt_flags_type
2291tro_flags2config(uint16_t flags)
2292{
2293 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2294 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2295 TRD_FLAGS_TYPE_EMPTY;
2296}
2297
2298/**
aPiecek3f247652021-04-19 13:40:25 +02002299 * @brief Print current node's iffeatures.
2300 * @param[in] tc is tree context.
2301 * @param[in,out] out is output handler.
2302 */
2303static void
2304tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2305{
2306 const struct lysp_qname *iffs;
2307
2308 iffs = tc->lysc_tree ?
2309 TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures :
2310 tc->pn->iffeatures;
2311
2312 LY_ARRAY_COUNT_TYPE i;
2313
2314 LY_ARRAY_FOR(iffs, i) {
2315 if (i == 0) {
2316 ly_print_(out, "%s", iffs[i].str);
2317 } else {
2318 ly_print_(out, ",%s", iffs[i].str);
2319 }
2320 }
2321
2322}
2323
2324/**
2325 * @brief Print current list's keys.
2326 *
2327 * Well, actually printing keys in the lysp_tree is trivial,
2328 * because char* points to all keys. However, special functions have
2329 * been reserved for this, because in principle the list of elements
2330 * can have more implementations.
2331 *
2332 * @param[in] tc is tree context.
2333 * @param[in,out] out is output handler.
2334 */
2335static void
2336tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2337{
2338 const struct lysp_node_list *list;
2339
2340 list = tc->lysc_tree ?
2341 (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn) :
2342 (const struct lysp_node_list *)tc->pn;
2343 assert(list->nodetype & LYS_LIST);
2344
2345 if (trg_charptr_has_data(list->key)) {
2346 ly_print_(out, "%s", list->key);
2347 }
2348}
2349
2350/**
2351 * @brief Get rpcs section if exists.
2352 * @param[in,out] tc is tree context.
2353 * @return Section representation if it exists. The @p tc is modified
2354 * and his pointer points to the first node in rpcs section.
2355 * @return Empty section representation otherwise.
2356 */
2357static struct trt_keyword_stmt
2358tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2359{
aPiecek9f792e52021-04-21 08:33:56 +02002360 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002361 const void *actions;
2362
2363 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002364 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002365 if (actions) {
2366 tc->cn = actions;
2367 }
2368 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002369 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002370 if (actions) {
2371 tc->pn = actions;
2372 tc->tpn = tc->pn;
2373 }
2374 }
2375
2376 if (actions) {
2377 tc->section = TRD_SECT_RPCS;
2378 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2379 } else {
2380 return TRP_EMPTY_KEYWORD_STMT;
2381 }
2382}
2383
2384/**
2385 * @brief Get notification section if exists
2386 * @param[in,out] tc is tree context.
2387 * @return Section representation if it exists.
2388 * The @p tc is modified and his pointer points to the
2389 * first node in notification section.
2390 * @return Empty section representation otherwise.
2391 */
2392static struct trt_keyword_stmt
2393tro_modi_get_notifications(struct trt_tree_ctx *tc)
2394{
aPiecek9f792e52021-04-21 08:33:56 +02002395 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002396 const void *notifs;
2397
2398 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002399 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002400 if (notifs) {
2401 tc->cn = notifs;
2402 }
2403 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002404 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002405 if (notifs) {
2406 tc->pn = notifs;
2407 tc->tpn = tc->pn;
2408 }
2409 }
2410
2411 if (notifs) {
2412 tc->section = TRD_SECT_NOTIF;
2413 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2414 } else {
2415 return TRP_EMPTY_KEYWORD_STMT;
2416 }
2417}
2418
2419/**
aPiecek96baa7f2021-04-23 12:32:00 +02002420 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002421 *
2422 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002423 * @param[in] u is index to the array of extensions (lysc_ext_instance
2424 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002425 * @return Section representation if it exists.
2426 * @return Empty section representation otherwise.
2427 */
2428static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002429tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002430{
aPiecek96baa7f2021-04-23 12:32:00 +02002431 assert(tc);
2432 const void *node;
2433 const char *yang_data_name;
2434
2435 if (tc->lysc_tree) {
2436 struct lysc_ext_instance *exts;
2437 struct lysc_ext_substmt *substmts;
2438
2439 exts = tc->cmod->exts;
2440 substmts = exts[u].substmts;
2441 if (!substmts) {
2442 return TRP_EMPTY_KEYWORD_STMT;
2443 }
2444 node = *(const struct lysc_node **)substmts->storage;
2445 yang_data_name = exts[u].argument;
2446 } else {
2447 struct lysp_ext_instance *exts;
2448
2449 exts = tc->pmod->exts;
2450 node = exts[u].parsed;
2451 yang_data_name = exts[u].argument;
2452 }
2453
2454 if (tc->lysc_tree) {
2455 tc->cn = node;
2456 } else {
2457 tc->tpn_ext = &tc->pmod->exts[u];
2458 tc->pn = node;
2459 }
2460
2461 if (node) {
2462 tc->section = TRD_SECT_YANG_DATA;
2463 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2464 } else {
2465 return TRP_EMPTY_KEYWORD_STMT;
2466 }
aPiecekef1e58e2021-04-19 13:19:44 +02002467}
2468
2469/**
2470 * @brief Get name of the module.
2471 * @param[in] tc is context of the tree.
2472 */
2473static struct trt_keyword_stmt
2474tro_read_module_name(const struct trt_tree_ctx *tc)
2475{
aPiecek9f792e52021-04-21 08:33:56 +02002476 assert(tc);
2477
2478 struct trt_keyword_stmt ret;
2479
2480 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2481 TRD_KEYWORD_SUBMODULE :
2482 TRD_KEYWORD_MODULE;
2483
2484 ret.str = !tc->lysc_tree ?
2485 LYSP_MODULE_NAME(tc->pmod) :
2486 tc->cmod->mod->name;
2487
2488 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002489}
2490
2491/**********************************************************************
2492 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002493 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002494
2495/**
aPiecek61d062b2020-11-02 11:05:09 +01002496 * @brief Check if list statement has keys.
2497 * @param[in] pn is pointer to the list.
2498 * @return 1 if has keys, otherwise 0.
2499 */
2500static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002501trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002502{
aPiecekef1e58e2021-04-19 13:19:44 +02002503 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002504}
2505
2506/**
2507 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002508 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002509 * @return 1 if has if-features, otherwise 0.
2510 */
2511static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002512trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002513{
2514 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002515 const struct lysp_qname *iffs;
2516
aPiecek61d062b2020-11-02 11:05:09 +01002517 ly_bool ret = 0;
2518
aPiecekef1e58e2021-04-19 13:19:44 +02002519 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002520 LY_ARRAY_FOR(iffs, u) {
2521 ret = 1;
2522 break;
2523 }
2524 return ret;
2525}
2526
2527/**
2528 * @brief Find out if leaf is also the key in last list.
2529 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002530 * @param[in] ca_last_list is pointer to last visited list.
2531 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002532 * @return 1 if leaf is also the key, otherwise 0.
2533 */
2534static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002535trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002536{
2537 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2538 const struct lysp_node_list *list = ca_last_list;
2539
2540 if (!list) {
2541 return 0;
2542 }
2543 return trg_charptr_has_data(list->key) ?
2544 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2545}
2546
2547/**
2548 * @brief Check if container's type is presence.
2549 * @param[in] pn is pointer to container.
2550 * @return 1 if container has presence statement, otherwise 0.
2551 */
2552static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002553trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002554{
2555 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2556}
2557
2558/**
2559 * @brief Get leaflist's path without lysp_node type control.
2560 * @param[in] pn is pointer to the leaflist.
2561 */
2562static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002563trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002564{
2565 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2566
2567 return list->type.path ? list->type.path->expr : NULL;
2568}
2569
2570/**
2571 * @brief Get leaflist's type name without lysp_node type control.
2572 * @param[in] pn is pointer to the leaflist.
2573 */
2574static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002575trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002576{
2577 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2578
2579 return list->type.name;
2580}
2581
2582/**
2583 * @brief Get leaf's path without lysp_node type control.
2584 * @param[in] pn is pointer to the leaf node.
2585 */
2586static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002587trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002588{
2589 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2590
2591 return leaf->type.path ? leaf->type.path->expr : NULL;
2592}
2593
2594/**
2595 * @brief Get leaf's type name without lysp_node type control.
2596 * @param[in] pn is pointer to the leaf's type name.
2597 */
2598static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002599trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002600{
2601 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2602
2603 return leaf->type.name;
2604}
2605
2606/**
aPiecek874ea4d2021-04-19 12:26:36 +02002607 * @brief Get pointer to data using node type specification
2608 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002609 *
aPiecek874ea4d2021-04-19 12:26:36 +02002610 * @param[in] flags is node type specification.
2611 * If it is the correct node, the getter function is called.
2612 * @param[in] f is getter function which provides the desired
2613 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002614 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002615 * @return NULL if node has wrong type or getter function return
2616 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002617 * @return Pointer to desired char pointer obtained from the node.
2618 */
2619static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002620trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002621{
2622 if (pn->nodetype & flags) {
2623 const char *ret = f(pn);
2624 return trg_charptr_has_data(ret) ? ret : NULL;
2625 } else {
2626 return NULL;
2627 }
2628}
2629
2630/**
aPiecek61d062b2020-11-02 11:05:09 +01002631 * @brief Resolve \<status\> of the current node.
2632 * @param[in] nodetype is node's type obtained from the tree.
2633 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002634 * @param[in] ca_lys_status is inherited status
2635 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002636 * @return The status type.
2637 */
2638static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002639trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002640{
2641 /* LYS_INPUT and LYS_OUTPUT is special case */
2642 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002643 return tro_flags2status(ca_lys_status);
2644 /* if ancestor's status is deprc or obslt
2645 * and also node's status is not set
2646 */
aPiecek61d062b2020-11-02 11:05:09 +01002647 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2648 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002649 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002650 } else {
2651 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002652 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002653 }
2654}
2655
2656/**
2657 * @brief Resolve \<flags\> of the current node.
2658 * @param[in] nodetype is node's type obtained from the tree.
2659 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002660 * @param[in] ca_ancestor is ancestor type obtained
2661 * from trt_parent_cache.
2662 * @param[in] ca_lys_config is inherited config item
2663 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002664 * @return The flags type.
2665 */
2666static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002667trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002668{
2669 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2670 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2671 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2672 return TRD_FLAGS_TYPE_RO;
2673 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2674 return TRD_FLAGS_TYPE_RO;
2675 } else if (nodetype & LYS_NOTIF) {
2676 return TRD_FLAGS_TYPE_NOTIF;
2677 } else if (nodetype & LYS_USES) {
2678 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2679 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2680 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002681 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002682 /* config is not set. Look at ancestor's config */
2683 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002684 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002685 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002686 }
2687}
2688
2689/**
2690 * @brief Resolve node type of the current node.
2691 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002692 * @param[in] ca_last_list is pointer to the last visited list.
2693 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002694 */
2695static trt_node_type
aPiecekef1e58e2021-04-19 13:19:44 +02002696trop_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002697{
2698 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2699 return TRD_NODE_ELSE;
2700 } else if (pn->nodetype & LYS_CASE) {
2701 return TRD_NODE_CASE;
2702 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2703 return TRD_NODE_OPTIONAL_CHOICE;
2704 } else if (pn->nodetype & LYS_CHOICE) {
2705 return TRD_NODE_CHOICE;
aPiecekef1e58e2021-04-19 13:19:44 +02002706 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002707 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002708 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002709 return TRD_NODE_KEYS;
2710 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2711 return TRD_NODE_LISTLEAFLIST;
2712 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2713 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002714 } 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 +01002715 return TRD_NODE_OPTIONAL;
2716 } else {
2717 return TRD_NODE_ELSE;
2718 }
2719}
2720
2721/**
aPiecekef1e58e2021-04-19 13:19:44 +02002722 * @brief Resolve \<type\> of the current node.
2723 * @param[in] pn is current node.
2724 */
2725static struct trt_type
2726trop_resolve_type(const struct lysp_node *pn)
2727{
2728 const char *tmp = NULL;
2729
2730 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2731 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2732 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2733 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2734 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2735 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2736 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2737 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2738 } else if (pn->nodetype == LYS_ANYDATA) {
2739 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2740 } else if (pn->nodetype & LYS_ANYXML) {
2741 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2742 } else {
2743 return TRP_EMPTY_TRT_TYPE;
2744 }
2745}
2746
2747/**
aPiecek61d062b2020-11-02 11:05:09 +01002748 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002749 * @param[in] ca contains stored important data
2750 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002751 * @param[in] tc is context of the tree.
2752 */
2753static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002754trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002755{
aPiecekef1e58e2021-04-19 13:19:44 +02002756 const struct lysp_node *pn;
2757 struct trt_node ret;
2758
aPiecek61d062b2020-11-02 11:05:09 +01002759 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002760
2761 pn = tc->pn;
2762 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002763
2764 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002765 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002766
2767 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2768 /* <flags> */
aPiecekef1e58e2021-04-19 13:19:44 +02002769 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002770
2771 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2772 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2773 /* set type of the node */
aPiecekef1e58e2021-04-19 13:19:44 +02002774 ret.name.type = trop_resolve_node_type(pn, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002775
2776 /* TODO: ret.name.module_prefix is not supported right now. */
2777 ret.name.module_prefix = NULL;
2778
2779 /* set node's name */
2780 ret.name.str = pn->name;
2781
2782 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002783 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002784
2785 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002786 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002787
aPiecek3f247652021-04-19 13:40:25 +02002788 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002789
2790 return ret;
2791}
2792
aPiecekef1e58e2021-04-19 13:19:44 +02002793/**
2794 * @brief Find out if the current node has siblings.
2795 * @param[in] tc is context of the tree.
2796 * @return 1 if sibling exists otherwise 0.
2797 */
2798static ly_bool
2799trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2800{
aPiecek3f247652021-04-19 13:40:25 +02002801 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002802}
2803
aPiecek96baa7f2021-04-23 12:32:00 +02002804/**
2805 * @brief Print all yang-data sections and print three dots instead
2806 * of nodes.
2807 * @param[in] exts is array of YANG extension instances from parsed
2808 * module (@ref sizedarrays).
2809 * @param[in] mll is maximum number of characters that can be printed
2810 * on one line.
2811 * @param[in,out] out is output handler.
2812 */
2813static void
2814trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2815{
2816 struct trt_keyword_stmt ks;
2817 LY_ARRAY_COUNT_TYPE u;
2818 struct trt_wrapper wr;
2819
2820 if (!exts) {
2821 return;
2822 }
2823
2824 ly_print_(out, "\n");
2825 ks.type = TRD_KEYWORD_YANG_DATA;
2826 wr = TRP_INIT_WRAPPER_BODY;
2827
2828 LY_ARRAY_FOR(exts, u) {
2829 ly_print_(out, "\n");
2830
2831 /* yang-data <yang-data-name>: */
2832 ks.str = exts[u].argument;
2833 trp_print_keyword_stmt(ks, mll, 0, out);
2834 ly_print_(out, "\n");
2835
2836 /* ... */
2837 trp_print_wrapper(wr, out);
2838 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2839 }
2840}
2841
aPiecek874ea4d2021-04-19 12:26:36 +02002842/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002843 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002844 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002845
2846/**
aPiecek874ea4d2021-04-19 12:26:36 +02002847 * @brief Change current node pointer to its parent
2848 * but only if parent exists.
2849 * @param[in,out] tc is tree context.
2850 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002851 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002852 * @return 0 if the node did not have parents.
2853 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002854 */
2855static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002856trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002857{
2858 assert(tc && tc->pn);
2859 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002860 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002861 tc->pn = tc->pn->parent;
2862 return 1;
2863 } else {
2864 return 0;
2865 }
2866}
2867
2868/**
aPiecek874ea4d2021-04-19 12:26:36 +02002869 * @brief Change the current node pointer to its child
2870 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002871 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002872 * @param[in,out] tc is context of the tree.
2873 * Contains pointer to the current node.
2874 * @return Non-empty \<node\> representation of the current
2875 * node's child. The @p tc is modified.
2876 * @return Empty \<node\> representation if child don't exists.
2877 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002878 */
2879static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002880trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002881{
aPiecekef1e58e2021-04-19 13:19:44 +02002882 const struct lysp_node *tmp;
2883
aPiecek61d062b2020-11-02 11:05:09 +01002884 assert(tc && tc->pn);
2885
aPiecek3f247652021-04-19 13:40:25 +02002886 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002887 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02002888 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01002889 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002890 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002891 }
2892}
2893
2894/**
aPiecek874ea4d2021-04-19 12:26:36 +02002895 * @brief Change the current node pointer to the first child of node's
2896 * parent. If current node is already first sibling/child then nothing
2897 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01002898 * @param[in,out] tc is tree context.
2899 */
2900static void
aPiecekef1e58e2021-04-19 13:19:44 +02002901trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002902{
aPiecek9f792e52021-04-21 08:33:56 +02002903 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01002904
aPiecekef1e58e2021-04-19 13:19:44 +02002905 if (trop_modi_parent(tc)) {
2906 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002907 } else {
2908 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01002909 switch (tc->section) {
2910 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02002911 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02002912 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002913 break;
2914 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02002915 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02002916 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002917 break;
2918 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02002919 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02002920 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002921 break;
2922 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02002923 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02002924 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002925 break;
2926 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02002927 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02002928 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002929 break;
2930 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02002931 /* tpn in this case is of type lysp_ext_instance */
2932 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01002933 break;
aPiecek96baa7f2021-04-23 12:32:00 +02002934 default:
2935 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01002936 }
aPiecek61d062b2020-11-02 11:05:09 +01002937 }
2938}
2939
2940/**
aPiecek874ea4d2021-04-19 12:26:36 +02002941 * @brief Change the pointer to the current node to its next sibling
2942 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002943 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002944 * @param[in,out] tc is tree context.
2945 * Contains pointer to the current node.
2946 * @return Non-empty \<node\> representation if sibling exists.
2947 * The @p tc is modified.
2948 * @return Empty \<node\> representation otherwise.
2949 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002950 */
2951static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002952trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002953{
aPiecekef1e58e2021-04-19 13:19:44 +02002954 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002955
2956 assert(tc && tc->pn);
2957
aPiecek3f247652021-04-19 13:40:25 +02002958 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002959
aPiecekef1e58e2021-04-19 13:19:44 +02002960 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02002961 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
2962 tc->tpn = pn;
2963 }
aPiecekef1e58e2021-04-19 13:19:44 +02002964 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002965 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002966 } else {
2967 return TRP_EMPTY_NODE;
2968 }
2969}
2970
2971/**
2972 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02002973 * @param[in,out] tc is tree context. It is modified and his current
2974 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01002975 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01002976 * @return Empty section structure otherwise.
2977 */
2978static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02002979trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002980{
aPiecek9f792e52021-04-21 08:33:56 +02002981 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01002982 const struct lysp_node_augment *augs;
2983
2984 /* if next_augment func was called for the first time */
2985 if (tc->section != TRD_SECT_AUGMENT) {
2986 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02002987 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01002988 } else {
2989 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02002990 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01002991 }
2992
aPiecekdc8fd572021-04-19 10:47:23 +02002993 if (augs) {
2994 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01002995 tc->tpn = tc->pn;
2996 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
2997 } else {
2998 return TRP_EMPTY_KEYWORD_STMT;
2999 }
3000}
3001
3002/**
aPiecek61d062b2020-11-02 11:05:09 +01003003 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003004 * @param[in,out] tc is tree context. It is modified and his current
3005 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003006 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003007 * @return Empty section representation otherwise.
3008 */
3009static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003010trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003011{
aPiecek9f792e52021-04-21 08:33:56 +02003012 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003013 const struct lysp_node_grp *grps;
3014
3015 if (tc->section != TRD_SECT_GROUPING) {
3016 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003017 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003018 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003019 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003020 }
3021
aPiecekdc8fd572021-04-19 10:47:23 +02003022 if (grps) {
3023 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003024 tc->tpn = tc->pn;
3025 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3026 } else {
3027 return TRP_EMPTY_KEYWORD_STMT;
3028 }
3029}
3030
aPiecek874ea4d2021-04-19 12:26:36 +02003031/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003032 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003033 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003034
3035/**
aPiecek3f247652021-04-19 13:40:25 +02003036 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003037 */
aPiecek3f247652021-04-19 13:40:25 +02003038static ly_bool
3039troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003040{
aPiecek3f247652021-04-19 13:40:25 +02003041 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3042}
aPiecek61d062b2020-11-02 11:05:09 +01003043
aPiecek3f247652021-04-19 13:40:25 +02003044/**
3045 * @brief Resolve \<flags\> of the current node.
3046 *
3047 * Use this function only if trt_tree_ctx.lysc_tree is true.
3048 *
3049 * @param[in] nodetype is current lysc_node.nodetype.
3050 * @param[in] flags is current lysc_node.flags.
3051 * @return The flags type.
3052 */
3053static trt_flags_type
3054troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3055{
3056 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3057 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3058 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3059 return TRD_FLAGS_TYPE_RO;
3060 } else if (nodetype & LYS_IS_NOTIF) {
3061 return TRD_FLAGS_TYPE_RO;
3062 } else if (nodetype & LYS_NOTIF) {
3063 return TRD_FLAGS_TYPE_NOTIF;
3064 } else if (nodetype & LYS_USES) {
3065 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3066 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3067 return TRD_FLAGS_TYPE_RPC;
3068 } else {
3069 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003070 }
aPiecek61d062b2020-11-02 11:05:09 +01003071}
3072
3073/**
aPiecek3f247652021-04-19 13:40:25 +02003074 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003075 *
aPiecek3f247652021-04-19 13:40:25 +02003076 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003077 *
aPiecek3f247652021-04-19 13:40:25 +02003078 * @param[in] nodetype is current lysc_node.nodetype.
3079 * @param[in] flags is current lysc_node.flags.
3080 */
3081static trt_node_type
3082troc_resolve_node_type(uint16_t nodetype, uint16_t flags)
3083{
3084 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3085 return TRD_NODE_ELSE;
3086 } else if (nodetype & LYS_CASE) {
3087 return TRD_NODE_CASE;
3088 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3089 return TRD_NODE_OPTIONAL_CHOICE;
3090 } else if (nodetype & LYS_CHOICE) {
3091 return TRD_NODE_CHOICE;
3092 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3093 return TRD_NODE_CONTAINER;
3094 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3095 return TRD_NODE_KEYS;
3096 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3097 return TRD_NODE_LISTLEAFLIST;
3098 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3099 return TRD_NODE_OPTIONAL;
3100 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3101 return TRD_NODE_OPTIONAL;
3102 } else {
3103 return TRD_NODE_ELSE;
3104 }
3105}
3106
3107/**
3108 * @brief Transformation of current lysc_node to struct trt_node.
3109 * @param[in] ca is not used.
3110 * @param[in] tc is context of the tree.
3111 */
3112static struct trt_node
3113troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3114{
3115 (void) ca;
3116 const struct lysc_node *cn;
3117 struct trt_node ret;
3118
aPiecek96baa7f2021-04-23 12:32:00 +02003119 assert(tc && tc->cn && tc->cn->priv);
aPiecek3f247652021-04-19 13:40:25 +02003120
3121 cn = tc->cn;
3122 ret = TRP_EMPTY_NODE;
3123
3124 /* <status> */
3125 ret.status = tro_flags2status(cn->flags);
3126
3127 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
3128 /* <flags> */
3129 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3130
3131 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
3132 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
3133 /* set type of the node */
3134 ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
3135
3136 /* TODO: ret.name.module_prefix is not supported right now. */
3137 ret.name.module_prefix = NULL;
3138
3139 /* set node's name */
3140 ret.name.str = cn->name;
3141
3142 /* <type> */
3143 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3144
3145 /* <iffeature> */
3146 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3147
3148 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3149
3150 return ret;
3151}
3152
3153/**********************************************************************
3154 * Modify troc getters
3155 *********************************************************************/
3156
3157/**
aPiecek01598c02021-04-23 14:18:24 +02003158 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003159 */
3160static ly_bool
3161troc_modi_parent(struct trt_tree_ctx *tc)
3162{
3163 assert(tc && tc->cn);
3164 /* If no parent exists, stay in actual node. */
3165 if (tc->cn->parent) {
3166 tc->cn = tc->cn->parent;
3167 return 1;
3168 } else {
3169 return 0;
3170 }
3171}
3172
3173/**
aPiecek01598c02021-04-23 14:18:24 +02003174 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003175 */
3176static struct trt_node
3177troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3178{
3179 const struct lysc_node *cn;
3180
3181 assert(tc && tc->cn);
3182
3183 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3184
3185 /* if next sibling exists */
3186 if (cn) {
3187 /* update trt_tree_ctx */
3188 tc->cn = cn;
3189 return troc_read_node(ca, tc);
3190 } else {
3191 return TRP_EMPTY_NODE;
3192 }
3193}
3194
3195/**
3196 * @copydoc trop_modi_next_child()
3197 */
3198static struct trt_node
3199troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3200{
3201 const struct lysc_node *tmp;
3202
3203 assert(tc && tc->cn);
3204
3205 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3206 tc->cn = tmp;
3207 return troc_read_node(ca, tc);
3208 } else {
3209 return TRP_EMPTY_NODE;
3210 }
3211}
3212
3213/**
aPiecek01598c02021-04-23 14:18:24 +02003214 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003215 */
3216static void
aPiecek3f247652021-04-19 13:40:25 +02003217troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003218{
aPiecek3f247652021-04-19 13:40:25 +02003219 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003220
aPiecek3f247652021-04-19 13:40:25 +02003221 if (troc_modi_parent(tc)) {
3222 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3223 } else {
3224 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003225 switch (tc->section) {
3226 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003227 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003228 break;
3229 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003230 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003231 break;
3232 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003233 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003234 break;
3235 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003236 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003237 break;
3238 default:
3239 assert(0);
3240 }
aPiecek61d062b2020-11-02 11:05:09 +01003241 }
3242}
3243
aPiecek874ea4d2021-04-19 12:26:36 +02003244/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003245 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003246 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003247
3248/**
3249 * @brief Get size of node name.
3250 * @param[in] name contains name and mark.
3251 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003252 * @return negative value as an indication that option mark
3253 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003254 */
3255static int32_t
3256trb_strlen_of_name_and_mark(struct trt_node_name name)
3257{
3258 size_t name_len = strlen(name.str);
3259
3260 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3261 /* counting also parentheses */
3262 name_len += 2;
3263 }
3264
3265 return trp_mark_is_used(name) ?
3266 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3267 (int32_t)name_len;
3268}
3269
3270/**
aPiecek874ea4d2021-04-19 12:26:36 +02003271 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3272 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003273 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003274 * @param[in] max_len4all is the maximum value of btw_opts_type
3275 * that it can have.
3276 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003277 */
3278static int16_t
3279trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3280{
3281 int32_t name_len;
3282 int16_t min_len;
3283 int16_t ret;
3284
3285 name_len = trb_strlen_of_name_and_mark(name);
3286
3287 /* negative value indicate that in name is some opt mark */
3288 min_len = name_len < 0 ?
3289 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3290 TRD_INDENT_BEFORE_TYPE;
3291 ret = abs(max_len4all) - abs(name_len);
3292
3293 /* correction -> negative indicate that name is too long. */
3294 return ret < 0 ? min_len : ret;
3295}
3296
3297/**
3298 * @brief Print node.
3299 *
aPiecek01598c02021-04-23 14:18:24 +02003300 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003301 * But difference is that take @p max_gap_before_type which will be
3302 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003303 *
3304 * @param[in] max_gap_before_type is number of indent before \<type\>.
3305 * @param[in] wr is wrapper for printing indentation before node.
3306 * @param[in] ca contains inherited data from ancestors.
3307 * @param[in] pc contains mainly functions for printing.
3308 * @param[in] tc is tree context.
3309 */
3310static void
3311trb_print_entire_node(uint32_t max_gap_before_type, struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3312{
3313 struct trt_node node = pc->fp.read.node(ca, tc);
3314 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3315
3316 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3317 /* print actual node with unified indent */
3318 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3319 }
3320 /* after -> print actual node with default indent */
3321 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3322 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
3323}
3324
3325/**
aPiecek874ea4d2021-04-19 12:26:36 +02003326 * @brief Check if parent of the current node is the last
3327 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003328 *
aPiecek874ea4d2021-04-19 12:26:36 +02003329 * To mantain stability use this function only if the current node is
3330 * the first of the siblings.
3331 * Side-effect -> current node is set to the first sibling
3332 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003333 *
aPiecek01598c02021-04-23 14:18:24 +02003334 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003335 * @param[in,out] tc is tree context.
3336 * @return 1 if parent is last sibling otherwise 0.
3337 */
3338static ly_bool
3339trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3340{
3341 if (fp.modify.parent(tc)) {
3342 ly_bool ret = fp.read.if_sibling_exists(tc);
3343 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3344 return !ret;
3345 } else {
3346 return !fp.read.if_sibling_exists(tc);
3347 }
3348}
3349
3350/**
3351 * @brief Find sibling with the biggest node name and return that size.
3352 *
3353 * Side-effect -> Current node is set to the first sibling.
3354 *
3355 * @param[in] ca contains inherited data from ancestors.
3356 * @param[in] pc contains mainly functions for printing.
3357 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003358 * @return positive number as a sign that only the node name is
3359 * included in the size.
3360 * @return negative number sign that node name and his opt mark is
3361 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003362 */
3363static int32_t
3364trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3365{
3366 int32_t ret = 0;
3367
3368 pc->fp.modify.first_sibling(tc);
3369
3370 for (struct trt_node node = pc->fp.read.node(ca, tc);
3371 !trp_node_is_empty(node);
3372 node = pc->fp.modify.next_sibling(ca, tc)) {
3373 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
3374 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3375 }
3376 pc->fp.modify.first_sibling(tc);
3377 return ret;
3378}
3379
3380/**
aPiecek874ea4d2021-04-19 12:26:36 +02003381 * @brief Find maximal indent between
3382 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003383 *
3384 * Side-effect -> Current node is set to the first sibling.
3385 *
3386 * @param[in] ca contains inherited data from ancestors.
3387 * @param[in] pc contains mainly functions for printing.
3388 * @param[in,out] tc is tree context.
3389 * @return max btw_opts_type value for rest of the siblings
3390 */
3391static int16_t
3392trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3393{
3394 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3395 int16_t ind_before_type = maxlen_node_name < 0 ?
3396 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3397 TRD_INDENT_BEFORE_TYPE;
3398
3399 return abs(maxlen_node_name) + ind_before_type;
3400}
3401
3402/**
aPiecek874ea4d2021-04-19 12:26:36 +02003403 * @brief Find out if it is possible to unify
3404 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003405 *
aPiecek874ea4d2021-04-19 12:26:36 +02003406 * The goal is for all node siblings to have the same alignment
3407 * for \<type\> as if they were in a column. All siblings who cannot
3408 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003409 * Side-effect -> Current node is set to the first sibling.
3410 *
3411 * @param[in] ca contains inherited data from ancestors.
3412 * @param[in] pc contains mainly functions for printing.
3413 * @param[in,out] tc is tree context.
3414 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003415 * @return positive number indicating the maximum number of spaces
3416 * before \<type\> if the length of the node name is 0. To calculate
3417 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003418 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003419*/
3420static uint32_t
3421trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3422{
3423 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3424}
3425
3426/**
aPiecek874ea4d2021-04-19 12:26:36 +02003427 * @brief For the current node: recursively print all of its child
3428 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003429 *
aPiecek01598c02021-04-23 14:18:24 +02003430 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003431 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003432 * Nodes are printed, including unified sibling node alignment
3433 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003434 * Side-effect -> current node is set to the last sibling.
3435 *
3436 * @param[in] wr is wrapper for printing identation before node.
3437 * @param[in] ca contains inherited data from ancestors.
3438 * @param[in] pc contains mainly functions for printing.
3439 * @param[in,out] tc is tree context.
3440 */
3441static void
3442trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3443{
3444 uint32_t max_gap_before_type;
3445 ly_bool sibling_flag = 0;
3446 ly_bool child_flag = 0;
3447
3448 /* if node is last sibling, then do not add '|' to wrapper */
3449 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3450 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3451
3452 /* try unified indentation in node */
3453 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3454
3455 /* print all siblings */
3456 do {
3457 struct trt_parent_cache new_ca;
3458 struct trt_node node;
3459 /* print linebreak before printing actual node */
3460 ly_print_(pc->out, "\n");
3461 /* print node */
3462 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
3463
aPiecek3f247652021-04-19 13:40:25 +02003464 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003465 /* go to the actual node's child or stay in actual node */
3466 node = pc->fp.modify.next_child(ca, tc);
3467 child_flag = !trp_node_is_empty(node);
3468
3469 if (child_flag) {
3470 /* print all childs - recursive call */
3471 trb_print_nodes(wr, new_ca, pc, tc);
3472 /* get back from child node to actual node */
3473 pc->fp.modify.parent(tc);
3474 }
3475
3476 /* go to the actual node's sibling */
3477 node = pc->fp.modify.next_sibling(ca, tc);
3478 sibling_flag = !trp_node_is_empty(node);
3479
3480 /* go to the next sibling or stay in actual node */
3481 } while (sibling_flag);
3482}
3483
3484/**
aPiecek153b00f2021-04-20 13:52:57 +02003485 * @brief Calculate the wrapper about how deep in the tree the node is.
3486 * @param[in] node from which to count.
3487 * @return wrapper for @p node.
3488 */
3489static struct trt_wrapper
3490trb_count_depth(const struct lysc_node *node)
3491{
3492 struct trt_wrapper wr = TRP_INIT_WRAPPER_TOP;
3493 const struct lysc_node *parent;
3494
3495 if (!node) {
3496 return wr;
3497 }
3498
3499 for (parent = node->parent; parent; parent = parent->parent) {
3500 wr = trp_wrapper_set_shift(wr);
3501 }
3502
3503 return wr;
3504}
3505
3506/**
3507 * @brief Print all parent nodes of @p node and the @p node itself.
3508 *
3509 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3510 *
3511 * @param[in] node on which the function is focused.
aPiecek01598c02021-04-23 14:18:24 +02003512 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003513 * @param[in,out] tc is context of tree printer.
3514 * @return wrapper for @p node.
3515 */
3516static void
3517trb_print_parents(const struct lysc_node *node, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3518{
3519 struct trt_wrapper wr;
3520
3521 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3522
3523 /* stop recursion */
3524 if (!node) {
3525 return;
3526 }
3527 trb_print_parents(node->parent, pc, tc);
3528
3529 /* setup for printing */
3530 tc->cn = node;
3531 wr = trb_count_depth(node);
3532
3533 /* print node */
3534 ly_print_(pc->out, "\n");
3535 trb_print_entire_node(0, wr, TRP_EMPTY_PARENT_CACHE, pc, tc);
3536}
3537
3538/**
aPiecekdc8fd572021-04-19 10:47:23 +02003539 * @brief Get address of the current node.
3540 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003541 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003542 */
3543static const void *
3544trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3545{
aPiecek3f247652021-04-19 13:40:25 +02003546 return tc->lysc_tree ?
3547 (const void *)tc->cn :
3548 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003549}
3550
3551/**
3552 * @brief Get address of current node's child.
3553 * @param[in,out] tc contains current node.
3554 */
3555static const void *
3556trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3557{
3558 if (!trb_tree_ctx_get_node(tc)) {
3559 return NULL;
3560 }
3561
aPiecek3f247652021-04-19 13:40:25 +02003562 if (tc->lysc_tree) {
3563 return lysc_node_child(tc->cn);
3564 } else {
3565 return lysp_node_child(tc->pn);
3566 }
aPiecekdc8fd572021-04-19 10:47:23 +02003567}
3568
3569/**
3570 * @brief Set current node on its child.
3571 * @param[in,out] tc contains current node.
3572 */
3573static void
3574trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3575{
aPiecek3f247652021-04-19 13:40:25 +02003576 const void *node = trb_tree_ctx_get_child(tc);
3577
3578 if (tc->lysc_tree) {
3579 tc->cn = node;
3580 } else {
3581 tc->pn = node;
3582 }
aPiecekdc8fd572021-04-19 10:47:23 +02003583}
3584
3585/**
aPiecek61d062b2020-11-02 11:05:09 +01003586 * @brief Print subtree of nodes.
3587 *
3588 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003589 * Before root node is no linebreak printing. This must be addressed by
3590 * the caller. Root node will also be printed. Behind last printed node
3591 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003592 *
aPiecek874ea4d2021-04-19 12:26:36 +02003593 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003594 * ::trb_try_unified_indent() function for root node.
3595 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003596 * @param[in] wr is wrapper saying how deep in the whole tree
3597 * is the root of the subtree.
3598 * @param[in] ca is parent_cache from root's parent.
3599 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003600 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003601 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003602 */
3603static void
3604trb_print_subtree_nodes(uint32_t max_gap_before_type, struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3605{
3606 struct trt_parent_cache new_ca;
3607 struct trt_node node;
3608
aPiecekdc8fd572021-04-19 10:47:23 +02003609 if (!trb_tree_ctx_get_node(tc)) {
3610 return;
3611 }
3612
aPiecek61d062b2020-11-02 11:05:09 +01003613 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
3614 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003615 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003616 node = pc->fp.modify.next_child(ca, tc);
3617
3618 if (!trp_node_is_empty(node)) {
3619 /* print root's nodes */
3620 trb_print_nodes(wr, new_ca, pc, tc);
3621 /* get back from child node to actual node */
3622 pc->fp.modify.parent(tc);
3623 }
3624}
3625
3626/**
3627 * @brief Get number of siblings.
3628 *
3629 * Side-effect -> current node is set to the first sibling.
3630 *
3631 * @param[in] fp contains callback functions which modify tree context
3632 * @param[in,out] tc is the tree context.
3633 * @return Number of siblings of the current node.
3634 */
3635static uint32_t
3636trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3637{
3638 uint32_t ret = 1;
3639 struct trt_node node = TRP_EMPTY_NODE;
3640
3641 /* including actual node */
3642 fp.first_sibling(tc);
3643 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3644 ret++;
3645 }
3646 fp.first_sibling(tc);
3647 return ret;
3648}
3649
3650/**
3651 * @brief Print all parents and their children.
3652 *
aPiecek874ea4d2021-04-19 12:26:36 +02003653 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003654 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003655 * for all top-level siblings. Use this function after 'module' keyword
3656 * or 'augment' and so. The nodes may not be exactly top-level in the
3657 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003658 *
aPiecek153b00f2021-04-20 13:52:57 +02003659 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3660 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003661 * @param[pc] pc contains mainly functions for printing.
3662 * @param[in,out] tc is tree context.
3663 */
3664static void
aPiecek153b00f2021-04-20 13:52:57 +02003665trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003666{
aPiecek61d062b2020-11-02 11:05:09 +01003667 struct trt_parent_cache ca;
3668 uint32_t total_parents;
3669 uint32_t max_gap_before_type;
3670
aPiecekdc8fd572021-04-19 10:47:23 +02003671 if (!trb_tree_ctx_get_node(tc)) {
3672 return;
3673 }
3674
aPiecek61d062b2020-11-02 11:05:09 +01003675 ca = TRP_EMPTY_PARENT_CACHE;
3676 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3677 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3678
aPiecek3f247652021-04-19 13:40:25 +02003679 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003680 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3681 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003682 ca.lys_config = 0x0;
3683 }
aPiecekdc8fd572021-04-19 10:47:23 +02003684 }
3685
aPiecek61d062b2020-11-02 11:05:09 +01003686 for (uint32_t i = 0; i < total_parents; i++) {
3687 ly_print_(pc->out, "\n");
3688 trb_print_subtree_nodes(max_gap_before_type, wr, ca, pc, tc);
3689 pc->fp.modify.next_sibling(ca, tc);
3690 }
3691}
3692
aPiecek874ea4d2021-04-19 12:26:36 +02003693/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003694 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003695 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003696
3697/**
aPiecekdc8fd572021-04-19 10:47:23 +02003698 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003699 *
aPiecekdc8fd572021-04-19 10:47:23 +02003700 * @param[in] module YANG schema tree structure representing
3701 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003702 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003703 * @param[in] max_line_length is the maximum line length limit
3704 * that should not be exceeded.
3705 * @param[in,out] pc will be adapted to lysp_tree.
3706 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003707 */
3708static void
aPiecekdc8fd572021-04-19 10:47:23 +02003709trm_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 +01003710{
aPiecekdc8fd572021-04-19 10:47:23 +02003711 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003712 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003713 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003714 .pmod = module->parsed,
3715 .cmod = NULL,
3716 .pn = module->parsed ? module->parsed->data : NULL,
3717 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003718 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003719 };
aPiecek61d062b2020-11-02 11:05:09 +01003720
aPiecekdc8fd572021-04-19 10:47:23 +02003721 pc->out = out;
3722
3723 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003724 .parent = trop_modi_parent,
3725 .first_sibling = trop_modi_first_sibling,
3726 .next_sibling = trop_modi_next_sibling,
3727 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01003728 };
3729
aPiecekdc8fd572021-04-19 10:47:23 +02003730 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003731 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003732 .node = trop_read_node,
3733 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003734 };
3735
aPiecekdc8fd572021-04-19 10:47:23 +02003736 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003737 .print_features_names = tro_print_features_names,
3738 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01003739 };
3740
aPiecekdc8fd572021-04-19 10:47:23 +02003741 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01003742}
3743
3744/**
aPiecek3f247652021-04-19 13:40:25 +02003745 * @brief Settings if lysc_node are used for browsing through the tree.
3746 *
3747 * Pointers to current nodes will be set to module data.
3748 *
3749 * @param[in] module YANG schema tree structure representing
3750 * YANG module.
3751 * @param[in] out is output handler.
3752 * @param[in] max_line_length is the maximum line length limit
3753 * that should not be exceeded.
3754 * @param[in,out] pc will be adapted to lysc_tree.
3755 * @param[in,out] tc will be adapted to lysc_tree.
3756 */
3757static void
3758trm_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)
3759{
3760 *tc = (struct trt_tree_ctx) {
3761 .lysc_tree = 1,
3762 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003763 .pmod = module->parsed,
3764 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02003765 .tpn = NULL,
3766 .pn = NULL,
3767 .cn = module->compiled->data
3768 };
3769
3770 pc->out = out;
3771
3772 pc->fp.modify = (struct trt_fp_modify_ctx) {
3773 .parent = troc_modi_parent,
3774 .first_sibling = troc_modi_first_sibling,
3775 .next_sibling = troc_modi_next_sibling,
3776 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02003777 };
3778
3779 pc->fp.read = (struct trt_fp_read) {
3780 .module_name = tro_read_module_name,
3781 .node = troc_read_node,
3782 .if_sibling_exists = troc_read_if_sibling_exists
3783 };
3784
3785 pc->fp.print = (struct trt_fp_print) {
3786 .print_features_names = tro_print_features_names,
3787 .print_keys = tro_print_keys
3788 };
3789
3790 pc->max_line_length = max_line_length;
3791}
3792
3793/**
3794 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02003795 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02003796 * @param[in,out] tc resets to lysc browsing.
3797 */
3798static void
3799trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3800{
aPiecek9f792e52021-04-21 08:33:56 +02003801 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003802}
3803
3804/**
3805 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02003806 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02003807 * @param[in,out] tc resets to lysp browsing.
3808 */
3809static void
3810trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3811{
aPiecek9f792e52021-04-21 08:33:56 +02003812 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003813}
3814
3815/**
3816 * @brief If augment's target node is located on the current module.
3817 * @param[in] pn is examined augment.
3818 * @param[in] pmod is current module.
3819 * @return 1 if nodeid refers to the local node, otherwise 0.
3820 */
3821static ly_bool
3822trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
3823{
3824 const char *id, *prefix, *name;
3825 size_t prefix_len, name_len;
3826 const struct lys_module *mod;
3827 ly_bool ret = 0;
3828
3829 if (pn == NULL) {
3830 return ret;
3831 }
3832
3833 id = pn->nodeid;
3834 if (!id) {
3835 return ret;
3836 }
3837 /* only absolute-schema-nodeid is taken into account */
3838 assert(id[0] == '/');
3839 ++id;
3840
3841 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3842 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003843 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
aPiecek3f247652021-04-19 13:40:25 +02003844 ret = mod->parsed == pmod;
3845 } else {
3846 ret = 1;
3847 }
3848
3849 return ret;
3850}
3851
3852/**
aPiecek96baa7f2021-04-23 12:32:00 +02003853 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01003854 *
aPiecekdc8fd572021-04-19 10:47:23 +02003855 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02003856 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01003857 *
aPiecekdc8fd572021-04-19 10:47:23 +02003858 * @param[in] ks is section representation.
3859 * @param[in] pc contains mainly functions for printing.
3860 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01003861 */
3862static void
aPiecekdc8fd572021-04-19 10:47:23 +02003863trm_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 +01003864{
aPiecekdc8fd572021-04-19 10:47:23 +02003865 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3866 return;
3867 }
3868
3869 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
3870 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02003871 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003872 } else {
aPiecek153b00f2021-04-20 13:52:57 +02003873 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003874 }
3875}
3876
3877/**
aPiecek96baa7f2021-04-23 12:32:00 +02003878 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02003879 *
aPiecek96baa7f2021-04-23 12:32:00 +02003880 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02003881 *
3882 * @param[in] ks is section representation.
3883 * @param[in] pc contains mainly functions for printing.
3884 * @param[in,out] tc is the tree context.
3885 */
3886static void
3887trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3888{
3889 ly_bool grp_has_data = 0;
3890
3891 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3892 return;
3893 }
3894
3895 if (ks.type == TRD_KEYWORD_GROUPING) {
3896 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
3897 }
3898
3899 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
3900 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02003901 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003902}
3903
3904/**
3905 * @brief Print 'module' keyword, its name and all nodes.
3906 * @param[in] pc contains mainly functions for printing.
3907 * @param[in,out] tc is the tree context.
3908 */
3909static void
3910trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3911{
3912 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
3913}
3914
3915/**
3916 * @brief For all augment sections: print 'augment' keyword,
3917 * its target node and all nodes.
3918 * @param[in] pc contains mainly functions for printing.
3919 * @param[in,out] tc is the tree context.
3920 */
3921static void
3922trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3923{
3924 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02003925 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02003926
aPiecek3f247652021-04-19 13:40:25 +02003927 if (tc->lysc_tree) {
3928 origin_was_lysc_tree = 1;
3929 trm_reset_to_lysp_tree_ctx(pc, tc);
3930 }
3931
aPiecekdc8fd572021-04-19 10:47:23 +02003932 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02003933 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003934 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02003935 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02003936
aPiecek3f247652021-04-19 13:40:25 +02003937 if (origin_was_lysc_tree) {
3938 /* if lysc tree is used, then only augments targeting
3939 * another module are printed
3940 */
aPiecek9f792e52021-04-21 08:33:56 +02003941 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02003942 continue;
3943 }
3944 }
3945
aPiecekdc8fd572021-04-19 10:47:23 +02003946 if (once) {
3947 ly_print_(pc->out, "\n");
3948 ly_print_(pc->out, "\n");
3949 once = 0;
3950 } else {
3951 ly_print_(pc->out, "\n");
3952 }
3953
3954 trm_print_section_as_subtree(ks, pc, tc);
3955 }
aPiecek3f247652021-04-19 13:40:25 +02003956
3957 if (origin_was_lysc_tree) {
3958 trm_reset_to_lysc_tree_ctx(pc, tc);
3959 }
aPiecekdc8fd572021-04-19 10:47:23 +02003960}
3961
3962/**
3963 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
3964 * @param[in] pc contains mainly functions for printing.
3965 * @param[in,out] tc is the tree context.
3966 */
3967static void
3968trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3969{
3970 struct trt_keyword_stmt rpc;
3971
aPiecek01598c02021-04-23 14:18:24 +02003972 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003973
3974 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
3975 ly_print_(pc->out, "\n");
3976 ly_print_(pc->out, "\n");
3977 trm_print_section_as_family_tree(rpc, pc, tc);
3978 }
3979}
3980
3981/**
3982 * @brief For notifications section: print 'notifications' keyword
3983 * and all its nodes.
3984 * @param[in] pc contains mainly functions for printing.
3985 * @param[in,out] tc is the tree context.
3986 */
3987static void
3988trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3989{
3990 struct trt_keyword_stmt notifs;
3991
aPiecek01598c02021-04-23 14:18:24 +02003992 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003993
3994 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
3995 ly_print_(pc->out, "\n");
3996 ly_print_(pc->out, "\n");
3997 trm_print_section_as_family_tree(notifs, pc, tc);
3998 }
3999}
4000
4001/**
4002 * @brief For all grouping sections: print 'grouping' keyword, its name
4003 * and all nodes.
4004 * @param[in] pc contains mainly functions for printing.
4005 * @param[in,out] tc is the tree context.
4006 */
4007static void
4008trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4009{
4010 ly_bool once;
4011
aPiecek01598c02021-04-23 14:18:24 +02004012 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004013 return;
4014 }
4015
4016 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004017 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004018 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004019 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004020 if (once) {
4021 ly_print_(pc->out, "\n");
4022 ly_print_(pc->out, "\n");
4023 once = 0;
4024 } else {
4025 ly_print_(pc->out, "\n");
4026 }
4027 trm_print_section_as_subtree(ks, pc, tc);
4028 }
4029}
4030
4031/**
4032 * @brief For all yang-data sections: print 'yang-data' keyword
4033 * and all its nodes.
4034 * @param[in] pc contains mainly functions for printing.
4035 * @param[in,out] tc is the tree context.
4036 */
4037static void
aPiecek96baa7f2021-04-23 12:32:00 +02004038trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004039{
aPiecek96baa7f2021-04-23 12:32:00 +02004040 ly_bool once;
4041 LY_ARRAY_COUNT_TYPE count;
4042
4043 count = LY_ARRAY_COUNT(tc->pmod->exts);
4044 if (count == 0) {
4045 return;
4046 }
4047
4048 once = 1;
4049 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4050 struct trt_keyword_stmt ks;
4051
aPiecek01598c02021-04-23 14:18:24 +02004052 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004053 * ::lysp_ext_instance.parsed.
4054 */
4055 if (!tc->pmod->exts[u].parsed) {
4056 /* print at least the yang-data names */
4057 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4058 continue;
4059 }
4060
4061 ks = tro_modi_next_yang_data(tc, u);
4062 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4063 break;
4064 }
4065
4066 if (once) {
4067 ly_print_(pc->out, "\n");
4068 ly_print_(pc->out, "\n");
4069 once = 0;
4070 } else {
4071 ly_print_(pc->out, "\n");
4072 }
4073
4074 trm_print_section_as_family_tree(ks, pc, tc);
4075 }
aPiecekdc8fd572021-04-19 10:47:23 +02004076}
4077
4078/**
4079 * @brief Print sections module, augment, rpcs, notifications,
4080 * grouping, yang-data.
4081 * @param[in] pc contains mainly functions for printing.
4082 * @param[in,out] tc is the tree context.
4083 */
4084static void
4085trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4086{
4087 trm_print_module_section(pc, tc);
4088 trm_print_augmentations(pc, tc);
4089 trm_print_rpcs(pc, tc);
4090 trm_print_notifications(pc, tc);
4091 trm_print_groupings(pc, tc);
4092 trm_print_yang_data(pc, tc);
4093 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004094}
4095
aPiecek874ea4d2021-04-19 12:26:36 +02004096/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004097 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004098 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004099
4100LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004101tree_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 +01004102{
4103 struct trt_printer_ctx pc;
4104 struct trt_tree_ctx tc;
4105 struct ly_out *new_out;
4106 LY_ERR erc;
4107 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4108
aPiecekdc8fd572021-04-19 10:47:23 +02004109 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4110
aPiecek61d062b2020-11-02 11:05:09 +01004111 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4112 return erc;
4113 }
4114
4115 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004116 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4117 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4118 } else {
4119 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4120 }
aPiecek61d062b2020-11-02 11:05:09 +01004121
4122 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004123 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004124
4125 ly_out_free(new_out, NULL, 1);
4126
aPiecekdc8fd572021-04-19 10:47:23 +02004127 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004128}
4129
4130LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004131tree_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 +01004132{
aPiecek153b00f2021-04-20 13:52:57 +02004133 struct trt_printer_ctx pc;
4134 struct trt_tree_ctx tc;
4135 struct ly_out *new_out;
4136 struct trt_wrapper wr;
4137 LY_ERR erc;
4138 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4139
4140 assert(out && node);
4141
4142 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4143 return LY_EINVAL;
4144 }
4145
4146 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4147 return erc;
4148 }
4149
4150 line_length = line_length == 0 ? SIZE_MAX : line_length;
4151 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4152
4153 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
4154 trb_print_parents(node, &pc, &tc);
4155
4156 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4157 tc.cn = lysc_node_child(node);
4158 wr = trb_count_depth(tc.cn);
4159 trb_print_family_tree(wr, &pc, &tc);
4160 }
4161 ly_print_(out, "\n");
4162
4163 erc = clb_arg.last_error;
4164 ly_out_free(new_out, NULL, 1);
4165
4166 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004167}
4168
4169LY_ERR
aPiecek9f792e52021-04-21 08:33:56 +02004170tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options), size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004171{
aPiecek9f792e52021-04-21 08:33:56 +02004172 struct trt_printer_ctx pc;
4173 struct trt_tree_ctx tc;
4174 struct ly_out *new_out;
4175 LY_ERR erc;
4176 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4177
4178 assert(submodp);
4179 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4180
4181 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4182 return erc;
4183 }
4184
4185 line_length = line_length == 0 ? SIZE_MAX : line_length;
4186 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4187 tc.pmod = (struct lysp_module *)submodp;
4188 tc.tpn = submodp->data;
4189 tc.pn = tc.tpn;
4190
4191 trm_print_sections(&pc, &tc);
4192 erc = clb_arg.last_error;
4193
4194 ly_out_free(new_out, NULL, 1);
4195
4196 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004197}