pline.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_add_expr(pline_t *pline, const char *xpath)
  82. {
  83. pexpr_t *pexpr = NULL;
  84. assert(pline);
  85. pexpr = pexpr_new();
  86. if (xpath)
  87. pexpr->xpath = faux_str_dup(xpath);
  88. faux_list_add(pline->exprs, pexpr);
  89. }
  90. pexpr_t *pline_current_expr(pline_t *pline)
  91. {
  92. assert(pline);
  93. if (faux_list_len(pline->exprs) == 0)
  94. pline_add_expr(pline, NULL);
  95. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  96. }
  97. void pline_debug(pline_t *pline)
  98. {
  99. faux_list_node_t *iter = NULL;
  100. pexpr_t *pexpr = NULL;
  101. pcompl_t *pcompl = NULL;
  102. iter = faux_list_head(pline->exprs);
  103. while (pexpr = (pexpr_t *)faux_list_each(&iter)) {
  104. printf("\n");
  105. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  106. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  107. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  108. }
  109. }
  110. static int
  111. sr_ly_module_is_internal(const struct lys_module *ly_mod);
  112. int
  113. sr_module_is_internal(const struct lys_module *ly_mod);
  114. // Don't use standard lys_find_child() because it checks given module to be
  115. // equal to found node's module. So augmented nodes will not be found.
  116. static const struct lysc_node *find_child(const struct lysc_node *node,
  117. const char *name)
  118. {
  119. const struct lysc_node *iter = NULL;
  120. if (!node)
  121. return NULL;
  122. LY_LIST_FOR(node, iter) {
  123. if (!(iter->nodetype & NODETYPE_CONF))
  124. continue;
  125. if (!(iter->flags & LYS_CONFIG_W))
  126. continue;
  127. if (!faux_str_cmp(iter->name, name))
  128. return iter;
  129. }
  130. return NULL;
  131. }
  132. bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  133. pline_t *pline)
  134. {
  135. faux_argv_node_t *arg = faux_argv_iter(argv);
  136. const struct lysc_node *node = NULL;
  137. char *rollback_xpath = NULL;
  138. // Rollback is a mechanism to roll to previous node while
  139. // oneliners parsing
  140. bool_t rollback = BOOL_FALSE;
  141. do {
  142. pexpr_t *pexpr = pline_current_expr(pline);
  143. const char *str = (const char *)faux_argv_current(arg);
  144. bool_t is_rollback = rollback;
  145. rollback = BOOL_FALSE;
  146. if (node && !is_rollback) {
  147. char *tmp = NULL;
  148. // Save rollback Xpath (for oneliners) before leaf node
  149. // Only leaf and leaf-list node allows to "rollback"
  150. // the path and add additional statements
  151. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  152. faux_str_free(rollback_xpath);
  153. rollback_xpath = faux_str_dup(pexpr->xpath);
  154. }
  155. // Add current node to Xpath
  156. tmp = faux_str_sprintf("/%s:%s",
  157. node->module->name, node->name);
  158. faux_str_cat(&pexpr->xpath, tmp);
  159. faux_str_free(tmp);
  160. // Activate current expression. Because it really has
  161. // new component
  162. pexpr->active = BOOL_TRUE;
  163. }
  164. if (!str)
  165. break;
  166. // Root of the module
  167. if (!node) {
  168. node = find_child(module->compiled->data, str);
  169. if (!node)
  170. return BOOL_FALSE;
  171. // Container
  172. } else if (node->nodetype & LYS_CONTAINER) {
  173. node = find_child(lysc_node_child(node), str);
  174. // List
  175. } else if (node->nodetype & LYS_LIST) {
  176. const struct lysc_node *iter = NULL;
  177. if (!is_rollback) {
  178. LY_LIST_FOR(lysc_node_child(node), iter) {
  179. char *tmp = NULL;
  180. struct lysc_node_leaf *leaf =
  181. (struct lysc_node_leaf *)iter;
  182. if (!(iter->nodetype & LYS_LEAF))
  183. continue;
  184. if (!(iter->flags & LYS_KEY))
  185. continue;
  186. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  187. tmp = faux_str_sprintf("[%s='%s']",
  188. leaf->name, str);
  189. faux_str_cat(&pexpr->xpath, tmp);
  190. faux_str_free(tmp);
  191. faux_argv_each(&arg);
  192. str = (const char *)faux_argv_current(arg);
  193. if (!str)
  194. break;
  195. }
  196. if (!str)
  197. break;
  198. }
  199. node = find_child(lysc_node_child(node), str);
  200. // Leaf
  201. } else if (node->nodetype & LYS_LEAF) {
  202. struct lysc_node_leaf *leaf =
  203. (struct lysc_node_leaf *)node;
  204. if (leaf->type->basetype != LY_TYPE_EMPTY)
  205. pexpr->value = faux_str_dup(str);
  206. // Expression was completed
  207. // So rollback (for oneliners)
  208. node = node->parent;
  209. pline_add_expr(pline, rollback_xpath);
  210. rollback = BOOL_TRUE;
  211. // Leaf-list
  212. } else if (node->nodetype & LYS_LEAFLIST) {
  213. char *tmp = NULL;
  214. tmp = faux_str_sprintf("[.='%s']", str);
  215. faux_str_cat(&pexpr->xpath, tmp);
  216. faux_str_free(tmp);
  217. // Expression was completed
  218. // So rollback (for oneliners)
  219. node = node->parent;
  220. pline_add_expr(pline, rollback_xpath);
  221. rollback = BOOL_TRUE;
  222. }
  223. faux_argv_each(&arg);
  224. } while (node);
  225. faux_str_free(rollback_xpath);
  226. return BOOL_TRUE;
  227. }
  228. pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
  229. {
  230. struct lys_module *module = NULL;
  231. pline_t *pline = pline_new();
  232. uint32_t i = 0;
  233. assert(ctx);
  234. if (!ctx)
  235. return NULL;
  236. // Iterate all modules
  237. i = 0;
  238. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  239. if (sr_module_is_internal(module))
  240. continue;
  241. if (!module->compiled)
  242. continue;
  243. if (!module->implemented)
  244. continue;
  245. if (!module->compiled->data)
  246. continue;
  247. if (pline_parse_module(module, argv, pline))
  248. break; // Found
  249. }
  250. return pline;
  251. }