pline.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 pline_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. // Rollback is a mechanism to roll to previous node while
  129. // oneliners parsing
  130. bool_t rollback = BOOL_FALSE;
  131. do {
  132. pexpr_t *pexpr = pline_current_expr(pline);
  133. const char *str = (const char *)faux_argv_current(*arg);
  134. bool_t is_rollback = rollback;
  135. rollback = BOOL_FALSE;
  136. if (node && !is_rollback) {
  137. char *tmp = NULL;
  138. // Save rollback Xpath (for oneliners) before leaf node
  139. // Only leaf and leaf-list node allows to "rollback"
  140. // the path and add additional statements
  141. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  142. faux_str_free(rollback_xpath);
  143. rollback_xpath = faux_str_dup(pexpr->xpath);
  144. }
  145. // Add current node to Xpath
  146. tmp = faux_str_sprintf("/%s:%s",
  147. node->module->name, node->name);
  148. faux_str_cat(&pexpr->xpath, tmp);
  149. faux_str_free(tmp);
  150. // printf("%s\n", pexpr->xpath);
  151. // Activate current expression. Because it really has
  152. // new component
  153. pexpr->active = BOOL_TRUE;
  154. }
  155. if (!str)
  156. break;
  157. // Root of the module
  158. if (!node) {
  159. node = find_child(module->compiled->data, str);
  160. if (!node)
  161. return BOOL_FALSE;
  162. // Container
  163. } else if (node->nodetype & LYS_CONTAINER) {
  164. node = find_child(lysc_node_child(node), str);
  165. // List
  166. } else if (node->nodetype & LYS_LIST) {
  167. const struct lysc_node *iter = NULL;
  168. if (!is_rollback) {
  169. LY_LIST_FOR(lysc_node_child(node), iter) {
  170. char *tmp = NULL;
  171. struct lysc_node_leaf *leaf =
  172. (struct lysc_node_leaf *)iter;
  173. if (!(iter->nodetype & LYS_LEAF))
  174. continue;
  175. if (!(iter->flags & LYS_KEY))
  176. continue;
  177. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  178. tmp = faux_str_sprintf("[%s='%s']",
  179. leaf->name, str);
  180. faux_str_cat(&pexpr->xpath, tmp);
  181. faux_str_free(tmp);
  182. faux_argv_each(arg);
  183. str = (const char *)faux_argv_current(*arg);
  184. if (!str)
  185. break;
  186. }
  187. if (!str)
  188. break;
  189. }
  190. node = find_child(lysc_node_child(node), str);
  191. // Leaf
  192. } else if (node->nodetype & LYS_LEAF) {
  193. struct lysc_node_leaf *leaf =
  194. (struct lysc_node_leaf *)node;
  195. pexpr_t *new = NULL;
  196. if (leaf->type->basetype != LY_TYPE_EMPTY)
  197. pexpr->value = faux_str_dup(str);
  198. // Expression was completed
  199. // So rollback (for oneliners)
  200. node = node->parent;
  201. new = pexpr_new();
  202. new->xpath = faux_str_dup(rollback_xpath);
  203. faux_list_add(pline->exprs, new);
  204. rollback = BOOL_TRUE;
  205. // Leaf-list
  206. } else if (node->nodetype & LYS_LEAFLIST) {
  207. pexpr_t *new = NULL;
  208. char *tmp = NULL;
  209. tmp = faux_str_sprintf("[.='%s']", str);
  210. faux_str_cat(&pexpr->xpath, tmp);
  211. faux_str_free(tmp);
  212. // Expression was completed
  213. // So rollback (for oneliners)
  214. node = node->parent;
  215. new = pexpr_new();
  216. new->xpath = faux_str_dup(rollback_xpath);
  217. faux_list_add(pline->exprs, new);
  218. rollback = BOOL_TRUE;
  219. }
  220. faux_argv_each(arg);
  221. } while (node);
  222. faux_str_free(rollback_xpath);
  223. return BOOL_TRUE;
  224. }
  225. pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
  226. {
  227. struct lys_module *module = NULL;
  228. pline_t *pline = pline_new();
  229. uint32_t i = 0;
  230. faux_argv_node_t *arg = faux_argv_iter(argv);
  231. assert(ctx);
  232. if (!ctx)
  233. return NULL;
  234. // Iterate all modules
  235. i = 0;
  236. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  237. if (sr_module_is_internal(module))
  238. continue;
  239. if (!module->compiled)
  240. continue;
  241. if (!module->implemented)
  242. continue;
  243. if (!module->compiled->data)
  244. continue;
  245. if (pline_parse_module(module, &arg, pline))
  246. break; // Found
  247. }
  248. return pline;
  249. }