show.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_sorted_list(faux_list_t *list, size_t level, uint32_t flags)
  103. {
  104. faux_list_node_t *iter = NULL;
  105. const struct lyd_node *lyd = NULL;
  106. if (!list)
  107. return;
  108. iter = faux_list_head(list);
  109. while ((lyd = (const struct lyd_node *)faux_list_each(&iter)))
  110. show_node(lyd, level, flags);
  111. }
  112. static char *list_keys_str(const struct lyd_node *node)
  113. {
  114. char *keys = NULL;
  115. const struct lyd_node *iter = NULL;
  116. if (!node)
  117. return NULL;
  118. if (node->schema->nodetype != LYS_LIST)
  119. return NULL;
  120. LY_LIST_FOR(lyd_child(node), iter) {
  121. if (!(iter->schema->nodetype & LYS_LEAF))
  122. continue;
  123. if (!(iter->schema->flags & LYS_KEY))
  124. continue;
  125. if (keys)
  126. faux_str_cat(&keys, " ");
  127. faux_str_cat(&keys, lyd_get_value(iter));
  128. }
  129. return keys;
  130. }
  131. static int list_compare(const void *first, const void *second)
  132. {
  133. int rc = 0;
  134. const struct lyd_node *f = (const struct lyd_node *)first;
  135. const struct lyd_node *s = (const struct lyd_node *)second;
  136. char *f_keys = list_keys_str(f);
  137. char *s_keys = list_keys_str(s);
  138. rc = faux_str_numcmp(f_keys, s_keys);
  139. faux_str_free(f_keys);
  140. faux_str_free(s_keys);
  141. return rc;
  142. }
  143. static int leaflist_compare(const void *first, const void *second)
  144. {
  145. const struct lyd_node *f = (const struct lyd_node *)first;
  146. const struct lyd_node *s = (const struct lyd_node *)second;
  147. return faux_str_numcmp(lyd_get_value(f), lyd_get_value(s));
  148. }
  149. static void show_subtree(const struct lyd_node *nodes_list, size_t level, uint32_t flags)
  150. {
  151. const struct lyd_node *iter = NULL;
  152. faux_list_t *list = NULL;
  153. const struct lysc_node *saved_lysc = NULL;
  154. if(!nodes_list)
  155. return;
  156. LY_LIST_FOR(nodes_list, iter) {
  157. if (saved_lysc) {
  158. if (saved_lysc == iter->schema) {
  159. faux_list_add(list, (void *)iter);
  160. continue;
  161. }
  162. show_sorted_list(list, level, flags);
  163. faux_list_free(list);
  164. list = NULL;
  165. saved_lysc = NULL;
  166. }
  167. if (((LYS_LIST == iter->schema->nodetype) ||
  168. (LYS_LEAFLIST == iter->schema->nodetype)) &&
  169. (iter->schema->flags & LYS_ORDBY_SYSTEM)) {
  170. saved_lysc = iter->schema;
  171. if (LYS_LIST == iter->schema->nodetype) {
  172. list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  173. list_compare, NULL, NULL);
  174. } else { // LEAFLIST
  175. list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  176. leaflist_compare, NULL, NULL);
  177. }
  178. faux_list_add(list, (void *)iter);
  179. continue;
  180. }
  181. show_node(iter, level, flags);
  182. }
  183. if (list) {
  184. show_sorted_list(list, level, flags);
  185. faux_list_free(list);
  186. }
  187. }
  188. bool_t show_xpath(sr_session_ctx_t *sess, const char *xpath, uint32_t flags)
  189. {
  190. sr_data_t *data = NULL;
  191. struct lyd_node *nodes_list = NULL;
  192. assert(sess);
  193. if (xpath) {
  194. if (sr_get_subtree(sess, xpath, 0, &data) != SR_ERR_OK)
  195. return BOOL_FALSE;
  196. nodes_list = lyd_child(data->tree);
  197. } else {
  198. if (sr_get_data(sess, "/*", 0, 0, 0, &data) != SR_ERR_OK)
  199. return BOOL_FALSE;
  200. nodes_list = data->tree;
  201. }
  202. show_subtree(nodes_list, 0, flags);
  203. sr_release_data(data);
  204. return BOOL_TRUE;
  205. }