blob: 525e50585a4163cce890e8c5e267f00ab98a8439 [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"
aPiecek704f8e92021-08-25 13:35:05 +020096#include "printer_schema.h"
aPiecek874ea4d2021-04-19 12:26:36 +020097#include "tree_schema_internal.h"
98#include "xpath.h"
99
aPiecek61d062b2020-11-02 11:05:09 +0100100/**
101 * @brief List of available actions.
102 */
103typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200104 TRD_PRINT = 0, /**< Normal behavior. It just prints. */
105 TRD_CHAR_COUNT /**< Characters will be counted instead of printing. */
aPiecek61d062b2020-11-02 11:05:09 +0100106} trt_ly_out_clb_arg_flag;
107
108/**
aPiecek874ea4d2021-04-19 12:26:36 +0200109 * @brief Structure is passed as 'writeclb' argument
aPiecek01598c02021-04-23 14:18:24 +0200110 * to the ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100111 */
112struct ly_out_clb_arg {
aPiecek874ea4d2021-04-19 12:26:36 +0200113 trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */
114 struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */
115 size_t counter; /**< Counter of printed characters. */
116 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 +0100117};
118
119/**
120 * @brief Initialize struct ly_out_clb_arg with default settings.
121 */
122#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
aPiecek874ea4d2021-04-19 12:26:36 +0200123 (struct ly_out_clb_arg) { \
124 .mode = MODE, .out = OUT, \
125 .counter = COUNTER, .last_error = LAST_ERROR \
126 }
aPiecek61d062b2020-11-02 11:05:09 +0100127
aPiecek874ea4d2021-04-19 12:26:36 +0200128/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100129 * Print getters
aPiecek874ea4d2021-04-19 12:26:36 +0200130 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100131
132/**
133 * @brief Callback functions that prints special cases.
134 *
135 * It just groups together tree context with trt_fp_print.
136 */
137struct trt_cf_print {
aPiecek874ea4d2021-04-19 12:26:36 +0200138 const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */
139 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 +0100140};
141
142/**
143 * @brief Callback functions for printing special cases.
144 *
aPiecek874ea4d2021-04-19 12:26:36 +0200145 * Functions with the suffix 'trp' can print most of the text on
146 * output, just by setting the pointer to the string. But in some
147 * cases, it's not that simple, because its entire string is fragmented
148 * in memory. For example, for printing list's keys or if-features.
aPiecek61d062b2020-11-02 11:05:09 +0100149 * However, this depends on how the libyang library is implemented.
aPiecek3f247652021-04-19 13:40:25 +0200150 * This implementation of the printer_tree module goes through
151 * a lysp tree, but if it goes through a lysc tree, these special cases
152 * would be different.
aPiecek61d062b2020-11-02 11:05:09 +0100153 * Functions must print including spaces or delimiters between names.
154 */
155struct trt_fp_print {
156 void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */
157 void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */
158};
159
160/**
161 * @brief Package which only groups getter function.
162 */
163struct trt_pck_print {
164 const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */
165 struct trt_fp_print fps; /**< Print function. */
166};
167
168/**
169 * @brief Initialize struct trt_pck_print by parameters.
170 */
171#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
aPiecek874ea4d2021-04-19 12:26:36 +0200172 (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT}
aPiecek61d062b2020-11-02 11:05:09 +0100173
aPiecek874ea4d2021-04-19 12:26:36 +0200174/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100175 * Indent
aPiecek874ea4d2021-04-19 12:26:36 +0200176 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100177
178/**
aPiecek874ea4d2021-04-19 12:26:36 +0200179 * @brief Constants which are defined in the RFC or are observable
180 * from the pyang tool.
aPiecek61d062b2020-11-02 11:05:09 +0100181 */
182typedef enum {
183 TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100184 TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with
185 a whitespace offset of at least two characters. */
186 TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */
187 TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */
188 TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */
189 TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */
aPiecek61d062b2020-11-02 11:05:09 +0100190 TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */
191} trt_cnf_indent;
192
193/**
194 * @brief Type of indent in node.
195 */
196typedef enum {
197 TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100198 TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */
aPiecek61d062b2020-11-02 11:05:09 +0100199 TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
200} trt_indent_in_node_type;
201
202/** Constant to indicate the need to break a line. */
203#define TRD_LINEBREAK -1
204
205/**
aPiecek874ea4d2021-04-19 12:26:36 +0200206 * @brief Records the alignment between the individual
207 * elements of the node.
aPiecek61d062b2020-11-02 11:05:09 +0100208 *
aPiecek874ea4d2021-04-19 12:26:36 +0200209 * @see trp_default_indent_in_node, trp_try_normal_indent_in_node
aPiecek61d062b2020-11-02 11:05:09 +0100210 */
211struct trt_indent_in_node {
212 trt_indent_in_node_type type; /**< Type of indent in node. */
aPiecek874ea4d2021-04-19 12:26:36 +0200213 int16_t btw_name_opts; /**< Indent between node name and \<opts\>. */
214 int16_t btw_opts_type; /**< Indent between \<opts\> and \<type\>. */
aPiecek61d062b2020-11-02 11:05:09 +0100215 int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */
216};
217
218/**
219 * @brief Type of wrappers to be printed.
220 */
221typedef enum {
222 TRD_WRAPPER_TOP = 0, /**< Related to the module. */
223 TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */
224} trd_wrapper_type;
225
226/**
227 * @brief For resolving sibling symbol ('|') placement.
228 *
229 * Bit indicates where the sibling symbol must be printed.
aPiecek874ea4d2021-04-19 12:26:36 +0200230 * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS.
aPiecek61d062b2020-11-02 11:05:09 +0100231 *
aPiecek874ea4d2021-04-19 12:26:36 +0200232 * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY,
233 * trp_wrapper_set_mark, trp_wrapper_set_shift,
234 * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper
aPiecek61d062b2020-11-02 11:05:09 +0100235 */
236struct trt_wrapper {
237 trd_wrapper_type type; /**< Location of the wrapper. */
238 uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed.
239 It follows that the maximum immersion of the printable node is 64. */
240 uint32_t actual_pos; /**< Actual position in bit_marks. */
241};
242
243/**
244 * @brief Get wrapper related to the module section.
245 *
246 * @code
247 * module: <module-name>
248 * +--<node>
249 * |
250 * @endcode
251 */
252#define TRP_INIT_WRAPPER_TOP \
aPiecek874ea4d2021-04-19 12:26:36 +0200253 (struct trt_wrapper) { \
254 .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \
255 }
aPiecek61d062b2020-11-02 11:05:09 +0100256
257/**
aPiecek874ea4d2021-04-19 12:26:36 +0200258 * @brief Get wrapper related to subsection
259 * e.g. Augmenations or Groupings.
aPiecek61d062b2020-11-02 11:05:09 +0100260 *
261 * @code
262 * module: <module-name>
263 * +--<node>
264 *
265 * augment <target-node>:
266 * +--<node>
267 * @endcode
268 */
269#define TRP_INIT_WRAPPER_BODY \
aPiecek874ea4d2021-04-19 12:26:36 +0200270 (struct trt_wrapper) { \
271 .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \
272 }
aPiecek61d062b2020-11-02 11:05:09 +0100273
274/**
275 * @brief Package which only groups wrapper and indent in node.
276 */
277struct trt_pck_indent {
278 struct trt_wrapper wrapper; /**< Coded " | | " sequence. */
279 struct trt_indent_in_node in_node; /**< Indent in node. */
280};
281
282/**
283 * @brief Initialize struct trt_pck_indent by parameters.
284 */
285#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200286 (struct trt_pck_indent){ \
287 .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \
288 }
aPiecek61d062b2020-11-02 11:05:09 +0100289
aPiecek874ea4d2021-04-19 12:26:36 +0200290/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100291 * status
aPiecek874ea4d2021-04-19 12:26:36 +0200292 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100293
294/**
295 * @brief Status of the node.
296 *
aPiecek874ea4d2021-04-19 12:26:36 +0200297 * @see trp_print_status
aPiecek61d062b2020-11-02 11:05:09 +0100298 */
299typedef enum {
300 TRD_STATUS_TYPE_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100301 TRD_STATUS_TYPE_CURRENT, /**< ::LYS_STATUS_CURR */
302 TRD_STATUS_TYPE_DEPRECATED, /**< ::LYS_STATUS_DEPRC */
aPiecek874ea4d2021-04-19 12:26:36 +0200303 TRD_STATUS_TYPE_OBSOLETE /**< ::LYS_STATUS_OBSLT */
aPiecek61d062b2020-11-02 11:05:09 +0100304} trt_status_type;
305
aPiecek874ea4d2021-04-19 12:26:36 +0200306/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100307 * flags
aPiecek874ea4d2021-04-19 12:26:36 +0200308 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100309
310/**
311 * @brief Flag of the node.
312 *
aPiecek874ea4d2021-04-19 12:26:36 +0200313 * @see trp_print_flags, trp_get_flags_strlen
aPiecek61d062b2020-11-02 11:05:09 +0100314 */
315typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200316 TRD_FLAGS_TYPE_EMPTY = 0, /**< -- */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100317 TRD_FLAGS_TYPE_RW, /**< rw */
318 TRD_FLAGS_TYPE_RO, /**< ro */
319 TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */
320 TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */
321 TRD_FLAGS_TYPE_RPC, /**< -x */
322 TRD_FLAGS_TYPE_NOTIF, /**< -n */
aPiecek61d062b2020-11-02 11:05:09 +0100323 TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */
324} trt_flags_type;
325
aPiecek874ea4d2021-04-19 12:26:36 +0200326/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100327 * node_name and opts
aPiecek874ea4d2021-04-19 12:26:36 +0200328 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100329
330#define TRD_NODE_NAME_PREFIX_CHOICE "("
331#define TRD_NODE_NAME_PREFIX_CASE ":("
332#define TRD_NODE_NAME_TRIPLE_DOT "..."
333
334/**
335 * @brief Type of the node.
336 *
aPiecek874ea4d2021-04-19 12:26:36 +0200337 * Used mainly to complete the correct \<opts\> next to or
338 * around the \<name\>.
aPiecek61d062b2020-11-02 11:05:09 +0100339 */
340typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200341 TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100342 TRD_NODE_CASE, /**< For case node. :(\<name\>) */
343 TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */
344 TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */
345 TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */
346 TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */
347 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list (without keys). \<name\>* */
348 TRD_NODE_KEYS, /**< For a list's keys. \<name\>* [\<keys\>] */
349 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
350 TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */
aPiecek874ea4d2021-04-19 12:26:36 +0200351 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 +0100352} trt_node_type;
353
354/**
355 * @brief Type of node and his name.
356 *
aPiecek874ea4d2021-04-19 12:26:36 +0200357 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
aPiecek61d062b2020-11-02 11:05:09 +0100358 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
359 */
360struct trt_node_name {
361 trt_node_type type; /**< Type of the node relevant for printing. */
aPiecek34fa3772021-05-21 12:35:46 +0200362 const char *module_prefix; /**< If the node is augmented into the tree from another module,
363 so this is the prefix of that module. */
aPiecek61d062b2020-11-02 11:05:09 +0100364 const char *str; /**< Name of the node. */
365};
366
367/**
368 * @brief Create struct trt_node_name as empty.
369 */
370#define TRP_EMPTY_NODE_NAME \
aPiecek874ea4d2021-04-19 12:26:36 +0200371 (struct trt_node_name) { \
372 .type = TRD_NODE_ELSE, .module_prefix = NULL, .str = NULL \
373 }
aPiecek61d062b2020-11-02 11:05:09 +0100374
375/**
376 * @brief Check if struct trt_node_name is empty.
377 */
378#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
379 !NODE_NAME.str
380
aPiecek874ea4d2021-04-19 12:26:36 +0200381/**
382 * @brief Every \<opts\> mark except string of list's keys
383 * has a length of one.
384 */
aPiecek61d062b2020-11-02 11:05:09 +0100385#define TRD_OPTS_MARK_LENGTH 1
386
aPiecek874ea4d2021-04-19 12:26:36 +0200387/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100388 * type
aPiecek874ea4d2021-04-19 12:26:36 +0200389 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100390
391/**
392 * @brief Type of the \<type\>
393 */
394typedef enum {
395 TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100396 TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
397 TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm.
aPiecek874ea4d2021-04-19 12:26:36 +0200398 So set type as ::TRD_TYPE_TARGET. */
aPiecek61d062b2020-11-02 11:05:09 +0100399 TRD_TYPE_EMPTY /**< Type is not used at all. */
400} trt_type_type;
401
402/**
403 * @brief \<type\> in the \<node\>.
404 *
aPiecek874ea4d2021-04-19 12:26:36 +0200405 * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
aPiecek61d062b2020-11-02 11:05:09 +0100406 */
407struct trt_type {
408 trt_type_type type; /**< Type of the \<type\>. */
409 const char *str; /**< Path or name of the type. */
410};
411
412/**
413 * @brief Create empty struct trt_type.
414 */
415#define TRP_EMPTY_TRT_TYPE \
416 (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}
417
418/**
419 * @brief Check if struct trt_type is empty.
420 */
421#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
422 TYPE_OF_TYPE.type == TRD_TYPE_EMPTY
423
424/**
425 * @brief Initialize struct trt_type by parameters.
426 */
427#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
428 (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}
429
aPiecek874ea4d2021-04-19 12:26:36 +0200430/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100431 * node
aPiecek874ea4d2021-04-19 12:26:36 +0200432 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100433
434/**
435 * @brief \<node\> data for printing.
436 *
aPiecek874ea4d2021-04-19 12:26:36 +0200437 * It contains RFC's:
438 * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
aPiecek61d062b2020-11-02 11:05:09 +0100439 * Item \<opts\> is moved to part struct trt_node_name.
aPiecek874ea4d2021-04-19 12:26:36 +0200440 * For printing [\<keys\>] and if-features is required special
441 * functions which prints them.
aPiecek61d062b2020-11-02 11:05:09 +0100442 *
aPiecek874ea4d2021-04-19 12:26:36 +0200443 * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty,
444 * trp_print_node_up_to_name, trp_print_divided_node_up_to_name,
445 * trp_print_node
aPiecek61d062b2020-11-02 11:05:09 +0100446 */
447struct trt_node {
aPiecek874ea4d2021-04-19 12:26:36 +0200448 trt_status_type status; /**< \<status\>. */
449 trt_flags_type flags; /**< \<flags\>. */
450 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
451 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
452 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and
453 will be printed by trt_fp_print.print_features_names callback. */
454 ly_bool last_one; /**< Information about whether the node is the last. */
aPiecek61d062b2020-11-02 11:05:09 +0100455};
456
457/**
458 * @brief Create struct trt_node as empty.
459 */
460#define TRP_EMPTY_NODE \
aPiecek874ea4d2021-04-19 12:26:36 +0200461 (struct trt_node) { \
462 .status = TRD_STATUS_TYPE_EMPTY, \
463 .flags = TRD_FLAGS_TYPE_EMPTY, \
464 .name = TRP_EMPTY_NODE_NAME, \
465 .type = TRP_EMPTY_TRT_TYPE, \
466 .iffeatures = 0, \
467 .last_one = 1 \
468 }
aPiecek61d062b2020-11-02 11:05:09 +0100469
470/**
471 * @brief Package which only groups indent and node.
472 */
473struct trt_pair_indent_node {
474 struct trt_indent_in_node indent;
475 struct trt_node node;
476};
477
478/**
479 * @brief Initialize struct trt_pair_indent_node by parameters.
480 */
481#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
aPiecek874ea4d2021-04-19 12:26:36 +0200482 (struct trt_pair_indent_node) { \
483 .indent = INDENT_IN_NODE, .node = NODE \
484 }
aPiecek61d062b2020-11-02 11:05:09 +0100485
aPiecek874ea4d2021-04-19 12:26:36 +0200486/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100487 * statement
aPiecek874ea4d2021-04-19 12:26:36 +0200488 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100489
490#define TRD_TOP_KEYWORD_MODULE "module"
491#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
492
493#define TRD_BODY_KEYWORD_AUGMENT "augment"
494#define TRD_BODY_KEYWORD_RPC "rpcs"
495#define TRD_BODY_KEYWORD_NOTIF "notifications"
496#define TRD_BODY_KEYWORD_GROUPING "grouping"
497#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
498
499/**
500 * @brief Type of the trt_keyword.
501 */
502typedef enum {
503 TRD_KEYWORD_EMPTY = 0,
Michal Vasko6a914fb2022-01-17 10:36:14 +0100504 TRD_KEYWORD_MODULE,
505 TRD_KEYWORD_SUBMODULE,
506 TRD_KEYWORD_AUGMENT,
507 TRD_KEYWORD_RPC,
508 TRD_KEYWORD_NOTIF,
509 TRD_KEYWORD_GROUPING,
aPiecek61d062b2020-11-02 11:05:09 +0100510 TRD_KEYWORD_YANG_DATA
511} trt_keyword_type;
512
513/**
514 * @brief Main sign of the tree nodes.
515 *
aPiecek874ea4d2021-04-19 12:26:36 +0200516 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
aPiecek61d062b2020-11-02 11:05:09 +0100517 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
518 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
519 * trp_keyword_type_strlen
520 *
521 */
522struct trt_keyword_stmt {
aPiecek874ea4d2021-04-19 12:26:36 +0200523 trt_keyword_type type; /**< String containing some of the top or body keyword. */
524 const char *str; /**< Name or path, it determines the type. */
aPiecek61d062b2020-11-02 11:05:09 +0100525};
526
527/**
528 * @brief Create struct trt_keyword_stmt as empty.
529 */
530#define TRP_EMPTY_KEYWORD_STMT \
531 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
532
533/**
534 * @brief Check if struct trt_keyword_stmt is empty.
535 */
536#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
537 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
538
539/**
540 * @brief Initialize struct trt_keyword_stmt by parameters.
541 */
542#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
543 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
544
aPiecek874ea4d2021-04-19 12:26:36 +0200545/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100546 * Modify getters
aPiecek874ea4d2021-04-19 12:26:36 +0200547 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100548
549struct trt_parent_cache;
550
551/**
552 * @brief Functions that change the state of the tree_ctx structure.
553 *
aPiecek3f247652021-04-19 13:40:25 +0200554 * The 'trop' or 'troc' functions are set here, which provide data
aPiecek874ea4d2021-04-19 12:26:36 +0200555 * for the 'trp' printing functions and are also called from the
556 * 'trb' browsing functions when walking through a tree. These callback
557 * functions need to be checked or reformulated if changes to the
558 * libyang library affect the printing tree. For all, if the value
559 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
560 * macro is returned.
aPiecek61d062b2020-11-02 11:05:09 +0100561 */
562struct trt_fp_modify_ctx {
563 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
564 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
565 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
566 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 +0100567};
568
aPiecek874ea4d2021-04-19 12:26:36 +0200569/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100570 * Read getters
aPiecek874ea4d2021-04-19 12:26:36 +0200571 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100572
573/**
574 * @brief Functions that do not change the state of the tree_structure.
575 *
576 * For details see trt_fp_modify_ctx.
577 */
578struct trt_fp_read {
aPiecek874ea4d2021-04-19 12:26:36 +0200579 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
580 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
581 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
aPiecek61d062b2020-11-02 11:05:09 +0100582};
583
aPiecek874ea4d2021-04-19 12:26:36 +0200584/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100585 * All getters
aPiecek874ea4d2021-04-19 12:26:36 +0200586 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100587
588/**
aPiecek874ea4d2021-04-19 12:26:36 +0200589 * @brief A set of all necessary functions that must be provided
590 * for the printer.
aPiecek61d062b2020-11-02 11:05:09 +0100591 */
592struct trt_fp_all {
593 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
594 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
595 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
596};
597
aPiecek874ea4d2021-04-19 12:26:36 +0200598/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100599 * Printer context
aPiecek874ea4d2021-04-19 12:26:36 +0200600 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100601
602/**
aPiecek01598c02021-04-23 14:18:24 +0200603 * @brief Main structure for @ref TRP_trp part.
aPiecek61d062b2020-11-02 11:05:09 +0100604 */
605struct trt_printer_ctx {
606 struct ly_out *out; /**< Handler to printing. */
aPiecek01598c02021-04-23 14:18:24 +0200607 struct trt_fp_all fp; /**< @ref TRP_tro functions callbacks. */
aPiecek61d062b2020-11-02 11:05:09 +0100608 size_t max_line_length; /**< The maximum number of characters that can be
609 printed on one line, including the last. */
610};
611
aPiecek874ea4d2021-04-19 12:26:36 +0200612/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100613 * Tro functions
aPiecek874ea4d2021-04-19 12:26:36 +0200614 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100615
616/**
617 * @brief The name of the section to which the node belongs.
618 */
619typedef enum {
620 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100621 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
622 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
623 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
624 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
aPiecek61d062b2020-11-02 11:05:09 +0100625 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
626} trt_actual_section;
627
628/**
629 * @brief Types of nodes that have some effect on their children.
630 */
631typedef enum {
aPiecek874ea4d2021-04-19 12:26:36 +0200632 TRD_ANCESTOR_ELSE = 0, /**< Everything not listed. */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100633 TRD_ANCESTOR_RPC_INPUT, /**< ::LYS_INPUT */
634 TRD_ANCESTOR_RPC_OUTPUT, /**< ::LYS_OUTPUT */
aPiecek874ea4d2021-04-19 12:26:36 +0200635 TRD_ANCESTOR_NOTIF /**< ::LYS_NOTIF */
aPiecek61d062b2020-11-02 11:05:09 +0100636} trt_ancestor_type;
637
638/**
639 * @brief Saved information when browsing the tree downwards.
640 *
aPiecek874ea4d2021-04-19 12:26:36 +0200641 * This structure helps prevent frequent retrieval of information
aPiecek01598c02021-04-23 14:18:24 +0200642 * from the tree. Functions @ref TRP_trb are designed to preserve
aPiecek874ea4d2021-04-19 12:26:36 +0200643 * this structures during their recursive calls. This functions do not
644 * interfere in any way with this data. This structure
aPiecek01598c02021-04-23 14:18:24 +0200645 * is used by @ref TRP_trop functions which, thanks to this
aPiecek874ea4d2021-04-19 12:26:36 +0200646 * structure, can return a node with the correct data. The word
647 * \b parent is in the structure name, because this data refers to
648 * the last parent and at the same time the states of its
649 * ancestors data. Only the function jumping on the child
650 * (next_child(...)) creates this structure, because the pointer
651 * to the current node moves down the tree. It's like passing
652 * the genetic code to children. Some data must be inherited and
653 * there are two approaches to this problem. Either it will always
654 * be determined which inheritance states belong to the current node
655 * (which can lead to regular travel to the root node) or
656 * the inheritance states will be stored during the recursive calls.
657 * So the problem was solved by the second option. Why does
658 * the structure contain this data? Because it walks through
aPiecek3f247652021-04-19 13:40:25 +0200659 * the lysp tree. For walks through the lysc tree is trt_parent_cache
660 * useless.
aPiecek61d062b2020-11-02 11:05:09 +0100661 *
aPiecek874ea4d2021-04-19 12:26:36 +0200662 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
aPiecek61d062b2020-11-02 11:05:09 +0100663 */
664struct trt_parent_cache {
aPiecek874ea4d2021-04-19 12:26:36 +0200665 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
666 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
667 uint16_t lys_config; /**< Inherited config W or R. */
668 const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
aPiecek61d062b2020-11-02 11:05:09 +0100669};
670
671/**
672 * @brief Return trt_parent_cache filled with default values.
673 */
674#define TRP_EMPTY_PARENT_CACHE \
aPiecek874ea4d2021-04-19 12:26:36 +0200675 (struct trt_parent_cache) { \
676 .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
677 .lys_config = LYS_CONFIG_W, .last_list = NULL \
678 }
aPiecek61d062b2020-11-02 11:05:09 +0100679
680/**
681 * @brief Main structure for browsing the libyang tree
682 */
683struct trt_tree_ctx {
aPiecek96baa7f2021-04-23 12:32:00 +0200684 ly_bool lysc_tree; /**< The lysc nodes are used for browsing through the tree.
685 It is assumed that once set, it does not change.
686 If it is true then trt_tree_ctx.pn and
687 trt_tree_ctx.tpn are not used.
688 If it is false then trt_tree_ctx.cn is not used. */
689 trt_actual_section section; /**< To which section pn points. */
690 const struct lysp_module *pmod; /**< Parsed YANG schema tree. */
691 const struct lysc_module *cmod; /**< Compiled YANG schema tree. */
692 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
693 union {
694 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
695 const struct lysp_ext_instance *tpn_ext; /**< Actual top-node is extension. Item trt_tree_ctx.section
696 is set to TRD_SECT_YANG_DATA. */
697 };
698 const struct lysc_node *cn; /**< Actual pointer to compiled node. */
aPiecek61d062b2020-11-02 11:05:09 +0100699};
700
aPiecek3f247652021-04-19 13:40:25 +0200701/**
aPiecekbbc02932021-05-21 07:19:41 +0200702 * @brief Check if lysp node is available from
703 * the current compiled node.
704 *
705 * Use only if trt_tree_ctx.lysc_tree is set to true.
706 */
707#define TRP_TREE_CTX_LYSP_NODE_PRESENT(CN) \
708 (CN->priv)
709
710/**
aPiecek3f247652021-04-19 13:40:25 +0200711 * @brief Get lysp_node from trt_tree_ctx.cn.
aPiecekbbc02932021-05-21 07:19:41 +0200712 *
713 * Use only if :TRP_TREE_CTX_LYSP_NODE_PRESENT returns true
714 * for that node.
aPiecek3f247652021-04-19 13:40:25 +0200715 */
716#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
717 ((const struct lysp_node *)CN->priv)
718
aPiecek01598c02021-04-23 14:18:24 +0200719/** Getter function for ::trop_node_charptr(). */
aPiecek61d062b2020-11-02 11:05:09 +0100720typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
721
aPiecekef1e58e2021-04-19 13:19:44 +0200722/**
723 * @brief Simple getter functions for lysp and lysc nodes.
724 *
725 * This structure is useful if we have a general algorithm
726 * (tro function) that can be used for both lysc and lysp nodes.
727 * Thanks to this structure, we prevent code redundancy.
728 * We don't have to write basically the same algorithm twice
729 * for lysp and lysc trees.
730 */
Michal Vasko6a914fb2022-01-17 10:36:14 +0100731struct tro_getters {
aPiecekef1e58e2021-04-19 13:19:44 +0200732 uint16_t (*nodetype)(const void *); /**< Get nodetype. */
733 const void *(*next)(const void *); /**< Get sibling. */
734 const void *(*parent)(const void *); /**< Get parent. */
735 const void *(*child)(const void *); /**< Get child. */
736 const void *(*actions)(const void *); /**< Get actions. */
737 const void *(*action_input)(const void *); /**< Get input action from action node. */
738 const void *(*action_output)(const void *); /**< Get output action from action node. */
739 const void *(*notifs)(const void *); /**< Get notifs. */
740};
741
aPiecek874ea4d2021-04-19 12:26:36 +0200742/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100743 * Definition of the general Trg functions
aPiecek874ea4d2021-04-19 12:26:36 +0200744 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100745
746/**
747 * @brief Print a substring but limited to the maximum length.
748 * @param[in] str is pointer to source.
749 * @param[in] len is number of characters to be printed.
750 * @param[in,out] out is output handler.
751 * @return str parameter shifted by len.
752 */
753static const char *
754trg_print_substr(const char *str, size_t len, struct ly_out *out)
755{
756 for (size_t i = 0; i < len; i++) {
757 ly_print_(out, "%c", str[0]);
758 str++;
759 }
760 return str;
761}
762
763/**
764 * @brief Pointer is not NULL and does not point to an empty string.
765 * @param[in] str is pointer to string to be checked.
766 * @return 1 if str pointing to non empty string otherwise 0.
767 */
768static ly_bool
769trg_charptr_has_data(const char *str)
770{
771 return (str) && (str[0] != '\0');
772}
773
774/**
aPiecek874ea4d2021-04-19 12:26:36 +0200775 * @brief Check if @p word in @p src is present where words are
776 * delimited by @p delim.
777 * @param[in] src is source where words are separated by @p delim.
aPiecek61d062b2020-11-02 11:05:09 +0100778 * @param[in] word to be searched.
aPiecek874ea4d2021-04-19 12:26:36 +0200779 * @param[in] delim is delimiter between @p words in @p src.
780 * @return 1 if src contains @p word otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100781 */
782static ly_bool
783trg_word_is_present(const char *src, const char *word, char delim)
784{
785 const char *hit;
786
787 if ((!src) || (src[0] == '\0') || (!word)) {
788 return 0;
789 }
790
791 hit = strstr(src, word);
792
793 if (hit) {
794 /* word was founded at the begin of src
795 * OR it match somewhere after delim
796 */
797 if ((hit == src) || (hit[-1] == delim)) {
798 /* end of word was founded at the end of src
799 * OR end of word was match somewhere before delim
800 */
801 char delim_or_end = (hit + strlen(word))[0];
802 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
803 return 1;
804 }
805 }
806 /* after -> hit is just substr and it's not the whole word */
807 /* jump to the next word */
808 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
809 /* skip delim */
810 src = src[0] == '\0' ? src : src + 1;
811 /* continue with searching */
812 return trg_word_is_present(src, word, delim);
813 } else {
814 return 0;
815 }
816}
817
aPiecek874ea4d2021-04-19 12:26:36 +0200818/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +0100819 * Definition of printer functions
aPiecek874ea4d2021-04-19 12:26:36 +0200820 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +0100821
822/**
aPiecek01598c02021-04-23 14:18:24 +0200823 * @brief Write callback for ::ly_out_new_clb().
aPiecek61d062b2020-11-02 11:05:09 +0100824 *
aPiecek874ea4d2021-04-19 12:26:36 +0200825 * @param[in] user_data is type of struct ly_out_clb_arg.
aPiecek61d062b2020-11-02 11:05:09 +0100826 * @param[in] buf contains input characters
827 * @param[in] count is number of characters in buf.
828 * @return Number of printed bytes.
829 * @return Negative value in case of error.
830 */
831static ssize_t
832trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
833{
834 LY_ERR erc = LY_SUCCESS;
835 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
836
837 switch (data->mode) {
838 case TRD_PRINT:
839 erc = ly_write_(data->out, buf, count);
840 break;
841 case TRD_CHAR_COUNT:
842 data->counter = data->counter + count;
843 break;
844 default:
845 break;
846 }
847
848 if (erc != LY_SUCCESS) {
849 data->last_error = erc;
850 return -1;
851 } else {
852 return count;
853 }
854}
855
856/**
857 * @brief Check that indent in node can be considered as equivalent.
858 * @param[in] first is the first indent in node.
859 * @param[in] second is the second indent in node.
860 * @return 1 if indents are equivalent otherwise 0.
861 */
862static ly_bool
863trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
864{
865 const ly_bool a = first.type == second.type;
866 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
867 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
868 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
869
870 return a && b && c && d;
871}
872
873/**
aPiecek874ea4d2021-04-19 12:26:36 +0200874 * @brief Setting space character because node is last sibling.
875 * @param[in] wr is wrapper over which the shift operation
876 * is to be performed.
aPiecek61d062b2020-11-02 11:05:09 +0100877 * @return New shifted wrapper.
878 */
879static struct trt_wrapper
880trp_wrapper_set_shift(struct trt_wrapper wr)
881{
882 assert(wr.actual_pos < 64);
883 /* +--<node>
884 * +--<node>
885 */
886 wr.actual_pos++;
887 return wr;
888}
889
890/**
aPiecek874ea4d2021-04-19 12:26:36 +0200891 * @brief Setting '|' symbol because node is divided or
892 * it is not last sibling.
aPiecek61d062b2020-11-02 11:05:09 +0100893 * @param[in] wr is source of wrapper.
894 * @return New wrapper which is marked at actual position and shifted.
895 */
896static struct trt_wrapper
897trp_wrapper_set_mark(struct trt_wrapper wr)
898{
899 assert(wr.actual_pos < 64);
900 wr.bit_marks1 |= 1U << wr.actual_pos;
901 return trp_wrapper_set_shift(wr);
902}
903
904/**
905 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
906 * @param[in] wr is actual wrapper.
aPiecek874ea4d2021-04-19 12:26:36 +0200907 * @param[in] last_one is flag. Value 1 saying if the node is the last
908 * and has no more siblings.
aPiecek61d062b2020-11-02 11:05:09 +0100909 * @return New wrapper for the actual node.
910 */
911static struct trt_wrapper
912trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
913{
914 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
915}
916
917/**
918 * @brief Test if the wrappers are equivalent.
919 * @param[in] first is the first wrapper.
920 * @param[in] second is the second wrapper.
921 * @return 1 if the wrappers are equivalent otherwise 0.
922 */
923static ly_bool
924trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
925{
926 const ly_bool a = first.type == second.type;
927 const ly_bool b = first.bit_marks1 == second.bit_marks1;
928 const ly_bool c = first.actual_pos == second.actual_pos;
929
930 return a && b && c;
931}
932
933/**
934 * @brief Print " | " sequence on line.
935 * @param[in] wr is wrapper to be printed.
936 * @param[in,out] out is output handler.
937 */
938static void
939trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
940{
941 uint32_t lb;
942
943 if (wr.type == TRD_WRAPPER_TOP) {
944 lb = TRD_INDENT_LINE_BEGIN;
945 } else if (wr.type == TRD_WRAPPER_BODY) {
946 lb = TRD_INDENT_LINE_BEGIN * 2;
947 } else {
948 lb = TRD_INDENT_LINE_BEGIN;
949 }
950
951 ly_print_(out, "%*c", lb, ' ');
952
953 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
954 return;
955 }
956
957 for (uint32_t i = 0; i < wr.actual_pos; i++) {
958 /** Test if the bit on the index is set. */
959 if ((wr.bit_marks1 >> i) & 1U) {
960 ly_print_(out, "|");
961 } else {
962 ly_print_(out, " ");
963 }
964
965 if (i != wr.actual_pos) {
966 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
967 }
968 }
969}
970
971/**
972 * @brief Check if struct trt_node is empty.
973 * @param[in] node is item to test.
974 * @return 1 if node is considered empty otherwise 0.
975 */
976static ly_bool
977trp_node_is_empty(struct trt_node node)
978{
979 const ly_bool a = !node.iffeatures;
980 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
981 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
982 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
983 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
984
985 return a && b && c && d && e;
986}
987
988/**
aPiecek874ea4d2021-04-19 12:26:36 +0200989 * @brief Check if [\<keys\>], \<type\> and
990 * \<iffeatures\> are empty/not_set.
aPiecek61d062b2020-11-02 11:05:09 +0100991 * @param[in] node is item to test.
aPiecek874ea4d2021-04-19 12:26:36 +0200992 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
993 * otherwise 0.
aPiecek61d062b2020-11-02 11:05:09 +0100994 */
995static ly_bool
996trp_node_body_is_empty(struct trt_node node)
997{
998 const ly_bool a = !node.iffeatures;
999 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
1000 const ly_bool c = node.name.type != TRD_NODE_KEYS;
1001
1002 return a && b && c;
1003}
1004
1005/**
1006 * @brief Print \<status\> of the node.
1007 * @param[in] status_type is type of status.
1008 * @param[in,out] out is output handler.
1009 */
1010static void
1011trp_print_status(trt_status_type status_type, struct ly_out *out)
1012{
1013 switch (status_type) {
1014 case TRD_STATUS_TYPE_CURRENT:
1015 ly_print_(out, "%c", '+');
1016 break;
1017 case TRD_STATUS_TYPE_DEPRECATED:
1018 ly_print_(out, "%c", 'x');
1019 break;
1020 case TRD_STATUS_TYPE_OBSOLETE:
1021 ly_print_(out, "%c", 'o');
1022 break;
1023 default:
1024 break;
1025 }
1026}
1027
1028/**
1029 * @brief Print \<flags\>.
1030 * @param[in] flags_type is type of \<flags\>.
1031 * @param[in,out] out is output handler.
1032 */
1033static void
1034trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
1035{
1036 switch (flags_type) {
1037 case TRD_FLAGS_TYPE_RW:
1038 ly_print_(out, "%s", "rw");
1039 break;
1040 case TRD_FLAGS_TYPE_RO:
1041 ly_print_(out, "%s", "ro");
1042 break;
1043 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
1044 ly_print_(out, "%s", "-w");
1045 break;
1046 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
1047 ly_print_(out, "%s", "-u");
1048 break;
1049 case TRD_FLAGS_TYPE_RPC:
1050 ly_print_(out, "%s", "-x");
1051 break;
1052 case TRD_FLAGS_TYPE_NOTIF:
1053 ly_print_(out, "%s", "-n");
1054 break;
1055 case TRD_FLAGS_TYPE_MOUNT_POINT:
1056 ly_print_(out, "%s", "mp");
1057 break;
1058 default:
aPiecekdc8fd572021-04-19 10:47:23 +02001059 ly_print_(out, "%s", "--");
aPiecek61d062b2020-11-02 11:05:09 +01001060 break;
1061 }
1062}
1063
1064/**
1065 * @brief Get size of the \<flags\>.
1066 * @param[in] flags_type is type of \<flags\>.
1067 * @return 0 if flags_type is not set otherwise 2.
1068 */
1069static size_t
1070trp_get_flags_strlen(trt_flags_type flags_type)
1071{
1072 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
1073}
1074
1075/**
1076 * @brief Print entire struct trt_node_name structure.
1077 * @param[in] node_name is item to print.
1078 * @param[in,out] out is output handler.
1079 */
1080static void
1081trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
1082{
1083 const char *mod_prefix;
1084 const char *colon;
1085 const char trd_node_name_suffix_choice[] = ")";
1086 const char trd_node_name_suffix_case[] = ")";
1087 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
1088 const char trd_opts_container[] = "!"; /**< For a presence container. */
1089 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
1090 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
1091 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
1092
1093 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1094 return;
1095 }
1096
1097 if (node_name.module_prefix) {
1098 mod_prefix = node_name.module_prefix;
1099 colon = ":";
1100 } else {
1101 mod_prefix = "";
1102 colon = "";
1103 }
1104
1105 switch (node_name.type) {
1106 case TRD_NODE_ELSE:
1107 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
1108 break;
1109 case TRD_NODE_CASE:
1110 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
1111 break;
1112 case TRD_NODE_CHOICE:
1113 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1114 break;
1115 case TRD_NODE_OPTIONAL_CHOICE:
1116 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);
1117 break;
1118 case TRD_NODE_OPTIONAL:
1119 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1120 break;
1121 case TRD_NODE_CONTAINER:
1122 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1123 break;
1124 case TRD_NODE_LISTLEAFLIST:
1125 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1126 break;
1127 case TRD_NODE_KEYS:
1128 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1129 break;
1130 case TRD_NODE_TOP_LEVEL1:
1131 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1132 break;
1133 case TRD_NODE_TOP_LEVEL2:
1134 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1135 break;
1136 case TRD_NODE_TRIPLE_DOT:
1137 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1138 break;
1139 default:
1140 break;
1141 }
1142}
1143
1144/**
aPiecek874ea4d2021-04-19 12:26:36 +02001145 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
1146 * struct trt_node_name.
aPiecek61d062b2020-11-02 11:05:09 +01001147 * @param[in] node_name is structure containing the 'mark'.
1148 * @return 1 if contain otherwise 0.
1149 */
1150static ly_bool
1151trp_mark_is_used(struct trt_node_name node_name)
1152{
1153 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1154 return 0;
1155 }
1156
1157 switch (node_name.type) {
1158 case TRD_NODE_ELSE:
1159 case TRD_NODE_CASE:
1160 case TRD_NODE_KEYS:
1161 return 0;
1162 default:
1163 return 1;
1164 }
1165}
1166
1167/**
1168 * @brief Print opts keys.
1169 * @param[in] node_name contains type of the node with his name.
1170 * @param[in] btw_name_opts is number of spaces between name and [keys].
aPiecek874ea4d2021-04-19 12:26:36 +02001171 * @param[in] cf is basically a pointer to the function that prints
1172 * the keys.
aPiecek61d062b2020-11-02 11:05:09 +01001173 * @param[in,out] out is output handler.
1174 */
1175static void
1176trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1177{
1178 if (node_name.type != TRD_NODE_KEYS) {
1179 return;
1180 }
1181
1182 /* <name><mark>___<keys>*/
1183 if (btw_name_opts > 0) {
1184 ly_print_(out, "%*c", btw_name_opts, ' ');
1185 }
1186 ly_print_(out, "[");
1187 cf.pf(cf.ctx, out);
1188 ly_print_(out, "]");
1189}
1190
1191/**
1192 * @brief Print entire struct trt_type structure.
1193 * @param[in] type is item to print.
1194 * @param[in,out] out is output handler.
1195 */
1196static void
1197trp_print_type(struct trt_type type, struct ly_out *out)
1198{
1199 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1200 return;
1201 }
1202
1203 switch (type.type) {
1204 case TRD_TYPE_NAME:
1205 ly_print_(out, "%s", type.str);
1206 break;
1207 case TRD_TYPE_TARGET:
1208 ly_print_(out, "-> %s", type.str);
1209 break;
1210 case TRD_TYPE_LEAFREF:
1211 ly_print_(out, "leafref");
1212 default:
1213 break;
1214 }
1215}
1216
1217/**
1218 * @brief Print all iffeatures of node
1219 *
1220 * @param[in] iffeature_flag contains if if-features is present.
aPiecek874ea4d2021-04-19 12:26:36 +02001221 * @param[in] cf is basically a pointer to the function that prints
1222 * the list of features.
aPiecek61d062b2020-11-02 11:05:09 +01001223 * @param[in,out] out is output handler.
1224 */
1225static void
1226trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1227{
1228 if (iffeature_flag) {
1229 ly_print_(out, "{");
1230 cf.pf(cf.ctx, out);
1231 ly_print_(out, "}?");
1232 }
1233}
1234
1235/**
1236 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1237 * @param[in] node contains items to print.
1238 * @param[in] out is output handler.
1239 */
1240static void
1241trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1242{
1243 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1244 trp_print_node_name(node.name, out);
1245 return;
1246 }
1247 /* <status>--<flags> */
1248 trp_print_status(node.status, out);
1249 ly_print_(out, "--");
aPiecek874ea4d2021-04-19 12:26:36 +02001250 /* If the node is a case node, there is no space before the <name>
1251 * also case node has no flags.
1252 */
aPiecek61d062b2020-11-02 11:05:09 +01001253 if (node.name.type != TRD_NODE_CASE) {
1254 trp_print_flags(node.flags, out);
1255 ly_print_(out, " ");
1256 }
1257 /* <name> */
1258 trp_print_node_name(node.name, out);
1259}
1260
1261/**
aPiecek874ea4d2021-04-19 12:26:36 +02001262 * @brief Print alignment (spaces) instead of
1263 * \<status\>--\<flags\> \<name\> for divided node.
aPiecek61d062b2020-11-02 11:05:09 +01001264 * @param[in] node contains items to print.
1265 * @param[in] out is output handler.
1266 */
1267static void
1268trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1269{
1270 uint32_t space = trp_get_flags_strlen(node.flags);
1271
1272 if (node.name.type == TRD_NODE_CASE) {
1273 /* :(<name> */
1274 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1275 } else if (node.name.type == TRD_NODE_CHOICE) {
1276 /* (<name> */
1277 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1278 } else {
1279 /* _<name> */
1280 space += strlen(" ");
1281 }
1282
1283 /* <name>
1284 * __
1285 */
1286 space += TRD_INDENT_LONG_LINE_BREAK;
1287
1288 ly_print_(out, "%*c", space, ' ');
1289}
1290
1291/**
1292 * @brief Print struct trt_node structure.
1293 * @param[in] node is item to print.
aPiecek874ea4d2021-04-19 12:26:36 +02001294 * @param[in] pck package of functions for
1295 * printing [\<keys\>] and \<iffeatures\>.
aPiecek61d062b2020-11-02 11:05:09 +01001296 * @param[in] indent is the indent in node.
1297 * @param[in,out] out is output handler.
1298 */
1299static void
1300trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1301{
1302 ly_bool triple_dot;
1303 ly_bool divided;
1304 struct trt_cf_print cf_print_keys;
1305 struct trt_cf_print cf_print_iffeatures;
1306
1307 if (trp_node_is_empty(node)) {
1308 return;
1309 }
1310
1311 /* <status>--<flags> <name><opts> <type> <if-features> */
1312 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1313 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1314
1315 if (triple_dot) {
1316 trp_print_node_name(node.name, out);
1317 return;
1318 } else if (!divided) {
1319 trp_print_node_up_to_name(node, out);
1320 } else {
1321 trp_print_divided_node_up_to_name(node, out);
1322 }
1323
1324 /* <opts> */
1325 /* <name>___<opts>*/
1326 cf_print_keys.ctx = pck.tree_ctx;
1327 cf_print_keys.pf = pck.fps.print_keys;
1328
1329 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1330
1331 /* <opts>__<type> */
1332 if (indent.btw_opts_type > 0) {
1333 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1334 }
1335
1336 /* <type> */
1337 trp_print_type(node.type, out);
1338
1339 /* <type>__<iffeatures> */
1340 if (indent.btw_type_iffeatures > 0) {
1341 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1342 }
1343
1344 /* <iffeatures> */
1345 cf_print_iffeatures.ctx = pck.tree_ctx;
1346 cf_print_iffeatures.pf = pck.fps.print_features_names;
1347
1348 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1349}
1350
1351/**
aPiecek874ea4d2021-04-19 12:26:36 +02001352 * @brief Print keyword based on trt_keyword_stmt.type.
aPiecek61d062b2020-11-02 11:05:09 +01001353 * @param[in] ks is keyword statement to print.
1354 * @param[in,out] out is output handler
1355 */
1356static void
1357trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1358{
1359 switch (ks.type) {
1360 case TRD_KEYWORD_MODULE:
1361 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1362 return;
1363 case TRD_KEYWORD_SUBMODULE:
1364 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1365 return;
1366 default:
1367 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1368 switch (ks.type) {
1369 case TRD_KEYWORD_AUGMENT:
1370 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1371 break;
1372 case TRD_KEYWORD_RPC:
1373 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1374 break;
1375 case TRD_KEYWORD_NOTIF:
1376 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1377 break;
1378 case TRD_KEYWORD_GROUPING:
1379 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1380 break;
1381 case TRD_KEYWORD_YANG_DATA:
1382 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1383 break;
1384 default:
1385 break;
1386 }
1387 break;
1388 }
1389}
1390
1391/**
1392 * @brief Get string length of stored keyword.
1393 * @param[in] type is type of the keyword statement.
1394 * @return length of the keyword statement name.
1395 */
1396static size_t
1397trp_keyword_type_strlen(trt_keyword_type type)
1398{
1399 switch (type) {
1400 case TRD_KEYWORD_MODULE:
1401 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1402 case TRD_KEYWORD_SUBMODULE:
1403 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1404 case TRD_KEYWORD_AUGMENT:
1405 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1406 case TRD_KEYWORD_RPC:
1407 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1408 case TRD_KEYWORD_NOTIF:
1409 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1410 case TRD_KEYWORD_GROUPING:
1411 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1412 case TRD_KEYWORD_YANG_DATA:
1413 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1414 default:
1415 return 0;
1416 }
1417}
1418
1419/**
aPiecek874ea4d2021-04-19 12:26:36 +02001420 * @brief Print trt_keyword_stmt.str which is string of name or path.
aPiecek61d062b2020-11-02 11:05:09 +01001421 * @param[in] ks is keyword statement structure.
1422 * @param[in] mll is max line length.
1423 * @param[in,out] out is output handler.
1424 */
1425static void
1426trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1427{
1428 uint32_t ind_initial;
1429 uint32_t ind_divided;
1430 /* flag if path must be splitted to more lines */
1431 ly_bool linebreak_was_set;
1432 /* flag if at least one subpath was printed */
1433 ly_bool subpath_printed;
1434 /* the sum of the sizes of the substrings on the current line */
1435 uint32_t how_far;
1436 /* pointer to start of the subpath */
1437 const char *sub_ptr;
1438 /* size of subpath from sub_ptr */
1439 size_t sub_len;
1440
1441 if ((!ks.str) || (ks.str[0] == '\0')) {
1442 return;
1443 }
1444
1445 /* module name cannot be splitted */
1446 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1447 ly_print_(out, "%s", ks.str);
1448 return;
1449 }
1450
1451 /* after -> for trd_keyword_stmt_body do */
1452
1453 /* set begin indentation */
1454 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1455 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1456 linebreak_was_set = 0;
1457 subpath_printed = 0;
1458 how_far = 0;
1459 sub_ptr = ks.str;
1460 sub_len = 0;
1461
1462 while (sub_ptr[0] != '\0') {
1463 uint32_t ind;
1464 /* skip slash */
1465 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
1466 /* get position of the end of substr */
1467 tmp = strchr(tmp, '/');
1468 /* set correct size if this is a last substring */
1469 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1470 /* actualize sum of the substring's sizes on the current line */
1471 how_far += sub_len;
1472 /* correction due to colon character if it this is last substring */
1473 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1474 /* choose indentation which depends on
1475 * whether the string is printed on multiple lines or not
1476 */
1477 ind = linebreak_was_set ? ind_divided : ind_initial;
1478 if (ind + how_far <= mll) {
1479 /* printing before max line length */
1480 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1481 subpath_printed = 1;
1482 } else {
1483 /* printing on new line */
1484 if (subpath_printed == 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001485 /* first subpath is too long
1486 * but print it at first line anyway
1487 */
aPiecek61d062b2020-11-02 11:05:09 +01001488 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1489 subpath_printed = 1;
1490 continue;
1491 }
1492 ly_print_(out, "\n");
1493 ly_print_(out, "%*c", ind_divided, ' ');
1494 linebreak_was_set = 1;
1495 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1496 how_far = sub_len;
1497 subpath_printed = 1;
1498 }
1499 }
1500}
1501
1502/**
aPiecek874ea4d2021-04-19 12:26:36 +02001503 * @brief Print separator based on trt_keyword_stmt.type
aPiecek61d062b2020-11-02 11:05:09 +01001504 * @param[in] ks is keyword statement structure.
aPiecekdc8fd572021-04-19 10:47:23 +02001505 * @param[in] grp_has_data is flag only for grouping section.
1506 * Set to 1 if grouping section has some nodes.
1507 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001508 * @param[in,out] out is output handler.
1509 */
1510static void
aPiecekdc8fd572021-04-19 10:47:23 +02001511trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out)
aPiecek61d062b2020-11-02 11:05:09 +01001512{
1513 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
aPiecekdc8fd572021-04-19 10:47:23 +02001514 if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) {
1515 return;
1516 } else {
1517 ly_print_(out, ":");
1518 }
aPiecek61d062b2020-11-02 11:05:09 +01001519 }
1520}
1521
1522/**
1523 * @brief Print entire struct trt_keyword_stmt structure.
1524 * @param[in] ks is item to print.
1525 * @param[in] mll is max line length.
aPiecekdc8fd572021-04-19 10:47:23 +02001526 * @param[in] grp_has_data is flag only for grouping section.
1527 * Set to 1 if grouping section has some nodes.
1528 * Set to 0 if it doesn't have nodes or it's not grouping section.
aPiecek61d062b2020-11-02 11:05:09 +01001529 * @param[in,out] out is output handler.
1530 */
1531static void
aPiecek874ea4d2021-04-19 12:26:36 +02001532trp_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 +01001533{
1534 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1535 return;
1536 }
1537 trt_print_keyword_stmt_begin(ks, out);
1538 trt_print_keyword_stmt_str(ks, mll, out);
aPiecekdc8fd572021-04-19 10:47:23 +02001539 trt_print_keyword_stmt_end(ks, grp_has_data, out);
aPiecek61d062b2020-11-02 11:05:09 +01001540}
1541
aPiecek874ea4d2021-04-19 12:26:36 +02001542/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01001543 * Main trp functions
aPiecek874ea4d2021-04-19 12:26:36 +02001544 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01001545
1546/**
aPiecek874ea4d2021-04-19 12:26:36 +02001547 * @brief Printing one line including wrapper and node
1548 * which can be incomplete (divided).
aPiecek61d062b2020-11-02 11:05:09 +01001549 * @param[in] node is \<node\> representation.
1550 * @param[in] pck contains special printing functions callback.
1551 * @param[in] indent contains wrapper and indent in node numbers.
1552 * @param[in,out] out is output handler.
1553 */
1554static void
1555trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1556{
1557 trp_print_wrapper(indent.wrapper, out);
1558 trp_print_node(node, pck, indent.in_node, out);
1559}
1560
1561/**
aPiecek874ea4d2021-04-19 12:26:36 +02001562 * @brief Printing one line including wrapper and
1563 * \<status\>--\<flags\> \<name\>\<option_mark\>.
aPiecek61d062b2020-11-02 11:05:09 +01001564 * @param[in] node is \<node\> representation.
1565 * @param[in] wr is wrapper for printing indentation before node.
1566 * @param[in] out is output handler.
1567 */
1568static void
1569trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1570{
1571 trp_print_wrapper(wr, out);
1572 trp_print_node_up_to_name(node, out);
1573}
1574
1575/**
aPiecek874ea4d2021-04-19 12:26:36 +02001576 * @brief Check if leafref target must be change to string 'leafref'
1577 * because his target string is too long.
aPiecek61d062b2020-11-02 11:05:09 +01001578 * @param[in] node containing leafref target.
1579 * @param[in] wr is wrapper for printing indentation before node.
1580 * @param[in] mll is max line length.
1581 * @param[in] out is output handler.
1582 * @return true if leafref must be changed to string 'leafref'.
1583 */
1584static ly_bool
1585trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1586{
1587 struct ly_out_clb_arg *data;
1588
1589 if (node.type.type != TRD_TYPE_TARGET) {
1590 return 0;
1591 }
1592
1593 /* set ly_out to counting characters */
1594 data = out->method.clb.arg;
1595
1596 data->counter = 0;
1597 data->mode = TRD_CHAR_COUNT;
1598 /* count number of printed bytes */
1599 trp_print_wrapper(wr, out);
1600 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1601 trp_print_divided_node_up_to_name(node, out);
1602 data->mode = TRD_PRINT;
1603
1604 return data->counter + strlen(node.type.str) > mll;
1605}
1606
1607/**
1608 * @brief Get default indent in node based on node values.
1609 * @param[in] node is \<node\> representation.
aPiecek874ea4d2021-04-19 12:26:36 +02001610 * @return Default indent in node assuming that the node
1611 * will not be divided.
aPiecek61d062b2020-11-02 11:05:09 +01001612 */
1613static struct trt_indent_in_node
1614trp_default_indent_in_node(struct trt_node node)
1615{
1616 struct trt_indent_in_node ret;
1617
1618 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1619
1620 /* btw_name_opts */
1621 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1622
1623 /* btw_opts_type */
1624 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1625 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1626 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1627 TRD_INDENT_BEFORE_TYPE;
1628 } else {
1629 ret.btw_opts_type = 0;
1630 }
1631
1632 /* btw_type_iffeatures */
1633 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1634
1635 return ret;
1636}
1637
1638/**
1639 * @brief Setting linebreaks in trt_indent_in_node.
1640 *
1641 * The order where the linebreak tag can be placed is from the end.
1642 *
aPiecek874ea4d2021-04-19 12:26:36 +02001643 * @param[in] indent containing alignment lengths
1644 * or already linebreak marks.
aPiecek61d062b2020-11-02 11:05:09 +01001645 * @return indent with a newly placed linebreak tag.
aPiecek874ea4d2021-04-19 12:26:36 +02001646 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
1647 * to place a more linebreaks.
aPiecek61d062b2020-11-02 11:05:09 +01001648 */
1649static struct trt_indent_in_node
1650trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1651{
1652 /* somewhere must be set a line break in node */
1653 struct trt_indent_in_node ret = indent;
1654
1655 /* gradually break the node from the end */
1656 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1657 ret.btw_type_iffeatures = TRD_LINEBREAK;
1658 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1659 ret.btw_opts_type = TRD_LINEBREAK;
1660 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1661 /* set line break between name and opts */
1662 ret.btw_name_opts = TRD_LINEBREAK;
1663 } else {
1664 /* it is not possible to place a more line breaks,
1665 * unfortunately the max_line_length constraint is violated
1666 */
1667 ret.type = TRD_INDENT_IN_NODE_FAILED;
1668 }
1669 return ret;
1670}
1671
1672/**
1673 * @brief Get the first half of the node based on the linebreak mark.
1674 *
1675 * Items in the second half of the node will be empty.
1676 *
1677 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001678 * @param[in] indent contains information in which part of the \<node\>
1679 * the first half ends.
aPiecek61d062b2020-11-02 11:05:09 +01001680 * @return first half of the node, indent is unchanged.
1681 */
1682static struct trt_pair_indent_node
1683trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1684{
1685 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1686
1687 if (indent.btw_name_opts == TRD_LINEBREAK) {
1688 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1689 ret.node.type = TRP_EMPTY_TRT_TYPE;
1690 ret.node.iffeatures = 0;
1691 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1692 ret.node.type = TRP_EMPTY_TRT_TYPE;
1693 ret.node.iffeatures = 0;
1694 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1695 ret.node.iffeatures = 0;
1696 }
1697
1698 return ret;
1699}
1700
1701/**
1702 * @brief Get the second half of the node based on the linebreak mark.
1703 *
1704 * Items in the first half of the node will be empty.
1705 * Indentations belonging to the first node will be reset to zero.
1706 *
1707 * @param[in] node the whole \<node\> to be split.
aPiecek874ea4d2021-04-19 12:26:36 +02001708 * @param[in] indent contains information in which part of the \<node\>
1709 * the second half starts.
aPiecek61d062b2020-11-02 11:05:09 +01001710 * @return second half of the node, indent is newly set.
1711 */
1712static struct trt_pair_indent_node
1713trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1714{
1715 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1716
1717 if (indent.btw_name_opts < 0) {
aPiecek874ea4d2021-04-19 12:26:36 +02001718 /* Logically, the information up to token <opts> should
1719 * be deleted, but the the trp_print_node function needs it to
1720 * create the correct indent.
aPiecek61d062b2020-11-02 11:05:09 +01001721 */
1722 ret.indent.btw_name_opts = 0;
1723 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1724 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1725 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1726 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1727 ret.indent.btw_name_opts = 0;
1728 ret.indent.btw_opts_type = 0;
1729 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1730 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1731 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1732 ret.node.type = TRP_EMPTY_TRT_TYPE;
1733 ret.indent.btw_name_opts = 0;
1734 ret.indent.btw_opts_type = 0;
1735 ret.indent.btw_type_iffeatures = 0;
1736 }
1737 return ret;
1738}
1739
1740/**
1741 * @brief Get the correct alignment for the node.
1742 *
aPiecek874ea4d2021-04-19 12:26:36 +02001743 * This function is recursively called itself. It's like a backend
aPiecek01598c02021-04-23 14:18:24 +02001744 * function for a function ::trp_try_normal_indent_in_node().
aPiecek61d062b2020-11-02 11:05:09 +01001745 *
1746 * @param[in] node is \<node\> representation.
1747 * @param[in] pck contains speciall callback functions for printing.
1748 * @param[in] indent contains wrapper and indent in node numbers.
1749 * @param[in] mll is max line length.
1750 * @param[in,out] cnt counting number of characters to print.
1751 * @param[in,out] out is output handler.
1752 * @return pair of node and indentation numbers of that node.
1753 */
1754static struct trt_pair_indent_node
1755trp_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)
1756{
1757 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1758
1759 trp_print_line(node, pck, indent, out);
1760
1761 if (*cnt <= mll) {
1762 /* success */
1763 return ret;
1764 } else {
1765 ret.indent = trp_indent_in_node_place_break(ret.indent);
1766 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1767 /* erase information in node due to line break */
1768 ret = trp_first_half_node(node, ret.indent);
1769 /* check if line fits, recursive call */
1770 *cnt = 0;
1771 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1772 /* make sure that the result will be with the status divided
1773 * or eventually with status failed */
1774 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1775 }
1776 return ret;
1777 }
1778}
1779
1780/**
1781 * @brief Get the correct alignment for the node.
1782 *
1783 * @param[in] node is \<node\> representation.
1784 * @param[in] pck contains speciall callback functions for printing.
1785 * @param[in] indent contains wrapper and indent in node numbers.
1786 * @param[in] mll is max line length.
1787 * @param[in,out] out is output handler.
aPiecek874ea4d2021-04-19 12:26:36 +02001788 * @return ::TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the
1789 * line, some indent variable has negative value as a line break sign.
1790 * @return ::TRD_INDENT_IN_NODE_NORMAL - the node fits into the line,
1791 * all indent variables values has non-negative number.
1792 * @return ::TRD_INDENT_IN_NODE_FAILED - the node does not fit into the
1793 * line, all indent variables has negative or zero values,
1794 * function failed.
aPiecek61d062b2020-11-02 11:05:09 +01001795 */
1796static struct trt_pair_indent_node
1797trp_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)
1798{
1799 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1800 struct ly_out_clb_arg *data;
1801
1802 /* set ly_out to counting characters */
1803 data = out->method.clb.arg;
1804
1805 data->counter = 0;
1806 data->mode = TRD_CHAR_COUNT;
1807 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1808 data->mode = TRD_PRINT;
1809
1810 return ret;
1811}
1812
1813/**
aPiecek01598c02021-04-23 14:18:24 +02001814 * @brief Auxiliary function for ::trp_print_entire_node()
aPiecek874ea4d2021-04-19 12:26:36 +02001815 * that prints split nodes.
aPiecek61d062b2020-11-02 11:05:09 +01001816 * @param[in] node is node representation.
1817 * @param[in] ppck contains speciall callback functions for printing.
1818 * @param[in] ipck contains wrapper and indent in node numbers.
1819 * @param[in] mll is max line length.
1820 * @param[in,out] out is output handler.
1821 */
1822static void
1823trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1824{
1825 ly_bool entire_node_was_printed;
1826 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1827
1828 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1829 /* nothing can be done, continue as usual */
1830 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1831 }
1832
1833 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1834 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1835
1836 if (!entire_node_was_printed) {
1837 ly_print_(out, "\n");
1838 /* continue with second half node */
1839 ind_node = trp_second_half_node(node, ind_node.indent);
1840 /* continue with printing node */
1841 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1842 } else {
1843 return;
1844 }
1845}
1846
1847/**
aPiecek874ea4d2021-04-19 12:26:36 +02001848 * @brief Printing of the wrapper and the whole node,
1849 * which can be divided into several lines.
aPiecek61d062b2020-11-02 11:05:09 +01001850 * @param[in] node is node representation.
1851 * @param[in] ppck contains speciall callback functions for printing.
1852 * @param[in] ipck contains wrapper and indent in node numbers.
1853 * @param[in] mll is max line length.
1854 * @param[in,out] out is output handler.
1855 */
1856static void
1857trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1858{
1859 struct trt_pair_indent_node ind_node1;
1860 struct trt_pair_indent_node ind_node2;
1861 struct trt_pck_indent tmp;
1862
1863 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1864 node.type.type = TRD_TYPE_LEAFREF;
1865 }
1866
1867 /* check if normal indent is possible */
1868 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1869
1870 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1871 /* node fits to one line */
1872 trp_print_line(node, ppck, ipck, out);
1873 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1874 /* node will be divided */
1875 /* print first half */
1876 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1877 /* pretend that this is normal node */
1878 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1879
1880 trp_print_line(ind_node1.node, ppck, tmp, out);
1881 ly_print_(out, "\n");
1882
1883 /* continue with second half on new line */
1884 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1885 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1886
1887 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1888 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1889 /* node name is too long */
1890 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1891
1892 if (trp_node_body_is_empty(node)) {
1893 return;
1894 } else {
1895 ly_print_(out, "\n");
1896
1897 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1898 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1899 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1900
1901 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1902 }
1903
1904 }
1905}
1906
aPiecek874ea4d2021-04-19 12:26:36 +02001907/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02001908 * trop and troc getters
aPiecekef1e58e2021-04-19 13:19:44 +02001909 *********************************************************************/
1910
1911/**
1912 * @brief Get nodetype.
1913 * @param[in] node is any lysp_node.
1914 */
1915static uint16_t
1916trop_nodetype(const void *node)
1917{
1918 return ((const struct lysp_node *)node)->nodetype;
1919}
1920
1921/**
1922 * @brief Get sibling.
1923 * @param[in] node is any lysp_node.
1924 */
1925static const void *
1926trop_next(const void *node)
1927{
1928 return ((const struct lysp_node *)node)->next;
1929}
1930
1931/**
1932 * @brief Get parent.
1933 * @param[in] node is any lysp_node.
1934 */
1935static const void *
1936trop_parent(const void *node)
1937{
1938 return ((const struct lysp_node *)node)->parent;
1939}
1940
1941/**
1942 * @brief Try to get child.
1943 * @param[in] node is any lysp_node.
1944 */
1945static const void *
1946trop_child(const void *node)
1947{
1948 return lysp_node_child(node);
1949}
1950
1951/**
1952 * @brief Try to get action.
1953 * @param[in] node is any lysp_node.
1954 */
1955static const void *
1956trop_actions(const void *node)
1957{
1958 return lysp_node_actions(node);
1959}
1960
1961/**
1962 * @brief Try to get action.
1963 * @param[in] node must be of type lysp_node_action.
1964 */
1965static const void *
1966trop_action_input(const void *node)
1967{
1968 return &((const struct lysp_node_action *)node)->input;
1969}
1970
1971/**
1972 * @brief Try to get action.
1973 * @param[in] node must be of type lysp_node_action.
1974 */
1975static const void *
1976trop_action_output(const void *node)
1977{
1978 return &((const struct lysp_node_action *)node)->output;
1979}
1980
1981/**
1982 * @brief Try to get action.
1983 * @param[in] node is any lysp_node.
1984 */
1985static const void *
1986trop_notifs(const void *node)
1987{
1988 return lysp_node_notifs(node);
1989}
1990
1991/**
aPiecek01598c02021-04-23 14:18:24 +02001992 * @brief Fill struct tro_getters with @ref TRP_trop getters
aPiecekef1e58e2021-04-19 13:19:44 +02001993 * which are adapted to lysp nodes.
1994 */
1995static struct tro_getters
1996trop_init_getters()
1997{
1998 return (struct tro_getters) {
1999 .nodetype = trop_nodetype,
2000 .next = trop_next,
2001 .parent = trop_parent,
2002 .child = trop_child,
2003 .actions = trop_actions,
2004 .action_input = trop_action_input,
2005 .action_output = trop_action_output,
2006 .notifs = trop_notifs
2007 };
2008}
2009
aPiecek3f247652021-04-19 13:40:25 +02002010/**
2011 * @brief Get nodetype.
2012 * @param[in] node is any lysc_node.
2013 */
2014static uint16_t
2015troc_nodetype(const void *node)
2016{
2017 return ((const struct lysc_node *)node)->nodetype;
2018}
2019
2020/**
2021 * @brief Get sibling.
2022 * @param[in] node is any lysc_node.
2023 */
2024static const void *
2025troc_next(const void *node)
2026{
2027 return ((const struct lysc_node *)node)->next;
2028}
2029
2030/**
2031 * @brief Get parent.
2032 * @param[in] node is any lysc_node.
2033 */
2034static const void *
2035troc_parent(const void *node)
2036{
2037 return ((const struct lysc_node *)node)->parent;
2038}
2039
2040/**
2041 * @brief Try to get child.
2042 * @param[in] node is any lysc_node.
2043 */
2044static const void *
2045troc_child(const void *node)
2046{
2047 return lysc_node_child(node);
2048}
2049
2050/**
2051 * @brief Try to get action.
2052 * @param[in] node is any lysc_node.
2053 */
2054static const void *
2055troc_actions(const void *node)
2056{
2057 return lysc_node_actions(node);
2058}
2059
2060/**
2061 * @brief Try to get action.
2062 * @param[in] node must be of type lysc_node_action.
2063 */
2064static const void *
2065troc_action_input(const void *node)
2066{
2067 return &((const struct lysc_node_action *)node)->input;
2068}
2069
2070/**
2071 * @brief Try to get action.
2072 * @param[in] node must be of type lysc_node_action.
2073 */
2074static const void *
2075troc_action_output(const void *node)
2076{
2077 return &((const struct lysc_node_action *)node)->output;
2078}
2079
2080/**
2081 * @brief Try to get action.
2082 * @param[in] node is any lysc_node.
2083 */
2084static const void *
2085troc_notifs(const void *node)
2086{
2087 return lysc_node_notifs(node);
2088}
2089
2090/**
aPiecek01598c02021-04-23 14:18:24 +02002091 * @brief Fill struct tro_getters with @ref TRP_troc getters
aPiecek3f247652021-04-19 13:40:25 +02002092 * which are adapted to lysc nodes.
2093 */
2094static struct tro_getters
2095troc_init_getters()
2096{
2097 return (struct tro_getters) {
2098 .nodetype = troc_nodetype,
2099 .next = troc_next,
2100 .parent = troc_parent,
2101 .child = troc_child,
2102 .actions = troc_actions,
2103 .action_input = troc_action_input,
2104 .action_output = troc_action_output,
2105 .notifs = troc_notifs
2106 };
2107}
2108
aPiecekef1e58e2021-04-19 13:19:44 +02002109/**********************************************************************
2110 * tro functions
2111 *********************************************************************/
2112
2113/**
2114 * @brief Get next sibling of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002115 *
2116 * This is a general algorithm that is able to
2117 * work with lysp_node or lysc_node.
2118 *
2119 * @param[in] node points to lysp_node or lysc_node.
2120 * @param[in] lysc_tree flag to determine what type the @p node is.
2121 * If set to true, then @p points to lysc_node otherwise lysp_node.
2122 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002123 */
2124static const void *
aPiecek3f247652021-04-19 13:40:25 +02002125tro_next_sibling(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002126{
2127 struct tro_getters get;
2128 const void *tmp, *parent;
2129 const void *ret;
2130
2131 assert(node);
2132
aPiecek3f247652021-04-19 13:40:25 +02002133 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002134
2135 if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
2136 if ((tmp = get.next(node))) {
2137 /* next action exists */
2138 ret = tmp;
2139 } else if ((parent = get.parent(node))) {
2140 /* maybe if notif exists as sibling */
2141 ret = get.notifs(parent);
2142 } else {
2143 ret = NULL;
2144 }
2145 } else if (get.nodetype(node) & LYS_INPUT) {
2146 if ((parent = get.parent(node))) {
2147 /* if output action has data */
2148 if (get.child(get.action_output(parent))) {
2149 /* then next sibling is output action */
2150 ret = get.action_output(parent);
2151 } else {
2152 /* input action cannot have siblings other
2153 * than output action.
2154 */
2155 ret = NULL;
2156 }
2157 } else {
2158 /* there is no way how to get output action */
2159 ret = NULL;
2160 }
2161 } else if (get.nodetype(node) & LYS_OUTPUT) {
2162 /* output action cannot have siblings */
2163 ret = NULL;
2164 } else if (get.nodetype(node) & LYS_NOTIF) {
2165 /* must have as a sibling only notif */
2166 ret = get.next(node);
2167 } else {
2168 /* for rest of nodes */
2169 if ((tmp = get.next(node))) {
2170 /* some sibling exists */
2171 ret = tmp;
2172 } else if ((parent = get.parent(node))) {
2173 /* Action and notif are siblings too.
2174 * They can be reached through parent.
2175 */
2176 if ((tmp = get.actions(parent))) {
2177 /* next sibling is action */
2178 ret = tmp;
2179 } else if ((tmp = get.notifs(parent))) {
2180 /* next sibling is notif */
2181 ret = tmp;
2182 } else {
2183 /* sibling not exists */
2184 ret = NULL;
2185 }
2186 } else {
2187 /* sibling not exists */
2188 ret = NULL;
2189 }
2190 }
2191
2192 return ret;
2193}
2194
2195/**
2196 * @brief Get child of the current node.
aPiecek3f247652021-04-19 13:40:25 +02002197 *
2198 * This is a general algorithm that is able to
2199 * work with lysp_node or lysc_node.
2200 *
2201 * @param[in] node points to lysp_node or lysc_node.
2202 * @param[in] lysc_tree flag to determine what type the @p node is.
2203 * If set to true, then @p points to lysc_node otherwise lysp_node.
2204 * This flag should be the same as trt_tree_ctx.lysc_tree.
aPiecekef1e58e2021-04-19 13:19:44 +02002205 */
2206static const void *
aPiecek3f247652021-04-19 13:40:25 +02002207tro_next_child(const void *node, ly_bool lysc_tree)
aPiecekef1e58e2021-04-19 13:19:44 +02002208{
2209 struct tro_getters get;
2210 const void *tmp;
2211 const void *ret;
2212
2213 assert(node);
2214
aPiecek3f247652021-04-19 13:40:25 +02002215 get = lysc_tree ? troc_init_getters() : trop_init_getters();
aPiecekef1e58e2021-04-19 13:19:44 +02002216
2217 if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
2218 if (get.child(get.action_input(node))) {
2219 /* go to LYS_INPUT */
2220 ret = get.action_input(node);
2221 } else if (get.child(get.action_output(node))) {
2222 /* go to LYS_OUTPUT */
2223 ret = get.action_output(node);
2224 } else {
2225 /* input action and output action have no data */
2226 ret = NULL;
2227 }
2228 } else {
2229 if ((tmp = get.child(node))) {
2230 ret = tmp;
2231 } else {
2232 /* current node can't have children or has no children */
2233 /* but maybe has some actions or notifs */
2234 if ((tmp = get.actions(node))) {
2235 ret = tmp;
2236 } else if ((tmp = get.notifs(node))) {
2237 ret = tmp;
2238 } else {
2239 ret = NULL;
2240 }
2241 }
2242 }
2243
2244 return ret;
2245}
2246
2247/**
aPiecek3f247652021-04-19 13:40:25 +02002248 * @brief Get new trt_parent_cache if we apply the transfer
2249 * to the child node in the tree.
2250 * @param[in] ca is parent cache for current node.
2251 * @param[in] tc contains current tree node.
2252 * @return Cache for the current node.
2253 */
2254static struct trt_parent_cache
2255tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2256{
2257 struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;
2258
2259 if (!tc->lysc_tree) {
2260 const struct lysp_node *pn = tc->pn;
2261
2262 ret.ancestor =
2263 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
2264 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
2265 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
2266 ca.ancestor;
2267
2268 ret.lys_status =
2269 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
2270 ca.lys_status;
2271
2272 ret.lys_config =
2273 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
2274 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
2275 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
2276 ca.lys_config;
2277
2278 ret.last_list =
2279 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
2280 ca.last_list;
2281 }
2282
2283 return ret;
2284}
2285
2286/**
aPiecekef1e58e2021-04-19 13:19:44 +02002287 * @brief Transformation of the Schema nodes flags to
2288 * Tree diagram \<status\>.
2289 * @param[in] flags is node's flags obtained from the tree.
2290 */
2291static trt_status_type
2292tro_flags2status(uint16_t flags)
2293{
2294 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2295 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2296 TRD_STATUS_TYPE_CURRENT;
2297}
2298
2299/**
2300 * @brief Transformation of the Schema nodes flags to Tree diagram
2301 * \<flags\> but more specifically 'ro' or 'rw'.
2302 * @param[in] flags is node's flags obtained from the tree.
2303 */
2304static trt_flags_type
2305tro_flags2config(uint16_t flags)
2306{
2307 return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
2308 flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
2309 TRD_FLAGS_TYPE_EMPTY;
2310}
2311
2312/**
aPiecek3f247652021-04-19 13:40:25 +02002313 * @brief Print current node's iffeatures.
2314 * @param[in] tc is tree context.
2315 * @param[in,out] out is output handler.
2316 */
2317static void
2318tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2319{
2320 const struct lysp_qname *iffs;
2321
aPiecekbbc02932021-05-21 07:19:41 +02002322 if (tc->lysc_tree) {
2323 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2324 iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
2325 } else {
2326 iffs = tc->pn->iffeatures;
2327 }
aPiecek3f247652021-04-19 13:40:25 +02002328 LY_ARRAY_COUNT_TYPE i;
2329
2330 LY_ARRAY_FOR(iffs, i) {
2331 if (i == 0) {
2332 ly_print_(out, "%s", iffs[i].str);
2333 } else {
2334 ly_print_(out, ",%s", iffs[i].str);
2335 }
2336 }
2337
2338}
2339
2340/**
2341 * @brief Print current list's keys.
2342 *
2343 * Well, actually printing keys in the lysp_tree is trivial,
2344 * because char* points to all keys. However, special functions have
2345 * been reserved for this, because in principle the list of elements
2346 * can have more implementations.
2347 *
2348 * @param[in] tc is tree context.
2349 * @param[in,out] out is output handler.
2350 */
2351static void
2352tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2353{
2354 const struct lysp_node_list *list;
2355
aPiecekbbc02932021-05-21 07:19:41 +02002356 if (tc->lysc_tree) {
2357 assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
2358 list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
2359 } else {
2360 list = (const struct lysp_node_list *)tc->pn;
2361 }
aPiecek3f247652021-04-19 13:40:25 +02002362 assert(list->nodetype & LYS_LIST);
2363
2364 if (trg_charptr_has_data(list->key)) {
2365 ly_print_(out, "%s", list->key);
2366 }
2367}
2368
2369/**
2370 * @brief Get rpcs section if exists.
2371 * @param[in,out] tc is tree context.
2372 * @return Section representation if it exists. The @p tc is modified
2373 * and his pointer points to the first node in rpcs section.
2374 * @return Empty section representation otherwise.
2375 */
2376static struct trt_keyword_stmt
2377tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2378{
aPiecek9f792e52021-04-21 08:33:56 +02002379 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002380 const void *actions;
2381
2382 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002383 actions = tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002384 if (actions) {
2385 tc->cn = actions;
2386 }
2387 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002388 actions = tc->pmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02002389 if (actions) {
2390 tc->pn = actions;
2391 tc->tpn = tc->pn;
2392 }
2393 }
2394
2395 if (actions) {
2396 tc->section = TRD_SECT_RPCS;
2397 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2398 } else {
2399 return TRP_EMPTY_KEYWORD_STMT;
2400 }
2401}
2402
2403/**
2404 * @brief Get notification section if exists
2405 * @param[in,out] tc is tree context.
2406 * @return Section representation if it exists.
2407 * The @p tc is modified and his pointer points to the
2408 * first node in notification section.
2409 * @return Empty section representation otherwise.
2410 */
2411static struct trt_keyword_stmt
2412tro_modi_get_notifications(struct trt_tree_ctx *tc)
2413{
aPiecek9f792e52021-04-21 08:33:56 +02002414 assert(tc);
aPiecek3f247652021-04-19 13:40:25 +02002415 const void *notifs;
2416
2417 if (tc->lysc_tree) {
aPiecek9f792e52021-04-21 08:33:56 +02002418 notifs = tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002419 if (notifs) {
2420 tc->cn = notifs;
2421 }
2422 } else {
aPiecek9f792e52021-04-21 08:33:56 +02002423 notifs = tc->pmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02002424 if (notifs) {
2425 tc->pn = notifs;
2426 tc->tpn = tc->pn;
2427 }
2428 }
2429
2430 if (notifs) {
2431 tc->section = TRD_SECT_NOTIF;
2432 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2433 } else {
2434 return TRP_EMPTY_KEYWORD_STMT;
2435 }
2436}
2437
2438/**
aPiecek96baa7f2021-04-23 12:32:00 +02002439 * @brief Get next yang-data section if it is possible.
aPiecekef1e58e2021-04-19 13:19:44 +02002440 *
2441 * @param[in,out] tc is tree context.
aPiecek96baa7f2021-04-23 12:32:00 +02002442 * @param[in] u is index to the array of extensions (lysc_ext_instance
2443 * or struct lysp_ext_instance).
aPiecekef1e58e2021-04-19 13:19:44 +02002444 * @return Section representation if it exists.
2445 * @return Empty section representation otherwise.
2446 */
2447static struct trt_keyword_stmt
aPiecek96baa7f2021-04-23 12:32:00 +02002448tro_modi_next_yang_data(struct trt_tree_ctx *tc, LY_ARRAY_COUNT_TYPE u)
aPiecekef1e58e2021-04-19 13:19:44 +02002449{
aPiecek96baa7f2021-04-23 12:32:00 +02002450 assert(tc);
2451 const void *node;
2452 const char *yang_data_name;
2453
2454 if (tc->lysc_tree) {
2455 struct lysc_ext_instance *exts;
2456 struct lysc_ext_substmt *substmts;
2457
2458 exts = tc->cmod->exts;
2459 substmts = exts[u].substmts;
2460 if (!substmts) {
2461 return TRP_EMPTY_KEYWORD_STMT;
2462 }
2463 node = *(const struct lysc_node **)substmts->storage;
2464 yang_data_name = exts[u].argument;
2465 } else {
2466 struct lysp_ext_instance *exts;
2467
2468 exts = tc->pmod->exts;
2469 node = exts[u].parsed;
2470 yang_data_name = exts[u].argument;
2471 }
2472
2473 if (tc->lysc_tree) {
2474 tc->cn = node;
2475 } else {
2476 tc->tpn_ext = &tc->pmod->exts[u];
2477 tc->pn = node;
2478 }
2479
2480 if (node) {
2481 tc->section = TRD_SECT_YANG_DATA;
2482 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_YANG_DATA, yang_data_name);
2483 } else {
2484 return TRP_EMPTY_KEYWORD_STMT;
2485 }
aPiecekef1e58e2021-04-19 13:19:44 +02002486}
2487
2488/**
2489 * @brief Get name of the module.
2490 * @param[in] tc is context of the tree.
2491 */
2492static struct trt_keyword_stmt
2493tro_read_module_name(const struct trt_tree_ctx *tc)
2494{
aPiecek9f792e52021-04-21 08:33:56 +02002495 assert(tc);
2496
2497 struct trt_keyword_stmt ret;
2498
2499 ret.type = !tc->lysc_tree && tc->pmod->is_submod ?
2500 TRD_KEYWORD_SUBMODULE :
2501 TRD_KEYWORD_MODULE;
2502
2503 ret.str = !tc->lysc_tree ?
2504 LYSP_MODULE_NAME(tc->pmod) :
2505 tc->cmod->mod->name;
2506
2507 return ret;
aPiecekef1e58e2021-04-19 13:19:44 +02002508}
2509
aPiecekb8d5a0a2021-05-20 08:20:24 +02002510/**
2511 * @brief Create implicit "case" node as parent of @p node.
2512 * @param[in] node child of implicit case node.
2513 * @return The case node ready to print.
2514 */
2515static struct trt_node
2516tro_create_implicit_case_node(struct trt_node node)
2517{
2518 struct trt_node ret;
2519
2520 ret.status = node.status;
2521 ret.flags = TRD_FLAGS_TYPE_EMPTY;
2522 ret.name.type = TRD_NODE_CASE;
2523 ret.name.module_prefix = node.name.module_prefix;
2524 ret.name.str = node.name.str;
2525 ret.type = TRP_EMPTY_TRT_TYPE;
aPiecek7a28e2f2021-05-21 07:27:03 +02002526 ret.iffeatures = 0;
aPiecekb8d5a0a2021-05-20 08:20:24 +02002527 ret.last_one = node.last_one;
2528
2529 return ret;
2530}
2531
aPiecekef1e58e2021-04-19 13:19:44 +02002532/**********************************************************************
2533 * Definition of trop reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02002534 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002535
2536/**
aPiecek61d062b2020-11-02 11:05:09 +01002537 * @brief Check if list statement has keys.
2538 * @param[in] pn is pointer to the list.
2539 * @return 1 if has keys, otherwise 0.
2540 */
2541static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002542trop_list_has_keys(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002543{
aPiecekef1e58e2021-04-19 13:19:44 +02002544 return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
aPiecek61d062b2020-11-02 11:05:09 +01002545}
2546
2547/**
2548 * @brief Check if it contains at least one feature.
aPiecek3f247652021-04-19 13:40:25 +02002549 * @param[in] pn is current node.
aPiecek61d062b2020-11-02 11:05:09 +01002550 * @return 1 if has if-features, otherwise 0.
2551 */
2552static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002553trop_node_has_iffeature(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002554{
2555 LY_ARRAY_COUNT_TYPE u;
aPiecekef1e58e2021-04-19 13:19:44 +02002556 const struct lysp_qname *iffs;
2557
aPiecek61d062b2020-11-02 11:05:09 +01002558 ly_bool ret = 0;
2559
aPiecekef1e58e2021-04-19 13:19:44 +02002560 iffs = pn->iffeatures;
aPiecek61d062b2020-11-02 11:05:09 +01002561 LY_ARRAY_FOR(iffs, u) {
2562 ret = 1;
2563 break;
2564 }
2565 return ret;
2566}
2567
2568/**
2569 * @brief Find out if leaf is also the key in last list.
2570 * @param[in] pn is pointer to leaf.
aPiecek874ea4d2021-04-19 12:26:36 +02002571 * @param[in] ca_last_list is pointer to last visited list.
2572 * Obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002573 * @return 1 if leaf is also the key, otherwise 0.
2574 */
2575static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002576trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002577{
2578 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2579 const struct lysp_node_list *list = ca_last_list;
2580
2581 if (!list) {
2582 return 0;
2583 }
2584 return trg_charptr_has_data(list->key) ?
2585 trg_word_is_present(list->key, leaf->name, ' ') : 0;
2586}
2587
2588/**
2589 * @brief Check if container's type is presence.
2590 * @param[in] pn is pointer to container.
2591 * @return 1 if container has presence statement, otherwise 0.
2592 */
2593static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002594trop_container_has_presence(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002595{
2596 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
2597}
2598
2599/**
2600 * @brief Get leaflist's path without lysp_node type control.
2601 * @param[in] pn is pointer to the leaflist.
2602 */
2603static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002604trop_leaflist_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002605{
2606 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2607
2608 return list->type.path ? list->type.path->expr : NULL;
2609}
2610
2611/**
2612 * @brief Get leaflist's type name without lysp_node type control.
2613 * @param[in] pn is pointer to the leaflist.
2614 */
2615static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002616trop_leaflist_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002617{
2618 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
2619
2620 return list->type.name;
2621}
2622
2623/**
2624 * @brief Get leaf's path without lysp_node type control.
2625 * @param[in] pn is pointer to the leaf node.
2626 */
2627static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002628trop_leaf_refpath(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002629{
2630 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2631
2632 return leaf->type.path ? leaf->type.path->expr : NULL;
2633}
2634
2635/**
2636 * @brief Get leaf's type name without lysp_node type control.
2637 * @param[in] pn is pointer to the leaf's type name.
2638 */
2639static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002640trop_leaf_type_name(const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002641{
2642 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
2643
2644 return leaf->type.name;
2645}
2646
2647/**
aPiecek874ea4d2021-04-19 12:26:36 +02002648 * @brief Get pointer to data using node type specification
2649 * and getter function.
aPiecek61d062b2020-11-02 11:05:09 +01002650 *
aPiecek874ea4d2021-04-19 12:26:36 +02002651 * @param[in] flags is node type specification.
2652 * If it is the correct node, the getter function is called.
2653 * @param[in] f is getter function which provides the desired
2654 * char pointer from the structure.
aPiecek61d062b2020-11-02 11:05:09 +01002655 * @param[in] pn pointer to node.
aPiecek874ea4d2021-04-19 12:26:36 +02002656 * @return NULL if node has wrong type or getter function return
2657 * pointer to NULL.
aPiecek61d062b2020-11-02 11:05:09 +01002658 * @return Pointer to desired char pointer obtained from the node.
2659 */
2660static const char *
aPiecekef1e58e2021-04-19 13:19:44 +02002661trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
aPiecek61d062b2020-11-02 11:05:09 +01002662{
2663 if (pn->nodetype & flags) {
2664 const char *ret = f(pn);
2665 return trg_charptr_has_data(ret) ? ret : NULL;
2666 } else {
2667 return NULL;
2668 }
2669}
2670
2671/**
aPiecek61d062b2020-11-02 11:05:09 +01002672 * @brief Resolve \<status\> of the current node.
2673 * @param[in] nodetype is node's type obtained from the tree.
2674 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002675 * @param[in] ca_lys_status is inherited status
2676 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002677 * @return The status type.
2678 */
2679static trt_status_type
aPiecekef1e58e2021-04-19 13:19:44 +02002680trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
aPiecek61d062b2020-11-02 11:05:09 +01002681{
2682 /* LYS_INPUT and LYS_OUTPUT is special case */
2683 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
aPiecekef1e58e2021-04-19 13:19:44 +02002684 return tro_flags2status(ca_lys_status);
2685 /* if ancestor's status is deprc or obslt
2686 * and also node's status is not set
2687 */
aPiecek61d062b2020-11-02 11:05:09 +01002688 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2689 /* get ancestor's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002690 return tro_flags2status(ca_lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002691 } else {
2692 /* else get node's status */
aPiecekef1e58e2021-04-19 13:19:44 +02002693 return tro_flags2status(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002694 }
2695}
2696
2697/**
2698 * @brief Resolve \<flags\> of the current node.
2699 * @param[in] nodetype is node's type obtained from the tree.
2700 * @param[in] flags is node's flags obtained from the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002701 * @param[in] ca_ancestor is ancestor type obtained
2702 * from trt_parent_cache.
2703 * @param[in] ca_lys_config is inherited config item
2704 * obtained from trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002705 * @return The flags type.
2706 */
2707static trt_flags_type
aPiecekef1e58e2021-04-19 13:19:44 +02002708trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
aPiecek61d062b2020-11-02 11:05:09 +01002709{
2710 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2711 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2712 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2713 return TRD_FLAGS_TYPE_RO;
2714 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2715 return TRD_FLAGS_TYPE_RO;
2716 } else if (nodetype & LYS_NOTIF) {
2717 return TRD_FLAGS_TYPE_NOTIF;
2718 } else if (nodetype & LYS_USES) {
2719 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2720 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2721 return TRD_FLAGS_TYPE_RPC;
aPiecek61d062b2020-11-02 11:05:09 +01002722 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002723 /* config is not set. Look at ancestor's config */
2724 return tro_flags2config(ca_lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002725 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002726 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01002727 }
2728}
2729
2730/**
2731 * @brief Resolve node type of the current node.
2732 * @param[in] pn is pointer to the current node in the tree.
aPiecek874ea4d2021-04-19 12:26:36 +02002733 * @param[in] ca_last_list is pointer to the last visited list.
2734 * Obtained from the trt_parent_cache.
aPiecek61d062b2020-11-02 11:05:09 +01002735 */
2736static trt_node_type
aPiecekef1e58e2021-04-19 13:19:44 +02002737trop_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
aPiecek61d062b2020-11-02 11:05:09 +01002738{
2739 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2740 return TRD_NODE_ELSE;
2741 } else if (pn->nodetype & LYS_CASE) {
2742 return TRD_NODE_CASE;
2743 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2744 return TRD_NODE_OPTIONAL_CHOICE;
2745 } else if (pn->nodetype & LYS_CHOICE) {
2746 return TRD_NODE_CHOICE;
aPiecekef1e58e2021-04-19 13:19:44 +02002747 } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002748 return TRD_NODE_CONTAINER;
aPiecekef1e58e2021-04-19 13:19:44 +02002749 } else if ((pn->nodetype & LYS_LIST) && (trop_list_has_keys(pn))) {
aPiecek61d062b2020-11-02 11:05:09 +01002750 return TRD_NODE_KEYS;
2751 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2752 return TRD_NODE_LISTLEAFLIST;
2753 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2754 return TRD_NODE_OPTIONAL;
aPiecekef1e58e2021-04-19 13:19:44 +02002755 } 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 +01002756 return TRD_NODE_OPTIONAL;
2757 } else {
2758 return TRD_NODE_ELSE;
2759 }
2760}
2761
2762/**
aPiecekef1e58e2021-04-19 13:19:44 +02002763 * @brief Resolve \<type\> of the current node.
2764 * @param[in] pn is current node.
2765 */
2766static struct trt_type
2767trop_resolve_type(const struct lysp_node *pn)
2768{
2769 const char *tmp = NULL;
2770
2771 if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
2772 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2773 } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
2774 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2775 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
2776 return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2777 } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
2778 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2779 } else if (pn->nodetype == LYS_ANYDATA) {
2780 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2781 } else if (pn->nodetype & LYS_ANYXML) {
2782 return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2783 } else {
2784 return TRP_EMPTY_TRT_TYPE;
2785 }
2786}
2787
2788/**
aPiecek61d062b2020-11-02 11:05:09 +01002789 * @brief Transformation of current lysp_node to struct trt_node.
aPiecek874ea4d2021-04-19 12:26:36 +02002790 * @param[in] ca contains stored important data
2791 * when browsing the tree downwards.
aPiecek61d062b2020-11-02 11:05:09 +01002792 * @param[in] tc is context of the tree.
2793 */
2794static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002795trop_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002796{
aPiecekef1e58e2021-04-19 13:19:44 +02002797 const struct lysp_node *pn;
2798 struct trt_node ret;
2799
aPiecek61d062b2020-11-02 11:05:09 +01002800 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
aPiecekef1e58e2021-04-19 13:19:44 +02002801
2802 pn = tc->pn;
2803 ret = TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002804
2805 /* <status> */
aPiecekef1e58e2021-04-19 13:19:44 +02002806 ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
aPiecek61d062b2020-11-02 11:05:09 +01002807
2808 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2809 /* <flags> */
aPiecekef1e58e2021-04-19 13:19:44 +02002810 ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
aPiecek61d062b2020-11-02 11:05:09 +01002811
2812 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2813 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2814 /* set type of the node */
aPiecekef1e58e2021-04-19 13:19:44 +02002815 ret.name.type = trop_resolve_node_type(pn, ca.last_list);
aPiecek61d062b2020-11-02 11:05:09 +01002816
aPiecek34fa3772021-05-21 12:35:46 +02002817 /* The parsed tree is not compiled, so no node can be augmented
2818 * from another module. This means that nodes from the parsed tree
2819 * will never have the prefix.
2820 */
aPiecek61d062b2020-11-02 11:05:09 +01002821 ret.name.module_prefix = NULL;
2822
2823 /* set node's name */
2824 ret.name.str = pn->name;
2825
2826 /* <type> */
aPiecekef1e58e2021-04-19 13:19:44 +02002827 ret.type = trop_resolve_type(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002828
2829 /* <iffeature> */
aPiecekef1e58e2021-04-19 13:19:44 +02002830 ret.iffeatures = trop_node_has_iffeature(pn);
aPiecek61d062b2020-11-02 11:05:09 +01002831
aPiecek3f247652021-04-19 13:40:25 +02002832 ret.last_one = !tro_next_sibling(pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01002833
2834 return ret;
2835}
2836
aPiecekef1e58e2021-04-19 13:19:44 +02002837/**
2838 * @brief Find out if the current node has siblings.
2839 * @param[in] tc is context of the tree.
2840 * @return 1 if sibling exists otherwise 0.
2841 */
2842static ly_bool
2843trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
2844{
aPiecek3f247652021-04-19 13:40:25 +02002845 return tro_next_sibling(tc->pn, tc->lysc_tree) != NULL;
aPiecekef1e58e2021-04-19 13:19:44 +02002846}
2847
aPiecek96baa7f2021-04-23 12:32:00 +02002848/**
2849 * @brief Print all yang-data sections and print three dots instead
2850 * of nodes.
2851 * @param[in] exts is array of YANG extension instances from parsed
2852 * module (@ref sizedarrays).
2853 * @param[in] mll is maximum number of characters that can be printed
2854 * on one line.
2855 * @param[in,out] out is output handler.
2856 */
2857static void
2858trop_yang_data_sections(const struct lysp_ext_instance *exts, size_t mll, struct ly_out *out)
2859{
2860 struct trt_keyword_stmt ks;
2861 LY_ARRAY_COUNT_TYPE u;
2862 struct trt_wrapper wr;
2863
2864 if (!exts) {
2865 return;
2866 }
2867
2868 ly_print_(out, "\n");
2869 ks.type = TRD_KEYWORD_YANG_DATA;
2870 wr = TRP_INIT_WRAPPER_BODY;
2871
2872 LY_ARRAY_FOR(exts, u) {
2873 ly_print_(out, "\n");
2874
2875 /* yang-data <yang-data-name>: */
2876 ks.str = exts[u].argument;
2877 trp_print_keyword_stmt(ks, mll, 0, out);
2878 ly_print_(out, "\n");
2879
2880 /* ... */
2881 trp_print_wrapper(wr, out);
2882 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
2883 }
2884}
2885
aPiecek874ea4d2021-04-19 12:26:36 +02002886/**********************************************************************
aPiecekef1e58e2021-04-19 13:19:44 +02002887 * Modify trop getters
aPiecek874ea4d2021-04-19 12:26:36 +02002888 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01002889
2890/**
aPiecek874ea4d2021-04-19 12:26:36 +02002891 * @brief Change current node pointer to its parent
2892 * but only if parent exists.
2893 * @param[in,out] tc is tree context.
2894 * Contains pointer to the current node.
aPiecek61d062b2020-11-02 11:05:09 +01002895 * @return 1 if the node had parents and the change was successful.
aPiecek874ea4d2021-04-19 12:26:36 +02002896 * @return 0 if the node did not have parents.
2897 * The pointer to the current node did not change.
aPiecek61d062b2020-11-02 11:05:09 +01002898 */
2899static ly_bool
aPiecekef1e58e2021-04-19 13:19:44 +02002900trop_modi_parent(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002901{
2902 assert(tc && tc->pn);
2903 /* If no parent exists, stay in actual node. */
aPiecek96baa7f2021-04-23 12:32:00 +02002904 if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
aPiecek61d062b2020-11-02 11:05:09 +01002905 tc->pn = tc->pn->parent;
2906 return 1;
2907 } else {
2908 return 0;
2909 }
2910}
2911
2912/**
aPiecek874ea4d2021-04-19 12:26:36 +02002913 * @brief Change the current node pointer to its child
2914 * but only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002915 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002916 * @param[in,out] tc is context of the tree.
2917 * Contains pointer to the current node.
2918 * @return Non-empty \<node\> representation of the current
2919 * node's child. The @p tc is modified.
2920 * @return Empty \<node\> representation if child don't exists.
2921 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002922 */
2923static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002924trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002925{
aPiecekef1e58e2021-04-19 13:19:44 +02002926 const struct lysp_node *tmp;
2927
aPiecek61d062b2020-11-02 11:05:09 +01002928 assert(tc && tc->pn);
2929
aPiecek3f247652021-04-19 13:40:25 +02002930 if ((tmp = tro_next_child(tc->pn, tc->lysc_tree))) {
aPiecekef1e58e2021-04-19 13:19:44 +02002931 tc->pn = tmp;
aPiecek3f247652021-04-19 13:40:25 +02002932 return trop_read_node(tro_parent_cache_for_child(ca, tc), tc);
aPiecek61d062b2020-11-02 11:05:09 +01002933 } else {
aPiecekef1e58e2021-04-19 13:19:44 +02002934 return TRP_EMPTY_NODE;
aPiecek61d062b2020-11-02 11:05:09 +01002935 }
2936}
2937
2938/**
aPiecek874ea4d2021-04-19 12:26:36 +02002939 * @brief Change the current node pointer to the first child of node's
2940 * parent. If current node is already first sibling/child then nothing
2941 * will change.
aPiecek61d062b2020-11-02 11:05:09 +01002942 * @param[in,out] tc is tree context.
2943 */
2944static void
aPiecekef1e58e2021-04-19 13:19:44 +02002945trop_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002946{
aPiecek9f792e52021-04-21 08:33:56 +02002947 assert(tc && tc->pn && tc->pmod);
aPiecek61d062b2020-11-02 11:05:09 +01002948
aPiecekef1e58e2021-04-19 13:19:44 +02002949 if (trop_modi_parent(tc)) {
2950 trop_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
aPiecek61d062b2020-11-02 11:05:09 +01002951 } else {
2952 /* current node is top-node */
aPiecek61d062b2020-11-02 11:05:09 +01002953 switch (tc->section) {
2954 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02002955 tc->pn = tc->pmod->data;
aPiecek96baa7f2021-04-23 12:32:00 +02002956 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002957 break;
2958 case TRD_SECT_AUGMENT:
aPiecek9f792e52021-04-21 08:33:56 +02002959 tc->pn = (const struct lysp_node *)tc->pmod->augments;
aPiecek96baa7f2021-04-23 12:32:00 +02002960 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002961 break;
2962 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02002963 tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
aPiecek96baa7f2021-04-23 12:32:00 +02002964 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002965 break;
2966 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02002967 tc->pn = (const struct lysp_node *)tc->pmod->notifs;
aPiecek96baa7f2021-04-23 12:32:00 +02002968 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002969 break;
2970 case TRD_SECT_GROUPING:
aPiecek9f792e52021-04-21 08:33:56 +02002971 tc->pn = (const struct lysp_node *)tc->pmod->groupings;
aPiecek96baa7f2021-04-23 12:32:00 +02002972 tc->tpn = tc->pn;
aPiecek61d062b2020-11-02 11:05:09 +01002973 break;
2974 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02002975 /* tpn in this case is of type lysp_ext_instance */
2976 tc->pn = tc->tpn_ext->parsed;
aPiecek61d062b2020-11-02 11:05:09 +01002977 break;
aPiecek96baa7f2021-04-23 12:32:00 +02002978 default:
2979 assert(0);
aPiecek61d062b2020-11-02 11:05:09 +01002980 }
aPiecek61d062b2020-11-02 11:05:09 +01002981 }
2982}
2983
2984/**
aPiecek874ea4d2021-04-19 12:26:36 +02002985 * @brief Change the pointer to the current node to its next sibling
2986 * only if exists.
aPiecek61d062b2020-11-02 11:05:09 +01002987 * @param[in] ca contains inherited data from ancestors.
aPiecek874ea4d2021-04-19 12:26:36 +02002988 * @param[in,out] tc is tree context.
2989 * Contains pointer to the current node.
2990 * @return Non-empty \<node\> representation if sibling exists.
2991 * The @p tc is modified.
2992 * @return Empty \<node\> representation otherwise.
2993 * The @p tc is not modified.
aPiecek61d062b2020-11-02 11:05:09 +01002994 */
2995static struct trt_node
aPiecekef1e58e2021-04-19 13:19:44 +02002996trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01002997{
aPiecekef1e58e2021-04-19 13:19:44 +02002998 const struct lysp_node *pn;
aPiecekef1e58e2021-04-19 13:19:44 +02002999
3000 assert(tc && tc->pn);
3001
aPiecek3f247652021-04-19 13:40:25 +02003002 pn = tro_next_sibling(tc->pn, tc->lysc_tree);
aPiecek61d062b2020-11-02 11:05:09 +01003003
aPiecekef1e58e2021-04-19 13:19:44 +02003004 if (pn) {
aPiecek96baa7f2021-04-23 12:32:00 +02003005 if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_YANG_DATA)) {
3006 tc->tpn = pn;
3007 }
aPiecekef1e58e2021-04-19 13:19:44 +02003008 tc->pn = pn;
aPiecekef1e58e2021-04-19 13:19:44 +02003009 return trop_read_node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003010 } else {
3011 return TRP_EMPTY_NODE;
3012 }
3013}
3014
3015/**
3016 * @brief Get next (or first) augment section if exists.
aPiecek874ea4d2021-04-19 12:26:36 +02003017 * @param[in,out] tc is tree context. It is modified and his current
3018 * node is set to the lysp_node_augment.
aPiecek61d062b2020-11-02 11:05:09 +01003019 * @return Section's representation if (next augment) section exists.
aPiecek61d062b2020-11-02 11:05:09 +01003020 * @return Empty section structure otherwise.
3021 */
3022static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003023trop_modi_next_augment(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003024{
aPiecek9f792e52021-04-21 08:33:56 +02003025 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003026 const struct lysp_node_augment *augs;
3027
3028 /* if next_augment func was called for the first time */
3029 if (tc->section != TRD_SECT_AUGMENT) {
3030 tc->section = TRD_SECT_AUGMENT;
aPiecek9f792e52021-04-21 08:33:56 +02003031 augs = tc->pmod->augments;
aPiecek61d062b2020-11-02 11:05:09 +01003032 } else {
3033 /* get augment sibling from top-node pointer */
aPiecekdc8fd572021-04-19 10:47:23 +02003034 augs = (const struct lysp_node_augment *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003035 }
3036
aPiecekdc8fd572021-04-19 10:47:23 +02003037 if (augs) {
3038 tc->pn = &augs->node;
aPiecek61d062b2020-11-02 11:05:09 +01003039 tc->tpn = tc->pn;
3040 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
3041 } else {
3042 return TRP_EMPTY_KEYWORD_STMT;
3043 }
3044}
3045
3046/**
aPiecek61d062b2020-11-02 11:05:09 +01003047 * @brief Get next (or first) grouping section if exists
aPiecek874ea4d2021-04-19 12:26:36 +02003048 * @param[in,out] tc is tree context. It is modified and his current
3049 * node is set to the lysp_node_grp.
aPiecek61d062b2020-11-02 11:05:09 +01003050 * @return The next (or first) section representation if it exists.
aPiecek61d062b2020-11-02 11:05:09 +01003051 * @return Empty section representation otherwise.
3052 */
3053static struct trt_keyword_stmt
aPiecekef1e58e2021-04-19 13:19:44 +02003054trop_modi_next_grouping(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003055{
aPiecek9f792e52021-04-21 08:33:56 +02003056 assert(tc);
aPiecek61d062b2020-11-02 11:05:09 +01003057 const struct lysp_node_grp *grps;
3058
3059 if (tc->section != TRD_SECT_GROUPING) {
3060 tc->section = TRD_SECT_GROUPING;
aPiecek9f792e52021-04-21 08:33:56 +02003061 grps = tc->pmod->groupings;
aPiecek61d062b2020-11-02 11:05:09 +01003062 } else {
aPiecekdc8fd572021-04-19 10:47:23 +02003063 grps = (const struct lysp_node_grp *)tc->tpn->next;
aPiecek61d062b2020-11-02 11:05:09 +01003064 }
3065
aPiecekdc8fd572021-04-19 10:47:23 +02003066 if (grps) {
3067 tc->pn = &grps->node;
aPiecek61d062b2020-11-02 11:05:09 +01003068 tc->tpn = tc->pn;
3069 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
3070 } else {
3071 return TRP_EMPTY_KEYWORD_STMT;
3072 }
3073}
3074
aPiecek874ea4d2021-04-19 12:26:36 +02003075/**********************************************************************
aPiecek3f247652021-04-19 13:40:25 +02003076 * Definition of troc reading functions
aPiecek874ea4d2021-04-19 12:26:36 +02003077 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003078
3079/**
aPiecek3f247652021-04-19 13:40:25 +02003080 * @copydoc trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003081 */
aPiecek3f247652021-04-19 13:40:25 +02003082static ly_bool
3083troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003084{
aPiecek3f247652021-04-19 13:40:25 +02003085 return tro_next_sibling(tc->cn, tc->lysc_tree) != NULL;
3086}
aPiecek61d062b2020-11-02 11:05:09 +01003087
aPiecek3f247652021-04-19 13:40:25 +02003088/**
3089 * @brief Resolve \<flags\> of the current node.
3090 *
3091 * Use this function only if trt_tree_ctx.lysc_tree is true.
3092 *
3093 * @param[in] nodetype is current lysc_node.nodetype.
3094 * @param[in] flags is current lysc_node.flags.
3095 * @return The flags type.
3096 */
3097static trt_flags_type
3098troc_resolve_flags(uint16_t nodetype, uint16_t flags)
3099{
3100 if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
3101 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
3102 } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
3103 return TRD_FLAGS_TYPE_RO;
3104 } else if (nodetype & LYS_IS_NOTIF) {
3105 return TRD_FLAGS_TYPE_RO;
3106 } else if (nodetype & LYS_NOTIF) {
3107 return TRD_FLAGS_TYPE_NOTIF;
3108 } else if (nodetype & LYS_USES) {
3109 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
3110 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
3111 return TRD_FLAGS_TYPE_RPC;
3112 } else {
3113 return tro_flags2config(flags);
aPiecek61d062b2020-11-02 11:05:09 +01003114 }
aPiecek61d062b2020-11-02 11:05:09 +01003115}
3116
3117/**
aPiecek3f247652021-04-19 13:40:25 +02003118 * @brief Resolve node type of the current node.
aPiecek61d062b2020-11-02 11:05:09 +01003119 *
aPiecek3f247652021-04-19 13:40:25 +02003120 * Use this function only if trt_tree_ctx.lysc_tree is true.
aPiecek61d062b2020-11-02 11:05:09 +01003121 *
aPiecek3f247652021-04-19 13:40:25 +02003122 * @param[in] nodetype is current lysc_node.nodetype.
3123 * @param[in] flags is current lysc_node.flags.
3124 */
3125static trt_node_type
3126troc_resolve_node_type(uint16_t nodetype, uint16_t flags)
3127{
3128 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
3129 return TRD_NODE_ELSE;
3130 } else if (nodetype & LYS_CASE) {
3131 return TRD_NODE_CASE;
3132 } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
3133 return TRD_NODE_OPTIONAL_CHOICE;
3134 } else if (nodetype & LYS_CHOICE) {
3135 return TRD_NODE_CHOICE;
3136 } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
3137 return TRD_NODE_CONTAINER;
3138 } else if ((nodetype & LYS_LIST) && !(flags & LYS_KEYLESS)) {
3139 return TRD_NODE_KEYS;
3140 } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3141 return TRD_NODE_LISTLEAFLIST;
3142 } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
3143 return TRD_NODE_OPTIONAL;
3144 } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
3145 return TRD_NODE_OPTIONAL;
3146 } else {
3147 return TRD_NODE_ELSE;
3148 }
3149}
3150
3151/**
aPiecek34fa3772021-05-21 12:35:46 +02003152 * @brief Resolve prefix (<prefix>:<name>) of node that has been
3153 * placed from another module via an augment statement.
3154 *
3155 * @param[in] cn is current compiled node.
3156 * @param[in] current_compiled_module is module whose nodes are
3157 * currently being printed.
3158 * @return Prefix of foreign module or NULL.
3159 */
3160static const char *
3161troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
3162{
3163 const struct lys_module *node_module;
3164 const char *ret = NULL;
3165
3166 node_module = cn->module;
3167 if (node_module->compiled != current_compiled_module) {
3168 ret = node_module->prefix;
3169 }
3170
3171 return ret;
3172}
3173
3174/**
aPiecek3f247652021-04-19 13:40:25 +02003175 * @brief Transformation of current lysc_node to struct trt_node.
3176 * @param[in] ca is not used.
3177 * @param[in] tc is context of the tree.
3178 */
3179static struct trt_node
3180troc_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
3181{
3182 (void) ca;
3183 const struct lysc_node *cn;
3184 struct trt_node ret;
3185
aPiecek082c7dc2021-05-20 08:55:07 +02003186 assert(tc && tc->cn);
aPiecek3f247652021-04-19 13:40:25 +02003187
3188 cn = tc->cn;
3189 ret = TRP_EMPTY_NODE;
3190
3191 /* <status> */
3192 ret.status = tro_flags2status(cn->flags);
3193
3194 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
3195 /* <flags> */
3196 ret.flags = troc_resolve_flags(cn->nodetype, cn->flags);
3197
3198 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
3199 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
3200 /* set type of the node */
3201 ret.name.type = troc_resolve_node_type(cn->nodetype, cn->flags);
3202
aPiecek34fa3772021-05-21 12:35:46 +02003203 /* <prefix> */
3204 ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);
aPiecek3f247652021-04-19 13:40:25 +02003205
3206 /* set node's name */
3207 ret.name.str = cn->name;
3208
aPiecekbbc02932021-05-21 07:19:41 +02003209 if (TRP_TREE_CTX_LYSP_NODE_PRESENT(cn)) {
aPiecek082c7dc2021-05-20 08:55:07 +02003210 /* <type> */
3211 ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));
aPiecek3f247652021-04-19 13:40:25 +02003212
aPiecek082c7dc2021-05-20 08:55:07 +02003213 /* <iffeature> */
3214 ret.iffeatures = trop_node_has_iffeature(TRP_TREE_CTX_GET_LYSP_NODE(cn));
3215 } else {
aPiecekbbc02932021-05-21 07:19:41 +02003216 /* only the implicit case node doesn't have access to lysp node */
aPiecek082c7dc2021-05-20 08:55:07 +02003217 assert(tc->cn->nodetype & LYS_CASE);
3218
3219 /* <type> */
3220 ret.type = TRP_EMPTY_TRT_TYPE;
3221
aPiecek7a28e2f2021-05-21 07:27:03 +02003222 /* <iffeature> */
3223 ret.iffeatures = 0;
aPiecek082c7dc2021-05-20 08:55:07 +02003224 }
aPiecek3f247652021-04-19 13:40:25 +02003225
3226 ret.last_one = !tro_next_sibling(cn, tc->lysc_tree);
3227
3228 return ret;
3229}
3230
3231/**********************************************************************
3232 * Modify troc getters
3233 *********************************************************************/
3234
3235/**
aPiecek01598c02021-04-23 14:18:24 +02003236 * @copydoc ::trop_modi_parent()
aPiecek3f247652021-04-19 13:40:25 +02003237 */
3238static ly_bool
3239troc_modi_parent(struct trt_tree_ctx *tc)
3240{
3241 assert(tc && tc->cn);
3242 /* If no parent exists, stay in actual node. */
3243 if (tc->cn->parent) {
3244 tc->cn = tc->cn->parent;
3245 return 1;
3246 } else {
3247 return 0;
3248 }
3249}
3250
3251/**
aPiecek01598c02021-04-23 14:18:24 +02003252 * @copydoc ::trop_modi_next_sibling()
aPiecek3f247652021-04-19 13:40:25 +02003253 */
3254static struct trt_node
3255troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3256{
3257 const struct lysc_node *cn;
3258
3259 assert(tc && tc->cn);
3260
3261 cn = tro_next_sibling(tc->cn, tc->lysc_tree);
3262
3263 /* if next sibling exists */
3264 if (cn) {
3265 /* update trt_tree_ctx */
3266 tc->cn = cn;
3267 return troc_read_node(ca, tc);
3268 } else {
3269 return TRP_EMPTY_NODE;
3270 }
3271}
3272
3273/**
3274 * @copydoc trop_modi_next_child()
3275 */
3276static struct trt_node
3277troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
3278{
3279 const struct lysc_node *tmp;
3280
3281 assert(tc && tc->cn);
3282
3283 if ((tmp = tro_next_child(tc->cn, tc->lysc_tree))) {
3284 tc->cn = tmp;
3285 return troc_read_node(ca, tc);
3286 } else {
3287 return TRP_EMPTY_NODE;
3288 }
3289}
3290
3291/**
aPiecek01598c02021-04-23 14:18:24 +02003292 * @copydoc ::trop_modi_first_sibling()
aPiecek61d062b2020-11-02 11:05:09 +01003293 */
3294static void
aPiecek3f247652021-04-19 13:40:25 +02003295troc_modi_first_sibling(struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003296{
aPiecek3f247652021-04-19 13:40:25 +02003297 assert(tc && tc->cn);
aPiecek61d062b2020-11-02 11:05:09 +01003298
aPiecek3f247652021-04-19 13:40:25 +02003299 if (troc_modi_parent(tc)) {
3300 troc_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
3301 } else {
3302 /* current node is top-node */
aPiecek3f247652021-04-19 13:40:25 +02003303 switch (tc->section) {
3304 case TRD_SECT_MODULE:
aPiecek9f792e52021-04-21 08:33:56 +02003305 tc->cn = tc->cmod->data;
aPiecek3f247652021-04-19 13:40:25 +02003306 break;
3307 case TRD_SECT_RPCS:
aPiecek9f792e52021-04-21 08:33:56 +02003308 tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
aPiecek3f247652021-04-19 13:40:25 +02003309 break;
3310 case TRD_SECT_NOTIF:
aPiecek9f792e52021-04-21 08:33:56 +02003311 tc->cn = (const struct lysc_node *)tc->cmod->notifs;
aPiecek3f247652021-04-19 13:40:25 +02003312 break;
3313 case TRD_SECT_YANG_DATA:
aPiecek96baa7f2021-04-23 12:32:00 +02003314 /* nothing to do */
aPiecek3f247652021-04-19 13:40:25 +02003315 break;
3316 default:
3317 assert(0);
3318 }
aPiecek61d062b2020-11-02 11:05:09 +01003319 }
3320}
3321
aPiecek874ea4d2021-04-19 12:26:36 +02003322/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003323 * Definition of tree browsing functions
aPiecek874ea4d2021-04-19 12:26:36 +02003324 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003325
3326/**
3327 * @brief Get size of node name.
3328 * @param[in] name contains name and mark.
3329 * @return positive value total size of the node name.
aPiecek874ea4d2021-04-19 12:26:36 +02003330 * @return negative value as an indication that option mark
3331 * is included in the total size.
aPiecek61d062b2020-11-02 11:05:09 +01003332 */
3333static int32_t
3334trb_strlen_of_name_and_mark(struct trt_node_name name)
3335{
3336 size_t name_len = strlen(name.str);
3337
3338 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
3339 /* counting also parentheses */
3340 name_len += 2;
3341 }
3342
3343 return trp_mark_is_used(name) ?
3344 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
3345 (int32_t)name_len;
3346}
3347
3348/**
aPiecek874ea4d2021-04-19 12:26:36 +02003349 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
3350 * for a particular node.
aPiecek61d062b2020-11-02 11:05:09 +01003351 * @param[in] name is the node for which we get btw_opts_type.
aPiecek874ea4d2021-04-19 12:26:36 +02003352 * @param[in] max_len4all is the maximum value of btw_opts_type
3353 * that it can have.
3354 * @return Indent between \<opts\> and \<type\> for node.
aPiecek61d062b2020-11-02 11:05:09 +01003355 */
3356static int16_t
3357trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
3358{
3359 int32_t name_len;
3360 int16_t min_len;
3361 int16_t ret;
3362
3363 name_len = trb_strlen_of_name_and_mark(name);
3364
3365 /* negative value indicate that in name is some opt mark */
3366 min_len = name_len < 0 ?
3367 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
3368 TRD_INDENT_BEFORE_TYPE;
3369 ret = abs(max_len4all) - abs(name_len);
3370
3371 /* correction -> negative indicate that name is too long. */
3372 return ret < 0 ? min_len : ret;
3373}
3374
3375/**
3376 * @brief Print node.
3377 *
aPiecek01598c02021-04-23 14:18:24 +02003378 * This function is wrapper for ::trp_print_entire_node().
aPiecek874ea4d2021-04-19 12:26:36 +02003379 * But difference is that take @p max_gap_before_type which will be
3380 * used to set the unified alignment.
aPiecek61d062b2020-11-02 11:05:09 +01003381 *
aPiecek9bdd7592021-05-20 08:13:20 +02003382 * @param[in] node to print.
aPiecek61d062b2020-11-02 11:05:09 +01003383 * @param[in] max_gap_before_type is number of indent before \<type\>.
3384 * @param[in] wr is wrapper for printing indentation before node.
aPiecek61d062b2020-11-02 11:05:09 +01003385 * @param[in] pc contains mainly functions for printing.
3386 * @param[in] tc is tree context.
3387 */
3388static void
aPiecek9bdd7592021-05-20 08:13:20 +02003389trb_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 +01003390{
aPiecek61d062b2020-11-02 11:05:09 +01003391 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
3392
3393 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
3394 /* print actual node with unified indent */
3395 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
3396 }
3397 /* after -> print actual node with default indent */
3398 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
3399 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
3400}
3401
3402/**
aPiecek874ea4d2021-04-19 12:26:36 +02003403 * @brief Check if parent of the current node is the last
3404 * of his siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003405 *
aPiecek874ea4d2021-04-19 12:26:36 +02003406 * To mantain stability use this function only if the current node is
3407 * the first of the siblings.
3408 * Side-effect -> current node is set to the first sibling
3409 * if node has a parent otherwise no side-effect.
aPiecek61d062b2020-11-02 11:05:09 +01003410 *
aPiecek01598c02021-04-23 14:18:24 +02003411 * @param[in] fp contains all @ref TRP_tro callback functions.
aPiecek61d062b2020-11-02 11:05:09 +01003412 * @param[in,out] tc is tree context.
3413 * @return 1 if parent is last sibling otherwise 0.
3414 */
3415static ly_bool
3416trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
3417{
3418 if (fp.modify.parent(tc)) {
3419 ly_bool ret = fp.read.if_sibling_exists(tc);
3420 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
3421 return !ret;
3422 } else {
3423 return !fp.read.if_sibling_exists(tc);
3424 }
3425}
3426
3427/**
3428 * @brief Find sibling with the biggest node name and return that size.
3429 *
3430 * Side-effect -> Current node is set to the first sibling.
3431 *
3432 * @param[in] ca contains inherited data from ancestors.
3433 * @param[in] pc contains mainly functions for printing.
3434 * @param[in,out] tc is tree context.
aPiecek874ea4d2021-04-19 12:26:36 +02003435 * @return positive number as a sign that only the node name is
3436 * included in the size.
3437 * @return negative number sign that node name and his opt mark is
3438 * included in the size.
aPiecek61d062b2020-11-02 11:05:09 +01003439 */
3440static int32_t
3441trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3442{
3443 int32_t ret = 0;
3444
3445 pc->fp.modify.first_sibling(tc);
3446
3447 for (struct trt_node node = pc->fp.read.node(ca, tc);
3448 !trp_node_is_empty(node);
3449 node = pc->fp.modify.next_sibling(ca, tc)) {
3450 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
3451 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
3452 }
3453 pc->fp.modify.first_sibling(tc);
3454 return ret;
3455}
3456
3457/**
aPiecek874ea4d2021-04-19 12:26:36 +02003458 * @brief Find maximal indent between
3459 * \<opts\> and \<type\> for siblings.
aPiecek61d062b2020-11-02 11:05:09 +01003460 *
3461 * Side-effect -> Current node is set to the first sibling.
3462 *
3463 * @param[in] ca contains inherited data from ancestors.
3464 * @param[in] pc contains mainly functions for printing.
3465 * @param[in,out] tc is tree context.
3466 * @return max btw_opts_type value for rest of the siblings
3467 */
3468static int16_t
3469trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3470{
3471 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
3472 int16_t ind_before_type = maxlen_node_name < 0 ?
3473 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
3474 TRD_INDENT_BEFORE_TYPE;
3475
3476 return abs(maxlen_node_name) + ind_before_type;
3477}
3478
3479/**
aPiecek874ea4d2021-04-19 12:26:36 +02003480 * @brief Find out if it is possible to unify
3481 * the alignment before \<type\>.
aPiecek61d062b2020-11-02 11:05:09 +01003482 *
aPiecek874ea4d2021-04-19 12:26:36 +02003483 * The goal is for all node siblings to have the same alignment
3484 * for \<type\> as if they were in a column. All siblings who cannot
3485 * adapt because they do not fit on the line at all are ignored.
aPiecek61d062b2020-11-02 11:05:09 +01003486 * Side-effect -> Current node is set to the first sibling.
3487 *
3488 * @param[in] ca contains inherited data from ancestors.
3489 * @param[in] pc contains mainly functions for printing.
3490 * @param[in,out] tc is tree context.
3491 * @return 0 if all siblings cannot fit on the line.
aPiecek874ea4d2021-04-19 12:26:36 +02003492 * @return positive number indicating the maximum number of spaces
3493 * before \<type\> if the length of the node name is 0. To calculate
3494 * the trt_indent_in_node.btw_opts_type indent size for a particular
aPiecek01598c02021-04-23 14:18:24 +02003495 * node, use the ::trb_calc_btw_opts_type().
aPiecek61d062b2020-11-02 11:05:09 +01003496*/
3497static uint32_t
3498trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3499{
3500 return trb_max_btw_opts_type4siblings(ca, pc, tc);
3501}
3502
3503/**
aPiecekb8d5a0a2021-05-20 08:20:24 +02003504 * @brief Check if there is no case statement
3505 * under the choice statement.
3506 *
3507 * It can return true only if the Parsed schema tree
3508 * is used for browsing.
3509 *
3510 * @param[in] tc is tree context.
3511 * @return 1 if implicit case statement is present otherwise 0.
3512 */
3513static ly_bool
3514trb_need_implicit_node_case(struct trt_tree_ctx *tc)
3515{
3516 return !tc->lysc_tree && tc->pn->parent &&
3517 (tc->pn->parent->nodetype & LYS_CHOICE) &&
3518 (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
3519 LYS_LEAF | LYS_LEAFLIST));
3520}
3521
3522static void trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type,
3523 struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);
3524
3525/**
3526 * @brief Print implicit case node and his subtree.
3527 *
3528 * @param[in] node is child of implicit case.
3529 * @param[in] wr is wrapper for printing identation before node.
3530 * @param[in] ca contains inherited data from ancestors.
3531 * @param[in] pc contains mainly functions for printing.
3532 * @param[in] tc is tree context. Its settings should be the same as
3533 * before the function call.
3534 */
3535static void
3536trb_print_implicit_node_case_subtree(struct trt_node node, struct trt_wrapper wr,
3537 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3538{
3539 struct trt_node case_node;
3540 struct trt_wrapper wr_case_child;
3541
3542 case_node = tro_create_implicit_case_node(node);
3543 ly_print_(pc->out, "\n");
3544 trb_print_entire_node(case_node, 0, wr, pc, tc);
3545 wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
3546 trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
3547 ly_print_(pc->out, "\n");
3548 trb_print_subtree_nodes(node, 0, wr_case_child, ca, pc, tc);
3549}
3550
3551/**
aPiecek874ea4d2021-04-19 12:26:36 +02003552 * @brief For the current node: recursively print all of its child
3553 * nodes and all of its siblings, including their children.
aPiecek61d062b2020-11-02 11:05:09 +01003554 *
aPiecek01598c02021-04-23 14:18:24 +02003555 * This function is an auxiliary function for ::trb_print_subtree_nodes().
aPiecek61d062b2020-11-02 11:05:09 +01003556 * The parent of the current node is expected to exist.
aPiecek874ea4d2021-04-19 12:26:36 +02003557 * Nodes are printed, including unified sibling node alignment
3558 * (align \<type\> to column).
aPiecek61d062b2020-11-02 11:05:09 +01003559 * Side-effect -> current node is set to the last sibling.
3560 *
3561 * @param[in] wr is wrapper for printing identation before node.
3562 * @param[in] ca contains inherited data from ancestors.
3563 * @param[in] pc contains mainly functions for printing.
3564 * @param[in,out] tc is tree context.
3565 */
3566static void
3567trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3568{
3569 uint32_t max_gap_before_type;
3570 ly_bool sibling_flag = 0;
3571 ly_bool child_flag = 0;
3572
3573 /* if node is last sibling, then do not add '|' to wrapper */
3574 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
3575 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
3576
3577 /* try unified indentation in node */
3578 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3579
3580 /* print all siblings */
3581 do {
3582 struct trt_parent_cache new_ca;
3583 struct trt_node node;
aPiecek9bdd7592021-05-20 08:13:20 +02003584 node = pc->fp.read.node(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003585
aPiecekb8d5a0a2021-05-20 08:20:24 +02003586 if (!trb_need_implicit_node_case(tc)) {
3587 /* normal behavior */
3588 ly_print_(pc->out, "\n");
3589 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
3590 new_ca = tro_parent_cache_for_child(ca, tc);
3591 /* go to the actual node's child or stay in actual node */
3592 node = pc->fp.modify.next_child(ca, tc);
3593 child_flag = !trp_node_is_empty(node);
aPiecek61d062b2020-11-02 11:05:09 +01003594
aPiecekb8d5a0a2021-05-20 08:20:24 +02003595 if (child_flag) {
3596 /* print all childs - recursive call */
3597 trb_print_nodes(wr, new_ca, pc, tc);
3598 /* get back from child node to actual node */
3599 pc->fp.modify.parent(tc);
3600 }
3601 } else {
3602 /* The case statement is omitted (shorthand).
3603 * Print implicit case node and his subtree.
3604 */
3605 trb_print_implicit_node_case_subtree(node, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003606 }
3607
3608 /* go to the actual node's sibling */
3609 node = pc->fp.modify.next_sibling(ca, tc);
3610 sibling_flag = !trp_node_is_empty(node);
3611
3612 /* go to the next sibling or stay in actual node */
3613 } while (sibling_flag);
3614}
3615
3616/**
aPiecek153b00f2021-04-20 13:52:57 +02003617 * @brief Calculate the wrapper about how deep in the tree the node is.
3618 * @param[in] node from which to count.
3619 * @return wrapper for @p node.
3620 */
3621static struct trt_wrapper
3622trb_count_depth(const struct lysc_node *node)
3623{
3624 struct trt_wrapper wr = TRP_INIT_WRAPPER_TOP;
3625 const struct lysc_node *parent;
3626
3627 if (!node) {
3628 return wr;
3629 }
3630
3631 for (parent = node->parent; parent; parent = parent->parent) {
3632 wr = trp_wrapper_set_shift(wr);
3633 }
3634
3635 return wr;
3636}
3637
3638/**
3639 * @brief Print all parent nodes of @p node and the @p node itself.
3640 *
3641 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
3642 *
3643 * @param[in] node on which the function is focused.
aPiecek01598c02021-04-23 14:18:24 +02003644 * @param[in] pc is @ref TRP_trp settings.
aPiecek153b00f2021-04-20 13:52:57 +02003645 * @param[in,out] tc is context of tree printer.
3646 * @return wrapper for @p node.
3647 */
3648static void
3649trb_print_parents(const struct lysc_node *node, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3650{
3651 struct trt_wrapper wr;
aPiecek9bdd7592021-05-20 08:13:20 +02003652 struct trt_node print_node;
aPiecek153b00f2021-04-20 13:52:57 +02003653
3654 assert(pc && tc && tc->section == TRD_SECT_MODULE);
3655
3656 /* stop recursion */
3657 if (!node) {
3658 return;
3659 }
3660 trb_print_parents(node->parent, pc, tc);
3661
3662 /* setup for printing */
3663 tc->cn = node;
3664 wr = trb_count_depth(node);
3665
3666 /* print node */
3667 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003668 print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
3669 trb_print_entire_node(print_node, 0, wr, pc, tc);
aPiecek153b00f2021-04-20 13:52:57 +02003670}
3671
3672/**
aPiecekdc8fd572021-04-19 10:47:23 +02003673 * @brief Get address of the current node.
3674 * @param[in] tc contains current node.
aPiecek3f247652021-04-19 13:40:25 +02003675 * @return Address of lysc_node or lysp_node, or NULL.
aPiecekdc8fd572021-04-19 10:47:23 +02003676 */
3677static const void *
3678trb_tree_ctx_get_node(struct trt_tree_ctx *tc)
3679{
aPiecek3f247652021-04-19 13:40:25 +02003680 return tc->lysc_tree ?
3681 (const void *)tc->cn :
3682 (const void *)tc->pn;
aPiecekdc8fd572021-04-19 10:47:23 +02003683}
3684
3685/**
3686 * @brief Get address of current node's child.
3687 * @param[in,out] tc contains current node.
3688 */
3689static const void *
3690trb_tree_ctx_get_child(struct trt_tree_ctx *tc)
3691{
3692 if (!trb_tree_ctx_get_node(tc)) {
3693 return NULL;
3694 }
3695
aPiecek3f247652021-04-19 13:40:25 +02003696 if (tc->lysc_tree) {
3697 return lysc_node_child(tc->cn);
3698 } else {
3699 return lysp_node_child(tc->pn);
3700 }
aPiecekdc8fd572021-04-19 10:47:23 +02003701}
3702
3703/**
3704 * @brief Set current node on its child.
3705 * @param[in,out] tc contains current node.
3706 */
3707static void
3708trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
3709{
aPiecek3f247652021-04-19 13:40:25 +02003710 const void *node = trb_tree_ctx_get_child(tc);
3711
3712 if (tc->lysc_tree) {
3713 tc->cn = node;
3714 } else {
3715 tc->pn = node;
3716 }
aPiecekdc8fd572021-04-19 10:47:23 +02003717}
3718
3719/**
aPiecek61d062b2020-11-02 11:05:09 +01003720 * @brief Print subtree of nodes.
3721 *
3722 * The current node is expected to be the root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003723 * Before root node is no linebreak printing. This must be addressed by
3724 * the caller. Root node will also be printed. Behind last printed node
3725 * is no linebreak.
aPiecek61d062b2020-11-02 11:05:09 +01003726 *
aPiecek9bdd7592021-05-20 08:13:20 +02003727 * @param[in] node is root of the subtree.
aPiecek874ea4d2021-04-19 12:26:36 +02003728 * @param[in] max_gap_before_type is result from
aPiecek01598c02021-04-23 14:18:24 +02003729 * ::trb_try_unified_indent() function for root node.
3730 * Set parameter to 0 if distance does not matter.
aPiecek874ea4d2021-04-19 12:26:36 +02003731 * @param[in] wr is wrapper saying how deep in the whole tree
3732 * is the root of the subtree.
3733 * @param[in] ca is parent_cache from root's parent.
3734 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
aPiecek01598c02021-04-23 14:18:24 +02003735 * @param[in] pc is @ref TRP_trp settings.
aPiecek874ea4d2021-04-19 12:26:36 +02003736 * @param[in,out] tc is context of tree printer.
aPiecek61d062b2020-11-02 11:05:09 +01003737 */
3738static void
aPiecek9bdd7592021-05-20 08:13:20 +02003739trb_print_subtree_nodes(struct trt_node node, uint32_t max_gap_before_type, struct trt_wrapper wr,
3740 struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003741{
3742 struct trt_parent_cache new_ca;
aPiecek61d062b2020-11-02 11:05:09 +01003743
aPiecek9bdd7592021-05-20 08:13:20 +02003744 trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003745 /* go to the actual node's child */
aPiecek3f247652021-04-19 13:40:25 +02003746 new_ca = tro_parent_cache_for_child(ca, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003747 node = pc->fp.modify.next_child(ca, tc);
3748
3749 if (!trp_node_is_empty(node)) {
3750 /* print root's nodes */
3751 trb_print_nodes(wr, new_ca, pc, tc);
3752 /* get back from child node to actual node */
3753 pc->fp.modify.parent(tc);
3754 }
3755}
3756
3757/**
3758 * @brief Get number of siblings.
3759 *
3760 * Side-effect -> current node is set to the first sibling.
3761 *
3762 * @param[in] fp contains callback functions which modify tree context
3763 * @param[in,out] tc is the tree context.
3764 * @return Number of siblings of the current node.
3765 */
3766static uint32_t
3767trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
3768{
3769 uint32_t ret = 1;
3770 struct trt_node node = TRP_EMPTY_NODE;
3771
3772 /* including actual node */
3773 fp.first_sibling(tc);
3774 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
3775 ret++;
3776 }
3777 fp.first_sibling(tc);
3778 return ret;
3779}
3780
3781/**
3782 * @brief Print all parents and their children.
3783 *
aPiecek874ea4d2021-04-19 12:26:36 +02003784 * This function is suitable for printing top-level nodes that
aPiecek01598c02021-04-23 14:18:24 +02003785 * do not have ancestors. Function call ::trb_print_subtree_nodes()
aPiecek153b00f2021-04-20 13:52:57 +02003786 * for all top-level siblings. Use this function after 'module' keyword
3787 * or 'augment' and so. The nodes may not be exactly top-level in the
3788 * tree, but the function considers them that way.
aPiecek61d062b2020-11-02 11:05:09 +01003789 *
aPiecek153b00f2021-04-20 13:52:57 +02003790 * @param[in] wr is wrapper saying how deeply the top-level nodes are
3791 * immersed in the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003792 * @param[pc] pc contains mainly functions for printing.
3793 * @param[in,out] tc is tree context.
3794 */
3795static void
aPiecek153b00f2021-04-20 13:52:57 +02003796trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecek61d062b2020-11-02 11:05:09 +01003797{
aPiecek61d062b2020-11-02 11:05:09 +01003798 struct trt_parent_cache ca;
aPiecek9bdd7592021-05-20 08:13:20 +02003799 struct trt_node node;
aPiecek61d062b2020-11-02 11:05:09 +01003800 uint32_t total_parents;
3801 uint32_t max_gap_before_type;
3802
aPiecekdc8fd572021-04-19 10:47:23 +02003803 if (!trb_tree_ctx_get_node(tc)) {
3804 return;
3805 }
3806
aPiecek61d062b2020-11-02 11:05:09 +01003807 ca = TRP_EMPTY_PARENT_CACHE;
3808 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
3809 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
3810
aPiecek3f247652021-04-19 13:40:25 +02003811 if (!tc->lysc_tree) {
aPiecek96baa7f2021-04-23 12:32:00 +02003812 if (((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) ||
3813 (tc->section == TRD_SECT_YANG_DATA)) {
aPiecek3f247652021-04-19 13:40:25 +02003814 ca.lys_config = 0x0;
3815 }
aPiecekdc8fd572021-04-19 10:47:23 +02003816 }
3817
aPiecek61d062b2020-11-02 11:05:09 +01003818 for (uint32_t i = 0; i < total_parents; i++) {
3819 ly_print_(pc->out, "\n");
aPiecek9bdd7592021-05-20 08:13:20 +02003820 node = pc->fp.read.node(ca, tc);
3821 trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
aPiecek61d062b2020-11-02 11:05:09 +01003822 pc->fp.modify.next_sibling(ca, tc);
3823 }
3824}
3825
aPiecek874ea4d2021-04-19 12:26:36 +02003826/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01003827 * Definition of trm main functions
aPiecek874ea4d2021-04-19 12:26:36 +02003828 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01003829
3830/**
aPiecekdc8fd572021-04-19 10:47:23 +02003831 * @brief Settings if lysp_node are used for browsing through the tree.
aPiecek61d062b2020-11-02 11:05:09 +01003832 *
aPiecekdc8fd572021-04-19 10:47:23 +02003833 * @param[in] module YANG schema tree structure representing
3834 * YANG module.
aPiecek61d062b2020-11-02 11:05:09 +01003835 * @param[in] out is output handler.
aPiecekdc8fd572021-04-19 10:47:23 +02003836 * @param[in] max_line_length is the maximum line length limit
3837 * that should not be exceeded.
3838 * @param[in,out] pc will be adapted to lysp_tree.
3839 * @param[in,out] tc will be adapted to lysp_tree.
aPiecek61d062b2020-11-02 11:05:09 +01003840 */
3841static void
aPiecekdc8fd572021-04-19 10:47:23 +02003842trm_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 +01003843{
aPiecekdc8fd572021-04-19 10:47:23 +02003844 *tc = (struct trt_tree_ctx) {
aPiecek3f247652021-04-19 13:40:25 +02003845 .lysc_tree = 0,
aPiecekdc8fd572021-04-19 10:47:23 +02003846 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003847 .pmod = module->parsed,
3848 .cmod = NULL,
3849 .pn = module->parsed ? module->parsed->data : NULL,
3850 .tpn = module->parsed ? module->parsed->data : NULL,
aPiecek3f247652021-04-19 13:40:25 +02003851 .cn = NULL
aPiecekdc8fd572021-04-19 10:47:23 +02003852 };
aPiecek61d062b2020-11-02 11:05:09 +01003853
aPiecekdc8fd572021-04-19 10:47:23 +02003854 pc->out = out;
3855
3856 pc->fp.modify = (struct trt_fp_modify_ctx) {
aPiecekef1e58e2021-04-19 13:19:44 +02003857 .parent = trop_modi_parent,
3858 .first_sibling = trop_modi_first_sibling,
3859 .next_sibling = trop_modi_next_sibling,
3860 .next_child = trop_modi_next_child,
aPiecek61d062b2020-11-02 11:05:09 +01003861 };
3862
aPiecekdc8fd572021-04-19 10:47:23 +02003863 pc->fp.read = (struct trt_fp_read) {
aPiecek61d062b2020-11-02 11:05:09 +01003864 .module_name = tro_read_module_name,
aPiecekef1e58e2021-04-19 13:19:44 +02003865 .node = trop_read_node,
3866 .if_sibling_exists = trop_read_if_sibling_exists
aPiecek61d062b2020-11-02 11:05:09 +01003867 };
3868
aPiecekdc8fd572021-04-19 10:47:23 +02003869 pc->fp.print = (struct trt_fp_print) {
aPiecek3f247652021-04-19 13:40:25 +02003870 .print_features_names = tro_print_features_names,
3871 .print_keys = tro_print_keys
aPiecek61d062b2020-11-02 11:05:09 +01003872 };
3873
aPiecekdc8fd572021-04-19 10:47:23 +02003874 pc->max_line_length = max_line_length;
aPiecek61d062b2020-11-02 11:05:09 +01003875}
3876
3877/**
aPiecek3f247652021-04-19 13:40:25 +02003878 * @brief Settings if lysc_node are used for browsing through the tree.
3879 *
3880 * Pointers to current nodes will be set to module data.
3881 *
3882 * @param[in] module YANG schema tree structure representing
3883 * YANG module.
3884 * @param[in] out is output handler.
3885 * @param[in] max_line_length is the maximum line length limit
3886 * that should not be exceeded.
3887 * @param[in,out] pc will be adapted to lysc_tree.
3888 * @param[in,out] tc will be adapted to lysc_tree.
3889 */
3890static void
3891trm_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)
3892{
3893 *tc = (struct trt_tree_ctx) {
3894 .lysc_tree = 1,
3895 .section = TRD_SECT_MODULE,
aPiecek9f792e52021-04-21 08:33:56 +02003896 .pmod = module->parsed,
3897 .cmod = module->compiled,
aPiecek3f247652021-04-19 13:40:25 +02003898 .tpn = NULL,
3899 .pn = NULL,
3900 .cn = module->compiled->data
3901 };
3902
3903 pc->out = out;
3904
3905 pc->fp.modify = (struct trt_fp_modify_ctx) {
3906 .parent = troc_modi_parent,
3907 .first_sibling = troc_modi_first_sibling,
3908 .next_sibling = troc_modi_next_sibling,
3909 .next_child = troc_modi_next_child,
aPiecek3f247652021-04-19 13:40:25 +02003910 };
3911
3912 pc->fp.read = (struct trt_fp_read) {
3913 .module_name = tro_read_module_name,
3914 .node = troc_read_node,
3915 .if_sibling_exists = troc_read_if_sibling_exists
3916 };
3917
3918 pc->fp.print = (struct trt_fp_print) {
3919 .print_features_names = tro_print_features_names,
3920 .print_keys = tro_print_keys
3921 };
3922
3923 pc->max_line_length = max_line_length;
3924}
3925
3926/**
3927 * @brief Reset settings to browsing through the lysc tree.
aPiecek01598c02021-04-23 14:18:24 +02003928 * @param[in,out] pc resets to @ref TRP_troc functions.
aPiecek3f247652021-04-19 13:40:25 +02003929 * @param[in,out] tc resets to lysc browsing.
3930 */
3931static void
3932trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3933{
aPiecek9f792e52021-04-21 08:33:56 +02003934 trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003935}
3936
3937/**
3938 * @brief Reset settings to browsing through the lysp tree.
aPiecek01598c02021-04-23 14:18:24 +02003939 * @param[in,out] pc resets to @ref TRP_trop functions.
aPiecek3f247652021-04-19 13:40:25 +02003940 * @param[in,out] tc resets to lysp browsing.
3941 */
3942static void
3943trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
3944{
aPiecek9f792e52021-04-21 08:33:56 +02003945 trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
aPiecek3f247652021-04-19 13:40:25 +02003946}
3947
3948/**
3949 * @brief If augment's target node is located on the current module.
3950 * @param[in] pn is examined augment.
3951 * @param[in] pmod is current module.
3952 * @return 1 if nodeid refers to the local node, otherwise 0.
3953 */
3954static ly_bool
3955trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
3956{
3957 const char *id, *prefix, *name;
3958 size_t prefix_len, name_len;
3959 const struct lys_module *mod;
3960 ly_bool ret = 0;
3961
3962 if (pn == NULL) {
3963 return ret;
3964 }
3965
3966 id = pn->nodeid;
3967 if (!id) {
3968 return ret;
3969 }
3970 /* only absolute-schema-nodeid is taken into account */
3971 assert(id[0] == '/');
3972 ++id;
3973
3974 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3975 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003976 mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
aPiecek3f247652021-04-19 13:40:25 +02003977 ret = mod->parsed == pmod;
3978 } else {
3979 ret = 1;
3980 }
3981
3982 return ret;
3983}
3984
3985/**
aPiecek96baa7f2021-04-23 12:32:00 +02003986 * @brief Printing section module, rpcs, notifications or yang-data.
aPiecek61d062b2020-11-02 11:05:09 +01003987 *
aPiecekdc8fd572021-04-19 10:47:23 +02003988 * First node must be the first child of 'module',
aPiecek96baa7f2021-04-23 12:32:00 +02003989 * 'rpcs', 'notifications' or 'yang-data'.
aPiecek61d062b2020-11-02 11:05:09 +01003990 *
aPiecekdc8fd572021-04-19 10:47:23 +02003991 * @param[in] ks is section representation.
3992 * @param[in] pc contains mainly functions for printing.
3993 * @param[in,out] tc is the tree context.
aPiecek61d062b2020-11-02 11:05:09 +01003994 */
3995static void
aPiecekdc8fd572021-04-19 10:47:23 +02003996trm_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 +01003997{
aPiecekdc8fd572021-04-19 10:47:23 +02003998 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
3999 return;
4000 }
4001
4002 trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out);
4003 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
aPiecek153b00f2021-04-20 13:52:57 +02004004 trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004005 } else {
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/**
aPiecek96baa7f2021-04-23 12:32:00 +02004011 * @brief Printing section augment or grouping.
aPiecekdc8fd572021-04-19 10:47:23 +02004012 *
aPiecek96baa7f2021-04-23 12:32:00 +02004013 * First node is 'augment' or 'grouping' itself.
aPiecekdc8fd572021-04-19 10:47:23 +02004014 *
4015 * @param[in] ks is section representation.
4016 * @param[in] pc contains mainly functions for printing.
4017 * @param[in,out] tc is the tree context.
4018 */
4019static void
4020trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4021{
4022 ly_bool grp_has_data = 0;
4023
4024 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4025 return;
4026 }
4027
4028 if (ks.type == TRD_KEYWORD_GROUPING) {
4029 grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0;
4030 }
4031
4032 trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out);
4033 trb_tree_ctx_set_child(tc);
aPiecek153b00f2021-04-20 13:52:57 +02004034 trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004035}
4036
4037/**
4038 * @brief Print 'module' keyword, its name and all nodes.
4039 * @param[in] pc contains mainly functions for printing.
4040 * @param[in,out] tc is the tree context.
4041 */
4042static void
4043trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4044{
4045 trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
4046}
4047
4048/**
4049 * @brief For all augment sections: print 'augment' keyword,
4050 * its target node and all nodes.
4051 * @param[in] pc contains mainly functions for printing.
4052 * @param[in,out] tc is the tree context.
4053 */
4054static void
4055trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4056{
4057 ly_bool once;
aPiecek3f247652021-04-19 13:40:25 +02004058 ly_bool origin_was_lysc_tree = 0;
aPiecekdc8fd572021-04-19 10:47:23 +02004059
aPiecek3f247652021-04-19 13:40:25 +02004060 if (tc->lysc_tree) {
4061 origin_was_lysc_tree = 1;
4062 trm_reset_to_lysp_tree_ctx(pc, tc);
4063 }
4064
aPiecekdc8fd572021-04-19 10:47:23 +02004065 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004066 for (struct trt_keyword_stmt ks = trop_modi_next_augment(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004067 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004068 ks = trop_modi_next_augment(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004069
aPiecek3f247652021-04-19 13:40:25 +02004070 if (origin_was_lysc_tree) {
4071 /* if lysc tree is used, then only augments targeting
4072 * another module are printed
4073 */
aPiecek9f792e52021-04-21 08:33:56 +02004074 if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
aPiecek3f247652021-04-19 13:40:25 +02004075 continue;
4076 }
4077 }
4078
aPiecekdc8fd572021-04-19 10:47:23 +02004079 if (once) {
4080 ly_print_(pc->out, "\n");
4081 ly_print_(pc->out, "\n");
4082 once = 0;
4083 } else {
4084 ly_print_(pc->out, "\n");
4085 }
4086
4087 trm_print_section_as_subtree(ks, pc, tc);
4088 }
aPiecek3f247652021-04-19 13:40:25 +02004089
4090 if (origin_was_lysc_tree) {
4091 trm_reset_to_lysc_tree_ctx(pc, tc);
4092 }
aPiecekdc8fd572021-04-19 10:47:23 +02004093}
4094
4095/**
4096 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
4097 * @param[in] pc contains mainly functions for printing.
4098 * @param[in,out] tc is the tree context.
4099 */
4100static void
4101trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4102{
4103 struct trt_keyword_stmt rpc;
4104
aPiecek01598c02021-04-23 14:18:24 +02004105 rpc = tro_modi_get_rpcs(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004106
4107 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
4108 ly_print_(pc->out, "\n");
4109 ly_print_(pc->out, "\n");
4110 trm_print_section_as_family_tree(rpc, pc, tc);
4111 }
4112}
4113
4114/**
4115 * @brief For notifications section: print 'notifications' keyword
4116 * and all its nodes.
4117 * @param[in] pc contains mainly functions for printing.
4118 * @param[in,out] tc is the tree context.
4119 */
4120static void
4121trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4122{
4123 struct trt_keyword_stmt notifs;
4124
aPiecek01598c02021-04-23 14:18:24 +02004125 notifs = tro_modi_get_notifications(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004126
4127 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
4128 ly_print_(pc->out, "\n");
4129 ly_print_(pc->out, "\n");
4130 trm_print_section_as_family_tree(notifs, pc, tc);
4131 }
4132}
4133
4134/**
4135 * @brief For all grouping sections: print 'grouping' keyword, its name
4136 * and all nodes.
4137 * @param[in] pc contains mainly functions for printing.
4138 * @param[in,out] tc is the tree context.
4139 */
4140static void
4141trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4142{
4143 ly_bool once;
4144
aPiecek01598c02021-04-23 14:18:24 +02004145 if (tc->lysc_tree) {
aPiecekdc8fd572021-04-19 10:47:23 +02004146 return;
4147 }
4148
4149 once = 1;
aPiecek01598c02021-04-23 14:18:24 +02004150 for (struct trt_keyword_stmt ks = trop_modi_next_grouping(tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004151 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
aPiecek01598c02021-04-23 14:18:24 +02004152 ks = trop_modi_next_grouping(tc)) {
aPiecekdc8fd572021-04-19 10:47:23 +02004153 if (once) {
4154 ly_print_(pc->out, "\n");
4155 ly_print_(pc->out, "\n");
4156 once = 0;
4157 } else {
4158 ly_print_(pc->out, "\n");
4159 }
4160 trm_print_section_as_subtree(ks, pc, tc);
4161 }
4162}
4163
4164/**
4165 * @brief For all yang-data sections: print 'yang-data' keyword
4166 * and all its nodes.
4167 * @param[in] pc contains mainly functions for printing.
4168 * @param[in,out] tc is the tree context.
4169 */
4170static void
aPiecek96baa7f2021-04-23 12:32:00 +02004171trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
aPiecekdc8fd572021-04-19 10:47:23 +02004172{
aPiecek96baa7f2021-04-23 12:32:00 +02004173 ly_bool once;
4174 LY_ARRAY_COUNT_TYPE count;
4175
4176 count = LY_ARRAY_COUNT(tc->pmod->exts);
4177 if (count == 0) {
4178 return;
4179 }
4180
4181 once = 1;
4182 for (LY_ARRAY_COUNT_TYPE u = 0; u < count; ++u) {
4183 struct trt_keyword_stmt ks;
4184
aPiecek01598c02021-04-23 14:18:24 +02004185 /* Only ::lys_compile_extension_instance() can set item
aPiecek96baa7f2021-04-23 12:32:00 +02004186 * ::lysp_ext_instance.parsed.
4187 */
4188 if (!tc->pmod->exts[u].parsed) {
4189 /* print at least the yang-data names */
4190 trop_yang_data_sections(tc->pmod->exts, pc->max_line_length, pc->out);
4191 continue;
4192 }
4193
4194 ks = tro_modi_next_yang_data(tc, u);
4195 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
4196 break;
4197 }
4198
4199 if (once) {
4200 ly_print_(pc->out, "\n");
4201 ly_print_(pc->out, "\n");
4202 once = 0;
4203 } else {
4204 ly_print_(pc->out, "\n");
4205 }
4206
4207 trm_print_section_as_family_tree(ks, pc, tc);
4208 }
aPiecekdc8fd572021-04-19 10:47:23 +02004209}
4210
4211/**
4212 * @brief Print sections module, augment, rpcs, notifications,
4213 * grouping, yang-data.
4214 * @param[in] pc contains mainly functions for printing.
4215 * @param[in,out] tc is the tree context.
4216 */
4217static void
4218trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
4219{
4220 trm_print_module_section(pc, tc);
4221 trm_print_augmentations(pc, tc);
4222 trm_print_rpcs(pc, tc);
4223 trm_print_notifications(pc, tc);
4224 trm_print_groupings(pc, tc);
4225 trm_print_yang_data(pc, tc);
4226 ly_print_(pc->out, "\n");
aPiecek61d062b2020-11-02 11:05:09 +01004227}
4228
aPiecek874ea4d2021-04-19 12:26:36 +02004229/**********************************************************************
aPiecek61d062b2020-11-02 11:05:09 +01004230 * Definition of module interface
aPiecek874ea4d2021-04-19 12:26:36 +02004231 *********************************************************************/
aPiecek61d062b2020-11-02 11:05:09 +01004232
4233LY_ERR
aPiecek3f247652021-04-19 13:40:25 +02004234tree_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 +01004235{
4236 struct trt_printer_ctx pc;
4237 struct trt_tree_ctx tc;
4238 struct ly_out *new_out;
4239 LY_ERR erc;
4240 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4241
aPiecekdc8fd572021-04-19 10:47:23 +02004242 LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);
4243
aPiecek61d062b2020-11-02 11:05:09 +01004244 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4245 return erc;
4246 }
4247
4248 line_length = line_length == 0 ? SIZE_MAX : line_length;
aPiecek3f247652021-04-19 13:40:25 +02004249 if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
4250 trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
4251 } else {
4252 trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
4253 }
aPiecek61d062b2020-11-02 11:05:09 +01004254
4255 trm_print_sections(&pc, &tc);
aPiecekdc8fd572021-04-19 10:47:23 +02004256 erc = clb_arg.last_error;
aPiecek61d062b2020-11-02 11:05:09 +01004257
4258 ly_out_free(new_out, NULL, 1);
4259
aPiecekdc8fd572021-04-19 10:47:23 +02004260 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004261}
4262
4263LY_ERR
aPiecek153b00f2021-04-20 13:52:57 +02004264tree_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 +01004265{
aPiecek153b00f2021-04-20 13:52:57 +02004266 struct trt_printer_ctx pc;
4267 struct trt_tree_ctx tc;
4268 struct ly_out *new_out;
4269 struct trt_wrapper wr;
4270 LY_ERR erc;
4271 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4272
4273 assert(out && node);
4274
4275 if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
4276 return LY_EINVAL;
4277 }
4278
4279 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4280 return erc;
4281 }
4282
4283 line_length = line_length == 0 ? SIZE_MAX : line_length;
4284 trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);
4285
4286 trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, 0, pc.out);
4287 trb_print_parents(node, &pc, &tc);
4288
4289 if (!(options & LYS_PRINT_NO_SUBSTMT)) {
4290 tc.cn = lysc_node_child(node);
4291 wr = trb_count_depth(tc.cn);
4292 trb_print_family_tree(wr, &pc, &tc);
4293 }
4294 ly_print_(out, "\n");
4295
4296 erc = clb_arg.last_error;
4297 ly_out_free(new_out, NULL, 1);
4298
4299 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004300}
4301
4302LY_ERR
Michal Vasko6a914fb2022-01-17 10:36:14 +01004303tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
4304 size_t line_length)
aPiecek61d062b2020-11-02 11:05:09 +01004305{
aPiecek9f792e52021-04-21 08:33:56 +02004306 struct trt_printer_ctx pc;
4307 struct trt_tree_ctx tc;
4308 struct ly_out *new_out;
4309 LY_ERR erc;
4310 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
4311
4312 assert(submodp);
4313 LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);
4314
4315 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
4316 return erc;
4317 }
4318
4319 line_length = line_length == 0 ? SIZE_MAX : line_length;
4320 trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
4321 tc.pmod = (struct lysp_module *)submodp;
4322 tc.tpn = submodp->data;
4323 tc.pn = tc.tpn;
4324
4325 trm_print_sections(&pc, &tc);
4326 erc = clb_arg.last_error;
4327
4328 ly_out_free(new_out, NULL, 1);
4329
4330 return erc;
aPiecek61d062b2020-11-02 11:05:09 +01004331}