blob: c5468ec010ae25fa066e7725056e3eca0969c8a2 [file] [log] [blame]
aPiecek61d062b2020-11-02 11:05:09 +01001/**
2 * @file printer_tree.c
3 * @author Adam Piecek <piecek@cesnet.cz>
4 * @brief RFC tree printer for libyang data structure
5 *
6 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
aPiecek874ea4d2021-04-19 12:26:36 +020013 *
14 * @section TRP_DESIGN Design
15 *
16 * @code
aPiecek61d062b2020-11-02 11:05:09 +010017 * +---------+ +---------+ +---------+
18 * output | trp | | trb | | tro |
19 * <---+ Print +<---+ Browse +<-->+ Obtain |
20 * | | | | | |
21 * +---------+ +----+----+ +---------+
22 * ^
23 * |
24 * +----+----+
25 * | trm |
26 * | Manager |
27 * | |
28 * +----+----+
29 * ^
30 * | input
31 * +
aPiecek874ea4d2021-04-19 12:26:36 +020032 * @endcode
aPiecek61d062b2020-11-02 11:05:09 +010033 *
aPiecek874ea4d2021-04-19 12:26:36 +020034 * @subsection TRP_GLOSSARY Glossary
aPiecek61d062b2020-11-02 11:05:09 +010035 *
aPiecek874ea4d2021-04-19 12:26:36 +020036 * @subsubsection TRP_trm trm
37 * Manager functions are at the peak of abstraction. They are
38 * able to print individual sections of the YANG tree diagram
39 * (eg module, notifications, rpcs ...) and they call
aPiecek01598c02021-04-23 14:18:24 +020040 * Browse functions (@ref TRP_trb).
aPiecek61d062b2020-11-02 11:05:09 +010041 *
aPiecek874ea4d2021-04-19 12:26:36 +020042 * @subsubsection TRP_trb trb
43 * Browse functions contain a general algorithm (Preorder DFS)
44 * for traversing the tree. It does not matter what data type
aPiecek01598c02021-04-23 14:18:24 +020045 * the tree contains (@ref lysc_node or @ref lysp_node), because it
aPiecek874ea4d2021-04-19 12:26:36 +020046 * requires a ready-made getter functions for traversing the tree
aPiecek01598c02021-04-23 14:18:24 +020047 * (@ref trt_fp_all) and transformation function to its own node
48 * data type (@ref trt_node). These getter functions are generally
49 * referred to as @ref TRP_tro. Browse functions can repeatedly
aPiecek874ea4d2021-04-19 12:26:36 +020050 * traverse nodes in the tree, for example, to calculate the alignment
51 * gap before the nodes \<type\> in the YANG Tree Diagram.
aPiecek01598c02021-04-23 14:18:24 +020052 * The obtained @ref trt_node is passed to the @ref TRP_trp functions
aPiecek874ea4d2021-04-19 12:26:36 +020053 * to print the Tree diagram.
54 *
55 * @subsubsection TRP_tro tro
56 * Functions that provide an extra wrapper for the libyang library.
aPiecekef1e58e2021-04-19 13:19:44 +020057 * The Obtain functions are further specialized according to whether
aPiecek01598c02021-04-23 14:18:24 +020058 * they operate on lysp_tree (@ref TRP_trop) or lysc_tree
59 * (@ref TRP_troc). If they are general algorithms, then they have the
aPiecek3f247652021-04-19 13:40:25 +020060 * prefix \b tro_. The Obtain functions provide information to
aPiecek01598c02021-04-23 14:18:24 +020061 * @ref TRP_trb functions for printing the Tree diagram.
aPiecekef1e58e2021-04-19 13:19:44 +020062 *
63 * @subsubsection TRP_trop trop
64 * Functions for Obtaining information from Parsed schema tree.
aPiecek874ea4d2021-04-19 12:26:36 +020065 *
aPiecek3f247652021-04-19 13:40:25 +020066 * @subsubsection TRP_troc troc
67 * Functions for Obtaining information from Compiled schema tree.
68 *
aPiecek874ea4d2021-04-19 12:26:36 +020069 * @subsubsection TRP_trp trp
70 * Print functions take care of the printing YANG diagram. They can
71 * also split one node into multiple lines if the node does not fit
72 * on one line.
73 *
74 * @subsubsection TRP_trt trt
75 * Data type marking in the printer_tree module.
76 *
77 * @subsubsection TRP_trg trg
78 * General functions.
79 *
80 * @subsection TRP_ADJUSTMENTS Adjustments
81 * It is assumed that the changes are likely to take place mainly for
aPiecek01598c02021-04-23 14:18:24 +020082 * @ref TRP_tro, @ref TRP_trop or @ref TRP_troc functions because
aPiecek3f247652021-04-19 13:40:25 +020083 * they are the only ones dependent on libyang implementation.
84 * In special cases, changes will also need to be made to the
aPiecek01598c02021-04-23 14:18:24 +020085 * @ref TRP_trp functions if a special algorithm is needed to print
aPiecek3f247652021-04-19 13:40:25 +020086 * (right now this is prepared for printing list's keys
87 * and if-features).
aPiecek61d062b2020-11-02 11:05:09 +010088 */
89
aPiecek874ea4d2021-04-19 12:26:36 +020090#include <assert.h>
91#include <string.h>
92
93#include "common.h"
94#include "compat.h"
95#include "out_internal.h"
96#include "tree_schema_internal.h"
97#include "xpath.h"
98
aPiecek61d062b2020-11-02 11:05:09 +010099/**
100 * @brief List of available actions.
101 */
102typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200103 TRD_PRINT = 0, /**< Normal behavior. It just prints. */
104 TRD_CHAR_COUNT /**< Characters will be counted instead of printing. */
aPiecek61d062b2020-11-02 11:05:09 +0100105} trt_ly_out_clb_arg_flag;
106
107/**
aPiecek874ea4d2021-04-19 12:26:36 +0200108 * @brief Structure is passed as 'writeclb' argument
aPiecek01598c02021-04-23 14:18:24 +0200109 * to the ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100110 */
111struct ly_out_clb_arg {
aPiecek874ea4d2021-04-19 12:26:36 +0200112 trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */
113 struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */
114 size_t counter; /**< Counter of printed characters. */
115 LY_ERR last_error; /**< The last error that occurred. If no error has occurred, it will be ::LY_SUCCESS. */
aPiecek61d062b2020-11-02 11:05:09 +0100116};
117
118/**
119 * @brief Initialize struct ly_out_clb_arg with default settings.
120 */
121#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
aPiecek874ea4d2021-04-19 12:26:36 +0200122 (struct ly_out_clb_arg) { \
123 .mode = MODE, .out = OUT, \
124 .counter = COUNTER, .last_error = LAST_ERROR \
125 }
aPiecek61d062b2020-11-02 11:05:09 +0100126
aPiecek874ea4d2021-04-19 12:26:36 +0200127/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100128 * Print getters
aPiecek874ea4d2021-04-19 12:26:36 +0200129 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100130
131/**
132 * @brief Callback functions that prints special cases.
133 *
134 * It just groups together tree context with trt_fp_print.
135 */
136struct trt_cf_print {
aPiecek874ea4d2021-04-19 12:26:36 +0200137 const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */
138 void (*pf)(const struct trt_tree_ctx *, struct ly_out *); /**< Pointing to function which printing list's keys or features. */
aPiecek61d062b2020-11-02 11:05:09 +0100139};
140
141/**
142 * @brief Callback functions for printing special cases.
143 *
aPiecek874ea4d2021-04-19 12:26:36 +0200144 * Functions with the suffix 'trp' can print most of the text on
145 * output, just by setting the pointer to the string. But in some
146 * cases, it's not that simple, because its entire string is fragmented
147 * in memory. For example, for printing list's keys or if-features.
aPiecek61d062b2020-11-02 11:05:09 +0100148 * However, this depends on how the libyang library is implemented.
aPiecek3f247652021-04-19 13:40:25 +0200149 * This implementation of the printer_tree module goes through
150 * a lysp tree, but if it goes through a lysc tree, these special cases
151 * would be different.
aPiecek61d062b2020-11-02 11:05:09 +0100152 * Functions must print including spaces or delimiters between names.
153 */
154struct trt_fp_print {
155 void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */
156 void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */
157};
158
159/**
160 * @brief Package which only groups getter function.
161 */
162struct trt_pck_print {
163 const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */
164 struct trt_fp_print fps; /**< Print function. */
165};
166
167/**
168 * @brief Initialize struct trt_pck_print by parameters.
169 */
170#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
aPiecek874ea4d2021-04-19 12:26:36 +0200171 (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT}
aPiecek61d062b2020-11-02 11:05:09 +0100172
aPiecek874ea4d2021-04-19 12:26:36 +0200173/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100174 * Indent
aPiecek874ea4d2021-04-19 12:26:36 +0200175 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100176
177/**
aPiecek874ea4d2021-04-19 12:26:36 +0200178 * @brief Constants which are defined in the RFC or are observable
179 * from the pyang tool.
aPiecek61d062b2020-11-02 11:05:09 +0100180 */
181typedef enum {
182 TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */
183 TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with a whitespace offset of at least two characters. */
184 TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */
185 TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */
186 TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */
187 TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */
188 TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */
189} trt_cnf_indent;
190
191/**
192 * @brief Type of indent in node.
193 */
194typedef enum {
195 TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */
196 TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */
197 TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
198} trt_indent_in_node_type;
199
200/** Constant to indicate the need to break a line. */
201#define TRD_LINEBREAK -1
202
203/**
aPiecek874ea4d2021-04-19 12:26:36 +0200204 * @brief Records the alignment between the individual
205 * elements of the node.
aPiecek61d062b2020-11-02 11:05:09 +0100206 *
aPiecek874ea4d2021-04-19 12:26:36 +0200207 * @see trp_default_indent_in_node, trp_try_normal_indent_in_node
aPiecek61d062b2020-11-02 11:05:09 +0100208 */
209struct trt_indent_in_node {
210 trt_indent_in_node_type type; /**< Type of indent in node. */
aPiecek874ea4d2021-04-19 12:26:36 +0200211 int16_t btw_name_opts; /**< Indent between node name and \<opts\>. */
212 int16_t btw_opts_type; /**< Indent between \<opts\> and \<type\>. */
aPiecek61d062b2020-11-02 11:05:09 +0100213 int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */
214};
215
216/**
217 * @brief Type of wrappers to be printed.
218 */
219typedef enum {
220 TRD_WRAPPER_TOP = 0, /**< Related to the module. */
221 TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */
222} trd_wrapper_type;
223
224/**
225 * @brief For resolving sibling symbol ('|') placement.
226 *
227 * Bit indicates where the sibling symbol must be printed.
aPiecek874ea4d2021-04-19 12:26:36 +0200228 * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS.
aPiecek61d062b2020-11-02 11:05:09 +0100229 *
aPiecek874ea4d2021-04-19 12:26:36 +0200230 * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY,
231 * trp_wrapper_set_mark, trp_wrapper_set_shift,
232 * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper
aPiecek61d062b2020-11-02 11:05:09 +0100233 */
234struct trt_wrapper {
235 trd_wrapper_type type; /**< Location of the wrapper. */
236 uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed.
237 It follows that the maximum immersion of the printable node is 64. */
238 uint32_t actual_pos; /**< Actual position in bit_marks. */
239};
240
241/**
242 * @brief Get wrapper related to the module section.
243 *
244 * @code
245 * module: <module-name>
246 * +--<node>
247 * |
248 * @endcode
249 */
250#define TRP_INIT_WRAPPER_TOP \
aPiecek874ea4d2021-04-19 12:26:36 +0200251 (struct trt_wrapper) { \
252 .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \
253 }
aPiecek61d062b2020-11-02 11:05:09 +0100254
255/**
aPiecek874ea4d2021-04-19 12:26:36 +0200256 * @brief Get wrapper related to subsection
257 * e.g. Augmenations or Groupings.
aPiecek61d062b2020-11-02 11:05:09 +0100258 *
259 * @code
260 * module: <module-name>
261 * +--<node>
262 *
263 * augment <target-node>:
264 * +--<node>
265 * @endcode
266 */
267#define TRP_INIT_WRAPPER_BODY \
aPiecek874ea4d2021-04-19 12:26:36 +0200268 (struct trt_wrapper) { \
269 .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \
270 }
aPiecek61d062b2020-11-02 11:05:09 +0100271
272/**
273 * @brief Package which only groups wrapper and indent in node.
274 */
275struct trt_pck_indent {
276 struct trt_wrapper wrapper; /**< Coded " | | " sequence. */
277 struct trt_indent_in_node in_node; /**< Indent in node. */
278};
279
280/**
281 * @brief Initialize struct trt_pck_indent by parameters.
282 */
283#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200284 (struct trt_pck_indent){ \
285 .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \
286 }
aPiecek61d062b2020-11-02 11:05:09 +0100287
aPiecek874ea4d2021-04-19 12:26:36 +0200288/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100289 * status
aPiecek874ea4d2021-04-19 12:26:36 +0200290 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100291
292/**
293 * @brief Status of the node.
294 *
aPiecek874ea4d2021-04-19 12:26:36 +0200295 * @see trp_print_status
aPiecek61d062b2020-11-02 11:05:09 +0100296 */
297typedef enum {
298 TRD_STATUS_TYPE_EMPTY = 0,
aPiecek874ea4d2021-04-19 12:26:36 +0200299 TRD_STATUS_TYPE_CURRENT, /**< ::LYS_STATUS_CURR */
300 TRD_STATUS_TYPE_DEPRECATED, /**< ::LYS_STATUS_DEPRC */
301 TRD_STATUS_TYPE_OBSOLETE /**< ::LYS_STATUS_OBSLT */
aPiecek61d062b2020-11-02 11:05:09 +0100302} trt_status_type;
303
aPiecek874ea4d2021-04-19 12:26:36 +0200304/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100305 * flags
aPiecek874ea4d2021-04-19 12:26:36 +0200306 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100307
308/**
309 * @brief Flag of the node.
310 *
aPiecek874ea4d2021-04-19 12:26:36 +0200311 * @see trp_print_flags, trp_get_flags_strlen
aPiecek61d062b2020-11-02 11:05:09 +0100312 */
313typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200314 TRD_FLAGS_TYPE_EMPTY = 0, /**< -- */
aPiecek61d062b2020-11-02 11:05:09 +0100315 TRD_FLAGS_TYPE_RW, /**< rw */
316 TRD_FLAGS_TYPE_RO, /**< ro */
317 TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */
318 TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */
319 TRD_FLAGS_TYPE_RPC, /**< -x */
320 TRD_FLAGS_TYPE_NOTIF, /**< -n */
321 TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */
322} trt_flags_type;
323
aPiecek874ea4d2021-04-19 12:26:36 +0200324/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100325 * node_name and opts
aPiecek874ea4d2021-04-19 12:26:36 +0200326 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100327
328#define TRD_NODE_NAME_PREFIX_CHOICE "("
329#define TRD_NODE_NAME_PREFIX_CASE ":("
330#define TRD_NODE_NAME_TRIPLE_DOT "..."
331
332/**
333 * @brief Type of the node.
334 *
aPiecek874ea4d2021-04-19 12:26:36 +0200335 * Used mainly to complete the correct \<opts\> next to or
336 * around the \<name\>.
aPiecek61d062b2020-11-02 11:05:09 +0100337 */
338typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200339 TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */
340 TRD_NODE_CASE, /**< For case node. :(\<name\>) */
341 TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */
342 TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */
343 TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */
344 TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */
345 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list (without keys). \<name\>* */
346 TRD_NODE_KEYS, /**< For a list's keys. \<name\>* [\<keys\>] */
347 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
348 TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */
349 TRD_NODE_TRIPLE_DOT /**< For collapsed sibling nodes and their children. Special case which doesn't belong here very well. */
aPiecek61d062b2020-11-02 11:05:09 +0100350} trt_node_type;
351
352/**
353 * @brief Type of node and his name.
354 *
aPiecek874ea4d2021-04-19 12:26:36 +0200355 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
aPiecek61d062b2020-11-02 11:05:09 +0100356 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
357 */
358struct trt_node_name {
359 trt_node_type type; /**< Type of the node relevant for printing. */
360 const char *module_prefix; /**< Prefix defined in the module where the node is defined. */
361 const char *str; /**< Name of the node. */
362};
363
364/**
365 * @brief Create struct trt_node_name as empty.
366 */
367#define TRP_EMPTY_NODE_NAME \
aPiecek874ea4d2021-04-19 12:26:36 +0200368 (struct trt_node_name) { \
369 .type = TRD_NODE_ELSE, .module_prefix = NULL, .str = NULL \
370 }
aPiecek61d062b2020-11-02 11:05:09 +0100371
372/**
373 * @brief Check if struct trt_node_name is empty.
374 */
375#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
376 !NODE_NAME.str
377
aPiecek874ea4d2021-04-19 12:26:36 +0200378/**
379 * @brief Every \<opts\> mark except string of list's keys
380 * has a length of one.
381 */
aPiecek61d062b2020-11-02 11:05:09 +0100382#define TRD_OPTS_MARK_LENGTH 1
383
aPiecek874ea4d2021-04-19 12:26:36 +0200384/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100385 * type
aPiecek874ea4d2021-04-19 12:26:36 +0200386 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100387
388/**
389 * @brief Type of the \<type\>
390 */
391typedef enum {
392 TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */
393 TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
aPiecek874ea4d2021-04-19 12:26:36 +0200394 TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm.
395 So set type as ::TRD_TYPE_TARGET. */
aPiecek61d062b2020-11-02 11:05:09 +0100396 TRD_TYPE_EMPTY /**< Type is not used at all. */
397} trt_type_type;
398
399/**
400 * @brief \<type\> in the \<node\>.
401 *
aPiecek874ea4d2021-04-19 12:26:36 +0200402 * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
aPiecek61d062b2020-11-02 11:05:09 +0100403 */
404struct trt_type {
405 trt_type_type type; /**< Type of the \<type\>. */
406 const char *str; /**< Path or name of the type. */
407};
408
409/**
410 * @brief Create empty struct trt_type.
411 */
412#define TRP_EMPTY_TRT_TYPE \
413 (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}
414
415/**
416 * @brief Check if struct trt_type is empty.
417 */
418#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
419 TYPE_OF_TYPE.type == TRD_TYPE_EMPTY
420
421/**
422 * @brief Initialize struct trt_type by parameters.
423 */
424#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
425 (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}
426
aPiecek874ea4d2021-04-19 12:26:36 +0200427/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100428 * node
aPiecek874ea4d2021-04-19 12:26:36 +0200429 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100430
431/**
432 * @brief \<node\> data for printing.
433 *
aPiecek874ea4d2021-04-19 12:26:36 +0200434 * It contains RFC's:
435 * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
aPiecek61d062b2020-11-02 11:05:09 +0100436 * Item \<opts\> is moved to part struct trt_node_name.
aPiecek874ea4d2021-04-19 12:26:36 +0200437 * For printing [\<keys\>] and if-features is required special
438 * functions which prints them.
aPiecek61d062b2020-11-02 11:05:09 +0100439 *
aPiecek874ea4d2021-04-19 12:26:36 +0200440 * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty,
441 * trp_print_node_up_to_name, trp_print_divided_node_up_to_name,
442 * trp_print_node
aPiecek61d062b2020-11-02 11:05:09 +0100443 */
444struct trt_node {
aPiecek874ea4d2021-04-19 12:26:36 +0200445 trt_status_type status; /**< \<status\>. */
446 trt_flags_type flags; /**< \<flags\>. */
447 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
448 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
449 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and
450 will be printed by trt_fp_print.print_features_names callback. */
451 ly_bool last_one; /**< Information about whether the node is the last. */
aPiecek61d062b2020-11-02 11:05:09 +0100452};
453
454/**
455 * @brief Create struct trt_node as empty.
456 */
457#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200458 (struct trt_node) { \
459 .status = TRD_STATUS_TYPE_EMPTY, \
460 .flags = TRD_FLAGS_TYPE_EMPTY, \
461 .name = TRP_EMPTY_NODE_NAME, \
462 .type = TRP_EMPTY_TRT_TYPE, \
463 .iffeatures = 0, \
464 .last_one = 1 \
465 }
aPiecek61d062b2020-11-02 11:05:09 +0100466
467/**
468 * @brief Package which only groups indent and node.
469 */
470struct trt_pair_indent_node {
471 struct trt_indent_in_node indent;
472 struct trt_node node;
473};
474
475/**
476 * @brief Initialize struct trt_pair_indent_node by parameters.
477 */
478#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200479 (struct trt_pair_indent_node) { \
480 .indent = INDENT_IN_NODE, .node = NODE \
481 }
aPiecek61d062b2020-11-02 11:05:09 +0100482
aPiecek874ea4d2021-04-19 12:26:36 +0200483/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100484 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200485 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100486
487#define TRD_TOP_KEYWORD_MODULE "module"
488#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
489
490#define TRD_BODY_KEYWORD_AUGMENT "augment"
491#define TRD_BODY_KEYWORD_RPC "rpcs"
492#define TRD_BODY_KEYWORD_NOTIF "notifications"
493#define TRD_BODY_KEYWORD_GROUPING "grouping"
494#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
495
496/**
497 * @brief Type of the trt_keyword.
498 */
499typedef enum {
500 TRD_KEYWORD_EMPTY = 0,
501 TRD_KEYWORD_MODULE,
502 TRD_KEYWORD_SUBMODULE,
503 TRD_KEYWORD_AUGMENT,
504 TRD_KEYWORD_RPC,
505 TRD_KEYWORD_NOTIF,
506 TRD_KEYWORD_GROUPING,
507 TRD_KEYWORD_YANG_DATA
508} trt_keyword_type;
509
510/**
511 * @brief Main sign of the tree nodes.
512 *
aPiecek874ea4d2021-04-19 12:26:36 +0200513 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100514 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
515 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
516 * trp_keyword_type_strlen
517 *
518 */
519struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200520 trt_keyword_type type; /**< String containing some of the top or body keyword. */
521 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100522};
523
524/**
525 * @brief Create struct trt_keyword_stmt as empty.
526 */
527#define TRP_EMPTY_KEYWORD_STMT \
528 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
529
530/**
531 * @brief Check if struct trt_keyword_stmt is empty.
532 */
533#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
534 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
535
536/**
537 * @brief Initialize struct trt_keyword_stmt by parameters.
538 */
539#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
540 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
541
aPiecek874ea4d2021-04-19 12:26:36 +0200542/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100543 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200544 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100545
546struct trt_parent_cache;
547
548/**
549 * @brief Functions that change the state of the tree_ctx structure.
550 *
aPiecek3f247652021-04-19 13:40:25 +0200551 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200552 * for the 'trp' printing functions and are also called from the
553 * 'trb' browsing functions when walking through a tree. These callback
554 * functions need to be checked or reformulated if changes to the
555 * libyang library affect the printing tree. For all, if the value
556 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
557 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100558 */
559struct trt_fp_modify_ctx {
560 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
561 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
562 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
563 struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */
aPiecek61d062b2020-11-02 11:05:09 +0100564};
565
aPiecek874ea4d2021-04-19 12:26:36 +0200566/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100567 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200568 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100569
570/**
571 * @brief Functions that do not change the state of the tree_structure.
572 *
573 * For details see trt_fp_modify_ctx.
574 */
575struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200576 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
577 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
578 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100579};
580
aPiecek874ea4d2021-04-19 12:26:36 +0200581/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100582 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200583 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100584
585/**
aPiecek874ea4d2021-04-19 12:26:36 +0200586 * @brief A set of all necessary functions that must be provided
587 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100588 */
589struct trt_fp_all {
590 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
591 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
592 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
593};
594
aPiecek874ea4d2021-04-19 12:26:36 +0200595/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100596 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200597 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100598
599/**
aPiecek01598c02021-04-23 14:18:24 +0200600 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100601 */
602struct trt_printer_ctx {
603 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200604 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100605 size_t max_line_length; /**< The maximum number of characters that can be
606 printed on one line, including the last. */
607};
608
aPiecek874ea4d2021-04-19 12:26:36 +0200609/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100610 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200611 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100612
613/**
614 * @brief The name of the section to which the node belongs.
615 */
616typedef enum {
617 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
618 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
619 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
620 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
621 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
622 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
623} trt_actual_section;
624
625/**
626 * @brief Types of nodes that have some effect on their children.
627 */
628typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200629 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
630 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
631 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
632 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100633} trt_ancestor_type;
634
635/**
636 * @brief Saved information when browsing the tree downwards.
637 *
aPiecek874ea4d2021-04-19 12:26:36 +0200638 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200639 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200640 * this structures during their recursive calls. This functions do not
641 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200642 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200643 * structure, can return a node with the correct data. The word
644 * \b parent is in the structure name, because this data refers to
645 * the last parent and at the same time the states of its
646 * ancestors data. Only the function jumping on the child
647 * (next_child(...)) creates this structure, because the pointer
648 * to the current node moves down the tree. It's like passing
649 * the genetic code to children. Some data must be inherited and
650 * there are two approaches to this problem. Either it will always
651 * be determined which inheritance states belong to the current node
652 * (which can lead to regular travel to the root node) or
653 * the inheritance states will be stored during the recursive calls.
654 * So the problem was solved by the second option. Why does
655 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200656 * the lysp tree. For walks through the lysc tree is trt_parent_cache
657 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100658 *
aPiecek874ea4d2021-04-19 12:26:36 +0200659 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100660 */
661struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200662 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
663 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
664 uint16_t lys_config; /**< Inherited config W or R. */
665 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100666};
667
668/**
669 * @brief Return trt_parent_cache filled with default values.
670 */
671#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200672 (struct trt_parent_cache) { \
673 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
674 .lys_config = LYS_CONFIG_W, .last_list = NULL \
675 }
aPiecek61d062b2020-11-02 11:05:09 +0100676
677/**
678 * @brief Main structure for browsing the libyang tree
679 */
680struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200681 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
682 It is assumed that once set, it does not change.
683 If it is true then trt_tree_ctx.pn and
684 trt_tree_ctx.tpn are not used.
685 If it is false then trt_tree_ctx.cn is not used. */
686 trt_actual_section section; /**< To which section pn points. */
687 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
688 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
689 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
690 union {
691 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
692 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
693 is set to TRD_SECT_YANG_DATA. */
694 };
695 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
aPiecek61d062b2020-11-02 11:05:09 +0100696};
697
aPiecek3f247652021-04-19 13:40:25 +0200698/**
699 * @brief Get lysp_node from trt_tree_ctx.cn.
700 */
701#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
702 ((const struct lysp_node *)CN->priv)
703
aPiecek01598c02021-04-23 14:18:24 +0200704/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100705typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
706
aPiecekef1e58e2021-04-19 13:19:44 +0200707/**
708 * @brief Simple getter functions for lysp and lysc nodes.
709 *
710 * This structure is useful if we have a general algorithm
711 * (tro function) that can be used for both lysc and lysp nodes.
712 * Thanks to this structure, we prevent code redundancy.
713 * We don't have to write basically the same algorithm twice
714 * for lysp and lysc trees.
715 */
716struct tro_getters
717{
718 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
719 const void *(*next)(const void *); /**< Get sibling. */
720 const void *(*parent)(const void *); /**< Get parent. */
721 const void *(*child)(const void *); /**< Get child. */
722 const void *(*actions)(const void *); /**< Get actions. */
723 const void *(*action_input)(const void *); /**< Get input action from action node. */
724 const void *(*action_output)(const void *); /**< Get output action from action node. */
725 const void *(*notifs)(const void *); /**< Get notifs. */
726};
727
aPiecek874ea4d2021-04-19 12:26:36 +0200728/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100729 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200730 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100731
732/**
733 * @brief Print a substring but limited to the maximum length.
734 * @param[in] str is pointer to source.
735 * @param[in] len is number of characters to be printed.
736 * @param[in,out] out is output handler.
737 * @return str parameter shifted by len.
738 */
739static const char *
740trg_print_substr(const char *str, size_t len, struct ly_out *out)
741{
742 for (size_t i = 0; i < len; i++) {
743 ly_print_(out, "%c", str[0]);
744 str++;
745 }
746 return str;
747}
748
749/**
750 * @brief Pointer is not NULL and does not point to an empty string.
751 * @param[in] str is pointer to string to be checked.
752 * @return 1 if str pointing to non empty string otherwise 0.
753 */
754static ly_bool
755trg_charptr_has_data(const char *str)
756{
757 return (str) && (str[0] != '\0');
758}
759
760/**
aPiecek874ea4d2021-04-19 12:26:36 +0200761 * @brief Check if @p word in @p src is present where words are
762 * delimited by @p delim.
763 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100764 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200765 * @param[in] delim is delimiter between @p words in @p src.
766 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100767 */
768static ly_bool
769trg_word_is_present(const char *src, const char *word, char delim)
770{
771 const char *hit;
772
773 if ((!src) || (src[0] == '\0') || (!word)) {
774 return 0;
775 }
776
777 hit = strstr(src, word);
778
779 if (hit) {
780 /* word was founded at the begin of src
781 * OR it match somewhere after delim
782 */
783 if ((hit == src) || (hit[-1] == delim)) {
784 /* end of word was founded at the end of src
785 * OR end of word was match somewhere before delim
786 */
787 char delim_or_end = (hit + strlen(word))[0];
788 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
789 return 1;
790 }
791 }
792 /* after -> hit is just substr and it's not the whole word */
793 /* jump to the next word */
794 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
795 /* skip delim */
796 src = src[0] == '\0' ? src : src + 1;
797 /* continue with searching */
798 return trg_word_is_present(src, word, delim);
799 } else {
800 return 0;
801 }
802}
803
aPiecek874ea4d2021-04-19 12:26:36 +0200804/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100805 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200806 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100807
808/**
aPiecek01598c02021-04-23 14:18:24 +0200809 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100810 *
aPiecek874ea4d2021-04-19 12:26:36 +0200811 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100812 * @param[in] buf contains input characters
813 * @param[in] count is number of characters in buf.
814 * @return Number of printed bytes.
815 * @return Negative value in case of error.
816 */
817static ssize_t
818trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
819{
820 LY_ERR erc = LY_SUCCESS;
821 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
822
823 switch (data->mode) {
824 case TRD_PRINT:
825 erc = ly_write_(data->out, buf, count);
826 break;
827 case TRD_CHAR_COUNT:
828 data->counter = data->counter + count;
829 break;
830 default:
831 break;
832 }
833
834 if (erc != LY_SUCCESS) {
835 data->last_error = erc;
836 return -1;
837 } else {
838 return count;
839 }
840}
841
842/**
843 * @brief Check that indent in node can be considered as equivalent.
844 * @param[in] first is the first indent in node.
845 * @param[in] second is the second indent in node.
846 * @return 1 if indents are equivalent otherwise 0.
847 */
848static ly_bool
849trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
850{
851 const ly_bool a = first.type == second.type;
852 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
853 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
854 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
855
856 return a && b && c && d;
857}
858
859/**
aPiecek874ea4d2021-04-19 12:26:36 +0200860 * @brief Setting space character because node is last sibling.
861 * @param[in] wr is wrapper over which the shift operation
862 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100863 * @return New shifted wrapper.
864 */
865static struct trt_wrapper
866trp_wrapper_set_shift(struct trt_wrapper wr)
867{
868 assert(wr.actual_pos < 64);
869 /* +--<node>
870 * +--<node>
871 */
872 wr.actual_pos++;
873 return wr;
874}
875
876/**
aPiecek874ea4d2021-04-19 12:26:36 +0200877 * @brief Setting '|' symbol because node is divided or
878 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100879 * @param[in] wr is source of wrapper.
880 * @return New wrapper which is marked at actual position and shifted.
881 */
882static struct trt_wrapper
883trp_wrapper_set_mark(struct trt_wrapper wr)
884{
885 assert(wr.actual_pos < 64);
886 wr.bit_marks1 |= 1U << wr.actual_pos;
887 return trp_wrapper_set_shift(wr);
888}
889
890/**
891 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
892 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200893 * @param[in] last_one is flag. Value 1 saying if the node is the last
894 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100895 * @return New wrapper for the actual node.
896 */
897static struct trt_wrapper
898trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
899{
900 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
901}
902
903/**
904 * @brief Test if the wrappers are equivalent.
905 * @param[in] first is the first wrapper.
906 * @param[in] second is the second wrapper.
907 * @return 1 if the wrappers are equivalent otherwise 0.
908 */
909static ly_bool
910trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
911{
912 const ly_bool a = first.type == second.type;
913 const ly_bool b = first.bit_marks1 == second.bit_marks1;
914 const ly_bool c = first.actual_pos == second.actual_pos;
915
916 return a && b && c;
917}
918
919/**
920 * @brief Print " | " sequence on line.
921 * @param[in] wr is wrapper to be printed.
922 * @param[in,out] out is output handler.
923 */
924static void
925trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
926{
927 uint32_t lb;
928
929 if (wr.type == TRD_WRAPPER_TOP) {
930 lb = TRD_INDENT_LINE_BEGIN;
931 } else if (wr.type == TRD_WRAPPER_BODY) {
932 lb = TRD_INDENT_LINE_BEGIN * 2;
933 } else {
934 lb = TRD_INDENT_LINE_BEGIN;
935 }
936
937 ly_print_(out, "%*c", lb, ' ');
938
939 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
940 return;
941 }
942
943 for (uint32_t i = 0; i < wr.actual_pos; i++) {
944 /** Test if the bit on the index is set. */
945 if ((wr.bit_marks1 >> i) & 1U) {
946 ly_print_(out, "|");
947 } else {
948 ly_print_(out, " ");
949 }
950
951 if (i != wr.actual_pos) {
952 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
953 }
954 }
955}
956
957/**
958 * @brief Check if struct trt_node is empty.
959 * @param[in] node is item to test.
960 * @return 1 if node is considered empty otherwise 0.
961 */
962static ly_bool
963trp_node_is_empty(struct trt_node node)
964{
965 const ly_bool a = !node.iffeatures;
966 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
967 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
968 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
969 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
970
971 return a && b && c && d && e;
972}
973
974/**
aPiecek874ea4d2021-04-19 12:26:36 +0200975 * @brief Check if [\<keys\>], \<type\> and
976 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +0100977 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +0200978 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
979 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100980 */
981static ly_bool
982trp_node_body_is_empty(struct trt_node node)
983{
984 const ly_bool a = !node.iffeatures;
985 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
986 const ly_bool c = node.name.type != TRD_NODE_KEYS;
987
988 return a && b && c;
989}
990
991/**
992 * @brief Print \<status\> of the node.
993 * @param[in] status_type is type of status.
994 * @param[in,out] out is output handler.
995 */
996static void
997trp_print_status(trt_status_type status_type, struct ly_out *out)
998{
999 switch (status_type) {
1000 case TRD_STATUS_TYPE_CURRENT:
1001 ly_print_(out, "%c", '+');
1002 break;
1003 case TRD_STATUS_TYPE_DEPRECATED:
1004 ly_print_(out, "%c", 'x');
1005 break;
1006 case TRD_STATUS_TYPE_OBSOLETE:
1007 ly_print_(out, "%c", 'o');
1008 break;
1009 default:
1010 break;
1011 }
1012}
1013
1014/**
1015 * @brief Print \<flags\>.
1016 * @param[in] flags_type is type of \<flags\>.
1017 * @param[in,out] out is output handler.
1018 */
1019static void
1020trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1021{
1022 switch (flags_type) {
1023 case TRD_FLAGS_TYPE_RW:
1024 ly_print_(out, "%s", "rw");
1025 break;
1026 case TRD_FLAGS_TYPE_RO:
1027 ly_print_(out, "%s", "ro");
1028 break;
1029 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1030 ly_print_(out, "%s", "-w");
1031 break;
1032 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1033 ly_print_(out, "%s", "-u");
1034 break;
1035 case TRD_FLAGS_TYPE_RPC:
1036 ly_print_(out, "%s", "-x");
1037 break;
1038 case TRD_FLAGS_TYPE_NOTIF:
1039 ly_print_(out, "%s", "-n");
1040 break;
1041 case TRD_FLAGS_TYPE_MOUNT_POINT:
1042 ly_print_(out, "%s", "mp");
1043 break;
1044 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001045 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001046 break;
1047 }
1048}
1049
1050/**
1051 * @brief Get size of the \<flags\>.
1052 * @param[in] flags_type is type of \<flags\>.
1053 * @return 0 if flags_type is not set otherwise 2.
1054 */
1055static size_t
1056trp_get_flags_strlen(trt_flags_type flags_type)
1057{
1058 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1059}
1060
1061/**
1062 * @brief Print entire struct trt_node_name structure.
1063 * @param[in] node_name is item to print.
1064 * @param[in,out] out is output handler.
1065 */
1066static void
1067trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1068{
1069 const char *mod_prefix;
1070 const char *colon;
1071 const char trd_node_name_suffix_choice[] = ")";
1072 const char trd_node_name_suffix_case[] = ")";
1073 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1074 const char trd_opts_container[] = "!"; /**< For a presence container. */
1075 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1076 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1077 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1078
1079 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1080 return;
1081 }
1082
1083 if (node_name.module_prefix) {
1084 mod_prefix = node_name.module_prefix;
1085 colon = ":";
1086 } else {
1087 mod_prefix = "";
1088 colon = "";
1089 }
1090
1091 switch (node_name.type) {
1092 case TRD_NODE_ELSE:
1093 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1094 break;
1095 case TRD_NODE_CASE:
1096 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1097 break;
1098 case TRD_NODE_CHOICE:
1099 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1100 break;
1101 case TRD_NODE_OPTIONAL_CHOICE:
1102 ly_print_(out, "%s%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice, trd_opts_optional);
1103 break;
1104 case TRD_NODE_OPTIONAL:
1105 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1106 break;
1107 case TRD_NODE_CONTAINER:
1108 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1109 break;
1110 case TRD_NODE_LISTLEAFLIST:
1111 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1112 break;
1113 case TRD_NODE_KEYS:
1114 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1115 break;
1116 case TRD_NODE_TOP_LEVEL1:
1117 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1118 break;
1119 case TRD_NODE_TOP_LEVEL2:
1120 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1121 break;
1122 case TRD_NODE_TRIPLE_DOT:
1123 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1124 break;
1125 default:
1126 break;
1127 }
1128}
1129
1130/**
aPiecek874ea4d2021-04-19 12:26:36 +02001131 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1132 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001133 * @param[in] node_name is structure containing the 'mark'.
1134 * @return 1 if contain otherwise 0.
1135 */
1136static ly_bool
1137trp_mark_is_used(struct trt_node_name node_name)
1138{
1139 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1140 return 0;
1141 }
1142
1143 switch (node_name.type) {
1144 case TRD_NODE_ELSE:
1145 case TRD_NODE_CASE:
1146 case TRD_NODE_KEYS:
1147 return 0;
1148 default:
1149 return 1;
1150 }
1151}
1152
1153/**
1154 * @brief Print opts keys.
1155 * @param[in] node_name contains type of the node with his name.
1156 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001157 * @param[in] cf is basically a pointer to the function that prints
1158 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001159 * @param[in,out] out is output handler.
1160 */
1161static void
1162trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1163{
1164 if (node_name.type != TRD_NODE_KEYS) {
1165 return;
1166 }
1167
1168 /* <name><mark>___<keys>*/
1169 if (btw_name_opts > 0) {
1170 ly_print_(out, "%*c", btw_name_opts, ' ');
1171 }
1172 ly_print_(out, "[");
1173 cf.pf(cf.ctx, out);
1174 ly_print_(out, "]");
1175}
1176
1177/**
1178 * @brief Print entire struct trt_type structure.
1179 * @param[in] type is item to print.
1180 * @param[in,out] out is output handler.
1181 */
1182static void
1183trp_print_type(struct trt_type type, struct ly_out *out)
1184{
1185 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1186 return;
1187 }
1188
1189 switch (type.type) {
1190 case TRD_TYPE_NAME:
1191 ly_print_(out, "%s", type.str);
1192 break;
1193 case TRD_TYPE_TARGET:
1194 ly_print_(out, "-> %s", type.str);
1195 break;
1196 case TRD_TYPE_LEAFREF:
1197 ly_print_(out, "leafref");
1198 default:
1199 break;
1200 }
1201}
1202
1203/**
1204 * @brief Print all iffeatures of node
1205 *
1206 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001207 * @param[in] cf is basically a pointer to the function that prints
1208 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001209 * @param[in,out] out is output handler.
1210 */
1211static void
1212trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1213{
1214 if (iffeature_flag) {
1215 ly_print_(out, "{");
1216 cf.pf(cf.ctx, out);
1217 ly_print_(out, "}?");
1218 }
1219}
1220
1221/**
1222 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1223 * @param[in] node contains items to print.
1224 * @param[in] out is output handler.
1225 */
1226static void
1227trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1228{
1229 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1230 trp_print_node_name(node.name, out);
1231 return;
1232 }
1233 /* <status>--<flags> */
1234 trp_print_status(node.status, out);
1235 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001236 /* If the node is a case node, there is no space before the <name>
1237 * also case node has no flags.
1238 */
aPiecek61d062b2020-11-02 11:05:09 +01001239 if (node.name.type != TRD_NODE_CASE) {
1240 trp_print_flags(node.flags, out);
1241 ly_print_(out, " ");
1242 }
1243 /* <name> */
1244 trp_print_node_name(node.name, out);
1245}
1246
1247/**
aPiecek874ea4d2021-04-19 12:26:36 +02001248 * @brief Print alignment (spaces) instead of
1249 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001250 * @param[in] node contains items to print.
1251 * @param[in] out is output handler.
1252 */
1253static void
1254trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1255{
1256 uint32_t space = trp_get_flags_strlen(node.flags);
1257
1258 if (node.name.type == TRD_NODE_CASE) {
1259 /* :(<name> */
1260 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1261 } else if (node.name.type == TRD_NODE_CHOICE) {
1262 /* (<name> */
1263 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1264 } else {
1265 /* _<name> */
1266 space += strlen(" ");
1267 }
1268
1269 /* <name>
1270 * __
1271 */
1272 space += TRD_INDENT_LONG_LINE_BREAK;
1273
1274 ly_print_(out, "%*c", space, ' ');
1275}
1276
1277/**
1278 * @brief Print struct trt_node structure.
1279 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001280 * @param[in] pck package of functions for
1281 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001282 * @param[in] indent is the indent in node.
1283 * @param[in,out] out is output handler.
1284 */
1285static void
1286trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1287{
1288 ly_bool triple_dot;
1289 ly_bool divided;
1290 struct trt_cf_print cf_print_keys;
1291 struct trt_cf_print cf_print_iffeatures;
1292
1293 if (trp_node_is_empty(node)) {
1294 return;
1295 }
1296
1297 /* <status>--<flags> <name><opts> <type> <if-features> */
1298 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1299 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1300
1301 if (triple_dot) {
1302 trp_print_node_name(node.name, out);
1303 return;
1304 } else if (!divided) {
1305 trp_print_node_up_to_name(node, out);
1306 } else {
1307 trp_print_divided_node_up_to_name(node, out);
1308 }
1309
1310 /* <opts> */
1311 /* <name>___<opts>*/
1312 cf_print_keys.ctx = pck.tree_ctx;
1313 cf_print_keys.pf = pck.fps.print_keys;
1314
1315 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1316
1317 /* <opts>__<type> */
1318 if (indent.btw_opts_type > 0) {
1319 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1320 }
1321
1322 /* <type> */
1323 trp_print_type(node.type, out);
1324
1325 /* <type>__<iffeatures> */
1326 if (indent.btw_type_iffeatures > 0) {
1327 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1328 }
1329
1330 /* <iffeatures> */
1331 cf_print_iffeatures.ctx = pck.tree_ctx;
1332 cf_print_iffeatures.pf = pck.fps.print_features_names;
1333
1334 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1335}
1336
1337/**
aPiecek874ea4d2021-04-19 12:26:36 +02001338 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001339 * @param[in] ks is keyword statement to print.
1340 * @param[in,out] out is output handler
1341 */
1342static void
1343trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1344{
1345 switch (ks.type) {
1346 case TRD_KEYWORD_MODULE:
1347 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1348 return;
1349 case TRD_KEYWORD_SUBMODULE:
1350 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1351 return;
1352 default:
1353 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1354 switch (ks.type) {
1355 case TRD_KEYWORD_AUGMENT:
1356 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1357 break;
1358 case TRD_KEYWORD_RPC:
1359 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1360 break;
1361 case TRD_KEYWORD_NOTIF:
1362 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1363 break;
1364 case TRD_KEYWORD_GROUPING:
1365 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1366 break;
1367 case TRD_KEYWORD_YANG_DATA:
1368 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1369 break;
1370 default:
1371 break;
1372 }
1373 break;
1374 }
1375}
1376
1377/**
1378 * @brief Get string length of stored keyword.
1379 * @param[in] type is type of the keyword statement.
1380 * @return length of the keyword statement name.
1381 */
1382static size_t
1383trp_keyword_type_strlen(trt_keyword_type type)
1384{
1385 switch (type) {
1386 case TRD_KEYWORD_MODULE:
1387 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1388 case TRD_KEYWORD_SUBMODULE:
1389 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1390 case TRD_KEYWORD_AUGMENT:
1391 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1392 case TRD_KEYWORD_RPC:
1393 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1394 case TRD_KEYWORD_NOTIF:
1395 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1396 case TRD_KEYWORD_GROUPING:
1397 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1398 case TRD_KEYWORD_YANG_DATA:
1399 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1400 default:
1401 return 0;
1402 }
1403}
1404
1405/**
aPiecek874ea4d2021-04-19 12:26:36 +02001406 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001407 * @param[in] ks is keyword statement structure.
1408 * @param[in] mll is max line length.
1409 * @param[in,out] out is output handler.
1410 */
1411static void
1412trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1413{
1414 uint32_t ind_initial;
1415 uint32_t ind_divided;
1416 /* flag if path must be splitted to more lines */
1417 ly_bool linebreak_was_set;
1418 /* flag if at least one subpath was printed */
1419 ly_bool subpath_printed;
1420 /* the sum of the sizes of the substrings on the current line */
1421 uint32_t how_far;
1422 /* pointer to start of the subpath */
1423 const char *sub_ptr;
1424 /* size of subpath from sub_ptr */
1425 size_t sub_len;
1426
1427 if ((!ks.str) || (ks.str[0] == '\0')) {
1428 return;
1429 }
1430
1431 /* module name cannot be splitted */
1432 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1433 ly_print_(out, "%s", ks.str);
1434 return;
1435 }
1436
1437 /* after -> for trd_keyword_stmt_body do */
1438
1439 /* set begin indentation */
1440 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1441 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1442 linebreak_was_set = 0;
1443 subpath_printed = 0;
1444 how_far = 0;
1445 sub_ptr = ks.str;
1446 sub_len = 0;
1447
1448 while (sub_ptr[0] != '\0') {
1449 uint32_t ind;
1450 /* skip slash */
1451 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
1452 /* get position of the end of substr */
1453 tmp = strchr(tmp, '/');
1454 /* set correct size if this is a last substring */
1455 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1456 /* actualize sum of the substring's sizes on the current line */
1457 how_far += sub_len;
1458 /* correction due to colon character if it this is last substring */
1459 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1460 /* choose indentation which depends on
1461 * whether the string is printed on multiple lines or not
1462 */
1463 ind = linebreak_was_set ? ind_divided : ind_initial;
1464 if (ind + how_far <= mll) {
1465 /* printing before max line length */
1466 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1467 subpath_printed = 1;
1468 } else {
1469 /* printing on new line */
1470 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001471 /* first subpath is too long
1472 * but print it at first line anyway
1473 */
aPiecek61d062b2020-11-02 11:05:09 +01001474 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1475 subpath_printed = 1;
1476 continue;
1477 }
1478 ly_print_(out, "\n");
1479 ly_print_(out, "%*c", ind_divided, ' ');
1480 linebreak_was_set = 1;
1481 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1482 how_far = sub_len;
1483 subpath_printed = 1;
1484 }
1485 }
1486}
1487
1488/**
aPiecek874ea4d2021-04-19 12:26:36 +02001489 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001490 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001491 * @param[in] grp_has_data is flag only for grouping section.
1492 * Set to 1 if grouping section has some nodes.
1493 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001494 * @param[in,out] out is output handler.
1495 */
1496static void
aPiecekdc8fd572021-04-19 10:47:23 +02001497trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001498{
1499 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001500 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1501 return;
1502 } else {
1503 ly_print_(out, ":");
1504 }
aPiecek61d062b2020-11-02 11:05:09 +01001505 }
1506}
1507
1508/**
1509 * @brief Print entire struct trt_keyword_stmt structure.
1510 * @param[in] ks is item to print.
1511 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001512 * @param[in] grp_has_data is flag only for grouping section.
1513 * Set to 1 if grouping section has some nodes.
1514 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001515 * @param[in,out] out is output handler.
1516 */
1517static void
aPiecek874ea4d2021-04-19 12:26:36 +02001518trp_print_keyword_stmt(struct trt_keyword_stmt ks, size_t mll, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001519{
1520 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1521 return;
1522 }
1523 trt_print_keyword_stmt_begin(ks, out);
1524 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001525 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001526}
1527
aPiecek874ea4d2021-04-19 12:26:36 +02001528/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001529 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001530 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001531
1532/**
aPiecek874ea4d2021-04-19 12:26:36 +02001533 * @brief Printing one line including wrapper and node
1534 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001535 * @param[in] node is \<node\> representation.
1536 * @param[in] pck contains special printing functions callback.
1537 * @param[in] indent contains wrapper and indent in node numbers.
1538 * @param[in,out] out is output handler.
1539 */
1540static void
1541trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1542{
1543 trp_print_wrapper(indent.wrapper, out);
1544 trp_print_node(node, pck, indent.in_node, out);
1545}
1546
1547/**
aPiecek874ea4d2021-04-19 12:26:36 +02001548 * @brief Printing one line including wrapper and
1549 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001550 * @param[in] node is \<node\> representation.
1551 * @param[in] wr is wrapper for printing indentation before node.
1552 * @param[in] out is output handler.
1553 */
1554static void
1555trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1556{
1557 trp_print_wrapper(wr, out);
1558 trp_print_node_up_to_name(node, out);
1559}
1560
1561/**
aPiecek874ea4d2021-04-19 12:26:36 +02001562 * @brief Check if leafref target must be change to string 'leafref'
1563 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001564 * @param[in] node containing leafref target.
1565 * @param[in] wr is wrapper for printing indentation before node.
1566 * @param[in] mll is max line length.
1567 * @param[in] out is output handler.
1568 * @return true if leafref must be changed to string 'leafref'.
1569 */
1570static ly_bool
1571trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1572{
1573 struct ly_out_clb_arg *data;
1574
1575 if (node.type.type != TRD_TYPE_TARGET) {
1576 return 0;
1577 }
1578
1579 /* set ly_out to counting characters */
1580 data = out->method.clb.arg;
1581
1582 data->counter = 0;
1583 data->mode = TRD_CHAR_COUNT;
1584 /* count number of printed bytes */
1585 trp_print_wrapper(wr, out);
1586 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1587 trp_print_divided_node_up_to_name(node, out);
1588 data->mode = TRD_PRINT;
1589
1590 return data->counter + strlen(node.type.str) > mll;
1591}
1592
1593/**
1594 * @brief Get default indent in node based on node values.
1595 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001596 * @return Default indent in node assuming that the node
1597 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001598 */
1599static struct trt_indent_in_node
1600trp_default_indent_in_node(struct trt_node node)
1601{
1602 struct trt_indent_in_node ret;
1603
1604 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1605
1606 /* btw_name_opts */
1607 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1608
1609 /* btw_opts_type */
1610 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1611 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1612 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1613 TRD_INDENT_BEFORE_TYPE;
1614 } else {
1615 ret.btw_opts_type = 0;
1616 }
1617
1618 /* btw_type_iffeatures */
1619 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1620
1621 return ret;
1622}
1623
1624/**
1625 * @brief Setting linebreaks in trt_indent_in_node.
1626 *
1627 * The order where the linebreak tag can be placed is from the end.
1628 *
aPiecek874ea4d2021-04-19 12:26:36 +02001629 * @param[in] indent containing alignment lengths
1630 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001631 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001632 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1633 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001634 */
1635static struct trt_indent_in_node
1636trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1637{
1638 /* somewhere must be set a line break in node */
1639 struct trt_indent_in_node ret = indent;
1640
1641 /* gradually break the node from the end */
1642 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1643 ret.btw_type_iffeatures = TRD_LINEBREAK;
1644 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1645 ret.btw_opts_type = TRD_LINEBREAK;
1646 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1647 /* set line break between name and opts */
1648 ret.btw_name_opts = TRD_LINEBREAK;
1649 } else {
1650 /* it is not possible to place a more line breaks,
1651 * unfortunately the max_line_length constraint is violated
1652 */
1653 ret.type = TRD_INDENT_IN_NODE_FAILED;
1654 }
1655 return ret;
1656}
1657
1658/**
1659 * @brief Get the first half of the node based on the linebreak mark.
1660 *
1661 * Items in the second half of the node will be empty.
1662 *
1663 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001664 * @param[in] indent contains information in which part of the \<node\>
1665 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001666 * @return first half of the node, indent is unchanged.
1667 */
1668static struct trt_pair_indent_node
1669trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1670{
1671 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1672
1673 if (indent.btw_name_opts == TRD_LINEBREAK) {
1674 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1675 ret.node.type = TRP_EMPTY_TRT_TYPE;
1676 ret.node.iffeatures = 0;
1677 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1678 ret.node.type = TRP_EMPTY_TRT_TYPE;
1679 ret.node.iffeatures = 0;
1680 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1681 ret.node.iffeatures = 0;
1682 }
1683
1684 return ret;
1685}
1686
1687/**
1688 * @brief Get the second half of the node based on the linebreak mark.
1689 *
1690 * Items in the first half of the node will be empty.
1691 * Indentations belonging to the first node will be reset to zero.
1692 *
1693 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001694 * @param[in] indent contains information in which part of the \<node\>
1695 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001696 * @return second half of the node, indent is newly set.
1697 */
1698static struct trt_pair_indent_node
1699trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1700{
1701 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1702
1703 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001704 /* Logically, the information up to token <opts> should
1705 * be deleted, but the the trp_print_node function needs it to
1706 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001707 */
1708 ret.indent.btw_name_opts = 0;
1709 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1710 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1711 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1712 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1713 ret.indent.btw_name_opts = 0;
1714 ret.indent.btw_opts_type = 0;
1715 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1716 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1717 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1718 ret.node.type = TRP_EMPTY_TRT_TYPE;
1719 ret.indent.btw_name_opts = 0;
1720 ret.indent.btw_opts_type = 0;
1721 ret.indent.btw_type_iffeatures = 0;
1722 }
1723 return ret;
1724}
1725
1726/**
1727 * @brief Get the correct alignment for the node.
1728 *
aPiecek874ea4d2021-04-19 12:26:36 +02001729 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001730 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001731 *
1732 * @param[in] node is \<node\> representation.
1733 * @param[in] pck contains speciall callback functions for printing.
1734 * @param[in] indent contains wrapper and indent in node numbers.
1735 * @param[in] mll is max line length.
1736 * @param[in,out] cnt counting number of characters to print.
1737 * @param[in,out] out is output handler.
1738 * @return pair of node and indentation numbers of that node.
1739 */
1740static struct trt_pair_indent_node
1741trp_try_normal_indent_in_node_(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, size_t *cnt, struct ly_out *out)
1742{
1743 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1744
1745 trp_print_line(node, pck, indent, out);
1746
1747 if (*cnt <= mll) {
1748 /* success */
1749 return ret;
1750 } else {
1751 ret.indent = trp_indent_in_node_place_break(ret.indent);
1752 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1753 /* erase information in node due to line break */
1754 ret = trp_first_half_node(node, ret.indent);
1755 /* check if line fits, recursive call */
1756 *cnt = 0;
1757 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1758 /* make sure that the result will be with the status divided
1759 * or eventually with status failed */
1760 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1761 }
1762 return ret;
1763 }
1764}
1765
1766/**
1767 * @brief Get the correct alignment for the node.
1768 *
1769 * @param[in] node is \<node\> representation.
1770 * @param[in] pck contains speciall callback functions for printing.
1771 * @param[in] indent contains wrapper and indent in node numbers.
1772 * @param[in] mll is max line length.
1773 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001774 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1775 * line, some indent variable has negative value as a line break sign.
1776 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1777 * all indent variables values has non-negative number.
1778 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1779 * line, all indent variables has negative or zero values,
1780 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001781 */
1782static struct trt_pair_indent_node
1783trp_try_normal_indent_in_node(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, struct ly_out *out)
1784{
1785 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1786 struct ly_out_clb_arg *data;
1787
1788 /* set ly_out to counting characters */
1789 data = out->method.clb.arg;
1790
1791 data->counter = 0;
1792 data->mode = TRD_CHAR_COUNT;
1793 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1794 data->mode = TRD_PRINT;
1795
1796 return ret;
1797}
1798
1799/**
aPiecek01598c02021-04-23 14:18:24 +02001800 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001801 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001802 * @param[in] node is node representation.
1803 * @param[in] ppck contains speciall callback functions for printing.
1804 * @param[in] ipck contains wrapper and indent in node numbers.
1805 * @param[in] mll is max line length.
1806 * @param[in,out] out is output handler.
1807 */
1808static void
1809trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1810{
1811 ly_bool entire_node_was_printed;
1812 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1813
1814 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1815 /* nothing can be done, continue as usual */
1816 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1817 }
1818
1819 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1820 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1821
1822 if (!entire_node_was_printed) {
1823 ly_print_(out, "\n");
1824 /* continue with second half node */
1825 ind_node = trp_second_half_node(node, ind_node.indent);
1826 /* continue with printing node */
1827 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1828 } else {
1829 return;
1830 }
1831}
1832
1833/**
aPiecek874ea4d2021-04-19 12:26:36 +02001834 * @brief Printing of the wrapper and the whole node,
1835 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001836 * @param[in] node is node representation.
1837 * @param[in] ppck contains speciall callback functions for printing.
1838 * @param[in] ipck contains wrapper and indent in node numbers.
1839 * @param[in] mll is max line length.
1840 * @param[in,out] out is output handler.
1841 */
1842static void
1843trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1844{
1845 struct trt_pair_indent_node ind_node1;
1846 struct trt_pair_indent_node ind_node2;
1847 struct trt_pck_indent tmp;
1848
1849 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1850 node.type.type = TRD_TYPE_LEAFREF;
1851 }
1852
1853 /* check if normal indent is possible */
1854 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1855
1856 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1857 /* node fits to one line */
1858 trp_print_line(node, ppck, ipck, out);
1859 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1860 /* node will be divided */
1861 /* print first half */
1862 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1863 /* pretend that this is normal node */
1864 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1865
1866 trp_print_line(ind_node1.node, ppck, tmp, out);
1867 ly_print_(out, "\n");
1868
1869 /* continue with second half on new line */
1870 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1871 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1872
1873 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1874 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1875 /* node name is too long */
1876 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1877
1878 if (trp_node_body_is_empty(node)) {
1879 return;
1880 } else {
1881 ly_print_(out, "\n");
1882
1883 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1884 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1885 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1886
1887 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1888 }
1889
1890 }
1891}
1892
aPiecek874ea4d2021-04-19 12:26:36 +02001893/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001894 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001895 *********************************************************************/
1896
1897/**
1898 * @brief Get nodetype.
1899 * @param[in] node is any lysp_node.
1900 */
1901static uint16_t
1902trop_nodetype(const void *node)
1903{
1904 return ((const struct lysp_node *)node)->nodetype;
1905}
1906
1907/**
1908 * @brief Get sibling.
1909 * @param[in] node is any lysp_node.
1910 */
1911static const void *
1912trop_next(const void *node)
1913{
1914 return ((const struct lysp_node *)node)->next;
1915}
1916
1917/**
1918 * @brief Get parent.
1919 * @param[in] node is any lysp_node.
1920 */
1921static const void *
1922trop_parent(const void *node)
1923{
1924 return ((const struct lysp_node *)node)->parent;
1925}
1926
1927/**
1928 * @brief Try to get child.
1929 * @param[in] node is any lysp_node.
1930 */
1931static const void *
1932trop_child(const void *node)
1933{
1934 return lysp_node_child(node);
1935}
1936
1937/**
1938 * @brief Try to get action.
1939 * @param[in] node is any lysp_node.
1940 */
1941static const void *
1942trop_actions(const void *node)
1943{
1944 return lysp_node_actions(node);
1945}
1946
1947/**
1948 * @brief Try to get action.
1949 * @param[in] node must be of type lysp_node_action.
1950 */
1951static const void *
1952trop_action_input(const void *node)
1953{
1954 return &((const struct lysp_node_action *)node)->input;
1955}
1956
1957/**
1958 * @brief Try to get action.
1959 * @param[in] node must be of type lysp_node_action.
1960 */
1961static const void *
1962trop_action_output(const void *node)
1963{
1964 return &((const struct lysp_node_action *)node)->output;
1965}
1966
1967/**
1968 * @brief Try to get action.
1969 * @param[in] node is any lysp_node.
1970 */
1971static const void *
1972trop_notifs(const void *node)
1973{
1974 return lysp_node_notifs(node);
1975}
1976
1977/**
aPiecek01598c02021-04-23 14:18:24 +02001978 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02001979 * which are adapted to lysp nodes.
1980 */
1981static struct tro_getters
1982trop_init_getters()
1983{
1984 return (struct tro_getters) {
1985 .nodetype = trop_nodetype,
1986 .next = trop_next,
1987 .parent = trop_parent,
1988 .child = trop_child,
1989 .actions = trop_actions,
1990 .action_input = trop_action_input,
1991 .action_output = trop_action_output,
1992 .notifs = trop_notifs
1993 };
1994}
1995
aPiecek3f247652021-04-19 13:40:25 +02001996/**
1997 * @brief Get nodetype.
1998 * @param[in] node is any lysc_node.
1999 */
2000static uint16_t
2001troc_nodetype(const void *node)
2002{
2003 return ((const struct lysc_node *)node)->nodetype;
2004}
2005
2006/**
2007 * @brief Get sibling.
2008 * @param[in] node is any lysc_node.
2009 */
2010static const void *
2011troc_next(const void *node)
2012{
2013 return ((const struct lysc_node *)node)->next;
2014}
2015
2016/**
2017 * @brief Get parent.
2018 * @param[in] node is any lysc_node.
2019 */
2020static const void *
2021troc_parent(const void *node)
2022{
2023 return ((const struct lysc_node *)node)->parent;
2024}
2025
2026/**
2027 * @brief Try to get child.
2028 * @param[in] node is any lysc_node.
2029 */
2030static const void *
2031troc_child(const void *node)
2032{
2033 return lysc_node_child(node);
2034}
2035
2036/**
2037 * @brief Try to get action.
2038 * @param[in] node is any lysc_node.
2039 */
2040static const void *
2041troc_actions(const void *node)
2042{
2043 return lysc_node_actions(node);
2044}
2045
2046/**
2047 * @brief Try to get action.
2048 * @param[in] node must be of type lysc_node_action.
2049 */
2050static const void *
2051troc_action_input(const void *node)
2052{
2053 return &((const struct lysc_node_action *)node)->input;
2054}
2055
2056/**
2057 * @brief Try to get action.
2058 * @param[in] node must be of type lysc_node_action.
2059 */
2060static const void *
2061troc_action_output(const void *node)
2062{
2063 return &((const struct lysc_node_action *)node)->output;
2064}
2065
2066/**
2067 * @brief Try to get action.
2068 * @param[in] node is any lysc_node.
2069 */
2070static const void *
2071troc_notifs(const void *node)
2072{
2073 return lysc_node_notifs(node);
2074}
2075
2076/**
aPiecek01598c02021-04-23 14:18:24 +02002077 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002078 * which are adapted to lysc nodes.
2079 */
2080static struct tro_getters
2081troc_init_getters()
2082{
2083 return (struct tro_getters) {
2084 .nodetype = troc_nodetype,
2085 .next = troc_next,
2086 .parent = troc_parent,
2087 .child = troc_child,
2088 .actions = troc_actions,
2089 .action_input = troc_action_input,
2090 .action_output = troc_action_output,
2091 .notifs = troc_notifs
2092 };
2093}
2094
aPiecekef1e58e2021-04-19 13:19:44 +02002095/**********************************************************************
2096 * tro functions
2097 *********************************************************************/
2098
2099/**
2100 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002101 *
2102 * This is a general algorithm that is able to
2103 * work with lysp_node or lysc_node.
2104 *
2105 * @param[in] node points to lysp_node or lysc_node.
2106 * @param[in] lysc_tree flag to determine what type the @p node is.
2107 * If set to true, then @p points to lysc_node otherwise lysp_node.
2108 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002109 */
2110static const void *
aPiecek3f247652021-04-19 13:40:25 +02002111tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002112{
2113 struct tro_getters get;
2114 const void *tmp, *parent;
2115 const void *ret;
2116
2117 assert(node);
2118
aPiecek3f247652021-04-19 13:40:25 +02002119 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002120
2121 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2122 if ((tmp = get.next(node))) {
2123 /* next action exists */
2124 ret = tmp;
2125 } else if ((parent = get.parent(node))) {
2126 /* maybe if notif exists as sibling */
2127 ret = get.notifs(parent);
2128 } else {
2129 ret = NULL;
2130 }
2131 } else if (get.nodetype(node) & LYS_INPUT) {
2132 if ((parent = get.parent(node))) {
2133 /* if output action has data */
2134 if (get.child(get.action_output(parent))) {
2135 /* then next sibling is output action */
2136 ret = get.action_output(parent);
2137 } else {
2138 /* input action cannot have siblings other
2139 * than output action.
2140 */
2141 ret = NULL;
2142 }
2143 } else {
2144 /* there is no way how to get output action */
2145 ret = NULL;
2146 }
2147 } else if (get.nodetype(node) & LYS_OUTPUT) {
2148 /* output action cannot have siblings */
2149 ret = NULL;
2150 } else if (get.nodetype(node) & LYS_NOTIF) {
2151 /* must have as a sibling only notif */
2152 ret = get.next(node);
2153 } else {
2154 /* for rest of nodes */
2155 if ((tmp = get.next(node))) {
2156 /* some sibling exists */
2157 ret = tmp;
2158 } else if ((parent = get.parent(node))) {
2159 /* Action and notif are siblings too.
2160 * They can be reached through parent.
2161 */
2162 if ((tmp = get.actions(parent))) {
2163 /* next sibling is action */
2164 ret = tmp;
2165 } else if ((tmp = get.notifs(parent))) {
2166 /* next sibling is notif */
2167 ret = tmp;
2168 } else {
2169 /* sibling not exists */
2170 ret = NULL;
2171 }
2172 } else {
2173 /* sibling not exists */
2174 ret = NULL;
2175 }
2176 }
2177
2178 return ret;
2179}
2180
2181/**
2182 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002183 *
2184 * This is a general algorithm that is able to
2185 * work with lysp_node or lysc_node.
2186 *
2187 * @param[in] node points to lysp_node or lysc_node.
2188 * @param[in] lysc_tree flag to determine what type the @p node is.
2189 * If set to true, then @p points to lysc_node otherwise lysp_node.
2190 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002191 */
2192static const void *
aPiecek3f247652021-04-19 13:40:25 +02002193tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002194{
2195 struct tro_getters get;
2196 const void *tmp;
2197 const void *ret;
2198
2199 assert(node);
2200
aPiecek3f247652021-04-19 13:40:25 +02002201 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002202
2203 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2204 if (get.child(get.action_input(node))) {
2205 /* go to LYS_INPUT */
2206 ret = get.action_input(node);
2207 } else if (get.child(get.action_output(node))) {
2208 /* go to LYS_OUTPUT */
2209 ret = get.action_output(node);
2210 } else {
2211 /* input action and output action have no data */
2212 ret = NULL;
2213 }
2214 } else {
2215 if ((tmp = get.child(node))) {
2216 ret = tmp;
2217 } else {
2218 /* current node can't have children or has no children */
2219 /* but maybe has some actions or notifs */
2220 if ((tmp = get.actions(node))) {
2221 ret = tmp;
2222 } else if ((tmp = get.notifs(node))) {
2223 ret = tmp;
2224 } else {
2225 ret = NULL;
2226 }
2227 }
2228 }
2229
2230 return ret;
2231}
2232
2233/**
aPiecek3f247652021-04-19 13:40:25 +02002234 * @brief Get new trt_parent_cache if we apply the transfer
2235 * to the child node in the tree.
2236 * @param[in] ca is parent cache for current node.
2237 * @param[in] tc contains current tree node.
2238 * @return Cache for the current node.
2239 */
2240static struct trt_parent_cache
2241tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2242{
2243 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2244
2245 if (!tc->lysc_tree) {
2246 const struct lysp_node *pn = tc->pn;
2247
2248 ret.ancestor =
2249 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2250 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2251 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2252 ca.ancestor;
2253
2254 ret.lys_status =
2255 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2256 ca.lys_status;
2257
2258 ret.lys_config =
2259 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2260 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2261 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2262 ca.lys_config;
2263
2264 ret.last_list =
2265 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2266 ca.last_list;
2267 }
2268
2269 return ret;
2270}
2271
2272/**
aPiecekef1e58e2021-04-19 13:19:44 +02002273 * @brief Transformation of the Schema nodes flags to
2274 * Tree diagram \<status\>.
2275 * @param[in] flags is node's flags obtained from the tree.
2276 */
2277static trt_status_type
2278tro_flags2status(uint16_t flags)
2279{
2280 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2281 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2282 TRD_STATUS_TYPE_CURRENT;
2283}
2284
2285/**
2286 * @brief Transformation of the Schema nodes flags to Tree diagram
2287 * \<flags\> but more specifically 'ro' or 'rw'.
2288 * @param[in] flags is node's flags obtained from the tree.
2289 */
2290static trt_flags_type
2291tro_flags2config(uint16_t flags)
2292{
2293 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2294 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2295 TRD_FLAGS_TYPE_EMPTY;
2296}
2297
2298/**
aPiecek3f247652021-04-19 13:40:25 +02002299 * @brief Print current node's iffeatures.
2300 * @param[in] tc is tree context.
2301 * @param[in,out] out is output handler.
2302 */
2303static void
2304tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2305{
2306 const struct lysp_qname *iffs;
2307
2308 iffs = tc->lysc_tree ?
2309 TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures :
2310 tc->pn->iffeatures;
2311
2312 LY_ARRAY_COUNT_TYPE i;
2313
2314 LY_ARRAY_FOR(iffs, i) {
2315 if (i == 0) {
2316 ly_print_(out, "%s", iffs[i].str);
2317 } else {
2318 ly_print_(out, ",%s", iffs[i].str);
2319 }
2320 }
2321
2322}
2323
2324/**
2325 * @brief Print current list's keys.
2326 *
2327 * Well, actually printing keys in the lysp_tree is trivial,
2328 * because char* points to all keys. However, special functions have
2329 * been reserved for this, because in principle the list of elements
2330 * can have more implementations.
2331 *
2332 * @param[in] tc is tree context.
2333 * @param[in,out] out is output handler.
2334 */
2335static void
2336tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2337{
2338 const struct lysp_node_list *list;
2339
2340 list = tc->lysc_tree ?
2341 (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn) :
2342 (const struct lysp_node_list *)tc->pn;
2343 assert(list->nodetype & LYS_LIST);
2344
2345 if (trg_charptr_has_data(list->key)) {
2346 ly_print_(out, "%s", list->key);
2347 }
2348}
2349
2350/**
2351 * @brief Get rpcs section if exists.
2352 * @param[in,out] tc is tree context.
2353 * @return Section representation if it exists. The @p tc is modified
2354 * and his pointer points to the first node in rpcs section.
2355 * @return Empty section representation otherwise.
2356 */
2357static struct trt_keyword_stmt
2358tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2359{
aPiecek9f792e52021-04-21 08:33:56 +02002360 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002361 const void *actions;
2362
2363 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002364 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002365 if (actions) {
2366 tc->cn = actions;
2367 }
2368 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002369 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002370 if (actions) {
2371 tc->pn = actions;
2372 tc->tpn = tc->pn;
2373 }
2374 }
2375
2376 if (actions) {
2377 tc->section = TRD_SECT_RPCS;
2378 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2379 } else {
2380 return TRP_EMPTY_KEYWORD_STMT;
2381 }
2382}
2383
2384/**
2385 * @brief Get notification section if exists
2386 * @param[in,out] tc is tree context.
2387 * @return Section representation if it exists.
2388 * The @p tc is modified and his pointer points to the
2389 * first node in notification section.
2390 * @return Empty section representation otherwise.
2391 */
2392static struct trt_keyword_stmt
2393tro_modi_get_notifications(struct trt_tree_ctx *tc)
2394{
aPiecek9f792e52021-04-21 08:33:56 +02002395 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002396 const void *notifs;
2397
2398 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002399 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002400 if (notifs) {
2401 tc->cn = notifs;
2402 }
2403 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002404 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002405 if (notifs) {
2406 tc->pn = notifs;
2407 tc->tpn = tc->pn;
2408 }
2409 }
2410
2411 if (notifs) {
2412 tc->section = TRD_SECT_NOTIF;
2413 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2414 } else {
2415 return TRP_EMPTY_KEYWORD_STMT;
2416 }
2417}
2418
2419/**
aPiecek96baa7f2021-04-23 12:32:00 +02002420 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002421 *
2422 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002423 * @param[in] u is index to the array of extensions (lysc_ext_instance
2424 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002425 * @return Section representation if it exists.
2426 * @return Empty section representation otherwise.
2427 */
2428static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002429tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002430{
aPiecek96baa7f2021-04-23 12:32:00 +02002431 assert(tc);
2432 const void *node;
2433 const char *yang_data_name;
2434
2435 if (tc->lysc_tree) {
2436 struct lysc_ext_instance *exts;
2437 struct lysc_ext_substmt *substmts;
2438
2439 exts = tc->cmod->exts;
2440 substmts = exts[u].substmts;
2441 if (!substmts) {
2442 return TRP_EMPTY_KEYWORD_STMT;
2443 }
2444 node = *(const struct lysc_node **)substmts->storage;
2445 yang_data_name = exts[u].argument;
2446 } else {
2447 struct lysp_ext_instance *exts;
2448
2449 exts = tc->pmod->exts;
2450 node = exts[u].parsed;
2451 yang_data_name = exts[u].argument;
2452 }
2453
2454 if (tc->lysc_tree) {
2455 tc->cn = node;
2456 } else {
2457 tc->tpn_ext = &tc->pmod->exts[u];
2458 tc->pn = node;
2459 }
2460
2461 if (node) {
2462 tc->section = TRD_SECT_YANG_DATA;
2463 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2464 } else {
2465 return TRP_EMPTY_KEYWORD_STMT;
2466 }
aPiecekef1e58e2021-04-19 13:19:44 +02002467}
2468
2469/**
2470 * @brief Get name of the module.
2471 * @param[in] tc is context of the tree.
2472 */
2473static struct trt_keyword_stmt
2474tro_read_module_name(const struct trt_tree_ctx *tc)
2475{
aPiecek9f792e52021-04-21 08:33:56 +02002476 assert(tc);
2477
2478 struct trt_keyword_stmt ret;
2479
2480 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2481 TRD_KEYWORD_SUBMODULE :
2482 TRD_KEYWORD_MODULE;
2483
2484 ret.str = !tc->lysc_tree ?
2485 LYSP_MODULE_NAME(tc->pmod) :
2486 tc->cmod->mod->name;
2487
2488 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002489}
2490
aPiecekb8d5a0a2021-05-20 08:20:24 +02002491/**
2492 * @brief Create implicit "case" node as parent of @p node.
2493 * @param[in] node child of implicit case node.
2494 * @return The case node ready to print.
2495 */
2496static struct trt_node
2497tro_create_implicit_case_node(struct trt_node node)
2498{
2499 struct trt_node ret;
2500
2501 ret.status = node.status;
2502 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2503 ret.name.type = TRD_NODE_CASE;
2504 ret.name.module_prefix = node.name.module_prefix;
2505 ret.name.str = node.name.str;
2506 ret.type = TRP_EMPTY_TRT_TYPE;
2507 ret.iffeatures = node.iffeatures;
2508 ret.last_one = node.last_one;
2509
2510 return ret;
2511}
2512
aPiecekef1e58e2021-04-19 13:19:44 +02002513/**********************************************************************
2514 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002515 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002516
2517/**
aPiecek61d062b2020-11-02 11:05:09 +01002518 * @brief Check if list statement has keys.
2519 * @param[in] pn is pointer to the list.
2520 * @return 1 if has keys, otherwise 0.
2521 */
2522static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002523trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002524{
aPiecekef1e58e2021-04-19 13:19:44 +02002525 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002526}
2527
2528/**
2529 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002530 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002531 * @return 1 if has if-features, otherwise 0.
2532 */
2533static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002534trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002535{
2536 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002537 const struct lysp_qname *iffs;
2538
aPiecek61d062b2020-11-02 11:05:09 +01002539 ly_bool ret = 0;
2540
aPiecekef1e58e2021-04-19 13:19:44 +02002541 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002542 LY_ARRAY_FOR(iffs, u) {
2543 ret = 1;
2544 break;
2545 }
2546 return ret;
2547}
2548
2549/**
2550 * @brief Find out if leaf is also the key in last list.
2551 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002552 * @param[in] ca_last_list is pointer to last visited list.
2553 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002554 * @return 1 if leaf is also the key, otherwise 0.
2555 */
2556static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002557trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002558{
2559 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2560 const struct lysp_node_list *list = ca_last_list;
2561
2562 if (!list) {
2563 return 0;
2564 }
2565 return trg_charptr_has_data(list->key) ?
2566 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2567}
2568
2569/**
2570 * @brief Check if container's type is presence.
2571 * @param[in] pn is pointer to container.
2572 * @return 1 if container has presence statement, otherwise 0.
2573 */
2574static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002575trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002576{
2577 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2578}
2579
2580/**
2581 * @brief Get leaflist's path without lysp_node type control.
2582 * @param[in] pn is pointer to the leaflist.
2583 */
2584static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002585trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002586{
2587 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2588
2589 return list->type.path ? list->type.path->expr : NULL;
2590}
2591
2592/**
2593 * @brief Get leaflist's type name without lysp_node type control.
2594 * @param[in] pn is pointer to the leaflist.
2595 */
2596static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002597trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002598{
2599 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2600
2601 return list->type.name;
2602}
2603
2604/**
2605 * @brief Get leaf's path without lysp_node type control.
2606 * @param[in] pn is pointer to the leaf node.
2607 */
2608static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002609trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002610{
2611 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2612
2613 return leaf->type.path ? leaf->type.path->expr : NULL;
2614}
2615
2616/**
2617 * @brief Get leaf's type name without lysp_node type control.
2618 * @param[in] pn is pointer to the leaf's type name.
2619 */
2620static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002621trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002622{
2623 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2624
2625 return leaf->type.name;
2626}
2627
2628/**
aPiecek874ea4d2021-04-19 12:26:36 +02002629 * @brief Get pointer to data using node type specification
2630 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002631 *
aPiecek874ea4d2021-04-19 12:26:36 +02002632 * @param[in] flags is node type specification.
2633 * If it is the correct node, the getter function is called.
2634 * @param[in] f is getter function which provides the desired
2635 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002636 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002637 * @return NULL if node has wrong type or getter function return
2638 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002639 * @return Pointer to desired char pointer obtained from the node.
2640 */
2641static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002642trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002643{
2644 if (pn->nodetype & flags) {
2645 const char *ret = f(pn);
2646 return trg_charptr_has_data(ret) ? ret : NULL;
2647 } else {
2648 return NULL;
2649 }
2650}
2651
2652/**
aPiecek61d062b2020-11-02 11:05:09 +01002653 * @brief Resolve \<status\> of the current node.
2654 * @param[in] nodetype is node's type obtained from the tree.
2655 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002656 * @param[in] ca_lys_status is inherited status
2657 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002658 * @return The status type.
2659 */
2660static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002661trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002662{
2663 /* LYS_INPUT and LYS_OUTPUT is special case */
2664 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002665 return tro_flags2status(ca_lys_status);
2666 /* if ancestor's status is deprc or obslt
2667 * and also node's status is not set
2668 */
aPiecek61d062b2020-11-02 11:05:09 +01002669 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2670 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002671 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002672 } else {
2673 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002674 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002675 }
2676}
2677
2678/**
2679 * @brief Resolve \<flags\> of the current node.
2680 * @param[in] nodetype is node's type obtained from the tree.
2681 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002682 * @param[in] ca_ancestor is ancestor type obtained
2683 * from trt_parent_cache.
2684 * @param[in] ca_lys_config is inherited config item
2685 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002686 * @return The flags type.
2687 */
2688static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002689trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002690{
2691 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2692 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2693 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2694 return TRD_FLAGS_TYPE_RO;
2695 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2696 return TRD_FLAGS_TYPE_RO;
2697 } else if (nodetype & LYS_NOTIF) {
2698 return TRD_FLAGS_TYPE_NOTIF;
2699 } else if (nodetype & LYS_USES) {
2700 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2701 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2702 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002703 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002704 /* config is not set. Look at ancestor's config */
2705 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002706 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002707 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002708 }
2709}
2710
2711/**
2712 * @brief Resolve node type of the current node.
2713 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002714 * @param[in] ca_last_list is pointer to the last visited list.
2715 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002716 */
2717static trt_node_type
aPiecekef1e58e2021-04-19 13:19:44 +02002718trop_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002719{
2720 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2721 return TRD_NODE_ELSE;
2722 } else if (pn->nodetype & LYS_CASE) {
2723 return TRD_NODE_CASE;
2724 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2725 return TRD_NODE_OPTIONAL_CHOICE;
2726 } else if (pn->nodetype & LYS_CHOICE) {
2727 return TRD_NODE_CHOICE;
aPiecekef1e58e2021-04-19 13:19:44 +02002728 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002729 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002730 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002731 return TRD_NODE_KEYS;
2732 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2733 return TRD_NODE_LISTLEAFLIST;
2734 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2735 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002736 } 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 +01002737 return TRD_NODE_OPTIONAL;
2738 } else {
2739 return TRD_NODE_ELSE;
2740 }
2741}
2742
2743/**
aPiecekef1e58e2021-04-19 13:19:44 +02002744 * @brief Resolve \<type\> of the current node.
2745 * @param[in] pn is current node.
2746 */
2747static struct trt_type
2748trop_resolve_type(const struct lysp_node *pn)
2749{
2750 const char *tmp = NULL;
2751
2752 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2753 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2754 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2755 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2756 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2757 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2758 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2759 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2760 } else if (pn->nodetype == LYS_ANYDATA) {
2761 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2762 } else if (pn->nodetype & LYS_ANYXML) {
2763 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2764 } else {
2765 return TRP_EMPTY_TRT_TYPE;
2766 }
2767}
2768
2769/**
aPiecek61d062b2020-11-02 11:05:09 +01002770 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002771 * @param[in] ca contains stored important data
2772 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002773 * @param[in] tc is context of the tree.
2774 */
2775static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002776trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002777{
aPiecekef1e58e2021-04-19 13:19:44 +02002778 const struct lysp_node *pn;
2779 struct trt_node ret;
2780
aPiecek61d062b2020-11-02 11:05:09 +01002781 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002782
2783 pn = tc->pn;
2784 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002785
2786 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002787 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002788
2789 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2790 /* <flags> */
aPiecekef1e58e2021-04-19 13:19:44 +02002791 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002792
2793 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2794 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2795 /* set type of the node */
aPiecekef1e58e2021-04-19 13:19:44 +02002796 ret.name.type = trop_resolve_node_type(pn, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002797
2798 /* TODO: ret.name.module_prefix is not supported right now. */
2799 ret.name.module_prefix = NULL;
2800
2801 /* set node's name */
2802 ret.name.str = pn->name;
2803
2804 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002805 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002806
2807 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002808 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002809
aPiecek3f247652021-04-19 13:40:25 +02002810 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002811
2812 return ret;
2813}
2814
aPiecekef1e58e2021-04-19 13:19:44 +02002815/**
2816 * @brief Find out if the current node has siblings.
2817 * @param[in] tc is context of the tree.
2818 * @return 1 if sibling exists otherwise 0.
2819 */
2820static ly_bool
2821trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2822{
aPiecek3f247652021-04-19 13:40:25 +02002823 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002824}
2825
aPiecek96baa7f2021-04-23 12:32:00 +02002826/**
2827 * @brief Print all yang-data sections and print three dots instead
2828 * of nodes.
2829 * @param[in] exts is array of YANG extension instances from parsed
2830 * module (@ref sizedarrays).
2831 * @param[in] mll is maximum number of characters that can be printed
2832 * on one line.
2833 * @param[in,out] out is output handler.
2834 */
2835static void
2836trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2837{
2838 struct trt_keyword_stmt ks;
2839 LY_ARRAY_COUNT_TYPE u;
2840 struct trt_wrapper wr;
2841
2842 if (!exts) {
2843 return;
2844 }
2845
2846 ly_print_(out, "\n");
2847 ks.type = TRD_KEYWORD_YANG_DATA;
2848 wr = TRP_INIT_WRAPPER_BODY;
2849
2850 LY_ARRAY_FOR(exts, u) {
2851 ly_print_(out, "\n");
2852
2853 /* yang-data <yang-data-name>: */
2854 ks.str = exts[u].argument;
2855 trp_print_keyword_stmt(ks, mll, 0, out);
2856 ly_print_(out, "\n");
2857
2858 /* ... */
2859 trp_print_wrapper(wr, out);
2860 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2861 }
2862}
2863
aPiecek874ea4d2021-04-19 12:26:36 +02002864/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002865 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002866 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002867
2868/**
aPiecek874ea4d2021-04-19 12:26:36 +02002869 * @brief Change current node pointer to its parent
2870 * but only if parent exists.
2871 * @param[in,out] tc is tree context.
2872 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002873 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002874 * @return 0 if the node did not have parents.
2875 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002876 */
2877static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002878trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002879{
2880 assert(tc && tc->pn);
2881 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002882 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002883 tc->pn = tc->pn->parent;
2884 return 1;
2885 } else {
2886 return 0;
2887 }
2888}
2889
2890/**
aPiecek874ea4d2021-04-19 12:26:36 +02002891 * @brief Change the current node pointer to its child
2892 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002893 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002894 * @param[in,out] tc is context of the tree.
2895 * Contains pointer to the current node.
2896 * @return Non-empty \<node\> representation of the current
2897 * node's child. The @p tc is modified.
2898 * @return Empty \<node\> representation if child don't exists.
2899 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002900 */
2901static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002902trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002903{
aPiecekef1e58e2021-04-19 13:19:44 +02002904 const struct lysp_node *tmp;
2905
aPiecek61d062b2020-11-02 11:05:09 +01002906 assert(tc && tc->pn);
2907
aPiecek3f247652021-04-19 13:40:25 +02002908 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002909 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02002910 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01002911 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002912 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002913 }
2914}
2915
2916/**
aPiecek874ea4d2021-04-19 12:26:36 +02002917 * @brief Change the current node pointer to the first child of node's
2918 * parent. If current node is already first sibling/child then nothing
2919 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01002920 * @param[in,out] tc is tree context.
2921 */
2922static void
aPiecekef1e58e2021-04-19 13:19:44 +02002923trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002924{
aPiecek9f792e52021-04-21 08:33:56 +02002925 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01002926
aPiecekef1e58e2021-04-19 13:19:44 +02002927 if (trop_modi_parent(tc)) {
2928 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002929 } else {
2930 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01002931 switch (tc->section) {
2932 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02002933 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02002934 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002935 break;
2936 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02002937 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02002938 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002939 break;
2940 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02002941 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02002942 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002943 break;
2944 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02002945 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02002946 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002947 break;
2948 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02002949 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02002950 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002951 break;
2952 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02002953 /* tpn in this case is of type lysp_ext_instance */
2954 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01002955 break;
aPiecek96baa7f2021-04-23 12:32:00 +02002956 default:
2957 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01002958 }
aPiecek61d062b2020-11-02 11:05:09 +01002959 }
2960}
2961
2962/**
aPiecek874ea4d2021-04-19 12:26:36 +02002963 * @brief Change the pointer to the current node to its next sibling
2964 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002965 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002966 * @param[in,out] tc is tree context.
2967 * Contains pointer to the current node.
2968 * @return Non-empty \<node\> representation if sibling exists.
2969 * The @p tc is modified.
2970 * @return Empty \<node\> representation otherwise.
2971 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002972 */
2973static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002974trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002975{
aPiecekef1e58e2021-04-19 13:19:44 +02002976 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002977
2978 assert(tc && tc->pn);
2979
aPiecek3f247652021-04-19 13:40:25 +02002980 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002981
aPiecekef1e58e2021-04-19 13:19:44 +02002982 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02002983 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
2984 tc->tpn = pn;
2985 }
aPiecekef1e58e2021-04-19 13:19:44 +02002986 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002987 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002988 } else {
2989 return TRP_EMPTY_NODE;
2990 }
2991}
2992
2993/**
2994 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02002995 * @param[in,out] tc is tree context. It is modified and his current
2996 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01002997 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01002998 * @return Empty section structure otherwise.
2999 */
3000static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003001trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003002{
aPiecek9f792e52021-04-21 08:33:56 +02003003 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003004 const struct lysp_node_augment *augs;
3005
3006 /* if next_augment func was called for the first time */
3007 if (tc->section != TRD_SECT_AUGMENT) {
3008 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003009 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003010 } else {
3011 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003012 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003013 }
3014
aPiecekdc8fd572021-04-19 10:47:23 +02003015 if (augs) {
3016 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003017 tc->tpn = tc->pn;
3018 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3019 } else {
3020 return TRP_EMPTY_KEYWORD_STMT;
3021 }
3022}
3023
3024/**
aPiecek61d062b2020-11-02 11:05:09 +01003025 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003026 * @param[in,out] tc is tree context. It is modified and his current
3027 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003028 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003029 * @return Empty section representation otherwise.
3030 */
3031static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003032trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003033{
aPiecek9f792e52021-04-21 08:33:56 +02003034 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003035 const struct lysp_node_grp *grps;
3036
3037 if (tc->section != TRD_SECT_GROUPING) {
3038 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003039 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003040 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003041 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003042 }
3043
aPiecekdc8fd572021-04-19 10:47:23 +02003044 if (grps) {
3045 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003046 tc->tpn = tc->pn;
3047 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3048 } else {
3049 return TRP_EMPTY_KEYWORD_STMT;
3050 }
3051}
3052
aPiecek874ea4d2021-04-19 12:26:36 +02003053/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003054 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003055 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003056
3057/**
aPiecek3f247652021-04-19 13:40:25 +02003058 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003059 */
aPiecek3f247652021-04-19 13:40:25 +02003060static ly_bool
3061troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003062{
aPiecek3f247652021-04-19 13:40:25 +02003063 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3064}
aPiecek61d062b2020-11-02 11:05:09 +01003065
aPiecek3f247652021-04-19 13:40:25 +02003066/**
3067 * @brief Resolve \<flags\> of the current node.
3068 *
3069 * Use this function only if trt_tree_ctx.lysc_tree is true.
3070 *
3071 * @param[in] nodetype is current lysc_node.nodetype.
3072 * @param[in] flags is current lysc_node.flags.
3073 * @return The flags type.
3074 */
3075static trt_flags_type
3076troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3077{
3078 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3079 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3080 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3081 return TRD_FLAGS_TYPE_RO;
3082 } else if (nodetype & LYS_IS_NOTIF) {
3083 return TRD_FLAGS_TYPE_RO;
3084 } else if (nodetype & LYS_NOTIF) {
3085 return TRD_FLAGS_TYPE_NOTIF;
3086 } else if (nodetype & LYS_USES) {
3087 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3088 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3089 return TRD_FLAGS_TYPE_RPC;
3090 } else {
3091 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003092 }
aPiecek61d062b2020-11-02 11:05:09 +01003093}
3094
3095/**
aPiecek3f247652021-04-19 13:40:25 +02003096 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003097 *
aPiecek3f247652021-04-19 13:40:25 +02003098 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003099 *
aPiecek3f247652021-04-19 13:40:25 +02003100 * @param[in] nodetype is current lysc_node.nodetype.
3101 * @param[in] flags is current lysc_node.flags.
3102 */
3103static trt_node_type
3104troc_resolve_node_type(uint16_t nodetype, uint16_t flags)
3105{
3106 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3107 return TRD_NODE_ELSE;
3108 } else if (nodetype & LYS_CASE) {
3109 return TRD_NODE_CASE;
3110 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3111 return TRD_NODE_OPTIONAL_CHOICE;
3112 } else if (nodetype & LYS_CHOICE) {
3113 return TRD_NODE_CHOICE;
3114 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3115 return TRD_NODE_CONTAINER;
3116 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3117 return TRD_NODE_KEYS;
3118 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3119 return TRD_NODE_LISTLEAFLIST;
3120 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3121 return TRD_NODE_OPTIONAL;
3122 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3123 return TRD_NODE_OPTIONAL;
3124 } else {
3125 return TRD_NODE_ELSE;
3126 }
3127}
3128
3129/**
3130 * @brief Transformation of current lysc_node to struct trt_node.
3131 * @param[in] ca is not used.
3132 * @param[in] tc is context of the tree.
3133 */
3134static struct trt_node
3135troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3136{
3137 (void) ca;
3138 const struct lysc_node *cn;
3139 struct trt_node ret;
3140
aPiecek96baa7f2021-04-23 12:32:00 +02003141 assert(tc && tc->cn && tc->cn->priv);
aPiecek3f247652021-04-19 13:40:25 +02003142
3143 cn = tc->cn;
3144 ret = TRP_EMPTY_NODE;
3145
3146 /* <status> */
3147 ret.status = tro_flags2status(cn->flags);
3148
3149 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
3150 /* <flags> */
3151 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3152
3153 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
3154 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
3155 /* set type of the node */
3156 ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
3157
3158 /* TODO: ret.name.module_prefix is not supported right now. */
3159 ret.name.module_prefix = NULL;
3160
3161 /* set node's name */
3162 ret.name.str = cn->name;
3163
3164 /* <type> */
3165 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3166
3167 /* <iffeature> */
3168 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3169
3170 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3171
3172 return ret;
3173}
3174
3175/**********************************************************************
3176 * Modify troc getters
3177 *********************************************************************/
3178
3179/**
aPiecek01598c02021-04-23 14:18:24 +02003180 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003181 */
3182static ly_bool
3183troc_modi_parent(struct trt_tree_ctx *tc)
3184{
3185 assert(tc && tc->cn);
3186 /* If no parent exists, stay in actual node. */
3187 if (tc->cn->parent) {
3188 tc->cn = tc->cn->parent;
3189 return 1;
3190 } else {
3191 return 0;
3192 }
3193}
3194
3195/**
aPiecek01598c02021-04-23 14:18:24 +02003196 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003197 */
3198static struct trt_node
3199troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3200{
3201 const struct lysc_node *cn;
3202
3203 assert(tc && tc->cn);
3204
3205 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3206
3207 /* if next sibling exists */
3208 if (cn) {
3209 /* update trt_tree_ctx */
3210 tc->cn = cn;
3211 return troc_read_node(ca, tc);
3212 } else {
3213 return TRP_EMPTY_NODE;
3214 }
3215}
3216
3217/**
3218 * @copydoc trop_modi_next_child()
3219 */
3220static struct trt_node
3221troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3222{
3223 const struct lysc_node *tmp;
3224
3225 assert(tc && tc->cn);
3226
3227 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3228 tc->cn = tmp;
3229 return troc_read_node(ca, tc);
3230 } else {
3231 return TRP_EMPTY_NODE;
3232 }
3233}
3234
3235/**
aPiecek01598c02021-04-23 14:18:24 +02003236 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003237 */
3238static void
aPiecek3f247652021-04-19 13:40:25 +02003239troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003240{
aPiecek3f247652021-04-19 13:40:25 +02003241 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003242
aPiecek3f247652021-04-19 13:40:25 +02003243 if (troc_modi_parent(tc)) {
3244 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3245 } else {
3246 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003247 switch (tc->section) {
3248 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003249 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003250 break;
3251 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003252 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003253 break;
3254 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003255 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003256 break;
3257 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003258 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003259 break;
3260 default:
3261 assert(0);
3262 }
aPiecek61d062b2020-11-02 11:05:09 +01003263 }
3264}
3265
aPiecek874ea4d2021-04-19 12:26:36 +02003266/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003267 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003268 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003269
3270/**
3271 * @brief Get size of node name.
3272 * @param[in] name contains name and mark.
3273 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003274 * @return negative value as an indication that option mark
3275 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003276 */
3277static int32_t
3278trb_strlen_of_name_and_mark(struct trt_node_name name)
3279{
3280 size_t name_len = strlen(name.str);
3281
3282 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3283 /* counting also parentheses */
3284 name_len += 2;
3285 }
3286
3287 return trp_mark_is_used(name) ?
3288 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3289 (int32_t)name_len;
3290}
3291
3292/**
aPiecek874ea4d2021-04-19 12:26:36 +02003293 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3294 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003295 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003296 * @param[in] max_len4all is the maximum value of btw_opts_type
3297 * that it can have.
3298 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003299 */
3300static int16_t
3301trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3302{
3303 int32_t name_len;
3304 int16_t min_len;
3305 int16_t ret;
3306
3307 name_len = trb_strlen_of_name_and_mark(name);
3308
3309 /* negative value indicate that in name is some opt mark */
3310 min_len = name_len < 0 ?
3311 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3312 TRD_INDENT_BEFORE_TYPE;
3313 ret = abs(max_len4all) - abs(name_len);
3314
3315 /* correction -> negative indicate that name is too long. */
3316 return ret < 0 ? min_len : ret;
3317}
3318
3319/**
3320 * @brief Print node.
3321 *
aPiecek01598c02021-04-23 14:18:24 +02003322 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003323 * But difference is that take @p max_gap_before_type which will be
3324 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003325 *
aPiecek9bdd7592021-05-20 08:13:20 +02003326 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003327 * @param[in] max_gap_before_type is number of indent before \<type\>.
3328 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003329 * @param[in] pc contains mainly functions for printing.
3330 * @param[in] tc is tree context.
3331 */
3332static void
aPiecek9bdd7592021-05-20 08:13:20 +02003333trb_print_entire_node(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003334{
aPiecek61d062b2020-11-02 11:05:09 +01003335 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3336
3337 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3338 /* print actual node with unified indent */
3339 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3340 }
3341 /* after -> print actual node with default indent */
3342 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3343 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
3344}
3345
3346/**
aPiecek874ea4d2021-04-19 12:26:36 +02003347 * @brief Check if parent of the current node is the last
3348 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003349 *
aPiecek874ea4d2021-04-19 12:26:36 +02003350 * To mantain stability use this function only if the current node is
3351 * the first of the siblings.
3352 * Side-effect -> current node is set to the first sibling
3353 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003354 *
aPiecek01598c02021-04-23 14:18:24 +02003355 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003356 * @param[in,out] tc is tree context.
3357 * @return 1 if parent is last sibling otherwise 0.
3358 */
3359static ly_bool
3360trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3361{
3362 if (fp.modify.parent(tc)) {
3363 ly_bool ret = fp.read.if_sibling_exists(tc);
3364 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3365 return !ret;
3366 } else {
3367 return !fp.read.if_sibling_exists(tc);
3368 }
3369}
3370
3371/**
3372 * @brief Find sibling with the biggest node name and return that size.
3373 *
3374 * Side-effect -> Current node is set to the first sibling.
3375 *
3376 * @param[in] ca contains inherited data from ancestors.
3377 * @param[in] pc contains mainly functions for printing.
3378 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003379 * @return positive number as a sign that only the node name is
3380 * included in the size.
3381 * @return negative number sign that node name and his opt mark is
3382 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003383 */
3384static int32_t
3385trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3386{
3387 int32_t ret = 0;
3388
3389 pc->fp.modify.first_sibling(tc);
3390
3391 for (struct trt_node node = pc->fp.read.node(ca, tc);
3392 !trp_node_is_empty(node);
3393 node = pc->fp.modify.next_sibling(ca, tc)) {
3394 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
3395 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3396 }
3397 pc->fp.modify.first_sibling(tc);
3398 return ret;
3399}
3400
3401/**
aPiecek874ea4d2021-04-19 12:26:36 +02003402 * @brief Find maximal indent between
3403 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003404 *
3405 * Side-effect -> Current node is set to the first sibling.
3406 *
3407 * @param[in] ca contains inherited data from ancestors.
3408 * @param[in] pc contains mainly functions for printing.
3409 * @param[in,out] tc is tree context.
3410 * @return max btw_opts_type value for rest of the siblings
3411 */
3412static int16_t
3413trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3414{
3415 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3416 int16_t ind_before_type = maxlen_node_name < 0 ?
3417 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3418 TRD_INDENT_BEFORE_TYPE;
3419
3420 return abs(maxlen_node_name) + ind_before_type;
3421}
3422
3423/**
aPiecek874ea4d2021-04-19 12:26:36 +02003424 * @brief Find out if it is possible to unify
3425 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003426 *
aPiecek874ea4d2021-04-19 12:26:36 +02003427 * The goal is for all node siblings to have the same alignment
3428 * for \<type\> as if they were in a column. All siblings who cannot
3429 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003430 * Side-effect -> Current node is set to the first sibling.
3431 *
3432 * @param[in] ca contains inherited data from ancestors.
3433 * @param[in] pc contains mainly functions for printing.
3434 * @param[in,out] tc is tree context.
3435 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003436 * @return positive number indicating the maximum number of spaces
3437 * before \<type\> if the length of the node name is 0. To calculate
3438 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003439 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003440*/
3441static uint32_t
3442trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3443{
3444 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3445}
3446
3447/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003448 * @brief Check if there is no case statement
3449 * under the choice statement.
3450 *
3451 * It can return true only if the Parsed schema tree
3452 * is used for browsing.
3453 *
3454 * @param[in] tc is tree context.
3455 * @return 1 if implicit case statement is present otherwise 0.
3456 */
3457static ly_bool
3458trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3459{
3460 return !tc->lysc_tree && tc->pn->parent &&
3461 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3462 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3463 LYS_LEAF | LYS_LEAFLIST));
3464}
3465
3466static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3467 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3468
3469/**
3470 * @brief Print implicit case node and his subtree.
3471 *
3472 * @param[in] node is child of implicit case.
3473 * @param[in] wr is wrapper for printing identation before node.
3474 * @param[in] ca contains inherited data from ancestors.
3475 * @param[in] pc contains mainly functions for printing.
3476 * @param[in] tc is tree context. Its settings should be the same as
3477 * before the function call.
3478 */
3479static void
3480trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3481 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3482{
3483 struct trt_node case_node;
3484 struct trt_wrapper wr_case_child;
3485
3486 case_node = tro_create_implicit_case_node(node);
3487 ly_print_(pc->out, "\n");
3488 trb_print_entire_node(case_node, 0, wr, pc, tc);
3489 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3490 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3491 ly_print_(pc->out, "\n");
3492 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3493}
3494
3495/**
aPiecek874ea4d2021-04-19 12:26:36 +02003496 * @brief For the current node: recursively print all of its child
3497 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003498 *
aPiecek01598c02021-04-23 14:18:24 +02003499 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003500 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003501 * Nodes are printed, including unified sibling node alignment
3502 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003503 * Side-effect -> current node is set to the last sibling.
3504 *
3505 * @param[in] wr is wrapper for printing identation before node.
3506 * @param[in] ca contains inherited data from ancestors.
3507 * @param[in] pc contains mainly functions for printing.
3508 * @param[in,out] tc is tree context.
3509 */
3510static void
3511trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3512{
3513 uint32_t max_gap_before_type;
3514 ly_bool sibling_flag = 0;
3515 ly_bool child_flag = 0;
3516
3517 /* if node is last sibling, then do not add '|' to wrapper */
3518 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3519 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3520
3521 /* try unified indentation in node */
3522 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3523
3524 /* print all siblings */
3525 do {
3526 struct trt_parent_cache new_ca;
3527 struct trt_node node;
aPiecek9bdd7592021-05-20 08:13:20 +02003528 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003529
aPiecekb8d5a0a2021-05-20 08:20:24 +02003530 if (!trb_need_implicit_node_case(tc)) {
3531 /* normal behavior */
3532 ly_print_(pc->out, "\n");
3533 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3534 new_ca = tro_parent_cache_for_child(ca, tc);
3535 /* go to the actual node's child or stay in actual node */
3536 node = pc->fp.modify.next_child(ca, tc);
3537 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003538
aPiecekb8d5a0a2021-05-20 08:20:24 +02003539 if (child_flag) {
3540 /* print all childs - recursive call */
3541 trb_print_nodes(wr, new_ca, pc, tc);
3542 /* get back from child node to actual node */
3543 pc->fp.modify.parent(tc);
3544 }
3545 } else {
3546 /* The case statement is omitted (shorthand).
3547 * Print implicit case node and his subtree.
3548 */
3549 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003550 }
3551
3552 /* go to the actual node's sibling */
3553 node = pc->fp.modify.next_sibling(ca, tc);
3554 sibling_flag = !trp_node_is_empty(node);
3555
3556 /* go to the next sibling or stay in actual node */
3557 } while (sibling_flag);
3558}
3559
3560/**
aPiecek153b00f2021-04-20 13:52:57 +02003561 * @brief Calculate the wrapper about how deep in the tree the node is.
3562 * @param[in] node from which to count.
3563 * @return wrapper for @p node.
3564 */
3565static struct trt_wrapper
3566trb_count_depth(const struct lysc_node *node)
3567{
3568 struct trt_wrapper wr = TRP_INIT_WRAPPER_TOP;
3569 const struct lysc_node *parent;
3570
3571 if (!node) {
3572 return wr;
3573 }
3574
3575 for (parent = node->parent; parent; parent = parent->parent) {
3576 wr = trp_wrapper_set_shift(wr);
3577 }
3578
3579 return wr;
3580}
3581
3582/**
3583 * @brief Print all parent nodes of @p node and the @p node itself.
3584 *
3585 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3586 *
3587 * @param[in] node on which the function is focused.
aPiecek01598c02021-04-23 14:18:24 +02003588 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003589 * @param[in,out] tc is context of tree printer.
3590 * @return wrapper for @p node.
3591 */
3592static void
3593trb_print_parents(const struct lysc_node *node, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3594{
3595 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003596 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003597
3598 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3599
3600 /* stop recursion */
3601 if (!node) {
3602 return;
3603 }
3604 trb_print_parents(node->parent, pc, tc);
3605
3606 /* setup for printing */
3607 tc->cn = node;
3608 wr = trb_count_depth(node);
3609
3610 /* print node */
3611 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003612 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3613 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003614}
3615
3616/**
aPiecekdc8fd572021-04-19 10:47:23 +02003617 * @brief Get address of the current node.
3618 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003619 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003620 */
3621static const void *
3622trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3623{
aPiecek3f247652021-04-19 13:40:25 +02003624 return tc->lysc_tree ?
3625 (const void *)tc->cn :
3626 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003627}
3628
3629/**
3630 * @brief Get address of current node's child.
3631 * @param[in,out] tc contains current node.
3632 */
3633static const void *
3634trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3635{
3636 if (!trb_tree_ctx_get_node(tc)) {
3637 return NULL;
3638 }
3639
aPiecek3f247652021-04-19 13:40:25 +02003640 if (tc->lysc_tree) {
3641 return lysc_node_child(tc->cn);
3642 } else {
3643 return lysp_node_child(tc->pn);
3644 }
aPiecekdc8fd572021-04-19 10:47:23 +02003645}
3646
3647/**
3648 * @brief Set current node on its child.
3649 * @param[in,out] tc contains current node.
3650 */
3651static void
3652trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3653{
aPiecek3f247652021-04-19 13:40:25 +02003654 const void *node = trb_tree_ctx_get_child(tc);
3655
3656 if (tc->lysc_tree) {
3657 tc->cn = node;
3658 } else {
3659 tc->pn = node;
3660 }
aPiecekdc8fd572021-04-19 10:47:23 +02003661}
3662
3663/**
aPiecek61d062b2020-11-02 11:05:09 +01003664 * @brief Print subtree of nodes.
3665 *
3666 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003667 * Before root node is no linebreak printing. This must be addressed by
3668 * the caller. Root node will also be printed. Behind last printed node
3669 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003670 *
aPiecek9bdd7592021-05-20 08:13:20 +02003671 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003672 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003673 * ::trb_try_unified_indent() function for root node.
3674 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003675 * @param[in] wr is wrapper saying how deep in the whole tree
3676 * is the root of the subtree.
3677 * @param[in] ca is parent_cache from root's parent.
3678 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003679 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003680 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003681 */
3682static void
aPiecek9bdd7592021-05-20 08:13:20 +02003683trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3684 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003685{
3686 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003687
aPiecek9bdd7592021-05-20 08:13:20 +02003688 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003689 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003690 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003691 node = pc->fp.modify.next_child(ca, tc);
3692
3693 if (!trp_node_is_empty(node)) {
3694 /* print root's nodes */
3695 trb_print_nodes(wr, new_ca, pc, tc);
3696 /* get back from child node to actual node */
3697 pc->fp.modify.parent(tc);
3698 }
3699}
3700
3701/**
3702 * @brief Get number of siblings.
3703 *
3704 * Side-effect -> current node is set to the first sibling.
3705 *
3706 * @param[in] fp contains callback functions which modify tree context
3707 * @param[in,out] tc is the tree context.
3708 * @return Number of siblings of the current node.
3709 */
3710static uint32_t
3711trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3712{
3713 uint32_t ret = 1;
3714 struct trt_node node = TRP_EMPTY_NODE;
3715
3716 /* including actual node */
3717 fp.first_sibling(tc);
3718 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3719 ret++;
3720 }
3721 fp.first_sibling(tc);
3722 return ret;
3723}
3724
3725/**
3726 * @brief Print all parents and their children.
3727 *
aPiecek874ea4d2021-04-19 12:26:36 +02003728 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003729 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003730 * for all top-level siblings. Use this function after 'module' keyword
3731 * or 'augment' and so. The nodes may not be exactly top-level in the
3732 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003733 *
aPiecek153b00f2021-04-20 13:52:57 +02003734 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3735 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003736 * @param[pc] pc contains mainly functions for printing.
3737 * @param[in,out] tc is tree context.
3738 */
3739static void
aPiecek153b00f2021-04-20 13:52:57 +02003740trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003741{
aPiecek61d062b2020-11-02 11:05:09 +01003742 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003743 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003744 uint32_t total_parents;
3745 uint32_t max_gap_before_type;
3746
aPiecekdc8fd572021-04-19 10:47:23 +02003747 if (!trb_tree_ctx_get_node(tc)) {
3748 return;
3749 }
3750
aPiecek61d062b2020-11-02 11:05:09 +01003751 ca = TRP_EMPTY_PARENT_CACHE;
3752 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3753 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3754
aPiecek3f247652021-04-19 13:40:25 +02003755 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003756 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3757 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003758 ca.lys_config = 0x0;
3759 }
aPiecekdc8fd572021-04-19 10:47:23 +02003760 }
3761
aPiecek61d062b2020-11-02 11:05:09 +01003762 for (uint32_t i = 0; i < total_parents; i++) {
3763 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003764 node = pc->fp.read.node(ca, tc);
3765 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003766 pc->fp.modify.next_sibling(ca, tc);
3767 }
3768}
3769
aPiecek874ea4d2021-04-19 12:26:36 +02003770/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003771 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003772 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003773
3774/**
aPiecekdc8fd572021-04-19 10:47:23 +02003775 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003776 *
aPiecekdc8fd572021-04-19 10:47:23 +02003777 * @param[in] module YANG schema tree structure representing
3778 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003779 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003780 * @param[in] max_line_length is the maximum line length limit
3781 * that should not be exceeded.
3782 * @param[in,out] pc will be adapted to lysp_tree.
3783 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003784 */
3785static void
aPiecekdc8fd572021-04-19 10:47:23 +02003786trm_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 +01003787{
aPiecekdc8fd572021-04-19 10:47:23 +02003788 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003789 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003790 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003791 .pmod = module->parsed,
3792 .cmod = NULL,
3793 .pn = module->parsed ? module->parsed->data : NULL,
3794 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003795 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003796 };
aPiecek61d062b2020-11-02 11:05:09 +01003797
aPiecekdc8fd572021-04-19 10:47:23 +02003798 pc->out = out;
3799
3800 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003801 .parent = trop_modi_parent,
3802 .first_sibling = trop_modi_first_sibling,
3803 .next_sibling = trop_modi_next_sibling,
3804 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01003805 };
3806
aPiecekdc8fd572021-04-19 10:47:23 +02003807 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003808 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003809 .node = trop_read_node,
3810 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003811 };
3812
aPiecekdc8fd572021-04-19 10:47:23 +02003813 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003814 .print_features_names = tro_print_features_names,
3815 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01003816 };
3817
aPiecekdc8fd572021-04-19 10:47:23 +02003818 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01003819}
3820
3821/**
aPiecek3f247652021-04-19 13:40:25 +02003822 * @brief Settings if lysc_node are used for browsing through the tree.
3823 *
3824 * Pointers to current nodes will be set to module data.
3825 *
3826 * @param[in] module YANG schema tree structure representing
3827 * YANG module.
3828 * @param[in] out is output handler.
3829 * @param[in] max_line_length is the maximum line length limit
3830 * that should not be exceeded.
3831 * @param[in,out] pc will be adapted to lysc_tree.
3832 * @param[in,out] tc will be adapted to lysc_tree.
3833 */
3834static void
3835trm_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)
3836{
3837 *tc = (struct trt_tree_ctx) {
3838 .lysc_tree = 1,
3839 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003840 .pmod = module->parsed,
3841 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02003842 .tpn = NULL,
3843 .pn = NULL,
3844 .cn = module->compiled->data
3845 };
3846
3847 pc->out = out;
3848
3849 pc->fp.modify = (struct trt_fp_modify_ctx) {
3850 .parent = troc_modi_parent,
3851 .first_sibling = troc_modi_first_sibling,
3852 .next_sibling = troc_modi_next_sibling,
3853 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02003854 };
3855
3856 pc->fp.read = (struct trt_fp_read) {
3857 .module_name = tro_read_module_name,
3858 .node = troc_read_node,
3859 .if_sibling_exists = troc_read_if_sibling_exists
3860 };
3861
3862 pc->fp.print = (struct trt_fp_print) {
3863 .print_features_names = tro_print_features_names,
3864 .print_keys = tro_print_keys
3865 };
3866
3867 pc->max_line_length = max_line_length;
3868}
3869
3870/**
3871 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02003872 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02003873 * @param[in,out] tc resets to lysc browsing.
3874 */
3875static void
3876trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3877{
aPiecek9f792e52021-04-21 08:33:56 +02003878 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003879}
3880
3881/**
3882 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02003883 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02003884 * @param[in,out] tc resets to lysp browsing.
3885 */
3886static void
3887trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3888{
aPiecek9f792e52021-04-21 08:33:56 +02003889 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003890}
3891
3892/**
3893 * @brief If augment's target node is located on the current module.
3894 * @param[in] pn is examined augment.
3895 * @param[in] pmod is current module.
3896 * @return 1 if nodeid refers to the local node, otherwise 0.
3897 */
3898static ly_bool
3899trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
3900{
3901 const char *id, *prefix, *name;
3902 size_t prefix_len, name_len;
3903 const struct lys_module *mod;
3904 ly_bool ret = 0;
3905
3906 if (pn == NULL) {
3907 return ret;
3908 }
3909
3910 id = pn->nodeid;
3911 if (!id) {
3912 return ret;
3913 }
3914 /* only absolute-schema-nodeid is taken into account */
3915 assert(id[0] == '/');
3916 ++id;
3917
3918 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3919 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003920 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
aPiecek3f247652021-04-19 13:40:25 +02003921 ret = mod->parsed == pmod;
3922 } else {
3923 ret = 1;
3924 }
3925
3926 return ret;
3927}
3928
3929/**
aPiecek96baa7f2021-04-23 12:32:00 +02003930 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01003931 *
aPiecekdc8fd572021-04-19 10:47:23 +02003932 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02003933 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01003934 *
aPiecekdc8fd572021-04-19 10:47:23 +02003935 * @param[in] ks is section representation.
3936 * @param[in] pc contains mainly functions for printing.
3937 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01003938 */
3939static void
aPiecekdc8fd572021-04-19 10:47:23 +02003940trm_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 +01003941{
aPiecekdc8fd572021-04-19 10:47:23 +02003942 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3943 return;
3944 }
3945
3946 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
3947 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02003948 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003949 } else {
aPiecek153b00f2021-04-20 13:52:57 +02003950 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003951 }
3952}
3953
3954/**
aPiecek96baa7f2021-04-23 12:32:00 +02003955 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02003956 *
aPiecek96baa7f2021-04-23 12:32:00 +02003957 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02003958 *
3959 * @param[in] ks is section representation.
3960 * @param[in] pc contains mainly functions for printing.
3961 * @param[in,out] tc is the tree context.
3962 */
3963static void
3964trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3965{
3966 ly_bool grp_has_data = 0;
3967
3968 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3969 return;
3970 }
3971
3972 if (ks.type == TRD_KEYWORD_GROUPING) {
3973 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
3974 }
3975
3976 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
3977 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02003978 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003979}
3980
3981/**
3982 * @brief Print 'module' keyword, its name and all nodes.
3983 * @param[in] pc contains mainly functions for printing.
3984 * @param[in,out] tc is the tree context.
3985 */
3986static void
3987trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3988{
3989 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
3990}
3991
3992/**
3993 * @brief For all augment sections: print 'augment' keyword,
3994 * its target node and all nodes.
3995 * @param[in] pc contains mainly functions for printing.
3996 * @param[in,out] tc is the tree context.
3997 */
3998static void
3999trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4000{
4001 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004002 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004003
aPiecek3f247652021-04-19 13:40:25 +02004004 if (tc->lysc_tree) {
4005 origin_was_lysc_tree = 1;
4006 trm_reset_to_lysp_tree_ctx(pc, tc);
4007 }
4008
aPiecekdc8fd572021-04-19 10:47:23 +02004009 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004010 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004011 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004012 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004013
aPiecek3f247652021-04-19 13:40:25 +02004014 if (origin_was_lysc_tree) {
4015 /* if lysc tree is used, then only augments targeting
4016 * another module are printed
4017 */
aPiecek9f792e52021-04-21 08:33:56 +02004018 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004019 continue;
4020 }
4021 }
4022
aPiecekdc8fd572021-04-19 10:47:23 +02004023 if (once) {
4024 ly_print_(pc->out, "\n");
4025 ly_print_(pc->out, "\n");
4026 once = 0;
4027 } else {
4028 ly_print_(pc->out, "\n");
4029 }
4030
4031 trm_print_section_as_subtree(ks, pc, tc);
4032 }
aPiecek3f247652021-04-19 13:40:25 +02004033
4034 if (origin_was_lysc_tree) {
4035 trm_reset_to_lysc_tree_ctx(pc, tc);
4036 }
aPiecekdc8fd572021-04-19 10:47:23 +02004037}
4038
4039/**
4040 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4041 * @param[in] pc contains mainly functions for printing.
4042 * @param[in,out] tc is the tree context.
4043 */
4044static void
4045trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4046{
4047 struct trt_keyword_stmt rpc;
4048
aPiecek01598c02021-04-23 14:18:24 +02004049 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004050
4051 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4052 ly_print_(pc->out, "\n");
4053 ly_print_(pc->out, "\n");
4054 trm_print_section_as_family_tree(rpc, pc, tc);
4055 }
4056}
4057
4058/**
4059 * @brief For notifications section: print 'notifications' keyword
4060 * and all its nodes.
4061 * @param[in] pc contains mainly functions for printing.
4062 * @param[in,out] tc is the tree context.
4063 */
4064static void
4065trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4066{
4067 struct trt_keyword_stmt notifs;
4068
aPiecek01598c02021-04-23 14:18:24 +02004069 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004070
4071 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4072 ly_print_(pc->out, "\n");
4073 ly_print_(pc->out, "\n");
4074 trm_print_section_as_family_tree(notifs, pc, tc);
4075 }
4076}
4077
4078/**
4079 * @brief For all grouping sections: print 'grouping' keyword, its name
4080 * and all nodes.
4081 * @param[in] pc contains mainly functions for printing.
4082 * @param[in,out] tc is the tree context.
4083 */
4084static void
4085trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4086{
4087 ly_bool once;
4088
aPiecek01598c02021-04-23 14:18:24 +02004089 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004090 return;
4091 }
4092
4093 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004094 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004095 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004096 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004097 if (once) {
4098 ly_print_(pc->out, "\n");
4099 ly_print_(pc->out, "\n");
4100 once = 0;
4101 } else {
4102 ly_print_(pc->out, "\n");
4103 }
4104 trm_print_section_as_subtree(ks, pc, tc);
4105 }
4106}
4107
4108/**
4109 * @brief For all yang-data sections: print 'yang-data' keyword
4110 * and all its nodes.
4111 * @param[in] pc contains mainly functions for printing.
4112 * @param[in,out] tc is the tree context.
4113 */
4114static void
aPiecek96baa7f2021-04-23 12:32:00 +02004115trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004116{
aPiecek96baa7f2021-04-23 12:32:00 +02004117 ly_bool once;
4118 LY_ARRAY_COUNT_TYPE count;
4119
4120 count = LY_ARRAY_COUNT(tc->pmod->exts);
4121 if (count == 0) {
4122 return;
4123 }
4124
4125 once = 1;
4126 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4127 struct trt_keyword_stmt ks;
4128
aPiecek01598c02021-04-23 14:18:24 +02004129 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004130 * ::lysp_ext_instance.parsed.
4131 */
4132 if (!tc->pmod->exts[u].parsed) {
4133 /* print at least the yang-data names */
4134 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4135 continue;
4136 }
4137
4138 ks = tro_modi_next_yang_data(tc, u);
4139 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4140 break;
4141 }
4142
4143 if (once) {
4144 ly_print_(pc->out, "\n");
4145 ly_print_(pc->out, "\n");
4146 once = 0;
4147 } else {
4148 ly_print_(pc->out, "\n");
4149 }
4150
4151 trm_print_section_as_family_tree(ks, pc, tc);
4152 }
aPiecekdc8fd572021-04-19 10:47:23 +02004153}
4154
4155/**
4156 * @brief Print sections module, augment, rpcs, notifications,
4157 * grouping, yang-data.
4158 * @param[in] pc contains mainly functions for printing.
4159 * @param[in,out] tc is the tree context.
4160 */
4161static void
4162trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4163{
4164 trm_print_module_section(pc, tc);
4165 trm_print_augmentations(pc, tc);
4166 trm_print_rpcs(pc, tc);
4167 trm_print_notifications(pc, tc);
4168 trm_print_groupings(pc, tc);
4169 trm_print_yang_data(pc, tc);
4170 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004171}
4172
aPiecek874ea4d2021-04-19 12:26:36 +02004173/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004174 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004175 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004176
4177LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004178tree_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 +01004179{
4180 struct trt_printer_ctx pc;
4181 struct trt_tree_ctx tc;
4182 struct ly_out *new_out;
4183 LY_ERR erc;
4184 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4185
aPiecekdc8fd572021-04-19 10:47:23 +02004186 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4187
aPiecek61d062b2020-11-02 11:05:09 +01004188 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4189 return erc;
4190 }
4191
4192 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004193 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4194 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4195 } else {
4196 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4197 }
aPiecek61d062b2020-11-02 11:05:09 +01004198
4199 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004200 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004201
4202 ly_out_free(new_out, NULL, 1);
4203
aPiecekdc8fd572021-04-19 10:47:23 +02004204 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004205}
4206
4207LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004208tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options, size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004209{
aPiecek153b00f2021-04-20 13:52:57 +02004210 struct trt_printer_ctx pc;
4211 struct trt_tree_ctx tc;
4212 struct ly_out *new_out;
4213 struct trt_wrapper wr;
4214 LY_ERR erc;
4215 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4216
4217 assert(out && node);
4218
4219 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4220 return LY_EINVAL;
4221 }
4222
4223 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4224 return erc;
4225 }
4226
4227 line_length = line_length == 0 ? SIZE_MAX : line_length;
4228 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4229
4230 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
4231 trb_print_parents(node, &pc, &tc);
4232
4233 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4234 tc.cn = lysc_node_child(node);
4235 wr = trb_count_depth(tc.cn);
4236 trb_print_family_tree(wr, &pc, &tc);
4237 }
4238 ly_print_(out, "\n");
4239
4240 erc = clb_arg.last_error;
4241 ly_out_free(new_out, NULL, 1);
4242
4243 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004244}
4245
4246LY_ERR
aPiecek9f792e52021-04-21 08:33:56 +02004247tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options), size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004248{
aPiecek9f792e52021-04-21 08:33:56 +02004249 struct trt_printer_ctx pc;
4250 struct trt_tree_ctx tc;
4251 struct ly_out *new_out;
4252 LY_ERR erc;
4253 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4254
4255 assert(submodp);
4256 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4257
4258 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4259 return erc;
4260 }
4261
4262 line_length = line_length == 0 ? SIZE_MAX : line_length;
4263 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4264 tc.pmod = (struct lysp_module *)submodp;
4265 tc.tpn = submodp->data;
4266 tc.pn = tc.tpn;
4267
4268 trm_print_sections(&pc, &tc);
4269 erc = clb_arg.last_error;
4270
4271 ly_out_free(new_out, NULL, 1);
4272
4273 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004274}