blob: 0eff496ac69877ca5790f7f88f300ff09ec87993 [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/**
aPiecekbbc02932021-05-21 07:19:41 +0200699 * @brief Check if lysp node is available from
700 * the current compiled node.
701 *
702 * Use only if trt_tree_ctx.lysc_tree is set to true.
703 */
704#define TRP_TREE_CTX_LYSP_NODE_PRESENT(CN) \
705 (CN->priv)
706
707/**
aPiecek3f247652021-04-19 13:40:25 +0200708 * @brief Get lysp_node from trt_tree_ctx.cn.
aPiecekbbc02932021-05-21 07:19:41 +0200709 *
710 * Use only if :TRP_TREE_CTX_LYSP_NODE_PRESENT returns true
711 * for that node.
aPiecek3f247652021-04-19 13:40:25 +0200712 */
713#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
714 ((const struct lysp_node *)CN->priv)
715
aPiecek01598c02021-04-23 14:18:24 +0200716/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100717typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
718
aPiecekef1e58e2021-04-19 13:19:44 +0200719/**
720 * @brief Simple getter functions for lysp and lysc nodes.
721 *
722 * This structure is useful if we have a general algorithm
723 * (tro function) that can be used for both lysc and lysp nodes.
724 * Thanks to this structure, we prevent code redundancy.
725 * We don't have to write basically the same algorithm twice
726 * for lysp and lysc trees.
727 */
728struct tro_getters
729{
730 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
731 const void *(*next)(const void *); /**< Get sibling. */
732 const void *(*parent)(const void *); /**< Get parent. */
733 const void *(*child)(const void *); /**< Get child. */
734 const void *(*actions)(const void *); /**< Get actions. */
735 const void *(*action_input)(const void *); /**< Get input action from action node. */
736 const void *(*action_output)(const void *); /**< Get output action from action node. */
737 const void *(*notifs)(const void *); /**< Get notifs. */
738};
739
aPiecek874ea4d2021-04-19 12:26:36 +0200740/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100741 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200742 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100743
744/**
745 * @brief Print a substring but limited to the maximum length.
746 * @param[in] str is pointer to source.
747 * @param[in] len is number of characters to be printed.
748 * @param[in,out] out is output handler.
749 * @return str parameter shifted by len.
750 */
751static const char *
752trg_print_substr(const char *str, size_t len, struct ly_out *out)
753{
754 for (size_t i = 0; i < len; i++) {
755 ly_print_(out, "%c", str[0]);
756 str++;
757 }
758 return str;
759}
760
761/**
762 * @brief Pointer is not NULL and does not point to an empty string.
763 * @param[in] str is pointer to string to be checked.
764 * @return 1 if str pointing to non empty string otherwise 0.
765 */
766static ly_bool
767trg_charptr_has_data(const char *str)
768{
769 return (str) && (str[0] != '\0');
770}
771
772/**
aPiecek874ea4d2021-04-19 12:26:36 +0200773 * @brief Check if @p word in @p src is present where words are
774 * delimited by @p delim.
775 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100776 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200777 * @param[in] delim is delimiter between @p words in @p src.
778 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100779 */
780static ly_bool
781trg_word_is_present(const char *src, const char *word, char delim)
782{
783 const char *hit;
784
785 if ((!src) || (src[0] == '\0') || (!word)) {
786 return 0;
787 }
788
789 hit = strstr(src, word);
790
791 if (hit) {
792 /* word was founded at the begin of src
793 * OR it match somewhere after delim
794 */
795 if ((hit == src) || (hit[-1] == delim)) {
796 /* end of word was founded at the end of src
797 * OR end of word was match somewhere before delim
798 */
799 char delim_or_end = (hit + strlen(word))[0];
800 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
801 return 1;
802 }
803 }
804 /* after -> hit is just substr and it's not the whole word */
805 /* jump to the next word */
806 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
807 /* skip delim */
808 src = src[0] == '\0' ? src : src + 1;
809 /* continue with searching */
810 return trg_word_is_present(src, word, delim);
811 } else {
812 return 0;
813 }
814}
815
aPiecek874ea4d2021-04-19 12:26:36 +0200816/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100817 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200818 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100819
820/**
aPiecek01598c02021-04-23 14:18:24 +0200821 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100822 *
aPiecek874ea4d2021-04-19 12:26:36 +0200823 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100824 * @param[in] buf contains input characters
825 * @param[in] count is number of characters in buf.
826 * @return Number of printed bytes.
827 * @return Negative value in case of error.
828 */
829static ssize_t
830trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
831{
832 LY_ERR erc = LY_SUCCESS;
833 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
834
835 switch (data->mode) {
836 case TRD_PRINT:
837 erc = ly_write_(data->out, buf, count);
838 break;
839 case TRD_CHAR_COUNT:
840 data->counter = data->counter + count;
841 break;
842 default:
843 break;
844 }
845
846 if (erc != LY_SUCCESS) {
847 data->last_error = erc;
848 return -1;
849 } else {
850 return count;
851 }
852}
853
854/**
855 * @brief Check that indent in node can be considered as equivalent.
856 * @param[in] first is the first indent in node.
857 * @param[in] second is the second indent in node.
858 * @return 1 if indents are equivalent otherwise 0.
859 */
860static ly_bool
861trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
862{
863 const ly_bool a = first.type == second.type;
864 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
865 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
866 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
867
868 return a && b && c && d;
869}
870
871/**
aPiecek874ea4d2021-04-19 12:26:36 +0200872 * @brief Setting space character because node is last sibling.
873 * @param[in] wr is wrapper over which the shift operation
874 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100875 * @return New shifted wrapper.
876 */
877static struct trt_wrapper
878trp_wrapper_set_shift(struct trt_wrapper wr)
879{
880 assert(wr.actual_pos < 64);
881 /* +--<node>
882 * +--<node>
883 */
884 wr.actual_pos++;
885 return wr;
886}
887
888/**
aPiecek874ea4d2021-04-19 12:26:36 +0200889 * @brief Setting '|' symbol because node is divided or
890 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100891 * @param[in] wr is source of wrapper.
892 * @return New wrapper which is marked at actual position and shifted.
893 */
894static struct trt_wrapper
895trp_wrapper_set_mark(struct trt_wrapper wr)
896{
897 assert(wr.actual_pos < 64);
898 wr.bit_marks1 |= 1U << wr.actual_pos;
899 return trp_wrapper_set_shift(wr);
900}
901
902/**
903 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
904 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200905 * @param[in] last_one is flag. Value 1 saying if the node is the last
906 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100907 * @return New wrapper for the actual node.
908 */
909static struct trt_wrapper
910trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
911{
912 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
913}
914
915/**
916 * @brief Test if the wrappers are equivalent.
917 * @param[in] first is the first wrapper.
918 * @param[in] second is the second wrapper.
919 * @return 1 if the wrappers are equivalent otherwise 0.
920 */
921static ly_bool
922trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
923{
924 const ly_bool a = first.type == second.type;
925 const ly_bool b = first.bit_marks1 == second.bit_marks1;
926 const ly_bool c = first.actual_pos == second.actual_pos;
927
928 return a && b && c;
929}
930
931/**
932 * @brief Print " | " sequence on line.
933 * @param[in] wr is wrapper to be printed.
934 * @param[in,out] out is output handler.
935 */
936static void
937trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
938{
939 uint32_t lb;
940
941 if (wr.type == TRD_WRAPPER_TOP) {
942 lb = TRD_INDENT_LINE_BEGIN;
943 } else if (wr.type == TRD_WRAPPER_BODY) {
944 lb = TRD_INDENT_LINE_BEGIN * 2;
945 } else {
946 lb = TRD_INDENT_LINE_BEGIN;
947 }
948
949 ly_print_(out, "%*c", lb, ' ');
950
951 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
952 return;
953 }
954
955 for (uint32_t i = 0; i < wr.actual_pos; i++) {
956 /** Test if the bit on the index is set. */
957 if ((wr.bit_marks1 >> i) & 1U) {
958 ly_print_(out, "|");
959 } else {
960 ly_print_(out, " ");
961 }
962
963 if (i != wr.actual_pos) {
964 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
965 }
966 }
967}
968
969/**
970 * @brief Check if struct trt_node is empty.
971 * @param[in] node is item to test.
972 * @return 1 if node is considered empty otherwise 0.
973 */
974static ly_bool
975trp_node_is_empty(struct trt_node node)
976{
977 const ly_bool a = !node.iffeatures;
978 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
979 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
980 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
981 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
982
983 return a && b && c && d && e;
984}
985
986/**
aPiecek874ea4d2021-04-19 12:26:36 +0200987 * @brief Check if [\<keys\>], \<type\> and
988 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +0100989 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +0200990 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
991 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100992 */
993static ly_bool
994trp_node_body_is_empty(struct trt_node node)
995{
996 const ly_bool a = !node.iffeatures;
997 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
998 const ly_bool c = node.name.type != TRD_NODE_KEYS;
999
1000 return a && b && c;
1001}
1002
1003/**
1004 * @brief Print \<status\> of the node.
1005 * @param[in] status_type is type of status.
1006 * @param[in,out] out is output handler.
1007 */
1008static void
1009trp_print_status(trt_status_type status_type, struct ly_out *out)
1010{
1011 switch (status_type) {
1012 case TRD_STATUS_TYPE_CURRENT:
1013 ly_print_(out, "%c", '+');
1014 break;
1015 case TRD_STATUS_TYPE_DEPRECATED:
1016 ly_print_(out, "%c", 'x');
1017 break;
1018 case TRD_STATUS_TYPE_OBSOLETE:
1019 ly_print_(out, "%c", 'o');
1020 break;
1021 default:
1022 break;
1023 }
1024}
1025
1026/**
1027 * @brief Print \<flags\>.
1028 * @param[in] flags_type is type of \<flags\>.
1029 * @param[in,out] out is output handler.
1030 */
1031static void
1032trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1033{
1034 switch (flags_type) {
1035 case TRD_FLAGS_TYPE_RW:
1036 ly_print_(out, "%s", "rw");
1037 break;
1038 case TRD_FLAGS_TYPE_RO:
1039 ly_print_(out, "%s", "ro");
1040 break;
1041 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1042 ly_print_(out, "%s", "-w");
1043 break;
1044 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1045 ly_print_(out, "%s", "-u");
1046 break;
1047 case TRD_FLAGS_TYPE_RPC:
1048 ly_print_(out, "%s", "-x");
1049 break;
1050 case TRD_FLAGS_TYPE_NOTIF:
1051 ly_print_(out, "%s", "-n");
1052 break;
1053 case TRD_FLAGS_TYPE_MOUNT_POINT:
1054 ly_print_(out, "%s", "mp");
1055 break;
1056 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001057 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001058 break;
1059 }
1060}
1061
1062/**
1063 * @brief Get size of the \<flags\>.
1064 * @param[in] flags_type is type of \<flags\>.
1065 * @return 0 if flags_type is not set otherwise 2.
1066 */
1067static size_t
1068trp_get_flags_strlen(trt_flags_type flags_type)
1069{
1070 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1071}
1072
1073/**
1074 * @brief Print entire struct trt_node_name structure.
1075 * @param[in] node_name is item to print.
1076 * @param[in,out] out is output handler.
1077 */
1078static void
1079trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1080{
1081 const char *mod_prefix;
1082 const char *colon;
1083 const char trd_node_name_suffix_choice[] = ")";
1084 const char trd_node_name_suffix_case[] = ")";
1085 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1086 const char trd_opts_container[] = "!"; /**< For a presence container. */
1087 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1088 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1089 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1090
1091 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1092 return;
1093 }
1094
1095 if (node_name.module_prefix) {
1096 mod_prefix = node_name.module_prefix;
1097 colon = ":";
1098 } else {
1099 mod_prefix = "";
1100 colon = "";
1101 }
1102
1103 switch (node_name.type) {
1104 case TRD_NODE_ELSE:
1105 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1106 break;
1107 case TRD_NODE_CASE:
1108 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1109 break;
1110 case TRD_NODE_CHOICE:
1111 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1112 break;
1113 case TRD_NODE_OPTIONAL_CHOICE:
1114 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);
1115 break;
1116 case TRD_NODE_OPTIONAL:
1117 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1118 break;
1119 case TRD_NODE_CONTAINER:
1120 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1121 break;
1122 case TRD_NODE_LISTLEAFLIST:
1123 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1124 break;
1125 case TRD_NODE_KEYS:
1126 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1127 break;
1128 case TRD_NODE_TOP_LEVEL1:
1129 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1130 break;
1131 case TRD_NODE_TOP_LEVEL2:
1132 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1133 break;
1134 case TRD_NODE_TRIPLE_DOT:
1135 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1136 break;
1137 default:
1138 break;
1139 }
1140}
1141
1142/**
aPiecek874ea4d2021-04-19 12:26:36 +02001143 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1144 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001145 * @param[in] node_name is structure containing the 'mark'.
1146 * @return 1 if contain otherwise 0.
1147 */
1148static ly_bool
1149trp_mark_is_used(struct trt_node_name node_name)
1150{
1151 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1152 return 0;
1153 }
1154
1155 switch (node_name.type) {
1156 case TRD_NODE_ELSE:
1157 case TRD_NODE_CASE:
1158 case TRD_NODE_KEYS:
1159 return 0;
1160 default:
1161 return 1;
1162 }
1163}
1164
1165/**
1166 * @brief Print opts keys.
1167 * @param[in] node_name contains type of the node with his name.
1168 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001169 * @param[in] cf is basically a pointer to the function that prints
1170 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001171 * @param[in,out] out is output handler.
1172 */
1173static void
1174trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1175{
1176 if (node_name.type != TRD_NODE_KEYS) {
1177 return;
1178 }
1179
1180 /* <name><mark>___<keys>*/
1181 if (btw_name_opts > 0) {
1182 ly_print_(out, "%*c", btw_name_opts, ' ');
1183 }
1184 ly_print_(out, "[");
1185 cf.pf(cf.ctx, out);
1186 ly_print_(out, "]");
1187}
1188
1189/**
1190 * @brief Print entire struct trt_type structure.
1191 * @param[in] type is item to print.
1192 * @param[in,out] out is output handler.
1193 */
1194static void
1195trp_print_type(struct trt_type type, struct ly_out *out)
1196{
1197 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1198 return;
1199 }
1200
1201 switch (type.type) {
1202 case TRD_TYPE_NAME:
1203 ly_print_(out, "%s", type.str);
1204 break;
1205 case TRD_TYPE_TARGET:
1206 ly_print_(out, "-> %s", type.str);
1207 break;
1208 case TRD_TYPE_LEAFREF:
1209 ly_print_(out, "leafref");
1210 default:
1211 break;
1212 }
1213}
1214
1215/**
1216 * @brief Print all iffeatures of node
1217 *
1218 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001219 * @param[in] cf is basically a pointer to the function that prints
1220 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001221 * @param[in,out] out is output handler.
1222 */
1223static void
1224trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1225{
1226 if (iffeature_flag) {
1227 ly_print_(out, "{");
1228 cf.pf(cf.ctx, out);
1229 ly_print_(out, "}?");
1230 }
1231}
1232
1233/**
1234 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1235 * @param[in] node contains items to print.
1236 * @param[in] out is output handler.
1237 */
1238static void
1239trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1240{
1241 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1242 trp_print_node_name(node.name, out);
1243 return;
1244 }
1245 /* <status>--<flags> */
1246 trp_print_status(node.status, out);
1247 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001248 /* If the node is a case node, there is no space before the <name>
1249 * also case node has no flags.
1250 */
aPiecek61d062b2020-11-02 11:05:09 +01001251 if (node.name.type != TRD_NODE_CASE) {
1252 trp_print_flags(node.flags, out);
1253 ly_print_(out, " ");
1254 }
1255 /* <name> */
1256 trp_print_node_name(node.name, out);
1257}
1258
1259/**
aPiecek874ea4d2021-04-19 12:26:36 +02001260 * @brief Print alignment (spaces) instead of
1261 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001262 * @param[in] node contains items to print.
1263 * @param[in] out is output handler.
1264 */
1265static void
1266trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1267{
1268 uint32_t space = trp_get_flags_strlen(node.flags);
1269
1270 if (node.name.type == TRD_NODE_CASE) {
1271 /* :(<name> */
1272 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1273 } else if (node.name.type == TRD_NODE_CHOICE) {
1274 /* (<name> */
1275 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1276 } else {
1277 /* _<name> */
1278 space += strlen(" ");
1279 }
1280
1281 /* <name>
1282 * __
1283 */
1284 space += TRD_INDENT_LONG_LINE_BREAK;
1285
1286 ly_print_(out, "%*c", space, ' ');
1287}
1288
1289/**
1290 * @brief Print struct trt_node structure.
1291 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001292 * @param[in] pck package of functions for
1293 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001294 * @param[in] indent is the indent in node.
1295 * @param[in,out] out is output handler.
1296 */
1297static void
1298trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1299{
1300 ly_bool triple_dot;
1301 ly_bool divided;
1302 struct trt_cf_print cf_print_keys;
1303 struct trt_cf_print cf_print_iffeatures;
1304
1305 if (trp_node_is_empty(node)) {
1306 return;
1307 }
1308
1309 /* <status>--<flags> <name><opts> <type> <if-features> */
1310 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1311 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1312
1313 if (triple_dot) {
1314 trp_print_node_name(node.name, out);
1315 return;
1316 } else if (!divided) {
1317 trp_print_node_up_to_name(node, out);
1318 } else {
1319 trp_print_divided_node_up_to_name(node, out);
1320 }
1321
1322 /* <opts> */
1323 /* <name>___<opts>*/
1324 cf_print_keys.ctx = pck.tree_ctx;
1325 cf_print_keys.pf = pck.fps.print_keys;
1326
1327 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1328
1329 /* <opts>__<type> */
1330 if (indent.btw_opts_type > 0) {
1331 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1332 }
1333
1334 /* <type> */
1335 trp_print_type(node.type, out);
1336
1337 /* <type>__<iffeatures> */
1338 if (indent.btw_type_iffeatures > 0) {
1339 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1340 }
1341
1342 /* <iffeatures> */
1343 cf_print_iffeatures.ctx = pck.tree_ctx;
1344 cf_print_iffeatures.pf = pck.fps.print_features_names;
1345
1346 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1347}
1348
1349/**
aPiecek874ea4d2021-04-19 12:26:36 +02001350 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001351 * @param[in] ks is keyword statement to print.
1352 * @param[in,out] out is output handler
1353 */
1354static void
1355trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1356{
1357 switch (ks.type) {
1358 case TRD_KEYWORD_MODULE:
1359 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1360 return;
1361 case TRD_KEYWORD_SUBMODULE:
1362 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1363 return;
1364 default:
1365 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1366 switch (ks.type) {
1367 case TRD_KEYWORD_AUGMENT:
1368 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1369 break;
1370 case TRD_KEYWORD_RPC:
1371 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1372 break;
1373 case TRD_KEYWORD_NOTIF:
1374 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1375 break;
1376 case TRD_KEYWORD_GROUPING:
1377 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1378 break;
1379 case TRD_KEYWORD_YANG_DATA:
1380 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1381 break;
1382 default:
1383 break;
1384 }
1385 break;
1386 }
1387}
1388
1389/**
1390 * @brief Get string length of stored keyword.
1391 * @param[in] type is type of the keyword statement.
1392 * @return length of the keyword statement name.
1393 */
1394static size_t
1395trp_keyword_type_strlen(trt_keyword_type type)
1396{
1397 switch (type) {
1398 case TRD_KEYWORD_MODULE:
1399 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1400 case TRD_KEYWORD_SUBMODULE:
1401 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1402 case TRD_KEYWORD_AUGMENT:
1403 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1404 case TRD_KEYWORD_RPC:
1405 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1406 case TRD_KEYWORD_NOTIF:
1407 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1408 case TRD_KEYWORD_GROUPING:
1409 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1410 case TRD_KEYWORD_YANG_DATA:
1411 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1412 default:
1413 return 0;
1414 }
1415}
1416
1417/**
aPiecek874ea4d2021-04-19 12:26:36 +02001418 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001419 * @param[in] ks is keyword statement structure.
1420 * @param[in] mll is max line length.
1421 * @param[in,out] out is output handler.
1422 */
1423static void
1424trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1425{
1426 uint32_t ind_initial;
1427 uint32_t ind_divided;
1428 /* flag if path must be splitted to more lines */
1429 ly_bool linebreak_was_set;
1430 /* flag if at least one subpath was printed */
1431 ly_bool subpath_printed;
1432 /* the sum of the sizes of the substrings on the current line */
1433 uint32_t how_far;
1434 /* pointer to start of the subpath */
1435 const char *sub_ptr;
1436 /* size of subpath from sub_ptr */
1437 size_t sub_len;
1438
1439 if ((!ks.str) || (ks.str[0] == '\0')) {
1440 return;
1441 }
1442
1443 /* module name cannot be splitted */
1444 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1445 ly_print_(out, "%s", ks.str);
1446 return;
1447 }
1448
1449 /* after -> for trd_keyword_stmt_body do */
1450
1451 /* set begin indentation */
1452 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1453 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1454 linebreak_was_set = 0;
1455 subpath_printed = 0;
1456 how_far = 0;
1457 sub_ptr = ks.str;
1458 sub_len = 0;
1459
1460 while (sub_ptr[0] != '\0') {
1461 uint32_t ind;
1462 /* skip slash */
1463 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
1464 /* get position of the end of substr */
1465 tmp = strchr(tmp, '/');
1466 /* set correct size if this is a last substring */
1467 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1468 /* actualize sum of the substring's sizes on the current line */
1469 how_far += sub_len;
1470 /* correction due to colon character if it this is last substring */
1471 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1472 /* choose indentation which depends on
1473 * whether the string is printed on multiple lines or not
1474 */
1475 ind = linebreak_was_set ? ind_divided : ind_initial;
1476 if (ind + how_far <= mll) {
1477 /* printing before max line length */
1478 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1479 subpath_printed = 1;
1480 } else {
1481 /* printing on new line */
1482 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001483 /* first subpath is too long
1484 * but print it at first line anyway
1485 */
aPiecek61d062b2020-11-02 11:05:09 +01001486 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1487 subpath_printed = 1;
1488 continue;
1489 }
1490 ly_print_(out, "\n");
1491 ly_print_(out, "%*c", ind_divided, ' ');
1492 linebreak_was_set = 1;
1493 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1494 how_far = sub_len;
1495 subpath_printed = 1;
1496 }
1497 }
1498}
1499
1500/**
aPiecek874ea4d2021-04-19 12:26:36 +02001501 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001502 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001503 * @param[in] grp_has_data is flag only for grouping section.
1504 * Set to 1 if grouping section has some nodes.
1505 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001506 * @param[in,out] out is output handler.
1507 */
1508static void
aPiecekdc8fd572021-04-19 10:47:23 +02001509trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001510{
1511 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001512 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1513 return;
1514 } else {
1515 ly_print_(out, ":");
1516 }
aPiecek61d062b2020-11-02 11:05:09 +01001517 }
1518}
1519
1520/**
1521 * @brief Print entire struct trt_keyword_stmt structure.
1522 * @param[in] ks is item to print.
1523 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001524 * @param[in] grp_has_data is flag only for grouping section.
1525 * Set to 1 if grouping section has some nodes.
1526 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001527 * @param[in,out] out is output handler.
1528 */
1529static void
aPiecek874ea4d2021-04-19 12:26:36 +02001530trp_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 +01001531{
1532 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1533 return;
1534 }
1535 trt_print_keyword_stmt_begin(ks, out);
1536 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001537 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001538}
1539
aPiecek874ea4d2021-04-19 12:26:36 +02001540/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001541 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001542 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001543
1544/**
aPiecek874ea4d2021-04-19 12:26:36 +02001545 * @brief Printing one line including wrapper and node
1546 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001547 * @param[in] node is \<node\> representation.
1548 * @param[in] pck contains special printing functions callback.
1549 * @param[in] indent contains wrapper and indent in node numbers.
1550 * @param[in,out] out is output handler.
1551 */
1552static void
1553trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1554{
1555 trp_print_wrapper(indent.wrapper, out);
1556 trp_print_node(node, pck, indent.in_node, out);
1557}
1558
1559/**
aPiecek874ea4d2021-04-19 12:26:36 +02001560 * @brief Printing one line including wrapper and
1561 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001562 * @param[in] node is \<node\> representation.
1563 * @param[in] wr is wrapper for printing indentation before node.
1564 * @param[in] out is output handler.
1565 */
1566static void
1567trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1568{
1569 trp_print_wrapper(wr, out);
1570 trp_print_node_up_to_name(node, out);
1571}
1572
1573/**
aPiecek874ea4d2021-04-19 12:26:36 +02001574 * @brief Check if leafref target must be change to string 'leafref'
1575 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001576 * @param[in] node containing leafref target.
1577 * @param[in] wr is wrapper for printing indentation before node.
1578 * @param[in] mll is max line length.
1579 * @param[in] out is output handler.
1580 * @return true if leafref must be changed to string 'leafref'.
1581 */
1582static ly_bool
1583trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1584{
1585 struct ly_out_clb_arg *data;
1586
1587 if (node.type.type != TRD_TYPE_TARGET) {
1588 return 0;
1589 }
1590
1591 /* set ly_out to counting characters */
1592 data = out->method.clb.arg;
1593
1594 data->counter = 0;
1595 data->mode = TRD_CHAR_COUNT;
1596 /* count number of printed bytes */
1597 trp_print_wrapper(wr, out);
1598 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1599 trp_print_divided_node_up_to_name(node, out);
1600 data->mode = TRD_PRINT;
1601
1602 return data->counter + strlen(node.type.str) > mll;
1603}
1604
1605/**
1606 * @brief Get default indent in node based on node values.
1607 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001608 * @return Default indent in node assuming that the node
1609 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001610 */
1611static struct trt_indent_in_node
1612trp_default_indent_in_node(struct trt_node node)
1613{
1614 struct trt_indent_in_node ret;
1615
1616 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1617
1618 /* btw_name_opts */
1619 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1620
1621 /* btw_opts_type */
1622 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1623 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1624 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1625 TRD_INDENT_BEFORE_TYPE;
1626 } else {
1627 ret.btw_opts_type = 0;
1628 }
1629
1630 /* btw_type_iffeatures */
1631 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1632
1633 return ret;
1634}
1635
1636/**
1637 * @brief Setting linebreaks in trt_indent_in_node.
1638 *
1639 * The order where the linebreak tag can be placed is from the end.
1640 *
aPiecek874ea4d2021-04-19 12:26:36 +02001641 * @param[in] indent containing alignment lengths
1642 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001643 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001644 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1645 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001646 */
1647static struct trt_indent_in_node
1648trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1649{
1650 /* somewhere must be set a line break in node */
1651 struct trt_indent_in_node ret = indent;
1652
1653 /* gradually break the node from the end */
1654 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1655 ret.btw_type_iffeatures = TRD_LINEBREAK;
1656 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1657 ret.btw_opts_type = TRD_LINEBREAK;
1658 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1659 /* set line break between name and opts */
1660 ret.btw_name_opts = TRD_LINEBREAK;
1661 } else {
1662 /* it is not possible to place a more line breaks,
1663 * unfortunately the max_line_length constraint is violated
1664 */
1665 ret.type = TRD_INDENT_IN_NODE_FAILED;
1666 }
1667 return ret;
1668}
1669
1670/**
1671 * @brief Get the first half of the node based on the linebreak mark.
1672 *
1673 * Items in the second half of the node will be empty.
1674 *
1675 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001676 * @param[in] indent contains information in which part of the \<node\>
1677 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001678 * @return first half of the node, indent is unchanged.
1679 */
1680static struct trt_pair_indent_node
1681trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1682{
1683 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1684
1685 if (indent.btw_name_opts == TRD_LINEBREAK) {
1686 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1687 ret.node.type = TRP_EMPTY_TRT_TYPE;
1688 ret.node.iffeatures = 0;
1689 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1690 ret.node.type = TRP_EMPTY_TRT_TYPE;
1691 ret.node.iffeatures = 0;
1692 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1693 ret.node.iffeatures = 0;
1694 }
1695
1696 return ret;
1697}
1698
1699/**
1700 * @brief Get the second half of the node based on the linebreak mark.
1701 *
1702 * Items in the first half of the node will be empty.
1703 * Indentations belonging to the first node will be reset to zero.
1704 *
1705 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001706 * @param[in] indent contains information in which part of the \<node\>
1707 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001708 * @return second half of the node, indent is newly set.
1709 */
1710static struct trt_pair_indent_node
1711trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1712{
1713 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1714
1715 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001716 /* Logically, the information up to token <opts> should
1717 * be deleted, but the the trp_print_node function needs it to
1718 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001719 */
1720 ret.indent.btw_name_opts = 0;
1721 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1722 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1723 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1724 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1725 ret.indent.btw_name_opts = 0;
1726 ret.indent.btw_opts_type = 0;
1727 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1728 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1729 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1730 ret.node.type = TRP_EMPTY_TRT_TYPE;
1731 ret.indent.btw_name_opts = 0;
1732 ret.indent.btw_opts_type = 0;
1733 ret.indent.btw_type_iffeatures = 0;
1734 }
1735 return ret;
1736}
1737
1738/**
1739 * @brief Get the correct alignment for the node.
1740 *
aPiecek874ea4d2021-04-19 12:26:36 +02001741 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001742 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001743 *
1744 * @param[in] node is \<node\> representation.
1745 * @param[in] pck contains speciall callback functions for printing.
1746 * @param[in] indent contains wrapper and indent in node numbers.
1747 * @param[in] mll is max line length.
1748 * @param[in,out] cnt counting number of characters to print.
1749 * @param[in,out] out is output handler.
1750 * @return pair of node and indentation numbers of that node.
1751 */
1752static struct trt_pair_indent_node
1753trp_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)
1754{
1755 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1756
1757 trp_print_line(node, pck, indent, out);
1758
1759 if (*cnt <= mll) {
1760 /* success */
1761 return ret;
1762 } else {
1763 ret.indent = trp_indent_in_node_place_break(ret.indent);
1764 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1765 /* erase information in node due to line break */
1766 ret = trp_first_half_node(node, ret.indent);
1767 /* check if line fits, recursive call */
1768 *cnt = 0;
1769 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1770 /* make sure that the result will be with the status divided
1771 * or eventually with status failed */
1772 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1773 }
1774 return ret;
1775 }
1776}
1777
1778/**
1779 * @brief Get the correct alignment for the node.
1780 *
1781 * @param[in] node is \<node\> representation.
1782 * @param[in] pck contains speciall callback functions for printing.
1783 * @param[in] indent contains wrapper and indent in node numbers.
1784 * @param[in] mll is max line length.
1785 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001786 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1787 * line, some indent variable has negative value as a line break sign.
1788 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1789 * all indent variables values has non-negative number.
1790 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1791 * line, all indent variables has negative or zero values,
1792 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001793 */
1794static struct trt_pair_indent_node
1795trp_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)
1796{
1797 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1798 struct ly_out_clb_arg *data;
1799
1800 /* set ly_out to counting characters */
1801 data = out->method.clb.arg;
1802
1803 data->counter = 0;
1804 data->mode = TRD_CHAR_COUNT;
1805 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1806 data->mode = TRD_PRINT;
1807
1808 return ret;
1809}
1810
1811/**
aPiecek01598c02021-04-23 14:18:24 +02001812 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001813 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001814 * @param[in] node is node representation.
1815 * @param[in] ppck contains speciall callback functions for printing.
1816 * @param[in] ipck contains wrapper and indent in node numbers.
1817 * @param[in] mll is max line length.
1818 * @param[in,out] out is output handler.
1819 */
1820static void
1821trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1822{
1823 ly_bool entire_node_was_printed;
1824 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1825
1826 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1827 /* nothing can be done, continue as usual */
1828 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1829 }
1830
1831 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1832 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1833
1834 if (!entire_node_was_printed) {
1835 ly_print_(out, "\n");
1836 /* continue with second half node */
1837 ind_node = trp_second_half_node(node, ind_node.indent);
1838 /* continue with printing node */
1839 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1840 } else {
1841 return;
1842 }
1843}
1844
1845/**
aPiecek874ea4d2021-04-19 12:26:36 +02001846 * @brief Printing of the wrapper and the whole node,
1847 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001848 * @param[in] node is node representation.
1849 * @param[in] ppck contains speciall callback functions for printing.
1850 * @param[in] ipck contains wrapper and indent in node numbers.
1851 * @param[in] mll is max line length.
1852 * @param[in,out] out is output handler.
1853 */
1854static void
1855trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1856{
1857 struct trt_pair_indent_node ind_node1;
1858 struct trt_pair_indent_node ind_node2;
1859 struct trt_pck_indent tmp;
1860
1861 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1862 node.type.type = TRD_TYPE_LEAFREF;
1863 }
1864
1865 /* check if normal indent is possible */
1866 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1867
1868 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1869 /* node fits to one line */
1870 trp_print_line(node, ppck, ipck, out);
1871 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1872 /* node will be divided */
1873 /* print first half */
1874 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1875 /* pretend that this is normal node */
1876 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1877
1878 trp_print_line(ind_node1.node, ppck, tmp, out);
1879 ly_print_(out, "\n");
1880
1881 /* continue with second half on new line */
1882 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1883 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1884
1885 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1886 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1887 /* node name is too long */
1888 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1889
1890 if (trp_node_body_is_empty(node)) {
1891 return;
1892 } else {
1893 ly_print_(out, "\n");
1894
1895 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1896 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1897 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1898
1899 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1900 }
1901
1902 }
1903}
1904
aPiecek874ea4d2021-04-19 12:26:36 +02001905/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001906 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001907 *********************************************************************/
1908
1909/**
1910 * @brief Get nodetype.
1911 * @param[in] node is any lysp_node.
1912 */
1913static uint16_t
1914trop_nodetype(const void *node)
1915{
1916 return ((const struct lysp_node *)node)->nodetype;
1917}
1918
1919/**
1920 * @brief Get sibling.
1921 * @param[in] node is any lysp_node.
1922 */
1923static const void *
1924trop_next(const void *node)
1925{
1926 return ((const struct lysp_node *)node)->next;
1927}
1928
1929/**
1930 * @brief Get parent.
1931 * @param[in] node is any lysp_node.
1932 */
1933static const void *
1934trop_parent(const void *node)
1935{
1936 return ((const struct lysp_node *)node)->parent;
1937}
1938
1939/**
1940 * @brief Try to get child.
1941 * @param[in] node is any lysp_node.
1942 */
1943static const void *
1944trop_child(const void *node)
1945{
1946 return lysp_node_child(node);
1947}
1948
1949/**
1950 * @brief Try to get action.
1951 * @param[in] node is any lysp_node.
1952 */
1953static const void *
1954trop_actions(const void *node)
1955{
1956 return lysp_node_actions(node);
1957}
1958
1959/**
1960 * @brief Try to get action.
1961 * @param[in] node must be of type lysp_node_action.
1962 */
1963static const void *
1964trop_action_input(const void *node)
1965{
1966 return &((const struct lysp_node_action *)node)->input;
1967}
1968
1969/**
1970 * @brief Try to get action.
1971 * @param[in] node must be of type lysp_node_action.
1972 */
1973static const void *
1974trop_action_output(const void *node)
1975{
1976 return &((const struct lysp_node_action *)node)->output;
1977}
1978
1979/**
1980 * @brief Try to get action.
1981 * @param[in] node is any lysp_node.
1982 */
1983static const void *
1984trop_notifs(const void *node)
1985{
1986 return lysp_node_notifs(node);
1987}
1988
1989/**
aPiecek01598c02021-04-23 14:18:24 +02001990 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02001991 * which are adapted to lysp nodes.
1992 */
1993static struct tro_getters
1994trop_init_getters()
1995{
1996 return (struct tro_getters) {
1997 .nodetype = trop_nodetype,
1998 .next = trop_next,
1999 .parent = trop_parent,
2000 .child = trop_child,
2001 .actions = trop_actions,
2002 .action_input = trop_action_input,
2003 .action_output = trop_action_output,
2004 .notifs = trop_notifs
2005 };
2006}
2007
aPiecek3f247652021-04-19 13:40:25 +02002008/**
2009 * @brief Get nodetype.
2010 * @param[in] node is any lysc_node.
2011 */
2012static uint16_t
2013troc_nodetype(const void *node)
2014{
2015 return ((const struct lysc_node *)node)->nodetype;
2016}
2017
2018/**
2019 * @brief Get sibling.
2020 * @param[in] node is any lysc_node.
2021 */
2022static const void *
2023troc_next(const void *node)
2024{
2025 return ((const struct lysc_node *)node)->next;
2026}
2027
2028/**
2029 * @brief Get parent.
2030 * @param[in] node is any lysc_node.
2031 */
2032static const void *
2033troc_parent(const void *node)
2034{
2035 return ((const struct lysc_node *)node)->parent;
2036}
2037
2038/**
2039 * @brief Try to get child.
2040 * @param[in] node is any lysc_node.
2041 */
2042static const void *
2043troc_child(const void *node)
2044{
2045 return lysc_node_child(node);
2046}
2047
2048/**
2049 * @brief Try to get action.
2050 * @param[in] node is any lysc_node.
2051 */
2052static const void *
2053troc_actions(const void *node)
2054{
2055 return lysc_node_actions(node);
2056}
2057
2058/**
2059 * @brief Try to get action.
2060 * @param[in] node must be of type lysc_node_action.
2061 */
2062static const void *
2063troc_action_input(const void *node)
2064{
2065 return &((const struct lysc_node_action *)node)->input;
2066}
2067
2068/**
2069 * @brief Try to get action.
2070 * @param[in] node must be of type lysc_node_action.
2071 */
2072static const void *
2073troc_action_output(const void *node)
2074{
2075 return &((const struct lysc_node_action *)node)->output;
2076}
2077
2078/**
2079 * @brief Try to get action.
2080 * @param[in] node is any lysc_node.
2081 */
2082static const void *
2083troc_notifs(const void *node)
2084{
2085 return lysc_node_notifs(node);
2086}
2087
2088/**
aPiecek01598c02021-04-23 14:18:24 +02002089 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002090 * which are adapted to lysc nodes.
2091 */
2092static struct tro_getters
2093troc_init_getters()
2094{
2095 return (struct tro_getters) {
2096 .nodetype = troc_nodetype,
2097 .next = troc_next,
2098 .parent = troc_parent,
2099 .child = troc_child,
2100 .actions = troc_actions,
2101 .action_input = troc_action_input,
2102 .action_output = troc_action_output,
2103 .notifs = troc_notifs
2104 };
2105}
2106
aPiecekef1e58e2021-04-19 13:19:44 +02002107/**********************************************************************
2108 * tro functions
2109 *********************************************************************/
2110
2111/**
2112 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002113 *
2114 * This is a general algorithm that is able to
2115 * work with lysp_node or lysc_node.
2116 *
2117 * @param[in] node points to lysp_node or lysc_node.
2118 * @param[in] lysc_tree flag to determine what type the @p node is.
2119 * If set to true, then @p points to lysc_node otherwise lysp_node.
2120 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002121 */
2122static const void *
aPiecek3f247652021-04-19 13:40:25 +02002123tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002124{
2125 struct tro_getters get;
2126 const void *tmp, *parent;
2127 const void *ret;
2128
2129 assert(node);
2130
aPiecek3f247652021-04-19 13:40:25 +02002131 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002132
2133 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2134 if ((tmp = get.next(node))) {
2135 /* next action exists */
2136 ret = tmp;
2137 } else if ((parent = get.parent(node))) {
2138 /* maybe if notif exists as sibling */
2139 ret = get.notifs(parent);
2140 } else {
2141 ret = NULL;
2142 }
2143 } else if (get.nodetype(node) & LYS_INPUT) {
2144 if ((parent = get.parent(node))) {
2145 /* if output action has data */
2146 if (get.child(get.action_output(parent))) {
2147 /* then next sibling is output action */
2148 ret = get.action_output(parent);
2149 } else {
2150 /* input action cannot have siblings other
2151 * than output action.
2152 */
2153 ret = NULL;
2154 }
2155 } else {
2156 /* there is no way how to get output action */
2157 ret = NULL;
2158 }
2159 } else if (get.nodetype(node) & LYS_OUTPUT) {
2160 /* output action cannot have siblings */
2161 ret = NULL;
2162 } else if (get.nodetype(node) & LYS_NOTIF) {
2163 /* must have as a sibling only notif */
2164 ret = get.next(node);
2165 } else {
2166 /* for rest of nodes */
2167 if ((tmp = get.next(node))) {
2168 /* some sibling exists */
2169 ret = tmp;
2170 } else if ((parent = get.parent(node))) {
2171 /* Action and notif are siblings too.
2172 * They can be reached through parent.
2173 */
2174 if ((tmp = get.actions(parent))) {
2175 /* next sibling is action */
2176 ret = tmp;
2177 } else if ((tmp = get.notifs(parent))) {
2178 /* next sibling is notif */
2179 ret = tmp;
2180 } else {
2181 /* sibling not exists */
2182 ret = NULL;
2183 }
2184 } else {
2185 /* sibling not exists */
2186 ret = NULL;
2187 }
2188 }
2189
2190 return ret;
2191}
2192
2193/**
2194 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002195 *
2196 * This is a general algorithm that is able to
2197 * work with lysp_node or lysc_node.
2198 *
2199 * @param[in] node points to lysp_node or lysc_node.
2200 * @param[in] lysc_tree flag to determine what type the @p node is.
2201 * If set to true, then @p points to lysc_node otherwise lysp_node.
2202 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002203 */
2204static const void *
aPiecek3f247652021-04-19 13:40:25 +02002205tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002206{
2207 struct tro_getters get;
2208 const void *tmp;
2209 const void *ret;
2210
2211 assert(node);
2212
aPiecek3f247652021-04-19 13:40:25 +02002213 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002214
2215 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2216 if (get.child(get.action_input(node))) {
2217 /* go to LYS_INPUT */
2218 ret = get.action_input(node);
2219 } else if (get.child(get.action_output(node))) {
2220 /* go to LYS_OUTPUT */
2221 ret = get.action_output(node);
2222 } else {
2223 /* input action and output action have no data */
2224 ret = NULL;
2225 }
2226 } else {
2227 if ((tmp = get.child(node))) {
2228 ret = tmp;
2229 } else {
2230 /* current node can't have children or has no children */
2231 /* but maybe has some actions or notifs */
2232 if ((tmp = get.actions(node))) {
2233 ret = tmp;
2234 } else if ((tmp = get.notifs(node))) {
2235 ret = tmp;
2236 } else {
2237 ret = NULL;
2238 }
2239 }
2240 }
2241
2242 return ret;
2243}
2244
2245/**
aPiecek3f247652021-04-19 13:40:25 +02002246 * @brief Get new trt_parent_cache if we apply the transfer
2247 * to the child node in the tree.
2248 * @param[in] ca is parent cache for current node.
2249 * @param[in] tc contains current tree node.
2250 * @return Cache for the current node.
2251 */
2252static struct trt_parent_cache
2253tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2254{
2255 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2256
2257 if (!tc->lysc_tree) {
2258 const struct lysp_node *pn = tc->pn;
2259
2260 ret.ancestor =
2261 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2262 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2263 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2264 ca.ancestor;
2265
2266 ret.lys_status =
2267 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2268 ca.lys_status;
2269
2270 ret.lys_config =
2271 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2272 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2273 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2274 ca.lys_config;
2275
2276 ret.last_list =
2277 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2278 ca.last_list;
2279 }
2280
2281 return ret;
2282}
2283
2284/**
aPiecekef1e58e2021-04-19 13:19:44 +02002285 * @brief Transformation of the Schema nodes flags to
2286 * Tree diagram \<status\>.
2287 * @param[in] flags is node's flags obtained from the tree.
2288 */
2289static trt_status_type
2290tro_flags2status(uint16_t flags)
2291{
2292 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2293 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2294 TRD_STATUS_TYPE_CURRENT;
2295}
2296
2297/**
2298 * @brief Transformation of the Schema nodes flags to Tree diagram
2299 * \<flags\> but more specifically 'ro' or 'rw'.
2300 * @param[in] flags is node's flags obtained from the tree.
2301 */
2302static trt_flags_type
2303tro_flags2config(uint16_t flags)
2304{
2305 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2306 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2307 TRD_FLAGS_TYPE_EMPTY;
2308}
2309
2310/**
aPiecek3f247652021-04-19 13:40:25 +02002311 * @brief Print current node's iffeatures.
2312 * @param[in] tc is tree context.
2313 * @param[in,out] out is output handler.
2314 */
2315static void
2316tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2317{
2318 const struct lysp_qname *iffs;
2319
aPiecekbbc02932021-05-21 07:19:41 +02002320 if (tc->lysc_tree) {
2321 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2322 iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
2323 } else {
2324 iffs = tc->pn->iffeatures;
2325 }
aPiecek3f247652021-04-19 13:40:25 +02002326 LY_ARRAY_COUNT_TYPE i;
2327
2328 LY_ARRAY_FOR(iffs, i) {
2329 if (i == 0) {
2330 ly_print_(out, "%s", iffs[i].str);
2331 } else {
2332 ly_print_(out, ",%s", iffs[i].str);
2333 }
2334 }
2335
2336}
2337
2338/**
2339 * @brief Print current list's keys.
2340 *
2341 * Well, actually printing keys in the lysp_tree is trivial,
2342 * because char* points to all keys. However, special functions have
2343 * been reserved for this, because in principle the list of elements
2344 * can have more implementations.
2345 *
2346 * @param[in] tc is tree context.
2347 * @param[in,out] out is output handler.
2348 */
2349static void
2350tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2351{
2352 const struct lysp_node_list *list;
2353
aPiecekbbc02932021-05-21 07:19:41 +02002354 if (tc->lysc_tree) {
2355 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2356 list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
2357 } else {
2358 list = (const struct lysp_node_list *)tc->pn;
2359 }
aPiecek3f247652021-04-19 13:40:25 +02002360 assert(list->nodetype & LYS_LIST);
2361
2362 if (trg_charptr_has_data(list->key)) {
2363 ly_print_(out, "%s", list->key);
2364 }
2365}
2366
2367/**
2368 * @brief Get rpcs section if exists.
2369 * @param[in,out] tc is tree context.
2370 * @return Section representation if it exists. The @p tc is modified
2371 * and his pointer points to the first node in rpcs section.
2372 * @return Empty section representation otherwise.
2373 */
2374static struct trt_keyword_stmt
2375tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2376{
aPiecek9f792e52021-04-21 08:33:56 +02002377 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002378 const void *actions;
2379
2380 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002381 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002382 if (actions) {
2383 tc->cn = actions;
2384 }
2385 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002386 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002387 if (actions) {
2388 tc->pn = actions;
2389 tc->tpn = tc->pn;
2390 }
2391 }
2392
2393 if (actions) {
2394 tc->section = TRD_SECT_RPCS;
2395 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2396 } else {
2397 return TRP_EMPTY_KEYWORD_STMT;
2398 }
2399}
2400
2401/**
2402 * @brief Get notification section if exists
2403 * @param[in,out] tc is tree context.
2404 * @return Section representation if it exists.
2405 * The @p tc is modified and his pointer points to the
2406 * first node in notification section.
2407 * @return Empty section representation otherwise.
2408 */
2409static struct trt_keyword_stmt
2410tro_modi_get_notifications(struct trt_tree_ctx *tc)
2411{
aPiecek9f792e52021-04-21 08:33:56 +02002412 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002413 const void *notifs;
2414
2415 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002416 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002417 if (notifs) {
2418 tc->cn = notifs;
2419 }
2420 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002421 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002422 if (notifs) {
2423 tc->pn = notifs;
2424 tc->tpn = tc->pn;
2425 }
2426 }
2427
2428 if (notifs) {
2429 tc->section = TRD_SECT_NOTIF;
2430 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2431 } else {
2432 return TRP_EMPTY_KEYWORD_STMT;
2433 }
2434}
2435
2436/**
aPiecek96baa7f2021-04-23 12:32:00 +02002437 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002438 *
2439 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002440 * @param[in] u is index to the array of extensions (lysc_ext_instance
2441 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002442 * @return Section representation if it exists.
2443 * @return Empty section representation otherwise.
2444 */
2445static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002446tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002447{
aPiecek96baa7f2021-04-23 12:32:00 +02002448 assert(tc);
2449 const void *node;
2450 const char *yang_data_name;
2451
2452 if (tc->lysc_tree) {
2453 struct lysc_ext_instance *exts;
2454 struct lysc_ext_substmt *substmts;
2455
2456 exts = tc->cmod->exts;
2457 substmts = exts[u].substmts;
2458 if (!substmts) {
2459 return TRP_EMPTY_KEYWORD_STMT;
2460 }
2461 node = *(const struct lysc_node **)substmts->storage;
2462 yang_data_name = exts[u].argument;
2463 } else {
2464 struct lysp_ext_instance *exts;
2465
2466 exts = tc->pmod->exts;
2467 node = exts[u].parsed;
2468 yang_data_name = exts[u].argument;
2469 }
2470
2471 if (tc->lysc_tree) {
2472 tc->cn = node;
2473 } else {
2474 tc->tpn_ext = &tc->pmod->exts[u];
2475 tc->pn = node;
2476 }
2477
2478 if (node) {
2479 tc->section = TRD_SECT_YANG_DATA;
2480 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2481 } else {
2482 return TRP_EMPTY_KEYWORD_STMT;
2483 }
aPiecekef1e58e2021-04-19 13:19:44 +02002484}
2485
2486/**
2487 * @brief Get name of the module.
2488 * @param[in] tc is context of the tree.
2489 */
2490static struct trt_keyword_stmt
2491tro_read_module_name(const struct trt_tree_ctx *tc)
2492{
aPiecek9f792e52021-04-21 08:33:56 +02002493 assert(tc);
2494
2495 struct trt_keyword_stmt ret;
2496
2497 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2498 TRD_KEYWORD_SUBMODULE :
2499 TRD_KEYWORD_MODULE;
2500
2501 ret.str = !tc->lysc_tree ?
2502 LYSP_MODULE_NAME(tc->pmod) :
2503 tc->cmod->mod->name;
2504
2505 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002506}
2507
aPiecekb8d5a0a2021-05-20 08:20:24 +02002508/**
2509 * @brief Create implicit "case" node as parent of @p node.
2510 * @param[in] node child of implicit case node.
2511 * @return The case node ready to print.
2512 */
2513static struct trt_node
2514tro_create_implicit_case_node(struct trt_node node)
2515{
2516 struct trt_node ret;
2517
2518 ret.status = node.status;
2519 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2520 ret.name.type = TRD_NODE_CASE;
2521 ret.name.module_prefix = node.name.module_prefix;
2522 ret.name.str = node.name.str;
2523 ret.type = TRP_EMPTY_TRT_TYPE;
aPiecek7a28e2f2021-05-21 07:27:03 +02002524 ret.iffeatures = 0;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002525 ret.last_one = node.last_one;
2526
2527 return ret;
2528}
2529
aPiecekef1e58e2021-04-19 13:19:44 +02002530/**********************************************************************
2531 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002532 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002533
2534/**
aPiecek61d062b2020-11-02 11:05:09 +01002535 * @brief Check if list statement has keys.
2536 * @param[in] pn is pointer to the list.
2537 * @return 1 if has keys, otherwise 0.
2538 */
2539static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002540trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002541{
aPiecekef1e58e2021-04-19 13:19:44 +02002542 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002543}
2544
2545/**
2546 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002547 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002548 * @return 1 if has if-features, otherwise 0.
2549 */
2550static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002551trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002552{
2553 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002554 const struct lysp_qname *iffs;
2555
aPiecek61d062b2020-11-02 11:05:09 +01002556 ly_bool ret = 0;
2557
aPiecekef1e58e2021-04-19 13:19:44 +02002558 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002559 LY_ARRAY_FOR(iffs, u) {
2560 ret = 1;
2561 break;
2562 }
2563 return ret;
2564}
2565
2566/**
2567 * @brief Find out if leaf is also the key in last list.
2568 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002569 * @param[in] ca_last_list is pointer to last visited list.
2570 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002571 * @return 1 if leaf is also the key, otherwise 0.
2572 */
2573static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002574trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002575{
2576 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2577 const struct lysp_node_list *list = ca_last_list;
2578
2579 if (!list) {
2580 return 0;
2581 }
2582 return trg_charptr_has_data(list->key) ?
2583 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2584}
2585
2586/**
2587 * @brief Check if container's type is presence.
2588 * @param[in] pn is pointer to container.
2589 * @return 1 if container has presence statement, otherwise 0.
2590 */
2591static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002592trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002593{
2594 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2595}
2596
2597/**
2598 * @brief Get leaflist's path without lysp_node type control.
2599 * @param[in] pn is pointer to the leaflist.
2600 */
2601static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002602trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002603{
2604 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2605
2606 return list->type.path ? list->type.path->expr : NULL;
2607}
2608
2609/**
2610 * @brief Get leaflist's type name without lysp_node type control.
2611 * @param[in] pn is pointer to the leaflist.
2612 */
2613static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002614trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002615{
2616 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2617
2618 return list->type.name;
2619}
2620
2621/**
2622 * @brief Get leaf's path without lysp_node type control.
2623 * @param[in] pn is pointer to the leaf node.
2624 */
2625static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002626trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002627{
2628 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2629
2630 return leaf->type.path ? leaf->type.path->expr : NULL;
2631}
2632
2633/**
2634 * @brief Get leaf's type name without lysp_node type control.
2635 * @param[in] pn is pointer to the leaf's type name.
2636 */
2637static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002638trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002639{
2640 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2641
2642 return leaf->type.name;
2643}
2644
2645/**
aPiecek874ea4d2021-04-19 12:26:36 +02002646 * @brief Get pointer to data using node type specification
2647 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002648 *
aPiecek874ea4d2021-04-19 12:26:36 +02002649 * @param[in] flags is node type specification.
2650 * If it is the correct node, the getter function is called.
2651 * @param[in] f is getter function which provides the desired
2652 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002653 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002654 * @return NULL if node has wrong type or getter function return
2655 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002656 * @return Pointer to desired char pointer obtained from the node.
2657 */
2658static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002659trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002660{
2661 if (pn->nodetype & flags) {
2662 const char *ret = f(pn);
2663 return trg_charptr_has_data(ret) ? ret : NULL;
2664 } else {
2665 return NULL;
2666 }
2667}
2668
2669/**
aPiecek61d062b2020-11-02 11:05:09 +01002670 * @brief Resolve \<status\> of the current node.
2671 * @param[in] nodetype is node's type obtained from the tree.
2672 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002673 * @param[in] ca_lys_status is inherited status
2674 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002675 * @return The status type.
2676 */
2677static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002678trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002679{
2680 /* LYS_INPUT and LYS_OUTPUT is special case */
2681 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002682 return tro_flags2status(ca_lys_status);
2683 /* if ancestor's status is deprc or obslt
2684 * and also node's status is not set
2685 */
aPiecek61d062b2020-11-02 11:05:09 +01002686 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2687 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002688 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002689 } else {
2690 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002691 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002692 }
2693}
2694
2695/**
2696 * @brief Resolve \<flags\> of the current node.
2697 * @param[in] nodetype is node's type obtained from the tree.
2698 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002699 * @param[in] ca_ancestor is ancestor type obtained
2700 * from trt_parent_cache.
2701 * @param[in] ca_lys_config is inherited config item
2702 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002703 * @return The flags type.
2704 */
2705static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002706trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002707{
2708 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2709 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2710 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2711 return TRD_FLAGS_TYPE_RO;
2712 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2713 return TRD_FLAGS_TYPE_RO;
2714 } else if (nodetype & LYS_NOTIF) {
2715 return TRD_FLAGS_TYPE_NOTIF;
2716 } else if (nodetype & LYS_USES) {
2717 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2718 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2719 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002720 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002721 /* config is not set. Look at ancestor's config */
2722 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002723 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002724 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002725 }
2726}
2727
2728/**
2729 * @brief Resolve node type of the current node.
2730 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002731 * @param[in] ca_last_list is pointer to the last visited list.
2732 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002733 */
2734static trt_node_type
aPiecekef1e58e2021-04-19 13:19:44 +02002735trop_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002736{
2737 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2738 return TRD_NODE_ELSE;
2739 } else if (pn->nodetype & LYS_CASE) {
2740 return TRD_NODE_CASE;
2741 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2742 return TRD_NODE_OPTIONAL_CHOICE;
2743 } else if (pn->nodetype & LYS_CHOICE) {
2744 return TRD_NODE_CHOICE;
aPiecekef1e58e2021-04-19 13:19:44 +02002745 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002746 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002747 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002748 return TRD_NODE_KEYS;
2749 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2750 return TRD_NODE_LISTLEAFLIST;
2751 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2752 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002753 } 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 +01002754 return TRD_NODE_OPTIONAL;
2755 } else {
2756 return TRD_NODE_ELSE;
2757 }
2758}
2759
2760/**
aPiecekef1e58e2021-04-19 13:19:44 +02002761 * @brief Resolve \<type\> of the current node.
2762 * @param[in] pn is current node.
2763 */
2764static struct trt_type
2765trop_resolve_type(const struct lysp_node *pn)
2766{
2767 const char *tmp = NULL;
2768
2769 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2770 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2771 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2772 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2773 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2774 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2775 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2776 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2777 } else if (pn->nodetype == LYS_ANYDATA) {
2778 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2779 } else if (pn->nodetype & LYS_ANYXML) {
2780 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2781 } else {
2782 return TRP_EMPTY_TRT_TYPE;
2783 }
2784}
2785
2786/**
aPiecek61d062b2020-11-02 11:05:09 +01002787 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002788 * @param[in] ca contains stored important data
2789 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002790 * @param[in] tc is context of the tree.
2791 */
2792static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002793trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002794{
aPiecekef1e58e2021-04-19 13:19:44 +02002795 const struct lysp_node *pn;
2796 struct trt_node ret;
2797
aPiecek61d062b2020-11-02 11:05:09 +01002798 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002799
2800 pn = tc->pn;
2801 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002802
2803 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002804 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002805
2806 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2807 /* <flags> */
aPiecekef1e58e2021-04-19 13:19:44 +02002808 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002809
2810 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2811 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2812 /* set type of the node */
aPiecekef1e58e2021-04-19 13:19:44 +02002813 ret.name.type = trop_resolve_node_type(pn, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002814
2815 /* TODO: ret.name.module_prefix is not supported right now. */
2816 ret.name.module_prefix = NULL;
2817
2818 /* set node's name */
2819 ret.name.str = pn->name;
2820
2821 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002822 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002823
2824 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002825 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002826
aPiecek3f247652021-04-19 13:40:25 +02002827 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002828
2829 return ret;
2830}
2831
aPiecekef1e58e2021-04-19 13:19:44 +02002832/**
2833 * @brief Find out if the current node has siblings.
2834 * @param[in] tc is context of the tree.
2835 * @return 1 if sibling exists otherwise 0.
2836 */
2837static ly_bool
2838trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2839{
aPiecek3f247652021-04-19 13:40:25 +02002840 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002841}
2842
aPiecek96baa7f2021-04-23 12:32:00 +02002843/**
2844 * @brief Print all yang-data sections and print three dots instead
2845 * of nodes.
2846 * @param[in] exts is array of YANG extension instances from parsed
2847 * module (@ref sizedarrays).
2848 * @param[in] mll is maximum number of characters that can be printed
2849 * on one line.
2850 * @param[in,out] out is output handler.
2851 */
2852static void
2853trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2854{
2855 struct trt_keyword_stmt ks;
2856 LY_ARRAY_COUNT_TYPE u;
2857 struct trt_wrapper wr;
2858
2859 if (!exts) {
2860 return;
2861 }
2862
2863 ly_print_(out, "\n");
2864 ks.type = TRD_KEYWORD_YANG_DATA;
2865 wr = TRP_INIT_WRAPPER_BODY;
2866
2867 LY_ARRAY_FOR(exts, u) {
2868 ly_print_(out, "\n");
2869
2870 /* yang-data <yang-data-name>: */
2871 ks.str = exts[u].argument;
2872 trp_print_keyword_stmt(ks, mll, 0, out);
2873 ly_print_(out, "\n");
2874
2875 /* ... */
2876 trp_print_wrapper(wr, out);
2877 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2878 }
2879}
2880
aPiecek874ea4d2021-04-19 12:26:36 +02002881/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002882 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002883 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002884
2885/**
aPiecek874ea4d2021-04-19 12:26:36 +02002886 * @brief Change current node pointer to its parent
2887 * but only if parent exists.
2888 * @param[in,out] tc is tree context.
2889 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002890 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002891 * @return 0 if the node did not have parents.
2892 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002893 */
2894static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002895trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002896{
2897 assert(tc && tc->pn);
2898 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002899 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002900 tc->pn = tc->pn->parent;
2901 return 1;
2902 } else {
2903 return 0;
2904 }
2905}
2906
2907/**
aPiecek874ea4d2021-04-19 12:26:36 +02002908 * @brief Change the current node pointer to its child
2909 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002910 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002911 * @param[in,out] tc is context of the tree.
2912 * Contains pointer to the current node.
2913 * @return Non-empty \<node\> representation of the current
2914 * node's child. The @p tc is modified.
2915 * @return Empty \<node\> representation if child don't exists.
2916 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002917 */
2918static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002919trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002920{
aPiecekef1e58e2021-04-19 13:19:44 +02002921 const struct lysp_node *tmp;
2922
aPiecek61d062b2020-11-02 11:05:09 +01002923 assert(tc && tc->pn);
2924
aPiecek3f247652021-04-19 13:40:25 +02002925 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002926 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02002927 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01002928 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002929 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002930 }
2931}
2932
2933/**
aPiecek874ea4d2021-04-19 12:26:36 +02002934 * @brief Change the current node pointer to the first child of node's
2935 * parent. If current node is already first sibling/child then nothing
2936 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01002937 * @param[in,out] tc is tree context.
2938 */
2939static void
aPiecekef1e58e2021-04-19 13:19:44 +02002940trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002941{
aPiecek9f792e52021-04-21 08:33:56 +02002942 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01002943
aPiecekef1e58e2021-04-19 13:19:44 +02002944 if (trop_modi_parent(tc)) {
2945 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002946 } else {
2947 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01002948 switch (tc->section) {
2949 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02002950 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02002951 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002952 break;
2953 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02002954 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02002955 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002956 break;
2957 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02002958 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02002959 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002960 break;
2961 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02002962 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02002963 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002964 break;
2965 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02002966 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02002967 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002968 break;
2969 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02002970 /* tpn in this case is of type lysp_ext_instance */
2971 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01002972 break;
aPiecek96baa7f2021-04-23 12:32:00 +02002973 default:
2974 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01002975 }
aPiecek61d062b2020-11-02 11:05:09 +01002976 }
2977}
2978
2979/**
aPiecek874ea4d2021-04-19 12:26:36 +02002980 * @brief Change the pointer to the current node to its next sibling
2981 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002982 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002983 * @param[in,out] tc is tree context.
2984 * Contains pointer to the current node.
2985 * @return Non-empty \<node\> representation if sibling exists.
2986 * The @p tc is modified.
2987 * @return Empty \<node\> representation otherwise.
2988 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002989 */
2990static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002991trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002992{
aPiecekef1e58e2021-04-19 13:19:44 +02002993 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002994
2995 assert(tc && tc->pn);
2996
aPiecek3f247652021-04-19 13:40:25 +02002997 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002998
aPiecekef1e58e2021-04-19 13:19:44 +02002999 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003000 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3001 tc->tpn = pn;
3002 }
aPiecekef1e58e2021-04-19 13:19:44 +02003003 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003004 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003005 } else {
3006 return TRP_EMPTY_NODE;
3007 }
3008}
3009
3010/**
3011 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003012 * @param[in,out] tc is tree context. It is modified and his current
3013 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003014 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003015 * @return Empty section structure otherwise.
3016 */
3017static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003018trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003019{
aPiecek9f792e52021-04-21 08:33:56 +02003020 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003021 const struct lysp_node_augment *augs;
3022
3023 /* if next_augment func was called for the first time */
3024 if (tc->section != TRD_SECT_AUGMENT) {
3025 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003026 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003027 } else {
3028 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003029 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003030 }
3031
aPiecekdc8fd572021-04-19 10:47:23 +02003032 if (augs) {
3033 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003034 tc->tpn = tc->pn;
3035 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3036 } else {
3037 return TRP_EMPTY_KEYWORD_STMT;
3038 }
3039}
3040
3041/**
aPiecek61d062b2020-11-02 11:05:09 +01003042 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003043 * @param[in,out] tc is tree context. It is modified and his current
3044 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003045 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003046 * @return Empty section representation otherwise.
3047 */
3048static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003049trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003050{
aPiecek9f792e52021-04-21 08:33:56 +02003051 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003052 const struct lysp_node_grp *grps;
3053
3054 if (tc->section != TRD_SECT_GROUPING) {
3055 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003056 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003057 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003058 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003059 }
3060
aPiecekdc8fd572021-04-19 10:47:23 +02003061 if (grps) {
3062 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003063 tc->tpn = tc->pn;
3064 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3065 } else {
3066 return TRP_EMPTY_KEYWORD_STMT;
3067 }
3068}
3069
aPiecek874ea4d2021-04-19 12:26:36 +02003070/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003071 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003072 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003073
3074/**
aPiecek3f247652021-04-19 13:40:25 +02003075 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003076 */
aPiecek3f247652021-04-19 13:40:25 +02003077static ly_bool
3078troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003079{
aPiecek3f247652021-04-19 13:40:25 +02003080 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3081}
aPiecek61d062b2020-11-02 11:05:09 +01003082
aPiecek3f247652021-04-19 13:40:25 +02003083/**
3084 * @brief Resolve \<flags\> of the current node.
3085 *
3086 * Use this function only if trt_tree_ctx.lysc_tree is true.
3087 *
3088 * @param[in] nodetype is current lysc_node.nodetype.
3089 * @param[in] flags is current lysc_node.flags.
3090 * @return The flags type.
3091 */
3092static trt_flags_type
3093troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3094{
3095 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3096 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3097 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3098 return TRD_FLAGS_TYPE_RO;
3099 } else if (nodetype & LYS_IS_NOTIF) {
3100 return TRD_FLAGS_TYPE_RO;
3101 } else if (nodetype & LYS_NOTIF) {
3102 return TRD_FLAGS_TYPE_NOTIF;
3103 } else if (nodetype & LYS_USES) {
3104 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3105 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3106 return TRD_FLAGS_TYPE_RPC;
3107 } else {
3108 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003109 }
aPiecek61d062b2020-11-02 11:05:09 +01003110}
3111
3112/**
aPiecek3f247652021-04-19 13:40:25 +02003113 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003114 *
aPiecek3f247652021-04-19 13:40:25 +02003115 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003116 *
aPiecek3f247652021-04-19 13:40:25 +02003117 * @param[in] nodetype is current lysc_node.nodetype.
3118 * @param[in] flags is current lysc_node.flags.
3119 */
3120static trt_node_type
3121troc_resolve_node_type(uint16_t nodetype, uint16_t flags)
3122{
3123 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3124 return TRD_NODE_ELSE;
3125 } else if (nodetype & LYS_CASE) {
3126 return TRD_NODE_CASE;
3127 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3128 return TRD_NODE_OPTIONAL_CHOICE;
3129 } else if (nodetype & LYS_CHOICE) {
3130 return TRD_NODE_CHOICE;
3131 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3132 return TRD_NODE_CONTAINER;
3133 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3134 return TRD_NODE_KEYS;
3135 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3136 return TRD_NODE_LISTLEAFLIST;
3137 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3138 return TRD_NODE_OPTIONAL;
3139 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3140 return TRD_NODE_OPTIONAL;
3141 } else {
3142 return TRD_NODE_ELSE;
3143 }
3144}
3145
3146/**
3147 * @brief Transformation of current lysc_node to struct trt_node.
3148 * @param[in] ca is not used.
3149 * @param[in] tc is context of the tree.
3150 */
3151static struct trt_node
3152troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3153{
3154 (void) ca;
3155 const struct lysc_node *cn;
3156 struct trt_node ret;
3157
aPiecek082c7dc2021-05-20 08:55:07 +02003158 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003159
3160 cn = tc->cn;
3161 ret = TRP_EMPTY_NODE;
3162
3163 /* <status> */
3164 ret.status = tro_flags2status(cn->flags);
3165
3166 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
3167 /* <flags> */
3168 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3169
3170 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
3171 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
3172 /* set type of the node */
3173 ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
3174
3175 /* TODO: ret.name.module_prefix is not supported right now. */
3176 ret.name.module_prefix = NULL;
3177
3178 /* set node's name */
3179 ret.name.str = cn->name;
3180
aPiecekbbc02932021-05-21 07:19:41 +02003181 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003182 /* <type> */
3183 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003184
aPiecek082c7dc2021-05-20 08:55:07 +02003185 /* <iffeature> */
3186 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3187 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003188 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003189 assert(tc->cn->nodetype & LYS_CASE);
3190
3191 /* <type> */
3192 ret.type = TRP_EMPTY_TRT_TYPE;
3193
aPiecek7a28e2f2021-05-21 07:27:03 +02003194 /* <iffeature> */
3195 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003196 }
aPiecek3f247652021-04-19 13:40:25 +02003197
3198 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3199
3200 return ret;
3201}
3202
3203/**********************************************************************
3204 * Modify troc getters
3205 *********************************************************************/
3206
3207/**
aPiecek01598c02021-04-23 14:18:24 +02003208 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003209 */
3210static ly_bool
3211troc_modi_parent(struct trt_tree_ctx *tc)
3212{
3213 assert(tc && tc->cn);
3214 /* If no parent exists, stay in actual node. */
3215 if (tc->cn->parent) {
3216 tc->cn = tc->cn->parent;
3217 return 1;
3218 } else {
3219 return 0;
3220 }
3221}
3222
3223/**
aPiecek01598c02021-04-23 14:18:24 +02003224 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003225 */
3226static struct trt_node
3227troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3228{
3229 const struct lysc_node *cn;
3230
3231 assert(tc && tc->cn);
3232
3233 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3234
3235 /* if next sibling exists */
3236 if (cn) {
3237 /* update trt_tree_ctx */
3238 tc->cn = cn;
3239 return troc_read_node(ca, tc);
3240 } else {
3241 return TRP_EMPTY_NODE;
3242 }
3243}
3244
3245/**
3246 * @copydoc trop_modi_next_child()
3247 */
3248static struct trt_node
3249troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3250{
3251 const struct lysc_node *tmp;
3252
3253 assert(tc && tc->cn);
3254
3255 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3256 tc->cn = tmp;
3257 return troc_read_node(ca, tc);
3258 } else {
3259 return TRP_EMPTY_NODE;
3260 }
3261}
3262
3263/**
aPiecek01598c02021-04-23 14:18:24 +02003264 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003265 */
3266static void
aPiecek3f247652021-04-19 13:40:25 +02003267troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003268{
aPiecek3f247652021-04-19 13:40:25 +02003269 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003270
aPiecek3f247652021-04-19 13:40:25 +02003271 if (troc_modi_parent(tc)) {
3272 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3273 } else {
3274 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003275 switch (tc->section) {
3276 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003277 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003278 break;
3279 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003280 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003281 break;
3282 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003283 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003284 break;
3285 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003286 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003287 break;
3288 default:
3289 assert(0);
3290 }
aPiecek61d062b2020-11-02 11:05:09 +01003291 }
3292}
3293
aPiecek874ea4d2021-04-19 12:26:36 +02003294/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003295 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003296 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003297
3298/**
3299 * @brief Get size of node name.
3300 * @param[in] name contains name and mark.
3301 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003302 * @return negative value as an indication that option mark
3303 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003304 */
3305static int32_t
3306trb_strlen_of_name_and_mark(struct trt_node_name name)
3307{
3308 size_t name_len = strlen(name.str);
3309
3310 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3311 /* counting also parentheses */
3312 name_len += 2;
3313 }
3314
3315 return trp_mark_is_used(name) ?
3316 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3317 (int32_t)name_len;
3318}
3319
3320/**
aPiecek874ea4d2021-04-19 12:26:36 +02003321 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3322 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003323 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003324 * @param[in] max_len4all is the maximum value of btw_opts_type
3325 * that it can have.
3326 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003327 */
3328static int16_t
3329trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3330{
3331 int32_t name_len;
3332 int16_t min_len;
3333 int16_t ret;
3334
3335 name_len = trb_strlen_of_name_and_mark(name);
3336
3337 /* negative value indicate that in name is some opt mark */
3338 min_len = name_len < 0 ?
3339 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3340 TRD_INDENT_BEFORE_TYPE;
3341 ret = abs(max_len4all) - abs(name_len);
3342
3343 /* correction -> negative indicate that name is too long. */
3344 return ret < 0 ? min_len : ret;
3345}
3346
3347/**
3348 * @brief Print node.
3349 *
aPiecek01598c02021-04-23 14:18:24 +02003350 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003351 * But difference is that take @p max_gap_before_type which will be
3352 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003353 *
aPiecek9bdd7592021-05-20 08:13:20 +02003354 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003355 * @param[in] max_gap_before_type is number of indent before \<type\>.
3356 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003357 * @param[in] pc contains mainly functions for printing.
3358 * @param[in] tc is tree context.
3359 */
3360static void
aPiecek9bdd7592021-05-20 08:13:20 +02003361trb_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 +01003362{
aPiecek61d062b2020-11-02 11:05:09 +01003363 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3364
3365 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3366 /* print actual node with unified indent */
3367 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3368 }
3369 /* after -> print actual node with default indent */
3370 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3371 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
3372}
3373
3374/**
aPiecek874ea4d2021-04-19 12:26:36 +02003375 * @brief Check if parent of the current node is the last
3376 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003377 *
aPiecek874ea4d2021-04-19 12:26:36 +02003378 * To mantain stability use this function only if the current node is
3379 * the first of the siblings.
3380 * Side-effect -> current node is set to the first sibling
3381 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003382 *
aPiecek01598c02021-04-23 14:18:24 +02003383 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003384 * @param[in,out] tc is tree context.
3385 * @return 1 if parent is last sibling otherwise 0.
3386 */
3387static ly_bool
3388trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3389{
3390 if (fp.modify.parent(tc)) {
3391 ly_bool ret = fp.read.if_sibling_exists(tc);
3392 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3393 return !ret;
3394 } else {
3395 return !fp.read.if_sibling_exists(tc);
3396 }
3397}
3398
3399/**
3400 * @brief Find sibling with the biggest node name and return that size.
3401 *
3402 * Side-effect -> Current node is set to the first sibling.
3403 *
3404 * @param[in] ca contains inherited data from ancestors.
3405 * @param[in] pc contains mainly functions for printing.
3406 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003407 * @return positive number as a sign that only the node name is
3408 * included in the size.
3409 * @return negative number sign that node name and his opt mark is
3410 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003411 */
3412static int32_t
3413trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3414{
3415 int32_t ret = 0;
3416
3417 pc->fp.modify.first_sibling(tc);
3418
3419 for (struct trt_node node = pc->fp.read.node(ca, tc);
3420 !trp_node_is_empty(node);
3421 node = pc->fp.modify.next_sibling(ca, tc)) {
3422 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
3423 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3424 }
3425 pc->fp.modify.first_sibling(tc);
3426 return ret;
3427}
3428
3429/**
aPiecek874ea4d2021-04-19 12:26:36 +02003430 * @brief Find maximal indent between
3431 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003432 *
3433 * Side-effect -> Current node is set to the first sibling.
3434 *
3435 * @param[in] ca contains inherited data from ancestors.
3436 * @param[in] pc contains mainly functions for printing.
3437 * @param[in,out] tc is tree context.
3438 * @return max btw_opts_type value for rest of the siblings
3439 */
3440static int16_t
3441trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3442{
3443 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3444 int16_t ind_before_type = maxlen_node_name < 0 ?
3445 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3446 TRD_INDENT_BEFORE_TYPE;
3447
3448 return abs(maxlen_node_name) + ind_before_type;
3449}
3450
3451/**
aPiecek874ea4d2021-04-19 12:26:36 +02003452 * @brief Find out if it is possible to unify
3453 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003454 *
aPiecek874ea4d2021-04-19 12:26:36 +02003455 * The goal is for all node siblings to have the same alignment
3456 * for \<type\> as if they were in a column. All siblings who cannot
3457 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003458 * Side-effect -> Current node is set to the first sibling.
3459 *
3460 * @param[in] ca contains inherited data from ancestors.
3461 * @param[in] pc contains mainly functions for printing.
3462 * @param[in,out] tc is tree context.
3463 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003464 * @return positive number indicating the maximum number of spaces
3465 * before \<type\> if the length of the node name is 0. To calculate
3466 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003467 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003468*/
3469static uint32_t
3470trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3471{
3472 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3473}
3474
3475/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003476 * @brief Check if there is no case statement
3477 * under the choice statement.
3478 *
3479 * It can return true only if the Parsed schema tree
3480 * is used for browsing.
3481 *
3482 * @param[in] tc is tree context.
3483 * @return 1 if implicit case statement is present otherwise 0.
3484 */
3485static ly_bool
3486trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3487{
3488 return !tc->lysc_tree && tc->pn->parent &&
3489 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3490 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3491 LYS_LEAF | LYS_LEAFLIST));
3492}
3493
3494static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3495 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3496
3497/**
3498 * @brief Print implicit case node and his subtree.
3499 *
3500 * @param[in] node is child of implicit case.
3501 * @param[in] wr is wrapper for printing identation before node.
3502 * @param[in] ca contains inherited data from ancestors.
3503 * @param[in] pc contains mainly functions for printing.
3504 * @param[in] tc is tree context. Its settings should be the same as
3505 * before the function call.
3506 */
3507static void
3508trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3509 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3510{
3511 struct trt_node case_node;
3512 struct trt_wrapper wr_case_child;
3513
3514 case_node = tro_create_implicit_case_node(node);
3515 ly_print_(pc->out, "\n");
3516 trb_print_entire_node(case_node, 0, wr, pc, tc);
3517 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3518 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3519 ly_print_(pc->out, "\n");
3520 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3521}
3522
3523/**
aPiecek874ea4d2021-04-19 12:26:36 +02003524 * @brief For the current node: recursively print all of its child
3525 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003526 *
aPiecek01598c02021-04-23 14:18:24 +02003527 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003528 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003529 * Nodes are printed, including unified sibling node alignment
3530 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003531 * Side-effect -> current node is set to the last sibling.
3532 *
3533 * @param[in] wr is wrapper for printing identation before node.
3534 * @param[in] ca contains inherited data from ancestors.
3535 * @param[in] pc contains mainly functions for printing.
3536 * @param[in,out] tc is tree context.
3537 */
3538static void
3539trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3540{
3541 uint32_t max_gap_before_type;
3542 ly_bool sibling_flag = 0;
3543 ly_bool child_flag = 0;
3544
3545 /* if node is last sibling, then do not add '|' to wrapper */
3546 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3547 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3548
3549 /* try unified indentation in node */
3550 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3551
3552 /* print all siblings */
3553 do {
3554 struct trt_parent_cache new_ca;
3555 struct trt_node node;
aPiecek9bdd7592021-05-20 08:13:20 +02003556 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003557
aPiecekb8d5a0a2021-05-20 08:20:24 +02003558 if (!trb_need_implicit_node_case(tc)) {
3559 /* normal behavior */
3560 ly_print_(pc->out, "\n");
3561 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3562 new_ca = tro_parent_cache_for_child(ca, tc);
3563 /* go to the actual node's child or stay in actual node */
3564 node = pc->fp.modify.next_child(ca, tc);
3565 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003566
aPiecekb8d5a0a2021-05-20 08:20:24 +02003567 if (child_flag) {
3568 /* print all childs - recursive call */
3569 trb_print_nodes(wr, new_ca, pc, tc);
3570 /* get back from child node to actual node */
3571 pc->fp.modify.parent(tc);
3572 }
3573 } else {
3574 /* The case statement is omitted (shorthand).
3575 * Print implicit case node and his subtree.
3576 */
3577 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003578 }
3579
3580 /* go to the actual node's sibling */
3581 node = pc->fp.modify.next_sibling(ca, tc);
3582 sibling_flag = !trp_node_is_empty(node);
3583
3584 /* go to the next sibling or stay in actual node */
3585 } while (sibling_flag);
3586}
3587
3588/**
aPiecek153b00f2021-04-20 13:52:57 +02003589 * @brief Calculate the wrapper about how deep in the tree the node is.
3590 * @param[in] node from which to count.
3591 * @return wrapper for @p node.
3592 */
3593static struct trt_wrapper
3594trb_count_depth(const struct lysc_node *node)
3595{
3596 struct trt_wrapper wr = TRP_INIT_WRAPPER_TOP;
3597 const struct lysc_node *parent;
3598
3599 if (!node) {
3600 return wr;
3601 }
3602
3603 for (parent = node->parent; parent; parent = parent->parent) {
3604 wr = trp_wrapper_set_shift(wr);
3605 }
3606
3607 return wr;
3608}
3609
3610/**
3611 * @brief Print all parent nodes of @p node and the @p node itself.
3612 *
3613 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3614 *
3615 * @param[in] node on which the function is focused.
aPiecek01598c02021-04-23 14:18:24 +02003616 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003617 * @param[in,out] tc is context of tree printer.
3618 * @return wrapper for @p node.
3619 */
3620static void
3621trb_print_parents(const struct lysc_node *node, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3622{
3623 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003624 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003625
3626 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3627
3628 /* stop recursion */
3629 if (!node) {
3630 return;
3631 }
3632 trb_print_parents(node->parent, pc, tc);
3633
3634 /* setup for printing */
3635 tc->cn = node;
3636 wr = trb_count_depth(node);
3637
3638 /* print node */
3639 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003640 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3641 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003642}
3643
3644/**
aPiecekdc8fd572021-04-19 10:47:23 +02003645 * @brief Get address of the current node.
3646 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003647 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003648 */
3649static const void *
3650trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3651{
aPiecek3f247652021-04-19 13:40:25 +02003652 return tc->lysc_tree ?
3653 (const void *)tc->cn :
3654 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003655}
3656
3657/**
3658 * @brief Get address of current node's child.
3659 * @param[in,out] tc contains current node.
3660 */
3661static const void *
3662trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3663{
3664 if (!trb_tree_ctx_get_node(tc)) {
3665 return NULL;
3666 }
3667
aPiecek3f247652021-04-19 13:40:25 +02003668 if (tc->lysc_tree) {
3669 return lysc_node_child(tc->cn);
3670 } else {
3671 return lysp_node_child(tc->pn);
3672 }
aPiecekdc8fd572021-04-19 10:47:23 +02003673}
3674
3675/**
3676 * @brief Set current node on its child.
3677 * @param[in,out] tc contains current node.
3678 */
3679static void
3680trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3681{
aPiecek3f247652021-04-19 13:40:25 +02003682 const void *node = trb_tree_ctx_get_child(tc);
3683
3684 if (tc->lysc_tree) {
3685 tc->cn = node;
3686 } else {
3687 tc->pn = node;
3688 }
aPiecekdc8fd572021-04-19 10:47:23 +02003689}
3690
3691/**
aPiecek61d062b2020-11-02 11:05:09 +01003692 * @brief Print subtree of nodes.
3693 *
3694 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003695 * Before root node is no linebreak printing. This must be addressed by
3696 * the caller. Root node will also be printed. Behind last printed node
3697 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003698 *
aPiecek9bdd7592021-05-20 08:13:20 +02003699 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003700 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003701 * ::trb_try_unified_indent() function for root node.
3702 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003703 * @param[in] wr is wrapper saying how deep in the whole tree
3704 * is the root of the subtree.
3705 * @param[in] ca is parent_cache from root's parent.
3706 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003707 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003708 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003709 */
3710static void
aPiecek9bdd7592021-05-20 08:13:20 +02003711trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3712 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003713{
3714 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003715
aPiecek9bdd7592021-05-20 08:13:20 +02003716 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003717 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003718 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003719 node = pc->fp.modify.next_child(ca, tc);
3720
3721 if (!trp_node_is_empty(node)) {
3722 /* print root's nodes */
3723 trb_print_nodes(wr, new_ca, pc, tc);
3724 /* get back from child node to actual node */
3725 pc->fp.modify.parent(tc);
3726 }
3727}
3728
3729/**
3730 * @brief Get number of siblings.
3731 *
3732 * Side-effect -> current node is set to the first sibling.
3733 *
3734 * @param[in] fp contains callback functions which modify tree context
3735 * @param[in,out] tc is the tree context.
3736 * @return Number of siblings of the current node.
3737 */
3738static uint32_t
3739trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3740{
3741 uint32_t ret = 1;
3742 struct trt_node node = TRP_EMPTY_NODE;
3743
3744 /* including actual node */
3745 fp.first_sibling(tc);
3746 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3747 ret++;
3748 }
3749 fp.first_sibling(tc);
3750 return ret;
3751}
3752
3753/**
3754 * @brief Print all parents and their children.
3755 *
aPiecek874ea4d2021-04-19 12:26:36 +02003756 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003757 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003758 * for all top-level siblings. Use this function after 'module' keyword
3759 * or 'augment' and so. The nodes may not be exactly top-level in the
3760 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003761 *
aPiecek153b00f2021-04-20 13:52:57 +02003762 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3763 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003764 * @param[pc] pc contains mainly functions for printing.
3765 * @param[in,out] tc is tree context.
3766 */
3767static void
aPiecek153b00f2021-04-20 13:52:57 +02003768trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003769{
aPiecek61d062b2020-11-02 11:05:09 +01003770 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003771 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003772 uint32_t total_parents;
3773 uint32_t max_gap_before_type;
3774
aPiecekdc8fd572021-04-19 10:47:23 +02003775 if (!trb_tree_ctx_get_node(tc)) {
3776 return;
3777 }
3778
aPiecek61d062b2020-11-02 11:05:09 +01003779 ca = TRP_EMPTY_PARENT_CACHE;
3780 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3781 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3782
aPiecek3f247652021-04-19 13:40:25 +02003783 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003784 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3785 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003786 ca.lys_config = 0x0;
3787 }
aPiecekdc8fd572021-04-19 10:47:23 +02003788 }
3789
aPiecek61d062b2020-11-02 11:05:09 +01003790 for (uint32_t i = 0; i < total_parents; i++) {
3791 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003792 node = pc->fp.read.node(ca, tc);
3793 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003794 pc->fp.modify.next_sibling(ca, tc);
3795 }
3796}
3797
aPiecek874ea4d2021-04-19 12:26:36 +02003798/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003799 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003800 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003801
3802/**
aPiecekdc8fd572021-04-19 10:47:23 +02003803 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003804 *
aPiecekdc8fd572021-04-19 10:47:23 +02003805 * @param[in] module YANG schema tree structure representing
3806 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003807 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003808 * @param[in] max_line_length is the maximum line length limit
3809 * that should not be exceeded.
3810 * @param[in,out] pc will be adapted to lysp_tree.
3811 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003812 */
3813static void
aPiecekdc8fd572021-04-19 10:47:23 +02003814trm_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 +01003815{
aPiecekdc8fd572021-04-19 10:47:23 +02003816 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003817 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003818 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003819 .pmod = module->parsed,
3820 .cmod = NULL,
3821 .pn = module->parsed ? module->parsed->data : NULL,
3822 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003823 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003824 };
aPiecek61d062b2020-11-02 11:05:09 +01003825
aPiecekdc8fd572021-04-19 10:47:23 +02003826 pc->out = out;
3827
3828 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003829 .parent = trop_modi_parent,
3830 .first_sibling = trop_modi_first_sibling,
3831 .next_sibling = trop_modi_next_sibling,
3832 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01003833 };
3834
aPiecekdc8fd572021-04-19 10:47:23 +02003835 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003836 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003837 .node = trop_read_node,
3838 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003839 };
3840
aPiecekdc8fd572021-04-19 10:47:23 +02003841 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003842 .print_features_names = tro_print_features_names,
3843 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01003844 };
3845
aPiecekdc8fd572021-04-19 10:47:23 +02003846 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01003847}
3848
3849/**
aPiecek3f247652021-04-19 13:40:25 +02003850 * @brief Settings if lysc_node are used for browsing through the tree.
3851 *
3852 * Pointers to current nodes will be set to module data.
3853 *
3854 * @param[in] module YANG schema tree structure representing
3855 * YANG module.
3856 * @param[in] out is output handler.
3857 * @param[in] max_line_length is the maximum line length limit
3858 * that should not be exceeded.
3859 * @param[in,out] pc will be adapted to lysc_tree.
3860 * @param[in,out] tc will be adapted to lysc_tree.
3861 */
3862static void
3863trm_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)
3864{
3865 *tc = (struct trt_tree_ctx) {
3866 .lysc_tree = 1,
3867 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003868 .pmod = module->parsed,
3869 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02003870 .tpn = NULL,
3871 .pn = NULL,
3872 .cn = module->compiled->data
3873 };
3874
3875 pc->out = out;
3876
3877 pc->fp.modify = (struct trt_fp_modify_ctx) {
3878 .parent = troc_modi_parent,
3879 .first_sibling = troc_modi_first_sibling,
3880 .next_sibling = troc_modi_next_sibling,
3881 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02003882 };
3883
3884 pc->fp.read = (struct trt_fp_read) {
3885 .module_name = tro_read_module_name,
3886 .node = troc_read_node,
3887 .if_sibling_exists = troc_read_if_sibling_exists
3888 };
3889
3890 pc->fp.print = (struct trt_fp_print) {
3891 .print_features_names = tro_print_features_names,
3892 .print_keys = tro_print_keys
3893 };
3894
3895 pc->max_line_length = max_line_length;
3896}
3897
3898/**
3899 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02003900 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02003901 * @param[in,out] tc resets to lysc browsing.
3902 */
3903static void
3904trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3905{
aPiecek9f792e52021-04-21 08:33:56 +02003906 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003907}
3908
3909/**
3910 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02003911 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02003912 * @param[in,out] tc resets to lysp browsing.
3913 */
3914static void
3915trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3916{
aPiecek9f792e52021-04-21 08:33:56 +02003917 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003918}
3919
3920/**
3921 * @brief If augment's target node is located on the current module.
3922 * @param[in] pn is examined augment.
3923 * @param[in] pmod is current module.
3924 * @return 1 if nodeid refers to the local node, otherwise 0.
3925 */
3926static ly_bool
3927trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
3928{
3929 const char *id, *prefix, *name;
3930 size_t prefix_len, name_len;
3931 const struct lys_module *mod;
3932 ly_bool ret = 0;
3933
3934 if (pn == NULL) {
3935 return ret;
3936 }
3937
3938 id = pn->nodeid;
3939 if (!id) {
3940 return ret;
3941 }
3942 /* only absolute-schema-nodeid is taken into account */
3943 assert(id[0] == '/');
3944 ++id;
3945
3946 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3947 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003948 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
aPiecek3f247652021-04-19 13:40:25 +02003949 ret = mod->parsed == pmod;
3950 } else {
3951 ret = 1;
3952 }
3953
3954 return ret;
3955}
3956
3957/**
aPiecek96baa7f2021-04-23 12:32:00 +02003958 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01003959 *
aPiecekdc8fd572021-04-19 10:47:23 +02003960 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02003961 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01003962 *
aPiecekdc8fd572021-04-19 10:47:23 +02003963 * @param[in] ks is section representation.
3964 * @param[in] pc contains mainly functions for printing.
3965 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01003966 */
3967static void
aPiecekdc8fd572021-04-19 10:47:23 +02003968trm_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 +01003969{
aPiecekdc8fd572021-04-19 10:47:23 +02003970 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3971 return;
3972 }
3973
3974 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
3975 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02003976 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02003977 } else {
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/**
aPiecek96baa7f2021-04-23 12:32:00 +02003983 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02003984 *
aPiecek96baa7f2021-04-23 12:32:00 +02003985 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02003986 *
3987 * @param[in] ks is section representation.
3988 * @param[in] pc contains mainly functions for printing.
3989 * @param[in,out] tc is the tree context.
3990 */
3991static void
3992trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3993{
3994 ly_bool grp_has_data = 0;
3995
3996 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3997 return;
3998 }
3999
4000 if (ks.type == TRD_KEYWORD_GROUPING) {
4001 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4002 }
4003
4004 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4005 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004006 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004007}
4008
4009/**
4010 * @brief Print 'module' keyword, its name and all nodes.
4011 * @param[in] pc contains mainly functions for printing.
4012 * @param[in,out] tc is the tree context.
4013 */
4014static void
4015trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4016{
4017 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4018}
4019
4020/**
4021 * @brief For all augment sections: print 'augment' keyword,
4022 * its target node and all nodes.
4023 * @param[in] pc contains mainly functions for printing.
4024 * @param[in,out] tc is the tree context.
4025 */
4026static void
4027trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4028{
4029 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004030 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004031
aPiecek3f247652021-04-19 13:40:25 +02004032 if (tc->lysc_tree) {
4033 origin_was_lysc_tree = 1;
4034 trm_reset_to_lysp_tree_ctx(pc, tc);
4035 }
4036
aPiecekdc8fd572021-04-19 10:47:23 +02004037 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004038 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004039 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004040 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004041
aPiecek3f247652021-04-19 13:40:25 +02004042 if (origin_was_lysc_tree) {
4043 /* if lysc tree is used, then only augments targeting
4044 * another module are printed
4045 */
aPiecek9f792e52021-04-21 08:33:56 +02004046 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004047 continue;
4048 }
4049 }
4050
aPiecekdc8fd572021-04-19 10:47:23 +02004051 if (once) {
4052 ly_print_(pc->out, "\n");
4053 ly_print_(pc->out, "\n");
4054 once = 0;
4055 } else {
4056 ly_print_(pc->out, "\n");
4057 }
4058
4059 trm_print_section_as_subtree(ks, pc, tc);
4060 }
aPiecek3f247652021-04-19 13:40:25 +02004061
4062 if (origin_was_lysc_tree) {
4063 trm_reset_to_lysc_tree_ctx(pc, tc);
4064 }
aPiecekdc8fd572021-04-19 10:47:23 +02004065}
4066
4067/**
4068 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4069 * @param[in] pc contains mainly functions for printing.
4070 * @param[in,out] tc is the tree context.
4071 */
4072static void
4073trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4074{
4075 struct trt_keyword_stmt rpc;
4076
aPiecek01598c02021-04-23 14:18:24 +02004077 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004078
4079 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4080 ly_print_(pc->out, "\n");
4081 ly_print_(pc->out, "\n");
4082 trm_print_section_as_family_tree(rpc, pc, tc);
4083 }
4084}
4085
4086/**
4087 * @brief For notifications section: print 'notifications' keyword
4088 * and all its nodes.
4089 * @param[in] pc contains mainly functions for printing.
4090 * @param[in,out] tc is the tree context.
4091 */
4092static void
4093trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4094{
4095 struct trt_keyword_stmt notifs;
4096
aPiecek01598c02021-04-23 14:18:24 +02004097 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004098
4099 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4100 ly_print_(pc->out, "\n");
4101 ly_print_(pc->out, "\n");
4102 trm_print_section_as_family_tree(notifs, pc, tc);
4103 }
4104}
4105
4106/**
4107 * @brief For all grouping sections: print 'grouping' keyword, its name
4108 * and all nodes.
4109 * @param[in] pc contains mainly functions for printing.
4110 * @param[in,out] tc is the tree context.
4111 */
4112static void
4113trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4114{
4115 ly_bool once;
4116
aPiecek01598c02021-04-23 14:18:24 +02004117 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004118 return;
4119 }
4120
4121 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004122 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004123 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004124 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004125 if (once) {
4126 ly_print_(pc->out, "\n");
4127 ly_print_(pc->out, "\n");
4128 once = 0;
4129 } else {
4130 ly_print_(pc->out, "\n");
4131 }
4132 trm_print_section_as_subtree(ks, pc, tc);
4133 }
4134}
4135
4136/**
4137 * @brief For all yang-data sections: print 'yang-data' keyword
4138 * and all its nodes.
4139 * @param[in] pc contains mainly functions for printing.
4140 * @param[in,out] tc is the tree context.
4141 */
4142static void
aPiecek96baa7f2021-04-23 12:32:00 +02004143trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004144{
aPiecek96baa7f2021-04-23 12:32:00 +02004145 ly_bool once;
4146 LY_ARRAY_COUNT_TYPE count;
4147
4148 count = LY_ARRAY_COUNT(tc->pmod->exts);
4149 if (count == 0) {
4150 return;
4151 }
4152
4153 once = 1;
4154 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4155 struct trt_keyword_stmt ks;
4156
aPiecek01598c02021-04-23 14:18:24 +02004157 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004158 * ::lysp_ext_instance.parsed.
4159 */
4160 if (!tc->pmod->exts[u].parsed) {
4161 /* print at least the yang-data names */
4162 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4163 continue;
4164 }
4165
4166 ks = tro_modi_next_yang_data(tc, u);
4167 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4168 break;
4169 }
4170
4171 if (once) {
4172 ly_print_(pc->out, "\n");
4173 ly_print_(pc->out, "\n");
4174 once = 0;
4175 } else {
4176 ly_print_(pc->out, "\n");
4177 }
4178
4179 trm_print_section_as_family_tree(ks, pc, tc);
4180 }
aPiecekdc8fd572021-04-19 10:47:23 +02004181}
4182
4183/**
4184 * @brief Print sections module, augment, rpcs, notifications,
4185 * grouping, yang-data.
4186 * @param[in] pc contains mainly functions for printing.
4187 * @param[in,out] tc is the tree context.
4188 */
4189static void
4190trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4191{
4192 trm_print_module_section(pc, tc);
4193 trm_print_augmentations(pc, tc);
4194 trm_print_rpcs(pc, tc);
4195 trm_print_notifications(pc, tc);
4196 trm_print_groupings(pc, tc);
4197 trm_print_yang_data(pc, tc);
4198 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004199}
4200
aPiecek874ea4d2021-04-19 12:26:36 +02004201/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004202 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004203 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004204
4205LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004206tree_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 +01004207{
4208 struct trt_printer_ctx pc;
4209 struct trt_tree_ctx tc;
4210 struct ly_out *new_out;
4211 LY_ERR erc;
4212 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4213
aPiecekdc8fd572021-04-19 10:47:23 +02004214 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4215
aPiecek61d062b2020-11-02 11:05:09 +01004216 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4217 return erc;
4218 }
4219
4220 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004221 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4222 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4223 } else {
4224 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4225 }
aPiecek61d062b2020-11-02 11:05:09 +01004226
4227 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004228 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004229
4230 ly_out_free(new_out, NULL, 1);
4231
aPiecekdc8fd572021-04-19 10:47:23 +02004232 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004233}
4234
4235LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004236tree_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 +01004237{
aPiecek153b00f2021-04-20 13:52:57 +02004238 struct trt_printer_ctx pc;
4239 struct trt_tree_ctx tc;
4240 struct ly_out *new_out;
4241 struct trt_wrapper wr;
4242 LY_ERR erc;
4243 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4244
4245 assert(out && node);
4246
4247 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4248 return LY_EINVAL;
4249 }
4250
4251 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4252 return erc;
4253 }
4254
4255 line_length = line_length == 0 ? SIZE_MAX : line_length;
4256 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4257
4258 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
4259 trb_print_parents(node, &pc, &tc);
4260
4261 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4262 tc.cn = lysc_node_child(node);
4263 wr = trb_count_depth(tc.cn);
4264 trb_print_family_tree(wr, &pc, &tc);
4265 }
4266 ly_print_(out, "\n");
4267
4268 erc = clb_arg.last_error;
4269 ly_out_free(new_out, NULL, 1);
4270
4271 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004272}
4273
4274LY_ERR
aPiecek9f792e52021-04-21 08:33:56 +02004275tree_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 +01004276{
aPiecek9f792e52021-04-21 08:33:56 +02004277 struct trt_printer_ctx pc;
4278 struct trt_tree_ctx tc;
4279 struct ly_out *new_out;
4280 LY_ERR erc;
4281 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4282
4283 assert(submodp);
4284 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4285
4286 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4287 return erc;
4288 }
4289
4290 line_length = line_length == 0 ? SIZE_MAX : line_length;
4291 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4292 tc.pmod = (struct lysp_module *)submodp;
4293 tc.tpn = submodp->data;
4294 tc.pn = tc.tpn;
4295
4296 trm_print_sections(&pc, &tc);
4297 erc = clb_arg.last_error;
4298
4299 ly_out_free(new_out, NULL, 1);
4300
4301 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004302}