pline.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 "private.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. char *escaped = NULL;
  349. struct lysc_node_leaf *leaf =
  350. (struct lysc_node_leaf *)iter;
  351. if (!(iter->nodetype & LYS_LEAF))
  352. continue;
  353. if (!(iter->flags & LYS_KEY))
  354. continue;
  355. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  356. // Parse statement if necessary
  357. if ((first_key && (flags & PPARSE_FIRST_KEY_W_STMT)) ||
  358. (!first_key && (flags & PPARSE_MULTI_KEYS_W_STMT))) {
  359. // Completion
  360. if (!str) {
  361. pline_add_compl(pline,
  362. PCOMPL_NODE, iter, NULL);
  363. break_upper_loop = BOOL_TRUE;
  364. break;
  365. }
  366. pexpr->args_num++;
  367. faux_argv_each(&arg);
  368. str = (const char *)faux_argv_current(arg);
  369. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  370. }
  371. first_key = BOOL_FALSE;
  372. // Completion
  373. if (!str) {
  374. char *tmp = NULL;
  375. tmp = faux_str_sprintf("%s/%s",
  376. pexpr->xpath, leaf->name);
  377. pline_add_compl(pline,
  378. PCOMPL_TYPE, iter, tmp);
  379. faux_str_free(tmp);
  380. break_upper_loop = BOOL_TRUE;
  381. break;
  382. }
  383. escaped = faux_str_c_esc(str);
  384. tmp = faux_str_sprintf("[%s=\"%s\"]",
  385. leaf->name, escaped);
  386. faux_str_free(escaped);
  387. faux_str_cat(&pexpr->xpath, tmp);
  388. faux_str_cat(&pexpr->last_keys, tmp);
  389. faux_str_free(tmp);
  390. pexpr->args_num++;
  391. faux_argv_each(&arg);
  392. str = (const char *)faux_argv_current(arg);
  393. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  394. }
  395. if (break_upper_loop)
  396. break;
  397. }
  398. pexpr->pat = PAT_LIST_KEY;
  399. // Completion
  400. if (!str) {
  401. pline_add_compl_subtree(pline, module, node);
  402. break;
  403. }
  404. // Next element
  405. node = find_child(lysc_node_child(node), str);
  406. // Leaf
  407. } else if (node->nodetype & LYS_LEAF) {
  408. struct lysc_node_leaf *leaf =
  409. (struct lysc_node_leaf *)node;
  410. // Next element
  411. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  412. pexpr->pat = PAT_LEAF_EMPTY;
  413. // Completion
  414. if (!str) {
  415. pline_add_compl_subtree(pline,
  416. module, node->parent);
  417. break;
  418. }
  419. // Don't get next argument when argument is not
  420. // really consumed
  421. next_arg = BOOL_FALSE;
  422. } else {
  423. pexpr->pat = PAT_LEAF;
  424. // Completion
  425. if (!str) {
  426. pline_add_compl(pline,
  427. PCOMPL_TYPE, node, NULL);
  428. break;
  429. }
  430. pexpr->pat = PAT_LEAF_VALUE;
  431. // Idenity must have prefix
  432. if (LY_TYPE_IDENT == leaf->type->basetype) {
  433. const char *prefix = NULL;
  434. prefix = identityref_prefix(
  435. (struct lysc_type_identityref *)
  436. leaf->type, str);
  437. if (prefix)
  438. pexpr->value = faux_str_sprintf(
  439. "%s:", prefix);
  440. }
  441. faux_str_cat(&pexpr->value, str);
  442. }
  443. // Expression was completed
  444. // So rollback (for oneliners)
  445. node = node->parent;
  446. pline_add_expr(pline, rollback_xpath,
  447. rollback_args_num, rollback_list_pos);
  448. rollback = BOOL_TRUE;
  449. // Leaf-list
  450. } else if (node->nodetype & LYS_LEAFLIST) {
  451. char *tmp = NULL;
  452. const char *prefix = NULL;
  453. struct lysc_node_leaflist *leaflist =
  454. (struct lysc_node_leaflist *)node;
  455. pexpr->pat = PAT_LEAFLIST;
  456. pexpr->list_pos = pexpr->args_num;
  457. faux_str_free(pexpr->last_keys);
  458. pexpr->last_keys = NULL;
  459. // Completion
  460. if (!str) {
  461. pline_add_compl(pline,
  462. PCOMPL_TYPE, node, pexpr->xpath);
  463. break;
  464. }
  465. pexpr->pat = PAT_LEAFLIST_VALUE;
  466. // Idenity must have prefix
  467. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  468. prefix = identityref_prefix(
  469. (struct lysc_type_identityref *)
  470. leaflist->type, str);
  471. }
  472. tmp = faux_str_sprintf("[.='%s%s%s']",
  473. prefix ? prefix : "", prefix ? ":" : "", str);
  474. faux_str_cat(&pexpr->xpath, tmp);
  475. faux_str_cat(&pexpr->last_keys, str);
  476. faux_str_free(tmp);
  477. pexpr->args_num++;
  478. // Expression was completed
  479. // So rollback (for oneliners)
  480. node = node->parent;
  481. pline_add_expr(pline, rollback_xpath,
  482. rollback_args_num, rollback_list_pos);
  483. rollback = BOOL_TRUE;
  484. }
  485. // Current argument was not consumed.
  486. // Break before getting next arg.
  487. if (!node && !rollback)
  488. break;
  489. if (next_arg)
  490. faux_argv_each(&arg);
  491. } while (BOOL_TRUE);
  492. // There is not-consumed argument so whole pline is invalid
  493. if (faux_argv_current(arg))
  494. pline->invalid = BOOL_TRUE;
  495. faux_str_free(rollback_xpath);
  496. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  497. if (!first_pexpr || !first_pexpr->xpath)
  498. return BOOL_FALSE; // Not found
  499. return BOOL_TRUE;
  500. }
  501. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, uint32_t flags)
  502. {
  503. const struct ly_ctx *ctx = NULL;
  504. struct lys_module *module = NULL;
  505. pline_t *pline = NULL;
  506. uint32_t i = 0;
  507. faux_list_node_t *last_expr_node = NULL;
  508. assert(sess);
  509. if (!sess)
  510. return NULL;
  511. pline = pline_new(sess);
  512. if (!pline)
  513. return NULL;
  514. ctx = sr_session_acquire_context(pline->sess);
  515. if (!ctx)
  516. return NULL;
  517. // Iterate all modules
  518. i = 0;
  519. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  520. if (sr_module_is_internal(module))
  521. continue;
  522. if (!module->compiled)
  523. continue;
  524. if (!module->implemented)
  525. continue;
  526. if (!module->compiled->data)
  527. continue;
  528. if (pline_parse_module(module, argv, pline, flags))
  529. break; // Found
  530. }
  531. sr_session_release_context(pline->sess);
  532. // Last parsed expression can be inactive so remove it from list
  533. last_expr_node = faux_list_tail(pline->exprs);
  534. if (last_expr_node) {
  535. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  536. if (!expr->active)
  537. faux_list_del(pline->exprs, last_expr_node);
  538. }
  539. flags = flags; // Happy compiler
  540. return pline;
  541. }
  542. static void identityref(struct lysc_ident *ident)
  543. {
  544. LY_ARRAY_COUNT_TYPE u = 0;
  545. if (!ident)
  546. return;
  547. if (!ident->derived) {
  548. printf("%s\n", ident->name);
  549. return;
  550. }
  551. LY_ARRAY_FOR(ident->derived, u) {
  552. identityref(ident->derived[u]);
  553. }
  554. }
  555. static void pline_print_type_completions(const struct lysc_type *type)
  556. {
  557. assert(type);
  558. switch (type->basetype) {
  559. case LY_TYPE_BOOL: {
  560. printf("true\nfalse\n");
  561. break;
  562. }
  563. case LY_TYPE_ENUM: {
  564. const struct lysc_type_enum *t =
  565. (const struct lysc_type_enum *)type;
  566. LY_ARRAY_COUNT_TYPE u = 0;
  567. LY_ARRAY_FOR(t->enums, u) {
  568. printf("%s\n",t->enums[u].name);
  569. }
  570. break;
  571. }
  572. case LY_TYPE_IDENT: {
  573. struct lysc_type_identityref *t =
  574. (struct lysc_type_identityref *)type;
  575. LY_ARRAY_COUNT_TYPE u = 0;
  576. LY_ARRAY_FOR(t->bases, u) {
  577. identityref(t->bases[u]);
  578. }
  579. break;
  580. }
  581. case LY_TYPE_UNION: {
  582. struct lysc_type_union *t =
  583. (struct lysc_type_union *)type;
  584. LY_ARRAY_COUNT_TYPE u = 0;
  585. LY_ARRAY_FOR(t->types, u) {
  586. pline_print_type_completions(t->types[u]);
  587. }
  588. break;
  589. }
  590. default:
  591. break;
  592. }
  593. }
  594. static void pline_print_type_help(const struct lysc_node *node,
  595. const struct lysc_type *type)
  596. {
  597. assert(type);
  598. if (type->basetype != LY_TYPE_UNION)
  599. printf("%s\n", node->name);
  600. switch (type->basetype) {
  601. case LY_TYPE_UINT8: {
  602. printf("Unsigned integer 8bit\n");
  603. break;
  604. }
  605. case LY_TYPE_UINT16: {
  606. printf("Unsigned integer 16bit\n");
  607. break;
  608. }
  609. case LY_TYPE_UINT32: {
  610. printf("Unsigned integer 32bit\n");
  611. break;
  612. }
  613. case LY_TYPE_UINT64: {
  614. printf("Unsigned integer 64bit\n");
  615. break;
  616. }
  617. case LY_TYPE_INT8: {
  618. printf("Integer 8bit\n");
  619. break;
  620. }
  621. case LY_TYPE_INT16: {
  622. printf("Integer 16bit\n");
  623. break;
  624. }
  625. case LY_TYPE_INT32: {
  626. printf("Integer 32bit\n");
  627. break;
  628. }
  629. case LY_TYPE_INT64: {
  630. printf("Integer 64bit\n");
  631. break;
  632. }
  633. case LY_TYPE_STRING: {
  634. printf("String\n");
  635. break;
  636. }
  637. case LY_TYPE_BOOL: {
  638. printf("Boolean true/false\n");
  639. break;
  640. }
  641. case LY_TYPE_DEC64: {
  642. printf("Signed decimal number\n");
  643. break;
  644. }
  645. case LY_TYPE_ENUM: {
  646. printf("Enumerated choice\n");
  647. break;
  648. }
  649. case LY_TYPE_IDENT: {
  650. printf("Identity\n");
  651. break;
  652. }
  653. case LY_TYPE_UNION: {
  654. struct lysc_type_union *t =
  655. (struct lysc_type_union *)type;
  656. LY_ARRAY_COUNT_TYPE u = 0;
  657. LY_ARRAY_FOR(t->types, u) {
  658. pline_print_type_help(node, t->types[u]);
  659. }
  660. break;
  661. }
  662. default:
  663. printf("Unknown\n");
  664. break;
  665. }
  666. }
  667. void pline_print_completions(const pline_t *pline, bool_t help)
  668. {
  669. faux_list_node_t *iter = NULL;
  670. pcompl_t *pcompl = NULL;
  671. iter = faux_list_head(pline->compls);
  672. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  673. struct lysc_type *type = NULL;
  674. const struct lysc_node *node = pcompl->node;
  675. if (pcompl->xpath && !help) {
  676. sr_val_t *vals = NULL;
  677. size_t val_num = 0;
  678. size_t i = 0;
  679. sr_get_items(pline->sess, pcompl->xpath,
  680. 0, 0, &vals, &val_num);
  681. for (i = 0; i < val_num; i++) {
  682. char *tmp = sr_val_to_str(&vals[i]);
  683. if (!tmp)
  684. continue;
  685. printf("%s\n", tmp);
  686. free(tmp);
  687. }
  688. }
  689. if (!node)
  690. continue;
  691. // Node
  692. if (PCOMPL_NODE == pcompl->type) {
  693. printf("%s\n", node->name);
  694. if (help) {
  695. if (!node->dsc) {
  696. printf("%s\n", node->name);
  697. } else {
  698. char *dsc = faux_str_getline(node->dsc,
  699. NULL);
  700. printf("%s\n", dsc);
  701. faux_str_free(dsc);
  702. }
  703. }
  704. continue;
  705. }
  706. // Type
  707. if (node->nodetype & LYS_LEAF)
  708. type = ((struct lysc_node_leaf *)node)->type;
  709. else if (node->nodetype & LYS_LEAFLIST)
  710. type = ((struct lysc_node_leaflist *)node)->type;
  711. else
  712. continue;
  713. if (help)
  714. pline_print_type_help(node, type);
  715. else
  716. pline_print_type_completions(type);
  717. }
  718. }