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