pline.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_add_compl(pline_t *pline,
  98. pcompl_type_e type, const struct lysc_node *node, char *xpath)
  99. {
  100. pcompl_t *pcompl = NULL;
  101. assert(pline);
  102. pcompl = pcompl_new();
  103. pcompl->type = type;
  104. pcompl->node = node;
  105. if (xpath)
  106. pcompl->xpath = faux_str_dup(xpath);
  107. faux_list_add(pline->compls, pcompl);
  108. }
  109. void pline_add_compl_subtree(pline_t *pline, const struct lysc_node *subtree)
  110. {
  111. const struct lysc_node *iter = NULL;
  112. assert(pline);
  113. if (!subtree)
  114. return;
  115. LY_LIST_FOR(subtree, iter) {
  116. if (!(iter->nodetype & NODETYPE_CONF))
  117. continue;
  118. if (!(iter->flags & LYS_CONFIG_W))
  119. continue;
  120. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  121. }
  122. }
  123. void pline_debug(pline_t *pline)
  124. {
  125. faux_list_node_t *iter = NULL;
  126. pexpr_t *pexpr = NULL;
  127. pcompl_t *pcompl = NULL;
  128. printf("=== Expressions:\n\n");
  129. iter = faux_list_head(pline->exprs);
  130. while (pexpr = (pexpr_t *)faux_list_each(&iter)) {
  131. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  132. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  133. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  134. printf("\n");
  135. }
  136. printf("=== Completions:\n\n");
  137. iter = faux_list_head(pline->compls);
  138. while (pcompl = (pcompl_t *)faux_list_each(&iter)) {
  139. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  140. "PCOMPL_NODE" : "PCOMPL_TYPE");
  141. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  142. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  143. printf("\n");
  144. }
  145. }
  146. static int
  147. sr_ly_module_is_internal(const struct lys_module *ly_mod);
  148. int
  149. sr_module_is_internal(const struct lys_module *ly_mod);
  150. // Don't use standard lys_find_child() because it checks given module to be
  151. // equal to found node's module. So augmented nodes will not be found.
  152. static const struct lysc_node *find_child(const struct lysc_node *node,
  153. const char *name)
  154. {
  155. const struct lysc_node *iter = NULL;
  156. if (!node)
  157. return NULL;
  158. LY_LIST_FOR(node, iter) {
  159. if (!(iter->nodetype & NODETYPE_CONF))
  160. continue;
  161. if (!(iter->flags & LYS_CONFIG_W))
  162. continue;
  163. if (!faux_str_cmp(iter->name, name))
  164. return iter;
  165. }
  166. return NULL;
  167. }
  168. bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  169. pline_t *pline)
  170. {
  171. faux_argv_node_t *arg = faux_argv_iter(argv);
  172. const struct lysc_node *node = NULL;
  173. char *rollback_xpath = NULL;
  174. // Rollback is a mechanism to roll to previous node while
  175. // oneliners parsing
  176. bool_t rollback = BOOL_FALSE;
  177. do {
  178. pexpr_t *pexpr = pline_current_expr(pline);
  179. const char *str = (const char *)faux_argv_current(arg);
  180. bool_t is_rollback = rollback;
  181. rollback = BOOL_FALSE;
  182. if (node && !is_rollback) {
  183. char *tmp = NULL;
  184. // Save rollback Xpath (for oneliners) before leaf node
  185. // Only leaf and leaf-list node allows to "rollback"
  186. // the path and add additional statements
  187. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  188. faux_str_free(rollback_xpath);
  189. rollback_xpath = faux_str_dup(pexpr->xpath);
  190. }
  191. // Add current node to Xpath
  192. tmp = faux_str_sprintf("/%s:%s",
  193. node->module->name, node->name);
  194. faux_str_cat(&pexpr->xpath, tmp);
  195. faux_str_free(tmp);
  196. // Activate current expression. Because it really has
  197. // new component
  198. pexpr->active = BOOL_TRUE;
  199. }
  200. // Root of the module
  201. if (!node) {
  202. // Completion
  203. if (!str) {
  204. pline_add_compl_subtree(pline,
  205. module->compiled->data);
  206. return BOOL_FALSE;
  207. }
  208. // Next element
  209. node = find_child(module->compiled->data, str);
  210. if (!node)
  211. return BOOL_FALSE;
  212. // Container
  213. } else if (node->nodetype & LYS_CONTAINER) {
  214. // Completion
  215. if (!str) {
  216. pline_add_compl_subtree(pline,
  217. lysc_node_child(node));
  218. break;
  219. }
  220. // Next element
  221. node = find_child(lysc_node_child(node), str);
  222. // List
  223. } else if (node->nodetype & LYS_LIST) {
  224. const struct lysc_node *iter = NULL;
  225. // Completion
  226. if (!str)
  227. break;
  228. // Next element
  229. if (!is_rollback) {
  230. LY_LIST_FOR(lysc_node_child(node), iter) {
  231. char *tmp = NULL;
  232. struct lysc_node_leaf *leaf =
  233. (struct lysc_node_leaf *)iter;
  234. if (!(iter->nodetype & LYS_LEAF))
  235. continue;
  236. if (!(iter->flags & LYS_KEY))
  237. continue;
  238. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  239. tmp = faux_str_sprintf("[%s='%s']",
  240. leaf->name, str);
  241. faux_str_cat(&pexpr->xpath, tmp);
  242. faux_str_free(tmp);
  243. faux_argv_each(&arg);
  244. str = (const char *)faux_argv_current(arg);
  245. if (!str)
  246. break;
  247. }
  248. if (!str)
  249. break;
  250. }
  251. node = find_child(lysc_node_child(node), str);
  252. // Leaf
  253. } else if (node->nodetype & LYS_LEAF) {
  254. struct lysc_node_leaf *leaf =
  255. (struct lysc_node_leaf *)node;
  256. // Completion
  257. if (!str)
  258. break;
  259. if (leaf->type->basetype != LY_TYPE_EMPTY)
  260. pexpr->value = faux_str_dup(str);
  261. // Expression was completed
  262. // So rollback (for oneliners)
  263. node = node->parent;
  264. pline_add_expr(pline, rollback_xpath);
  265. rollback = BOOL_TRUE;
  266. // Leaf-list
  267. } else if (node->nodetype & LYS_LEAFLIST) {
  268. char *tmp = NULL;
  269. // Completion
  270. if (!str)
  271. break;
  272. tmp = faux_str_sprintf("[.='%s']", str);
  273. faux_str_cat(&pexpr->xpath, tmp);
  274. faux_str_free(tmp);
  275. // Expression was completed
  276. // So rollback (for oneliners)
  277. node = node->parent;
  278. pline_add_expr(pline, rollback_xpath);
  279. rollback = BOOL_TRUE;
  280. }
  281. faux_argv_each(&arg);
  282. } while (node);
  283. faux_str_free(rollback_xpath);
  284. return BOOL_TRUE;
  285. }
  286. pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
  287. {
  288. struct lys_module *module = NULL;
  289. pline_t *pline = pline_new();
  290. uint32_t i = 0;
  291. assert(ctx);
  292. if (!ctx)
  293. return NULL;
  294. // Iterate all modules
  295. i = 0;
  296. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  297. if (sr_module_is_internal(module))
  298. continue;
  299. if (!module->compiled)
  300. continue;
  301. if (!module->implemented)
  302. continue;
  303. if (!module->compiled->data)
  304. continue;
  305. if (pline_parse_module(module, argv, pline))
  306. break; // Found
  307. }
  308. return pline;
  309. }