pline.c 18 KB

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