show.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <faux/faux.h>
  7. #include <faux/str.h>
  8. #include <faux/list.h>
  9. #include <faux/argv.h>
  10. #include <sysrepo.h>
  11. #include <sysrepo/xpath.h>
  12. #include <sysrepo/values.h>
  13. #include <libyang/tree_edit.h>
  14. #include "sr_copypaste.h"
  15. #include "pline.h"
  16. #include "show.h"
  17. #define LEVEL_SPACES_NUM 2
  18. static void show_container(const struct lyd_node *node, size_t level, uint32_t flags);
  19. static void show_list(const struct lyd_node *node, size_t level, uint32_t flags);
  20. static void show_leaf(const struct lyd_node *node, size_t level, uint32_t flags);
  21. static void show_leaflist(const struct lyd_node *node, size_t level, uint32_t flags);
  22. static void show_node(const struct lyd_node *node, size_t level, uint32_t flags);
  23. static void show_subtree(const struct lyd_node *nodes_list, size_t level, uint32_t flags);
  24. static void show_container(const struct lyd_node *node, size_t level, uint32_t flags)
  25. {
  26. if (!node)
  27. return;
  28. printf("%*s%s\n", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name);
  29. show_subtree(lyd_child(node), level + 1, flags);
  30. }
  31. static void show_list(const struct lyd_node *node, size_t level, uint32_t flags)
  32. {
  33. size_t keys_num = 0;
  34. bool_t with_stmt = BOOL_FALSE;
  35. const struct lyd_node *iter = NULL;
  36. if (!node)
  37. return;
  38. printf("%*s%s", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name);
  39. with_stmt = list_key_with_stmt(node->schema, flags);
  40. LY_LIST_FOR(lyd_child(node), iter) {
  41. if (!(iter->schema->nodetype & LYS_LEAF))
  42. continue;
  43. if (!(iter->schema->flags & LYS_KEY))
  44. continue;
  45. if (with_stmt)
  46. printf(" %s", iter->schema->name);
  47. printf(" %s", lyd_get_value(iter));
  48. }
  49. printf("\n");
  50. show_subtree(lyd_child(node), level + 1, flags);
  51. }
  52. static void show_leaf(const struct lyd_node *node, size_t level, uint32_t flags)
  53. {
  54. struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
  55. if (!node)
  56. return;
  57. if (node->schema->flags & LYS_KEY)
  58. return;
  59. printf("%*s%s", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name);
  60. leaf = (struct lysc_node_leaf *)node->schema;
  61. if (leaf->type->basetype != LY_TYPE_EMPTY)
  62. printf(" %s", lyd_get_value(node));
  63. printf("\n");
  64. }
  65. static void show_leaflist(const struct lyd_node *node, size_t level, uint32_t flags)
  66. {
  67. if (!node)
  68. return;
  69. printf("%*s%s %s\n", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name,
  70. lyd_get_value(node));
  71. }
  72. static void show_node(const struct lyd_node *node, size_t level, uint32_t flags)
  73. {
  74. const struct lysc_node *schema = NULL;
  75. if (!node)
  76. return;
  77. if (node->flags & LYD_DEFAULT)
  78. return;
  79. schema = node->schema;
  80. if (!schema)
  81. return;
  82. if (!(schema->nodetype & SRP_NODETYPE_CONF))
  83. return;
  84. if (!(schema->flags & LYS_CONFIG_W))
  85. return;
  86. // Container
  87. if (schema->nodetype & LYS_CONTAINER) {
  88. show_container((const struct lyd_node *)node, level, flags);
  89. // List
  90. } else if (schema->nodetype & LYS_LIST) {
  91. show_list((const struct lyd_node *)node, level, flags);
  92. // Leaf
  93. } else if (schema->nodetype & LYS_LEAF) {
  94. show_leaf((const struct lyd_node *)node, level, flags);
  95. // Leaf-list
  96. } else if (schema->nodetype & LYS_LEAFLIST) {
  97. show_leaflist((const struct lyd_node *)node, level, flags);
  98. } else {
  99. return;
  100. }
  101. }
  102. static void show_subtree(const struct lyd_node *nodes_list, size_t level, uint32_t flags)
  103. {
  104. const struct lyd_node *iter = NULL;
  105. if(!nodes_list)
  106. return;
  107. LY_LIST_FOR(nodes_list, iter) {
  108. show_node(iter, level, flags);
  109. }
  110. }
  111. bool_t show_xpath(sr_session_ctx_t *sess, const char *xpath, uint32_t flags)
  112. {
  113. sr_data_t *data = NULL;
  114. struct lyd_node *nodes_list = NULL;
  115. assert(sess);
  116. if (xpath) {
  117. if (sr_get_subtree(sess, xpath, 0, &data) != SR_ERR_OK)
  118. return BOOL_FALSE;
  119. nodes_list = lyd_child(data->tree);
  120. } else {
  121. if (sr_get_data(sess, "/*", 0, 0, 0, &data) != SR_ERR_OK)
  122. return BOOL_FALSE;
  123. nodes_list = data->tree;
  124. }
  125. show_subtree(nodes_list, 0, flags);
  126. sr_release_data(data);
  127. return BOOL_TRUE;
  128. }