pline.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. static struct lysc_ident *find_ident(struct lysc_ident *ident, const char *name)
  173. {
  174. LY_ARRAY_COUNT_TYPE u = 0;
  175. if (!ident)
  176. return NULL;
  177. if (!ident->derived) {
  178. if (!faux_str_cmp(name, ident->name))
  179. return ident;
  180. return NULL;
  181. }
  182. LY_ARRAY_FOR(ident->derived, u) {
  183. struct lysc_ident *identity = find_ident(ident->derived[u], name);
  184. if (identity)
  185. return identity;
  186. }
  187. return NULL;
  188. }
  189. static const char *identityref_prefix(struct lysc_type_identityref *type,
  190. const char *name)
  191. {
  192. LY_ARRAY_COUNT_TYPE u = 0;
  193. assert(type);
  194. LY_ARRAY_FOR(type->bases, u) {
  195. struct lysc_ident *identity = find_ident(type->bases[u], name);
  196. if (identity)
  197. return identity->module->name;
  198. }
  199. return NULL;
  200. }
  201. bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  202. pline_t *pline)
  203. {
  204. faux_argv_node_t *arg = faux_argv_iter(argv);
  205. const struct lysc_node *node = NULL;
  206. char *rollback_xpath = NULL;
  207. // Rollback is a mechanism to roll to previous node while
  208. // oneliners parsing
  209. bool_t rollback = BOOL_FALSE;
  210. do {
  211. pexpr_t *pexpr = pline_current_expr(pline);
  212. const char *str = (const char *)faux_argv_current(arg);
  213. bool_t is_rollback = rollback;
  214. bool_t next_arg = BOOL_TRUE;
  215. rollback = BOOL_FALSE;
  216. if (node && !is_rollback) {
  217. char *tmp = NULL;
  218. // Save rollback Xpath (for oneliners) before leaf node
  219. // Only leaf and leaf-list node allows to "rollback"
  220. // the path and add additional statements
  221. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  222. faux_str_free(rollback_xpath);
  223. rollback_xpath = faux_str_dup(pexpr->xpath);
  224. }
  225. // Add current node to Xpath
  226. tmp = faux_str_sprintf("/%s:%s",
  227. node->module->name, node->name);
  228. faux_str_cat(&pexpr->xpath, tmp);
  229. faux_str_free(tmp);
  230. // Activate current expression. Because it really has
  231. // new component
  232. pexpr->active = BOOL_TRUE;
  233. }
  234. // Root of the module
  235. if (!node) {
  236. // Completion
  237. if (!str) {
  238. pline_add_compl_subtree(pline, module, node);
  239. return BOOL_FALSE;
  240. }
  241. // Next element
  242. node = find_child(module->compiled->data, str);
  243. if (!node)
  244. return BOOL_FALSE;
  245. // Container
  246. } else if (node->nodetype & LYS_CONTAINER) {
  247. // Completion
  248. if (!str) {
  249. pline_add_compl_subtree(pline, module, node);
  250. break;
  251. }
  252. // Next element
  253. node = find_child(lysc_node_child(node), str);
  254. // List
  255. } else if (node->nodetype & LYS_LIST) {
  256. const struct lysc_node *iter = NULL;
  257. // Next element
  258. if (!is_rollback) {
  259. bool_t break_upper_loop = BOOL_FALSE;
  260. LY_LIST_FOR(lysc_node_child(node), iter) {
  261. char *tmp = NULL;
  262. struct lysc_node_leaf *leaf =
  263. (struct lysc_node_leaf *)iter;
  264. if (!(iter->nodetype & LYS_LEAF))
  265. continue;
  266. if (!(iter->flags & LYS_KEY))
  267. continue;
  268. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  269. // Completion
  270. if (!str) {
  271. char *tmp = NULL;
  272. tmp = faux_str_sprintf("%s/%s",
  273. pexpr->xpath, leaf->name);
  274. pline_add_compl(pline,
  275. PCOMPL_TYPE, iter, tmp);
  276. faux_str_free(tmp);
  277. break_upper_loop = BOOL_TRUE;
  278. break;
  279. }
  280. tmp = faux_str_sprintf("[%s='%s']",
  281. leaf->name, str);
  282. faux_str_cat(&pexpr->xpath, tmp);
  283. faux_str_free(tmp);
  284. faux_argv_each(&arg);
  285. str = (const char *)faux_argv_current(arg);
  286. }
  287. if (break_upper_loop)
  288. break;
  289. }
  290. // Completion
  291. if (!str) {
  292. pline_add_compl_subtree(pline, module, node);
  293. break;
  294. }
  295. // Next element
  296. node = find_child(lysc_node_child(node), str);
  297. // Leaf
  298. } else if (node->nodetype & LYS_LEAF) {
  299. struct lysc_node_leaf *leaf =
  300. (struct lysc_node_leaf *)node;
  301. // Next element
  302. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  303. // Completion
  304. if (!str) {
  305. pline_add_compl_subtree(pline,
  306. module, node->parent);
  307. break;
  308. }
  309. // Don't get next argument when argument is not
  310. // really consumed
  311. next_arg = BOOL_FALSE;
  312. } else {
  313. // Completion
  314. if (!str) {
  315. pline_add_compl(pline,
  316. PCOMPL_TYPE, node, NULL);
  317. break;
  318. }
  319. // Idenity must have prefix
  320. if (LY_TYPE_IDENT == leaf->type->basetype) {
  321. const char *prefix = NULL;
  322. prefix = identityref_prefix(
  323. (struct lysc_type_identityref *)
  324. leaf->type, str);
  325. if (prefix)
  326. pexpr->value = faux_str_sprintf(
  327. "%s:", prefix);
  328. }
  329. faux_str_cat(&pexpr->value, str);
  330. }
  331. // Expression was completed
  332. // So rollback (for oneliners)
  333. node = node->parent;
  334. pline_add_expr(pline, rollback_xpath);
  335. rollback = BOOL_TRUE;
  336. // Leaf-list
  337. } else if (node->nodetype & LYS_LEAFLIST) {
  338. char *tmp = NULL;
  339. const char *prefix = NULL;
  340. struct lysc_node_leaflist *leaflist =
  341. (struct lysc_node_leaflist *)node;
  342. // Completion
  343. if (!str) {
  344. pline_add_compl(pline,
  345. PCOMPL_TYPE, node, pexpr->xpath);
  346. break;
  347. }
  348. // Idenity must have prefix
  349. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  350. prefix = identityref_prefix(
  351. (struct lysc_type_identityref *)
  352. leaflist->type, str);
  353. }
  354. tmp = faux_str_sprintf("[.='%s%s%s']",
  355. prefix ? prefix : "", prefix ? ":" : "", str);
  356. faux_str_cat(&pexpr->xpath, tmp);
  357. faux_str_free(tmp);
  358. // Expression was completed
  359. // So rollback (for oneliners)
  360. node = node->parent;
  361. pline_add_expr(pline, rollback_xpath);
  362. rollback = BOOL_TRUE;
  363. }
  364. if (next_arg)
  365. faux_argv_each(&arg);
  366. } while (node || rollback);
  367. faux_str_free(rollback_xpath);
  368. return BOOL_TRUE;
  369. }
  370. pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
  371. {
  372. struct lys_module *module = NULL;
  373. pline_t *pline = pline_new();
  374. uint32_t i = 0;
  375. assert(ctx);
  376. if (!ctx)
  377. return NULL;
  378. // Iterate all modules
  379. i = 0;
  380. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  381. if (sr_module_is_internal(module))
  382. continue;
  383. if (!module->compiled)
  384. continue;
  385. if (!module->implemented)
  386. continue;
  387. if (!module->compiled->data)
  388. continue;
  389. if (pline_parse_module(module, argv, pline))
  390. break; // Found
  391. }
  392. return pline;
  393. }
  394. static void identityref(struct lysc_ident *ident)
  395. {
  396. LY_ARRAY_COUNT_TYPE u = 0;
  397. if (!ident)
  398. return;
  399. if (!ident->derived) {
  400. printf("%s\n", ident->name);
  401. return;
  402. }
  403. LY_ARRAY_FOR(ident->derived, u) {
  404. identityref(ident->derived[u]);
  405. }
  406. }
  407. void pline_print_type_completions(const struct lysc_type *type)
  408. {
  409. assert(type);
  410. switch (type->basetype) {
  411. case LY_TYPE_BOOL: {
  412. printf("true\nfalse\n");
  413. break;
  414. }
  415. case LY_TYPE_ENUM: {
  416. const struct lysc_type_enum *t =
  417. (const struct lysc_type_enum *)type;
  418. LY_ARRAY_COUNT_TYPE u = 0;
  419. LY_ARRAY_FOR(t->enums, u) {
  420. printf("%s\n",t->enums[u].name);
  421. }
  422. break;
  423. }
  424. case LY_TYPE_IDENT: {
  425. struct lysc_type_identityref *t =
  426. (struct lysc_type_identityref *)type;
  427. LY_ARRAY_COUNT_TYPE u = 0;
  428. LY_ARRAY_FOR(t->bases, u) {
  429. identityref(t->bases[u]);
  430. }
  431. break;
  432. }
  433. case LY_TYPE_UNION: {
  434. struct lysc_type_union *t =
  435. (struct lysc_type_union *)type;
  436. LY_ARRAY_COUNT_TYPE u = 0;
  437. LY_ARRAY_FOR(t->types, u) {
  438. pline_print_type_completions(t->types[u]);
  439. }
  440. break;
  441. }
  442. default:
  443. break;
  444. }
  445. }
  446. void pline_print_completions(const pline_t *pline)
  447. {
  448. faux_list_node_t *iter = NULL;
  449. pcompl_t *pcompl = NULL;
  450. iter = faux_list_head(pline->compls);
  451. while (pcompl = (pcompl_t *)faux_list_each(&iter)) {
  452. struct lysc_type *type = NULL;
  453. const struct lysc_node *node = pcompl->node;
  454. // printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  455. if (!node)
  456. continue;
  457. // Node
  458. if (PCOMPL_NODE == pcompl->type) {
  459. printf("%s\n", node->name);
  460. continue;
  461. }
  462. // Type
  463. if (node->nodetype & LYS_LEAF)
  464. type = ((struct lysc_node_leaf *)node)->type;
  465. else if (node->nodetype & LYS_LEAFLIST)
  466. type = ((struct lysc_node_leaflist *)node)->type;
  467. else
  468. continue;
  469. pline_print_type_completions(type);
  470. }
  471. }