blob: c7bb9dc9c0205fc9f1caeabdb8b6cccd7ceb1347 [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
40 * 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
aPiecek3f247652021-04-19 13:40:25 +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
47 * (\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
50 * traverse nodes in the tree, for example, to calculate the alignment
51 * gap before the nodes \<type\> in the YANG Tree Diagram.
52 * The obtained \ref trt_node is passed to the \ref TRP_trp functions
53 * 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
aPiecek3f247652021-04-19 13:40:25 +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
60 * prefix \b tro_. The Obtain functions provide information to
61 * \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
aPiecek3f247652021-04-19 13:40:25 +020082 * \ref TRP_tro, \ref TRP_trop or \ref TRP_troc functions because
83 * they are the only ones dependent on libyang implementation.
84 * In special cases, changes will also need to be made to the
85 * \ref TRP_trp functions if a special algorithm is needed to print
86 * (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
109 * 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. */
564 struct trt_keyword_stmt (*next_augment)(struct trt_tree_ctx *); /**< Jump to the augment section. */
565 struct trt_keyword_stmt (*get_rpcs)(struct trt_tree_ctx *); /**< Jump to the rpcs section. */
566 struct trt_keyword_stmt (*get_notifications)(struct trt_tree_ctx *); /**< Jump to the notifications section. */
567 struct trt_keyword_stmt (*next_grouping)(struct trt_tree_ctx *); /**< Jump to the grouping section. */
aPiecek61d062b2020-11-02 11:05:09 +0100568};
569
aPiecek874ea4d2021-04-19 12:26:36 +0200570/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100571 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200572 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100573
574/**
575 * @brief Functions that do not change the state of the tree_structure.
576 *
577 * For details see trt_fp_modify_ctx.
578 */
579struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200580 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
581 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
582 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100583};
584
aPiecek874ea4d2021-04-19 12:26:36 +0200585/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100586 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200587 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100588
589/**
aPiecek874ea4d2021-04-19 12:26:36 +0200590 * @brief A set of all necessary functions that must be provided
591 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100592 */
593struct trt_fp_all {
594 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
595 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
596 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
597};
598
aPiecek874ea4d2021-04-19 12:26:36 +0200599/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100600 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200601 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100602
603/**
aPiecek874ea4d2021-04-19 12:26:36 +0200604 * @brief Main structure for \ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100605 */
606struct trt_printer_ctx {
607 struct ly_out *out; /**< Handler to printing. */
aPiecek874ea4d2021-04-19 12:26:36 +0200608 struct trt_fp_all fp; /**< \ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100609 size_t max_line_length; /**< The maximum number of characters that can be
610 printed on one line, including the last. */
611};
612
aPiecek874ea4d2021-04-19 12:26:36 +0200613/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100614 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200615 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100616
617/**
618 * @brief The name of the section to which the node belongs.
619 */
620typedef enum {
621 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
622 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
623 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
624 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
625 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
626 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
627} trt_actual_section;
628
629/**
630 * @brief Types of nodes that have some effect on their children.
631 */
632typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200633 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
634 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
635 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
636 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100637} trt_ancestor_type;
638
639/**
640 * @brief Saved information when browsing the tree downwards.
641 *
aPiecek874ea4d2021-04-19 12:26:36 +0200642 * This structure helps prevent frequent retrieval of information
643 * from the tree. Functions \ref TRP_trb are designed to preserve
644 * this structures during their recursive calls. This functions do not
645 * interfere in any way with this data. This structure
aPiecekef1e58e2021-04-19 13:19:44 +0200646 * is used by \ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200647 * structure, can return a node with the correct data. The word
648 * \b parent is in the structure name, because this data refers to
649 * the last parent and at the same time the states of its
650 * ancestors data. Only the function jumping on the child
651 * (next_child(...)) creates this structure, because the pointer
652 * to the current node moves down the tree. It's like passing
653 * the genetic code to children. Some data must be inherited and
654 * there are two approaches to this problem. Either it will always
655 * be determined which inheritance states belong to the current node
656 * (which can lead to regular travel to the root node) or
657 * the inheritance states will be stored during the recursive calls.
658 * So the problem was solved by the second option. Why does
659 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200660 * the lysp tree. For walks through the lysc tree is trt_parent_cache
661 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100662 *
aPiecek874ea4d2021-04-19 12:26:36 +0200663 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100664 */
665struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200666 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
667 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
668 uint16_t lys_config; /**< Inherited config W or R. */
669 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100670};
671
672/**
673 * @brief Return trt_parent_cache filled with default values.
674 */
675#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200676 (struct trt_parent_cache) { \
677 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
678 .lys_config = LYS_CONFIG_W, .last_list = NULL \
679 }
aPiecek61d062b2020-11-02 11:05:09 +0100680
681/**
682 * @brief Main structure for browsing the libyang tree
683 */
684struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200685 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
686 It is assumed that once set, it does not change.
687 If it is true then trt_tree_ctx.pn and
688 trt_tree_ctx.tpn are not used.
689 If it is false then trt_tree_ctx.cn is not used. */
690 trt_actual_section section; /**< To which section pn points. */
691 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
692 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
693 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
694 union {
695 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
696 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
697 is set to TRD_SECT_YANG_DATA. */
698 };
699 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
aPiecek61d062b2020-11-02 11:05:09 +0100700};
701
aPiecek3f247652021-04-19 13:40:25 +0200702/**
703 * @brief Get lysp_node from trt_tree_ctx.cn.
704 */
705#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
706 ((const struct lysp_node *)CN->priv)
707
708/** Getter function for trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100709typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
710
aPiecekef1e58e2021-04-19 13:19:44 +0200711/**
712 * @brief Simple getter functions for lysp and lysc nodes.
713 *
714 * This structure is useful if we have a general algorithm
715 * (tro function) that can be used for both lysc and lysp nodes.
716 * Thanks to this structure, we prevent code redundancy.
717 * We don't have to write basically the same algorithm twice
718 * for lysp and lysc trees.
719 */
720struct tro_getters
721{
722 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
723 const void *(*next)(const void *); /**< Get sibling. */
724 const void *(*parent)(const void *); /**< Get parent. */
725 const void *(*child)(const void *); /**< Get child. */
726 const void *(*actions)(const void *); /**< Get actions. */
727 const void *(*action_input)(const void *); /**< Get input action from action node. */
728 const void *(*action_output)(const void *); /**< Get output action from action node. */
729 const void *(*notifs)(const void *); /**< Get notifs. */
730};
731
aPiecek874ea4d2021-04-19 12:26:36 +0200732/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100733 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200734 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100735
736/**
737 * @brief Print a substring but limited to the maximum length.
738 * @param[in] str is pointer to source.
739 * @param[in] len is number of characters to be printed.
740 * @param[in,out] out is output handler.
741 * @return str parameter shifted by len.
742 */
743static const char *
744trg_print_substr(const char *str, size_t len, struct ly_out *out)
745{
746 for (size_t i = 0; i < len; i++) {
747 ly_print_(out, "%c", str[0]);
748 str++;
749 }
750 return str;
751}
752
753/**
754 * @brief Pointer is not NULL and does not point to an empty string.
755 * @param[in] str is pointer to string to be checked.
756 * @return 1 if str pointing to non empty string otherwise 0.
757 */
758static ly_bool
759trg_charptr_has_data(const char *str)
760{
761 return (str) && (str[0] != '\0');
762}
763
764/**
aPiecek874ea4d2021-04-19 12:26:36 +0200765 * @brief Check if @p word in @p src is present where words are
766 * delimited by @p delim.
767 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100768 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200769 * @param[in] delim is delimiter between @p words in @p src.
770 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100771 */
772static ly_bool
773trg_word_is_present(const char *src, const char *word, char delim)
774{
775 const char *hit;
776
777 if ((!src) || (src[0] == '\0') || (!word)) {
778 return 0;
779 }
780
781 hit = strstr(src, word);
782
783 if (hit) {
784 /* word was founded at the begin of src
785 * OR it match somewhere after delim
786 */
787 if ((hit == src) || (hit[-1] == delim)) {
788 /* end of word was founded at the end of src
789 * OR end of word was match somewhere before delim
790 */
791 char delim_or_end = (hit + strlen(word))[0];
792 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
793 return 1;
794 }
795 }
796 /* after -> hit is just substr and it's not the whole word */
797 /* jump to the next word */
798 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
799 /* skip delim */
800 src = src[0] == '\0' ? src : src + 1;
801 /* continue with searching */
802 return trg_word_is_present(src, word, delim);
803 } else {
804 return 0;
805 }
806}
807
aPiecek874ea4d2021-04-19 12:26:36 +0200808/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100809 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200810 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100811
812/**
aPiecek874ea4d2021-04-19 12:26:36 +0200813 * @brief Write callback for ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100814 *
aPiecek874ea4d2021-04-19 12:26:36 +0200815 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100816 * @param[in] buf contains input characters
817 * @param[in] count is number of characters in buf.
818 * @return Number of printed bytes.
819 * @return Negative value in case of error.
820 */
821static ssize_t
822trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
823{
824 LY_ERR erc = LY_SUCCESS;
825 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
826
827 switch (data->mode) {
828 case TRD_PRINT:
829 erc = ly_write_(data->out, buf, count);
830 break;
831 case TRD_CHAR_COUNT:
832 data->counter = data->counter + count;
833 break;
834 default:
835 break;
836 }
837
838 if (erc != LY_SUCCESS) {
839 data->last_error = erc;
840 return -1;
841 } else {
842 return count;
843 }
844}
845
846/**
847 * @brief Check that indent in node can be considered as equivalent.
848 * @param[in] first is the first indent in node.
849 * @param[in] second is the second indent in node.
850 * @return 1 if indents are equivalent otherwise 0.
851 */
852static ly_bool
853trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
854{
855 const ly_bool a = first.type == second.type;
856 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
857 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
858 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
859
860 return a && b && c && d;
861}
862
863/**
aPiecek874ea4d2021-04-19 12:26:36 +0200864 * @brief Setting space character because node is last sibling.
865 * @param[in] wr is wrapper over which the shift operation
866 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100867 * @return New shifted wrapper.
868 */
869static struct trt_wrapper
870trp_wrapper_set_shift(struct trt_wrapper wr)
871{
872 assert(wr.actual_pos < 64);
873 /* +--<node>
874 * +--<node>
875 */
876 wr.actual_pos++;
877 return wr;
878}
879
880/**
aPiecek874ea4d2021-04-19 12:26:36 +0200881 * @brief Setting '|' symbol because node is divided or
882 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100883 * @param[in] wr is source of wrapper.
884 * @return New wrapper which is marked at actual position and shifted.
885 */
886static struct trt_wrapper
887trp_wrapper_set_mark(struct trt_wrapper wr)
888{
889 assert(wr.actual_pos < 64);
890 wr.bit_marks1 |= 1U << wr.actual_pos;
891 return trp_wrapper_set_shift(wr);
892}
893
894/**
895 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
896 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200897 * @param[in] last_one is flag. Value 1 saying if the node is the last
898 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100899 * @return New wrapper for the actual node.
900 */
901static struct trt_wrapper
902trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
903{
904 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
905}
906
907/**
908 * @brief Test if the wrappers are equivalent.
909 * @param[in] first is the first wrapper.
910 * @param[in] second is the second wrapper.
911 * @return 1 if the wrappers are equivalent otherwise 0.
912 */
913static ly_bool
914trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
915{
916 const ly_bool a = first.type == second.type;
917 const ly_bool b = first.bit_marks1 == second.bit_marks1;
918 const ly_bool c = first.actual_pos == second.actual_pos;
919
920 return a && b && c;
921}
922
923/**
924 * @brief Print " | " sequence on line.
925 * @param[in] wr is wrapper to be printed.
926 * @param[in,out] out is output handler.
927 */
928static void
929trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
930{
931 uint32_t lb;
932
933 if (wr.type == TRD_WRAPPER_TOP) {
934 lb = TRD_INDENT_LINE_BEGIN;
935 } else if (wr.type == TRD_WRAPPER_BODY) {
936 lb = TRD_INDENT_LINE_BEGIN * 2;
937 } else {
938 lb = TRD_INDENT_LINE_BEGIN;
939 }
940
941 ly_print_(out, "%*c", lb, ' ');
942
943 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
944 return;
945 }
946
947 for (uint32_t i = 0; i < wr.actual_pos; i++) {
948 /** Test if the bit on the index is set. */
949 if ((wr.bit_marks1 >> i) & 1U) {
950 ly_print_(out, "|");
951 } else {
952 ly_print_(out, " ");
953 }
954
955 if (i != wr.actual_pos) {
956 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
957 }
958 }
959}
960
961/**
962 * @brief Check if struct trt_node is empty.
963 * @param[in] node is item to test.
964 * @return 1 if node is considered empty otherwise 0.
965 */
966static ly_bool
967trp_node_is_empty(struct trt_node node)
968{
969 const ly_bool a = !node.iffeatures;
970 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
971 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
972 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
973 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
974
975 return a && b && c && d && e;
976}
977
978/**
aPiecek874ea4d2021-04-19 12:26:36 +0200979 * @brief Check if [\<keys\>], \<type\> and
980 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +0100981 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +0200982 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
983 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100984 */
985static ly_bool
986trp_node_body_is_empty(struct trt_node node)
987{
988 const ly_bool a = !node.iffeatures;
989 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
990 const ly_bool c = node.name.type != TRD_NODE_KEYS;
991
992 return a && b && c;
993}
994
995/**
996 * @brief Print \<status\> of the node.
997 * @param[in] status_type is type of status.
998 * @param[in,out] out is output handler.
999 */
1000static void
1001trp_print_status(trt_status_type status_type, struct ly_out *out)
1002{
1003 switch (status_type) {
1004 case TRD_STATUS_TYPE_CURRENT:
1005 ly_print_(out, "%c", '+');
1006 break;
1007 case TRD_STATUS_TYPE_DEPRECATED:
1008 ly_print_(out, "%c", 'x');
1009 break;
1010 case TRD_STATUS_TYPE_OBSOLETE:
1011 ly_print_(out, "%c", 'o');
1012 break;
1013 default:
1014 break;
1015 }
1016}
1017
1018/**
1019 * @brief Print \<flags\>.
1020 * @param[in] flags_type is type of \<flags\>.
1021 * @param[in,out] out is output handler.
1022 */
1023static void
1024trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1025{
1026 switch (flags_type) {
1027 case TRD_FLAGS_TYPE_RW:
1028 ly_print_(out, "%s", "rw");
1029 break;
1030 case TRD_FLAGS_TYPE_RO:
1031 ly_print_(out, "%s", "ro");
1032 break;
1033 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1034 ly_print_(out, "%s", "-w");
1035 break;
1036 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1037 ly_print_(out, "%s", "-u");
1038 break;
1039 case TRD_FLAGS_TYPE_RPC:
1040 ly_print_(out, "%s", "-x");
1041 break;
1042 case TRD_FLAGS_TYPE_NOTIF:
1043 ly_print_(out, "%s", "-n");
1044 break;
1045 case TRD_FLAGS_TYPE_MOUNT_POINT:
1046 ly_print_(out, "%s", "mp");
1047 break;
1048 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001049 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001050 break;
1051 }
1052}
1053
1054/**
1055 * @brief Get size of the \<flags\>.
1056 * @param[in] flags_type is type of \<flags\>.
1057 * @return 0 if flags_type is not set otherwise 2.
1058 */
1059static size_t
1060trp_get_flags_strlen(trt_flags_type flags_type)
1061{
1062 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1063}
1064
1065/**
1066 * @brief Print entire struct trt_node_name structure.
1067 * @param[in] node_name is item to print.
1068 * @param[in,out] out is output handler.
1069 */
1070static void
1071trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1072{
1073 const char *mod_prefix;
1074 const char *colon;
1075 const char trd_node_name_suffix_choice[] = ")";
1076 const char trd_node_name_suffix_case[] = ")";
1077 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1078 const char trd_opts_container[] = "!"; /**< For a presence container. */
1079 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1080 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1081 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1082
1083 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1084 return;
1085 }
1086
1087 if (node_name.module_prefix) {
1088 mod_prefix = node_name.module_prefix;
1089 colon = ":";
1090 } else {
1091 mod_prefix = "";
1092 colon = "";
1093 }
1094
1095 switch (node_name.type) {
1096 case TRD_NODE_ELSE:
1097 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1098 break;
1099 case TRD_NODE_CASE:
1100 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1101 break;
1102 case TRD_NODE_CHOICE:
1103 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1104 break;
1105 case TRD_NODE_OPTIONAL_CHOICE:
1106 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);
1107 break;
1108 case TRD_NODE_OPTIONAL:
1109 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1110 break;
1111 case TRD_NODE_CONTAINER:
1112 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1113 break;
1114 case TRD_NODE_LISTLEAFLIST:
1115 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1116 break;
1117 case TRD_NODE_KEYS:
1118 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1119 break;
1120 case TRD_NODE_TOP_LEVEL1:
1121 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1122 break;
1123 case TRD_NODE_TOP_LEVEL2:
1124 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1125 break;
1126 case TRD_NODE_TRIPLE_DOT:
1127 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1128 break;
1129 default:
1130 break;
1131 }
1132}
1133
1134/**
aPiecek874ea4d2021-04-19 12:26:36 +02001135 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1136 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001137 * @param[in] node_name is structure containing the 'mark'.
1138 * @return 1 if contain otherwise 0.
1139 */
1140static ly_bool
1141trp_mark_is_used(struct trt_node_name node_name)
1142{
1143 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1144 return 0;
1145 }
1146
1147 switch (node_name.type) {
1148 case TRD_NODE_ELSE:
1149 case TRD_NODE_CASE:
1150 case TRD_NODE_KEYS:
1151 return 0;
1152 default:
1153 return 1;
1154 }
1155}
1156
1157/**
1158 * @brief Print opts keys.
1159 * @param[in] node_name contains type of the node with his name.
1160 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001161 * @param[in] cf is basically a pointer to the function that prints
1162 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001163 * @param[in,out] out is output handler.
1164 */
1165static void
1166trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1167{
1168 if (node_name.type != TRD_NODE_KEYS) {
1169 return;
1170 }
1171
1172 /* <name><mark>___<keys>*/
1173 if (btw_name_opts > 0) {
1174 ly_print_(out, "%*c", btw_name_opts, ' ');
1175 }
1176 ly_print_(out, "[");
1177 cf.pf(cf.ctx, out);
1178 ly_print_(out, "]");
1179}
1180
1181/**
1182 * @brief Print entire struct trt_type structure.
1183 * @param[in] type is item to print.
1184 * @param[in,out] out is output handler.
1185 */
1186static void
1187trp_print_type(struct trt_type type, struct ly_out *out)
1188{
1189 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1190 return;
1191 }
1192
1193 switch (type.type) {
1194 case TRD_TYPE_NAME:
1195 ly_print_(out, "%s", type.str);
1196 break;
1197 case TRD_TYPE_TARGET:
1198 ly_print_(out, "-> %s", type.str);
1199 break;
1200 case TRD_TYPE_LEAFREF:
1201 ly_print_(out, "leafref");
1202 default:
1203 break;
1204 }
1205}
1206
1207/**
1208 * @brief Print all iffeatures of node
1209 *
1210 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001211 * @param[in] cf is basically a pointer to the function that prints
1212 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001213 * @param[in,out] out is output handler.
1214 */
1215static void
1216trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1217{
1218 if (iffeature_flag) {
1219 ly_print_(out, "{");
1220 cf.pf(cf.ctx, out);
1221 ly_print_(out, "}?");
1222 }
1223}
1224
1225/**
1226 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1227 * @param[in] node contains items to print.
1228 * @param[in] out is output handler.
1229 */
1230static void
1231trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1232{
1233 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1234 trp_print_node_name(node.name, out);
1235 return;
1236 }
1237 /* <status>--<flags> */
1238 trp_print_status(node.status, out);
1239 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001240 /* If the node is a case node, there is no space before the <name>
1241 * also case node has no flags.
1242 */
aPiecek61d062b2020-11-02 11:05:09 +01001243 if (node.name.type != TRD_NODE_CASE) {
1244 trp_print_flags(node.flags, out);
1245 ly_print_(out, " ");
1246 }
1247 /* <name> */
1248 trp_print_node_name(node.name, out);
1249}
1250
1251/**
aPiecek874ea4d2021-04-19 12:26:36 +02001252 * @brief Print alignment (spaces) instead of
1253 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001254 * @param[in] node contains items to print.
1255 * @param[in] out is output handler.
1256 */
1257static void
1258trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1259{
1260 uint32_t space = trp_get_flags_strlen(node.flags);
1261
1262 if (node.name.type == TRD_NODE_CASE) {
1263 /* :(<name> */
1264 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1265 } else if (node.name.type == TRD_NODE_CHOICE) {
1266 /* (<name> */
1267 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1268 } else {
1269 /* _<name> */
1270 space += strlen(" ");
1271 }
1272
1273 /* <name>
1274 * __
1275 */
1276 space += TRD_INDENT_LONG_LINE_BREAK;
1277
1278 ly_print_(out, "%*c", space, ' ');
1279}
1280
1281/**
1282 * @brief Print struct trt_node structure.
1283 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001284 * @param[in] pck package of functions for
1285 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001286 * @param[in] indent is the indent in node.
1287 * @param[in,out] out is output handler.
1288 */
1289static void
1290trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1291{
1292 ly_bool triple_dot;
1293 ly_bool divided;
1294 struct trt_cf_print cf_print_keys;
1295 struct trt_cf_print cf_print_iffeatures;
1296
1297 if (trp_node_is_empty(node)) {
1298 return;
1299 }
1300
1301 /* <status>--<flags> <name><opts> <type> <if-features> */
1302 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1303 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1304
1305 if (triple_dot) {
1306 trp_print_node_name(node.name, out);
1307 return;
1308 } else if (!divided) {
1309 trp_print_node_up_to_name(node, out);
1310 } else {
1311 trp_print_divided_node_up_to_name(node, out);
1312 }
1313
1314 /* <opts> */
1315 /* <name>___<opts>*/
1316 cf_print_keys.ctx = pck.tree_ctx;
1317 cf_print_keys.pf = pck.fps.print_keys;
1318
1319 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1320
1321 /* <opts>__<type> */
1322 if (indent.btw_opts_type > 0) {
1323 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1324 }
1325
1326 /* <type> */
1327 trp_print_type(node.type, out);
1328
1329 /* <type>__<iffeatures> */
1330 if (indent.btw_type_iffeatures > 0) {
1331 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1332 }
1333
1334 /* <iffeatures> */
1335 cf_print_iffeatures.ctx = pck.tree_ctx;
1336 cf_print_iffeatures.pf = pck.fps.print_features_names;
1337
1338 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1339}
1340
1341/**
aPiecek874ea4d2021-04-19 12:26:36 +02001342 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001343 * @param[in] ks is keyword statement to print.
1344 * @param[in,out] out is output handler
1345 */
1346static void
1347trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1348{
1349 switch (ks.type) {
1350 case TRD_KEYWORD_MODULE:
1351 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1352 return;
1353 case TRD_KEYWORD_SUBMODULE:
1354 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1355 return;
1356 default:
1357 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1358 switch (ks.type) {
1359 case TRD_KEYWORD_AUGMENT:
1360 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1361 break;
1362 case TRD_KEYWORD_RPC:
1363 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1364 break;
1365 case TRD_KEYWORD_NOTIF:
1366 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1367 break;
1368 case TRD_KEYWORD_GROUPING:
1369 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1370 break;
1371 case TRD_KEYWORD_YANG_DATA:
1372 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1373 break;
1374 default:
1375 break;
1376 }
1377 break;
1378 }
1379}
1380
1381/**
1382 * @brief Get string length of stored keyword.
1383 * @param[in] type is type of the keyword statement.
1384 * @return length of the keyword statement name.
1385 */
1386static size_t
1387trp_keyword_type_strlen(trt_keyword_type type)
1388{
1389 switch (type) {
1390 case TRD_KEYWORD_MODULE:
1391 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1392 case TRD_KEYWORD_SUBMODULE:
1393 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1394 case TRD_KEYWORD_AUGMENT:
1395 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1396 case TRD_KEYWORD_RPC:
1397 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1398 case TRD_KEYWORD_NOTIF:
1399 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1400 case TRD_KEYWORD_GROUPING:
1401 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1402 case TRD_KEYWORD_YANG_DATA:
1403 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1404 default:
1405 return 0;
1406 }
1407}
1408
1409/**
aPiecek874ea4d2021-04-19 12:26:36 +02001410 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001411 * @param[in] ks is keyword statement structure.
1412 * @param[in] mll is max line length.
1413 * @param[in,out] out is output handler.
1414 */
1415static void
1416trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1417{
1418 uint32_t ind_initial;
1419 uint32_t ind_divided;
1420 /* flag if path must be splitted to more lines */
1421 ly_bool linebreak_was_set;
1422 /* flag if at least one subpath was printed */
1423 ly_bool subpath_printed;
1424 /* the sum of the sizes of the substrings on the current line */
1425 uint32_t how_far;
1426 /* pointer to start of the subpath */
1427 const char *sub_ptr;
1428 /* size of subpath from sub_ptr */
1429 size_t sub_len;
1430
1431 if ((!ks.str) || (ks.str[0] == '\0')) {
1432 return;
1433 }
1434
1435 /* module name cannot be splitted */
1436 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1437 ly_print_(out, "%s", ks.str);
1438 return;
1439 }
1440
1441 /* after -> for trd_keyword_stmt_body do */
1442
1443 /* set begin indentation */
1444 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1445 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1446 linebreak_was_set = 0;
1447 subpath_printed = 0;
1448 how_far = 0;
1449 sub_ptr = ks.str;
1450 sub_len = 0;
1451
1452 while (sub_ptr[0] != '\0') {
1453 uint32_t ind;
1454 /* skip slash */
1455 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
1456 /* get position of the end of substr */
1457 tmp = strchr(tmp, '/');
1458 /* set correct size if this is a last substring */
1459 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1460 /* actualize sum of the substring's sizes on the current line */
1461 how_far += sub_len;
1462 /* correction due to colon character if it this is last substring */
1463 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1464 /* choose indentation which depends on
1465 * whether the string is printed on multiple lines or not
1466 */
1467 ind = linebreak_was_set ? ind_divided : ind_initial;
1468 if (ind + how_far <= mll) {
1469 /* printing before max line length */
1470 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1471 subpath_printed = 1;
1472 } else {
1473 /* printing on new line */
1474 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001475 /* first subpath is too long
1476 * but print it at first line anyway
1477 */
aPiecek61d062b2020-11-02 11:05:09 +01001478 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1479 subpath_printed = 1;
1480 continue;
1481 }
1482 ly_print_(out, "\n");
1483 ly_print_(out, "%*c", ind_divided, ' ');
1484 linebreak_was_set = 1;
1485 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1486 how_far = sub_len;
1487 subpath_printed = 1;
1488 }
1489 }
1490}
1491
1492/**
aPiecek874ea4d2021-04-19 12:26:36 +02001493 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001494 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001495 * @param[in] grp_has_data is flag only for grouping section.
1496 * Set to 1 if grouping section has some nodes.
1497 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001498 * @param[in,out] out is output handler.
1499 */
1500static void
aPiecekdc8fd572021-04-19 10:47:23 +02001501trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001502{
1503 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001504 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1505 return;
1506 } else {
1507 ly_print_(out, ":");
1508 }
aPiecek61d062b2020-11-02 11:05:09 +01001509 }
1510}
1511
1512/**
1513 * @brief Print entire struct trt_keyword_stmt structure.
1514 * @param[in] ks is item to print.
1515 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001516 * @param[in] grp_has_data is flag only for grouping section.
1517 * Set to 1 if grouping section has some nodes.
1518 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001519 * @param[in,out] out is output handler.
1520 */
1521static void
aPiecek874ea4d2021-04-19 12:26:36 +02001522trp_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 +01001523{
1524 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1525 return;
1526 }
1527 trt_print_keyword_stmt_begin(ks, out);
1528 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001529 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001530}
1531
aPiecek874ea4d2021-04-19 12:26:36 +02001532/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001533 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001534 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001535
1536/**
aPiecek874ea4d2021-04-19 12:26:36 +02001537 * @brief Printing one line including wrapper and node
1538 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001539 * @param[in] node is \<node\> representation.
1540 * @param[in] pck contains special printing functions callback.
1541 * @param[in] indent contains wrapper and indent in node numbers.
1542 * @param[in,out] out is output handler.
1543 */
1544static void
1545trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1546{
1547 trp_print_wrapper(indent.wrapper, out);
1548 trp_print_node(node, pck, indent.in_node, out);
1549}
1550
1551/**
aPiecek874ea4d2021-04-19 12:26:36 +02001552 * @brief Printing one line including wrapper and
1553 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001554 * @param[in] node is \<node\> representation.
1555 * @param[in] wr is wrapper for printing indentation before node.
1556 * @param[in] out is output handler.
1557 */
1558static void
1559trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1560{
1561 trp_print_wrapper(wr, out);
1562 trp_print_node_up_to_name(node, out);
1563}
1564
1565/**
aPiecek874ea4d2021-04-19 12:26:36 +02001566 * @brief Check if leafref target must be change to string 'leafref'
1567 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001568 * @param[in] node containing leafref target.
1569 * @param[in] wr is wrapper for printing indentation before node.
1570 * @param[in] mll is max line length.
1571 * @param[in] out is output handler.
1572 * @return true if leafref must be changed to string 'leafref'.
1573 */
1574static ly_bool
1575trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1576{
1577 struct ly_out_clb_arg *data;
1578
1579 if (node.type.type != TRD_TYPE_TARGET) {
1580 return 0;
1581 }
1582
1583 /* set ly_out to counting characters */
1584 data = out->method.clb.arg;
1585
1586 data->counter = 0;
1587 data->mode = TRD_CHAR_COUNT;
1588 /* count number of printed bytes */
1589 trp_print_wrapper(wr, out);
1590 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1591 trp_print_divided_node_up_to_name(node, out);
1592 data->mode = TRD_PRINT;
1593
1594 return data->counter + strlen(node.type.str) > mll;
1595}
1596
1597/**
1598 * @brief Get default indent in node based on node values.
1599 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001600 * @return Default indent in node assuming that the node
1601 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001602 */
1603static struct trt_indent_in_node
1604trp_default_indent_in_node(struct trt_node node)
1605{
1606 struct trt_indent_in_node ret;
1607
1608 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1609
1610 /* btw_name_opts */
1611 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1612
1613 /* btw_opts_type */
1614 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1615 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1616 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1617 TRD_INDENT_BEFORE_TYPE;
1618 } else {
1619 ret.btw_opts_type = 0;
1620 }
1621
1622 /* btw_type_iffeatures */
1623 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1624
1625 return ret;
1626}
1627
1628/**
1629 * @brief Setting linebreaks in trt_indent_in_node.
1630 *
1631 * The order where the linebreak tag can be placed is from the end.
1632 *
aPiecek874ea4d2021-04-19 12:26:36 +02001633 * @param[in] indent containing alignment lengths
1634 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001635 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001636 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1637 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001638 */
1639static struct trt_indent_in_node
1640trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1641{
1642 /* somewhere must be set a line break in node */
1643 struct trt_indent_in_node ret = indent;
1644
1645 /* gradually break the node from the end */
1646 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1647 ret.btw_type_iffeatures = TRD_LINEBREAK;
1648 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1649 ret.btw_opts_type = TRD_LINEBREAK;
1650 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1651 /* set line break between name and opts */
1652 ret.btw_name_opts = TRD_LINEBREAK;
1653 } else {
1654 /* it is not possible to place a more line breaks,
1655 * unfortunately the max_line_length constraint is violated
1656 */
1657 ret.type = TRD_INDENT_IN_NODE_FAILED;
1658 }
1659 return ret;
1660}
1661
1662/**
1663 * @brief Get the first half of the node based on the linebreak mark.
1664 *
1665 * Items in the second half of the node will be empty.
1666 *
1667 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001668 * @param[in] indent contains information in which part of the \<node\>
1669 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001670 * @return first half of the node, indent is unchanged.
1671 */
1672static struct trt_pair_indent_node
1673trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1674{
1675 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1676
1677 if (indent.btw_name_opts == TRD_LINEBREAK) {
1678 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1679 ret.node.type = TRP_EMPTY_TRT_TYPE;
1680 ret.node.iffeatures = 0;
1681 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1682 ret.node.type = TRP_EMPTY_TRT_TYPE;
1683 ret.node.iffeatures = 0;
1684 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1685 ret.node.iffeatures = 0;
1686 }
1687
1688 return ret;
1689}
1690
1691/**
1692 * @brief Get the second half of the node based on the linebreak mark.
1693 *
1694 * Items in the first half of the node will be empty.
1695 * Indentations belonging to the first node will be reset to zero.
1696 *
1697 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001698 * @param[in] indent contains information in which part of the \<node\>
1699 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001700 * @return second half of the node, indent is newly set.
1701 */
1702static struct trt_pair_indent_node
1703trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1704{
1705 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1706
1707 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001708 /* Logically, the information up to token <opts> should
1709 * be deleted, but the the trp_print_node function needs it to
1710 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001711 */
1712 ret.indent.btw_name_opts = 0;
1713 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1714 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1715 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1716 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1717 ret.indent.btw_name_opts = 0;
1718 ret.indent.btw_opts_type = 0;
1719 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1720 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1721 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1722 ret.node.type = TRP_EMPTY_TRT_TYPE;
1723 ret.indent.btw_name_opts = 0;
1724 ret.indent.btw_opts_type = 0;
1725 ret.indent.btw_type_iffeatures = 0;
1726 }
1727 return ret;
1728}
1729
1730/**
1731 * @brief Get the correct alignment for the node.
1732 *
aPiecek874ea4d2021-04-19 12:26:36 +02001733 * This function is recursively called itself. It's like a backend
1734 * function for a function trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001735 *
1736 * @param[in] node is \<node\> representation.
1737 * @param[in] pck contains speciall callback functions for printing.
1738 * @param[in] indent contains wrapper and indent in node numbers.
1739 * @param[in] mll is max line length.
1740 * @param[in,out] cnt counting number of characters to print.
1741 * @param[in,out] out is output handler.
1742 * @return pair of node and indentation numbers of that node.
1743 */
1744static struct trt_pair_indent_node
1745trp_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)
1746{
1747 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1748
1749 trp_print_line(node, pck, indent, out);
1750
1751 if (*cnt <= mll) {
1752 /* success */
1753 return ret;
1754 } else {
1755 ret.indent = trp_indent_in_node_place_break(ret.indent);
1756 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1757 /* erase information in node due to line break */
1758 ret = trp_first_half_node(node, ret.indent);
1759 /* check if line fits, recursive call */
1760 *cnt = 0;
1761 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1762 /* make sure that the result will be with the status divided
1763 * or eventually with status failed */
1764 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1765 }
1766 return ret;
1767 }
1768}
1769
1770/**
1771 * @brief Get the correct alignment for the node.
1772 *
1773 * @param[in] node is \<node\> representation.
1774 * @param[in] pck contains speciall callback functions for printing.
1775 * @param[in] indent contains wrapper and indent in node numbers.
1776 * @param[in] mll is max line length.
1777 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001778 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1779 * line, some indent variable has negative value as a line break sign.
1780 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1781 * all indent variables values has non-negative number.
1782 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1783 * line, all indent variables has negative or zero values,
1784 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001785 */
1786static struct trt_pair_indent_node
1787trp_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)
1788{
1789 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1790 struct ly_out_clb_arg *data;
1791
1792 /* set ly_out to counting characters */
1793 data = out->method.clb.arg;
1794
1795 data->counter = 0;
1796 data->mode = TRD_CHAR_COUNT;
1797 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1798 data->mode = TRD_PRINT;
1799
1800 return ret;
1801}
1802
1803/**
aPiecek874ea4d2021-04-19 12:26:36 +02001804 * @brief Auxiliary function for trp_print_entire_node()
1805 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001806 * @param[in] node is node representation.
1807 * @param[in] ppck contains speciall callback functions for printing.
1808 * @param[in] ipck contains wrapper and indent in node numbers.
1809 * @param[in] mll is max line length.
1810 * @param[in,out] out is output handler.
1811 */
1812static void
1813trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1814{
1815 ly_bool entire_node_was_printed;
1816 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1817
1818 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1819 /* nothing can be done, continue as usual */
1820 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1821 }
1822
1823 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1824 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1825
1826 if (!entire_node_was_printed) {
1827 ly_print_(out, "\n");
1828 /* continue with second half node */
1829 ind_node = trp_second_half_node(node, ind_node.indent);
1830 /* continue with printing node */
1831 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1832 } else {
1833 return;
1834 }
1835}
1836
1837/**
aPiecek874ea4d2021-04-19 12:26:36 +02001838 * @brief Printing of the wrapper and the whole node,
1839 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001840 * @param[in] node is node representation.
1841 * @param[in] ppck contains speciall callback functions for printing.
1842 * @param[in] ipck contains wrapper and indent in node numbers.
1843 * @param[in] mll is max line length.
1844 * @param[in,out] out is output handler.
1845 */
1846static void
1847trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1848{
1849 struct trt_pair_indent_node ind_node1;
1850 struct trt_pair_indent_node ind_node2;
1851 struct trt_pck_indent tmp;
1852
1853 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1854 node.type.type = TRD_TYPE_LEAFREF;
1855 }
1856
1857 /* check if normal indent is possible */
1858 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1859
1860 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1861 /* node fits to one line */
1862 trp_print_line(node, ppck, ipck, out);
1863 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1864 /* node will be divided */
1865 /* print first half */
1866 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1867 /* pretend that this is normal node */
1868 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1869
1870 trp_print_line(ind_node1.node, ppck, tmp, out);
1871 ly_print_(out, "\n");
1872
1873 /* continue with second half on new line */
1874 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1875 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1876
1877 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1878 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1879 /* node name is too long */
1880 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1881
1882 if (trp_node_body_is_empty(node)) {
1883 return;
1884 } else {
1885 ly_print_(out, "\n");
1886
1887 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1888 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1889 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1890
1891 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1892 }
1893
1894 }
1895}
1896
aPiecek874ea4d2021-04-19 12:26:36 +02001897/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001898 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001899 *********************************************************************/
1900
1901/**
1902 * @brief Get nodetype.
1903 * @param[in] node is any lysp_node.
1904 */
1905static uint16_t
1906trop_nodetype(const void *node)
1907{
1908 return ((const struct lysp_node *)node)->nodetype;
1909}
1910
1911/**
1912 * @brief Get sibling.
1913 * @param[in] node is any lysp_node.
1914 */
1915static const void *
1916trop_next(const void *node)
1917{
1918 return ((const struct lysp_node *)node)->next;
1919}
1920
1921/**
1922 * @brief Get parent.
1923 * @param[in] node is any lysp_node.
1924 */
1925static const void *
1926trop_parent(const void *node)
1927{
1928 return ((const struct lysp_node *)node)->parent;
1929}
1930
1931/**
1932 * @brief Try to get child.
1933 * @param[in] node is any lysp_node.
1934 */
1935static const void *
1936trop_child(const void *node)
1937{
1938 return lysp_node_child(node);
1939}
1940
1941/**
1942 * @brief Try to get action.
1943 * @param[in] node is any lysp_node.
1944 */
1945static const void *
1946trop_actions(const void *node)
1947{
1948 return lysp_node_actions(node);
1949}
1950
1951/**
1952 * @brief Try to get action.
1953 * @param[in] node must be of type lysp_node_action.
1954 */
1955static const void *
1956trop_action_input(const void *node)
1957{
1958 return &((const struct lysp_node_action *)node)->input;
1959}
1960
1961/**
1962 * @brief Try to get action.
1963 * @param[in] node must be of type lysp_node_action.
1964 */
1965static const void *
1966trop_action_output(const void *node)
1967{
1968 return &((const struct lysp_node_action *)node)->output;
1969}
1970
1971/**
1972 * @brief Try to get action.
1973 * @param[in] node is any lysp_node.
1974 */
1975static const void *
1976trop_notifs(const void *node)
1977{
1978 return lysp_node_notifs(node);
1979}
1980
1981/**
1982 * @brief Fill struct tro_getters with \ref TRP_trop getters
1983 * which are adapted to lysp nodes.
1984 */
1985static struct tro_getters
1986trop_init_getters()
1987{
1988 return (struct tro_getters) {
1989 .nodetype = trop_nodetype,
1990 .next = trop_next,
1991 .parent = trop_parent,
1992 .child = trop_child,
1993 .actions = trop_actions,
1994 .action_input = trop_action_input,
1995 .action_output = trop_action_output,
1996 .notifs = trop_notifs
1997 };
1998}
1999
aPiecek3f247652021-04-19 13:40:25 +02002000/**
2001 * @brief Get nodetype.
2002 * @param[in] node is any lysc_node.
2003 */
2004static uint16_t
2005troc_nodetype(const void *node)
2006{
2007 return ((const struct lysc_node *)node)->nodetype;
2008}
2009
2010/**
2011 * @brief Get sibling.
2012 * @param[in] node is any lysc_node.
2013 */
2014static const void *
2015troc_next(const void *node)
2016{
2017 return ((const struct lysc_node *)node)->next;
2018}
2019
2020/**
2021 * @brief Get parent.
2022 * @param[in] node is any lysc_node.
2023 */
2024static const void *
2025troc_parent(const void *node)
2026{
2027 return ((const struct lysc_node *)node)->parent;
2028}
2029
2030/**
2031 * @brief Try to get child.
2032 * @param[in] node is any lysc_node.
2033 */
2034static const void *
2035troc_child(const void *node)
2036{
2037 return lysc_node_child(node);
2038}
2039
2040/**
2041 * @brief Try to get action.
2042 * @param[in] node is any lysc_node.
2043 */
2044static const void *
2045troc_actions(const void *node)
2046{
2047 return lysc_node_actions(node);
2048}
2049
2050/**
2051 * @brief Try to get action.
2052 * @param[in] node must be of type lysc_node_action.
2053 */
2054static const void *
2055troc_action_input(const void *node)
2056{
2057 return &((const struct lysc_node_action *)node)->input;
2058}
2059
2060/**
2061 * @brief Try to get action.
2062 * @param[in] node must be of type lysc_node_action.
2063 */
2064static const void *
2065troc_action_output(const void *node)
2066{
2067 return &((const struct lysc_node_action *)node)->output;
2068}
2069
2070/**
2071 * @brief Try to get action.
2072 * @param[in] node is any lysc_node.
2073 */
2074static const void *
2075troc_notifs(const void *node)
2076{
2077 return lysc_node_notifs(node);
2078}
2079
2080/**
2081 * @brief Fill struct tro_getters with \ref TRP_troc getters
2082 * which are adapted to lysc nodes.
2083 */
2084static struct tro_getters
2085troc_init_getters()
2086{
2087 return (struct tro_getters) {
2088 .nodetype = troc_nodetype,
2089 .next = troc_next,
2090 .parent = troc_parent,
2091 .child = troc_child,
2092 .actions = troc_actions,
2093 .action_input = troc_action_input,
2094 .action_output = troc_action_output,
2095 .notifs = troc_notifs
2096 };
2097}
2098
aPiecekef1e58e2021-04-19 13:19:44 +02002099/**********************************************************************
2100 * tro functions
2101 *********************************************************************/
2102
2103/**
2104 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002105 *
2106 * This is a general algorithm that is able to
2107 * work with lysp_node or lysc_node.
2108 *
2109 * @param[in] node points to lysp_node or lysc_node.
2110 * @param[in] lysc_tree flag to determine what type the @p node is.
2111 * If set to true, then @p points to lysc_node otherwise lysp_node.
2112 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002113 */
2114static const void *
aPiecek3f247652021-04-19 13:40:25 +02002115tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002116{
2117 struct tro_getters get;
2118 const void *tmp, *parent;
2119 const void *ret;
2120
2121 assert(node);
2122
aPiecek3f247652021-04-19 13:40:25 +02002123 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002124
2125 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2126 if ((tmp = get.next(node))) {
2127 /* next action exists */
2128 ret = tmp;
2129 } else if ((parent = get.parent(node))) {
2130 /* maybe if notif exists as sibling */
2131 ret = get.notifs(parent);
2132 } else {
2133 ret = NULL;
2134 }
2135 } else if (get.nodetype(node) & LYS_INPUT) {
2136 if ((parent = get.parent(node))) {
2137 /* if output action has data */
2138 if (get.child(get.action_output(parent))) {
2139 /* then next sibling is output action */
2140 ret = get.action_output(parent);
2141 } else {
2142 /* input action cannot have siblings other
2143 * than output action.
2144 */
2145 ret = NULL;
2146 }
2147 } else {
2148 /* there is no way how to get output action */
2149 ret = NULL;
2150 }
2151 } else if (get.nodetype(node) & LYS_OUTPUT) {
2152 /* output action cannot have siblings */
2153 ret = NULL;
2154 } else if (get.nodetype(node) & LYS_NOTIF) {
2155 /* must have as a sibling only notif */
2156 ret = get.next(node);
2157 } else {
2158 /* for rest of nodes */
2159 if ((tmp = get.next(node))) {
2160 /* some sibling exists */
2161 ret = tmp;
2162 } else if ((parent = get.parent(node))) {
2163 /* Action and notif are siblings too.
2164 * They can be reached through parent.
2165 */
2166 if ((tmp = get.actions(parent))) {
2167 /* next sibling is action */
2168 ret = tmp;
2169 } else if ((tmp = get.notifs(parent))) {
2170 /* next sibling is notif */
2171 ret = tmp;
2172 } else {
2173 /* sibling not exists */
2174 ret = NULL;
2175 }
2176 } else {
2177 /* sibling not exists */
2178 ret = NULL;
2179 }
2180 }
2181
2182 return ret;
2183}
2184
2185/**
2186 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002187 *
2188 * This is a general algorithm that is able to
2189 * work with lysp_node or lysc_node.
2190 *
2191 * @param[in] node points to lysp_node or lysc_node.
2192 * @param[in] lysc_tree flag to determine what type the @p node is.
2193 * If set to true, then @p points to lysc_node otherwise lysp_node.
2194 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002195 */
2196static const void *
aPiecek3f247652021-04-19 13:40:25 +02002197tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002198{
2199 struct tro_getters get;
2200 const void *tmp;
2201 const void *ret;
2202
2203 assert(node);
2204
aPiecek3f247652021-04-19 13:40:25 +02002205 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002206
2207 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2208 if (get.child(get.action_input(node))) {
2209 /* go to LYS_INPUT */
2210 ret = get.action_input(node);
2211 } else if (get.child(get.action_output(node))) {
2212 /* go to LYS_OUTPUT */
2213 ret = get.action_output(node);
2214 } else {
2215 /* input action and output action have no data */
2216 ret = NULL;
2217 }
2218 } else {
2219 if ((tmp = get.child(node))) {
2220 ret = tmp;
2221 } else {
2222 /* current node can't have children or has no children */
2223 /* but maybe has some actions or notifs */
2224 if ((tmp = get.actions(node))) {
2225 ret = tmp;
2226 } else if ((tmp = get.notifs(node))) {
2227 ret = tmp;
2228 } else {
2229 ret = NULL;
2230 }
2231 }
2232 }
2233
2234 return ret;
2235}
2236
2237/**
aPiecek3f247652021-04-19 13:40:25 +02002238 * @brief Get new trt_parent_cache if we apply the transfer
2239 * to the child node in the tree.
2240 * @param[in] ca is parent cache for current node.
2241 * @param[in] tc contains current tree node.
2242 * @return Cache for the current node.
2243 */
2244static struct trt_parent_cache
2245tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2246{
2247 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2248
2249 if (!tc->lysc_tree) {
2250 const struct lysp_node *pn = tc->pn;
2251
2252 ret.ancestor =
2253 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2254 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2255 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2256 ca.ancestor;
2257
2258 ret.lys_status =
2259 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2260 ca.lys_status;
2261
2262 ret.lys_config =
2263 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2264 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2265 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2266 ca.lys_config;
2267
2268 ret.last_list =
2269 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2270 ca.last_list;
2271 }
2272
2273 return ret;
2274}
2275
2276/**
aPiecekef1e58e2021-04-19 13:19:44 +02002277 * @brief Transformation of the Schema nodes flags to
2278 * Tree diagram \<status\>.
2279 * @param[in] flags is node's flags obtained from the tree.
2280 */
2281static trt_status_type
2282tro_flags2status(uint16_t flags)
2283{
2284 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2285 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2286 TRD_STATUS_TYPE_CURRENT;
2287}
2288
2289/**
2290 * @brief Transformation of the Schema nodes flags to Tree diagram
2291 * \<flags\> but more specifically 'ro' or 'rw'.
2292 * @param[in] flags is node's flags obtained from the tree.
2293 */
2294static trt_flags_type
2295tro_flags2config(uint16_t flags)
2296{
2297 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2298 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2299 TRD_FLAGS_TYPE_EMPTY;
2300}
2301
2302/**
aPiecek3f247652021-04-19 13:40:25 +02002303 * @brief Print current node's iffeatures.
2304 * @param[in] tc is tree context.
2305 * @param[in,out] out is output handler.
2306 */
2307static void
2308tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2309{
2310 const struct lysp_qname *iffs;
2311
2312 iffs = tc->lysc_tree ?
2313 TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures :
2314 tc->pn->iffeatures;
2315
2316 LY_ARRAY_COUNT_TYPE i;
2317
2318 LY_ARRAY_FOR(iffs, i) {
2319 if (i == 0) {
2320 ly_print_(out, "%s", iffs[i].str);
2321 } else {
2322 ly_print_(out, ",%s", iffs[i].str);
2323 }
2324 }
2325
2326}
2327
2328/**
2329 * @brief Print current list's keys.
2330 *
2331 * Well, actually printing keys in the lysp_tree is trivial,
2332 * because char* points to all keys. However, special functions have
2333 * been reserved for this, because in principle the list of elements
2334 * can have more implementations.
2335 *
2336 * @param[in] tc is tree context.
2337 * @param[in,out] out is output handler.
2338 */
2339static void
2340tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2341{
2342 const struct lysp_node_list *list;
2343
2344 list = tc->lysc_tree ?
2345 (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn) :
2346 (const struct lysp_node_list *)tc->pn;
2347 assert(list->nodetype & LYS_LIST);
2348
2349 if (trg_charptr_has_data(list->key)) {
2350 ly_print_(out, "%s", list->key);
2351 }
2352}
2353
2354/**
2355 * @brief Get rpcs section if exists.
2356 * @param[in,out] tc is tree context.
2357 * @return Section representation if it exists. The @p tc is modified
2358 * and his pointer points to the first node in rpcs section.
2359 * @return Empty section representation otherwise.
2360 */
2361static struct trt_keyword_stmt
2362tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2363{
aPiecek9f792e52021-04-21 08:33:56 +02002364 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002365 const void *actions;
2366
2367 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002368 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002369 if (actions) {
2370 tc->cn = actions;
2371 }
2372 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002373 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002374 if (actions) {
2375 tc->pn = actions;
2376 tc->tpn = tc->pn;
2377 }
2378 }
2379
2380 if (actions) {
2381 tc->section = TRD_SECT_RPCS;
2382 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2383 } else {
2384 return TRP_EMPTY_KEYWORD_STMT;
2385 }
2386}
2387
2388/**
2389 * @brief Get notification section if exists
2390 * @param[in,out] tc is tree context.
2391 * @return Section representation if it exists.
2392 * The @p tc is modified and his pointer points to the
2393 * first node in notification section.
2394 * @return Empty section representation otherwise.
2395 */
2396static struct trt_keyword_stmt
2397tro_modi_get_notifications(struct trt_tree_ctx *tc)
2398{
aPiecek9f792e52021-04-21 08:33:56 +02002399 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002400 const void *notifs;
2401
2402 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002403 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002404 if (notifs) {
2405 tc->cn = notifs;
2406 }
2407 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002408 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002409 if (notifs) {
2410 tc->pn = notifs;
2411 tc->tpn = tc->pn;
2412 }
2413 }
2414
2415 if (notifs) {
2416 tc->section = TRD_SECT_NOTIF;
2417 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2418 } else {
2419 return TRP_EMPTY_KEYWORD_STMT;
2420 }
2421}
2422
2423/**
aPiecek96baa7f2021-04-23 12:32:00 +02002424 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002425 *
2426 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002427 * @param[in] u is index to the array of extensions (lysc_ext_instance
2428 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002429 * @return Section representation if it exists.
2430 * @return Empty section representation otherwise.
2431 */
2432static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002433tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002434{
aPiecek96baa7f2021-04-23 12:32:00 +02002435 assert(tc);
2436 const void *node;
2437 const char *yang_data_name;
2438
2439 if (tc->lysc_tree) {
2440 struct lysc_ext_instance *exts;
2441 struct lysc_ext_substmt *substmts;
2442
2443 exts = tc->cmod->exts;
2444 substmts = exts[u].substmts;
2445 if (!substmts) {
2446 return TRP_EMPTY_KEYWORD_STMT;
2447 }
2448 node = *(const struct lysc_node **)substmts->storage;
2449 yang_data_name = exts[u].argument;
2450 } else {
2451 struct lysp_ext_instance *exts;
2452
2453 exts = tc->pmod->exts;
2454 node = exts[u].parsed;
2455 yang_data_name = exts[u].argument;
2456 }
2457
2458 if (tc->lysc_tree) {
2459 tc->cn = node;
2460 } else {
2461 tc->tpn_ext = &tc->pmod->exts[u];
2462 tc->pn = node;
2463 }
2464
2465 if (node) {
2466 tc->section = TRD_SECT_YANG_DATA;
2467 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2468 } else {
2469 return TRP_EMPTY_KEYWORD_STMT;
2470 }
aPiecekef1e58e2021-04-19 13:19:44 +02002471}
2472
2473/**
2474 * @brief Get name of the module.
2475 * @param[in] tc is context of the tree.
2476 */
2477static struct trt_keyword_stmt
2478tro_read_module_name(const struct trt_tree_ctx *tc)
2479{
aPiecek9f792e52021-04-21 08:33:56 +02002480 assert(tc);
2481
2482 struct trt_keyword_stmt ret;
2483
2484 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2485 TRD_KEYWORD_SUBMODULE :
2486 TRD_KEYWORD_MODULE;
2487
2488 ret.str = !tc->lysc_tree ?
2489 LYSP_MODULE_NAME(tc->pmod) :
2490 tc->cmod->mod->name;
2491
2492 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002493}
2494
2495/**********************************************************************
2496 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002497 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002498
2499/**
aPiecek61d062b2020-11-02 11:05:09 +01002500 * @brief Check if list statement has keys.
2501 * @param[in] pn is pointer to the list.
2502 * @return 1 if has keys, otherwise 0.
2503 */
2504static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002505trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002506{
aPiecekef1e58e2021-04-19 13:19:44 +02002507 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002508}
2509
2510/**
2511 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002512 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002513 * @return 1 if has if-features, otherwise 0.
2514 */
2515static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002516trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002517{
2518 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002519 const struct lysp_qname *iffs;
2520
aPiecek61d062b2020-11-02 11:05:09 +01002521 ly_bool ret = 0;
2522
aPiecekef1e58e2021-04-19 13:19:44 +02002523 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002524 LY_ARRAY_FOR(iffs, u) {
2525 ret = 1;
2526 break;
2527 }
2528 return ret;
2529}
2530
2531/**
2532 * @brief Find out if leaf is also the key in last list.
2533 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002534 * @param[in] ca_last_list is pointer to last visited list.
2535 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002536 * @return 1 if leaf is also the key, otherwise 0.
2537 */
2538static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002539trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002540{
2541 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2542 const struct lysp_node_list *list = ca_last_list;
2543
2544 if (!list) {
2545 return 0;
2546 }
2547 return trg_charptr_has_data(list->key) ?
2548 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2549}
2550
2551/**
2552 * @brief Check if container's type is presence.
2553 * @param[in] pn is pointer to container.
2554 * @return 1 if container has presence statement, otherwise 0.
2555 */
2556static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002557trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002558{
2559 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2560}
2561
2562/**
2563 * @brief Get leaflist's path without lysp_node type control.
2564 * @param[in] pn is pointer to the leaflist.
2565 */
2566static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002567trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002568{
2569 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2570
2571 return list->type.path ? list->type.path->expr : NULL;
2572}
2573
2574/**
2575 * @brief Get leaflist's type name without lysp_node type control.
2576 * @param[in] pn is pointer to the leaflist.
2577 */
2578static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002579trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002580{
2581 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2582
2583 return list->type.name;
2584}
2585
2586/**
2587 * @brief Get leaf's path without lysp_node type control.
2588 * @param[in] pn is pointer to the leaf node.
2589 */
2590static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002591trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002592{
2593 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2594
2595 return leaf->type.path ? leaf->type.path->expr : NULL;
2596}
2597
2598/**
2599 * @brief Get leaf's type name without lysp_node type control.
2600 * @param[in] pn is pointer to the leaf's type name.
2601 */
2602static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002603trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002604{
2605 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2606
2607 return leaf->type.name;
2608}
2609
2610/**
aPiecek874ea4d2021-04-19 12:26:36 +02002611 * @brief Get pointer to data using node type specification
2612 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002613 *
aPiecek874ea4d2021-04-19 12:26:36 +02002614 * @param[in] flags is node type specification.
2615 * If it is the correct node, the getter function is called.
2616 * @param[in] f is getter function which provides the desired
2617 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002618 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002619 * @return NULL if node has wrong type or getter function return
2620 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002621 * @return Pointer to desired char pointer obtained from the node.
2622 */
2623static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002624trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002625{
2626 if (pn->nodetype & flags) {
2627 const char *ret = f(pn);
2628 return trg_charptr_has_data(ret) ? ret : NULL;
2629 } else {
2630 return NULL;
2631 }
2632}
2633
2634/**
aPiecek61d062b2020-11-02 11:05:09 +01002635 * @brief Resolve \<status\> of the current node.
2636 * @param[in] nodetype is node's type obtained from the tree.
2637 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002638 * @param[in] ca_lys_status is inherited status
2639 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002640 * @return The status type.
2641 */
2642static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002643trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002644{
2645 /* LYS_INPUT and LYS_OUTPUT is special case */
2646 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002647 return tro_flags2status(ca_lys_status);
2648 /* if ancestor's status is deprc or obslt
2649 * and also node's status is not set
2650 */
aPiecek61d062b2020-11-02 11:05:09 +01002651 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2652 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002653 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002654 } else {
2655 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002656 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002657 }
2658}
2659
2660/**
2661 * @brief Resolve \<flags\> of the current node.
2662 * @param[in] nodetype is node's type obtained from the tree.
2663 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002664 * @param[in] ca_ancestor is ancestor type obtained
2665 * from trt_parent_cache.
2666 * @param[in] ca_lys_config is inherited config item
2667 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002668 * @return The flags type.
2669 */
2670static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002671trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002672{
2673 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2674 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2675 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2676 return TRD_FLAGS_TYPE_RO;
2677 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2678 return TRD_FLAGS_TYPE_RO;
2679 } else if (nodetype & LYS_NOTIF) {
2680 return TRD_FLAGS_TYPE_NOTIF;
2681 } else if (nodetype & LYS_USES) {
2682 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2683 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2684 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002685 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002686 /* config is not set. Look at ancestor's config */
2687 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002688 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002689 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002690 }
2691}
2692
2693/**
2694 * @brief Resolve node type of the current node.
2695 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002696 * @param[in] ca_last_list is pointer to the last visited list.
2697 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002698 */
2699static trt_node_type
aPiecekef1e58e2021-04-19 13:19:44 +02002700trop_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002701{
2702 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2703 return TRD_NODE_ELSE;
2704 } else if (pn->nodetype & LYS_CASE) {
2705 return TRD_NODE_CASE;
2706 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2707 return TRD_NODE_OPTIONAL_CHOICE;
2708 } else if (pn->nodetype & LYS_CHOICE) {
2709 return TRD_NODE_CHOICE;
aPiecekef1e58e2021-04-19 13:19:44 +02002710 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002711 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002712 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002713 return TRD_NODE_KEYS;
2714 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2715 return TRD_NODE_LISTLEAFLIST;
2716 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2717 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002718 } 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 +01002719 return TRD_NODE_OPTIONAL;
2720 } else {
2721 return TRD_NODE_ELSE;
2722 }
2723}
2724
2725/**
aPiecekef1e58e2021-04-19 13:19:44 +02002726 * @brief Resolve \<type\> of the current node.
2727 * @param[in] pn is current node.
2728 */
2729static struct trt_type
2730trop_resolve_type(const struct lysp_node *pn)
2731{
2732 const char *tmp = NULL;
2733
2734 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2735 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2736 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2737 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2738 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2739 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2740 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2741 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2742 } else if (pn->nodetype == LYS_ANYDATA) {
2743 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2744 } else if (pn->nodetype & LYS_ANYXML) {
2745 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2746 } else {
2747 return TRP_EMPTY_TRT_TYPE;
2748 }
2749}
2750
2751/**
aPiecek61d062b2020-11-02 11:05:09 +01002752 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002753 * @param[in] ca contains stored important data
2754 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002755 * @param[in] tc is context of the tree.
2756 */
2757static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002758trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002759{
aPiecekef1e58e2021-04-19 13:19:44 +02002760 const struct lysp_node *pn;
2761 struct trt_node ret;
2762
aPiecek61d062b2020-11-02 11:05:09 +01002763 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002764
2765 pn = tc->pn;
2766 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002767
2768 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002769 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002770
2771 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2772 /* <flags> */
aPiecekef1e58e2021-04-19 13:19:44 +02002773 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002774
2775 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2776 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2777 /* set type of the node */
aPiecekef1e58e2021-04-19 13:19:44 +02002778 ret.name.type = trop_resolve_node_type(pn, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002779
2780 /* TODO: ret.name.module_prefix is not supported right now. */
2781 ret.name.module_prefix = NULL;
2782
2783 /* set node's name */
2784 ret.name.str = pn->name;
2785
2786 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002787 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002788
2789 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002790 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002791
aPiecek3f247652021-04-19 13:40:25 +02002792 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002793
2794 return ret;
2795}
2796
aPiecekef1e58e2021-04-19 13:19:44 +02002797/**
2798 * @brief Find out if the current node has siblings.
2799 * @param[in] tc is context of the tree.
2800 * @return 1 if sibling exists otherwise 0.
2801 */
2802static ly_bool
2803trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2804{
aPiecek3f247652021-04-19 13:40:25 +02002805 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002806}
2807
aPiecek96baa7f2021-04-23 12:32:00 +02002808/**
2809 * @brief Print all yang-data sections and print three dots instead
2810 * of nodes.
2811 * @param[in] exts is array of YANG extension instances from parsed
2812 * module (@ref sizedarrays).
2813 * @param[in] mll is maximum number of characters that can be printed
2814 * on one line.
2815 * @param[in,out] out is output handler.
2816 */
2817static void
2818trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2819{
2820 struct trt_keyword_stmt ks;
2821 LY_ARRAY_COUNT_TYPE u;
2822 struct trt_wrapper wr;
2823
2824 if (!exts) {
2825 return;
2826 }
2827
2828 ly_print_(out, "\n");
2829 ks.type = TRD_KEYWORD_YANG_DATA;
2830 wr = TRP_INIT_WRAPPER_BODY;
2831
2832 LY_ARRAY_FOR(exts, u) {
2833 ly_print_(out, "\n");
2834
2835 /* yang-data <yang-data-name>: */
2836 ks.str = exts[u].argument;
2837 trp_print_keyword_stmt(ks, mll, 0, out);
2838 ly_print_(out, "\n");
2839
2840 /* ... */
2841 trp_print_wrapper(wr, out);
2842 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2843 }
2844}
2845
aPiecek874ea4d2021-04-19 12:26:36 +02002846/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002847 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002848 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002849
2850/**
aPiecek874ea4d2021-04-19 12:26:36 +02002851 * @brief Change current node pointer to its parent
2852 * but only if parent exists.
2853 * @param[in,out] tc is tree context.
2854 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002855 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002856 * @return 0 if the node did not have parents.
2857 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002858 */
2859static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002860trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002861{
2862 assert(tc && tc->pn);
2863 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002864 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002865 tc->pn = tc->pn->parent;
2866 return 1;
2867 } else {
2868 return 0;
2869 }
2870}
2871
2872/**
aPiecek874ea4d2021-04-19 12:26:36 +02002873 * @brief Change the current node pointer to its child
2874 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002875 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002876 * @param[in,out] tc is context of the tree.
2877 * Contains pointer to the current node.
2878 * @return Non-empty \<node\> representation of the current
2879 * node's child. The @p tc is modified.
2880 * @return Empty \<node\> representation if child don't exists.
2881 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002882 */
2883static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002884trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002885{
aPiecekef1e58e2021-04-19 13:19:44 +02002886 const struct lysp_node *tmp;
2887
aPiecek61d062b2020-11-02 11:05:09 +01002888 assert(tc && tc->pn);
2889
aPiecek3f247652021-04-19 13:40:25 +02002890 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002891 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02002892 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01002893 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002894 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002895 }
2896}
2897
2898/**
aPiecek874ea4d2021-04-19 12:26:36 +02002899 * @brief Change the current node pointer to the first child of node's
2900 * parent. If current node is already first sibling/child then nothing
2901 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01002902 * @param[in,out] tc is tree context.
2903 */
2904static void
aPiecekef1e58e2021-04-19 13:19:44 +02002905trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002906{
aPiecek9f792e52021-04-21 08:33:56 +02002907 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01002908
aPiecekef1e58e2021-04-19 13:19:44 +02002909 if (trop_modi_parent(tc)) {
2910 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002911 } else {
2912 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01002913 switch (tc->section) {
2914 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02002915 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02002916 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002917 break;
2918 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02002919 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02002920 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002921 break;
2922 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02002923 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02002924 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002925 break;
2926 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02002927 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02002928 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002929 break;
2930 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02002931 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02002932 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002933 break;
2934 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02002935 /* tpn in this case is of type lysp_ext_instance */
2936 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01002937 break;
aPiecek96baa7f2021-04-23 12:32:00 +02002938 default:
2939 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01002940 }
aPiecek61d062b2020-11-02 11:05:09 +01002941 }
2942}
2943
2944/**
aPiecek874ea4d2021-04-19 12:26:36 +02002945 * @brief Change the pointer to the current node to its next sibling
2946 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002947 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002948 * @param[in,out] tc is tree context.
2949 * Contains pointer to the current node.
2950 * @return Non-empty \<node\> representation if sibling exists.
2951 * The @p tc is modified.
2952 * @return Empty \<node\> representation otherwise.
2953 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002954 */
2955static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002956trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002957{
aPiecekef1e58e2021-04-19 13:19:44 +02002958 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002959
2960 assert(tc && tc->pn);
2961
aPiecek3f247652021-04-19 13:40:25 +02002962 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002963
aPiecekef1e58e2021-04-19 13:19:44 +02002964 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02002965 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
2966 tc->tpn = pn;
2967 }
aPiecekef1e58e2021-04-19 13:19:44 +02002968 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002969 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002970 } else {
2971 return TRP_EMPTY_NODE;
2972 }
2973}
2974
2975/**
2976 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02002977 * @param[in,out] tc is tree context. It is modified and his current
2978 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01002979 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01002980 * @return Empty section structure otherwise.
2981 */
2982static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02002983trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002984{
aPiecek9f792e52021-04-21 08:33:56 +02002985 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01002986 const struct lysp_node_augment *augs;
2987
2988 /* if next_augment func was called for the first time */
2989 if (tc->section != TRD_SECT_AUGMENT) {
2990 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02002991 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01002992 } else {
2993 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02002994 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01002995 }
2996
aPiecekdc8fd572021-04-19 10:47:23 +02002997 if (augs) {
2998 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01002999 tc->tpn = tc->pn;
3000 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3001 } else {
3002 return TRP_EMPTY_KEYWORD_STMT;
3003 }
3004}
3005
3006/**
aPiecek61d062b2020-11-02 11:05:09 +01003007 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003008 * @param[in,out] tc is tree context. It is modified and his current
3009 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003010 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003011 * @return Empty section representation otherwise.
3012 */
3013static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003014trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003015{
aPiecek9f792e52021-04-21 08:33:56 +02003016 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003017 const struct lysp_node_grp *grps;
3018
3019 if (tc->section != TRD_SECT_GROUPING) {
3020 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003021 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003022 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003023 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003024 }
3025
aPiecekdc8fd572021-04-19 10:47:23 +02003026 if (grps) {
3027 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003028 tc->tpn = tc->pn;
3029 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3030 } else {
3031 return TRP_EMPTY_KEYWORD_STMT;
3032 }
3033}
3034
aPiecek874ea4d2021-04-19 12:26:36 +02003035/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003036 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003037 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003038
3039/**
aPiecek3f247652021-04-19 13:40:25 +02003040 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003041 */
aPiecek3f247652021-04-19 13:40:25 +02003042static ly_bool
3043troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003044{
aPiecek3f247652021-04-19 13:40:25 +02003045 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3046}
aPiecek61d062b2020-11-02 11:05:09 +01003047
aPiecek3f247652021-04-19 13:40:25 +02003048/**
3049 * @brief Resolve \<flags\> of the current node.
3050 *
3051 * Use this function only if trt_tree_ctx.lysc_tree is true.
3052 *
3053 * @param[in] nodetype is current lysc_node.nodetype.
3054 * @param[in] flags is current lysc_node.flags.
3055 * @return The flags type.
3056 */
3057static trt_flags_type
3058troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3059{
3060 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3061 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3062 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3063 return TRD_FLAGS_TYPE_RO;
3064 } else if (nodetype & LYS_IS_NOTIF) {
3065 return TRD_FLAGS_TYPE_RO;
3066 } else if (nodetype & LYS_NOTIF) {
3067 return TRD_FLAGS_TYPE_NOTIF;
3068 } else if (nodetype & LYS_USES) {
3069 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3070 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3071 return TRD_FLAGS_TYPE_RPC;
3072 } else {
3073 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003074 }
aPiecek61d062b2020-11-02 11:05:09 +01003075}
3076
3077/**
aPiecek3f247652021-04-19 13:40:25 +02003078 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003079 *
aPiecek3f247652021-04-19 13:40:25 +02003080 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003081 *
aPiecek3f247652021-04-19 13:40:25 +02003082 * @param[in] nodetype is current lysc_node.nodetype.
3083 * @param[in] flags is current lysc_node.flags.
3084 */
3085static trt_node_type
3086troc_resolve_node_type(uint16_t nodetype, uint16_t flags)
3087{
3088 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3089 return TRD_NODE_ELSE;
3090 } else if (nodetype & LYS_CASE) {
3091 return TRD_NODE_CASE;
3092 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3093 return TRD_NODE_OPTIONAL_CHOICE;
3094 } else if (nodetype & LYS_CHOICE) {
3095 return TRD_NODE_CHOICE;
3096 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3097 return TRD_NODE_CONTAINER;
3098 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3099 return TRD_NODE_KEYS;
3100 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3101 return TRD_NODE_LISTLEAFLIST;
3102 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3103 return TRD_NODE_OPTIONAL;
3104 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3105 return TRD_NODE_OPTIONAL;
3106 } else {
3107 return TRD_NODE_ELSE;
3108 }
3109}
3110
3111/**
3112 * @brief Transformation of current lysc_node to struct trt_node.
3113 * @param[in] ca is not used.
3114 * @param[in] tc is context of the tree.
3115 */
3116static struct trt_node
3117troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3118{
3119 (void) ca;
3120 const struct lysc_node *cn;
3121 struct trt_node ret;
3122
aPiecek96baa7f2021-04-23 12:32:00 +02003123 assert(tc && tc->cn && tc->cn->priv);
aPiecek3f247652021-04-19 13:40:25 +02003124
3125 cn = tc->cn;
3126 ret = TRP_EMPTY_NODE;
3127
3128 /* <status> */
3129 ret.status = tro_flags2status(cn->flags);
3130
3131 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
3132 /* <flags> */
3133 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3134
3135 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
3136 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
3137 /* set type of the node */
3138 ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
3139
3140 /* TODO: ret.name.module_prefix is not supported right now. */
3141 ret.name.module_prefix = NULL;
3142
3143 /* set node's name */
3144 ret.name.str = cn->name;
3145
3146 /* <type> */
3147 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3148
3149 /* <iffeature> */
3150 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3151
3152 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3153
3154 return ret;
3155}
3156
3157/**********************************************************************
3158 * Modify troc getters
3159 *********************************************************************/
3160
3161/**
3162 * @copydoc trop_modi_parent()
3163 */
3164static ly_bool
3165troc_modi_parent(struct trt_tree_ctx *tc)
3166{
3167 assert(tc && tc->cn);
3168 /* If no parent exists, stay in actual node. */
3169 if (tc->cn->parent) {
3170 tc->cn = tc->cn->parent;
3171 return 1;
3172 } else {
3173 return 0;
3174 }
3175}
3176
3177/**
3178 * @copydoc trop_modi_next_sibling()
3179 */
3180static struct trt_node
3181troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3182{
3183 const struct lysc_node *cn;
3184
3185 assert(tc && tc->cn);
3186
3187 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3188
3189 /* if next sibling exists */
3190 if (cn) {
3191 /* update trt_tree_ctx */
3192 tc->cn = cn;
3193 return troc_read_node(ca, tc);
3194 } else {
3195 return TRP_EMPTY_NODE;
3196 }
3197}
3198
3199/**
3200 * @copydoc trop_modi_next_child()
3201 */
3202static struct trt_node
3203troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3204{
3205 const struct lysc_node *tmp;
3206
3207 assert(tc && tc->cn);
3208
3209 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3210 tc->cn = tmp;
3211 return troc_read_node(ca, tc);
3212 } else {
3213 return TRP_EMPTY_NODE;
3214 }
3215}
3216
3217/**
3218 * @copydoc trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003219 */
3220static void
aPiecek3f247652021-04-19 13:40:25 +02003221troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003222{
aPiecek3f247652021-04-19 13:40:25 +02003223 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003224
aPiecek3f247652021-04-19 13:40:25 +02003225 if (troc_modi_parent(tc)) {
3226 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3227 } else {
3228 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003229 switch (tc->section) {
3230 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003231 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003232 break;
3233 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003234 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003235 break;
3236 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003237 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003238 break;
3239 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003240 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003241 break;
3242 default:
3243 assert(0);
3244 }
aPiecek61d062b2020-11-02 11:05:09 +01003245 }
3246}
3247
aPiecek874ea4d2021-04-19 12:26:36 +02003248/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003249 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003250 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003251
3252/**
3253 * @brief Get size of node name.
3254 * @param[in] name contains name and mark.
3255 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003256 * @return negative value as an indication that option mark
3257 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003258 */
3259static int32_t
3260trb_strlen_of_name_and_mark(struct trt_node_name name)
3261{
3262 size_t name_len = strlen(name.str);
3263
3264 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3265 /* counting also parentheses */
3266 name_len += 2;
3267 }
3268
3269 return trp_mark_is_used(name) ?
3270 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3271 (int32_t)name_len;
3272}
3273
3274/**
aPiecek874ea4d2021-04-19 12:26:36 +02003275 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3276 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003277 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003278 * @param[in] max_len4all is the maximum value of btw_opts_type
3279 * that it can have.
3280 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003281 */
3282static int16_t
3283trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3284{
3285 int32_t name_len;
3286 int16_t min_len;
3287 int16_t ret;
3288
3289 name_len = trb_strlen_of_name_and_mark(name);
3290
3291 /* negative value indicate that in name is some opt mark */
3292 min_len = name_len < 0 ?
3293 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3294 TRD_INDENT_BEFORE_TYPE;
3295 ret = abs(max_len4all) - abs(name_len);
3296
3297 /* correction -> negative indicate that name is too long. */
3298 return ret < 0 ? min_len : ret;
3299}
3300
3301/**
3302 * @brief Print node.
3303 *
aPiecek874ea4d2021-04-19 12:26:36 +02003304 * This function is wrapper for trp_print_entire_node().
3305 * But difference is that take @p max_gap_before_type which will be
3306 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003307 *
3308 * @param[in] max_gap_before_type is number of indent before \<type\>.
3309 * @param[in] wr is wrapper for printing indentation before node.
3310 * @param[in] ca contains inherited data from ancestors.
3311 * @param[in] pc contains mainly functions for printing.
3312 * @param[in] tc is tree context.
3313 */
3314static void
3315trb_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)
3316{
3317 struct trt_node node = pc->fp.read.node(ca, tc);
3318 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3319
3320 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3321 /* print actual node with unified indent */
3322 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3323 }
3324 /* after -> print actual node with default indent */
3325 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3326 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
3327}
3328
3329/**
aPiecek874ea4d2021-04-19 12:26:36 +02003330 * @brief Check if parent of the current node is the last
3331 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003332 *
aPiecek874ea4d2021-04-19 12:26:36 +02003333 * To mantain stability use this function only if the current node is
3334 * the first of the siblings.
3335 * Side-effect -> current node is set to the first sibling
3336 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003337 *
aPiecek874ea4d2021-04-19 12:26:36 +02003338 * @param[in] fp contains all \ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003339 * @param[in,out] tc is tree context.
3340 * @return 1 if parent is last sibling otherwise 0.
3341 */
3342static ly_bool
3343trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3344{
3345 if (fp.modify.parent(tc)) {
3346 ly_bool ret = fp.read.if_sibling_exists(tc);
3347 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3348 return !ret;
3349 } else {
3350 return !fp.read.if_sibling_exists(tc);
3351 }
3352}
3353
3354/**
3355 * @brief Find sibling with the biggest node name and return that size.
3356 *
3357 * Side-effect -> Current node is set to the first sibling.
3358 *
3359 * @param[in] ca contains inherited data from ancestors.
3360 * @param[in] pc contains mainly functions for printing.
3361 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003362 * @return positive number as a sign that only the node name is
3363 * included in the size.
3364 * @return negative number sign that node name and his opt mark is
3365 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003366 */
3367static int32_t
3368trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3369{
3370 int32_t ret = 0;
3371
3372 pc->fp.modify.first_sibling(tc);
3373
3374 for (struct trt_node node = pc->fp.read.node(ca, tc);
3375 !trp_node_is_empty(node);
3376 node = pc->fp.modify.next_sibling(ca, tc)) {
3377 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
3378 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3379 }
3380 pc->fp.modify.first_sibling(tc);
3381 return ret;
3382}
3383
3384/**
aPiecek874ea4d2021-04-19 12:26:36 +02003385 * @brief Find maximal indent between
3386 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003387 *
3388 * Side-effect -> Current node is set to the first sibling.
3389 *
3390 * @param[in] ca contains inherited data from ancestors.
3391 * @param[in] pc contains mainly functions for printing.
3392 * @param[in,out] tc is tree context.
3393 * @return max btw_opts_type value for rest of the siblings
3394 */
3395static int16_t
3396trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3397{
3398 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3399 int16_t ind_before_type = maxlen_node_name < 0 ?
3400 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3401 TRD_INDENT_BEFORE_TYPE;
3402
3403 return abs(maxlen_node_name) + ind_before_type;
3404}
3405
3406/**
aPiecek874ea4d2021-04-19 12:26:36 +02003407 * @brief Find out if it is possible to unify
3408 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003409 *
aPiecek874ea4d2021-04-19 12:26:36 +02003410 * The goal is for all node siblings to have the same alignment
3411 * for \<type\> as if they were in a column. All siblings who cannot
3412 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003413 * Side-effect -> Current node is set to the first sibling.
3414 *
3415 * @param[in] ca contains inherited data from ancestors.
3416 * @param[in] pc contains mainly functions for printing.
3417 * @param[in,out] tc is tree context.
3418 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003419 * @return positive number indicating the maximum number of spaces
3420 * before \<type\> if the length of the node name is 0. To calculate
3421 * the trt_indent_in_node.btw_opts_type indent size for a particular
3422 * node, use the trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003423*/
3424static uint32_t
3425trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3426{
3427 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3428}
3429
3430/**
aPiecek874ea4d2021-04-19 12:26:36 +02003431 * @brief For the current node: recursively print all of its child
3432 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003433 *
aPiecek874ea4d2021-04-19 12:26:36 +02003434 * This function is an auxiliary function for trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003435 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003436 * Nodes are printed, including unified sibling node alignment
3437 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003438 * Side-effect -> current node is set to the last sibling.
3439 *
3440 * @param[in] wr is wrapper for printing identation before node.
3441 * @param[in] ca contains inherited data from ancestors.
3442 * @param[in] pc contains mainly functions for printing.
3443 * @param[in,out] tc is tree context.
3444 */
3445static void
3446trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3447{
3448 uint32_t max_gap_before_type;
3449 ly_bool sibling_flag = 0;
3450 ly_bool child_flag = 0;
3451
3452 /* if node is last sibling, then do not add '|' to wrapper */
3453 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3454 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3455
3456 /* try unified indentation in node */
3457 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3458
3459 /* print all siblings */
3460 do {
3461 struct trt_parent_cache new_ca;
3462 struct trt_node node;
3463 /* print linebreak before printing actual node */
3464 ly_print_(pc->out, "\n");
3465 /* print node */
3466 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
3467
aPiecek3f247652021-04-19 13:40:25 +02003468 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003469 /* go to the actual node's child or stay in actual node */
3470 node = pc->fp.modify.next_child(ca, tc);
3471 child_flag = !trp_node_is_empty(node);
3472
3473 if (child_flag) {
3474 /* print all childs - recursive call */
3475 trb_print_nodes(wr, new_ca, pc, tc);
3476 /* get back from child node to actual node */
3477 pc->fp.modify.parent(tc);
3478 }
3479
3480 /* go to the actual node's sibling */
3481 node = pc->fp.modify.next_sibling(ca, tc);
3482 sibling_flag = !trp_node_is_empty(node);
3483
3484 /* go to the next sibling or stay in actual node */
3485 } while (sibling_flag);
3486}
3487
3488/**
aPiecek153b00f2021-04-20 13:52:57 +02003489 * @brief Calculate the wrapper about how deep in the tree the node is.
3490 * @param[in] node from which to count.
3491 * @return wrapper for @p node.
3492 */
3493static struct trt_wrapper
3494trb_count_depth(const struct lysc_node *node)
3495{
3496 struct trt_wrapper wr = TRP_INIT_WRAPPER_TOP;
3497 const struct lysc_node *parent;
3498
3499 if (!node) {
3500 return wr;
3501 }
3502
3503 for (parent = node->parent; parent; parent = parent->parent) {
3504 wr = trp_wrapper_set_shift(wr);
3505 }
3506
3507 return wr;
3508}
3509
3510/**
3511 * @brief Print all parent nodes of @p node and the @p node itself.
3512 *
3513 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3514 *
3515 * @param[in] node on which the function is focused.
3516 * @param[in] pc is \ref TRP_trp settings.
3517 * @param[in,out] tc is context of tree printer.
3518 * @return wrapper for @p node.
3519 */
3520static void
3521trb_print_parents(const struct lysc_node *node, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3522{
3523 struct trt_wrapper wr;
3524
3525 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3526
3527 /* stop recursion */
3528 if (!node) {
3529 return;
3530 }
3531 trb_print_parents(node->parent, pc, tc);
3532
3533 /* setup for printing */
3534 tc->cn = node;
3535 wr = trb_count_depth(node);
3536
3537 /* print node */
3538 ly_print_(pc->out, "\n");
3539 trb_print_entire_node(0, wr, TRP_EMPTY_PARENT_CACHE, pc, tc);
3540}
3541
3542/**
aPiecekdc8fd572021-04-19 10:47:23 +02003543 * @brief Get address of the current node.
3544 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003545 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003546 */
3547static const void *
3548trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3549{
aPiecek3f247652021-04-19 13:40:25 +02003550 return tc->lysc_tree ?
3551 (const void *)tc->cn :
3552 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003553}
3554
3555/**
3556 * @brief Get address of current node's child.
3557 * @param[in,out] tc contains current node.
3558 */
3559static const void *
3560trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3561{
3562 if (!trb_tree_ctx_get_node(tc)) {
3563 return NULL;
3564 }
3565
aPiecek3f247652021-04-19 13:40:25 +02003566 if (tc->lysc_tree) {
3567 return lysc_node_child(tc->cn);
3568 } else {
3569 return lysp_node_child(tc->pn);
3570 }
aPiecekdc8fd572021-04-19 10:47:23 +02003571}
3572
3573/**
3574 * @brief Set current node on its child.
3575 * @param[in,out] tc contains current node.
3576 */
3577static void
3578trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3579{
aPiecek3f247652021-04-19 13:40:25 +02003580 const void *node = trb_tree_ctx_get_child(tc);
3581
3582 if (tc->lysc_tree) {
3583 tc->cn = node;
3584 } else {
3585 tc->pn = node;
3586 }
aPiecekdc8fd572021-04-19 10:47:23 +02003587}
3588
3589/**
aPiecek61d062b2020-11-02 11:05:09 +01003590 * @brief Print subtree of nodes.
3591 *
3592 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003593 * Before root node is no linebreak printing. This must be addressed by
3594 * the caller. Root node will also be printed. Behind last printed node
3595 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003596 *
aPiecek874ea4d2021-04-19 12:26:36 +02003597 * @param[in] max_gap_before_type is result from
3598 * trb_try_unified_indent() function for root node. Set parameter to 0
3599 * if distance does not matter.
3600 * @param[in] wr is wrapper saying how deep in the whole tree
3601 * is the root of the subtree.
3602 * @param[in] ca is parent_cache from root's parent.
3603 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
3604 * @param[in] pc is \ref TRP_trp settings.
3605 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003606 */
3607static void
3608trb_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)
3609{
3610 struct trt_parent_cache new_ca;
3611 struct trt_node node;
3612
aPiecekdc8fd572021-04-19 10:47:23 +02003613 if (!trb_tree_ctx_get_node(tc)) {
3614 return;
3615 }
3616
aPiecek61d062b2020-11-02 11:05:09 +01003617 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
3618 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003619 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003620 node = pc->fp.modify.next_child(ca, tc);
3621
3622 if (!trp_node_is_empty(node)) {
3623 /* print root's nodes */
3624 trb_print_nodes(wr, new_ca, pc, tc);
3625 /* get back from child node to actual node */
3626 pc->fp.modify.parent(tc);
3627 }
3628}
3629
3630/**
3631 * @brief Get number of siblings.
3632 *
3633 * Side-effect -> current node is set to the first sibling.
3634 *
3635 * @param[in] fp contains callback functions which modify tree context
3636 * @param[in,out] tc is the tree context.
3637 * @return Number of siblings of the current node.
3638 */
3639static uint32_t
3640trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3641{
3642 uint32_t ret = 1;
3643 struct trt_node node = TRP_EMPTY_NODE;
3644
3645 /* including actual node */
3646 fp.first_sibling(tc);
3647 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3648 ret++;
3649 }
3650 fp.first_sibling(tc);
3651 return ret;
3652}
3653
3654/**
3655 * @brief Print all parents and their children.
3656 *
aPiecek874ea4d2021-04-19 12:26:36 +02003657 * This function is suitable for printing top-level nodes that
aPiecek153b00f2021-04-20 13:52:57 +02003658 * do not have ancestors. Function call trb_print_subtree_nodes()
3659 * for all top-level siblings. Use this function after 'module' keyword
3660 * or 'augment' and so. The nodes may not be exactly top-level in the
3661 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003662 *
aPiecek153b00f2021-04-20 13:52:57 +02003663 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3664 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003665 * @param[pc] pc contains mainly functions for printing.
3666 * @param[in,out] tc is tree context.
3667 */
3668static void
aPiecek153b00f2021-04-20 13:52:57 +02003669trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003670{
aPiecek61d062b2020-11-02 11:05:09 +01003671 struct trt_parent_cache ca;
3672 uint32_t total_parents;
3673 uint32_t max_gap_before_type;
3674
aPiecekdc8fd572021-04-19 10:47:23 +02003675 if (!trb_tree_ctx_get_node(tc)) {
3676 return;
3677 }
3678
aPiecek61d062b2020-11-02 11:05:09 +01003679 ca = TRP_EMPTY_PARENT_CACHE;
3680 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3681 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3682
aPiecek3f247652021-04-19 13:40:25 +02003683 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003684 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3685 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003686 ca.lys_config = 0x0;
3687 }
aPiecekdc8fd572021-04-19 10:47:23 +02003688 }
3689
aPiecek61d062b2020-11-02 11:05:09 +01003690 for (uint32_t i = 0; i < total_parents; i++) {
3691 ly_print_(pc->out, "\n");
3692 trb_print_subtree_nodes(max_gap_before_type, wr, ca, pc, tc);
3693 pc->fp.modify.next_sibling(ca, tc);
3694 }
3695}
3696
aPiecek874ea4d2021-04-19 12:26:36 +02003697/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003698 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003699 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003700
3701/**
aPiecekdc8fd572021-04-19 10:47:23 +02003702 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003703 *
aPiecekdc8fd572021-04-19 10:47:23 +02003704 * @param[in] module YANG schema tree structure representing
3705 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003706 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003707 * @param[in] max_line_length is the maximum line length limit
3708 * that should not be exceeded.
3709 * @param[in,out] pc will be adapted to lysp_tree.
3710 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003711 */
3712static void
aPiecekdc8fd572021-04-19 10:47:23 +02003713trm_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 +01003714{
aPiecekdc8fd572021-04-19 10:47:23 +02003715 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003716 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003717 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003718 .pmod = module->parsed,
3719 .cmod = NULL,
3720 .pn = module->parsed ? module->parsed->data : NULL,
3721 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003722 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003723 };
aPiecek61d062b2020-11-02 11:05:09 +01003724
aPiecekdc8fd572021-04-19 10:47:23 +02003725 pc->out = out;
3726
3727 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003728 .parent = trop_modi_parent,
3729 .first_sibling = trop_modi_first_sibling,
3730 .next_sibling = trop_modi_next_sibling,
3731 .next_child = trop_modi_next_child,
3732 .next_augment = trop_modi_next_augment,
aPiecek3f247652021-04-19 13:40:25 +02003733 .get_rpcs = tro_modi_get_rpcs,
3734 .get_notifications = tro_modi_get_notifications,
aPiecekef1e58e2021-04-19 13:19:44 +02003735 .next_grouping = trop_modi_next_grouping,
aPiecek61d062b2020-11-02 11:05:09 +01003736 };
3737
aPiecekdc8fd572021-04-19 10:47:23 +02003738 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003739 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003740 .node = trop_read_node,
3741 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003742 };
3743
aPiecekdc8fd572021-04-19 10:47:23 +02003744 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003745 .print_features_names = tro_print_features_names,
3746 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01003747 };
3748
aPiecekdc8fd572021-04-19 10:47:23 +02003749 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01003750}
3751
3752/**
aPiecek3f247652021-04-19 13:40:25 +02003753 * @brief Settings if lysc_node are used for browsing through the tree.
3754 *
3755 * Pointers to current nodes will be set to module data.
3756 *
3757 * @param[in] module YANG schema tree structure representing
3758 * YANG module.
3759 * @param[in] out is output handler.
3760 * @param[in] max_line_length is the maximum line length limit
3761 * that should not be exceeded.
3762 * @param[in,out] pc will be adapted to lysc_tree.
3763 * @param[in,out] tc will be adapted to lysc_tree.
3764 */
3765static void
3766trm_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)
3767{
3768 *tc = (struct trt_tree_ctx) {
3769 .lysc_tree = 1,
3770 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003771 .pmod = module->parsed,
3772 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02003773 .tpn = NULL,
3774 .pn = NULL,
3775 .cn = module->compiled->data
3776 };
3777
3778 pc->out = out;
3779
3780 pc->fp.modify = (struct trt_fp_modify_ctx) {
3781 .parent = troc_modi_parent,
3782 .first_sibling = troc_modi_first_sibling,
3783 .next_sibling = troc_modi_next_sibling,
3784 .next_child = troc_modi_next_child,
3785 .next_augment = trop_modi_next_augment,
3786 .get_rpcs = tro_modi_get_rpcs,
3787 .get_notifications = tro_modi_get_notifications,
3788 .next_grouping = NULL,
aPiecek3f247652021-04-19 13:40:25 +02003789 };
3790
3791 pc->fp.read = (struct trt_fp_read) {
3792 .module_name = tro_read_module_name,
3793 .node = troc_read_node,
3794 .if_sibling_exists = troc_read_if_sibling_exists
3795 };
3796
3797 pc->fp.print = (struct trt_fp_print) {
3798 .print_features_names = tro_print_features_names,
3799 .print_keys = tro_print_keys
3800 };
3801
3802 pc->max_line_length = max_line_length;
3803}
3804
3805/**
3806 * @brief Reset settings to browsing through the lysc tree.
3807 * @param[in,out] pc resets to \ref TRP_troc functions.
3808 * @param[in,out] tc resets to lysc browsing.
3809 */
3810static void
3811trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3812{
aPiecek9f792e52021-04-21 08:33:56 +02003813 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003814}
3815
3816/**
3817 * @brief Reset settings to browsing through the lysp tree.
3818 * @param[in,out] pc resets to \ref TRP_trop functions.
3819 * @param[in,out] tc resets to lysp browsing.
3820 */
3821static void
3822trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3823{
aPiecek9f792e52021-04-21 08:33:56 +02003824 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003825}
3826
3827/**
3828 * @brief If augment's target node is located on the current module.
3829 * @param[in] pn is examined augment.
3830 * @param[in] pmod is current module.
3831 * @return 1 if nodeid refers to the local node, otherwise 0.
3832 */
3833static ly_bool
3834trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
3835{
3836 const char *id, *prefix, *name;
3837 size_t prefix_len, name_len;
3838 const struct lys_module *mod;
3839 ly_bool ret = 0;
3840
3841 if (pn == NULL) {
3842 return ret;
3843 }
3844
3845 id = pn->nodeid;
3846 if (!id) {
3847 return ret;
3848 }
3849 /* only absolute-schema-nodeid is taken into account */
3850 assert(id[0] == '/');
3851 ++id;
3852
3853 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3854 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003855 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
aPiecek3f247652021-04-19 13:40:25 +02003856 ret = mod->parsed == pmod;
3857 } else {
3858 ret = 1;
3859 }
3860
3861 return ret;
3862}
3863
3864/**
aPiecek96baa7f2021-04-23 12:32:00 +02003865 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01003866 *
aPiecekdc8fd572021-04-19 10:47:23 +02003867 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02003868 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01003869 *
aPiecekdc8fd572021-04-19 10:47:23 +02003870 * @param[in] ks is section representation.
3871 * @param[in] pc contains mainly functions for printing.
3872 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01003873 */
3874static void
aPiecekdc8fd572021-04-19 10:47:23 +02003875trm_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 +01003876{
aPiecekdc8fd572021-04-19 10:47:23 +02003877 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3878 return;
3879 }
3880
3881 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
3882 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02003883 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003884 } else {
aPiecek153b00f2021-04-20 13:52:57 +02003885 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003886 }
3887}
3888
3889/**
aPiecek96baa7f2021-04-23 12:32:00 +02003890 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02003891 *
aPiecek96baa7f2021-04-23 12:32:00 +02003892 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02003893 *
3894 * @param[in] ks is section representation.
3895 * @param[in] pc contains mainly functions for printing.
3896 * @param[in,out] tc is the tree context.
3897 */
3898static void
3899trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3900{
3901 ly_bool grp_has_data = 0;
3902
3903 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3904 return;
3905 }
3906
3907 if (ks.type == TRD_KEYWORD_GROUPING) {
3908 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
3909 }
3910
3911 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
3912 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02003913 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003914}
3915
3916/**
3917 * @brief Print 'module' keyword, its name 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_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3923{
3924 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
3925}
3926
3927/**
3928 * @brief For all augment sections: print 'augment' keyword,
3929 * its target node and all nodes.
3930 * @param[in] pc contains mainly functions for printing.
3931 * @param[in,out] tc is the tree context.
3932 */
3933static void
3934trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3935{
3936 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02003937 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02003938
3939 if (!pc->fp.modify.next_augment) {
3940 return;
3941 }
3942
aPiecek3f247652021-04-19 13:40:25 +02003943 if (tc->lysc_tree) {
3944 origin_was_lysc_tree = 1;
3945 trm_reset_to_lysp_tree_ctx(pc, tc);
3946 }
3947
aPiecekdc8fd572021-04-19 10:47:23 +02003948 once = 1;
3949 for (struct trt_keyword_stmt ks = pc->fp.modify.next_augment(tc);
3950 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
3951 ks = pc->fp.modify.next_augment(tc)) {
3952
aPiecek3f247652021-04-19 13:40:25 +02003953 if (origin_was_lysc_tree) {
3954 /* if lysc tree is used, then only augments targeting
3955 * another module are printed
3956 */
aPiecek9f792e52021-04-21 08:33:56 +02003957 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02003958 continue;
3959 }
3960 }
3961
aPiecekdc8fd572021-04-19 10:47:23 +02003962 if (once) {
3963 ly_print_(pc->out, "\n");
3964 ly_print_(pc->out, "\n");
3965 once = 0;
3966 } else {
3967 ly_print_(pc->out, "\n");
3968 }
3969
3970 trm_print_section_as_subtree(ks, pc, tc);
3971 }
aPiecek3f247652021-04-19 13:40:25 +02003972
3973 if (origin_was_lysc_tree) {
3974 trm_reset_to_lysc_tree_ctx(pc, tc);
3975 }
aPiecekdc8fd572021-04-19 10:47:23 +02003976}
3977
3978/**
3979 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
3980 * @param[in] pc contains mainly functions for printing.
3981 * @param[in,out] tc is the tree context.
3982 */
3983static void
3984trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3985{
3986 struct trt_keyword_stmt rpc;
3987
3988 assert(pc->fp.modify.get_rpcs);
3989
3990 rpc = pc->fp.modify.get_rpcs(tc);
3991
3992 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
3993 ly_print_(pc->out, "\n");
3994 ly_print_(pc->out, "\n");
3995 trm_print_section_as_family_tree(rpc, pc, tc);
3996 }
3997}
3998
3999/**
4000 * @brief For notifications section: print 'notifications' keyword
4001 * and all its nodes.
4002 * @param[in] pc contains mainly functions for printing.
4003 * @param[in,out] tc is the tree context.
4004 */
4005static void
4006trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4007{
4008 struct trt_keyword_stmt notifs;
4009
4010 assert(pc->fp.modify.get_notifications);
4011
4012 notifs = pc->fp.modify.get_notifications(tc);
4013
4014 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4015 ly_print_(pc->out, "\n");
4016 ly_print_(pc->out, "\n");
4017 trm_print_section_as_family_tree(notifs, pc, tc);
4018 }
4019}
4020
4021/**
4022 * @brief For all grouping sections: print 'grouping' keyword, its name
4023 * and all nodes.
4024 * @param[in] pc contains mainly functions for printing.
4025 * @param[in,out] tc is the tree context.
4026 */
4027static void
4028trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4029{
4030 ly_bool once;
4031
4032 if (!pc->fp.modify.next_grouping) {
4033 return;
4034 }
4035
4036 once = 1;
4037 for (struct trt_keyword_stmt ks = pc->fp.modify.next_grouping(tc);
4038 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
4039 ks = pc->fp.modify.next_grouping(tc)) {
4040 if (once) {
4041 ly_print_(pc->out, "\n");
4042 ly_print_(pc->out, "\n");
4043 once = 0;
4044 } else {
4045 ly_print_(pc->out, "\n");
4046 }
4047 trm_print_section_as_subtree(ks, pc, tc);
4048 }
4049}
4050
4051/**
4052 * @brief For all yang-data sections: print 'yang-data' keyword
4053 * and all its nodes.
4054 * @param[in] pc contains mainly functions for printing.
4055 * @param[in,out] tc is the tree context.
4056 */
4057static void
aPiecek96baa7f2021-04-23 12:32:00 +02004058trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004059{
aPiecek96baa7f2021-04-23 12:32:00 +02004060 ly_bool once;
4061 LY_ARRAY_COUNT_TYPE count;
4062
4063 count = LY_ARRAY_COUNT(tc->pmod->exts);
4064 if (count == 0) {
4065 return;
4066 }
4067
4068 once = 1;
4069 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4070 struct trt_keyword_stmt ks;
4071
4072 /* Only lys_compile_extension_instance() can set item
4073 * ::lysp_ext_instance.parsed.
4074 */
4075 if (!tc->pmod->exts[u].parsed) {
4076 /* print at least the yang-data names */
4077 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4078 continue;
4079 }
4080
4081 ks = tro_modi_next_yang_data(tc, u);
4082 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4083 break;
4084 }
4085
4086 if (once) {
4087 ly_print_(pc->out, "\n");
4088 ly_print_(pc->out, "\n");
4089 once = 0;
4090 } else {
4091 ly_print_(pc->out, "\n");
4092 }
4093
4094 trm_print_section_as_family_tree(ks, pc, tc);
4095 }
aPiecekdc8fd572021-04-19 10:47:23 +02004096}
4097
4098/**
4099 * @brief Print sections module, augment, rpcs, notifications,
4100 * grouping, yang-data.
4101 * @param[in] pc contains mainly functions for printing.
4102 * @param[in,out] tc is the tree context.
4103 */
4104static void
4105trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4106{
4107 trm_print_module_section(pc, tc);
4108 trm_print_augmentations(pc, tc);
4109 trm_print_rpcs(pc, tc);
4110 trm_print_notifications(pc, tc);
4111 trm_print_groupings(pc, tc);
4112 trm_print_yang_data(pc, tc);
4113 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004114}
4115
aPiecek874ea4d2021-04-19 12:26:36 +02004116/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004117 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004118 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004119
4120LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004121tree_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 +01004122{
4123 struct trt_printer_ctx pc;
4124 struct trt_tree_ctx tc;
4125 struct ly_out *new_out;
4126 LY_ERR erc;
4127 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4128
aPiecekdc8fd572021-04-19 10:47:23 +02004129 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4130
aPiecek61d062b2020-11-02 11:05:09 +01004131 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4132 return erc;
4133 }
4134
4135 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004136 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4137 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4138 } else {
4139 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4140 }
aPiecek61d062b2020-11-02 11:05:09 +01004141
4142 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004143 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004144
4145 ly_out_free(new_out, NULL, 1);
4146
aPiecekdc8fd572021-04-19 10:47:23 +02004147 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004148}
4149
4150LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004151tree_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 +01004152{
aPiecek153b00f2021-04-20 13:52:57 +02004153 struct trt_printer_ctx pc;
4154 struct trt_tree_ctx tc;
4155 struct ly_out *new_out;
4156 struct trt_wrapper wr;
4157 LY_ERR erc;
4158 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4159
4160 assert(out && node);
4161
4162 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4163 return LY_EINVAL;
4164 }
4165
4166 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4167 return erc;
4168 }
4169
4170 line_length = line_length == 0 ? SIZE_MAX : line_length;
4171 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4172
4173 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
4174 trb_print_parents(node, &pc, &tc);
4175
4176 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4177 tc.cn = lysc_node_child(node);
4178 wr = trb_count_depth(tc.cn);
4179 trb_print_family_tree(wr, &pc, &tc);
4180 }
4181 ly_print_(out, "\n");
4182
4183 erc = clb_arg.last_error;
4184 ly_out_free(new_out, NULL, 1);
4185
4186 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004187}
4188
4189LY_ERR
aPiecek9f792e52021-04-21 08:33:56 +02004190tree_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 +01004191{
aPiecek9f792e52021-04-21 08:33:56 +02004192 struct trt_printer_ctx pc;
4193 struct trt_tree_ctx tc;
4194 struct ly_out *new_out;
4195 LY_ERR erc;
4196 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4197
4198 assert(submodp);
4199 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4200
4201 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4202 return erc;
4203 }
4204
4205 line_length = line_length == 0 ? SIZE_MAX : line_length;
4206 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4207 tc.pmod = (struct lysp_module *)submodp;
4208 tc.tpn = submodp->data;
4209 tc.pn = tc.tpn;
4210
4211 trm_print_sections(&pc, &tc);
4212 erc = clb_arg.last_error;
4213
4214 ly_out_free(new_out, NULL, 1);
4215
4216 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004217}