blob: 286b2866469c645ddae08ff01e5cb6db567ca6e9 [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. */
568 struct trt_keyword_stmt (*next_yang_data)(struct trt_tree_ctx *); /**< Jump to the yang-data section. */
569};
570
aPiecek874ea4d2021-04-19 12:26:36 +0200571/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100572 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200573 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100574
575/**
576 * @brief Functions that do not change the state of the tree_structure.
577 *
578 * For details see trt_fp_modify_ctx.
579 */
580struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200581 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
582 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
583 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100584};
585
aPiecek874ea4d2021-04-19 12:26:36 +0200586/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100587 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200588 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100589
590/**
aPiecek874ea4d2021-04-19 12:26:36 +0200591 * @brief A set of all necessary functions that must be provided
592 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100593 */
594struct trt_fp_all {
595 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
596 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
597 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
598};
599
aPiecek874ea4d2021-04-19 12:26:36 +0200600/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100601 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200602 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100603
604/**
aPiecek874ea4d2021-04-19 12:26:36 +0200605 * @brief Main structure for \ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100606 */
607struct trt_printer_ctx {
608 struct ly_out *out; /**< Handler to printing. */
aPiecek874ea4d2021-04-19 12:26:36 +0200609 struct trt_fp_all fp; /**< \ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100610 size_t max_line_length; /**< The maximum number of characters that can be
611 printed on one line, including the last. */
612};
613
aPiecek874ea4d2021-04-19 12:26:36 +0200614/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100615 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200616 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100617
618/**
619 * @brief The name of the section to which the node belongs.
620 */
621typedef enum {
622 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
623 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
624 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
625 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
626 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
627 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
628} trt_actual_section;
629
630/**
631 * @brief Types of nodes that have some effect on their children.
632 */
633typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200634 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
635 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
636 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
637 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100638} trt_ancestor_type;
639
640/**
641 * @brief Saved information when browsing the tree downwards.
642 *
aPiecek874ea4d2021-04-19 12:26:36 +0200643 * This structure helps prevent frequent retrieval of information
644 * from the tree. Functions \ref TRP_trb are designed to preserve
645 * this structures during their recursive calls. This functions do not
646 * interfere in any way with this data. This structure
aPiecekef1e58e2021-04-19 13:19:44 +0200647 * is used by \ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200648 * structure, can return a node with the correct data. The word
649 * \b parent is in the structure name, because this data refers to
650 * the last parent and at the same time the states of its
651 * ancestors data. Only the function jumping on the child
652 * (next_child(...)) creates this structure, because the pointer
653 * to the current node moves down the tree. It's like passing
654 * the genetic code to children. Some data must be inherited and
655 * there are two approaches to this problem. Either it will always
656 * be determined which inheritance states belong to the current node
657 * (which can lead to regular travel to the root node) or
658 * the inheritance states will be stored during the recursive calls.
659 * So the problem was solved by the second option. Why does
660 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200661 * the lysp tree. For walks through the lysc tree is trt_parent_cache
662 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100663 *
aPiecek874ea4d2021-04-19 12:26:36 +0200664 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100665 */
666struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200667 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
668 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
669 uint16_t lys_config; /**< Inherited config W or R. */
670 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100671};
672
673/**
674 * @brief Return trt_parent_cache filled with default values.
675 */
676#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200677 (struct trt_parent_cache) { \
678 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
679 .lys_config = LYS_CONFIG_W, .last_list = NULL \
680 }
aPiecek61d062b2020-11-02 11:05:09 +0100681
682/**
683 * @brief Main structure for browsing the libyang tree
684 */
685struct trt_tree_ctx {
aPiecek3f247652021-04-19 13:40:25 +0200686 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
687 It is assumed that once set, it does not change.
688 If it is true then trt_tree_ctx.pn and
689 trt_tree_ctx.tpn are not used.
690 If it is false then trt_tree_ctx.cn is not used. */
aPiecek61d062b2020-11-02 11:05:09 +0100691 trt_actual_section section; /**< To which section pn points. */
692 const struct lys_module *module; /**< Schema tree structures. */
693 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
694 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
aPiecek3f247652021-04-19 13:40:25 +0200695 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
aPiecek61d062b2020-11-02 11:05:09 +0100696};
697
aPiecek3f247652021-04-19 13:40:25 +0200698/**
699 * @brief Get lysp_node from trt_tree_ctx.cn.
700 */
701#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
702 ((const struct lysp_node *)CN->priv)
703
704/** Getter function for trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100705typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
706
aPiecekef1e58e2021-04-19 13:19:44 +0200707/**
708 * @brief Simple getter functions for lysp and lysc nodes.
709 *
710 * This structure is useful if we have a general algorithm
711 * (tro function) that can be used for both lysc and lysp nodes.
712 * Thanks to this structure, we prevent code redundancy.
713 * We don't have to write basically the same algorithm twice
714 * for lysp and lysc trees.
715 */
716struct tro_getters
717{
718 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
719 const void *(*next)(const void *); /**< Get sibling. */
720 const void *(*parent)(const void *); /**< Get parent. */
721 const void *(*child)(const void *); /**< Get child. */
722 const void *(*actions)(const void *); /**< Get actions. */
723 const void *(*action_input)(const void *); /**< Get input action from action node. */
724 const void *(*action_output)(const void *); /**< Get output action from action node. */
725 const void *(*notifs)(const void *); /**< Get notifs. */
726};
727
aPiecek874ea4d2021-04-19 12:26:36 +0200728/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100729 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200730 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100731
732/**
733 * @brief Print a substring but limited to the maximum length.
734 * @param[in] str is pointer to source.
735 * @param[in] len is number of characters to be printed.
736 * @param[in,out] out is output handler.
737 * @return str parameter shifted by len.
738 */
739static const char *
740trg_print_substr(const char *str, size_t len, struct ly_out *out)
741{
742 for (size_t i = 0; i < len; i++) {
743 ly_print_(out, "%c", str[0]);
744 str++;
745 }
746 return str;
747}
748
749/**
750 * @brief Pointer is not NULL and does not point to an empty string.
751 * @param[in] str is pointer to string to be checked.
752 * @return 1 if str pointing to non empty string otherwise 0.
753 */
754static ly_bool
755trg_charptr_has_data(const char *str)
756{
757 return (str) && (str[0] != '\0');
758}
759
760/**
aPiecek874ea4d2021-04-19 12:26:36 +0200761 * @brief Check if @p word in @p src is present where words are
762 * delimited by @p delim.
763 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100764 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200765 * @param[in] delim is delimiter between @p words in @p src.
766 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100767 */
768static ly_bool
769trg_word_is_present(const char *src, const char *word, char delim)
770{
771 const char *hit;
772
773 if ((!src) || (src[0] == '\0') || (!word)) {
774 return 0;
775 }
776
777 hit = strstr(src, word);
778
779 if (hit) {
780 /* word was founded at the begin of src
781 * OR it match somewhere after delim
782 */
783 if ((hit == src) || (hit[-1] == delim)) {
784 /* end of word was founded at the end of src
785 * OR end of word was match somewhere before delim
786 */
787 char delim_or_end = (hit + strlen(word))[0];
788 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
789 return 1;
790 }
791 }
792 /* after -> hit is just substr and it's not the whole word */
793 /* jump to the next word */
794 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
795 /* skip delim */
796 src = src[0] == '\0' ? src : src + 1;
797 /* continue with searching */
798 return trg_word_is_present(src, word, delim);
799 } else {
800 return 0;
801 }
802}
803
aPiecek874ea4d2021-04-19 12:26:36 +0200804/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100805 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200806 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100807
808/**
aPiecek874ea4d2021-04-19 12:26:36 +0200809 * @brief Write callback for ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100810 *
aPiecek874ea4d2021-04-19 12:26:36 +0200811 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100812 * @param[in] buf contains input characters
813 * @param[in] count is number of characters in buf.
814 * @return Number of printed bytes.
815 * @return Negative value in case of error.
816 */
817static ssize_t
818trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
819{
820 LY_ERR erc = LY_SUCCESS;
821 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
822
823 switch (data->mode) {
824 case TRD_PRINT:
825 erc = ly_write_(data->out, buf, count);
826 break;
827 case TRD_CHAR_COUNT:
828 data->counter = data->counter + count;
829 break;
830 default:
831 break;
832 }
833
834 if (erc != LY_SUCCESS) {
835 data->last_error = erc;
836 return -1;
837 } else {
838 return count;
839 }
840}
841
842/**
843 * @brief Check that indent in node can be considered as equivalent.
844 * @param[in] first is the first indent in node.
845 * @param[in] second is the second indent in node.
846 * @return 1 if indents are equivalent otherwise 0.
847 */
848static ly_bool
849trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
850{
851 const ly_bool a = first.type == second.type;
852 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
853 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
854 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
855
856 return a && b && c && d;
857}
858
859/**
aPiecek874ea4d2021-04-19 12:26:36 +0200860 * @brief Setting space character because node is last sibling.
861 * @param[in] wr is wrapper over which the shift operation
862 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100863 * @return New shifted wrapper.
864 */
865static struct trt_wrapper
866trp_wrapper_set_shift(struct trt_wrapper wr)
867{
868 assert(wr.actual_pos < 64);
869 /* +--<node>
870 * +--<node>
871 */
872 wr.actual_pos++;
873 return wr;
874}
875
876/**
aPiecek874ea4d2021-04-19 12:26:36 +0200877 * @brief Setting '|' symbol because node is divided or
878 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100879 * @param[in] wr is source of wrapper.
880 * @return New wrapper which is marked at actual position and shifted.
881 */
882static struct trt_wrapper
883trp_wrapper_set_mark(struct trt_wrapper wr)
884{
885 assert(wr.actual_pos < 64);
886 wr.bit_marks1 |= 1U << wr.actual_pos;
887 return trp_wrapper_set_shift(wr);
888}
889
890/**
891 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
892 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200893 * @param[in] last_one is flag. Value 1 saying if the node is the last
894 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100895 * @return New wrapper for the actual node.
896 */
897static struct trt_wrapper
898trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
899{
900 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
901}
902
903/**
904 * @brief Test if the wrappers are equivalent.
905 * @param[in] first is the first wrapper.
906 * @param[in] second is the second wrapper.
907 * @return 1 if the wrappers are equivalent otherwise 0.
908 */
909static ly_bool
910trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
911{
912 const ly_bool a = first.type == second.type;
913 const ly_bool b = first.bit_marks1 == second.bit_marks1;
914 const ly_bool c = first.actual_pos == second.actual_pos;
915
916 return a && b && c;
917}
918
919/**
920 * @brief Print " | " sequence on line.
921 * @param[in] wr is wrapper to be printed.
922 * @param[in,out] out is output handler.
923 */
924static void
925trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
926{
927 uint32_t lb;
928
929 if (wr.type == TRD_WRAPPER_TOP) {
930 lb = TRD_INDENT_LINE_BEGIN;
931 } else if (wr.type == TRD_WRAPPER_BODY) {
932 lb = TRD_INDENT_LINE_BEGIN * 2;
933 } else {
934 lb = TRD_INDENT_LINE_BEGIN;
935 }
936
937 ly_print_(out, "%*c", lb, ' ');
938
939 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
940 return;
941 }
942
943 for (uint32_t i = 0; i < wr.actual_pos; i++) {
944 /** Test if the bit on the index is set. */
945 if ((wr.bit_marks1 >> i) & 1U) {
946 ly_print_(out, "|");
947 } else {
948 ly_print_(out, " ");
949 }
950
951 if (i != wr.actual_pos) {
952 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
953 }
954 }
955}
956
957/**
958 * @brief Check if struct trt_node is empty.
959 * @param[in] node is item to test.
960 * @return 1 if node is considered empty otherwise 0.
961 */
962static ly_bool
963trp_node_is_empty(struct trt_node node)
964{
965 const ly_bool a = !node.iffeatures;
966 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
967 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
968 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
969 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
970
971 return a && b && c && d && e;
972}
973
974/**
aPiecek874ea4d2021-04-19 12:26:36 +0200975 * @brief Check if [\<keys\>], \<type\> and
976 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +0100977 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +0200978 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
979 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100980 */
981static ly_bool
982trp_node_body_is_empty(struct trt_node node)
983{
984 const ly_bool a = !node.iffeatures;
985 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
986 const ly_bool c = node.name.type != TRD_NODE_KEYS;
987
988 return a && b && c;
989}
990
991/**
992 * @brief Print \<status\> of the node.
993 * @param[in] status_type is type of status.
994 * @param[in,out] out is output handler.
995 */
996static void
997trp_print_status(trt_status_type status_type, struct ly_out *out)
998{
999 switch (status_type) {
1000 case TRD_STATUS_TYPE_CURRENT:
1001 ly_print_(out, "%c", '+');
1002 break;
1003 case TRD_STATUS_TYPE_DEPRECATED:
1004 ly_print_(out, "%c", 'x');
1005 break;
1006 case TRD_STATUS_TYPE_OBSOLETE:
1007 ly_print_(out, "%c", 'o');
1008 break;
1009 default:
1010 break;
1011 }
1012}
1013
1014/**
1015 * @brief Print \<flags\>.
1016 * @param[in] flags_type is type of \<flags\>.
1017 * @param[in,out] out is output handler.
1018 */
1019static void
1020trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1021{
1022 switch (flags_type) {
1023 case TRD_FLAGS_TYPE_RW:
1024 ly_print_(out, "%s", "rw");
1025 break;
1026 case TRD_FLAGS_TYPE_RO:
1027 ly_print_(out, "%s", "ro");
1028 break;
1029 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1030 ly_print_(out, "%s", "-w");
1031 break;
1032 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1033 ly_print_(out, "%s", "-u");
1034 break;
1035 case TRD_FLAGS_TYPE_RPC:
1036 ly_print_(out, "%s", "-x");
1037 break;
1038 case TRD_FLAGS_TYPE_NOTIF:
1039 ly_print_(out, "%s", "-n");
1040 break;
1041 case TRD_FLAGS_TYPE_MOUNT_POINT:
1042 ly_print_(out, "%s", "mp");
1043 break;
1044 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001045 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001046 break;
1047 }
1048}
1049
1050/**
1051 * @brief Get size of the \<flags\>.
1052 * @param[in] flags_type is type of \<flags\>.
1053 * @return 0 if flags_type is not set otherwise 2.
1054 */
1055static size_t
1056trp_get_flags_strlen(trt_flags_type flags_type)
1057{
1058 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1059}
1060
1061/**
1062 * @brief Print entire struct trt_node_name structure.
1063 * @param[in] node_name is item to print.
1064 * @param[in,out] out is output handler.
1065 */
1066static void
1067trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1068{
1069 const char *mod_prefix;
1070 const char *colon;
1071 const char trd_node_name_suffix_choice[] = ")";
1072 const char trd_node_name_suffix_case[] = ")";
1073 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1074 const char trd_opts_container[] = "!"; /**< For a presence container. */
1075 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1076 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1077 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1078
1079 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1080 return;
1081 }
1082
1083 if (node_name.module_prefix) {
1084 mod_prefix = node_name.module_prefix;
1085 colon = ":";
1086 } else {
1087 mod_prefix = "";
1088 colon = "";
1089 }
1090
1091 switch (node_name.type) {
1092 case TRD_NODE_ELSE:
1093 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1094 break;
1095 case TRD_NODE_CASE:
1096 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1097 break;
1098 case TRD_NODE_CHOICE:
1099 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1100 break;
1101 case TRD_NODE_OPTIONAL_CHOICE:
1102 ly_print_(out, "%s%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice, trd_opts_optional);
1103 break;
1104 case TRD_NODE_OPTIONAL:
1105 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1106 break;
1107 case TRD_NODE_CONTAINER:
1108 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1109 break;
1110 case TRD_NODE_LISTLEAFLIST:
1111 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1112 break;
1113 case TRD_NODE_KEYS:
1114 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1115 break;
1116 case TRD_NODE_TOP_LEVEL1:
1117 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1118 break;
1119 case TRD_NODE_TOP_LEVEL2:
1120 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1121 break;
1122 case TRD_NODE_TRIPLE_DOT:
1123 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1124 break;
1125 default:
1126 break;
1127 }
1128}
1129
1130/**
aPiecek874ea4d2021-04-19 12:26:36 +02001131 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1132 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001133 * @param[in] node_name is structure containing the 'mark'.
1134 * @return 1 if contain otherwise 0.
1135 */
1136static ly_bool
1137trp_mark_is_used(struct trt_node_name node_name)
1138{
1139 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1140 return 0;
1141 }
1142
1143 switch (node_name.type) {
1144 case TRD_NODE_ELSE:
1145 case TRD_NODE_CASE:
1146 case TRD_NODE_KEYS:
1147 return 0;
1148 default:
1149 return 1;
1150 }
1151}
1152
1153/**
1154 * @brief Print opts keys.
1155 * @param[in] node_name contains type of the node with his name.
1156 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001157 * @param[in] cf is basically a pointer to the function that prints
1158 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001159 * @param[in,out] out is output handler.
1160 */
1161static void
1162trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1163{
1164 if (node_name.type != TRD_NODE_KEYS) {
1165 return;
1166 }
1167
1168 /* <name><mark>___<keys>*/
1169 if (btw_name_opts > 0) {
1170 ly_print_(out, "%*c", btw_name_opts, ' ');
1171 }
1172 ly_print_(out, "[");
1173 cf.pf(cf.ctx, out);
1174 ly_print_(out, "]");
1175}
1176
1177/**
1178 * @brief Print entire struct trt_type structure.
1179 * @param[in] type is item to print.
1180 * @param[in,out] out is output handler.
1181 */
1182static void
1183trp_print_type(struct trt_type type, struct ly_out *out)
1184{
1185 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1186 return;
1187 }
1188
1189 switch (type.type) {
1190 case TRD_TYPE_NAME:
1191 ly_print_(out, "%s", type.str);
1192 break;
1193 case TRD_TYPE_TARGET:
1194 ly_print_(out, "-> %s", type.str);
1195 break;
1196 case TRD_TYPE_LEAFREF:
1197 ly_print_(out, "leafref");
1198 default:
1199 break;
1200 }
1201}
1202
1203/**
1204 * @brief Print all iffeatures of node
1205 *
1206 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001207 * @param[in] cf is basically a pointer to the function that prints
1208 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001209 * @param[in,out] out is output handler.
1210 */
1211static void
1212trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1213{
1214 if (iffeature_flag) {
1215 ly_print_(out, "{");
1216 cf.pf(cf.ctx, out);
1217 ly_print_(out, "}?");
1218 }
1219}
1220
1221/**
1222 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1223 * @param[in] node contains items to print.
1224 * @param[in] out is output handler.
1225 */
1226static void
1227trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1228{
1229 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1230 trp_print_node_name(node.name, out);
1231 return;
1232 }
1233 /* <status>--<flags> */
1234 trp_print_status(node.status, out);
1235 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001236 /* If the node is a case node, there is no space before the <name>
1237 * also case node has no flags.
1238 */
aPiecek61d062b2020-11-02 11:05:09 +01001239 if (node.name.type != TRD_NODE_CASE) {
1240 trp_print_flags(node.flags, out);
1241 ly_print_(out, " ");
1242 }
1243 /* <name> */
1244 trp_print_node_name(node.name, out);
1245}
1246
1247/**
aPiecek874ea4d2021-04-19 12:26:36 +02001248 * @brief Print alignment (spaces) instead of
1249 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001250 * @param[in] node contains items to print.
1251 * @param[in] out is output handler.
1252 */
1253static void
1254trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1255{
1256 uint32_t space = trp_get_flags_strlen(node.flags);
1257
1258 if (node.name.type == TRD_NODE_CASE) {
1259 /* :(<name> */
1260 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1261 } else if (node.name.type == TRD_NODE_CHOICE) {
1262 /* (<name> */
1263 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1264 } else {
1265 /* _<name> */
1266 space += strlen(" ");
1267 }
1268
1269 /* <name>
1270 * __
1271 */
1272 space += TRD_INDENT_LONG_LINE_BREAK;
1273
1274 ly_print_(out, "%*c", space, ' ');
1275}
1276
1277/**
1278 * @brief Print struct trt_node structure.
1279 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001280 * @param[in] pck package of functions for
1281 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001282 * @param[in] indent is the indent in node.
1283 * @param[in,out] out is output handler.
1284 */
1285static void
1286trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1287{
1288 ly_bool triple_dot;
1289 ly_bool divided;
1290 struct trt_cf_print cf_print_keys;
1291 struct trt_cf_print cf_print_iffeatures;
1292
1293 if (trp_node_is_empty(node)) {
1294 return;
1295 }
1296
1297 /* <status>--<flags> <name><opts> <type> <if-features> */
1298 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1299 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1300
1301 if (triple_dot) {
1302 trp_print_node_name(node.name, out);
1303 return;
1304 } else if (!divided) {
1305 trp_print_node_up_to_name(node, out);
1306 } else {
1307 trp_print_divided_node_up_to_name(node, out);
1308 }
1309
1310 /* <opts> */
1311 /* <name>___<opts>*/
1312 cf_print_keys.ctx = pck.tree_ctx;
1313 cf_print_keys.pf = pck.fps.print_keys;
1314
1315 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1316
1317 /* <opts>__<type> */
1318 if (indent.btw_opts_type > 0) {
1319 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1320 }
1321
1322 /* <type> */
1323 trp_print_type(node.type, out);
1324
1325 /* <type>__<iffeatures> */
1326 if (indent.btw_type_iffeatures > 0) {
1327 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1328 }
1329
1330 /* <iffeatures> */
1331 cf_print_iffeatures.ctx = pck.tree_ctx;
1332 cf_print_iffeatures.pf = pck.fps.print_features_names;
1333
1334 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1335}
1336
1337/**
aPiecek874ea4d2021-04-19 12:26:36 +02001338 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001339 * @param[in] ks is keyword statement to print.
1340 * @param[in,out] out is output handler
1341 */
1342static void
1343trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1344{
1345 switch (ks.type) {
1346 case TRD_KEYWORD_MODULE:
1347 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1348 return;
1349 case TRD_KEYWORD_SUBMODULE:
1350 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1351 return;
1352 default:
1353 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1354 switch (ks.type) {
1355 case TRD_KEYWORD_AUGMENT:
1356 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1357 break;
1358 case TRD_KEYWORD_RPC:
1359 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1360 break;
1361 case TRD_KEYWORD_NOTIF:
1362 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1363 break;
1364 case TRD_KEYWORD_GROUPING:
1365 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1366 break;
1367 case TRD_KEYWORD_YANG_DATA:
1368 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1369 break;
1370 default:
1371 break;
1372 }
1373 break;
1374 }
1375}
1376
1377/**
1378 * @brief Get string length of stored keyword.
1379 * @param[in] type is type of the keyword statement.
1380 * @return length of the keyword statement name.
1381 */
1382static size_t
1383trp_keyword_type_strlen(trt_keyword_type type)
1384{
1385 switch (type) {
1386 case TRD_KEYWORD_MODULE:
1387 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1388 case TRD_KEYWORD_SUBMODULE:
1389 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1390 case TRD_KEYWORD_AUGMENT:
1391 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1392 case TRD_KEYWORD_RPC:
1393 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1394 case TRD_KEYWORD_NOTIF:
1395 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1396 case TRD_KEYWORD_GROUPING:
1397 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1398 case TRD_KEYWORD_YANG_DATA:
1399 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1400 default:
1401 return 0;
1402 }
1403}
1404
1405/**
aPiecek874ea4d2021-04-19 12:26:36 +02001406 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001407 * @param[in] ks is keyword statement structure.
1408 * @param[in] mll is max line length.
1409 * @param[in,out] out is output handler.
1410 */
1411static void
1412trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1413{
1414 uint32_t ind_initial;
1415 uint32_t ind_divided;
1416 /* flag if path must be splitted to more lines */
1417 ly_bool linebreak_was_set;
1418 /* flag if at least one subpath was printed */
1419 ly_bool subpath_printed;
1420 /* the sum of the sizes of the substrings on the current line */
1421 uint32_t how_far;
1422 /* pointer to start of the subpath */
1423 const char *sub_ptr;
1424 /* size of subpath from sub_ptr */
1425 size_t sub_len;
1426
1427 if ((!ks.str) || (ks.str[0] == '\0')) {
1428 return;
1429 }
1430
1431 /* module name cannot be splitted */
1432 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1433 ly_print_(out, "%s", ks.str);
1434 return;
1435 }
1436
1437 /* after -> for trd_keyword_stmt_body do */
1438
1439 /* set begin indentation */
1440 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1441 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1442 linebreak_was_set = 0;
1443 subpath_printed = 0;
1444 how_far = 0;
1445 sub_ptr = ks.str;
1446 sub_len = 0;
1447
1448 while (sub_ptr[0] != '\0') {
1449 uint32_t ind;
1450 /* skip slash */
1451 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
1452 /* get position of the end of substr */
1453 tmp = strchr(tmp, '/');
1454 /* set correct size if this is a last substring */
1455 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1456 /* actualize sum of the substring's sizes on the current line */
1457 how_far += sub_len;
1458 /* correction due to colon character if it this is last substring */
1459 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1460 /* choose indentation which depends on
1461 * whether the string is printed on multiple lines or not
1462 */
1463 ind = linebreak_was_set ? ind_divided : ind_initial;
1464 if (ind + how_far <= mll) {
1465 /* printing before max line length */
1466 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1467 subpath_printed = 1;
1468 } else {
1469 /* printing on new line */
1470 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001471 /* first subpath is too long
1472 * but print it at first line anyway
1473 */
aPiecek61d062b2020-11-02 11:05:09 +01001474 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1475 subpath_printed = 1;
1476 continue;
1477 }
1478 ly_print_(out, "\n");
1479 ly_print_(out, "%*c", ind_divided, ' ');
1480 linebreak_was_set = 1;
1481 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1482 how_far = sub_len;
1483 subpath_printed = 1;
1484 }
1485 }
1486}
1487
1488/**
aPiecek874ea4d2021-04-19 12:26:36 +02001489 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001490 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001491 * @param[in] grp_has_data is flag only for grouping section.
1492 * Set to 1 if grouping section has some nodes.
1493 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001494 * @param[in,out] out is output handler.
1495 */
1496static void
aPiecekdc8fd572021-04-19 10:47:23 +02001497trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001498{
1499 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001500 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1501 return;
1502 } else {
1503 ly_print_(out, ":");
1504 }
aPiecek61d062b2020-11-02 11:05:09 +01001505 }
1506}
1507
1508/**
1509 * @brief Print entire struct trt_keyword_stmt structure.
1510 * @param[in] ks is item to print.
1511 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001512 * @param[in] grp_has_data is flag only for grouping section.
1513 * Set to 1 if grouping section has some nodes.
1514 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001515 * @param[in,out] out is output handler.
1516 */
1517static void
aPiecek874ea4d2021-04-19 12:26:36 +02001518trp_print_keyword_stmt(struct trt_keyword_stmt ks, size_t mll, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001519{
1520 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1521 return;
1522 }
1523 trt_print_keyword_stmt_begin(ks, out);
1524 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001525 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001526}
1527
aPiecek874ea4d2021-04-19 12:26:36 +02001528/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001529 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001530 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001531
1532/**
aPiecek874ea4d2021-04-19 12:26:36 +02001533 * @brief Printing one line including wrapper and node
1534 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001535 * @param[in] node is \<node\> representation.
1536 * @param[in] pck contains special printing functions callback.
1537 * @param[in] indent contains wrapper and indent in node numbers.
1538 * @param[in,out] out is output handler.
1539 */
1540static void
1541trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1542{
1543 trp_print_wrapper(indent.wrapper, out);
1544 trp_print_node(node, pck, indent.in_node, out);
1545}
1546
1547/**
aPiecek874ea4d2021-04-19 12:26:36 +02001548 * @brief Printing one line including wrapper and
1549 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001550 * @param[in] node is \<node\> representation.
1551 * @param[in] wr is wrapper for printing indentation before node.
1552 * @param[in] out is output handler.
1553 */
1554static void
1555trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1556{
1557 trp_print_wrapper(wr, out);
1558 trp_print_node_up_to_name(node, out);
1559}
1560
1561/**
aPiecek874ea4d2021-04-19 12:26:36 +02001562 * @brief Check if leafref target must be change to string 'leafref'
1563 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001564 * @param[in] node containing leafref target.
1565 * @param[in] wr is wrapper for printing indentation before node.
1566 * @param[in] mll is max line length.
1567 * @param[in] out is output handler.
1568 * @return true if leafref must be changed to string 'leafref'.
1569 */
1570static ly_bool
1571trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1572{
1573 struct ly_out_clb_arg *data;
1574
1575 if (node.type.type != TRD_TYPE_TARGET) {
1576 return 0;
1577 }
1578
1579 /* set ly_out to counting characters */
1580 data = out->method.clb.arg;
1581
1582 data->counter = 0;
1583 data->mode = TRD_CHAR_COUNT;
1584 /* count number of printed bytes */
1585 trp_print_wrapper(wr, out);
1586 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1587 trp_print_divided_node_up_to_name(node, out);
1588 data->mode = TRD_PRINT;
1589
1590 return data->counter + strlen(node.type.str) > mll;
1591}
1592
1593/**
1594 * @brief Get default indent in node based on node values.
1595 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001596 * @return Default indent in node assuming that the node
1597 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001598 */
1599static struct trt_indent_in_node
1600trp_default_indent_in_node(struct trt_node node)
1601{
1602 struct trt_indent_in_node ret;
1603
1604 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1605
1606 /* btw_name_opts */
1607 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1608
1609 /* btw_opts_type */
1610 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1611 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1612 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1613 TRD_INDENT_BEFORE_TYPE;
1614 } else {
1615 ret.btw_opts_type = 0;
1616 }
1617
1618 /* btw_type_iffeatures */
1619 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1620
1621 return ret;
1622}
1623
1624/**
1625 * @brief Setting linebreaks in trt_indent_in_node.
1626 *
1627 * The order where the linebreak tag can be placed is from the end.
1628 *
aPiecek874ea4d2021-04-19 12:26:36 +02001629 * @param[in] indent containing alignment lengths
1630 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001631 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001632 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1633 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001634 */
1635static struct trt_indent_in_node
1636trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1637{
1638 /* somewhere must be set a line break in node */
1639 struct trt_indent_in_node ret = indent;
1640
1641 /* gradually break the node from the end */
1642 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1643 ret.btw_type_iffeatures = TRD_LINEBREAK;
1644 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1645 ret.btw_opts_type = TRD_LINEBREAK;
1646 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1647 /* set line break between name and opts */
1648 ret.btw_name_opts = TRD_LINEBREAK;
1649 } else {
1650 /* it is not possible to place a more line breaks,
1651 * unfortunately the max_line_length constraint is violated
1652 */
1653 ret.type = TRD_INDENT_IN_NODE_FAILED;
1654 }
1655 return ret;
1656}
1657
1658/**
1659 * @brief Get the first half of the node based on the linebreak mark.
1660 *
1661 * Items in the second half of the node will be empty.
1662 *
1663 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001664 * @param[in] indent contains information in which part of the \<node\>
1665 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001666 * @return first half of the node, indent is unchanged.
1667 */
1668static struct trt_pair_indent_node
1669trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1670{
1671 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1672
1673 if (indent.btw_name_opts == TRD_LINEBREAK) {
1674 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1675 ret.node.type = TRP_EMPTY_TRT_TYPE;
1676 ret.node.iffeatures = 0;
1677 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1678 ret.node.type = TRP_EMPTY_TRT_TYPE;
1679 ret.node.iffeatures = 0;
1680 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1681 ret.node.iffeatures = 0;
1682 }
1683
1684 return ret;
1685}
1686
1687/**
1688 * @brief Get the second half of the node based on the linebreak mark.
1689 *
1690 * Items in the first half of the node will be empty.
1691 * Indentations belonging to the first node will be reset to zero.
1692 *
1693 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001694 * @param[in] indent contains information in which part of the \<node\>
1695 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001696 * @return second half of the node, indent is newly set.
1697 */
1698static struct trt_pair_indent_node
1699trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1700{
1701 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1702
1703 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001704 /* Logically, the information up to token <opts> should
1705 * be deleted, but the the trp_print_node function needs it to
1706 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001707 */
1708 ret.indent.btw_name_opts = 0;
1709 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1710 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1711 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1712 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1713 ret.indent.btw_name_opts = 0;
1714 ret.indent.btw_opts_type = 0;
1715 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1716 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1717 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1718 ret.node.type = TRP_EMPTY_TRT_TYPE;
1719 ret.indent.btw_name_opts = 0;
1720 ret.indent.btw_opts_type = 0;
1721 ret.indent.btw_type_iffeatures = 0;
1722 }
1723 return ret;
1724}
1725
1726/**
1727 * @brief Get the correct alignment for the node.
1728 *
aPiecek874ea4d2021-04-19 12:26:36 +02001729 * This function is recursively called itself. It's like a backend
1730 * function for a function trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001731 *
1732 * @param[in] node is \<node\> representation.
1733 * @param[in] pck contains speciall callback functions for printing.
1734 * @param[in] indent contains wrapper and indent in node numbers.
1735 * @param[in] mll is max line length.
1736 * @param[in,out] cnt counting number of characters to print.
1737 * @param[in,out] out is output handler.
1738 * @return pair of node and indentation numbers of that node.
1739 */
1740static struct trt_pair_indent_node
1741trp_try_normal_indent_in_node_(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, size_t *cnt, struct ly_out *out)
1742{
1743 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1744
1745 trp_print_line(node, pck, indent, out);
1746
1747 if (*cnt <= mll) {
1748 /* success */
1749 return ret;
1750 } else {
1751 ret.indent = trp_indent_in_node_place_break(ret.indent);
1752 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1753 /* erase information in node due to line break */
1754 ret = trp_first_half_node(node, ret.indent);
1755 /* check if line fits, recursive call */
1756 *cnt = 0;
1757 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1758 /* make sure that the result will be with the status divided
1759 * or eventually with status failed */
1760 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1761 }
1762 return ret;
1763 }
1764}
1765
1766/**
1767 * @brief Get the correct alignment for the node.
1768 *
1769 * @param[in] node is \<node\> representation.
1770 * @param[in] pck contains speciall callback functions for printing.
1771 * @param[in] indent contains wrapper and indent in node numbers.
1772 * @param[in] mll is max line length.
1773 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001774 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1775 * line, some indent variable has negative value as a line break sign.
1776 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1777 * all indent variables values has non-negative number.
1778 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1779 * line, all indent variables has negative or zero values,
1780 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001781 */
1782static struct trt_pair_indent_node
1783trp_try_normal_indent_in_node(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, struct ly_out *out)
1784{
1785 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1786 struct ly_out_clb_arg *data;
1787
1788 /* set ly_out to counting characters */
1789 data = out->method.clb.arg;
1790
1791 data->counter = 0;
1792 data->mode = TRD_CHAR_COUNT;
1793 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1794 data->mode = TRD_PRINT;
1795
1796 return ret;
1797}
1798
1799/**
aPiecek874ea4d2021-04-19 12:26:36 +02001800 * @brief Auxiliary function for trp_print_entire_node()
1801 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001802 * @param[in] node is node representation.
1803 * @param[in] ppck contains speciall callback functions for printing.
1804 * @param[in] ipck contains wrapper and indent in node numbers.
1805 * @param[in] mll is max line length.
1806 * @param[in,out] out is output handler.
1807 */
1808static void
1809trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1810{
1811 ly_bool entire_node_was_printed;
1812 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1813
1814 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1815 /* nothing can be done, continue as usual */
1816 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1817 }
1818
1819 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1820 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1821
1822 if (!entire_node_was_printed) {
1823 ly_print_(out, "\n");
1824 /* continue with second half node */
1825 ind_node = trp_second_half_node(node, ind_node.indent);
1826 /* continue with printing node */
1827 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1828 } else {
1829 return;
1830 }
1831}
1832
1833/**
aPiecek874ea4d2021-04-19 12:26:36 +02001834 * @brief Printing of the wrapper and the whole node,
1835 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001836 * @param[in] node is node representation.
1837 * @param[in] ppck contains speciall callback functions for printing.
1838 * @param[in] ipck contains wrapper and indent in node numbers.
1839 * @param[in] mll is max line length.
1840 * @param[in,out] out is output handler.
1841 */
1842static void
1843trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1844{
1845 struct trt_pair_indent_node ind_node1;
1846 struct trt_pair_indent_node ind_node2;
1847 struct trt_pck_indent tmp;
1848
1849 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1850 node.type.type = TRD_TYPE_LEAFREF;
1851 }
1852
1853 /* check if normal indent is possible */
1854 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1855
1856 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1857 /* node fits to one line */
1858 trp_print_line(node, ppck, ipck, out);
1859 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1860 /* node will be divided */
1861 /* print first half */
1862 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1863 /* pretend that this is normal node */
1864 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1865
1866 trp_print_line(ind_node1.node, ppck, tmp, out);
1867 ly_print_(out, "\n");
1868
1869 /* continue with second half on new line */
1870 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1871 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1872
1873 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1874 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1875 /* node name is too long */
1876 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1877
1878 if (trp_node_body_is_empty(node)) {
1879 return;
1880 } else {
1881 ly_print_(out, "\n");
1882
1883 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1884 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1885 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1886
1887 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1888 }
1889
1890 }
1891}
1892
aPiecek874ea4d2021-04-19 12:26:36 +02001893/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001894 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001895 *********************************************************************/
1896
1897/**
1898 * @brief Get nodetype.
1899 * @param[in] node is any lysp_node.
1900 */
1901static uint16_t
1902trop_nodetype(const void *node)
1903{
1904 return ((const struct lysp_node *)node)->nodetype;
1905}
1906
1907/**
1908 * @brief Get sibling.
1909 * @param[in] node is any lysp_node.
1910 */
1911static const void *
1912trop_next(const void *node)
1913{
1914 return ((const struct lysp_node *)node)->next;
1915}
1916
1917/**
1918 * @brief Get parent.
1919 * @param[in] node is any lysp_node.
1920 */
1921static const void *
1922trop_parent(const void *node)
1923{
1924 return ((const struct lysp_node *)node)->parent;
1925}
1926
1927/**
1928 * @brief Try to get child.
1929 * @param[in] node is any lysp_node.
1930 */
1931static const void *
1932trop_child(const void *node)
1933{
1934 return lysp_node_child(node);
1935}
1936
1937/**
1938 * @brief Try to get action.
1939 * @param[in] node is any lysp_node.
1940 */
1941static const void *
1942trop_actions(const void *node)
1943{
1944 return lysp_node_actions(node);
1945}
1946
1947/**
1948 * @brief Try to get action.
1949 * @param[in] node must be of type lysp_node_action.
1950 */
1951static const void *
1952trop_action_input(const void *node)
1953{
1954 return &((const struct lysp_node_action *)node)->input;
1955}
1956
1957/**
1958 * @brief Try to get action.
1959 * @param[in] node must be of type lysp_node_action.
1960 */
1961static const void *
1962trop_action_output(const void *node)
1963{
1964 return &((const struct lysp_node_action *)node)->output;
1965}
1966
1967/**
1968 * @brief Try to get action.
1969 * @param[in] node is any lysp_node.
1970 */
1971static const void *
1972trop_notifs(const void *node)
1973{
1974 return lysp_node_notifs(node);
1975}
1976
1977/**
1978 * @brief Fill struct tro_getters with \ref TRP_trop getters
1979 * which are adapted to lysp nodes.
1980 */
1981static struct tro_getters
1982trop_init_getters()
1983{
1984 return (struct tro_getters) {
1985 .nodetype = trop_nodetype,
1986 .next = trop_next,
1987 .parent = trop_parent,
1988 .child = trop_child,
1989 .actions = trop_actions,
1990 .action_input = trop_action_input,
1991 .action_output = trop_action_output,
1992 .notifs = trop_notifs
1993 };
1994}
1995
aPiecek3f247652021-04-19 13:40:25 +02001996/**
1997 * @brief Get nodetype.
1998 * @param[in] node is any lysc_node.
1999 */
2000static uint16_t
2001troc_nodetype(const void *node)
2002{
2003 return ((const struct lysc_node *)node)->nodetype;
2004}
2005
2006/**
2007 * @brief Get sibling.
2008 * @param[in] node is any lysc_node.
2009 */
2010static const void *
2011troc_next(const void *node)
2012{
2013 return ((const struct lysc_node *)node)->next;
2014}
2015
2016/**
2017 * @brief Get parent.
2018 * @param[in] node is any lysc_node.
2019 */
2020static const void *
2021troc_parent(const void *node)
2022{
2023 return ((const struct lysc_node *)node)->parent;
2024}
2025
2026/**
2027 * @brief Try to get child.
2028 * @param[in] node is any lysc_node.
2029 */
2030static const void *
2031troc_child(const void *node)
2032{
2033 return lysc_node_child(node);
2034}
2035
2036/**
2037 * @brief Try to get action.
2038 * @param[in] node is any lysc_node.
2039 */
2040static const void *
2041troc_actions(const void *node)
2042{
2043 return lysc_node_actions(node);
2044}
2045
2046/**
2047 * @brief Try to get action.
2048 * @param[in] node must be of type lysc_node_action.
2049 */
2050static const void *
2051troc_action_input(const void *node)
2052{
2053 return &((const struct lysc_node_action *)node)->input;
2054}
2055
2056/**
2057 * @brief Try to get action.
2058 * @param[in] node must be of type lysc_node_action.
2059 */
2060static const void *
2061troc_action_output(const void *node)
2062{
2063 return &((const struct lysc_node_action *)node)->output;
2064}
2065
2066/**
2067 * @brief Try to get action.
2068 * @param[in] node is any lysc_node.
2069 */
2070static const void *
2071troc_notifs(const void *node)
2072{
2073 return lysc_node_notifs(node);
2074}
2075
2076/**
2077 * @brief Fill struct tro_getters with \ref TRP_troc getters
2078 * which are adapted to lysc nodes.
2079 */
2080static struct tro_getters
2081troc_init_getters()
2082{
2083 return (struct tro_getters) {
2084 .nodetype = troc_nodetype,
2085 .next = troc_next,
2086 .parent = troc_parent,
2087 .child = troc_child,
2088 .actions = troc_actions,
2089 .action_input = troc_action_input,
2090 .action_output = troc_action_output,
2091 .notifs = troc_notifs
2092 };
2093}
2094
aPiecekef1e58e2021-04-19 13:19:44 +02002095/**********************************************************************
2096 * tro functions
2097 *********************************************************************/
2098
2099/**
2100 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002101 *
2102 * This is a general algorithm that is able to
2103 * work with lysp_node or lysc_node.
2104 *
2105 * @param[in] node points to lysp_node or lysc_node.
2106 * @param[in] lysc_tree flag to determine what type the @p node is.
2107 * If set to true, then @p points to lysc_node otherwise lysp_node.
2108 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002109 */
2110static const void *
aPiecek3f247652021-04-19 13:40:25 +02002111tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002112{
2113 struct tro_getters get;
2114 const void *tmp, *parent;
2115 const void *ret;
2116
2117 assert(node);
2118
aPiecek3f247652021-04-19 13:40:25 +02002119 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002120
2121 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2122 if ((tmp = get.next(node))) {
2123 /* next action exists */
2124 ret = tmp;
2125 } else if ((parent = get.parent(node))) {
2126 /* maybe if notif exists as sibling */
2127 ret = get.notifs(parent);
2128 } else {
2129 ret = NULL;
2130 }
2131 } else if (get.nodetype(node) & LYS_INPUT) {
2132 if ((parent = get.parent(node))) {
2133 /* if output action has data */
2134 if (get.child(get.action_output(parent))) {
2135 /* then next sibling is output action */
2136 ret = get.action_output(parent);
2137 } else {
2138 /* input action cannot have siblings other
2139 * than output action.
2140 */
2141 ret = NULL;
2142 }
2143 } else {
2144 /* there is no way how to get output action */
2145 ret = NULL;
2146 }
2147 } else if (get.nodetype(node) & LYS_OUTPUT) {
2148 /* output action cannot have siblings */
2149 ret = NULL;
2150 } else if (get.nodetype(node) & LYS_NOTIF) {
2151 /* must have as a sibling only notif */
2152 ret = get.next(node);
2153 } else {
2154 /* for rest of nodes */
2155 if ((tmp = get.next(node))) {
2156 /* some sibling exists */
2157 ret = tmp;
2158 } else if ((parent = get.parent(node))) {
2159 /* Action and notif are siblings too.
2160 * They can be reached through parent.
2161 */
2162 if ((tmp = get.actions(parent))) {
2163 /* next sibling is action */
2164 ret = tmp;
2165 } else if ((tmp = get.notifs(parent))) {
2166 /* next sibling is notif */
2167 ret = tmp;
2168 } else {
2169 /* sibling not exists */
2170 ret = NULL;
2171 }
2172 } else {
2173 /* sibling not exists */
2174 ret = NULL;
2175 }
2176 }
2177
2178 return ret;
2179}
2180
2181/**
2182 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002183 *
2184 * This is a general algorithm that is able to
2185 * work with lysp_node or lysc_node.
2186 *
2187 * @param[in] node points to lysp_node or lysc_node.
2188 * @param[in] lysc_tree flag to determine what type the @p node is.
2189 * If set to true, then @p points to lysc_node otherwise lysp_node.
2190 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002191 */
2192static const void *
aPiecek3f247652021-04-19 13:40:25 +02002193tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002194{
2195 struct tro_getters get;
2196 const void *tmp;
2197 const void *ret;
2198
2199 assert(node);
2200
aPiecek3f247652021-04-19 13:40:25 +02002201 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002202
2203 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2204 if (get.child(get.action_input(node))) {
2205 /* go to LYS_INPUT */
2206 ret = get.action_input(node);
2207 } else if (get.child(get.action_output(node))) {
2208 /* go to LYS_OUTPUT */
2209 ret = get.action_output(node);
2210 } else {
2211 /* input action and output action have no data */
2212 ret = NULL;
2213 }
2214 } else {
2215 if ((tmp = get.child(node))) {
2216 ret = tmp;
2217 } else {
2218 /* current node can't have children or has no children */
2219 /* but maybe has some actions or notifs */
2220 if ((tmp = get.actions(node))) {
2221 ret = tmp;
2222 } else if ((tmp = get.notifs(node))) {
2223 ret = tmp;
2224 } else {
2225 ret = NULL;
2226 }
2227 }
2228 }
2229
2230 return ret;
2231}
2232
2233/**
aPiecek3f247652021-04-19 13:40:25 +02002234 * @brief Get new trt_parent_cache if we apply the transfer
2235 * to the child node in the tree.
2236 * @param[in] ca is parent cache for current node.
2237 * @param[in] tc contains current tree node.
2238 * @return Cache for the current node.
2239 */
2240static struct trt_parent_cache
2241tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2242{
2243 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2244
2245 if (!tc->lysc_tree) {
2246 const struct lysp_node *pn = tc->pn;
2247
2248 ret.ancestor =
2249 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2250 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2251 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2252 ca.ancestor;
2253
2254 ret.lys_status =
2255 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2256 ca.lys_status;
2257
2258 ret.lys_config =
2259 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2260 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2261 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2262 ca.lys_config;
2263
2264 ret.last_list =
2265 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2266 ca.last_list;
2267 }
2268
2269 return ret;
2270}
2271
2272/**
aPiecekef1e58e2021-04-19 13:19:44 +02002273 * @brief Transformation of the Schema nodes flags to
2274 * Tree diagram \<status\>.
2275 * @param[in] flags is node's flags obtained from the tree.
2276 */
2277static trt_status_type
2278tro_flags2status(uint16_t flags)
2279{
2280 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2281 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2282 TRD_STATUS_TYPE_CURRENT;
2283}
2284
2285/**
2286 * @brief Transformation of the Schema nodes flags to Tree diagram
2287 * \<flags\> but more specifically 'ro' or 'rw'.
2288 * @param[in] flags is node's flags obtained from the tree.
2289 */
2290static trt_flags_type
2291tro_flags2config(uint16_t flags)
2292{
2293 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2294 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2295 TRD_FLAGS_TYPE_EMPTY;
2296}
2297
2298/**
aPiecek3f247652021-04-19 13:40:25 +02002299 * @brief Print current node's iffeatures.
2300 * @param[in] tc is tree context.
2301 * @param[in,out] out is output handler.
2302 */
2303static void
2304tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2305{
2306 const struct lysp_qname *iffs;
2307
2308 iffs = tc->lysc_tree ?
2309 TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures :
2310 tc->pn->iffeatures;
2311
2312 LY_ARRAY_COUNT_TYPE i;
2313
2314 LY_ARRAY_FOR(iffs, i) {
2315 if (i == 0) {
2316 ly_print_(out, "%s", iffs[i].str);
2317 } else {
2318 ly_print_(out, ",%s", iffs[i].str);
2319 }
2320 }
2321
2322}
2323
2324/**
2325 * @brief Print current list's keys.
2326 *
2327 * Well, actually printing keys in the lysp_tree is trivial,
2328 * because char* points to all keys. However, special functions have
2329 * been reserved for this, because in principle the list of elements
2330 * can have more implementations.
2331 *
2332 * @param[in] tc is tree context.
2333 * @param[in,out] out is output handler.
2334 */
2335static void
2336tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2337{
2338 const struct lysp_node_list *list;
2339
2340 list = tc->lysc_tree ?
2341 (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn) :
2342 (const struct lysp_node_list *)tc->pn;
2343 assert(list->nodetype & LYS_LIST);
2344
2345 if (trg_charptr_has_data(list->key)) {
2346 ly_print_(out, "%s", list->key);
2347 }
2348}
2349
2350/**
2351 * @brief Get rpcs section if exists.
2352 * @param[in,out] tc is tree context.
2353 * @return Section representation if it exists. The @p tc is modified
2354 * and his pointer points to the first node in rpcs section.
2355 * @return Empty section representation otherwise.
2356 */
2357static struct trt_keyword_stmt
2358tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2359{
2360 assert(tc && tc->module);
2361 const void *actions;
2362
2363 if (tc->lysc_tree) {
2364 actions = tc->module->compiled->rpcs;
2365 if (actions) {
2366 tc->cn = actions;
2367 }
2368 } else {
2369 actions = tc->module->parsed->rpcs;
2370 if (actions) {
2371 tc->pn = actions;
2372 tc->tpn = tc->pn;
2373 }
2374 }
2375
2376 if (actions) {
2377 tc->section = TRD_SECT_RPCS;
2378 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2379 } else {
2380 return TRP_EMPTY_KEYWORD_STMT;
2381 }
2382}
2383
2384/**
2385 * @brief Get notification section if exists
2386 * @param[in,out] tc is tree context.
2387 * @return Section representation if it exists.
2388 * The @p tc is modified and his pointer points to the
2389 * first node in notification section.
2390 * @return Empty section representation otherwise.
2391 */
2392static struct trt_keyword_stmt
2393tro_modi_get_notifications(struct trt_tree_ctx *tc)
2394{
2395 assert(tc && tc->module);
2396 const void *notifs;
2397
2398 if (tc->lysc_tree) {
2399 notifs = tc->module->compiled->notifs;
2400 if (notifs) {
2401 tc->cn = notifs;
2402 }
2403 } else {
2404 notifs = tc->module->parsed->notifs;
2405 if (notifs) {
2406 tc->pn = notifs;
2407 tc->tpn = tc->pn;
2408 }
2409 }
2410
2411 if (notifs) {
2412 tc->section = TRD_SECT_NOTIF;
2413 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2414 } else {
2415 return TRP_EMPTY_KEYWORD_STMT;
2416 }
2417}
2418
2419/**
aPiecekef1e58e2021-04-19 13:19:44 +02002420 * @brief Get next yang-data section if exists.
2421 *
2422 * Not implemented.
2423 *
2424 * @param[in,out] tc is tree context.
2425 * @return Section representation if it exists.
2426 * @return Empty section representation otherwise.
2427 */
2428static struct trt_keyword_stmt
2429tro_modi_next_yang_data(struct trt_tree_ctx *tc)
2430{
2431 tc->section = TRD_SECT_YANG_DATA;
2432 /* TODO: yang-data is not supported */
2433 return TRP_EMPTY_KEYWORD_STMT;
2434}
2435
2436/**
2437 * @brief Get name of the module.
2438 * @param[in] tc is context of the tree.
2439 */
2440static struct trt_keyword_stmt
2441tro_read_module_name(const struct trt_tree_ctx *tc)
2442{
2443 assert(tc && tc->module && tc->module->name);
2444 return (struct trt_keyword_stmt) {
2445 .type = TRD_KEYWORD_MODULE, .str = tc->module->name
2446 };
2447}
2448
2449/**********************************************************************
2450 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002451 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002452
2453/**
aPiecek61d062b2020-11-02 11:05:09 +01002454 * @brief Check if list statement has keys.
2455 * @param[in] pn is pointer to the list.
2456 * @return 1 if has keys, otherwise 0.
2457 */
2458static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002459trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002460{
aPiecekef1e58e2021-04-19 13:19:44 +02002461 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002462}
2463
2464/**
2465 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002466 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002467 * @return 1 if has if-features, otherwise 0.
2468 */
2469static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002470trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002471{
2472 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002473 const struct lysp_qname *iffs;
2474
aPiecek61d062b2020-11-02 11:05:09 +01002475 ly_bool ret = 0;
2476
aPiecekef1e58e2021-04-19 13:19:44 +02002477 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002478 LY_ARRAY_FOR(iffs, u) {
2479 ret = 1;
2480 break;
2481 }
2482 return ret;
2483}
2484
2485/**
2486 * @brief Find out if leaf is also the key in last list.
2487 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002488 * @param[in] ca_last_list is pointer to last visited list.
2489 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002490 * @return 1 if leaf is also the key, otherwise 0.
2491 */
2492static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002493trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002494{
2495 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2496 const struct lysp_node_list *list = ca_last_list;
2497
2498 if (!list) {
2499 return 0;
2500 }
2501 return trg_charptr_has_data(list->key) ?
2502 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2503}
2504
2505/**
2506 * @brief Check if container's type is presence.
2507 * @param[in] pn is pointer to container.
2508 * @return 1 if container has presence statement, otherwise 0.
2509 */
2510static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002511trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002512{
2513 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2514}
2515
2516/**
2517 * @brief Get leaflist's path without lysp_node type control.
2518 * @param[in] pn is pointer to the leaflist.
2519 */
2520static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002521trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002522{
2523 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2524
2525 return list->type.path ? list->type.path->expr : NULL;
2526}
2527
2528/**
2529 * @brief Get leaflist's type name without lysp_node type control.
2530 * @param[in] pn is pointer to the leaflist.
2531 */
2532static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002533trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002534{
2535 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2536
2537 return list->type.name;
2538}
2539
2540/**
2541 * @brief Get leaf's path without lysp_node type control.
2542 * @param[in] pn is pointer to the leaf node.
2543 */
2544static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002545trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002546{
2547 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2548
2549 return leaf->type.path ? leaf->type.path->expr : NULL;
2550}
2551
2552/**
2553 * @brief Get leaf's type name without lysp_node type control.
2554 * @param[in] pn is pointer to the leaf's type name.
2555 */
2556static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002557trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002558{
2559 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2560
2561 return leaf->type.name;
2562}
2563
2564/**
aPiecek874ea4d2021-04-19 12:26:36 +02002565 * @brief Get pointer to data using node type specification
2566 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002567 *
aPiecek874ea4d2021-04-19 12:26:36 +02002568 * @param[in] flags is node type specification.
2569 * If it is the correct node, the getter function is called.
2570 * @param[in] f is getter function which provides the desired
2571 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002572 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002573 * @return NULL if node has wrong type or getter function return
2574 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002575 * @return Pointer to desired char pointer obtained from the node.
2576 */
2577static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002578trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002579{
2580 if (pn->nodetype & flags) {
2581 const char *ret = f(pn);
2582 return trg_charptr_has_data(ret) ? ret : NULL;
2583 } else {
2584 return NULL;
2585 }
2586}
2587
2588/**
aPiecek61d062b2020-11-02 11:05:09 +01002589 * @brief Resolve \<status\> of the current node.
2590 * @param[in] nodetype is node's type obtained from the tree.
2591 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002592 * @param[in] ca_lys_status is inherited status
2593 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002594 * @return The status type.
2595 */
2596static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002597trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002598{
2599 /* LYS_INPUT and LYS_OUTPUT is special case */
2600 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002601 return tro_flags2status(ca_lys_status);
2602 /* if ancestor's status is deprc or obslt
2603 * and also node's status is not set
2604 */
aPiecek61d062b2020-11-02 11:05:09 +01002605 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2606 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002607 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002608 } else {
2609 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002610 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002611 }
2612}
2613
2614/**
2615 * @brief Resolve \<flags\> of the current node.
2616 * @param[in] nodetype is node's type obtained from the tree.
2617 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002618 * @param[in] ca_ancestor is ancestor type obtained
2619 * from trt_parent_cache.
2620 * @param[in] ca_lys_config is inherited config item
2621 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002622 * @return The flags type.
2623 */
2624static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002625trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002626{
2627 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2628 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2629 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2630 return TRD_FLAGS_TYPE_RO;
2631 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2632 return TRD_FLAGS_TYPE_RO;
2633 } else if (nodetype & LYS_NOTIF) {
2634 return TRD_FLAGS_TYPE_NOTIF;
2635 } else if (nodetype & LYS_USES) {
2636 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2637 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2638 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002639 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002640 /* config is not set. Look at ancestor's config */
2641 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002642 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002643 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002644 }
2645}
2646
2647/**
2648 * @brief Resolve node type of the current node.
2649 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002650 * @param[in] ca_last_list is pointer to the last visited list.
2651 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002652 */
2653static trt_node_type
aPiecekef1e58e2021-04-19 13:19:44 +02002654trop_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002655{
2656 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2657 return TRD_NODE_ELSE;
2658 } else if (pn->nodetype & LYS_CASE) {
2659 return TRD_NODE_CASE;
2660 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2661 return TRD_NODE_OPTIONAL_CHOICE;
2662 } else if (pn->nodetype & LYS_CHOICE) {
2663 return TRD_NODE_CHOICE;
aPiecekef1e58e2021-04-19 13:19:44 +02002664 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002665 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002666 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002667 return TRD_NODE_KEYS;
2668 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2669 return TRD_NODE_LISTLEAFLIST;
2670 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2671 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002672 } 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 +01002673 return TRD_NODE_OPTIONAL;
2674 } else {
2675 return TRD_NODE_ELSE;
2676 }
2677}
2678
2679/**
aPiecekef1e58e2021-04-19 13:19:44 +02002680 * @brief Resolve \<type\> of the current node.
2681 * @param[in] pn is current node.
2682 */
2683static struct trt_type
2684trop_resolve_type(const struct lysp_node *pn)
2685{
2686 const char *tmp = NULL;
2687
2688 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2689 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2690 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2691 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2692 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2693 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2694 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2695 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2696 } else if (pn->nodetype == LYS_ANYDATA) {
2697 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2698 } else if (pn->nodetype & LYS_ANYXML) {
2699 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2700 } else {
2701 return TRP_EMPTY_TRT_TYPE;
2702 }
2703}
2704
2705/**
aPiecek61d062b2020-11-02 11:05:09 +01002706 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002707 * @param[in] ca contains stored important data
2708 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002709 * @param[in] tc is context of the tree.
2710 */
2711static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002712trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002713{
aPiecekef1e58e2021-04-19 13:19:44 +02002714 const struct lysp_node *pn;
2715 struct trt_node ret;
2716
aPiecek61d062b2020-11-02 11:05:09 +01002717 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002718
2719 pn = tc->pn;
2720 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002721
2722 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002723 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002724
2725 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2726 /* <flags> */
aPiecekef1e58e2021-04-19 13:19:44 +02002727 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002728
2729 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2730 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2731 /* set type of the node */
aPiecekef1e58e2021-04-19 13:19:44 +02002732 ret.name.type = trop_resolve_node_type(pn, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002733
2734 /* TODO: ret.name.module_prefix is not supported right now. */
2735 ret.name.module_prefix = NULL;
2736
2737 /* set node's name */
2738 ret.name.str = pn->name;
2739
2740 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002741 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002742
2743 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002744 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002745
aPiecek3f247652021-04-19 13:40:25 +02002746 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002747
2748 return ret;
2749}
2750
aPiecekef1e58e2021-04-19 13:19:44 +02002751/**
2752 * @brief Find out if the current node has siblings.
2753 * @param[in] tc is context of the tree.
2754 * @return 1 if sibling exists otherwise 0.
2755 */
2756static ly_bool
2757trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2758{
aPiecek3f247652021-04-19 13:40:25 +02002759 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002760}
2761
aPiecek874ea4d2021-04-19 12:26:36 +02002762/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002763 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002764 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002765
2766/**
aPiecek874ea4d2021-04-19 12:26:36 +02002767 * @brief Change current node pointer to its parent
2768 * but only if parent exists.
2769 * @param[in,out] tc is tree context.
2770 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002771 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002772 * @return 0 if the node did not have parents.
2773 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002774 */
2775static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002776trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002777{
2778 assert(tc && tc->pn);
2779 /* If no parent exists, stay in actual node. */
2780 if (tc->pn != tc->tpn) {
2781 tc->pn = tc->pn->parent;
2782 return 1;
2783 } else {
2784 return 0;
2785 }
2786}
2787
2788/**
aPiecek874ea4d2021-04-19 12:26:36 +02002789 * @brief Change the current node pointer to its child
2790 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002791 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002792 * @param[in,out] tc is context of the tree.
2793 * Contains pointer to the current node.
2794 * @return Non-empty \<node\> representation of the current
2795 * node's child. The @p tc is modified.
2796 * @return Empty \<node\> representation if child don't exists.
2797 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002798 */
2799static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002800trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002801{
aPiecekef1e58e2021-04-19 13:19:44 +02002802 const struct lysp_node *tmp;
2803
aPiecek61d062b2020-11-02 11:05:09 +01002804 assert(tc && tc->pn);
2805
aPiecek3f247652021-04-19 13:40:25 +02002806 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002807 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02002808 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01002809 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002810 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002811 }
2812}
2813
2814/**
aPiecek874ea4d2021-04-19 12:26:36 +02002815 * @brief Change the current node pointer to the first child of node's
2816 * parent. If current node is already first sibling/child then nothing
2817 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01002818 * @param[in,out] tc is tree context.
2819 */
2820static void
aPiecekef1e58e2021-04-19 13:19:44 +02002821trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002822{
aPiecekef1e58e2021-04-19 13:19:44 +02002823 assert(tc && tc->pn);
aPiecek61d062b2020-11-02 11:05:09 +01002824
aPiecekef1e58e2021-04-19 13:19:44 +02002825 if (trop_modi_parent(tc)) {
2826 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002827 } else {
2828 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01002829 struct lysp_module *pm = tc->module->parsed;
2830
2831 switch (tc->section) {
2832 case TRD_SECT_MODULE:
2833 tc->pn = pm->data;
2834 break;
2835 case TRD_SECT_AUGMENT:
aPiecekdc8fd572021-04-19 10:47:23 +02002836 tc->pn = (const struct lysp_node *)pm->augments;
aPiecek61d062b2020-11-02 11:05:09 +01002837 break;
2838 case TRD_SECT_RPCS:
2839 tc->pn = (const struct lysp_node *)pm->rpcs;
2840 break;
2841 case TRD_SECT_NOTIF:
2842 tc->pn = (const struct lysp_node *)pm->notifs;
2843 break;
2844 case TRD_SECT_GROUPING:
aPiecekdc8fd572021-04-19 10:47:23 +02002845 tc->pn = (const struct lysp_node *)pm->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01002846 break;
2847 case TRD_SECT_YANG_DATA:
2848 /*TODO: yang-data is not supported now */
2849 break;
2850 }
2851
2852 /* update pointer to top-node */
2853 tc->tpn = tc->pn;
2854 }
2855}
2856
2857/**
aPiecek874ea4d2021-04-19 12:26:36 +02002858 * @brief Change the pointer to the current node to its next sibling
2859 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002860 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002861 * @param[in,out] tc is tree context.
2862 * Contains pointer to the current node.
2863 * @return Non-empty \<node\> representation if sibling exists.
2864 * The @p tc is modified.
2865 * @return Empty \<node\> representation otherwise.
2866 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002867 */
2868static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002869trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002870{
aPiecekef1e58e2021-04-19 13:19:44 +02002871 const struct lysp_node *pn;
2872 const struct lysp_node *tpn;
2873
2874 assert(tc && tc->pn);
2875
aPiecek3f247652021-04-19 13:40:25 +02002876 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecekef1e58e2021-04-19 13:19:44 +02002877 tpn = tc->tpn == tc->pn ? pn : tc->tpn;
aPiecek61d062b2020-11-02 11:05:09 +01002878
2879 /* if next sibling exists */
aPiecekef1e58e2021-04-19 13:19:44 +02002880 if (pn) {
aPiecek61d062b2020-11-02 11:05:09 +01002881 /* update trt_tree_ctx */
aPiecekef1e58e2021-04-19 13:19:44 +02002882 tc->pn = pn;
2883 tc->tpn = tpn;
2884 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002885 } else {
2886 return TRP_EMPTY_NODE;
2887 }
2888}
2889
2890/**
2891 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02002892 * @param[in,out] tc is tree context. It is modified and his current
2893 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01002894 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01002895 * @return Empty section structure otherwise.
2896 */
2897static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02002898trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002899{
2900 assert(tc && tc->module && tc->module->parsed);
2901 const struct lysp_node_augment *augs;
2902
2903 /* if next_augment func was called for the first time */
2904 if (tc->section != TRD_SECT_AUGMENT) {
2905 tc->section = TRD_SECT_AUGMENT;
2906 augs = tc->module->parsed->augments;
2907 } else {
2908 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02002909 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01002910 }
2911
aPiecekdc8fd572021-04-19 10:47:23 +02002912 if (augs) {
2913 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01002914 tc->tpn = tc->pn;
2915 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
2916 } else {
2917 return TRP_EMPTY_KEYWORD_STMT;
2918 }
2919}
2920
2921/**
aPiecek61d062b2020-11-02 11:05:09 +01002922 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02002923 * @param[in,out] tc is tree context. It is modified and his current
2924 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01002925 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01002926 * @return Empty section representation otherwise.
2927 */
2928static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02002929trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002930{
2931 assert(tc && tc->module && tc->module->parsed);
2932 const struct lysp_node_grp *grps;
2933
2934 if (tc->section != TRD_SECT_GROUPING) {
2935 tc->section = TRD_SECT_GROUPING;
2936 grps = tc->module->parsed->groupings;
2937 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02002938 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01002939 }
2940
aPiecekdc8fd572021-04-19 10:47:23 +02002941 if (grps) {
2942 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01002943 tc->tpn = tc->pn;
2944 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
2945 } else {
2946 return TRP_EMPTY_KEYWORD_STMT;
2947 }
2948}
2949
aPiecek874ea4d2021-04-19 12:26:36 +02002950/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02002951 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002952 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002953
2954/**
aPiecek3f247652021-04-19 13:40:25 +02002955 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01002956 */
aPiecek3f247652021-04-19 13:40:25 +02002957static ly_bool
2958troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002959{
aPiecek3f247652021-04-19 13:40:25 +02002960 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
2961}
aPiecek61d062b2020-11-02 11:05:09 +01002962
aPiecek3f247652021-04-19 13:40:25 +02002963/**
2964 * @brief Resolve \<flags\> of the current node.
2965 *
2966 * Use this function only if trt_tree_ctx.lysc_tree is true.
2967 *
2968 * @param[in] nodetype is current lysc_node.nodetype.
2969 * @param[in] flags is current lysc_node.flags.
2970 * @return The flags type.
2971 */
2972static trt_flags_type
2973troc_resolve_flags(uint16_t nodetype, uint16_t flags)
2974{
2975 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
2976 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2977 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
2978 return TRD_FLAGS_TYPE_RO;
2979 } else if (nodetype & LYS_IS_NOTIF) {
2980 return TRD_FLAGS_TYPE_RO;
2981 } else if (nodetype & LYS_NOTIF) {
2982 return TRD_FLAGS_TYPE_NOTIF;
2983 } else if (nodetype & LYS_USES) {
2984 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2985 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2986 return TRD_FLAGS_TYPE_RPC;
2987 } else {
2988 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002989 }
aPiecek61d062b2020-11-02 11:05:09 +01002990}
2991
2992/**
aPiecek3f247652021-04-19 13:40:25 +02002993 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002994 *
aPiecek3f247652021-04-19 13:40:25 +02002995 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01002996 *
aPiecek3f247652021-04-19 13:40:25 +02002997 * @param[in] nodetype is current lysc_node.nodetype.
2998 * @param[in] flags is current lysc_node.flags.
2999 */
3000static trt_node_type
3001troc_resolve_node_type(uint16_t nodetype, uint16_t flags)
3002{
3003 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3004 return TRD_NODE_ELSE;
3005 } else if (nodetype & LYS_CASE) {
3006 return TRD_NODE_CASE;
3007 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3008 return TRD_NODE_OPTIONAL_CHOICE;
3009 } else if (nodetype & LYS_CHOICE) {
3010 return TRD_NODE_CHOICE;
3011 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3012 return TRD_NODE_CONTAINER;
3013 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3014 return TRD_NODE_KEYS;
3015 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3016 return TRD_NODE_LISTLEAFLIST;
3017 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3018 return TRD_NODE_OPTIONAL;
3019 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3020 return TRD_NODE_OPTIONAL;
3021 } else {
3022 return TRD_NODE_ELSE;
3023 }
3024}
3025
3026/**
3027 * @brief Transformation of current lysc_node to struct trt_node.
3028 * @param[in] ca is not used.
3029 * @param[in] tc is context of the tree.
3030 */
3031static struct trt_node
3032troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3033{
3034 (void) ca;
3035 const struct lysc_node *cn;
3036 struct trt_node ret;
3037
3038 assert(tc && tc->cn);
3039
3040 cn = tc->cn;
3041 ret = TRP_EMPTY_NODE;
3042
3043 /* <status> */
3044 ret.status = tro_flags2status(cn->flags);
3045
3046 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
3047 /* <flags> */
3048 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3049
3050 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
3051 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
3052 /* set type of the node */
3053 ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
3054
3055 /* TODO: ret.name.module_prefix is not supported right now. */
3056 ret.name.module_prefix = NULL;
3057
3058 /* set node's name */
3059 ret.name.str = cn->name;
3060
3061 /* <type> */
3062 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3063
3064 /* <iffeature> */
3065 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3066
3067 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3068
3069 return ret;
3070}
3071
3072/**********************************************************************
3073 * Modify troc getters
3074 *********************************************************************/
3075
3076/**
3077 * @copydoc trop_modi_parent()
3078 */
3079static ly_bool
3080troc_modi_parent(struct trt_tree_ctx *tc)
3081{
3082 assert(tc && tc->cn);
3083 /* If no parent exists, stay in actual node. */
3084 if (tc->cn->parent) {
3085 tc->cn = tc->cn->parent;
3086 return 1;
3087 } else {
3088 return 0;
3089 }
3090}
3091
3092/**
3093 * @copydoc trop_modi_next_sibling()
3094 */
3095static struct trt_node
3096troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3097{
3098 const struct lysc_node *cn;
3099
3100 assert(tc && tc->cn);
3101
3102 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3103
3104 /* if next sibling exists */
3105 if (cn) {
3106 /* update trt_tree_ctx */
3107 tc->cn = cn;
3108 return troc_read_node(ca, tc);
3109 } else {
3110 return TRP_EMPTY_NODE;
3111 }
3112}
3113
3114/**
3115 * @copydoc trop_modi_next_child()
3116 */
3117static struct trt_node
3118troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3119{
3120 const struct lysc_node *tmp;
3121
3122 assert(tc && tc->cn);
3123
3124 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3125 tc->cn = tmp;
3126 return troc_read_node(ca, tc);
3127 } else {
3128 return TRP_EMPTY_NODE;
3129 }
3130}
3131
3132/**
3133 * @copydoc trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003134 */
3135static void
aPiecek3f247652021-04-19 13:40:25 +02003136troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003137{
aPiecek3f247652021-04-19 13:40:25 +02003138 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003139
aPiecek3f247652021-04-19 13:40:25 +02003140 if (troc_modi_parent(tc)) {
3141 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3142 } else {
3143 /* current node is top-node */
3144 struct lysc_module *cm = tc->module->compiled;
aPiecek61d062b2020-11-02 11:05:09 +01003145
aPiecek3f247652021-04-19 13:40:25 +02003146 switch (tc->section) {
3147 case TRD_SECT_MODULE:
3148 tc->cn = cm->data;
3149 break;
3150 case TRD_SECT_RPCS:
3151 tc->cn = (const struct lysc_node *)cm->rpcs;
3152 break;
3153 case TRD_SECT_NOTIF:
3154 tc->cn = (const struct lysc_node *)cm->notifs;
3155 break;
3156 case TRD_SECT_YANG_DATA:
3157 /*TODO: yang-data is not supported now */
3158 break;
3159 default:
3160 assert(0);
3161 }
aPiecek61d062b2020-11-02 11:05:09 +01003162 }
3163}
3164
aPiecek874ea4d2021-04-19 12:26:36 +02003165/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003166 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003167 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003168
3169/**
3170 * @brief Get size of node name.
3171 * @param[in] name contains name and mark.
3172 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003173 * @return negative value as an indication that option mark
3174 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003175 */
3176static int32_t
3177trb_strlen_of_name_and_mark(struct trt_node_name name)
3178{
3179 size_t name_len = strlen(name.str);
3180
3181 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3182 /* counting also parentheses */
3183 name_len += 2;
3184 }
3185
3186 return trp_mark_is_used(name) ?
3187 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3188 (int32_t)name_len;
3189}
3190
3191/**
aPiecek874ea4d2021-04-19 12:26:36 +02003192 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3193 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003194 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003195 * @param[in] max_len4all is the maximum value of btw_opts_type
3196 * that it can have.
3197 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003198 */
3199static int16_t
3200trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3201{
3202 int32_t name_len;
3203 int16_t min_len;
3204 int16_t ret;
3205
3206 name_len = trb_strlen_of_name_and_mark(name);
3207
3208 /* negative value indicate that in name is some opt mark */
3209 min_len = name_len < 0 ?
3210 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3211 TRD_INDENT_BEFORE_TYPE;
3212 ret = abs(max_len4all) - abs(name_len);
3213
3214 /* correction -> negative indicate that name is too long. */
3215 return ret < 0 ? min_len : ret;
3216}
3217
3218/**
3219 * @brief Print node.
3220 *
aPiecek874ea4d2021-04-19 12:26:36 +02003221 * This function is wrapper for trp_print_entire_node().
3222 * But difference is that take @p max_gap_before_type which will be
3223 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003224 *
3225 * @param[in] max_gap_before_type is number of indent before \<type\>.
3226 * @param[in] wr is wrapper for printing indentation before node.
3227 * @param[in] ca contains inherited data from ancestors.
3228 * @param[in] pc contains mainly functions for printing.
3229 * @param[in] tc is tree context.
3230 */
3231static void
3232trb_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)
3233{
3234 struct trt_node node = pc->fp.read.node(ca, tc);
3235 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3236
3237 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3238 /* print actual node with unified indent */
3239 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3240 }
3241 /* after -> print actual node with default indent */
3242 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3243 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
3244}
3245
3246/**
aPiecek874ea4d2021-04-19 12:26:36 +02003247 * @brief Check if parent of the current node is the last
3248 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003249 *
aPiecek874ea4d2021-04-19 12:26:36 +02003250 * To mantain stability use this function only if the current node is
3251 * the first of the siblings.
3252 * Side-effect -> current node is set to the first sibling
3253 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003254 *
aPiecek874ea4d2021-04-19 12:26:36 +02003255 * @param[in] fp contains all \ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003256 * @param[in,out] tc is tree context.
3257 * @return 1 if parent is last sibling otherwise 0.
3258 */
3259static ly_bool
3260trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3261{
3262 if (fp.modify.parent(tc)) {
3263 ly_bool ret = fp.read.if_sibling_exists(tc);
3264 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3265 return !ret;
3266 } else {
3267 return !fp.read.if_sibling_exists(tc);
3268 }
3269}
3270
3271/**
3272 * @brief Find sibling with the biggest node name and return that size.
3273 *
3274 * Side-effect -> Current node is set to the first sibling.
3275 *
3276 * @param[in] ca contains inherited data from ancestors.
3277 * @param[in] pc contains mainly functions for printing.
3278 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003279 * @return positive number as a sign that only the node name is
3280 * included in the size.
3281 * @return negative number sign that node name and his opt mark is
3282 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003283 */
3284static int32_t
3285trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3286{
3287 int32_t ret = 0;
3288
3289 pc->fp.modify.first_sibling(tc);
3290
3291 for (struct trt_node node = pc->fp.read.node(ca, tc);
3292 !trp_node_is_empty(node);
3293 node = pc->fp.modify.next_sibling(ca, tc)) {
3294 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
3295 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3296 }
3297 pc->fp.modify.first_sibling(tc);
3298 return ret;
3299}
3300
3301/**
aPiecek874ea4d2021-04-19 12:26:36 +02003302 * @brief Find maximal indent between
3303 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003304 *
3305 * Side-effect -> Current node is set to the first sibling.
3306 *
3307 * @param[in] ca contains inherited data from ancestors.
3308 * @param[in] pc contains mainly functions for printing.
3309 * @param[in,out] tc is tree context.
3310 * @return max btw_opts_type value for rest of the siblings
3311 */
3312static int16_t
3313trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3314{
3315 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3316 int16_t ind_before_type = maxlen_node_name < 0 ?
3317 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3318 TRD_INDENT_BEFORE_TYPE;
3319
3320 return abs(maxlen_node_name) + ind_before_type;
3321}
3322
3323/**
aPiecek874ea4d2021-04-19 12:26:36 +02003324 * @brief Find out if it is possible to unify
3325 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003326 *
aPiecek874ea4d2021-04-19 12:26:36 +02003327 * The goal is for all node siblings to have the same alignment
3328 * for \<type\> as if they were in a column. All siblings who cannot
3329 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003330 * Side-effect -> Current node is set to the first sibling.
3331 *
3332 * @param[in] ca contains inherited data from ancestors.
3333 * @param[in] pc contains mainly functions for printing.
3334 * @param[in,out] tc is tree context.
3335 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003336 * @return positive number indicating the maximum number of spaces
3337 * before \<type\> if the length of the node name is 0. To calculate
3338 * the trt_indent_in_node.btw_opts_type indent size for a particular
3339 * node, use the trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003340*/
3341static uint32_t
3342trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3343{
3344 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3345}
3346
3347/**
aPiecek874ea4d2021-04-19 12:26:36 +02003348 * @brief For the current node: recursively print all of its child
3349 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003350 *
aPiecek874ea4d2021-04-19 12:26:36 +02003351 * This function is an auxiliary function for trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003352 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003353 * Nodes are printed, including unified sibling node alignment
3354 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003355 * Side-effect -> current node is set to the last sibling.
3356 *
3357 * @param[in] wr is wrapper for printing identation before node.
3358 * @param[in] ca contains inherited data from ancestors.
3359 * @param[in] pc contains mainly functions for printing.
3360 * @param[in,out] tc is tree context.
3361 */
3362static void
3363trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3364{
3365 uint32_t max_gap_before_type;
3366 ly_bool sibling_flag = 0;
3367 ly_bool child_flag = 0;
3368
3369 /* if node is last sibling, then do not add '|' to wrapper */
3370 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3371 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3372
3373 /* try unified indentation in node */
3374 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3375
3376 /* print all siblings */
3377 do {
3378 struct trt_parent_cache new_ca;
3379 struct trt_node node;
3380 /* print linebreak before printing actual node */
3381 ly_print_(pc->out, "\n");
3382 /* print node */
3383 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
3384
aPiecek3f247652021-04-19 13:40:25 +02003385 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003386 /* go to the actual node's child or stay in actual node */
3387 node = pc->fp.modify.next_child(ca, tc);
3388 child_flag = !trp_node_is_empty(node);
3389
3390 if (child_flag) {
3391 /* print all childs - recursive call */
3392 trb_print_nodes(wr, new_ca, pc, tc);
3393 /* get back from child node to actual node */
3394 pc->fp.modify.parent(tc);
3395 }
3396
3397 /* go to the actual node's sibling */
3398 node = pc->fp.modify.next_sibling(ca, tc);
3399 sibling_flag = !trp_node_is_empty(node);
3400
3401 /* go to the next sibling or stay in actual node */
3402 } while (sibling_flag);
3403}
3404
3405/**
aPiecekdc8fd572021-04-19 10:47:23 +02003406 * @brief Get address of the current node.
3407 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003408 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003409 */
3410static const void *
3411trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3412{
aPiecek3f247652021-04-19 13:40:25 +02003413 return tc->lysc_tree ?
3414 (const void *)tc->cn :
3415 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003416}
3417
3418/**
3419 * @brief Get address of current node's child.
3420 * @param[in,out] tc contains current node.
3421 */
3422static const void *
3423trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3424{
3425 if (!trb_tree_ctx_get_node(tc)) {
3426 return NULL;
3427 }
3428
aPiecek3f247652021-04-19 13:40:25 +02003429 if (tc->lysc_tree) {
3430 return lysc_node_child(tc->cn);
3431 } else {
3432 return lysp_node_child(tc->pn);
3433 }
aPiecekdc8fd572021-04-19 10:47:23 +02003434}
3435
3436/**
3437 * @brief Set current node on its child.
3438 * @param[in,out] tc contains current node.
3439 */
3440static void
3441trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3442{
aPiecek3f247652021-04-19 13:40:25 +02003443 const void *node = trb_tree_ctx_get_child(tc);
3444
3445 if (tc->lysc_tree) {
3446 tc->cn = node;
3447 } else {
3448 tc->pn = node;
3449 }
aPiecekdc8fd572021-04-19 10:47:23 +02003450}
3451
3452/**
aPiecek61d062b2020-11-02 11:05:09 +01003453 * @brief Print subtree of nodes.
3454 *
3455 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003456 * Before root node is no linebreak printing. This must be addressed by
3457 * the caller. Root node will also be printed. Behind last printed node
3458 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003459 *
aPiecek874ea4d2021-04-19 12:26:36 +02003460 * @param[in] max_gap_before_type is result from
3461 * trb_try_unified_indent() function for root node. Set parameter to 0
3462 * if distance does not matter.
3463 * @param[in] wr is wrapper saying how deep in the whole tree
3464 * is the root of the subtree.
3465 * @param[in] ca is parent_cache from root's parent.
3466 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
3467 * @param[in] pc is \ref TRP_trp settings.
3468 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003469 */
3470static void
3471trb_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)
3472{
3473 struct trt_parent_cache new_ca;
3474 struct trt_node node;
3475
aPiecekdc8fd572021-04-19 10:47:23 +02003476 if (!trb_tree_ctx_get_node(tc)) {
3477 return;
3478 }
3479
aPiecek61d062b2020-11-02 11:05:09 +01003480 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
3481 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003482 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003483 node = pc->fp.modify.next_child(ca, tc);
3484
3485 if (!trp_node_is_empty(node)) {
3486 /* print root's nodes */
3487 trb_print_nodes(wr, new_ca, pc, tc);
3488 /* get back from child node to actual node */
3489 pc->fp.modify.parent(tc);
3490 }
3491}
3492
3493/**
3494 * @brief Get number of siblings.
3495 *
3496 * Side-effect -> current node is set to the first sibling.
3497 *
3498 * @param[in] fp contains callback functions which modify tree context
3499 * @param[in,out] tc is the tree context.
3500 * @return Number of siblings of the current node.
3501 */
3502static uint32_t
3503trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3504{
3505 uint32_t ret = 1;
3506 struct trt_node node = TRP_EMPTY_NODE;
3507
3508 /* including actual node */
3509 fp.first_sibling(tc);
3510 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3511 ret++;
3512 }
3513 fp.first_sibling(tc);
3514 return ret;
3515}
3516
3517/**
3518 * @brief Print all parents and their children.
3519 *
aPiecek874ea4d2021-04-19 12:26:36 +02003520 * This function is suitable for printing top-level nodes that
aPiecek3f247652021-04-19 13:40:25 +02003521 * do not have ancestors. Function call trb_print_subtree_nodes() for all
3522 * top-level siblings. Use this function after 'module' keyword or
3523 * 'augment' and so.
aPiecek61d062b2020-11-02 11:05:09 +01003524 *
3525 * @param[in] wr_t is type of the wrapper.
3526 * @param[pc] pc contains mainly functions for printing.
3527 * @param[in,out] tc is tree context.
3528 */
3529static void
3530trb_print_family_tree(trd_wrapper_type wr_t, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3531{
3532 struct trt_wrapper wr;
3533 struct trt_parent_cache ca;
3534 uint32_t total_parents;
3535 uint32_t max_gap_before_type;
3536
aPiecekdc8fd572021-04-19 10:47:23 +02003537 if (!trb_tree_ctx_get_node(tc)) {
3538 return;
3539 }
3540
aPiecek61d062b2020-11-02 11:05:09 +01003541 wr = wr_t == TRD_WRAPPER_TOP ? TRP_INIT_WRAPPER_TOP : TRP_INIT_WRAPPER_BODY;
3542 ca = TRP_EMPTY_PARENT_CACHE;
3543 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3544 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3545
aPiecek3f247652021-04-19 13:40:25 +02003546 if (!tc->lysc_tree) {
3547 if ((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) {
3548 ca.lys_config = 0x0;
3549 }
aPiecekdc8fd572021-04-19 10:47:23 +02003550 }
3551
aPiecek61d062b2020-11-02 11:05:09 +01003552 for (uint32_t i = 0; i < total_parents; i++) {
3553 ly_print_(pc->out, "\n");
3554 trb_print_subtree_nodes(max_gap_before_type, wr, ca, pc, tc);
3555 pc->fp.modify.next_sibling(ca, tc);
3556 }
3557}
3558
aPiecek874ea4d2021-04-19 12:26:36 +02003559/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003560 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003561 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003562
3563/**
aPiecekdc8fd572021-04-19 10:47:23 +02003564 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003565 *
aPiecekdc8fd572021-04-19 10:47:23 +02003566 * @param[in] module YANG schema tree structure representing
3567 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003568 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003569 * @param[in] max_line_length is the maximum line length limit
3570 * that should not be exceeded.
3571 * @param[in,out] pc will be adapted to lysp_tree.
3572 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003573 */
3574static void
aPiecekdc8fd572021-04-19 10:47:23 +02003575trm_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 +01003576{
aPiecekdc8fd572021-04-19 10:47:23 +02003577 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003578 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003579 .section = TRD_SECT_MODULE,
3580 .module = module,
3581 .pn = module->parsed->data,
3582 .tpn = module->parsed->data,
aPiecek3f247652021-04-19 13:40:25 +02003583 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003584 };
aPiecek61d062b2020-11-02 11:05:09 +01003585
aPiecekdc8fd572021-04-19 10:47:23 +02003586 pc->out = out;
3587
3588 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003589 .parent = trop_modi_parent,
3590 .first_sibling = trop_modi_first_sibling,
3591 .next_sibling = trop_modi_next_sibling,
3592 .next_child = trop_modi_next_child,
3593 .next_augment = trop_modi_next_augment,
aPiecek3f247652021-04-19 13:40:25 +02003594 .get_rpcs = tro_modi_get_rpcs,
3595 .get_notifications = tro_modi_get_notifications,
aPiecekef1e58e2021-04-19 13:19:44 +02003596 .next_grouping = trop_modi_next_grouping,
aPiecek61d062b2020-11-02 11:05:09 +01003597 .next_yang_data = tro_modi_next_yang_data
3598 };
3599
aPiecekdc8fd572021-04-19 10:47:23 +02003600 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003601 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003602 .node = trop_read_node,
3603 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003604 };
3605
aPiecekdc8fd572021-04-19 10:47:23 +02003606 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003607 .print_features_names = tro_print_features_names,
3608 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01003609 };
3610
aPiecekdc8fd572021-04-19 10:47:23 +02003611 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01003612}
3613
3614/**
aPiecek3f247652021-04-19 13:40:25 +02003615 * @brief Settings if lysc_node are used for browsing through the tree.
3616 *
3617 * Pointers to current nodes will be set to module data.
3618 *
3619 * @param[in] module YANG schema tree structure representing
3620 * YANG module.
3621 * @param[in] out is output handler.
3622 * @param[in] max_line_length is the maximum line length limit
3623 * that should not be exceeded.
3624 * @param[in,out] pc will be adapted to lysc_tree.
3625 * @param[in,out] tc will be adapted to lysc_tree.
3626 */
3627static void
3628trm_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)
3629{
3630 *tc = (struct trt_tree_ctx) {
3631 .lysc_tree = 1,
3632 .section = TRD_SECT_MODULE,
3633 .module = module,
3634 .tpn = NULL,
3635 .pn = NULL,
3636 .cn = module->compiled->data
3637 };
3638
3639 pc->out = out;
3640
3641 pc->fp.modify = (struct trt_fp_modify_ctx) {
3642 .parent = troc_modi_parent,
3643 .first_sibling = troc_modi_first_sibling,
3644 .next_sibling = troc_modi_next_sibling,
3645 .next_child = troc_modi_next_child,
3646 .next_augment = trop_modi_next_augment,
3647 .get_rpcs = tro_modi_get_rpcs,
3648 .get_notifications = tro_modi_get_notifications,
3649 .next_grouping = NULL,
3650 .next_yang_data = tro_modi_next_yang_data
3651 };
3652
3653 pc->fp.read = (struct trt_fp_read) {
3654 .module_name = tro_read_module_name,
3655 .node = troc_read_node,
3656 .if_sibling_exists = troc_read_if_sibling_exists
3657 };
3658
3659 pc->fp.print = (struct trt_fp_print) {
3660 .print_features_names = tro_print_features_names,
3661 .print_keys = tro_print_keys
3662 };
3663
3664 pc->max_line_length = max_line_length;
3665}
3666
3667/**
3668 * @brief Reset settings to browsing through the lysc tree.
3669 * @param[in,out] pc resets to \ref TRP_troc functions.
3670 * @param[in,out] tc resets to lysc browsing.
3671 */
3672static void
3673trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3674{
3675 trm_lysc_tree_ctx(tc->module, pc->out, pc->max_line_length, pc, tc);
3676}
3677
3678/**
3679 * @brief Reset settings to browsing through the lysp tree.
3680 * @param[in,out] pc resets to \ref TRP_trop functions.
3681 * @param[in,out] tc resets to lysp browsing.
3682 */
3683static void
3684trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3685{
3686 trm_lysp_tree_ctx(tc->module, pc->out, pc->max_line_length, pc, tc);
3687}
3688
3689/**
3690 * @brief If augment's target node is located on the current module.
3691 * @param[in] pn is examined augment.
3692 * @param[in] pmod is current module.
3693 * @return 1 if nodeid refers to the local node, otherwise 0.
3694 */
3695static ly_bool
3696trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
3697{
3698 const char *id, *prefix, *name;
3699 size_t prefix_len, name_len;
3700 const struct lys_module *mod;
3701 ly_bool ret = 0;
3702
3703 if (pn == NULL) {
3704 return ret;
3705 }
3706
3707 id = pn->nodeid;
3708 if (!id) {
3709 return ret;
3710 }
3711 /* only absolute-schema-nodeid is taken into account */
3712 assert(id[0] == '/');
3713 ++id;
3714
3715 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3716 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003717 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
aPiecek3f247652021-04-19 13:40:25 +02003718 ret = mod->parsed == pmod;
3719 } else {
3720 ret = 1;
3721 }
3722
3723 return ret;
3724}
3725
3726/**
aPiecekdc8fd572021-04-19 10:47:23 +02003727 * @brief Printing section 'module', 'rpcs' or 'notifications'.
aPiecek61d062b2020-11-02 11:05:09 +01003728 *
aPiecekdc8fd572021-04-19 10:47:23 +02003729 * First node must be the first child of 'module',
3730 * 'rpcs' or 'notifications'.
aPiecek61d062b2020-11-02 11:05:09 +01003731 *
aPiecekdc8fd572021-04-19 10:47:23 +02003732 * @param[in] ks is section representation.
3733 * @param[in] pc contains mainly functions for printing.
3734 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01003735 */
3736static void
aPiecekdc8fd572021-04-19 10:47:23 +02003737trm_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 +01003738{
aPiecekdc8fd572021-04-19 10:47:23 +02003739 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3740 return;
3741 }
3742
3743 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
3744 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
3745 trb_print_family_tree(TRD_WRAPPER_TOP, pc, tc);
3746 } else {
3747 trb_print_family_tree(TRD_WRAPPER_BODY, pc, tc);
3748 }
3749}
3750
3751/**
3752 * @brief Printing section 'augment', 'grouping' or 'yang-data'.
3753 *
3754 * First node is augment, grouping or yang-data itself.
3755 *
3756 * @param[in] ks is section representation.
3757 * @param[in] pc contains mainly functions for printing.
3758 * @param[in,out] tc is the tree context.
3759 */
3760static void
3761trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3762{
3763 ly_bool grp_has_data = 0;
3764
3765 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3766 return;
3767 }
3768
3769 if (ks.type == TRD_KEYWORD_GROUPING) {
3770 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
3771 }
3772
3773 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
3774 trb_tree_ctx_set_child(tc);
3775 trb_print_family_tree(TRD_WRAPPER_BODY, pc, tc);
3776}
3777
3778/**
3779 * @brief Print 'module' keyword, its name and all nodes.
3780 * @param[in] pc contains mainly functions for printing.
3781 * @param[in,out] tc is the tree context.
3782 */
3783static void
3784trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3785{
3786 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
3787}
3788
3789/**
3790 * @brief For all augment sections: print 'augment' keyword,
3791 * its target node and all nodes.
3792 * @param[in] pc contains mainly functions for printing.
3793 * @param[in,out] tc is the tree context.
3794 */
3795static void
3796trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3797{
3798 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02003799 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02003800
3801 if (!pc->fp.modify.next_augment) {
3802 return;
3803 }
3804
aPiecek3f247652021-04-19 13:40:25 +02003805 if (tc->lysc_tree) {
3806 origin_was_lysc_tree = 1;
3807 trm_reset_to_lysp_tree_ctx(pc, tc);
3808 }
3809
aPiecekdc8fd572021-04-19 10:47:23 +02003810 once = 1;
3811 for (struct trt_keyword_stmt ks = pc->fp.modify.next_augment(tc);
3812 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
3813 ks = pc->fp.modify.next_augment(tc)) {
3814
aPiecek3f247652021-04-19 13:40:25 +02003815 if (origin_was_lysc_tree) {
3816 /* if lysc tree is used, then only augments targeting
3817 * another module are printed
3818 */
3819 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->module->parsed)) {
3820 continue;
3821 }
3822 }
3823
aPiecekdc8fd572021-04-19 10:47:23 +02003824 if (once) {
3825 ly_print_(pc->out, "\n");
3826 ly_print_(pc->out, "\n");
3827 once = 0;
3828 } else {
3829 ly_print_(pc->out, "\n");
3830 }
3831
3832 trm_print_section_as_subtree(ks, pc, tc);
3833 }
aPiecek3f247652021-04-19 13:40:25 +02003834
3835 if (origin_was_lysc_tree) {
3836 trm_reset_to_lysc_tree_ctx(pc, tc);
3837 }
aPiecekdc8fd572021-04-19 10:47:23 +02003838}
3839
3840/**
3841 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
3842 * @param[in] pc contains mainly functions for printing.
3843 * @param[in,out] tc is the tree context.
3844 */
3845static void
3846trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3847{
3848 struct trt_keyword_stmt rpc;
3849
3850 assert(pc->fp.modify.get_rpcs);
3851
3852 rpc = pc->fp.modify.get_rpcs(tc);
3853
3854 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
3855 ly_print_(pc->out, "\n");
3856 ly_print_(pc->out, "\n");
3857 trm_print_section_as_family_tree(rpc, pc, tc);
3858 }
3859}
3860
3861/**
3862 * @brief For notifications section: print 'notifications' keyword
3863 * and all its nodes.
3864 * @param[in] pc contains mainly functions for printing.
3865 * @param[in,out] tc is the tree context.
3866 */
3867static void
3868trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3869{
3870 struct trt_keyword_stmt notifs;
3871
3872 assert(pc->fp.modify.get_notifications);
3873
3874 notifs = pc->fp.modify.get_notifications(tc);
3875
3876 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
3877 ly_print_(pc->out, "\n");
3878 ly_print_(pc->out, "\n");
3879 trm_print_section_as_family_tree(notifs, pc, tc);
3880 }
3881}
3882
3883/**
3884 * @brief For all grouping sections: print 'grouping' keyword, its name
3885 * and all nodes.
3886 * @param[in] pc contains mainly functions for printing.
3887 * @param[in,out] tc is the tree context.
3888 */
3889static void
3890trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3891{
3892 ly_bool once;
3893
3894 if (!pc->fp.modify.next_grouping) {
3895 return;
3896 }
3897
3898 once = 1;
3899 for (struct trt_keyword_stmt ks = pc->fp.modify.next_grouping(tc);
3900 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
3901 ks = pc->fp.modify.next_grouping(tc)) {
3902 if (once) {
3903 ly_print_(pc->out, "\n");
3904 ly_print_(pc->out, "\n");
3905 once = 0;
3906 } else {
3907 ly_print_(pc->out, "\n");
3908 }
3909 trm_print_section_as_subtree(ks, pc, tc);
3910 }
3911}
3912
3913/**
3914 * @brief For all yang-data sections: print 'yang-data' keyword
3915 * and all its nodes.
3916 * @param[in] pc contains mainly functions for printing.
3917 * @param[in,out] tc is the tree context.
3918 */
3919static void
3920trm_print_yang_data(struct trt_printer_ctx *UNUSED(pc), struct trt_tree_ctx *UNUSED(tc))
3921{
3922 /* TODO yang-data is not implemented */
3923 return;
3924}
3925
3926/**
3927 * @brief Print sections module, augment, rpcs, notifications,
3928 * grouping, yang-data.
3929 * @param[in] pc contains mainly functions for printing.
3930 * @param[in,out] tc is the tree context.
3931 */
3932static void
3933trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3934{
3935 trm_print_module_section(pc, tc);
3936 trm_print_augmentations(pc, tc);
3937 trm_print_rpcs(pc, tc);
3938 trm_print_notifications(pc, tc);
3939 trm_print_groupings(pc, tc);
3940 trm_print_yang_data(pc, tc);
3941 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01003942}
3943
aPiecek874ea4d2021-04-19 12:26:36 +02003944/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003945 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02003946 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003947
3948LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02003949tree_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 +01003950{
3951 struct trt_printer_ctx pc;
3952 struct trt_tree_ctx tc;
3953 struct ly_out *new_out;
3954 LY_ERR erc;
3955 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
3956
aPiecekdc8fd572021-04-19 10:47:23 +02003957 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
3958
aPiecek61d062b2020-11-02 11:05:09 +01003959 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
3960 return erc;
3961 }
3962
3963 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02003964 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
3965 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
3966 } else {
3967 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
3968 }
aPiecek61d062b2020-11-02 11:05:09 +01003969
3970 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003971 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01003972
3973 ly_out_free(new_out, NULL, 1);
3974
aPiecekdc8fd572021-04-19 10:47:23 +02003975 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01003976}
3977
3978LY_ERR
3979tree_print_submodule(struct ly_out *UNUSED(out), const struct lys_module *UNUSED(module), const struct lysp_submodule *UNUSED(submodp), uint32_t UNUSED(options), size_t UNUSED(line_length))
3980// LY_ERR tree_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options, size_t line_length)
3981{
3982 /** Not implemented right now. */
3983 return LY_SUCCESS;
3984}
3985
3986LY_ERR
3987tree_print_compiled_node(struct ly_out *UNUSED(out), const struct lysc_node *UNUSED(node), uint32_t UNUSED(options), size_t UNUSED(line_length))
3988// LY_ERR tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options, size_t line_length)
3989{
3990 /** Not implemented right now. */
3991 return LY_SUCCESS;
3992}