show.c 7.6 KB

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