pline.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 <faux/faux.h>
  9. #include <faux/str.h>
  10. #include <faux/list.h>
  11. #include <faux/argv.h>
  12. #include <sysrepo.h>
  13. #include <sysrepo/xpath.h>
  14. #include <sysrepo/values.h>
  15. #include <libyang/tree_edit.h>
  16. #include "sr_copypaste.h"
  17. #include "pline.h"
  18. #define NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST)
  19. static pexpr_t *pexpr_new(void)
  20. {
  21. pexpr_t *pexpr = NULL;
  22. pexpr = faux_zmalloc(sizeof(*pexpr));
  23. assert(pexpr);
  24. if (!pexpr)
  25. return NULL;
  26. // Initialize
  27. pexpr->xpath = NULL;
  28. pexpr->value = NULL;
  29. pexpr->active = BOOL_FALSE;
  30. pexpr->pat = PAT_NONE;
  31. pexpr->args_num = 0;
  32. pexpr->list_pos = 0;
  33. return pexpr;
  34. }
  35. static void pexpr_free(pexpr_t *pexpr)
  36. {
  37. if (!pexpr)
  38. return;
  39. faux_str_free(pexpr->xpath);
  40. faux_str_free(pexpr->value);
  41. free(pexpr);
  42. }
  43. static pcompl_t *pcompl_new(void)
  44. {
  45. pcompl_t *pcompl = NULL;
  46. pcompl = faux_zmalloc(sizeof(*pcompl));
  47. assert(pcompl);
  48. if (!pcompl)
  49. return NULL;
  50. // Initialize
  51. pcompl->type = PCOMPL_NODE;
  52. pcompl->node = NULL;
  53. pcompl->xpath = NULL;
  54. return pcompl;
  55. }
  56. static void pcompl_free(pcompl_t *pcompl)
  57. {
  58. if (!pcompl)
  59. return;
  60. faux_str_free(pcompl->xpath);
  61. free(pcompl);
  62. }
  63. pline_t *pline_new(sr_session_ctx_t *sess)
  64. {
  65. pline_t *pline = NULL;
  66. pline = faux_zmalloc(sizeof(*pline));
  67. assert(pline);
  68. if (!pline)
  69. return NULL;
  70. // Init
  71. pline->sess = sess;
  72. pline->invalid = BOOL_FALSE;
  73. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  74. NULL, NULL, (faux_list_free_fn)pexpr_free);
  75. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  76. NULL, NULL, (faux_list_free_fn)pcompl_free);
  77. return pline;
  78. }
  79. void pline_free(pline_t *pline)
  80. {
  81. if (!pline)
  82. return;
  83. faux_list_free(pline->exprs);
  84. faux_list_free(pline->compls);
  85. faux_free(pline);
  86. }
  87. static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath,
  88. size_t args_num, size_t list_pos)
  89. {
  90. pexpr_t *pexpr = NULL;
  91. assert(pline);
  92. pexpr = pexpr_new();
  93. if (xpath)
  94. pexpr->xpath = faux_str_dup(xpath);
  95. pexpr->args_num = args_num;
  96. pexpr->list_pos = list_pos;
  97. faux_list_add(pline->exprs, pexpr);
  98. return pexpr;
  99. }
  100. pexpr_t *pline_current_expr(pline_t *pline)
  101. {
  102. assert(pline);
  103. if (faux_list_len(pline->exprs) == 0)
  104. pline_add_expr(pline, NULL, 0, 0);
  105. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  106. }
  107. static void pline_add_compl(pline_t *pline,
  108. pcompl_type_e type, const struct lysc_node *node, char *xpath)
  109. {
  110. pcompl_t *pcompl = NULL;
  111. assert(pline);
  112. pcompl = pcompl_new();
  113. pcompl->type = type;
  114. pcompl->node = node;
  115. if (xpath)
  116. pcompl->xpath = faux_str_dup(xpath);
  117. faux_list_add(pline->compls, pcompl);
  118. }
  119. static void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
  120. const struct lysc_node *node)
  121. {
  122. const struct lysc_node *subtree = NULL;
  123. const struct lysc_node *iter = NULL;
  124. assert(pline);
  125. assert(module);
  126. if (node)
  127. subtree = lysc_node_child(node);
  128. else
  129. subtree = module->compiled->data;
  130. LY_LIST_FOR(subtree, iter) {
  131. if (!(iter->nodetype & NODETYPE_CONF))
  132. continue;
  133. if (!(iter->flags & LYS_CONFIG_W))
  134. continue;
  135. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  136. }
  137. }
  138. void pline_debug(pline_t *pline)
  139. {
  140. faux_list_node_t *iter = NULL;
  141. pexpr_t *pexpr = NULL;
  142. pcompl_t *pcompl = NULL;
  143. printf("====== Pline:\n\n");
  144. printf("invalid = %s\n", pline->invalid ? "true" : "false");
  145. printf("\n");
  146. printf("=== Expressions:\n\n");
  147. iter = faux_list_head(pline->exprs);
  148. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  149. char *pat = NULL;
  150. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  151. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  152. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  153. switch (pexpr->pat) {
  154. case 0x0001:
  155. pat = "NONE";
  156. break;
  157. case 0x0002:
  158. pat = "CONTAINER";
  159. break;
  160. case 0x0004:
  161. pat = "LIST";
  162. break;
  163. case 0x0008:
  164. pat = "LIST_KEY";
  165. break;
  166. case 0x0010:
  167. pat = "LIST_KEY_INCOMPLETED";
  168. break;
  169. case 0x0020:
  170. pat = "LEAF";
  171. break;
  172. case 0x0040:
  173. pat = "LEAF_VALUE";
  174. break;
  175. case 0x0080:
  176. pat = "LEAF_EMPTY";
  177. break;
  178. case 0x0100:
  179. pat = "LEAFLIST";
  180. break;
  181. case 0x0200:
  182. pat = "LEAFLIST_VALUE";
  183. break;
  184. default:
  185. pat = "UNKNOWN";
  186. break;
  187. }
  188. printf("pexpr.pat = %s\n", pat);
  189. printf("pexpr.args_num = %lu\n", pexpr->args_num);
  190. printf("pexpr.list_pos = %lu\n", pexpr->list_pos);
  191. printf("\n");
  192. }
  193. printf("=== Completions:\n\n");
  194. iter = faux_list_head(pline->compls);
  195. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  196. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  197. "PCOMPL_NODE" : "PCOMPL_TYPE");
  198. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  199. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  200. printf("\n");
  201. }
  202. }
  203. // Don't use standard lys_find_child() because it checks given module to be
  204. // equal to found node's module. So augmented nodes will not be found.
  205. static const struct lysc_node *find_child(const struct lysc_node *node,
  206. const char *name)
  207. {
  208. const struct lysc_node *iter = NULL;
  209. if (!node)
  210. return NULL;
  211. LY_LIST_FOR(node, iter) {
  212. if (!(iter->nodetype & NODETYPE_CONF))
  213. continue;
  214. if (!(iter->flags & LYS_CONFIG_W))
  215. continue;
  216. if (!faux_str_cmp(iter->name, name))
  217. return iter;
  218. }
  219. return NULL;
  220. }
  221. static struct lysc_ident *find_ident(struct lysc_ident *ident, const char *name)
  222. {
  223. LY_ARRAY_COUNT_TYPE u = 0;
  224. if (!ident)
  225. return NULL;
  226. if (!ident->derived) {
  227. if (!faux_str_cmp(name, ident->name))
  228. return ident;
  229. return NULL;
  230. }
  231. LY_ARRAY_FOR(ident->derived, u) {
  232. struct lysc_ident *identity = find_ident(ident->derived[u], name);
  233. if (identity)
  234. return identity;
  235. }
  236. return NULL;
  237. }
  238. static const char *identityref_prefix(struct lysc_type_identityref *type,
  239. const char *name)
  240. {
  241. LY_ARRAY_COUNT_TYPE u = 0;
  242. assert(type);
  243. LY_ARRAY_FOR(type->bases, u) {
  244. struct lysc_ident *identity = find_ident(type->bases[u], name);
  245. if (identity)
  246. return identity->module->name;
  247. }
  248. return NULL;
  249. }
  250. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  251. pline_t *pline)
  252. {
  253. faux_argv_node_t *arg = faux_argv_iter(argv);
  254. const struct lysc_node *node = NULL;
  255. char *rollback_xpath = NULL;
  256. size_t rollback_args_num = 0;
  257. size_t rollback_list_pos = 0;
  258. // Rollback is a mechanism to roll to previous node while
  259. // oneliners parsing
  260. bool_t rollback = BOOL_FALSE;
  261. pexpr_t *first_pexpr = NULL;
  262. // It's necessary because upper function can use the same pline object
  263. // for another modules before. It uses the same object to collect
  264. // possible completions. But pline is really invalid only when all
  265. // modules don't recognize argument.
  266. pline->invalid = BOOL_FALSE;
  267. do {
  268. pexpr_t *pexpr = pline_current_expr(pline);
  269. const char *str = (const char *)faux_argv_current(arg);
  270. bool_t is_rollback = rollback;
  271. bool_t next_arg = BOOL_TRUE;
  272. rollback = BOOL_FALSE;
  273. if (node && !is_rollback) {
  274. char *tmp = NULL;
  275. // Save rollback Xpath (for oneliners) before leaf node
  276. // Only leaf and leaf-list node allows to "rollback"
  277. // the path and add additional statements
  278. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  279. faux_str_free(rollback_xpath);
  280. rollback_xpath = faux_str_dup(pexpr->xpath);
  281. rollback_args_num = pexpr->args_num;
  282. rollback_list_pos = pexpr->list_pos;
  283. }
  284. // Add current node to Xpath
  285. tmp = faux_str_sprintf("/%s:%s",
  286. node->module->name, node->name);
  287. faux_str_cat(&pexpr->xpath, tmp);
  288. faux_str_free(tmp);
  289. pexpr->args_num++;
  290. // Activate current expression. Because it really has
  291. // new component
  292. pexpr->active = BOOL_TRUE;
  293. }
  294. // Root of the module
  295. if (!node) {
  296. // Completion
  297. if (!str) {
  298. pline_add_compl_subtree(pline, module, node);
  299. break;
  300. }
  301. // Next element
  302. node = find_child(module->compiled->data, str);
  303. if (!node)
  304. break;
  305. // Container
  306. } else if (node->nodetype & LYS_CONTAINER) {
  307. pexpr->pat = PAT_CONTAINER;
  308. // Completion
  309. if (!str) {
  310. pline_add_compl_subtree(pline, module, node);
  311. break;
  312. }
  313. // Next element
  314. node = find_child(lysc_node_child(node), str);
  315. // List
  316. } else if (node->nodetype & LYS_LIST) {
  317. const struct lysc_node *iter = NULL;
  318. pexpr->pat = PAT_LIST;
  319. pexpr->list_pos = pexpr->args_num;
  320. // Next element
  321. if (!is_rollback) {
  322. bool_t break_upper_loop = BOOL_FALSE;
  323. LY_LIST_FOR(lysc_node_child(node), iter) {
  324. char *tmp = NULL;
  325. struct lysc_node_leaf *leaf =
  326. (struct lysc_node_leaf *)iter;
  327. if (!(iter->nodetype & LYS_LEAF))
  328. continue;
  329. if (!(iter->flags & LYS_KEY))
  330. continue;
  331. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  332. // Completion
  333. if (!str) {
  334. char *tmp = NULL;
  335. tmp = faux_str_sprintf("%s/%s",
  336. pexpr->xpath, leaf->name);
  337. pline_add_compl(pline,
  338. PCOMPL_TYPE, iter, tmp);
  339. faux_str_free(tmp);
  340. break_upper_loop = BOOL_TRUE;
  341. break;
  342. }
  343. tmp = faux_str_sprintf("[%s='%s']",
  344. leaf->name, str);
  345. faux_str_cat(&pexpr->xpath, tmp);
  346. faux_str_free(tmp);
  347. pexpr->args_num++;
  348. faux_argv_each(&arg);
  349. str = (const char *)faux_argv_current(arg);
  350. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  351. }
  352. if (break_upper_loop)
  353. break;
  354. }
  355. pexpr->pat = PAT_LIST_KEY;
  356. // Completion
  357. if (!str) {
  358. pline_add_compl_subtree(pline, module, node);
  359. break;
  360. }
  361. // Next element
  362. node = find_child(lysc_node_child(node), str);
  363. // Leaf
  364. } else if (node->nodetype & LYS_LEAF) {
  365. struct lysc_node_leaf *leaf =
  366. (struct lysc_node_leaf *)node;
  367. // Next element
  368. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  369. pexpr->pat = PAT_LEAF_EMPTY;
  370. // Completion
  371. if (!str) {
  372. pline_add_compl_subtree(pline,
  373. module, node->parent);
  374. break;
  375. }
  376. // Don't get next argument when argument is not
  377. // really consumed
  378. next_arg = BOOL_FALSE;
  379. } else {
  380. pexpr->pat = PAT_LEAF;
  381. // Completion
  382. if (!str) {
  383. pline_add_compl(pline,
  384. PCOMPL_TYPE, node, NULL);
  385. break;
  386. }
  387. pexpr->pat = PAT_LEAF_VALUE;
  388. // Idenity must have prefix
  389. if (LY_TYPE_IDENT == leaf->type->basetype) {
  390. const char *prefix = NULL;
  391. prefix = identityref_prefix(
  392. (struct lysc_type_identityref *)
  393. leaf->type, str);
  394. if (prefix)
  395. pexpr->value = faux_str_sprintf(
  396. "%s:", prefix);
  397. }
  398. faux_str_cat(&pexpr->value, str);
  399. }
  400. // Expression was completed
  401. // So rollback (for oneliners)
  402. node = node->parent;
  403. pline_add_expr(pline, rollback_xpath,
  404. rollback_args_num, rollback_list_pos);
  405. rollback = BOOL_TRUE;
  406. // Leaf-list
  407. } else if (node->nodetype & LYS_LEAFLIST) {
  408. char *tmp = NULL;
  409. const char *prefix = NULL;
  410. struct lysc_node_leaflist *leaflist =
  411. (struct lysc_node_leaflist *)node;
  412. pexpr->pat = PAT_LEAFLIST;
  413. pexpr->list_pos = pexpr->args_num;
  414. // Completion
  415. if (!str) {
  416. pline_add_compl(pline,
  417. PCOMPL_TYPE, node, pexpr->xpath);
  418. break;
  419. }
  420. pexpr->pat = PAT_LEAFLIST_VALUE;
  421. // Idenity must have prefix
  422. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  423. prefix = identityref_prefix(
  424. (struct lysc_type_identityref *)
  425. leaflist->type, str);
  426. }
  427. tmp = faux_str_sprintf("[.='%s%s%s']",
  428. prefix ? prefix : "", prefix ? ":" : "", str);
  429. faux_str_cat(&pexpr->xpath, tmp);
  430. faux_str_free(tmp);
  431. pexpr->args_num++;
  432. // Expression was completed
  433. // So rollback (for oneliners)
  434. node = node->parent;
  435. pline_add_expr(pline, rollback_xpath,
  436. rollback_args_num, rollback_list_pos);
  437. rollback = BOOL_TRUE;
  438. }
  439. // Current argument was not consumed.
  440. // Break before getting next arg.
  441. if (!node && !rollback)
  442. break;
  443. if (next_arg)
  444. faux_argv_each(&arg);
  445. } while (BOOL_TRUE);
  446. // There is not-consumed argument so whole pline is invalid
  447. if (faux_argv_current(arg))
  448. pline->invalid = BOOL_TRUE;
  449. faux_str_free(rollback_xpath);
  450. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  451. if (!first_pexpr || !first_pexpr->xpath)
  452. return BOOL_FALSE; // Not found
  453. return BOOL_TRUE;
  454. }
  455. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, uint32_t flags)
  456. {
  457. const struct ly_ctx *ctx = NULL;
  458. struct lys_module *module = NULL;
  459. pline_t *pline = NULL;
  460. uint32_t i = 0;
  461. faux_list_node_t *last_expr_node = NULL;
  462. assert(sess);
  463. if (!sess)
  464. return NULL;
  465. pline = pline_new(sess);
  466. if (!pline)
  467. return NULL;
  468. ctx = sr_session_acquire_context(pline->sess);
  469. if (!ctx)
  470. return NULL;
  471. // Iterate all modules
  472. i = 0;
  473. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  474. if (sr_module_is_internal(module))
  475. continue;
  476. if (!module->compiled)
  477. continue;
  478. if (!module->implemented)
  479. continue;
  480. if (!module->compiled->data)
  481. continue;
  482. if (pline_parse_module(module, argv, pline))
  483. break; // Found
  484. }
  485. sr_session_release_context(pline->sess);
  486. // Last parsed expression can be inactive so remove it from list
  487. last_expr_node = faux_list_tail(pline->exprs);
  488. if (last_expr_node) {
  489. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  490. if (!expr->active)
  491. faux_list_del(pline->exprs, last_expr_node);
  492. }
  493. return pline;
  494. }
  495. static void identityref(struct lysc_ident *ident)
  496. {
  497. LY_ARRAY_COUNT_TYPE u = 0;
  498. if (!ident)
  499. return;
  500. if (!ident->derived) {
  501. printf("%s\n", ident->name);
  502. return;
  503. }
  504. LY_ARRAY_FOR(ident->derived, u) {
  505. identityref(ident->derived[u]);
  506. }
  507. }
  508. static void pline_print_type_completions(const struct lysc_type *type)
  509. {
  510. assert(type);
  511. switch (type->basetype) {
  512. case LY_TYPE_BOOL: {
  513. printf("true\nfalse\n");
  514. break;
  515. }
  516. case LY_TYPE_ENUM: {
  517. const struct lysc_type_enum *t =
  518. (const struct lysc_type_enum *)type;
  519. LY_ARRAY_COUNT_TYPE u = 0;
  520. LY_ARRAY_FOR(t->enums, u) {
  521. printf("%s\n",t->enums[u].name);
  522. }
  523. break;
  524. }
  525. case LY_TYPE_IDENT: {
  526. struct lysc_type_identityref *t =
  527. (struct lysc_type_identityref *)type;
  528. LY_ARRAY_COUNT_TYPE u = 0;
  529. LY_ARRAY_FOR(t->bases, u) {
  530. identityref(t->bases[u]);
  531. }
  532. break;
  533. }
  534. case LY_TYPE_UNION: {
  535. struct lysc_type_union *t =
  536. (struct lysc_type_union *)type;
  537. LY_ARRAY_COUNT_TYPE u = 0;
  538. LY_ARRAY_FOR(t->types, u) {
  539. pline_print_type_completions(t->types[u]);
  540. }
  541. break;
  542. }
  543. default:
  544. break;
  545. }
  546. }
  547. static void pline_print_type_help(const struct lysc_node *node,
  548. const struct lysc_type *type)
  549. {
  550. assert(type);
  551. if (type->basetype != LY_TYPE_UNION)
  552. printf("%s\n", node->name);
  553. switch (type->basetype) {
  554. case LY_TYPE_UINT8: {
  555. printf("Unsigned integer 8bit\n");
  556. break;
  557. }
  558. case LY_TYPE_UINT16: {
  559. printf("Unsigned integer 16bit\n");
  560. break;
  561. }
  562. case LY_TYPE_UINT32: {
  563. printf("Unsigned integer 32bit\n");
  564. break;
  565. }
  566. case LY_TYPE_UINT64: {
  567. printf("Unsigned integer 64bit\n");
  568. break;
  569. }
  570. case LY_TYPE_INT8: {
  571. printf("Integer 8bit\n");
  572. break;
  573. }
  574. case LY_TYPE_INT16: {
  575. printf("Integer 16bit\n");
  576. break;
  577. }
  578. case LY_TYPE_INT32: {
  579. printf("Integer 32bit\n");
  580. break;
  581. }
  582. case LY_TYPE_INT64: {
  583. printf("Integer 64bit\n");
  584. break;
  585. }
  586. case LY_TYPE_STRING: {
  587. printf("String\n");
  588. break;
  589. }
  590. case LY_TYPE_BOOL: {
  591. printf("Boolean true/false\n");
  592. break;
  593. }
  594. case LY_TYPE_DEC64: {
  595. printf("Signed decimal number\n");
  596. break;
  597. }
  598. case LY_TYPE_ENUM: {
  599. printf("Enumerated choice\n");
  600. break;
  601. }
  602. case LY_TYPE_IDENT: {
  603. printf("Identity\n");
  604. break;
  605. }
  606. case LY_TYPE_UNION: {
  607. struct lysc_type_union *t =
  608. (struct lysc_type_union *)type;
  609. LY_ARRAY_COUNT_TYPE u = 0;
  610. LY_ARRAY_FOR(t->types, u) {
  611. pline_print_type_help(node, t->types[u]);
  612. }
  613. break;
  614. }
  615. default:
  616. printf("Unknown\n");
  617. break;
  618. }
  619. }
  620. void pline_print_completions(const pline_t *pline, bool_t help)
  621. {
  622. faux_list_node_t *iter = NULL;
  623. pcompl_t *pcompl = NULL;
  624. iter = faux_list_head(pline->compls);
  625. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  626. struct lysc_type *type = NULL;
  627. const struct lysc_node *node = pcompl->node;
  628. if (pcompl->xpath && !help) {
  629. sr_val_t *vals = NULL;
  630. size_t val_num = 0;
  631. size_t i = 0;
  632. sr_get_items(pline->sess, pcompl->xpath,
  633. 0, 0, &vals, &val_num);
  634. for (i = 0; i < val_num; i++) {
  635. char *tmp = sr_val_to_str(&vals[i]);
  636. if (!tmp)
  637. continue;
  638. printf("%s\n", tmp);
  639. free(tmp);
  640. }
  641. }
  642. if (!node)
  643. continue;
  644. // Node
  645. if (PCOMPL_NODE == pcompl->type) {
  646. printf("%s\n", node->name);
  647. if (help) {
  648. if (!node->dsc) {
  649. printf("%s\n", node->name);
  650. } else {
  651. char *dsc = faux_str_getline(node->dsc,
  652. NULL);
  653. printf("%s\n", dsc);
  654. faux_str_free(dsc);
  655. }
  656. }
  657. continue;
  658. }
  659. // Type
  660. if (node->nodetype & LYS_LEAF)
  661. type = ((struct lysc_node_leaf *)node)->type;
  662. else if (node->nodetype & LYS_LEAFLIST)
  663. type = ((struct lysc_node_leaflist *)node)->type;
  664. else
  665. continue;
  666. if (help)
  667. pline_print_type_help(node, type);
  668. else
  669. pline_print_type_completions(type);
  670. }
  671. }