pline.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. if (!pline)
  75. return;
  76. faux_list_free(pline->exprs);
  77. faux_list_free(pline->compls);
  78. faux_free(pline);
  79. }
  80. pexpr_t *pline_add_expr(pline_t *pline, const char *xpath)
  81. {
  82. pexpr_t *pexpr = NULL;
  83. assert(pline);
  84. pexpr = pexpr_new();
  85. if (xpath)
  86. pexpr->xpath = faux_str_dup(xpath);
  87. faux_list_add(pline->exprs, pexpr);
  88. }
  89. pexpr_t *pline_current_expr(pline_t *pline)
  90. {
  91. assert(pline);
  92. if (faux_list_len(pline->exprs) == 0)
  93. pline_add_expr(pline, NULL);
  94. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  95. }
  96. void pline_add_compl(pline_t *pline,
  97. pcompl_type_e type, const struct lysc_node *node, char *xpath)
  98. {
  99. pcompl_t *pcompl = NULL;
  100. assert(pline);
  101. pcompl = pcompl_new();
  102. pcompl->type = type;
  103. pcompl->node = node;
  104. if (xpath)
  105. pcompl->xpath = faux_str_dup(xpath);
  106. faux_list_add(pline->compls, pcompl);
  107. }
  108. void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
  109. const struct lysc_node *node)
  110. {
  111. const struct lysc_node *subtree = NULL;
  112. const struct lysc_node *iter = NULL;
  113. assert(pline);
  114. assert(module);
  115. if (node)
  116. subtree = lysc_node_child(node);
  117. else
  118. subtree = module->compiled->data;
  119. LY_LIST_FOR(subtree, iter) {
  120. if (!(iter->nodetype & NODETYPE_CONF))
  121. continue;
  122. if (!(iter->flags & LYS_CONFIG_W))
  123. continue;
  124. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  125. }
  126. }
  127. void pline_debug(pline_t *pline)
  128. {
  129. faux_list_node_t *iter = NULL;
  130. pexpr_t *pexpr = NULL;
  131. pcompl_t *pcompl = NULL;
  132. printf("=== Expressions:\n\n");
  133. iter = faux_list_head(pline->exprs);
  134. while (pexpr = (pexpr_t *)faux_list_each(&iter)) {
  135. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  136. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  137. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  138. printf("\n");
  139. }
  140. printf("=== Completions:\n\n");
  141. iter = faux_list_head(pline->compls);
  142. while (pcompl = (pcompl_t *)faux_list_each(&iter)) {
  143. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  144. "PCOMPL_NODE" : "PCOMPL_TYPE");
  145. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  146. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  147. printf("\n");
  148. }
  149. }
  150. static int
  151. sr_ly_module_is_internal(const struct lys_module *ly_mod);
  152. int
  153. sr_module_is_internal(const struct lys_module *ly_mod);
  154. // Don't use standard lys_find_child() because it checks given module to be
  155. // equal to found node's module. So augmented nodes will not be found.
  156. static const struct lysc_node *find_child(const struct lysc_node *node,
  157. const char *name)
  158. {
  159. const struct lysc_node *iter = NULL;
  160. if (!node)
  161. return NULL;
  162. LY_LIST_FOR(node, iter) {
  163. if (!(iter->nodetype & NODETYPE_CONF))
  164. continue;
  165. if (!(iter->flags & LYS_CONFIG_W))
  166. continue;
  167. if (!faux_str_cmp(iter->name, name))
  168. return iter;
  169. }
  170. return NULL;
  171. }
  172. bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  173. pline_t *pline)
  174. {
  175. faux_argv_node_t *arg = faux_argv_iter(argv);
  176. const struct lysc_node *node = NULL;
  177. char *rollback_xpath = NULL;
  178. // Rollback is a mechanism to roll to previous node while
  179. // oneliners parsing
  180. bool_t rollback = BOOL_FALSE;
  181. do {
  182. pexpr_t *pexpr = pline_current_expr(pline);
  183. const char *str = (const char *)faux_argv_current(arg);
  184. bool_t is_rollback = rollback;
  185. bool_t next_arg = BOOL_TRUE;
  186. rollback = BOOL_FALSE;
  187. if (node && !is_rollback) {
  188. char *tmp = NULL;
  189. // Save rollback Xpath (for oneliners) before leaf node
  190. // Only leaf and leaf-list node allows to "rollback"
  191. // the path and add additional statements
  192. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  193. faux_str_free(rollback_xpath);
  194. rollback_xpath = faux_str_dup(pexpr->xpath);
  195. }
  196. // Add current node to Xpath
  197. tmp = faux_str_sprintf("/%s:%s",
  198. node->module->name, node->name);
  199. faux_str_cat(&pexpr->xpath, tmp);
  200. faux_str_free(tmp);
  201. // Activate current expression. Because it really has
  202. // new component
  203. pexpr->active = BOOL_TRUE;
  204. }
  205. // Root of the module
  206. if (!node) {
  207. // Completion
  208. if (!str) {
  209. pline_add_compl_subtree(pline, module, node);
  210. return BOOL_FALSE;
  211. }
  212. // Next element
  213. node = find_child(module->compiled->data, str);
  214. if (!node)
  215. return BOOL_FALSE;
  216. // Container
  217. } else if (node->nodetype & LYS_CONTAINER) {
  218. // Completion
  219. if (!str) {
  220. pline_add_compl_subtree(pline, module, node);
  221. break;
  222. }
  223. // Next element
  224. node = find_child(lysc_node_child(node), str);
  225. // List
  226. } else if (node->nodetype & LYS_LIST) {
  227. const struct lysc_node *iter = NULL;
  228. // Next element
  229. if (!is_rollback) {
  230. bool_t break_upper_loop = BOOL_FALSE;
  231. LY_LIST_FOR(lysc_node_child(node), iter) {
  232. char *tmp = NULL;
  233. struct lysc_node_leaf *leaf =
  234. (struct lysc_node_leaf *)iter;
  235. if (!(iter->nodetype & LYS_LEAF))
  236. continue;
  237. if (!(iter->flags & LYS_KEY))
  238. continue;
  239. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  240. // Completion
  241. if (!str) {
  242. char *tmp = NULL;
  243. tmp = faux_str_sprintf("%s/%s",
  244. pexpr->xpath, leaf->name);
  245. pline_add_compl(pline,
  246. PCOMPL_TYPE, iter, tmp);
  247. faux_str_free(tmp);
  248. break_upper_loop = BOOL_TRUE;
  249. break;
  250. }
  251. tmp = faux_str_sprintf("[%s='%s']",
  252. leaf->name, str);
  253. faux_str_cat(&pexpr->xpath, tmp);
  254. faux_str_free(tmp);
  255. faux_argv_each(&arg);
  256. str = (const char *)faux_argv_current(arg);
  257. }
  258. if (break_upper_loop)
  259. break;
  260. }
  261. // Completion
  262. if (!str) {
  263. pline_add_compl_subtree(pline, module, node);
  264. break;
  265. }
  266. // Next element
  267. node = find_child(lysc_node_child(node), str);
  268. // Leaf
  269. } else if (node->nodetype & LYS_LEAF) {
  270. struct lysc_node_leaf *leaf =
  271. (struct lysc_node_leaf *)node;
  272. printf("node=%s, str=%s\n", node->name, str);
  273. // Next element
  274. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  275. if (!str) {
  276. pline_add_compl_subtree(pline,
  277. module, node->parent);
  278. break;
  279. }
  280. // Don't get next argument when argument is not
  281. // really consumed
  282. next_arg = BOOL_FALSE;
  283. } else {
  284. // Completion
  285. if (!str) {
  286. pline_add_compl(pline,
  287. PCOMPL_TYPE, node, NULL);
  288. break;
  289. }
  290. pexpr->value = faux_str_dup(str);
  291. }
  292. // Expression was completed
  293. // So rollback (for oneliners)
  294. node = node->parent;
  295. pline_add_expr(pline, rollback_xpath);
  296. rollback = BOOL_TRUE;
  297. // Leaf-list
  298. } else if (node->nodetype & LYS_LEAFLIST) {
  299. char *tmp = NULL;
  300. // Completion
  301. if (!str) {
  302. pline_add_compl(pline,
  303. PCOMPL_TYPE, node, pexpr->xpath);
  304. break;
  305. }
  306. tmp = faux_str_sprintf("[.='%s']", str);
  307. faux_str_cat(&pexpr->xpath, tmp);
  308. faux_str_free(tmp);
  309. // Expression was completed
  310. // So rollback (for oneliners)
  311. node = node->parent;
  312. pline_add_expr(pline, rollback_xpath);
  313. rollback = BOOL_TRUE;
  314. }
  315. if (next_arg)
  316. faux_argv_each(&arg);
  317. } while (node || rollback);
  318. faux_str_free(rollback_xpath);
  319. return BOOL_TRUE;
  320. }
  321. pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
  322. {
  323. struct lys_module *module = NULL;
  324. pline_t *pline = pline_new();
  325. uint32_t i = 0;
  326. assert(ctx);
  327. if (!ctx)
  328. return NULL;
  329. // Iterate all modules
  330. i = 0;
  331. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  332. if (sr_module_is_internal(module))
  333. continue;
  334. if (!module->compiled)
  335. continue;
  336. if (!module->implemented)
  337. continue;
  338. if (!module->compiled->data)
  339. continue;
  340. if (pline_parse_module(module, argv, pline))
  341. break; // Found
  342. }
  343. return pline;
  344. }
  345. static void identityref(struct lysc_ident *ident)
  346. {
  347. LY_ARRAY_COUNT_TYPE u = 0;
  348. if (!ident)
  349. return;
  350. if (!ident->derived) {
  351. printf("%s\n", ident->name);
  352. return;
  353. }
  354. LY_ARRAY_FOR(ident->derived, u) {
  355. identityref(ident->derived[u]);
  356. }
  357. }
  358. void pline_print_type_completions(const struct lysc_type *type)
  359. {
  360. assert(type);
  361. switch (type->basetype) {
  362. case LY_TYPE_BOOL: {
  363. printf("true\nfalse\n");
  364. break;
  365. }
  366. case LY_TYPE_ENUM: {
  367. const struct lysc_type_enum *t =
  368. (const struct lysc_type_enum *)type;
  369. LY_ARRAY_COUNT_TYPE u = 0;
  370. LY_ARRAY_FOR(t->enums, u) {
  371. printf("%s\n",t->enums[u].name);
  372. }
  373. break;
  374. }
  375. case LY_TYPE_IDENT: {
  376. struct lysc_type_identityref *t =
  377. (struct lysc_type_identityref *)type;
  378. LY_ARRAY_COUNT_TYPE u = 0;
  379. LY_ARRAY_FOR(t->bases, u) {
  380. identityref(t->bases[u]);
  381. }
  382. break;
  383. }
  384. case LY_TYPE_UNION: {
  385. struct lysc_type_union *t =
  386. (struct lysc_type_union *)type;
  387. LY_ARRAY_COUNT_TYPE u = 0;
  388. LY_ARRAY_FOR(t->types, u) {
  389. pline_print_type_completions(t->types[u]);
  390. }
  391. break;
  392. }
  393. default:
  394. break;
  395. }
  396. }
  397. void pline_print_completions(const pline_t *pline)
  398. {
  399. faux_list_node_t *iter = NULL;
  400. pcompl_t *pcompl = NULL;
  401. iter = faux_list_head(pline->compls);
  402. while (pcompl = (pcompl_t *)faux_list_each(&iter)) {
  403. struct lysc_type *type = NULL;
  404. const struct lysc_node *node = pcompl->node;
  405. // printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  406. if (!node)
  407. continue;
  408. // Node
  409. if (PCOMPL_NODE == pcompl->type) {
  410. printf("%s\n", node->name);
  411. continue;
  412. }
  413. // Type
  414. if (node->nodetype & LYS_LEAF)
  415. type = ((struct lysc_node_leaf *)node)->type;
  416. else if (node->nodetype & LYS_LEAFLIST)
  417. type = ((struct lysc_node_leaflist *)node)->type;
  418. else
  419. continue;
  420. pline_print_type_completions(type);
  421. }
  422. }