pline.c 17 KB

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