pline.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. bool_t list_key_with_stmt(const struct lysc_node *node, uint32_t flags)
  271. {
  272. bool_t with_stmt = BOOL_FALSE;
  273. size_t keys_num = 0;
  274. // Parse keys's statement or not
  275. keys_num = list_num_of_keys(node);
  276. if (keys_num > 1) {
  277. if (flags & PPARSE_MULTI_KEYS_W_STMT)
  278. with_stmt = BOOL_TRUE;
  279. } else {
  280. if (flags & PPARSE_SINGLE_KEY_W_STMT)
  281. with_stmt = BOOL_TRUE;
  282. }
  283. return with_stmt;
  284. }
  285. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  286. pline_t *pline, uint32_t flags)
  287. {
  288. faux_argv_node_t *arg = faux_argv_iter(argv);
  289. const struct lysc_node *node = NULL;
  290. char *rollback_xpath = NULL;
  291. size_t rollback_args_num = 0;
  292. size_t rollback_list_pos = 0;
  293. // Rollback is a mechanism to roll to previous node while
  294. // oneliners parsing
  295. bool_t rollback = BOOL_FALSE;
  296. pexpr_t *first_pexpr = NULL;
  297. // It's necessary because upper function can use the same pline object
  298. // for another modules before. It uses the same object to collect
  299. // possible completions. But pline is really invalid only when all
  300. // modules don't recognize argument.
  301. pline->invalid = BOOL_FALSE;
  302. do {
  303. pexpr_t *pexpr = pline_current_expr(pline);
  304. const char *str = (const char *)faux_argv_current(arg);
  305. bool_t is_rollback = rollback;
  306. bool_t next_arg = BOOL_TRUE;
  307. rollback = BOOL_FALSE;
  308. if (node && !is_rollback) {
  309. char *tmp = NULL;
  310. // Save rollback Xpath (for oneliners) before leaf node
  311. // Only leaf and leaf-list node allows to "rollback"
  312. // the path and add additional statements
  313. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  314. faux_str_free(rollback_xpath);
  315. rollback_xpath = faux_str_dup(pexpr->xpath);
  316. rollback_args_num = pexpr->args_num;
  317. rollback_list_pos = pexpr->list_pos;
  318. }
  319. // Add current node to Xpath
  320. tmp = faux_str_sprintf("/%s:%s",
  321. node->module->name, node->name);
  322. faux_str_cat(&pexpr->xpath, tmp);
  323. faux_str_free(tmp);
  324. pexpr->args_num++;
  325. // Activate current expression. Because it really has
  326. // new component
  327. pexpr->active = BOOL_TRUE;
  328. }
  329. // Root of the module
  330. if (!node) {
  331. // Completion
  332. if (!str) {
  333. pline_add_compl_subtree(pline, module, node);
  334. break;
  335. }
  336. // Next element
  337. node = find_child(module->compiled->data, str);
  338. if (!node)
  339. break;
  340. // Container
  341. } else if (node->nodetype & LYS_CONTAINER) {
  342. pexpr->pat = PAT_CONTAINER;
  343. // Completion
  344. if (!str) {
  345. pline_add_compl_subtree(pline, module, node);
  346. break;
  347. }
  348. // Next element
  349. node = find_child(lysc_node_child(node), str);
  350. // List
  351. } else if (node->nodetype & LYS_LIST) {
  352. const struct lysc_node *iter = NULL;
  353. pexpr->pat = PAT_LIST;
  354. pexpr->list_pos = pexpr->args_num;
  355. faux_str_free(pexpr->last_keys);
  356. pexpr->last_keys = NULL;
  357. // Next element
  358. if (!is_rollback) {
  359. bool_t break_upper_loop = BOOL_FALSE;
  360. bool_t with_stmt = list_key_with_stmt(node, flags);
  361. LY_LIST_FOR(lysc_node_child(node), iter) {
  362. char *tmp = NULL;
  363. struct lysc_node_leaf *leaf =
  364. (struct lysc_node_leaf *)iter;
  365. if (!(iter->nodetype & LYS_LEAF))
  366. continue;
  367. if (!(iter->flags & LYS_KEY))
  368. continue;
  369. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  370. // Parse statement if necessary
  371. if (with_stmt) {
  372. // Completion
  373. if (!str) {
  374. pline_add_compl(pline,
  375. PCOMPL_NODE, iter, NULL);
  376. break_upper_loop = BOOL_TRUE;
  377. break;
  378. }
  379. pexpr->args_num++;
  380. faux_argv_each(&arg);
  381. str = (const char *)faux_argv_current(arg);
  382. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  383. }
  384. // Completion
  385. if (!str) {
  386. char *tmp = NULL;
  387. tmp = faux_str_sprintf("%s/%s",
  388. pexpr->xpath, leaf->name);
  389. pline_add_compl(pline,
  390. PCOMPL_TYPE, iter, tmp);
  391. faux_str_free(tmp);
  392. break_upper_loop = BOOL_TRUE;
  393. break;
  394. }
  395. tmp = faux_str_sprintf("[%s='%s']",
  396. leaf->name, str);
  397. faux_str_cat(&pexpr->xpath, tmp);
  398. faux_str_cat(&pexpr->last_keys, tmp);
  399. faux_str_free(tmp);
  400. pexpr->args_num++;
  401. faux_argv_each(&arg);
  402. str = (const char *)faux_argv_current(arg);
  403. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  404. }
  405. if (break_upper_loop)
  406. break;
  407. }
  408. pexpr->pat = PAT_LIST_KEY;
  409. // Completion
  410. if (!str) {
  411. pline_add_compl_subtree(pline, module, node);
  412. break;
  413. }
  414. // Next element
  415. node = find_child(lysc_node_child(node), str);
  416. // Leaf
  417. } else if (node->nodetype & LYS_LEAF) {
  418. struct lysc_node_leaf *leaf =
  419. (struct lysc_node_leaf *)node;
  420. // Next element
  421. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  422. pexpr->pat = PAT_LEAF_EMPTY;
  423. // Completion
  424. if (!str) {
  425. pline_add_compl_subtree(pline,
  426. module, node->parent);
  427. break;
  428. }
  429. // Don't get next argument when argument is not
  430. // really consumed
  431. next_arg = BOOL_FALSE;
  432. } else {
  433. pexpr->pat = PAT_LEAF;
  434. // Completion
  435. if (!str) {
  436. pline_add_compl(pline,
  437. PCOMPL_TYPE, node, NULL);
  438. break;
  439. }
  440. pexpr->pat = PAT_LEAF_VALUE;
  441. // Idenity must have prefix
  442. if (LY_TYPE_IDENT == leaf->type->basetype) {
  443. const char *prefix = NULL;
  444. prefix = identityref_prefix(
  445. (struct lysc_type_identityref *)
  446. leaf->type, str);
  447. if (prefix)
  448. pexpr->value = faux_str_sprintf(
  449. "%s:", prefix);
  450. }
  451. faux_str_cat(&pexpr->value, str);
  452. }
  453. // Expression was completed
  454. // So rollback (for oneliners)
  455. node = node->parent;
  456. pline_add_expr(pline, rollback_xpath,
  457. rollback_args_num, rollback_list_pos);
  458. rollback = BOOL_TRUE;
  459. // Leaf-list
  460. } else if (node->nodetype & LYS_LEAFLIST) {
  461. char *tmp = NULL;
  462. const char *prefix = NULL;
  463. struct lysc_node_leaflist *leaflist =
  464. (struct lysc_node_leaflist *)node;
  465. pexpr->pat = PAT_LEAFLIST;
  466. pexpr->list_pos = pexpr->args_num;
  467. faux_str_free(pexpr->last_keys);
  468. pexpr->last_keys = NULL;
  469. // Completion
  470. if (!str) {
  471. pline_add_compl(pline,
  472. PCOMPL_TYPE, node, pexpr->xpath);
  473. break;
  474. }
  475. pexpr->pat = PAT_LEAFLIST_VALUE;
  476. // Idenity must have prefix
  477. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  478. prefix = identityref_prefix(
  479. (struct lysc_type_identityref *)
  480. leaflist->type, str);
  481. }
  482. tmp = faux_str_sprintf("[.='%s%s%s']",
  483. prefix ? prefix : "", prefix ? ":" : "", str);
  484. faux_str_cat(&pexpr->xpath, tmp);
  485. faux_str_cat(&pexpr->last_keys, str);
  486. faux_str_free(tmp);
  487. pexpr->args_num++;
  488. // Expression was completed
  489. // So rollback (for oneliners)
  490. node = node->parent;
  491. pline_add_expr(pline, rollback_xpath,
  492. rollback_args_num, rollback_list_pos);
  493. rollback = BOOL_TRUE;
  494. }
  495. // Current argument was not consumed.
  496. // Break before getting next arg.
  497. if (!node && !rollback)
  498. break;
  499. if (next_arg)
  500. faux_argv_each(&arg);
  501. } while (BOOL_TRUE);
  502. // There is not-consumed argument so whole pline is invalid
  503. if (faux_argv_current(arg))
  504. pline->invalid = BOOL_TRUE;
  505. faux_str_free(rollback_xpath);
  506. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  507. if (!first_pexpr || !first_pexpr->xpath)
  508. return BOOL_FALSE; // Not found
  509. return BOOL_TRUE;
  510. }
  511. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, uint32_t flags)
  512. {
  513. const struct ly_ctx *ctx = NULL;
  514. struct lys_module *module = NULL;
  515. pline_t *pline = NULL;
  516. uint32_t i = 0;
  517. faux_list_node_t *last_expr_node = NULL;
  518. assert(sess);
  519. if (!sess)
  520. return NULL;
  521. pline = pline_new(sess);
  522. if (!pline)
  523. return NULL;
  524. ctx = sr_session_acquire_context(pline->sess);
  525. if (!ctx)
  526. return NULL;
  527. // Iterate all modules
  528. i = 0;
  529. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  530. if (sr_module_is_internal(module))
  531. continue;
  532. if (!module->compiled)
  533. continue;
  534. if (!module->implemented)
  535. continue;
  536. if (!module->compiled->data)
  537. continue;
  538. if (pline_parse_module(module, argv, pline, flags))
  539. break; // Found
  540. }
  541. sr_session_release_context(pline->sess);
  542. // Last parsed expression can be inactive so remove it from list
  543. last_expr_node = faux_list_tail(pline->exprs);
  544. if (last_expr_node) {
  545. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  546. if (!expr->active)
  547. faux_list_del(pline->exprs, last_expr_node);
  548. }
  549. flags = flags; // Happy compiler
  550. return pline;
  551. }
  552. static void identityref(struct lysc_ident *ident)
  553. {
  554. LY_ARRAY_COUNT_TYPE u = 0;
  555. if (!ident)
  556. return;
  557. if (!ident->derived) {
  558. printf("%s\n", ident->name);
  559. return;
  560. }
  561. LY_ARRAY_FOR(ident->derived, u) {
  562. identityref(ident->derived[u]);
  563. }
  564. }
  565. static void pline_print_type_completions(const struct lysc_type *type)
  566. {
  567. assert(type);
  568. switch (type->basetype) {
  569. case LY_TYPE_BOOL: {
  570. printf("true\nfalse\n");
  571. break;
  572. }
  573. case LY_TYPE_ENUM: {
  574. const struct lysc_type_enum *t =
  575. (const struct lysc_type_enum *)type;
  576. LY_ARRAY_COUNT_TYPE u = 0;
  577. LY_ARRAY_FOR(t->enums, u) {
  578. printf("%s\n",t->enums[u].name);
  579. }
  580. break;
  581. }
  582. case LY_TYPE_IDENT: {
  583. struct lysc_type_identityref *t =
  584. (struct lysc_type_identityref *)type;
  585. LY_ARRAY_COUNT_TYPE u = 0;
  586. LY_ARRAY_FOR(t->bases, u) {
  587. identityref(t->bases[u]);
  588. }
  589. break;
  590. }
  591. case LY_TYPE_UNION: {
  592. struct lysc_type_union *t =
  593. (struct lysc_type_union *)type;
  594. LY_ARRAY_COUNT_TYPE u = 0;
  595. LY_ARRAY_FOR(t->types, u) {
  596. pline_print_type_completions(t->types[u]);
  597. }
  598. break;
  599. }
  600. default:
  601. break;
  602. }
  603. }
  604. static void pline_print_type_help(const struct lysc_node *node,
  605. const struct lysc_type *type)
  606. {
  607. assert(type);
  608. if (type->basetype != LY_TYPE_UNION)
  609. printf("%s\n", node->name);
  610. switch (type->basetype) {
  611. case LY_TYPE_UINT8: {
  612. printf("Unsigned integer 8bit\n");
  613. break;
  614. }
  615. case LY_TYPE_UINT16: {
  616. printf("Unsigned integer 16bit\n");
  617. break;
  618. }
  619. case LY_TYPE_UINT32: {
  620. printf("Unsigned integer 32bit\n");
  621. break;
  622. }
  623. case LY_TYPE_UINT64: {
  624. printf("Unsigned integer 64bit\n");
  625. break;
  626. }
  627. case LY_TYPE_INT8: {
  628. printf("Integer 8bit\n");
  629. break;
  630. }
  631. case LY_TYPE_INT16: {
  632. printf("Integer 16bit\n");
  633. break;
  634. }
  635. case LY_TYPE_INT32: {
  636. printf("Integer 32bit\n");
  637. break;
  638. }
  639. case LY_TYPE_INT64: {
  640. printf("Integer 64bit\n");
  641. break;
  642. }
  643. case LY_TYPE_STRING: {
  644. printf("String\n");
  645. break;
  646. }
  647. case LY_TYPE_BOOL: {
  648. printf("Boolean true/false\n");
  649. break;
  650. }
  651. case LY_TYPE_DEC64: {
  652. printf("Signed decimal number\n");
  653. break;
  654. }
  655. case LY_TYPE_ENUM: {
  656. printf("Enumerated choice\n");
  657. break;
  658. }
  659. case LY_TYPE_IDENT: {
  660. printf("Identity\n");
  661. break;
  662. }
  663. case LY_TYPE_UNION: {
  664. struct lysc_type_union *t =
  665. (struct lysc_type_union *)type;
  666. LY_ARRAY_COUNT_TYPE u = 0;
  667. LY_ARRAY_FOR(t->types, u) {
  668. pline_print_type_help(node, t->types[u]);
  669. }
  670. break;
  671. }
  672. default:
  673. printf("Unknown\n");
  674. break;
  675. }
  676. }
  677. void pline_print_completions(const pline_t *pline, bool_t help)
  678. {
  679. faux_list_node_t *iter = NULL;
  680. pcompl_t *pcompl = NULL;
  681. iter = faux_list_head(pline->compls);
  682. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  683. struct lysc_type *type = NULL;
  684. const struct lysc_node *node = pcompl->node;
  685. if (pcompl->xpath && !help) {
  686. sr_val_t *vals = NULL;
  687. size_t val_num = 0;
  688. size_t i = 0;
  689. sr_get_items(pline->sess, pcompl->xpath,
  690. 0, 0, &vals, &val_num);
  691. for (i = 0; i < val_num; i++) {
  692. char *tmp = sr_val_to_str(&vals[i]);
  693. if (!tmp)
  694. continue;
  695. printf("%s\n", tmp);
  696. free(tmp);
  697. }
  698. }
  699. if (!node)
  700. continue;
  701. // Node
  702. if (PCOMPL_NODE == pcompl->type) {
  703. printf("%s\n", node->name);
  704. if (help) {
  705. if (!node->dsc) {
  706. printf("%s\n", node->name);
  707. } else {
  708. char *dsc = faux_str_getline(node->dsc,
  709. NULL);
  710. printf("%s\n", dsc);
  711. faux_str_free(dsc);
  712. }
  713. }
  714. continue;
  715. }
  716. // Type
  717. if (node->nodetype & LYS_LEAF)
  718. type = ((struct lysc_node_leaf *)node)->type;
  719. else if (node->nodetype & LYS_LEAFLIST)
  720. type = ((struct lysc_node_leaflist *)node)->type;
  721. else
  722. continue;
  723. if (help)
  724. pline_print_type_help(node, type);
  725. else
  726. pline_print_type_completions(type);
  727. }
  728. }