blob: 7062d583b3d54cb0626c23eb662f052522ac6652 [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
13 */
14
15#include <assert.h>
16#include <string.h>
17
18#include "compat.h"
19#include "out_internal.h"
20#include "printer_internal.h"
21#include "xpath.h"
22
23/******************************************************************************
24 * Declarations start
25 *****************************************************************************/
26
27/*
28 * +---------+ +---------+ +---------+
29 * output | trp | | trb | | tro |
30 * <---+ Print +<---+ Browse +<-->+ Obtain |
31 * | | | | | |
32 * +---------+ +----+----+ +---------+
33 * ^
34 * |
35 * +----+----+
36 * | trm |
37 * | Manager |
38 * | |
39 * +----+----+
40 * ^
41 * | input
42 * +
43 *
44 * Glossary:
45 * trt - type
46 * trp - functions for Printing
47 * trb - functions for Browse the tree
48 * tro - functions for Obtaining information from libyang
49 * trm - Main functions, Manager
50 * trg - General functions
51 *
52 * - Manager functions (trm) are able to print individual sections of the YANG tree diagram
53 * (eg module, notifications, rpcs ...) and they call Browse functions (trb).
54 * - Browse functions contain a general algorithm (Preorder DFS) for traversing the tree.
55 * They call the Obtain functions (tro) to get information about the node
56 * or eg to get a sibling or child for the current node.
57 * This obtained information is passed on to the Print functions (trp) for printing.
58 * Gap offsets before the node type are also calculated in the Browse functions.
59 * - Print functions (trp) take care of the printing itself.
60 * They can also split one node into multiple lines if the node does not fit on one line.
61 *
62 * For future adjustments:
63 * it is assumed that the changes are likely to take place mainly for tro functions
64 * because they are the only ones dependent on libyang implementation.
65 * In special cases, changes will also need to be made to the trp functions
66 * if a special algorithm is needed to print (right now this is prepared for printing list's keys
67 * and if-features).
68 */
69
70/**
71 * @brief List of available actions.
72 */
73typedef enum {
74 TRD_PRINT = 0,
75 TRD_CHAR_COUNT
76} trt_ly_out_clb_arg_flag;
77
78/**
79 * @brief Specific argument to be passed to the ly_write_clb.
80 */
81struct ly_out_clb_arg {
82 trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */
83 struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */
84 size_t counter; /**< Counter of printed characters. */
85 LY_ERR last_error; /**< The last error that occurred. If no error has occurred, it will be LY_SUCCESS. */
86};
87
88/**
89 * @brief Initialize struct ly_out_clb_arg with default settings.
90 */
91#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
92 (struct ly_out_clb_arg){.mode = MODE, .out = OUT, .counter = COUNTER, .last_error = LAST_ERROR}
93
94/******************************************************************************
95 * Print getters
96 *****************************************************************************/
97
98/**
99 * @brief Callback functions that prints special cases.
100 *
101 * It just groups together tree context with trt_fp_print.
102 */
103struct trt_cf_print {
104 const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */
105 void (*pf)(const struct trt_tree_ctx *, struct ly_out *); /**< Pointing to function which printing list's keys or features. */
106};
107
108/**
109 * @brief Callback functions for printing special cases.
110 *
111 * Functions with the suffix 'trp' can print most of the text on output, just by setting the pointer to the string.
112 * But in some cases, it's not that simple, because its entire string is fragmented in memory.
113 * For example, for printing list's keys or if-features.
114 * However, this depends on how the libyang library is implemented.
115 * This implementation of the printer_tree module goes through a lysp tree, but if it goes through a lysc tree,
116 * these special cases would be different.
117 * Functions must print including spaces or delimiters between names.
118 */
119struct trt_fp_print {
120 void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */
121 void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */
122};
123
124/**
125 * @brief Package which only groups getter function.
126 */
127struct trt_pck_print {
128 const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */
129 struct trt_fp_print fps; /**< Print function. */
130};
131
132/**
133 * @brief Initialize struct trt_pck_print by parameters.
134 */
135#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
136 (struct trt_pck_print){.tree_ctx = TREE_CTX, .fps = FP_PRINT}
137
138/******************************************************************************
139 * Indent
140 *****************************************************************************/
141
142/**
143 * @brief Constants which are defined in the RFC or are observable from the pyang tool.
144 */
145typedef enum {
146 TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */
147 TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with a whitespace offset of at least two characters. */
148 TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */
149 TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */
150 TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */
151 TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */
152 TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */
153} trt_cnf_indent;
154
155/**
156 * @brief Type of indent in node.
157 */
158typedef enum {
159 TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */
160 TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */
161 TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
162} trt_indent_in_node_type;
163
164/** Constant to indicate the need to break a line. */
165#define TRD_LINEBREAK -1
166
167/**
168 * @brief Records the alignment between the individual elements of the node.
169 *
170 * See trp_indent_in_node_are_eq, trp_indent_in_node_place_break.
171 */
172struct trt_indent_in_node {
173 trt_indent_in_node_type type; /**< Type of indent in node. */
174 int16_t btw_name_opts; /**< Indent between node name and opts. */
175 int16_t btw_opts_type; /**< Indent between opts and type. */
176 int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */
177};
178
179/**
180 * @brief Type of wrappers to be printed.
181 */
182typedef enum {
183 TRD_WRAPPER_TOP = 0, /**< Related to the module. */
184 TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */
185} trd_wrapper_type;
186
187/**
188 * @brief For resolving sibling symbol ('|') placement.
189 *
190 * Bit indicates where the sibling symbol must be printed.
191 * This place is in multiples of TRD_INDENT_BTW_SIBLINGS.
192 *
193 * See: TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY, trp_wrapper_set_mark,
194 * trp_wrapper_set_shift, trp_wrapper_if_last_sibling, trp_wrapper_eq,
195 * trp_print_wrapper
196 */
197struct trt_wrapper {
198 trd_wrapper_type type; /**< Location of the wrapper. */
199 uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed.
200 It follows that the maximum immersion of the printable node is 64. */
201 uint32_t actual_pos; /**< Actual position in bit_marks. */
202};
203
204/**
205 * @brief Get wrapper related to the module section.
206 *
207 * @code
208 * module: <module-name>
209 * +--<node>
210 * |
211 * @endcode
212 */
213#define TRP_INIT_WRAPPER_TOP \
214 (struct trt_wrapper) {.type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0}
215
216/**
217 * @brief Get wrapper related to subsection e.g. Augmenations or Groupings.
218 *
219 * @code
220 * module: <module-name>
221 * +--<node>
222 *
223 * augment <target-node>:
224 * +--<node>
225 * @endcode
226 */
227#define TRP_INIT_WRAPPER_BODY \
228 (struct trt_wrapper) {.type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0}
229
230/**
231 * @brief Package which only groups wrapper and indent in node.
232 */
233struct trt_pck_indent {
234 struct trt_wrapper wrapper; /**< Coded " | | " sequence. */
235 struct trt_indent_in_node in_node; /**< Indent in node. */
236};
237
238/**
239 * @brief Initialize struct trt_pck_indent by parameters.
240 */
241#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
242 (struct trt_pck_indent){.wrapper = WRAPPER, .in_node = INDENT_IN_NODE}
243
244/******************************************************************************
245 * status
246 *****************************************************************************/
247
248/**
249 * @brief Status of the node.
250 *
251 * See: trp_print_status
252 */
253typedef enum {
254 TRD_STATUS_TYPE_EMPTY = 0,
255 TRD_STATUS_TYPE_CURRENT,
256 TRD_STATUS_TYPE_DEPRECATED,
257 TRD_STATUS_TYPE_OBSOLETE
258} trt_status_type;
259
260/******************************************************************************
261 * flags
262 *****************************************************************************/
263
264/**
265 * @brief Flag of the node.
266 *
267 * See: trp_print_flags, trp_get_flags_strlen
268 */
269typedef enum {
270 TRD_FLAGS_TYPE_EMPTY = 0,
271 TRD_FLAGS_TYPE_RW, /**< rw */
272 TRD_FLAGS_TYPE_RO, /**< ro */
273 TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */
274 TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */
275 TRD_FLAGS_TYPE_RPC, /**< -x */
276 TRD_FLAGS_TYPE_NOTIF, /**< -n */
277 TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */
278} trt_flags_type;
279
280/******************************************************************************
281 * node_name and opts
282 *****************************************************************************/
283
284#define TRD_NODE_NAME_PREFIX_CHOICE "("
285#define TRD_NODE_NAME_PREFIX_CASE ":("
286#define TRD_NODE_NAME_TRIPLE_DOT "..."
287
288/**
289 * @brief Type of the node.
290 *
291 * Used mainly to complete the correct \<opts\> next to or around the \<name\>.
292 */
293typedef enum {
294 TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */
295 TRD_NODE_CASE, /**< For case node. :(\<name\>) */
296 TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */
297 TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */
298 TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */
299 TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */
300 TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list (without keys). \<name\>* */
301 TRD_NODE_KEYS, /**< For a list's keys. \<name\>* [\<keys\>] */
302 TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */
303 TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */
304 TRD_NODE_TRIPLE_DOT /**< For collapsed sibling nodes and their children. Special case which doesn't belong here very well. */
305} trt_node_type;
306
307/**
308 * @brief Type of node and his name.
309 *
310 * See: TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
311 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
312 */
313struct trt_node_name {
314 trt_node_type type; /**< Type of the node relevant for printing. */
315 const char *module_prefix; /**< Prefix defined in the module where the node is defined. */
316 const char *str; /**< Name of the node. */
317};
318
319/**
320 * @brief Create struct trt_node_name as empty.
321 */
322#define TRP_EMPTY_NODE_NAME \
323 (struct trt_node_name){.type = TRD_NODE_ELSE, .module_prefix = NULL, .str = NULL}
324
325/**
326 * @brief Check if struct trt_node_name is empty.
327 */
328#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
329 !NODE_NAME.str
330
331/**< Every opts mark has a length of one. */
332#define TRD_OPTS_MARK_LENGTH 1
333
334/******************************************************************************
335 * type
336 *****************************************************************************/
337
338/**
339 * @brief Type of the \<type\>
340 */
341typedef enum {
342 TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */
343 TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
344 TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm. So set type as TRD_TYPE_TARGET. */
345 TRD_TYPE_EMPTY /**< Type is not used at all. */
346} trt_type_type;
347
348/**
349 * @brief \<type\> in the \<node\>.
350 *
351 * See: TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
352 */
353struct trt_type {
354 trt_type_type type; /**< Type of the \<type\>. */
355 const char *str; /**< Path or name of the type. */
356};
357
358/**
359 * @brief Create empty struct trt_type.
360 */
361#define TRP_EMPTY_TRT_TYPE \
362 (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}
363
364/**
365 * @brief Check if struct trt_type is empty.
366 */
367#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
368 TYPE_OF_TYPE.type == TRD_TYPE_EMPTY
369
370/**
371 * @brief Initialize struct trt_type by parameters.
372 */
373#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
374 (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}
375
376/******************************************************************************
377 * node
378 *****************************************************************************/
379
380/**
381 * @brief \<node\> data for printing.
382 *
383 * It contains RFC's: \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
384 * Item \<opts\> is moved to part struct trt_node_name.
385 * For printing [\<keys\>] and if-features is required special functions which prints them.
386 *
387 * See: TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty, trp_print_node_up_to_name,
388 * trp_print_divided_node_up_to_name, trp_print_node
389 */
390struct trt_node {
391 trt_status_type status; /**< \<status\>. */
392 trt_flags_type flags; /**< \<flags\>. */
393 struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */
394 struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */
395 ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and will be printed by print_features_names callback. */
396 ly_bool last_one; /**< Information about whether the node is the last. */
397};
398
399/**
400 * @brief Create struct trt_node as empty.
401 */
402#define TRP_EMPTY_NODE \
403 (struct trt_node) {.status = TRD_STATUS_TYPE_EMPTY, .flags = TRD_FLAGS_TYPE_EMPTY, \
404 .name = TRP_EMPTY_NODE_NAME, .type = TRP_EMPTY_TRT_TYPE, .iffeatures = 0, .last_one = 1}
405
406/**
407 * @brief Package which only groups indent and node.
408 */
409struct trt_pair_indent_node {
410 struct trt_indent_in_node indent;
411 struct trt_node node;
412};
413
414/**
415 * @brief Initialize struct trt_pair_indent_node by parameters.
416 */
417#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
418 (struct trt_pair_indent_node){.indent = INDENT_IN_NODE, .node = NODE}
419
420/******************************************************************************
421 * statement
422 *****************************************************************************/
423
424#define TRD_TOP_KEYWORD_MODULE "module"
425#define TRD_TOP_KEYWORD_SUBMODULE "submodule"
426
427#define TRD_BODY_KEYWORD_AUGMENT "augment"
428#define TRD_BODY_KEYWORD_RPC "rpcs"
429#define TRD_BODY_KEYWORD_NOTIF "notifications"
430#define TRD_BODY_KEYWORD_GROUPING "grouping"
431#define TRD_BODY_KEYWORD_YANG_DATA "yang-data"
432
433/**
434 * @brief Type of the trt_keyword.
435 */
436typedef enum {
437 TRD_KEYWORD_EMPTY = 0,
438 TRD_KEYWORD_MODULE,
439 TRD_KEYWORD_SUBMODULE,
440 TRD_KEYWORD_AUGMENT,
441 TRD_KEYWORD_RPC,
442 TRD_KEYWORD_NOTIF,
443 TRD_KEYWORD_GROUPING,
444 TRD_KEYWORD_YANG_DATA
445} trt_keyword_type;
446
447/**
448 * @brief Main sign of the tree nodes.
449 *
450 * See: TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
451 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
452 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
453 * trp_keyword_type_strlen
454 *
455 */
456struct trt_keyword_stmt {
457 trt_keyword_type type; /**< String containing some of the top or body keyword. */
458 const char *str; /**< Name or path, it determines the type. */
459};
460
461/**
462 * @brief Create struct trt_keyword_stmt as empty.
463 */
464#define TRP_EMPTY_KEYWORD_STMT \
465 (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL}
466
467/**
468 * @brief Check if struct trt_keyword_stmt is empty.
469 */
470#define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \
471 KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY
472
473/**
474 * @brief Initialize struct trt_keyword_stmt by parameters.
475 */
476#define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \
477 (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING}
478
479/******************************************************************************
480 * Modify getters
481 *****************************************************************************/
482
483struct trt_parent_cache;
484
485/**
486 * @brief Functions that change the state of the tree_ctx structure.
487 *
488 * The 'tro' functions are set here, which provide data for the 'trp' printing functions
489 * and are also called from the 'trb' browsing functions when walking through a tree.
490 * These callback functions need to be checked or reformulated
491 * if changes to the libyang library affect the printing tree.
492 * For all, if the value cannot be returned,
493 * its empty version obtained by relevant TRP_EMPTY macro is returned.
494 */
495struct trt_fp_modify_ctx {
496 ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */
497 void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */
498 struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */
499 struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */
500 struct trt_keyword_stmt (*next_augment)(struct trt_tree_ctx *); /**< Jump to the augment section. */
501 struct trt_keyword_stmt (*get_rpcs)(struct trt_tree_ctx *); /**< Jump to the rpcs section. */
502 struct trt_keyword_stmt (*get_notifications)(struct trt_tree_ctx *); /**< Jump to the notifications section. */
503 struct trt_keyword_stmt (*next_grouping)(struct trt_tree_ctx *); /**< Jump to the grouping section. */
504 struct trt_keyword_stmt (*next_yang_data)(struct trt_tree_ctx *); /**< Jump to the yang-data section. */
505};
506
507/******************************************************************************
508 * Read getters
509 *****************************************************************************/
510
511/**
512 * @brief Functions that do not change the state of the tree_structure.
513 *
514 * For details see trt_fp_modify_ctx.
515 */
516struct trt_fp_read {
517 struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */
518 struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */
519 ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */
520};
521
522/******************************************************************************
523 * All getters
524 *****************************************************************************/
525
526/**
527 * @brief A set of all necessary functions that must be provided for the printer.
528 */
529struct trt_fp_all {
530 struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */
531 struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */
532 struct trt_fp_print print; /**< Functions pointers for printing special items in node. */
533};
534
535/******************************************************************************
536 * Printer context
537 *****************************************************************************/
538
539/**
540 * @brief Main structure for trp component (printer part).
541 */
542struct trt_printer_ctx {
543 struct ly_out *out; /**< Handler to printing. */
544 struct trt_fp_all fp; /**< 'tro' functions callbacks. */
545 size_t max_line_length; /**< The maximum number of characters that can be
546 printed on one line, including the last. */
547};
548
549/******************************************************************************
550 * Tro functions
551 *****************************************************************************/
552
553/**
554 * @brief The name of the section to which the node belongs.
555 */
556typedef enum {
557 TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */
558 TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */
559 TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */
560 TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */
561 TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */
562 TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */
563} trt_actual_section;
564
565/**
566 * @brief Types of nodes that have some effect on their children.
567 */
568typedef enum {
569 TRD_ANCESTOR_ELSE = 0,
570 TRD_ANCESTOR_RPC_INPUT,
571 TRD_ANCESTOR_RPC_OUTPUT,
572 TRD_ANCESTOR_NOTIF
573} trt_ancestor_type;
574
575/**
576 * @brief Saved information when browsing the tree downwards.
577 *
578 * This structure helps prevent frequent retrieval of information from the tree.
579 * Browsing functions (trb) are designed to preserve this structures during their recursive calls.
580 * Browsing functions (trb) do not interfere in any way with this data.
581 * This structure is used by Obtaining functions (tro) which, thanks to this structure, can return a node with the correct data.
582 * The word parent is in the name, because this data refers to the last parent and at the same time the states of its ancestors data.
583 * Only the function jumping on the child (next_child(...)) creates this structure,
584 * because the pointer to the current node moves down the tree.
585 * It's like passing the genetic code to children.
586 * Some data must be inherited and there are two approaches to this problem.
587 * Either it will always be determined which inheritance states belong to the current node
588 * (which can lead to regular travel to the root node) or the inheritance states will be stored during the recursive calls.
589 * So the problem was solved by the second option.
590 * Why does the structure contain this data? Because it walks through the lysp tree.
591 * In the future, this data may change if another type of tree (such as the lysc tree) is traversed.
592 *
593 * See: TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
594 */
595struct trt_parent_cache {
596 trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
597 uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */
598 uint16_t lys_config; /**< Inherited config W or R. */
599 const struct lysp_node_list *last_list; /**< The last LYS_LIST passed. */
600};
601
602/**
603 * @brief Return trt_parent_cache filled with default values.
604 */
605#define TRP_EMPTY_PARENT_CACHE \
606 (struct trt_parent_cache) {.ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
607 .lys_config = LYS_CONFIG_W, .last_list = NULL}
608
609/**
610 * @brief Main structure for browsing the libyang tree
611 */
612struct trt_tree_ctx {
613 trt_actual_section section; /**< To which section pn points. */
614 const struct lys_module *module; /**< Schema tree structures. */
615 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
616 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
617};
618
619/**
620 * @brief Used for updating trt_tree_ctx
621 */
622struct trt_tree_ctx_node_patch {
623 const struct lysp_node *pn; /**< Actual pointer to parsed node. */
624 const struct lysp_node *tpn; /**< Pointer to actual top-node. */
625};
626
627/**
628 * @brief Initialize struct trt_keyword_stmt by parameters.
629 */
630#define TRP_INIT_TREE_CTX_NODE_PATCH(PN, TPN) \
631 (struct trt_tree_ctx_node_patch){.pn = PN, .tpn = TPN}
632
633/** Getter function for tro_lysp_node_charptr function. */
634typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);
635
636/******************************************************************************
637 * Definition of the general Trg functions
638 *****************************************************************************/
639
640/**
641 * @brief Print a substring but limited to the maximum length.
642 * @param[in] str is pointer to source.
643 * @param[in] len is number of characters to be printed.
644 * @param[in,out] out is output handler.
645 * @return str parameter shifted by len.
646 */
647static const char *
648trg_print_substr(const char *str, size_t len, struct ly_out *out)
649{
650 for (size_t i = 0; i < len; i++) {
651 ly_print_(out, "%c", str[0]);
652 str++;
653 }
654 return str;
655}
656
657/**
658 * @brief Pointer is not NULL and does not point to an empty string.
659 * @param[in] str is pointer to string to be checked.
660 * @return 1 if str pointing to non empty string otherwise 0.
661 */
662static ly_bool
663trg_charptr_has_data(const char *str)
664{
665 return (str) && (str[0] != '\0');
666}
667
668/**
669 * @brief Check if 'word' in 'src' is present where words are delimited by 'delim'.
670 * @param[in] src is source where words are separated by delim.
671 * @param[in] word to be searched.
672 * @param[in] delim is delimiter between words in src.
673 * @return 1 if src contains word otherwise 0.
674 */
675static ly_bool
676trg_word_is_present(const char *src, const char *word, char delim)
677{
678 const char *hit;
679
680 if ((!src) || (src[0] == '\0') || (!word)) {
681 return 0;
682 }
683
684 hit = strstr(src, word);
685
686 if (hit) {
687 /* word was founded at the begin of src
688 * OR it match somewhere after delim
689 */
690 if ((hit == src) || (hit[-1] == delim)) {
691 /* end of word was founded at the end of src
692 * OR end of word was match somewhere before delim
693 */
694 char delim_or_end = (hit + strlen(word))[0];
695 if ((delim_or_end == '\0') || (delim_or_end == delim)) {
696 return 1;
697 }
698 }
699 /* after -> hit is just substr and it's not the whole word */
700 /* jump to the next word */
701 for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
702 /* skip delim */
703 src = src[0] == '\0' ? src : src + 1;
704 /* continue with searching */
705 return trg_word_is_present(src, word, delim);
706 } else {
707 return 0;
708 }
709}
710
711/******************************************************************************
712 * Definition of printer functions
713 *****************************************************************************/
714
715/**
716 * @brief Write callback for ly_out_new_clb function.
717 *
718 * @param[in] user_data is type of struct ly_out_clb_arg*.
719 * @param[in] buf contains input characters
720 * @param[in] count is number of characters in buf.
721 * @return Number of printed bytes.
722 * @return Negative value in case of error.
723 */
724static ssize_t
725trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
726{
727 LY_ERR erc = LY_SUCCESS;
728 struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;
729
730 switch (data->mode) {
731 case TRD_PRINT:
732 erc = ly_write_(data->out, buf, count);
733 break;
734 case TRD_CHAR_COUNT:
735 data->counter = data->counter + count;
736 break;
737 default:
738 break;
739 }
740
741 if (erc != LY_SUCCESS) {
742 data->last_error = erc;
743 return -1;
744 } else {
745 return count;
746 }
747}
748
749/**
750 * @brief Check that indent in node can be considered as equivalent.
751 * @param[in] first is the first indent in node.
752 * @param[in] second is the second indent in node.
753 * @return 1 if indents are equivalent otherwise 0.
754 */
755static ly_bool
756trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
757{
758 const ly_bool a = first.type == second.type;
759 const ly_bool b = first.btw_name_opts == second.btw_name_opts;
760 const ly_bool c = first.btw_opts_type == second.btw_opts_type;
761 const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;
762
763 return a && b && c && d;
764}
765
766/**
767 * @brief Setting ' ' symbol because node is last sibling.
768 * @param[in] wr is wrapper over which the shift operation is to be performed.
769 * @return New shifted wrapper.
770 */
771static struct trt_wrapper
772trp_wrapper_set_shift(struct trt_wrapper wr)
773{
774 assert(wr.actual_pos < 64);
775 /* +--<node>
776 * +--<node>
777 */
778 wr.actual_pos++;
779 return wr;
780}
781
782/**
783 * @brief Setting '|' symbol because node is divided or it is not last sibling.
784 * @param[in] wr is source of wrapper.
785 * @return New wrapper which is marked at actual position and shifted.
786 */
787static struct trt_wrapper
788trp_wrapper_set_mark(struct trt_wrapper wr)
789{
790 assert(wr.actual_pos < 64);
791 wr.bit_marks1 |= 1U << wr.actual_pos;
792 return trp_wrapper_set_shift(wr);
793}
794
795/**
796 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
797 * @param[in] wr is actual wrapper.
798 * @param[in] last_one is flag. Value 1 saying if the node is the last and has no more siblings.
799 * @return New wrapper for the actual node.
800 */
801static struct trt_wrapper
802trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
803{
804 return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
805}
806
807/**
808 * @brief Test if the wrappers are equivalent.
809 * @param[in] first is the first wrapper.
810 * @param[in] second is the second wrapper.
811 * @return 1 if the wrappers are equivalent otherwise 0.
812 */
813static ly_bool
814trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
815{
816 const ly_bool a = first.type == second.type;
817 const ly_bool b = first.bit_marks1 == second.bit_marks1;
818 const ly_bool c = first.actual_pos == second.actual_pos;
819
820 return a && b && c;
821}
822
823/**
824 * @brief Print " | " sequence on line.
825 * @param[in] wr is wrapper to be printed.
826 * @param[in,out] out is output handler.
827 */
828static void
829trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
830{
831 uint32_t lb;
832
833 if (wr.type == TRD_WRAPPER_TOP) {
834 lb = TRD_INDENT_LINE_BEGIN;
835 } else if (wr.type == TRD_WRAPPER_BODY) {
836 lb = TRD_INDENT_LINE_BEGIN * 2;
837 } else {
838 lb = TRD_INDENT_LINE_BEGIN;
839 }
840
841 ly_print_(out, "%*c", lb, ' ');
842
843 if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
844 return;
845 }
846
847 for (uint32_t i = 0; i < wr.actual_pos; i++) {
848 /** Test if the bit on the index is set. */
849 if ((wr.bit_marks1 >> i) & 1U) {
850 ly_print_(out, "|");
851 } else {
852 ly_print_(out, " ");
853 }
854
855 if (i != wr.actual_pos) {
856 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
857 }
858 }
859}
860
861/**
862 * @brief Check if struct trt_node is empty.
863 * @param[in] node is item to test.
864 * @return 1 if node is considered empty otherwise 0.
865 */
866static ly_bool
867trp_node_is_empty(struct trt_node node)
868{
869 const ly_bool a = !node.iffeatures;
870 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
871 const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name);
872 const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY;
873 const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY;
874
875 return a && b && c && d && e;
876}
877
878/**
879 * @brief Check if [\<keys\>], \<type\> and \<iffeatures\> are empty/not_set.
880 * @param[in] node is item to test.
881 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\> otherwise 0.
882 */
883static ly_bool
884trp_node_body_is_empty(struct trt_node node)
885{
886 const ly_bool a = !node.iffeatures;
887 const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type);
888 const ly_bool c = node.name.type != TRD_NODE_KEYS;
889
890 return a && b && c;
891}
892
893/**
894 * @brief Print \<status\> of the node.
895 * @param[in] status_type is type of status.
896 * @param[in,out] out is output handler.
897 */
898static void
899trp_print_status(trt_status_type status_type, struct ly_out *out)
900{
901 switch (status_type) {
902 case TRD_STATUS_TYPE_CURRENT:
903 ly_print_(out, "%c", '+');
904 break;
905 case TRD_STATUS_TYPE_DEPRECATED:
906 ly_print_(out, "%c", 'x');
907 break;
908 case TRD_STATUS_TYPE_OBSOLETE:
909 ly_print_(out, "%c", 'o');
910 break;
911 default:
912 break;
913 }
914}
915
916/**
917 * @brief Print \<flags\>.
918 * @param[in] flags_type is type of \<flags\>.
919 * @param[in,out] out is output handler.
920 */
921static void
922trp_print_flags(trt_flags_type flags_type, struct ly_out *out)
923{
924 switch (flags_type) {
925 case TRD_FLAGS_TYPE_RW:
926 ly_print_(out, "%s", "rw");
927 break;
928 case TRD_FLAGS_TYPE_RO:
929 ly_print_(out, "%s", "ro");
930 break;
931 case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS:
932 ly_print_(out, "%s", "-w");
933 break;
934 case TRD_FLAGS_TYPE_USES_OF_GROUPING:
935 ly_print_(out, "%s", "-u");
936 break;
937 case TRD_FLAGS_TYPE_RPC:
938 ly_print_(out, "%s", "-x");
939 break;
940 case TRD_FLAGS_TYPE_NOTIF:
941 ly_print_(out, "%s", "-n");
942 break;
943 case TRD_FLAGS_TYPE_MOUNT_POINT:
944 ly_print_(out, "%s", "mp");
945 break;
946 default:
947 break;
948 }
949}
950
951/**
952 * @brief Get size of the \<flags\>.
953 * @param[in] flags_type is type of \<flags\>.
954 * @return 0 if flags_type is not set otherwise 2.
955 */
956static size_t
957trp_get_flags_strlen(trt_flags_type flags_type)
958{
959 return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2;
960}
961
962/**
963 * @brief Print entire struct trt_node_name structure.
964 * @param[in] node_name is item to print.
965 * @param[in,out] out is output handler.
966 */
967static void
968trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
969{
970 const char *mod_prefix;
971 const char *colon;
972 const char trd_node_name_suffix_choice[] = ")";
973 const char trd_node_name_suffix_case[] = ")";
974 const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */
975 const char trd_opts_container[] = "!"; /**< For a presence container. */
976 const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */
977 const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */
978 const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */
979
980 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
981 return;
982 }
983
984 if (node_name.module_prefix) {
985 mod_prefix = node_name.module_prefix;
986 colon = ":";
987 } else {
988 mod_prefix = "";
989 colon = "";
990 }
991
992 switch (node_name.type) {
993 case TRD_NODE_ELSE:
994 ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
995 break;
996 case TRD_NODE_CASE:
997 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
998 break;
999 case TRD_NODE_CHOICE:
1000 ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
1001 break;
1002 case TRD_NODE_OPTIONAL_CHOICE:
1003 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);
1004 break;
1005 case TRD_NODE_OPTIONAL:
1006 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional);
1007 break;
1008 case TRD_NODE_CONTAINER:
1009 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container);
1010 break;
1011 case TRD_NODE_LISTLEAFLIST:
1012 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1013 break;
1014 case TRD_NODE_KEYS:
1015 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list);
1016 break;
1017 case TRD_NODE_TOP_LEVEL1:
1018 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash);
1019 break;
1020 case TRD_NODE_TOP_LEVEL2:
1021 ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign);
1022 break;
1023 case TRD_NODE_TRIPLE_DOT:
1024 ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
1025 break;
1026 default:
1027 break;
1028 }
1029}
1030
1031/**
1032 * @brief Check if mark (?, !, *, /, @) is implicitly contained in struct trt_node_name.
1033 * @param[in] node_name is structure containing the 'mark'.
1034 * @return 1 if contain otherwise 0.
1035 */
1036static ly_bool
1037trp_mark_is_used(struct trt_node_name node_name)
1038{
1039 if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
1040 return 0;
1041 }
1042
1043 switch (node_name.type) {
1044 case TRD_NODE_ELSE:
1045 case TRD_NODE_CASE:
1046 case TRD_NODE_KEYS:
1047 return 0;
1048 default:
1049 return 1;
1050 }
1051}
1052
1053/**
1054 * @brief Print opts keys.
1055 * @param[in] node_name contains type of the node with his name.
1056 * @param[in] btw_name_opts is number of spaces between name and [keys].
1057 * @param[in] cf is basically a pointer to the function that prints the keys.
1058 * @param[in,out] out is output handler.
1059 */
1060static void
1061trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
1062{
1063 if (node_name.type != TRD_NODE_KEYS) {
1064 return;
1065 }
1066
1067 /* <name><mark>___<keys>*/
1068 if (btw_name_opts > 0) {
1069 ly_print_(out, "%*c", btw_name_opts, ' ');
1070 }
1071 ly_print_(out, "[");
1072 cf.pf(cf.ctx, out);
1073 ly_print_(out, "]");
1074}
1075
1076/**
1077 * @brief Print entire struct trt_type structure.
1078 * @param[in] type is item to print.
1079 * @param[in,out] out is output handler.
1080 */
1081static void
1082trp_print_type(struct trt_type type, struct ly_out *out)
1083{
1084 if (TRP_TRT_TYPE_IS_EMPTY(type)) {
1085 return;
1086 }
1087
1088 switch (type.type) {
1089 case TRD_TYPE_NAME:
1090 ly_print_(out, "%s", type.str);
1091 break;
1092 case TRD_TYPE_TARGET:
1093 ly_print_(out, "-> %s", type.str);
1094 break;
1095 case TRD_TYPE_LEAFREF:
1096 ly_print_(out, "leafref");
1097 default:
1098 break;
1099 }
1100}
1101
1102/**
1103 * @brief Print all iffeatures of node
1104 *
1105 * @param[in] iffeature_flag contains if if-features is present.
1106 * @param[in] cf is basically a pointer to the function that prints the list of features.
1107 * @param[in,out] out is output handler.
1108 */
1109static void
1110trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out)
1111{
1112 if (iffeature_flag) {
1113 ly_print_(out, "{");
1114 cf.pf(cf.ctx, out);
1115 ly_print_(out, "}?");
1116 }
1117}
1118
1119/**
1120 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
1121 * @param[in] node contains items to print.
1122 * @param[in] out is output handler.
1123 */
1124static void
1125trp_print_node_up_to_name(struct trt_node node, struct ly_out *out)
1126{
1127 if (node.name.type == TRD_NODE_TRIPLE_DOT) {
1128 trp_print_node_name(node.name, out);
1129 return;
1130 }
1131 /* <status>--<flags> */
1132 trp_print_status(node.status, out);
1133 ly_print_(out, "--");
1134 /* If the node is a case node, there is no space before the <name> */
1135 /* also case node has no flags. */
1136 if (node.name.type != TRD_NODE_CASE) {
1137 trp_print_flags(node.flags, out);
1138 ly_print_(out, " ");
1139 }
1140 /* <name> */
1141 trp_print_node_name(node.name, out);
1142}
1143
1144/**
1145 * @brief Print alignment (spaces) instead of \<status\>--\<flags\> \<name\> for divided node.
1146 * @param[in] node contains items to print.
1147 * @param[in] out is output handler.
1148 */
1149static void
1150trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out)
1151{
1152 uint32_t space = trp_get_flags_strlen(node.flags);
1153
1154 if (node.name.type == TRD_NODE_CASE) {
1155 /* :(<name> */
1156 space += strlen(TRD_NODE_NAME_PREFIX_CASE);
1157 } else if (node.name.type == TRD_NODE_CHOICE) {
1158 /* (<name> */
1159 space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
1160 } else {
1161 /* _<name> */
1162 space += strlen(" ");
1163 }
1164
1165 /* <name>
1166 * __
1167 */
1168 space += TRD_INDENT_LONG_LINE_BREAK;
1169
1170 ly_print_(out, "%*c", space, ' ');
1171}
1172
1173/**
1174 * @brief Print struct trt_node structure.
1175 * @param[in] node is item to print.
1176 * @param[in] pck package of functions for printing [\<keys\>] and \<iffeatures\>.
1177 * @param[in] indent is the indent in node.
1178 * @param[in,out] out is output handler.
1179 */
1180static void
1181trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
1182{
1183 ly_bool triple_dot;
1184 ly_bool divided;
1185 struct trt_cf_print cf_print_keys;
1186 struct trt_cf_print cf_print_iffeatures;
1187
1188 if (trp_node_is_empty(node)) {
1189 return;
1190 }
1191
1192 /* <status>--<flags> <name><opts> <type> <if-features> */
1193 triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT;
1194 divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;
1195
1196 if (triple_dot) {
1197 trp_print_node_name(node.name, out);
1198 return;
1199 } else if (!divided) {
1200 trp_print_node_up_to_name(node, out);
1201 } else {
1202 trp_print_divided_node_up_to_name(node, out);
1203 }
1204
1205 /* <opts> */
1206 /* <name>___<opts>*/
1207 cf_print_keys.ctx = pck.tree_ctx;
1208 cf_print_keys.pf = pck.fps.print_keys;
1209
1210 trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out);
1211
1212 /* <opts>__<type> */
1213 if (indent.btw_opts_type > 0) {
1214 ly_print_(out, "%*c", indent.btw_opts_type, ' ');
1215 }
1216
1217 /* <type> */
1218 trp_print_type(node.type, out);
1219
1220 /* <type>__<iffeatures> */
1221 if (indent.btw_type_iffeatures > 0) {
1222 ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
1223 }
1224
1225 /* <iffeatures> */
1226 cf_print_iffeatures.ctx = pck.tree_ctx;
1227 cf_print_iffeatures.pf = pck.fps.print_features_names;
1228
1229 trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out);
1230}
1231
1232/**
1233 * @brief Print .keyword based on .type.
1234 * @param[in] ks is keyword statement to print.
1235 * @param[in,out] out is output handler
1236 */
1237static void
1238trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
1239{
1240 switch (ks.type) {
1241 case TRD_KEYWORD_MODULE:
1242 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE);
1243 return;
1244 case TRD_KEYWORD_SUBMODULE:
1245 ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE);
1246 return;
1247 default:
1248 ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
1249 switch (ks.type) {
1250 case TRD_KEYWORD_AUGMENT:
1251 ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT);
1252 break;
1253 case TRD_KEYWORD_RPC:
1254 ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC);
1255 break;
1256 case TRD_KEYWORD_NOTIF:
1257 ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF);
1258 break;
1259 case TRD_KEYWORD_GROUPING:
1260 ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING);
1261 break;
1262 case TRD_KEYWORD_YANG_DATA:
1263 ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA);
1264 break;
1265 default:
1266 break;
1267 }
1268 break;
1269 }
1270}
1271
1272/**
1273 * @brief Get string length of stored keyword.
1274 * @param[in] type is type of the keyword statement.
1275 * @return length of the keyword statement name.
1276 */
1277static size_t
1278trp_keyword_type_strlen(trt_keyword_type type)
1279{
1280 switch (type) {
1281 case TRD_KEYWORD_MODULE:
1282 return sizeof(TRD_TOP_KEYWORD_MODULE) - 1;
1283 case TRD_KEYWORD_SUBMODULE:
1284 return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1;
1285 case TRD_KEYWORD_AUGMENT:
1286 return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1;
1287 case TRD_KEYWORD_RPC:
1288 return sizeof(TRD_BODY_KEYWORD_RPC) - 1;
1289 case TRD_KEYWORD_NOTIF:
1290 return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1;
1291 case TRD_KEYWORD_GROUPING:
1292 return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1;
1293 case TRD_KEYWORD_YANG_DATA:
1294 return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1;
1295 default:
1296 return 0;
1297 }
1298}
1299
1300/**
1301 * @brief Print .str which is string of name or path.
1302 * @param[in] ks is keyword statement structure.
1303 * @param[in] mll is max line length.
1304 * @param[in,out] out is output handler.
1305 */
1306static void
1307trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1308{
1309 uint32_t ind_initial;
1310 uint32_t ind_divided;
1311 /* flag if path must be splitted to more lines */
1312 ly_bool linebreak_was_set;
1313 /* flag if at least one subpath was printed */
1314 ly_bool subpath_printed;
1315 /* the sum of the sizes of the substrings on the current line */
1316 uint32_t how_far;
1317 /* pointer to start of the subpath */
1318 const char *sub_ptr;
1319 /* size of subpath from sub_ptr */
1320 size_t sub_len;
1321
1322 if ((!ks.str) || (ks.str[0] == '\0')) {
1323 return;
1324 }
1325
1326 /* module name cannot be splitted */
1327 if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) {
1328 ly_print_(out, "%s", ks.str);
1329 return;
1330 }
1331
1332 /* after -> for trd_keyword_stmt_body do */
1333
1334 /* set begin indentation */
1335 ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1;
1336 ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
1337 linebreak_was_set = 0;
1338 subpath_printed = 0;
1339 how_far = 0;
1340 sub_ptr = ks.str;
1341 sub_len = 0;
1342
1343 while (sub_ptr[0] != '\0') {
1344 uint32_t ind;
1345 /* skip slash */
1346 const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;
1347 /* get position of the end of substr */
1348 tmp = strchr(tmp, '/');
1349 /* set correct size if this is a last substring */
1350 sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
1351 /* actualize sum of the substring's sizes on the current line */
1352 how_far += sub_len;
1353 /* correction due to colon character if it this is last substring */
1354 how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
1355 /* choose indentation which depends on
1356 * whether the string is printed on multiple lines or not
1357 */
1358 ind = linebreak_was_set ? ind_divided : ind_initial;
1359 if (ind + how_far <= mll) {
1360 /* printing before max line length */
1361 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1362 subpath_printed = 1;
1363 } else {
1364 /* printing on new line */
1365 if (subpath_printed == 0) {
1366 /* first subpath is too long but print it at first line anyway */
1367 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1368 subpath_printed = 1;
1369 continue;
1370 }
1371 ly_print_(out, "\n");
1372 ly_print_(out, "%*c", ind_divided, ' ');
1373 linebreak_was_set = 1;
1374 sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
1375 how_far = sub_len;
1376 subpath_printed = 1;
1377 }
1378 }
1379}
1380
1381/**
1382 * @brief Print separator based on .type.
1383 * @param[in] ks is keyword statement structure.
1384 * @param[in,out] out is output handler.
1385 */
1386static void
1387trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, struct ly_out *out)
1388{
1389 if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) {
1390 ly_print_(out, ":");
1391 }
1392}
1393
1394/**
1395 * @brief Print entire struct trt_keyword_stmt structure.
1396 * @param[in] ks is item to print.
1397 * @param[in] mll is max line length.
1398 * @param[in,out] out is output handler.
1399 */
1400static void
1401trp_print_keyword_stmt(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
1402{
1403 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
1404 return;
1405 }
1406 trt_print_keyword_stmt_begin(ks, out);
1407 trt_print_keyword_stmt_str(ks, mll, out);
1408 trt_print_keyword_stmt_end(ks, out);
1409}
1410
1411/******************************************************************************
1412 * Main trp functions
1413 *****************************************************************************/
1414
1415/**
1416 * @brief Printing one line including wrapper and node which can be incomplete (divided).
1417 * @param[in] node is \<node\> representation.
1418 * @param[in] pck contains special printing functions callback.
1419 * @param[in] indent contains wrapper and indent in node numbers.
1420 * @param[in,out] out is output handler.
1421 */
1422static void
1423trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
1424{
1425 trp_print_wrapper(indent.wrapper, out);
1426 trp_print_node(node, pck, indent.in_node, out);
1427}
1428
1429/**
1430 * @brief Printing one line including wrapper and \<status\>--\<flags\> \<name\>\<option_mark\>.
1431 * @param[in] node is \<node\> representation.
1432 * @param[in] wr is wrapper for printing indentation before node.
1433 * @param[in] out is output handler.
1434 */
1435static void
1436trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out)
1437{
1438 trp_print_wrapper(wr, out);
1439 trp_print_node_up_to_name(node, out);
1440}
1441
1442/**
1443 * @brief Check if leafref target must be change to string 'leafref' because his target string is too long.
1444 * @param[in] node containing leafref target.
1445 * @param[in] wr is wrapper for printing indentation before node.
1446 * @param[in] mll is max line length.
1447 * @param[in] out is output handler.
1448 * @return true if leafref must be changed to string 'leafref'.
1449 */
1450static ly_bool
1451trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
1452{
1453 struct ly_out_clb_arg *data;
1454
1455 if (node.type.type != TRD_TYPE_TARGET) {
1456 return 0;
1457 }
1458
1459 /* set ly_out to counting characters */
1460 data = out->method.clb.arg;
1461
1462 data->counter = 0;
1463 data->mode = TRD_CHAR_COUNT;
1464 /* count number of printed bytes */
1465 trp_print_wrapper(wr, out);
1466 ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
1467 trp_print_divided_node_up_to_name(node, out);
1468 data->mode = TRD_PRINT;
1469
1470 return data->counter + strlen(node.type.str) > mll;
1471}
1472
1473/**
1474 * @brief Get default indent in node based on node values.
1475 * @param[in] node is \<node\> representation.
1476 * @return Default indent in node assuming that the node will not be divided.
1477 */
1478static struct trt_indent_in_node
1479trp_default_indent_in_node(struct trt_node node)
1480{
1481 struct trt_indent_in_node ret;
1482
1483 ret.type = TRD_INDENT_IN_NODE_NORMAL;
1484
1485 /* btw_name_opts */
1486 ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0;
1487
1488 /* btw_opts_type */
1489 if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) {
1490 ret.btw_opts_type = trp_mark_is_used(node.name) ?
1491 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
1492 TRD_INDENT_BEFORE_TYPE;
1493 } else {
1494 ret.btw_opts_type = 0;
1495 }
1496
1497 /* btw_type_iffeatures */
1498 ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0;
1499
1500 return ret;
1501}
1502
1503/**
1504 * @brief Setting linebreaks in trt_indent_in_node.
1505 *
1506 * The order where the linebreak tag can be placed is from the end.
1507 *
1508 * @param[in] indent containing alignment lengths or already linebreak marks.
1509 * @return indent with a newly placed linebreak tag.
1510 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible to place a more linebreaks.
1511 */
1512static struct trt_indent_in_node
1513trp_indent_in_node_place_break(struct trt_indent_in_node indent)
1514{
1515 /* somewhere must be set a line break in node */
1516 struct trt_indent_in_node ret = indent;
1517
1518 /* gradually break the node from the end */
1519 if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
1520 ret.btw_type_iffeatures = TRD_LINEBREAK;
1521 } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
1522 ret.btw_opts_type = TRD_LINEBREAK;
1523 } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
1524 /* set line break between name and opts */
1525 ret.btw_name_opts = TRD_LINEBREAK;
1526 } else {
1527 /* it is not possible to place a more line breaks,
1528 * unfortunately the max_line_length constraint is violated
1529 */
1530 ret.type = TRD_INDENT_IN_NODE_FAILED;
1531 }
1532 return ret;
1533}
1534
1535/**
1536 * @brief Get the first half of the node based on the linebreak mark.
1537 *
1538 * Items in the second half of the node will be empty.
1539 *
1540 * @param[in] node the whole \<node\> to be split.
1541 * @param[in] indent contains information in which part of the \<node\> the first half ends.
1542 * @return first half of the node, indent is unchanged.
1543 */
1544static struct trt_pair_indent_node
1545trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent)
1546{
1547 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1548
1549 if (indent.btw_name_opts == TRD_LINEBREAK) {
1550 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1551 ret.node.type = TRP_EMPTY_TRT_TYPE;
1552 ret.node.iffeatures = 0;
1553 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1554 ret.node.type = TRP_EMPTY_TRT_TYPE;
1555 ret.node.iffeatures = 0;
1556 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1557 ret.node.iffeatures = 0;
1558 }
1559
1560 return ret;
1561}
1562
1563/**
1564 * @brief Get the second half of the node based on the linebreak mark.
1565 *
1566 * Items in the first half of the node will be empty.
1567 * Indentations belonging to the first node will be reset to zero.
1568 *
1569 * @param[in] node the whole \<node\> to be split.
1570 * @param[in] indent contains information in which part of the \<node\> the second half starts.
1571 * @return second half of the node, indent is newly set.
1572 */
1573static struct trt_pair_indent_node
1574trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent)
1575{
1576 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node);
1577
1578 if (indent.btw_name_opts < 0) {
1579 /* Logically, the information up to token <opts> should be deleted,
1580 * but the the trp_print_node function needs it to create
1581 * the correct indent.
1582 */
1583 ret.indent.btw_name_opts = 0;
1584 ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
1585 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1586 } else if (indent.btw_opts_type == TRD_LINEBREAK) {
1587 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1588 ret.indent.btw_name_opts = 0;
1589 ret.indent.btw_opts_type = 0;
1590 ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
1591 } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) {
1592 ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type;
1593 ret.node.type = TRP_EMPTY_TRT_TYPE;
1594 ret.indent.btw_name_opts = 0;
1595 ret.indent.btw_opts_type = 0;
1596 ret.indent.btw_type_iffeatures = 0;
1597 }
1598 return ret;
1599}
1600
1601/**
1602 * @brief Get the correct alignment for the node.
1603 *
1604 * This function is recursively called itself.
1605 * It's like a backend function for a function trp_try_normal_indent_in_node.
1606 *
1607 * @param[in] node is \<node\> representation.
1608 * @param[in] pck contains speciall callback functions for printing.
1609 * @param[in] indent contains wrapper and indent in node numbers.
1610 * @param[in] mll is max line length.
1611 * @param[in,out] cnt counting number of characters to print.
1612 * @param[in,out] out is output handler.
1613 * @return pair of node and indentation numbers of that node.
1614 */
1615static struct trt_pair_indent_node
1616trp_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)
1617{
1618 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1619
1620 trp_print_line(node, pck, indent, out);
1621
1622 if (*cnt <= mll) {
1623 /* success */
1624 return ret;
1625 } else {
1626 ret.indent = trp_indent_in_node_place_break(ret.indent);
1627 if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) {
1628 /* erase information in node due to line break */
1629 ret = trp_first_half_node(node, ret.indent);
1630 /* check if line fits, recursive call */
1631 *cnt = 0;
1632 ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out);
1633 /* make sure that the result will be with the status divided
1634 * or eventually with status failed */
1635 ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
1636 }
1637 return ret;
1638 }
1639}
1640
1641/**
1642 * @brief Get the correct alignment for the node.
1643 *
1644 * @param[in] node is \<node\> representation.
1645 * @param[in] pck contains speciall callback functions for printing.
1646 * @param[in] indent contains wrapper and indent in node numbers.
1647 * @param[in] mll is max line length.
1648 * @param[in,out] out is output handler.
1649 * @return .type == TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the line, some indent variable has negative value as a line break sign.
1650 * @return .type == TRD_INDENT_IN_NODE_NORMAL - the node fits into the line, all indent variables values has non-negative number.
1651 * @return .type == TRD_INDENT_IN_NODE_FAILED - the node does not fit into the line, all indent variables has negative or zero values, function failed.
1652 */
1653static struct trt_pair_indent_node
1654trp_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)
1655{
1656 struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node);
1657 struct ly_out_clb_arg *data;
1658
1659 /* set ly_out to counting characters */
1660 data = out->method.clb.arg;
1661
1662 data->counter = 0;
1663 data->mode = TRD_CHAR_COUNT;
1664 ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out);
1665 data->mode = TRD_PRINT;
1666
1667 return ret;
1668}
1669
1670/**
1671 * @brief Auxiliary function for trp_print_entire_node that prints split nodes.
1672 * @param[in] node is node representation.
1673 * @param[in] ppck contains speciall callback functions for printing.
1674 * @param[in] ipck contains wrapper and indent in node numbers.
1675 * @param[in] mll is max line length.
1676 * @param[in,out] out is output handler.
1677 */
1678static void
1679trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1680{
1681 ly_bool entire_node_was_printed;
1682 struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1683
1684 if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1685 /* nothing can be done, continue as usual */
1686 ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1687 }
1688
1689 trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out);
1690 entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent);
1691
1692 if (!entire_node_was_printed) {
1693 ly_print_(out, "\n");
1694 /* continue with second half node */
1695 ind_node = trp_second_half_node(node, ind_node.indent);
1696 /* continue with printing node */
1697 trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out);
1698 } else {
1699 return;
1700 }
1701}
1702
1703/**
1704 * @brief Printing of the wrapper and the whole node, which can be divided into several lines.
1705 * @param[in] node is node representation.
1706 * @param[in] ppck contains speciall callback functions for printing.
1707 * @param[in] ipck contains wrapper and indent in node numbers.
1708 * @param[in] mll is max line length.
1709 * @param[in,out] out is output handler.
1710 */
1711static void
1712trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
1713{
1714 struct trt_pair_indent_node ind_node1;
1715 struct trt_pair_indent_node ind_node2;
1716 struct trt_pck_indent tmp;
1717
1718 if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) {
1719 node.type.type = TRD_TYPE_LEAFREF;
1720 }
1721
1722 /* check if normal indent is possible */
1723 ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out);
1724
1725 if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
1726 /* node fits to one line */
1727 trp_print_line(node, ppck, ipck, out);
1728 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
1729 /* node will be divided */
1730 /* print first half */
1731 tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent);
1732 /* pretend that this is normal node */
1733 tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;
1734
1735 trp_print_line(ind_node1.node, ppck, tmp, out);
1736 ly_print_(out, "\n");
1737
1738 /* continue with second half on new line */
1739 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1740 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1741
1742 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1743 } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) {
1744 /* node name is too long */
1745 trp_print_line_up_to_node_name(node, ipck.wrapper, out);
1746
1747 if (trp_node_body_is_empty(node)) {
1748 return;
1749 } else {
1750 ly_print_(out, "\n");
1751
1752 ind_node2 = trp_second_half_node(node, ind_node1.indent);
1753 ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
1754 tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent);
1755
1756 trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out);
1757 }
1758
1759 }
1760}
1761
1762/******************************************************************************
1763 * Definition of Tro reading functions
1764 *****************************************************************************/
1765
1766/**
1767 * @brief Get new trt_parent_cache if we apply the transfer to the child node in the tree.
1768 * @param[in] ca is parent cache for current node.
1769 * @param[in] pn is pointer to the current tree node.
1770 * @return Cache for the current node.
1771 */
1772static struct trt_parent_cache
1773tro_parent_cache_for_child(struct trt_parent_cache ca, const struct lysp_node *pn)
1774{
1775 struct trt_parent_cache ret;
1776
1777 ret.ancestor =
1778 pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
1779 pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
1780 pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
1781 ca.ancestor;
1782
1783 ret.lys_status =
1784 pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
1785 ca.lys_status;
1786
1787 ret.lys_config =
1788 ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
1789 ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
1790 pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
1791 ca.lys_config;
1792
1793 ret.last_list =
1794 pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
1795 ca.last_list;
1796
1797 return ret;
1798}
1799
1800/**
1801 * @brief Read next sibling of the current node.
1802 * @param[in] origin_tc is context of the tree.
1803 * @return A patch structure that has prepared pointers for updating struct trt_tree_ctx.
1804 * If sibling exists then .pn points to him, otherwise is set to NULL.
1805 * The .tpn points to its sibling if it exists and if .pn points to the same node as .tpn,
1806 * otherwise .tpn value from origin_tc is copied.
1807 */
1808static struct trt_tree_ctx_node_patch
1809tro_read_next_sibling(const struct trt_tree_ctx *origin_tc)
1810{
1811 assert(origin_tc && origin_tc->pn);
1812
1813 struct trt_tree_ctx_node_patch tc = TRP_INIT_TREE_CTX_NODE_PATCH(origin_tc->pn, origin_tc->tpn);
1814
1815 if (tc.pn->nodetype & (LYS_RPC | LYS_ACTION)) {
1816 if (tc.tpn == tc.pn) {
1817 /* just go to the top-node's sibling */
1818 tc.pn = tc.pn->next;
1819 tc.tpn = tc.pn;
1820 } else {
1821 /* try to go to the notif node as sibling */
1822 if (!tc.pn->next) {
1823 tc.pn = (const struct lysp_node *)lysp_node_notifs(tc.pn->parent);
1824 } else {
1825 tc.pn = tc.pn->next;
1826 }
1827 }
1828 } else if (tc.pn->nodetype & LYS_INPUT) {
1829 const struct lysp_node_action *parent = (struct lysp_node_action *)tc.pn->parent;
1830 /* if output action has data */
1831 if (parent->output.child) {
1832 /* then next sibling is output action */
1833 tc.pn = &parent->output.node;
1834 } else {
1835 /* else input action has no sibling */
1836 tc.pn = NULL;
1837 }
1838 /* if current node is output action */
1839 } else if (tc.pn->nodetype & LYS_OUTPUT) {
1840 /* then next sibling does not exist */
1841 tc.pn = NULL;
1842 /* if current node is notification */
1843 } else if (tc.pn->nodetype & LYS_NOTIF) {
1844 if (tc.tpn == tc.pn) {
1845 tc.pn = tc.pn->next;
1846 tc.tpn = tc.pn;
1847 } else {
1848 tc.pn = tc.pn->next;
1849 }
1850 } else {
1851 /* else actual node is some node with 'next' element */
1852 if (tc.tpn == tc.pn) {
1853 tc.tpn = tc.pn->next;
1854 }
1855 tc.pn = tc.pn->next;
1856 }
1857
1858 return tc;
1859}
1860
1861/**
1862 * @brief Find out if the current node has siblings.
1863 * @param[in] tc is context of the tree.
1864 * @return 1 if sibling exists otherwise 0.
1865 */
1866static ly_bool
1867tro_read_if_sibling_exists(const struct trt_tree_ctx *tc)
1868{
1869 return tro_read_next_sibling(tc).pn != NULL;
1870}
1871
1872/**
1873 * @brief Check if list statement has keys.
1874 * @param[in] pn is pointer to the list.
1875 * @return 1 if has keys, otherwise 0.
1876 */
1877static ly_bool
1878tro_lysp_list_has_keys(const struct lysp_node_list *pn)
1879{
1880 return trg_charptr_has_data(pn->key);
1881}
1882
1883/**
1884 * @brief Check if it contains at least one feature.
1885 * @param[in] iffs is pointer to the if-features.
1886 * @return 1 if has if-features, otherwise 0.
1887 */
1888static ly_bool
1889tro_lysp_node_to_iffeature(const struct lysp_qname *iffs)
1890{
1891 LY_ARRAY_COUNT_TYPE u;
1892 ly_bool ret = 0;
1893
1894 LY_ARRAY_FOR(iffs, u) {
1895 ret = 1;
1896 break;
1897 }
1898 return ret;
1899}
1900
1901/**
1902 * @brief Find out if leaf is also the key in last list.
1903 * @param[in] pn is pointer to leaf.
1904 * @param[in] ca_last_list is pointer to last visited list. Obtained from trt_parent_cache.
1905 * @return 1 if leaf is also the key, otherwise 0.
1906 */
1907static ly_bool
1908tro_lysp_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
1909{
1910 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
1911 const struct lysp_node_list *list = ca_last_list;
1912
1913 if (!list) {
1914 return 0;
1915 }
1916 return trg_charptr_has_data(list->key) ?
1917 trg_word_is_present(list->key, leaf->name, ' ') : 0;
1918}
1919
1920/**
1921 * @brief Check if container's type is presence.
1922 * @param[in] pn is pointer to container.
1923 * @return 1 if container has presence statement, otherwise 0.
1924 */
1925static ly_bool
1926tro_lysp_container_has_presence(const struct lysp_node *pn)
1927{
1928 return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
1929}
1930
1931/**
1932 * @brief Get leaflist's path without lysp_node type control.
1933 * @param[in] pn is pointer to the leaflist.
1934 */
1935static const char *
1936tro_lysp_leaflist_refpath(const struct lysp_node *pn)
1937{
1938 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
1939
1940 return list->type.path ? list->type.path->expr : NULL;
1941}
1942
1943/**
1944 * @brief Get leaflist's type name without lysp_node type control.
1945 * @param[in] pn is pointer to the leaflist.
1946 */
1947static const char *
1948tro_lysp_leaflist_type_name(const struct lysp_node *pn)
1949{
1950 const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;
1951
1952 return list->type.name;
1953}
1954
1955/**
1956 * @brief Get leaf's path without lysp_node type control.
1957 * @param[in] pn is pointer to the leaf node.
1958 */
1959static const char *
1960tro_lysp_leaf_refpath(const struct lysp_node *pn)
1961{
1962 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
1963
1964 return leaf->type.path ? leaf->type.path->expr : NULL;
1965}
1966
1967/**
1968 * @brief Get leaf's type name without lysp_node type control.
1969 * @param[in] pn is pointer to the leaf's type name.
1970 */
1971static const char *
1972tro_lysp_leaf_type_name(const struct lysp_node *pn)
1973{
1974 const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
1975
1976 return leaf->type.name;
1977}
1978
1979/**
1980 * @brief Get pointer to data using node type specification and getter function.
1981 *
1982 * @param[in] flags is node type specification. If it is the correct node, the getter function is called.
1983 * @param[in] f is getter function which provides the desired char pointer from the structure.
1984 * @param[in] pn pointer to node.
1985 * @return NULL if node has wrong type or getter function return pointer to NULL.
1986 * @return Pointer to desired char pointer obtained from the node.
1987 */
1988static const char *
1989tro_lysp_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
1990{
1991 if (pn->nodetype & flags) {
1992 const char *ret = f(pn);
1993 return trg_charptr_has_data(ret) ? ret : NULL;
1994 } else {
1995 return NULL;
1996 }
1997}
1998
1999/**
2000 * @brief Transformation of the lysp flags to Yang tree \<status\>.
2001 * @param[in] flags is node's flags obtained from the tree.
2002 */
2003static trt_status_type
2004tro_lysp_flags2status(uint16_t flags)
2005{
2006 return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE :
2007 flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED :
2008 TRD_STATUS_TYPE_CURRENT;
2009}
2010
2011/**
2012 * @brief Transformation of the lysp flags to Yang tree \<flags\> but more specifically 'ro' or 'rw'.
2013 * @param[in] flags is node's flags obtained from the tree.
2014 */
2015static trt_flags_type
2016tro_lysp_flags2config(uint16_t flags)
2017{
2018 return flags & LYS_CONFIG_R ?
2019 TRD_FLAGS_TYPE_RO : TRD_FLAGS_TYPE_RW;
2020}
2021
2022/**
2023 * @brief Get name of the module.
2024 * @param[in] tc is context of the tree.
2025 */
2026static struct trt_keyword_stmt
2027tro_read_module_name(const struct trt_tree_ctx *tc)
2028{
2029 assert(tc && tc->module && tc->module->name);
2030 return (struct trt_keyword_stmt) {
2031 .type = TRD_KEYWORD_MODULE, .str = tc->module->name
2032 };
2033}
2034
2035/**
2036 * @brief Resolve \<status\> of the current node.
2037 * @param[in] nodetype is node's type obtained from the tree.
2038 * @param[in] flags is node's flags obtained from the tree.
2039 * @param[in] ca_lys_status is inherited status obtained from trt_parent_cache.
2040 * @return The status type.
2041 */
2042static trt_status_type
2043tro_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
2044{
2045 /* LYS_INPUT and LYS_OUTPUT is special case */
2046 if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2047 return tro_lysp_flags2status(ca_lys_status);
2048 /* if ancestor's status is deprc or obslt and also node's status is not set */
2049 } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2050 /* get ancestor's status */
2051 return tro_lysp_flags2status(ca_lys_status);
2052 } else {
2053 /* else get node's status */
2054 return tro_lysp_flags2status(flags);
2055 }
2056}
2057
2058/**
2059 * @brief Resolve \<flags\> of the current node.
2060 * @param[in] nodetype is node's type obtained from the tree.
2061 * @param[in] flags is node's flags obtained from the tree.
2062 * @param[in] ca_ancestor is ancestor type obtained from trt_parent_cache.
2063 * @param[in] ca_lys_config is inherited config item obtained from trt_parent_cache.
2064 * @return The flags type.
2065 */
2066static trt_flags_type
2067tro_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config)
2068{
2069 if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
2070 return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
2071 } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
2072 return TRD_FLAGS_TYPE_RO;
2073 } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
2074 return TRD_FLAGS_TYPE_RO;
2075 } else if (nodetype & LYS_NOTIF) {
2076 return TRD_FLAGS_TYPE_NOTIF;
2077 } else if (nodetype & LYS_USES) {
2078 return TRD_FLAGS_TYPE_USES_OF_GROUPING;
2079 } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
2080 return TRD_FLAGS_TYPE_RPC;
2081 /* if config is not set then look at ancestor's config and get his config */
2082 } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
2083 return tro_lysp_flags2config(ca_lys_config);
2084 } else {
2085 return tro_lysp_flags2config(flags);
2086 }
2087}
2088
2089/**
2090 * @brief Resolve node type of the current node.
2091 * @param[in] pn is pointer to the current node in the tree.
2092 * @param[in] ca_last_list is pointer to the last visited list. Obtained from the trt_parent_cache.
2093 */
2094static trt_node_type
2095tro_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
2096{
2097 if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
2098 return TRD_NODE_ELSE;
2099 } else if (pn->nodetype & LYS_CASE) {
2100 return TRD_NODE_CASE;
2101 } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
2102 return TRD_NODE_OPTIONAL_CHOICE;
2103 } else if (pn->nodetype & LYS_CHOICE) {
2104 return TRD_NODE_CHOICE;
2105 } else if ((pn->nodetype & LYS_CONTAINER) && (tro_lysp_container_has_presence(pn))) {
2106 return TRD_NODE_CONTAINER;
2107 } else if ((pn->nodetype & LYS_LIST) && (tro_lysp_list_has_keys((const struct lysp_node_list *)pn))) {
2108 return TRD_NODE_KEYS;
2109 } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2110 return TRD_NODE_LISTLEAFLIST;
2111 } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
2112 return TRD_NODE_OPTIONAL;
2113 } else if ((pn->nodetype & LYS_LEAF) && !(pn->flags & LYS_MAND_TRUE) && (!tro_lysp_leaf_is_key(pn, ca_last_list))) {
2114 return TRD_NODE_OPTIONAL;
2115 } else {
2116 return TRD_NODE_ELSE;
2117 }
2118}
2119
2120/**
2121 * @brief Transformation of current lysp_node to struct trt_node.
2122 * @param[in] ca contains stored important data when browsing the tree downwards.
2123 * @param[in] tc is context of the tree.
2124 */
2125static struct trt_node
2126tro_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
2127{
2128 assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);
2129 const struct lysp_node *pn = tc->pn;
2130 struct trt_node ret = TRP_EMPTY_NODE;
2131 const char *tmp;
2132
2133 /* <status> */
2134 ret.status = tro_resolve_status(pn->nodetype, pn->flags, ca.lys_status);
2135
2136 /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */
2137 /* <flags> */
2138 ret.flags = tro_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config);
2139
2140 /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */
2141 /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */
2142 /* set type of the node */
2143 ret.name.type = tro_resolve_node_type(pn, ca.last_list);
2144
2145 /* TODO: ret.name.module_prefix is not supported right now. */
2146 ret.name.module_prefix = NULL;
2147
2148 /* set node's name */
2149 ret.name.str = pn->name;
2150
2151 /* <type> */
2152 tmp = NULL;
2153
2154 if ((tmp = tro_lysp_node_charptr(LYS_LEAFLIST, tro_lysp_leaflist_refpath, pn))) {
2155 ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2156 } else if ((tmp = tro_lysp_node_charptr(LYS_LEAFLIST, tro_lysp_leaflist_type_name, pn))) {
2157 ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2158 } else if ((tmp = tro_lysp_node_charptr(LYS_LEAF, tro_lysp_leaf_refpath, pn))) {
2159 ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
2160 } else if ((tmp = tro_lysp_node_charptr(LYS_LEAF, tro_lysp_leaf_type_name, pn))) {
2161 ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
2162 } else if ((pn->nodetype & LYS_ANYDATA) == LYS_ANYDATA) {
2163 ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
2164 } else if (pn->nodetype & LYS_ANYXML) {
2165 ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
2166 } else {
2167 ret.type = TRP_EMPTY_TRT_TYPE;
2168 }
2169
2170 /* <iffeature> */
2171 ret.iffeatures = tro_lysp_node_to_iffeature(pn->iffeatures);
2172
2173 ret.last_one = !tro_read_if_sibling_exists(tc);
2174
2175 return ret;
2176}
2177
2178/******************************************************************************
2179 * Modify Tro getters
2180 *****************************************************************************/
2181
2182/**
2183 * @brief Change current node pointer to its parent but only if parent exists.
2184 * @param[in,out] tc is tree context. Contains pointer to the current node.
2185 * @return 1 if the node had parents and the change was successful.
2186 * @return 0 if the node did not have parents. The pointer to the current node did not change.
2187 */
2188static ly_bool
2189tro_modi_parent(struct trt_tree_ctx *tc)
2190{
2191 assert(tc && tc->pn);
2192 /* If no parent exists, stay in actual node. */
2193 if (tc->pn != tc->tpn) {
2194 tc->pn = tc->pn->parent;
2195 return 1;
2196 } else {
2197 return 0;
2198 }
2199}
2200
2201/**
2202 * @brief Change the current node pointer to its child but only if exists.
2203 * @param[in] ca contains inherited data from ancestors.
2204 * @param[in,out] tc is context of the tree. Contains pointer to the current node.
2205 * @return Non-empty \<node\> representation of the current node's child. The tc parameter is modified.
2206 * @return Empty \<node\> representation if child don't exists. The tc parameter is not modified.
2207 */
2208static struct trt_node
2209tro_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
2210{
2211 assert(tc && tc->pn);
2212
2213 struct trt_parent_cache new_ca = tro_parent_cache_for_child(ca, tc->pn);
2214
2215 if (tc->pn->nodetype & (LYS_ACTION | LYS_RPC)) {
2216 const struct lysp_node_action *act = (const struct lysp_node_action *)tc->pn;
2217 if (act->input.child) {
2218 /* go to LYS_INPUT */
2219 tc->pn = &act->input.node;
2220 return tro_read_node(new_ca, tc);
2221 } else if (act->output.child) {
2222 /* go to LYS_OUTPUT */
2223 tc->pn = &act->output.node;
2224 return tro_read_node(new_ca, tc);
2225 } else {
2226 /* input action and output action are not set */
2227 return TRP_EMPTY_NODE;
2228 }
2229 } else {
2230 const struct lysp_node *pn = lysp_node_child(tc->pn);
2231 if (pn) {
2232 tc->pn = pn;
2233 return tro_read_node(new_ca, tc);
2234 } else {
2235 /* current node can't have children or has no children */
2236 /* but maybe has some actions or notifs */
2237 const struct lysp_node_action *actions = lysp_node_actions(tc->pn);
2238 const struct lysp_node_notif *notifs = lysp_node_notifs(tc->pn);
2239
2240 if (actions) {
2241 tc->pn = (const struct lysp_node *)actions;
2242 return tro_read_node(new_ca, tc);
2243 } else if (notifs) {
2244 tc->pn = (const struct lysp_node *)notifs;
2245 return tro_read_node(new_ca, tc);
2246 } else {
2247 return TRP_EMPTY_NODE;
2248 }
2249 }
2250 }
2251}
2252
2253/**
2254 * @brief Change the current node pointer to the first child of node's parent.
2255 * If current node is already first sibling/child then nothing will change.
2256 * @param[in,out] tc is tree context.
2257 */
2258static void
2259tro_modi_first_sibling(struct trt_tree_ctx *tc)
2260{
2261 assert(tc && tc->pn && tc->module && tc->module->parsed);
2262
2263 if (tro_modi_parent(tc)) {
2264 tro_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc);
2265 } else {
2266 /* current node is top-node */
2267
2268 struct lysp_module *pm = tc->module->parsed;
2269
2270 switch (tc->section) {
2271 case TRD_SECT_MODULE:
2272 tc->pn = pm->data;
2273 break;
2274 case TRD_SECT_AUGMENT:
2275 tc->pn = ((const struct lysp_node_augment *)tc->pn->parent)->child;
2276 break;
2277 case TRD_SECT_RPCS:
2278 tc->pn = (const struct lysp_node *)pm->rpcs;
2279 break;
2280 case TRD_SECT_NOTIF:
2281 tc->pn = (const struct lysp_node *)pm->notifs;
2282 break;
2283 case TRD_SECT_GROUPING:
2284 tc->pn = ((const struct lysp_node_grp *)tc->pn->parent)->child;
2285 break;
2286 case TRD_SECT_YANG_DATA:
2287 /*TODO: yang-data is not supported now */
2288 break;
2289 }
2290
2291 /* update pointer to top-node */
2292 tc->tpn = tc->pn;
2293 }
2294}
2295
2296/**
2297 * @brief Change the pointer to the current node to its next sibling only if exists.
2298 * @param[in] ca contains inherited data from ancestors.
2299 * @param[in,out] tc is tree context. Contains pointer to the current node.
2300 * @return Non-empty \<node\> representation if sibling exists. The tc is modified.
2301 * @return Empty \<node\> representation otherwise. The tc is not modified.
2302 */
2303static struct trt_node
2304tro_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
2305{
2306 struct trt_tree_ctx_node_patch patch = tro_read_next_sibling(tc);
2307
2308 /* if next sibling exists */
2309 if (patch.pn) {
2310 /* update trt_tree_ctx */
2311 tc->pn = patch.pn;
2312 tc->tpn = patch.tpn;
2313 return tro_read_node(ca, tc);
2314 } else {
2315 return TRP_EMPTY_NODE;
2316 }
2317}
2318
2319/**
2320 * @brief Get next (or first) augment section if exists.
2321 * @param[in,out] tc is tree context.
2322 * @return Section's representation if (next augment) section exists.
2323 * The tc is modified and his pointer points to the first node in augment section.
2324 * @return Empty section structure otherwise.
2325 */
2326static struct trt_keyword_stmt
2327tro_modi_next_augment(struct trt_tree_ctx *tc)
2328{
2329 assert(tc && tc->module && tc->module->parsed);
2330 const struct lysp_node_augment *augs;
2331
2332 /* if next_augment func was called for the first time */
2333 if (tc->section != TRD_SECT_AUGMENT) {
2334 tc->section = TRD_SECT_AUGMENT;
2335 augs = tc->module->parsed->augments;
2336 } else {
2337 /* get augment sibling from top-node pointer */
2338 augs = (const struct lysp_node_augment *)tc->tpn->parent->next;
2339 }
2340
2341 if ((augs) && (augs->child)) {
2342 tc->pn = augs->child;
2343 tc->tpn = tc->pn;
2344 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid);
2345 } else {
2346 return TRP_EMPTY_KEYWORD_STMT;
2347 }
2348}
2349
2350/**
2351 * @brief Get rpcs section if exists.
2352 * @param[in,out] tc is tree context.
2353 * @return Section representation if it exists.
2354 * The tc is modified and his pointer points to the first node in rpcs section.
2355 * @return Empty section representation otherwise.
2356 */
2357static struct trt_keyword_stmt
2358tro_modi_get_rpcs(struct trt_tree_ctx *tc)
2359{
2360 assert(tc && tc->module && tc->module->parsed);
2361 const struct lysp_node_action *actions = tc->module->parsed->rpcs;
2362
2363 if (actions) {
2364 tc->section = TRD_SECT_RPCS;
2365 tc->pn = &actions->node;
2366 tc->tpn = tc->pn;
2367 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL);
2368 } else {
2369 return TRP_EMPTY_KEYWORD_STMT;
2370 }
2371}
2372
2373/**
2374 * @brief Get notification section if exists
2375 * @param[in,out] tc is tree context.
2376 * @return Section representation if it exists.
2377 * The tc is modified and his pointer points to the first node in notification section.
2378 * @return Empty section representation otherwise.
2379 */
2380static struct trt_keyword_stmt
2381tro_modi_get_notifications(struct trt_tree_ctx *tc)
2382{
2383 assert(tc && tc->module && tc->module->parsed);
2384 const struct lysp_node_notif *notifs = tc->module->parsed->notifs;
2385
2386 if (notifs) {
2387 tc->section = TRD_SECT_NOTIF;
2388 tc->pn = &notifs->node;
2389 tc->tpn = tc->pn;
2390 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL);
2391 } else {
2392 return TRP_EMPTY_KEYWORD_STMT;
2393 }
2394}
2395
2396/**
2397 * @brief Get next (or first) grouping section if exists
2398 * @param[in,out] tc is tree context.
2399 * @return The next (or first) section representation if it exists.
2400 * The tc is modified and his pointer points to the first node in this grouping section.
2401 * @return Empty section representation otherwise.
2402 */
2403static struct trt_keyword_stmt
2404tro_modi_next_grouping(struct trt_tree_ctx *tc)
2405{
2406 assert(tc && tc->module && tc->module->parsed);
2407 const struct lysp_node_grp *grps;
2408
2409 if (tc->section != TRD_SECT_GROUPING) {
2410 tc->section = TRD_SECT_GROUPING;
2411 grps = tc->module->parsed->groupings;
2412 } else {
2413 grps = (const struct lysp_node_grp *)tc->tpn->parent->next;
2414 }
2415
2416 if ((grps) && (grps->child)) {
2417 tc->pn = grps->child;
2418 tc->tpn = tc->pn;
2419 return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name);
2420 } else {
2421 return TRP_EMPTY_KEYWORD_STMT;
2422 }
2423}
2424
2425/**
2426 * @brief Get next yang-data section if exists.
2427 *
2428 * Not implemented.
2429 *
2430 * @param[in,out] tc is tree context.
2431 * @return Section representation if it exists.
2432 * @return Empty section representation otherwise.
2433 */
2434static struct trt_keyword_stmt
2435tro_modi_next_yang_data(struct trt_tree_ctx *tc)
2436{
2437 tc->section = TRD_SECT_YANG_DATA;
2438 /* TODO: yang-data is not supported */
2439 return TRP_EMPTY_KEYWORD_STMT;
2440}
2441
2442/******************************************************************************
2443 * Print Tro getters
2444 *****************************************************************************/
2445
2446/**
2447 * @brief Print current node's iffeatures.
2448 * @param[in] tc is tree context.
2449 * @param[in,out] out is output handler.
2450 */
2451static void
2452tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
2453{
2454 const struct lysp_qname *iffs = tc->pn->iffeatures;
2455
2456 LY_ARRAY_COUNT_TYPE i;
2457
2458 LY_ARRAY_FOR(iffs, i) {
2459 if (i == 0) {
2460 ly_print_(out, "%s", iffs[i].str);
2461 } else {
2462 ly_print_(out, ",%s", iffs[i].str);
2463 }
2464 }
2465
2466}
2467
2468/**
2469 * @brief Print current list's keys.
2470 *
2471 * Well, actually printing keys in the lysp_tree is trivial, because char* points to all keys.
2472 * However, special functions have been reserved for this, because in principle
2473 * the list of elements can have more implementations.
2474 *
2475 * @param[in] tc is tree context.
2476 * @param[in,out] out is output handler.
2477 */
2478static void
2479tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
2480{
2481 const struct lysp_node *pn = tc->pn;
2482 const struct lysp_node_list *list;
2483
2484 if (pn->nodetype != LYS_LIST) {
2485 return;
2486 }
2487
2488 list = (const struct lysp_node_list *)pn;
2489
2490 if (trg_charptr_has_data(list->key)) {
2491 ly_print_(out, "%s", list->key);
2492 }
2493}
2494
2495/******************************************************************************
2496 * Definition of tree browsing functions
2497 *****************************************************************************/
2498
2499/**
2500 * @brief Get size of node name.
2501 * @param[in] name contains name and mark.
2502 * @return positive value total size of the node name.
2503 * @return negative value as an indication that option mark is included in the total size.
2504 */
2505static int32_t
2506trb_strlen_of_name_and_mark(struct trt_node_name name)
2507{
2508 size_t name_len = strlen(name.str);
2509
2510 if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) {
2511 /* counting also parentheses */
2512 name_len += 2;
2513 }
2514
2515 return trp_mark_is_used(name) ?
2516 ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) :
2517 (int32_t)name_len;
2518}
2519
2520/**
2521 * @brief Calculate the btw_opts_type indent size for a particular node.
2522 * @param[in] name is the node for which we get btw_opts_type.
2523 * @param[in] max_len4all is the maximum value of btw_opts_type that it can have.
2524 * @return btw_opts_type for node.
2525 */
2526static int16_t
2527trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all)
2528{
2529 int32_t name_len;
2530 int16_t min_len;
2531 int16_t ret;
2532
2533 name_len = trb_strlen_of_name_and_mark(name);
2534
2535 /* negative value indicate that in name is some opt mark */
2536 min_len = name_len < 0 ?
2537 TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH :
2538 TRD_INDENT_BEFORE_TYPE;
2539 ret = abs(max_len4all) - abs(name_len);
2540
2541 /* correction -> negative indicate that name is too long. */
2542 return ret < 0 ? min_len : ret;
2543}
2544
2545/**
2546 * @brief Print node.
2547 *
2548 * This function is wrapper for trp_print_entire_node function.
2549 * But difference is that take max_gap_before_type parameter which will be used to set the unified alignment.
2550 *
2551 * @param[in] max_gap_before_type is number of indent before \<type\>.
2552 * @param[in] wr is wrapper for printing indentation before node.
2553 * @param[in] ca contains inherited data from ancestors.
2554 * @param[in] pc contains mainly functions for printing.
2555 * @param[in] tc is tree context.
2556 */
2557static void
2558trb_print_entire_node(uint32_t max_gap_before_type, struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2559{
2560 struct trt_node node = pc->fp.read.node(ca, tc);
2561 struct trt_indent_in_node ind = trp_default_indent_in_node(node);
2562
2563 if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) {
2564 /* print actual node with unified indent */
2565 ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type);
2566 }
2567 /* after -> print actual node with default indent */
2568 trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
2569 TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
2570}
2571
2572/**
2573 * @brief Check if parent of the current node is the last of his siblings.
2574 *
2575 * To mantain stability use this function only if the current node is the first of the siblings.
2576 * Side-effect -> current node is set to the first sibling if node has a parent otherwise no side-effect.
2577 *
2578 * @param[in] fp contains all 'tro' callback functions.
2579 * @param[in,out] tc is tree context.
2580 * @return 1 if parent is last sibling otherwise 0.
2581 */
2582static ly_bool
2583trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc)
2584{
2585 if (fp.modify.parent(tc)) {
2586 ly_bool ret = fp.read.if_sibling_exists(tc);
2587 fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc);
2588 return !ret;
2589 } else {
2590 return !fp.read.if_sibling_exists(tc);
2591 }
2592}
2593
2594/**
2595 * @brief Find sibling with the biggest node name and return that size.
2596 *
2597 * Side-effect -> Current node is set to the first sibling.
2598 *
2599 * @param[in] ca contains inherited data from ancestors.
2600 * @param[in] pc contains mainly functions for printing.
2601 * @param[in,out] tc is tree context.
2602 * @return positive number lesser than upper_limit as a sign that only the node name is included in the size.
2603 * @return negative number whose absolute value is less than upper_limit and sign that node name and his opt mark is included in the size.
2604 */
2605static int32_t
2606trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2607{
2608 int32_t ret = 0;
2609
2610 pc->fp.modify.first_sibling(tc);
2611
2612 for (struct trt_node node = pc->fp.read.node(ca, tc);
2613 !trp_node_is_empty(node);
2614 node = pc->fp.modify.next_sibling(ca, tc)) {
2615 int32_t maxlen = trb_strlen_of_name_and_mark(node.name);
2616 ret = abs(maxlen) > abs(ret) ? maxlen : ret;
2617 }
2618 pc->fp.modify.first_sibling(tc);
2619 return ret;
2620}
2621
2622/**
2623 * @brief Find maximal indent between \<opts\> and \<type\> for siblings.
2624 *
2625 * Side-effect -> Current node is set to the first sibling.
2626 *
2627 * @param[in] ca contains inherited data from ancestors.
2628 * @param[in] pc contains mainly functions for printing.
2629 * @param[in,out] tc is tree context.
2630 * @return max btw_opts_type value for rest of the siblings
2631 */
2632static int16_t
2633trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2634{
2635 int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc);
2636 int16_t ind_before_type = maxlen_node_name < 0 ?
2637 TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */
2638 TRD_INDENT_BEFORE_TYPE;
2639
2640 return abs(maxlen_node_name) + ind_before_type;
2641}
2642
2643/**
2644 * @brief Find out if it is possible to unify the alignment before \<type\>.
2645 *
2646 * The goal is for all node siblings to have the same alignment for \<type\> as if they were in a column.
2647 * All siblings who cannot adapt because they do not fit on the line at all are ignored.
2648 * Side-effect -> Current node is set to the first sibling.
2649 *
2650 * @param[in] ca contains inherited data from ancestors.
2651 * @param[in] pc contains mainly functions for printing.
2652 * @param[in,out] tc is tree context.
2653 * @return 0 if all siblings cannot fit on the line.
2654 * @return positive number indicating the maximum number of spaces before \<type\> if the length of the node name is 0.
2655 * To calculate the btw_opts_type indent size for a particular node, use the trb_calc_btw_opts_type function.
2656*/
2657static uint32_t
2658trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2659{
2660 return trb_max_btw_opts_type4siblings(ca, pc, tc);
2661}
2662
2663/**
2664 * @brief For the current node: recursively print all of its child nodes and all of its siblings, including their children.
2665 *
2666 * This function is an auxiliary function for trb_print_subtree_nodes.
2667 * The parent of the current node is expected to exist.
2668 * Nodes are printed, including unified sibling node alignment (align \<type\> to column).
2669 * Side-effect -> current node is set to the last sibling.
2670 *
2671 * @param[in] wr is wrapper for printing identation before node.
2672 * @param[in] ca contains inherited data from ancestors.
2673 * @param[in] pc contains mainly functions for printing.
2674 * @param[in,out] tc is tree context.
2675 */
2676static void
2677trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2678{
2679 uint32_t max_gap_before_type;
2680 ly_bool sibling_flag = 0;
2681 ly_bool child_flag = 0;
2682
2683 /* if node is last sibling, then do not add '|' to wrapper */
2684 wr = trb_parent_is_last_sibling(pc->fp, tc) ?
2685 trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
2686
2687 /* try unified indentation in node */
2688 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
2689
2690 /* print all siblings */
2691 do {
2692 struct trt_parent_cache new_ca;
2693 struct trt_node node;
2694 /* print linebreak before printing actual node */
2695 ly_print_(pc->out, "\n");
2696 /* print node */
2697 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
2698
2699 new_ca = tro_parent_cache_for_child(ca, tc->pn);
2700 /* go to the actual node's child or stay in actual node */
2701 node = pc->fp.modify.next_child(ca, tc);
2702 child_flag = !trp_node_is_empty(node);
2703
2704 if (child_flag) {
2705 /* print all childs - recursive call */
2706 trb_print_nodes(wr, new_ca, pc, tc);
2707 /* get back from child node to actual node */
2708 pc->fp.modify.parent(tc);
2709 }
2710
2711 /* go to the actual node's sibling */
2712 node = pc->fp.modify.next_sibling(ca, tc);
2713 sibling_flag = !trp_node_is_empty(node);
2714
2715 /* go to the next sibling or stay in actual node */
2716 } while (sibling_flag);
2717}
2718
2719/**
2720 * @brief Print subtree of nodes.
2721 *
2722 * The current node is expected to be the root of the subtree.
2723 * Before root node is no linebreak printing. This must be addressed by the caller.
2724 * Root node will also be printed. Behind last printed node is no linebreak.
2725 *
2726 * @param[in] max_gap_before_type is result from trb_try_unified_indent function for root node. Set parameter to 0 if distance does not matter.
2727 * @param[in] wr is wrapper saying how deep in the whole tree is the root of the subtree.
2728 * @param[in] ca is parent_cache from root's parent. If root is top-level node, insert TRP_EMPTY_PARENT_CACHE.
2729 * @param[in] pc is pointer to the printer (trp) context.
2730 * @param[in,out] tc is pointer to the tree (tro) context.
2731 */
2732static void
2733trb_print_subtree_nodes(uint32_t max_gap_before_type, struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2734{
2735 struct trt_parent_cache new_ca;
2736 struct trt_node node;
2737
2738 trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc);
2739 /* go to the actual node's child */
2740 new_ca = tro_parent_cache_for_child(ca, tc->pn);
2741 node = pc->fp.modify.next_child(ca, tc);
2742
2743 if (!trp_node_is_empty(node)) {
2744 /* print root's nodes */
2745 trb_print_nodes(wr, new_ca, pc, tc);
2746 /* get back from child node to actual node */
2747 pc->fp.modify.parent(tc);
2748 }
2749}
2750
2751/**
2752 * @brief Get number of siblings.
2753 *
2754 * Side-effect -> current node is set to the first sibling.
2755 *
2756 * @param[in] fp contains callback functions which modify tree context
2757 * @param[in,out] tc is the tree context.
2758 * @return Number of siblings of the current node.
2759 */
2760static uint32_t
2761trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc)
2762{
2763 uint32_t ret = 1;
2764 struct trt_node node = TRP_EMPTY_NODE;
2765
2766 /* including actual node */
2767 fp.first_sibling(tc);
2768 while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) {
2769 ret++;
2770 }
2771 fp.first_sibling(tc);
2772 return ret;
2773}
2774
2775/**
2776 * @brief Print all parents and their children.
2777 *
2778 * This function is suitable for printing top-level nodes that do not have ancestors.
2779 * Function call print_subtree_nodes for all top-level siblings.
2780 * Use this function after 'module' keyword or 'augment' and so.
2781 *
2782 * @param[in] wr_t is type of the wrapper.
2783 * @param[pc] pc contains mainly functions for printing.
2784 * @param[in,out] tc is tree context.
2785 */
2786static void
2787trb_print_family_tree(trd_wrapper_type wr_t, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2788{
2789 struct trt_wrapper wr;
2790 struct trt_parent_cache ca;
2791 uint32_t total_parents;
2792 uint32_t max_gap_before_type;
2793
2794 wr = wr_t == TRD_WRAPPER_TOP ? TRP_INIT_WRAPPER_TOP : TRP_INIT_WRAPPER_BODY;
2795 ca = TRP_EMPTY_PARENT_CACHE;
2796 total_parents = trb_get_number_of_siblings(pc->fp.modify, tc);
2797 max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
2798
2799 for (uint32_t i = 0; i < total_parents; i++) {
2800 ly_print_(pc->out, "\n");
2801 trb_print_subtree_nodes(max_gap_before_type, wr, ca, pc, tc);
2802 pc->fp.modify.next_sibling(ca, tc);
2803 }
2804}
2805
2806/******************************************************************************
2807 * Definition of trm main functions
2808 *****************************************************************************/
2809
2810/**
2811 * @brief General function to prevent repetitiveness code.
2812 * @param[in] ks is section representation.
2813 * @param[in] pc contains mainly functions for printing.
2814 * @param[in,out] tc is the tree context.
2815 */
2816static void
2817trm_print_body_section(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2818{
2819 if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) {
2820 return;
2821 }
2822 trp_print_keyword_stmt(ks, pc->max_line_length, pc->out);
2823 trb_print_family_tree(TRD_WRAPPER_BODY, pc, tc);
2824}
2825
2826/**
2827 * @brief Print 'module' keyword, its name and all nodes.
2828 * @param[in] pc contains mainly functions for printing.
2829 * @param[in,out] tc is the tree context.
2830 */
2831static void
2832trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2833{
2834 trp_print_keyword_stmt(pc->fp.read.module_name(tc), pc->max_line_length, pc->out);
2835 /* check if module section contains any data */
2836 if (tc->tpn) {
2837 trb_print_family_tree(TRD_WRAPPER_TOP, pc, tc);
2838 }
2839}
2840
2841/**
2842 * @brief For all augment sections: print 'augment' keyword, its target node and all nodes.
2843 * @param[in] pc contains mainly functions for printing.
2844 * @param[in,out] tc is the tree context.
2845 */
2846static void
2847trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2848{
2849 ly_bool once = 1;
2850
2851 for (struct trt_keyword_stmt ks = pc->fp.modify.next_augment(tc);
2852 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
2853 ks = pc->fp.modify.next_augment(tc)) {
2854 if (once) {
2855 ly_print_(pc->out, "\n");
2856 ly_print_(pc->out, "\n");
2857 once = 0;
2858 } else {
2859 ly_print_(pc->out, "\n");
2860 }
2861 trm_print_body_section(ks, pc, tc);
2862 }
2863}
2864
2865/**
2866 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
2867 * @param[in] pc contains mainly functions for printing.
2868 * @param[in,out] tc is the tree context.
2869 */
2870static void
2871trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2872{
2873 struct trt_keyword_stmt rpc = pc->fp.modify.get_rpcs(tc);
2874
2875 if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) {
2876 ly_print_(pc->out, "\n");
2877 ly_print_(pc->out, "\n");
2878 trm_print_body_section(rpc, pc, tc);
2879 }
2880}
2881
2882/**
2883 * @brief For notifications section: print 'notifications' keyword and all its nodes.
2884 * @param[in] pc contains mainly functions for printing.
2885 * @param[in,out] tc is the tree context.
2886 */
2887static void
2888trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2889{
2890 struct trt_keyword_stmt notifs = pc->fp.modify.get_notifications(tc);
2891
2892 if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) {
2893 ly_print_(pc->out, "\n");
2894 ly_print_(pc->out, "\n");
2895 trm_print_body_section(notifs, pc, tc);
2896 }
2897}
2898
2899/**
2900 * @brief For all grouping sections: print 'grouping' keyword, its name and all nodes.
2901 * @param[in] pc contains mainly functions for printing.
2902 * @param[in,out] tc is the tree context.
2903 */
2904static void
2905trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2906{
2907 ly_bool once = 1;
2908
2909 for (struct trt_keyword_stmt ks = pc->fp.modify.next_grouping(tc);
2910 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
2911 ks = pc->fp.modify.next_grouping(tc)) {
2912 if (once) {
2913 ly_print_(pc->out, "\n");
2914 ly_print_(pc->out, "\n");
2915 once = 0;
2916 } else {
2917 ly_print_(pc->out, "\n");
2918 }
2919 trm_print_body_section(ks, pc, tc);
2920 }
2921}
2922
2923/**
2924 * @brief For all yang-data sections: print 'yang-data' keyword and all its nodes.
2925 * @param[in] pc contains mainly functions for printing.
2926 * @param[in,out] tc is the tree context.
2927 */
2928static void
2929trm_print_yang_data(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2930{
2931 ly_bool once = 1;
2932
2933 for (struct trt_keyword_stmt ks = pc->fp.modify.next_yang_data(tc);
2934 !(TRP_KEYWORD_STMT_IS_EMPTY(ks));
2935 ks = pc->fp.modify.next_yang_data(tc)) {
2936 if (once) {
2937 ly_print_(pc->out, "\n");
2938 ly_print_(pc->out, "\n");
2939 once = 0;
2940 } else {
2941 ly_print_(pc->out, "\n");
2942 }
2943 trm_print_body_section(ks, pc, tc);
2944 }
2945}
2946
2947/**
2948 * @brief Print sections module, augment, rpcs, notifications, grouping, yang-data.
2949 * @param[in] pc contains mainly functions for printing.
2950 * @param[in,out] tc is the tree context.
2951 */
2952static void
2953trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
2954{
2955 trm_print_module_section(pc, tc);
2956
2957 trm_print_augmentations(pc, tc);
2958
2959 trm_print_rpcs(pc, tc);
2960
2961 trm_print_notifications(pc, tc);
2962
2963 trm_print_groupings(pc, tc);
2964
2965 trm_print_yang_data(pc, tc);
2966
2967 ly_print_(pc->out, "\n");
2968}
2969
2970/**
2971 * @brief Set default settings for trt_printer_ctx.
2972 *
2973 * Fill trt_printer_ctx so that it will contain all items correctly defined
2974 * except for max_line_length which is parameters of the printer tree module.
2975 *
2976 * @param[in] out is output handler.
2977 * @param[in] max_line_length is the maximum line length limit that should not be exceeded.
2978 * @param[in,out] ctx fill structure with default values.
2979 */
2980static void
2981trm_default_printer_ctx(struct ly_out *out, size_t max_line_length, struct trt_printer_ctx *ctx)
2982{
2983 ctx->out = out;
2984
2985 ctx->fp.modify = (struct trt_fp_modify_ctx) {
2986 .parent = tro_modi_parent,
2987 .first_sibling = tro_modi_first_sibling,
2988 .next_sibling = tro_modi_next_sibling,
2989 .next_child = tro_modi_next_child,
2990 .next_augment = tro_modi_next_augment,
2991 .get_rpcs = tro_modi_get_rpcs,
2992 .get_notifications = tro_modi_get_notifications,
2993 .next_grouping = tro_modi_next_grouping,
2994 .next_yang_data = tro_modi_next_yang_data
2995 };
2996
2997 ctx->fp.read = (struct trt_fp_read) {
2998 .module_name = tro_read_module_name,
2999 .node = tro_read_node,
3000 .if_sibling_exists = tro_read_if_sibling_exists
3001 };
3002
3003 ctx->fp.print = (struct trt_fp_print) {
3004 .print_features_names = tro_print_features_names,
3005 .print_keys = tro_print_keys
3006 };
3007
3008 ctx->max_line_length = max_line_length;
3009}
3010
3011/**
3012 * @brief Set default settings for trt_tree_ctx.
3013 *
3014 * Pointers to current nodes will be set to module data.
3015 *
3016 * @param[in] module is pointer to the YANG schema tree structures representing YANG module.
3017 * @param[in,out] tc fill structure with default values.
3018 */
3019static void
3020trm_default_tree_ctx(const struct lys_module *module, struct trt_tree_ctx *tc)
3021{
3022 tc->section = TRD_SECT_MODULE;
3023 tc->module = module;
3024 tc->pn = module->parsed->data;
3025 tc->tpn = module->parsed->data;
3026}
3027
3028/******************************************************************************
3029 * Definition of module interface
3030 *****************************************************************************/
3031
3032LY_ERR
3033tree_print_parsed_module(struct ly_out *out, const struct lys_module *module, uint32_t UNUSED(options), size_t line_length)
3034{
3035 struct trt_printer_ctx pc;
3036 struct trt_tree_ctx tc;
3037 struct ly_out *new_out;
3038 LY_ERR erc;
3039 struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);
3040
3041 if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
3042 return erc;
3043 }
3044
3045 line_length = line_length == 0 ? SIZE_MAX : line_length;
3046 trm_default_printer_ctx(new_out, line_length, &pc);
3047 trm_default_tree_ctx(module, &tc);
3048
3049 trm_print_sections(&pc, &tc);
3050
3051 ly_out_free(new_out, NULL, 1);
3052
3053 return clb_arg.last_error;
3054}
3055
3056LY_ERR
3057tree_print_submodule(struct ly_out *UNUSED(out), const struct lys_module *UNUSED(module), const struct lysp_submodule *UNUSED(submodp), uint32_t UNUSED(options), size_t UNUSED(line_length))
3058// LY_ERR tree_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options, size_t line_length)
3059{
3060 /** Not implemented right now. */
3061 return LY_SUCCESS;
3062}
3063
3064LY_ERR
3065tree_print_compiled_node(struct ly_out *UNUSED(out), const struct lysc_node *UNUSED(node), uint32_t UNUSED(options), size_t UNUSED(line_length))
3066// LY_ERR tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options, size_t line_length)
3067{
3068 /** Not implemented right now. */
3069 return LY_SUCCESS;
3070}