show.c 7.6 KB

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