show.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 4
  18. #define JUN(flags) (flags & PPARSE_JUNIPER_SHOW)
  19. static void show_container(const struct lyd_node *node, size_t level, uint32_t flags);
  20. static void show_list(const struct lyd_node *node, size_t level, uint32_t flags);
  21. static void show_leaf(const struct lyd_node *node, size_t level, uint32_t flags);
  22. static void show_leaflist(const struct lyd_node *node, size_t level, uint32_t flags);
  23. static void show_node(const struct lyd_node *node, size_t level, uint32_t flags);
  24. static void show_subtree(const struct lyd_node *nodes_list, size_t level, uint32_t flags);
  25. static const char *get_value(const struct lyd_node *node)
  26. {
  27. const struct lysc_node *schema = NULL;
  28. const struct lysc_type *type = NULL;
  29. const struct lyd_value *value = NULL;
  30. if (!node)
  31. return NULL;
  32. schema = node->schema;
  33. if (!(schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  34. return NULL;
  35. if (schema->nodetype & LYS_LEAF)
  36. type = ((const struct lysc_node_leaf *)schema)->type;
  37. else
  38. type = ((const struct lysc_node_leaflist *)schema)->type;
  39. if (type->basetype != LY_TYPE_IDENT)
  40. return lyd_get_value(node);
  41. // Identity
  42. value = &((const struct lyd_node_term *)node)->value;
  43. return value->ident->name;
  44. }
  45. static void show_container(const struct lyd_node *node, size_t level, uint32_t flags)
  46. {
  47. if (!node)
  48. return;
  49. printf("%*s%s%s\n", (int)(level * LEVEL_SPACES_NUM), "",
  50. node->schema->name, JUN(flags) ? " {" : "");
  51. show_subtree(lyd_child(node), level + 1, flags);
  52. if (JUN(flags))
  53. printf("%*s%s\n", (int)(level * LEVEL_SPACES_NUM), "", "}");
  54. }
  55. static void show_list(const struct lyd_node *node, size_t level, uint32_t flags)
  56. {
  57. size_t keys_num = 0;
  58. bool_t with_stmt = BOOL_FALSE;
  59. const struct lyd_node *iter = NULL;
  60. if (!node)
  61. return;
  62. printf("%*s%s", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name);
  63. with_stmt = list_key_with_stmt(node->schema, flags);
  64. LY_LIST_FOR(lyd_child(node), iter) {
  65. if (!(iter->schema->nodetype & LYS_LEAF))
  66. continue;
  67. if (!(iter->schema->flags & LYS_KEY))
  68. continue;
  69. if (with_stmt)
  70. printf(" %s", iter->schema->name);
  71. printf(" %s", get_value(iter));
  72. }
  73. printf("%s\n", JUN(flags) ? " {" : "");
  74. show_subtree(lyd_child(node), level + 1, flags);
  75. if (JUN(flags))
  76. printf("%*s%s\n", (int)(level * LEVEL_SPACES_NUM), "", "}");
  77. }
  78. static void show_leaf(const struct lyd_node *node, size_t level, uint32_t flags)
  79. {
  80. struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
  81. if (!node)
  82. return;
  83. if (node->schema->flags & LYS_KEY)
  84. return;
  85. printf("%*s%s", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name);
  86. leaf = (struct lysc_node_leaf *)node->schema;
  87. if (leaf->type->basetype != LY_TYPE_EMPTY)
  88. printf(" %s", get_value(node));
  89. printf("%s\n", JUN(flags) ? ";" : "");
  90. }
  91. static void show_leaflist(const struct lyd_node *node, size_t level, uint32_t flags)
  92. {
  93. if (!node)
  94. return;
  95. printf("%*s%s %s%s\n", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name,
  96. get_value(node), JUN(flags) ? ";" : "");
  97. }
  98. static void show_node(const struct lyd_node *node, size_t level, uint32_t flags)
  99. {
  100. const struct lysc_node *schema = NULL;
  101. if (!node)
  102. return;
  103. if (node->flags & LYD_DEFAULT)
  104. return;
  105. schema = node->schema;
  106. if (!schema)
  107. return;
  108. if (!(schema->nodetype & SRP_NODETYPE_CONF))
  109. return;
  110. if (!(schema->flags & LYS_CONFIG_W))
  111. return;
  112. // Container
  113. if (schema->nodetype & LYS_CONTAINER) {
  114. show_container((const struct lyd_node *)node, level, flags);
  115. // List
  116. } else if (schema->nodetype & LYS_LIST) {
  117. show_list((const struct lyd_node *)node, level, flags);
  118. // Leaf
  119. } else if (schema->nodetype & LYS_LEAF) {
  120. show_leaf((const struct lyd_node *)node, level, flags);
  121. // Leaf-list
  122. } else if (schema->nodetype & LYS_LEAFLIST) {
  123. show_leaflist((const struct lyd_node *)node, level, flags);
  124. } else {
  125. return;
  126. }
  127. }
  128. static void show_sorted_list(faux_list_t *list, size_t level, uint32_t flags)
  129. {
  130. faux_list_node_t *iter = NULL;
  131. const struct lyd_node *lyd = NULL;
  132. if (!list)
  133. return;
  134. iter = faux_list_head(list);
  135. while ((lyd = (const struct lyd_node *)faux_list_each(&iter)))
  136. show_node(lyd, level, flags);
  137. }
  138. static char *list_keys_str(const struct lyd_node *node)
  139. {
  140. char *keys = NULL;
  141. const struct lyd_node *iter = NULL;
  142. if (!node)
  143. return NULL;
  144. if (node->schema->nodetype != LYS_LIST)
  145. return NULL;
  146. LY_LIST_FOR(lyd_child(node), iter) {
  147. if (!(iter->schema->nodetype & LYS_LEAF))
  148. continue;
  149. if (!(iter->schema->flags & LYS_KEY))
  150. continue;
  151. if (keys)
  152. faux_str_cat(&keys, " ");
  153. faux_str_cat(&keys, get_value(iter));
  154. }
  155. return keys;
  156. }
  157. static int list_compare(const void *first, const void *second)
  158. {
  159. int rc = 0;
  160. const struct lyd_node *f = (const struct lyd_node *)first;
  161. const struct lyd_node *s = (const struct lyd_node *)second;
  162. char *f_keys = list_keys_str(f);
  163. char *s_keys = list_keys_str(s);
  164. rc = faux_str_numcmp(f_keys, s_keys);
  165. faux_str_free(f_keys);
  166. faux_str_free(s_keys);
  167. return rc;
  168. }
  169. static int leaflist_compare(const void *first, const void *second)
  170. {
  171. const struct lyd_node *f = (const struct lyd_node *)first;
  172. const struct lyd_node *s = (const struct lyd_node *)second;
  173. return faux_str_numcmp(get_value(f), get_value(s));
  174. }
  175. static void show_subtree(const struct lyd_node *nodes_list, size_t level, uint32_t flags)
  176. {
  177. const struct lyd_node *iter = NULL;
  178. faux_list_t *list = NULL;
  179. const struct lysc_node *saved_lysc = NULL;
  180. if(!nodes_list)
  181. return;
  182. LY_LIST_FOR(nodes_list, iter) {
  183. if (saved_lysc) {
  184. if (saved_lysc == iter->schema) {
  185. faux_list_add(list, (void *)iter);
  186. continue;
  187. }
  188. show_sorted_list(list, level, flags);
  189. faux_list_free(list);
  190. list = NULL;
  191. saved_lysc = NULL;
  192. }
  193. if (((LYS_LIST == iter->schema->nodetype) ||
  194. (LYS_LEAFLIST == iter->schema->nodetype)) &&
  195. (iter->schema->flags & LYS_ORDBY_SYSTEM)) {
  196. saved_lysc = iter->schema;
  197. if (LYS_LIST == iter->schema->nodetype) {
  198. list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  199. list_compare, NULL, NULL);
  200. } else { // LEAFLIST
  201. list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  202. leaflist_compare, NULL, NULL);
  203. }
  204. faux_list_add(list, (void *)iter);
  205. continue;
  206. }
  207. show_node(iter, level, flags);
  208. }
  209. if (list) {
  210. show_sorted_list(list, level, flags);
  211. faux_list_free(list);
  212. }
  213. }
  214. bool_t show_xpath(sr_session_ctx_t *sess, const char *xpath, uint32_t flags)
  215. {
  216. sr_data_t *data = NULL;
  217. struct lyd_node *nodes_list = NULL;
  218. assert(sess);
  219. if (xpath) {
  220. if (sr_get_subtree(sess, xpath, 0, &data) != SR_ERR_OK)
  221. return BOOL_FALSE;
  222. nodes_list = lyd_child(data->tree);
  223. } else {
  224. if (sr_get_data(sess, "/*", 0, 0, 0, &data) != SR_ERR_OK)
  225. return BOOL_FALSE;
  226. nodes_list = data->tree;
  227. }
  228. show_subtree(nodes_list, 0, flags);
  229. sr_release_data(data);
  230. return BOOL_TRUE;
  231. }