show.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. }
  68. static void show_node(const struct lyd_node *node, size_t level, uint32_t flags)
  69. {
  70. const struct lysc_node *schema = NULL;
  71. if (!node)
  72. return;
  73. if (node->flags & LYD_DEFAULT)
  74. return;
  75. schema = node->schema;
  76. if (!schema)
  77. return;
  78. if (!(schema->nodetype & SRP_NODETYPE_CONF))
  79. return;
  80. if (!(schema->flags & LYS_CONFIG_W))
  81. return;
  82. // Container
  83. if (schema->nodetype & LYS_CONTAINER) {
  84. show_container((const struct lyd_node *)node, level, flags);
  85. // List
  86. } else if (schema->nodetype & LYS_LIST) {
  87. show_list((const struct lyd_node *)node, level, flags);
  88. // Leaf
  89. } else if (schema->nodetype & LYS_LEAF) {
  90. show_leaf((const struct lyd_node *)node, level, flags);
  91. // Leaf-list
  92. } else if (schema->nodetype & LYS_LEAFLIST) {
  93. show_leaflist((const struct lyd_node *)node, level, flags);
  94. } else {
  95. return;
  96. }
  97. }
  98. static void show_subtree(const struct lyd_node *nodes_list, size_t level, uint32_t flags)
  99. {
  100. const struct lyd_node *iter = NULL;
  101. if(!nodes_list)
  102. return;
  103. LY_LIST_FOR(nodes_list, iter) {
  104. show_node(iter, level, flags);
  105. }
  106. }
  107. bool_t show_xpath(sr_session_ctx_t *sess, const char *xpath, uint32_t flags)
  108. {
  109. sr_data_t *data = NULL;
  110. struct lyd_node *nodes_list = NULL;
  111. assert(sess);
  112. if (xpath) {
  113. if (sr_get_subtree(sess, xpath, 0, &data) != SR_ERR_OK)
  114. return BOOL_FALSE;
  115. nodes_list = lyd_child(data->tree);
  116. } else {
  117. if (sr_get_data(sess, "/*", 0, 0, 0, &data) != SR_ERR_OK)
  118. return BOOL_FALSE;
  119. nodes_list = data->tree;
  120. }
  121. show_subtree(nodes_list, 0, flags);
  122. sr_release_data(data);
  123. return BOOL_TRUE;
  124. }