pline.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /** @file pline.c
  2. */
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <sysrepo.h>
  9. #include <sysrepo/xpath.h>
  10. #include <libyang/tree_edit.h>
  11. #include <faux/faux.h>
  12. #include <faux/str.h>
  13. #include <faux/list.h>
  14. #include <faux/argv.h>
  15. #include "pline.h"
  16. #define NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST)
  17. pexpr_t *pexpr_new(void)
  18. {
  19. pexpr_t *pexpr = NULL;
  20. pexpr = faux_zmalloc(sizeof(*pexpr));
  21. assert(pexpr);
  22. if (!pexpr)
  23. return NULL;
  24. // Initialize
  25. pexpr->xpath = NULL;
  26. pexpr->value = NULL;
  27. pexpr->active = BOOL_FALSE;
  28. return pexpr;
  29. }
  30. void pexpr_free(pexpr_t *pexpr)
  31. {
  32. if (!pexpr)
  33. return;
  34. faux_str_free(pexpr->xpath);
  35. faux_str_free(pexpr->value);
  36. free(pexpr);
  37. }
  38. pcompl_t *pcompl_new(void)
  39. {
  40. pcompl_t *pcompl = NULL;
  41. pcompl = faux_zmalloc(sizeof(*pcompl));
  42. assert(pcompl);
  43. if (!pcompl)
  44. return NULL;
  45. // Initialize
  46. pcompl->type = PCOMPL_NODE;
  47. pcompl->node = NULL;
  48. pcompl->xpath = NULL;
  49. return pcompl;
  50. }
  51. void pcompl_free(pcompl_t *pcompl)
  52. {
  53. if (!pcompl)
  54. return;
  55. faux_str_free(pcompl->xpath);
  56. free(pcompl);
  57. }
  58. pline_t *pline_new(void)
  59. {
  60. pline_t *pline = NULL;
  61. pline = faux_zmalloc(sizeof(*pline));
  62. assert(pline);
  63. if (!pline)
  64. return NULL;
  65. // Init
  66. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  67. NULL, NULL, (faux_list_free_fn)pexpr_free);
  68. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  69. NULL, NULL, (faux_list_free_fn)pcompl_free);
  70. return pline;
  71. }
  72. void pline_free(pline_t *pline)
  73. {
  74. LY_ARRAY_COUNT_TYPE u = 0;
  75. if (!pline)
  76. return;
  77. faux_list_free(pline->exprs);
  78. faux_list_free(pline->compls);
  79. faux_free(pline);
  80. }
  81. pexpr_t *pline_current_expr(pline_t *pline)
  82. {
  83. assert(pline);
  84. if (faux_list_len(pline->exprs) == 0)
  85. faux_list_add(pline->exprs, pexpr_new());
  86. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  87. }
  88. void pline_debug(pline_t *pline)
  89. {
  90. faux_list_node_t *iter = NULL;
  91. pexpr_t *pexpr = NULL;
  92. pcompl_t *pcompl = NULL;
  93. iter = faux_list_head(pline->exprs);
  94. while (pexpr = (pexpr_t *)faux_list_each(&iter)) {
  95. printf("\n");
  96. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  97. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  98. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  99. }
  100. }
  101. static int
  102. sr_ly_module_is_internal(const struct lys_module *ly_mod);
  103. int
  104. sr_module_is_internal(const struct lys_module *ly_mod);
  105. // Don't use standard lys_find_child() because it checks given module to be
  106. // equal to found node's module. So augmented nodes will not be found.
  107. static const struct lysc_node *find_child(const struct lysc_node *node,
  108. const char *name)
  109. {
  110. const struct lysc_node *iter = NULL;
  111. if (!node)
  112. return NULL;
  113. LY_LIST_FOR(node, iter) {
  114. if (!(iter->nodetype & NODETYPE_CONF))
  115. continue;
  116. if (!(iter->flags & LYS_CONFIG_W))
  117. continue;
  118. if (!faux_str_cmp(iter->name, name))
  119. return iter;
  120. }
  121. return NULL;
  122. }
  123. bool_t parse_module(const struct lys_module *module, faux_argv_node_t **arg,
  124. pline_t *pline)
  125. {
  126. const struct lysc_node *node = NULL;
  127. char *rollback_xpath = NULL;
  128. bool_t rollback = BOOL_FALSE;
  129. do {
  130. pexpr_t *pexpr = pline_current_expr(pline);
  131. const char *str = (const char *)faux_argv_current(*arg);
  132. bool_t is_rollback = rollback;
  133. rollback = BOOL_FALSE;
  134. if (node && !is_rollback) {
  135. char *tmp = NULL;
  136. // Save rollback Xpath (for oneliners) before leaf node
  137. // Only leaf node allows to "rollback"
  138. // the path and add additional statements
  139. if (node->nodetype & LYS_LEAF) {
  140. faux_str_free(rollback_xpath);
  141. rollback_xpath = faux_str_dup(pexpr->xpath);
  142. }
  143. // Add current node to Xpath
  144. tmp = faux_str_sprintf("/%s:%s",
  145. node->module->name, node->name);
  146. faux_str_cat(&pexpr->xpath, tmp);
  147. faux_str_free(tmp);
  148. printf("%s\n", pexpr->xpath);
  149. // Activate current expression. Because it really has
  150. // new component
  151. pexpr->active = BOOL_TRUE;
  152. }
  153. printf("for str = %s\n", str);
  154. if (!str)
  155. break;
  156. // Root of the module
  157. if (!node) {
  158. printf("Module\n");
  159. node = find_child(module->compiled->data, str);
  160. if (!node)
  161. return BOOL_FALSE;
  162. // continue; // Don't get next arg
  163. // Container
  164. } else if (node->nodetype & LYS_CONTAINER) {
  165. printf("Container\n");
  166. node = find_child(lysc_node_child(node), str);
  167. // List
  168. } else if (node->nodetype & LYS_LIST) {
  169. printf("List\n");
  170. const struct lysc_node *iter = NULL;
  171. printf("str = %s\n", str);
  172. if (!is_rollback) {
  173. LY_LIST_FOR(lysc_node_child(node), iter) {
  174. char *tmp = NULL;
  175. struct lysc_node_leaf *leaf =
  176. (struct lysc_node_leaf *)iter;
  177. if (!(iter->nodetype & LYS_LEAF))
  178. continue;
  179. if (!(iter->flags & LYS_KEY))
  180. continue;
  181. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  182. tmp = faux_str_sprintf("[%s='%s']",
  183. leaf->name, str);
  184. faux_str_cat(&pexpr->xpath, tmp);
  185. faux_str_free(tmp);
  186. printf("%s\n", pexpr->xpath);
  187. faux_argv_each(arg);
  188. str = (const char *)faux_argv_current(*arg);
  189. printf("list str = %s\n", str);
  190. if (!str)
  191. break;
  192. }
  193. if (!str)
  194. break;
  195. }
  196. node = find_child(lysc_node_child(node), str);
  197. printf("list next node = %s\n", node ? node->name : "NULL");
  198. // Leaf
  199. } else if (node->nodetype & LYS_LEAF) {
  200. printf("Leaf\n");
  201. struct lysc_node_leaf *leaf =
  202. (struct lysc_node_leaf *)node;
  203. pexpr_t *new = NULL;
  204. if (leaf->type->basetype != LY_TYPE_EMPTY) {
  205. pexpr->value = faux_str_dup(str);
  206. printf("value=%s\n", pexpr->value);
  207. }
  208. // Expression was completed
  209. // So rollback (for oneliners)
  210. node = node->parent;
  211. new = pexpr_new();
  212. new->xpath = faux_str_dup(rollback_xpath);
  213. faux_list_add(pline->exprs, new);
  214. rollback = BOOL_TRUE;
  215. }
  216. faux_argv_each(arg);
  217. } while (node);
  218. faux_str_free(rollback_xpath);
  219. return BOOL_TRUE;
  220. }
  221. /*
  222. bool_t parse_tree(const struct lysc_node *tree, faux_argv_node_t **arg,
  223. pline_t *pline)
  224. {
  225. const struct lysc_node *node = NULL;
  226. const char *str = (const char *)faux_argv_current(*arg);
  227. node = find_child(tree, str);
  228. if (node) {
  229. parse_node(node, arg, pline);
  230. return BOOL_TRUE;
  231. }
  232. return BOOL_FALSE;
  233. }
  234. */
  235. pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
  236. {
  237. struct lys_module *module = NULL;
  238. pline_t *pline = pline_new();
  239. uint32_t i = 0;
  240. faux_argv_node_t *arg = faux_argv_iter(argv);
  241. assert(ctx);
  242. if (!ctx)
  243. return NULL;
  244. // Iterate all modules
  245. i = 0;
  246. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  247. if (sr_module_is_internal(module))
  248. continue;
  249. if (!module->compiled)
  250. continue;
  251. if (!module->implemented)
  252. continue;
  253. if (!module->compiled->data)
  254. continue;
  255. if (parse_module(module, &arg, pline))
  256. break; // Found
  257. }
  258. return pline;
  259. }