pline.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 <syslog.h>
  9. #include <faux/faux.h>
  10. #include <faux/str.h>
  11. #include <faux/list.h>
  12. #include <faux/argv.h>
  13. #include <sysrepo.h>
  14. #include <sysrepo/xpath.h>
  15. #include <sysrepo/values.h>
  16. #include <libyang/tree_edit.h>
  17. #include "private.h"
  18. #include "pline.h"
  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, const 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 & SRP_NODETYPE_CONF))
  134. continue;
  135. if (!(iter->flags & LYS_CONFIG_W))
  136. continue;
  137. if ((iter->nodetype & LYS_LEAF) && (iter->flags & LYS_KEY))
  138. continue;
  139. if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
  140. pline_add_compl_subtree(pline, module, iter);
  141. continue;
  142. }
  143. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  144. }
  145. }
  146. void pline_debug(pline_t *pline)
  147. {
  148. faux_list_node_t *iter = NULL;
  149. pexpr_t *pexpr = NULL;
  150. pcompl_t *pcompl = NULL;
  151. printf("====== Pline:\n\n");
  152. printf("invalid = %s\n", pline->invalid ? "true" : "false");
  153. printf("\n");
  154. printf("=== Expressions:\n\n");
  155. iter = faux_list_head(pline->exprs);
  156. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  157. char *pat = NULL;
  158. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  159. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  160. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  161. switch (pexpr->pat) {
  162. case 0x0001:
  163. pat = "NONE";
  164. break;
  165. case 0x0002:
  166. pat = "CONTAINER";
  167. break;
  168. case 0x0004:
  169. pat = "LIST";
  170. break;
  171. case 0x0008:
  172. pat = "LIST_KEY";
  173. break;
  174. case 0x0010:
  175. pat = "LIST_KEY_INCOMPLETED";
  176. break;
  177. case 0x0020:
  178. pat = "LEAF";
  179. break;
  180. case 0x0040:
  181. pat = "LEAF_VALUE";
  182. break;
  183. case 0x0080:
  184. pat = "LEAF_EMPTY";
  185. break;
  186. case 0x0100:
  187. pat = "LEAFLIST";
  188. break;
  189. case 0x0200:
  190. pat = "LEAFLIST_VALUE";
  191. break;
  192. default:
  193. pat = "UNKNOWN";
  194. break;
  195. }
  196. printf("pexpr.pat = %s\n", pat);
  197. printf("pexpr.args_num = %lu\n", pexpr->args_num);
  198. printf("pexpr.list_pos = %lu\n", pexpr->list_pos);
  199. printf("pexpr.last_keys = %s\n", pexpr->last_keys ? pexpr->last_keys : "NULL");
  200. printf("\n");
  201. }
  202. printf("=== Completions:\n\n");
  203. iter = faux_list_head(pline->compls);
  204. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  205. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  206. "PCOMPL_NODE" : "PCOMPL_TYPE");
  207. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  208. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  209. printf("\n");
  210. }
  211. }
  212. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  213. pline_t *pline, pline_opts_t *opts)
  214. {
  215. faux_argv_node_t *arg = faux_argv_iter(argv);
  216. const struct lysc_node *node = NULL;
  217. char *rollback_xpath = NULL;
  218. size_t rollback_args_num = 0;
  219. size_t rollback_list_pos = 0;
  220. // Rollback is a mechanism to roll to previous node while
  221. // oneliners parsing
  222. bool_t rollback = BOOL_FALSE;
  223. pexpr_t *first_pexpr = NULL;
  224. // It's necessary because upper function can use the same pline object
  225. // for another modules before. It uses the same object to collect
  226. // possible completions. But pline is really invalid only when all
  227. // modules don't recognize argument.
  228. pline->invalid = BOOL_FALSE;
  229. do {
  230. pexpr_t *pexpr = pline_current_expr(pline);
  231. const char *str = (const char *)faux_argv_current(arg);
  232. bool_t is_rollback = rollback;
  233. bool_t next_arg = BOOL_TRUE;
  234. rollback = BOOL_FALSE;
  235. if (node && !is_rollback) {
  236. char *tmp = NULL;
  237. // Save rollback Xpath (for oneliners) before leaf node
  238. // Only leaf and leaf-list node allows to "rollback"
  239. // the path and add additional statements
  240. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  241. faux_str_free(rollback_xpath);
  242. rollback_xpath = faux_str_dup(pexpr->xpath);
  243. rollback_args_num = pexpr->args_num;
  244. rollback_list_pos = pexpr->list_pos;
  245. }
  246. // Add current node to Xpath
  247. tmp = faux_str_sprintf("/%s:%s",
  248. node->module->name, node->name);
  249. faux_str_cat(&pexpr->xpath, tmp);
  250. faux_str_free(tmp);
  251. pexpr->args_num++;
  252. // Activate current expression. Because it really has
  253. // new component
  254. pexpr->active = BOOL_TRUE;
  255. }
  256. // Root of the module
  257. if (!node) {
  258. // Completion
  259. if (!str) {
  260. pline_add_compl_subtree(pline, module, node);
  261. break;
  262. }
  263. // Next element
  264. node = klysc_find_child(module->compiled->data, str);
  265. if (!node)
  266. break;
  267. // Container
  268. } else if (node->nodetype & LYS_CONTAINER) {
  269. pexpr->pat = PAT_CONTAINER;
  270. // Completion
  271. if (!str) {
  272. pline_add_compl_subtree(pline, module, node);
  273. break;
  274. }
  275. // Next element
  276. node = klysc_find_child(lysc_node_child(node), str);
  277. // List
  278. } else if (node->nodetype & LYS_LIST) {
  279. const struct lysc_node *iter = NULL;
  280. pexpr->pat = PAT_LIST;
  281. pexpr->list_pos = pexpr->args_num;
  282. faux_str_free(pexpr->last_keys);
  283. pexpr->last_keys = NULL;
  284. // Next element
  285. if (!is_rollback) {
  286. bool_t break_upper_loop = BOOL_FALSE;
  287. // Keys without statement. Positional parameters.
  288. if (!opts->keys_w_stmt) {
  289. LY_LIST_FOR(lysc_node_child(node), iter) {
  290. char *tmp = NULL;
  291. char *escaped = NULL;
  292. struct lysc_node_leaf *leaf =
  293. (struct lysc_node_leaf *)iter;
  294. if (!(iter->nodetype & LYS_LEAF))
  295. continue;
  296. if (!(iter->flags & LYS_KEY))
  297. continue;
  298. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  299. // Completion
  300. if (!str) {
  301. char *tmp = NULL;
  302. char *compl_xpath = NULL;
  303. tmp = faux_str_sprintf("%s/%s",
  304. pexpr->xpath, leaf->name);
  305. pline_add_compl(pline,
  306. PCOMPL_TYPE, iter, tmp);
  307. compl_xpath = klysc_leafref_xpath(iter, tmp);
  308. if (compl_xpath) {
  309. pline_add_compl(pline, PCOMPL_TYPE,
  310. NULL, compl_xpath);
  311. faux_str_free(compl_xpath);
  312. }
  313. faux_str_free(tmp);
  314. break_upper_loop = BOOL_TRUE;
  315. break;
  316. }
  317. escaped = faux_str_c_esc(str);
  318. tmp = faux_str_sprintf("[%s=\"%s\"]",
  319. leaf->name, escaped);
  320. faux_str_free(escaped);
  321. faux_str_cat(&pexpr->xpath, tmp);
  322. faux_str_cat(&pexpr->last_keys, tmp);
  323. faux_str_free(tmp);
  324. pexpr->args_num++;
  325. faux_argv_each(&arg);
  326. str = (const char *)faux_argv_current(arg);
  327. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  328. }
  329. // Keys with statements. Arbitrary order of keys.
  330. } else {
  331. faux_list_t *keys = NULL;
  332. unsigned int specified_keys_num = 0;
  333. klysc_key_t *cur_key = NULL;
  334. bool_t first_key = BOOL_TRUE;
  335. bool_t first_key_is_optional = BOOL_FALSE;
  336. faux_list_node_t *key_iter = NULL;
  337. // List keys
  338. keys = faux_list_new(FAUX_LIST_UNSORTED,
  339. FAUX_LIST_UNIQUE,
  340. klysc_key_compare,
  341. klysc_key_kcompare,
  342. (faux_list_free_fn)faux_free);
  343. LY_LIST_FOR(lysc_node_child(node), iter) {
  344. struct lysc_node_leaf *leaf =
  345. (struct lysc_node_leaf *)iter;
  346. klysc_key_t *key = NULL;
  347. if (!(iter->nodetype & LYS_LEAF))
  348. continue;
  349. if (!(iter->flags & LYS_KEY))
  350. continue;
  351. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  352. key = faux_zmalloc(sizeof(*key));
  353. assert(key);
  354. key->node = iter;
  355. if (opts->default_keys &&
  356. (key->dflt = klysc_node_ext_default(iter))) {
  357. if (first_key)
  358. first_key_is_optional = BOOL_TRUE;
  359. }
  360. faux_list_add(keys, key);
  361. first_key = BOOL_FALSE;
  362. }
  363. while (specified_keys_num < faux_list_len(keys)) {
  364. char *tmp = NULL;
  365. char *escaped = NULL;
  366. // First key without statement. Must be mandatory.
  367. if ((0 == specified_keys_num) &&
  368. !opts->first_key_w_stmt &&
  369. !first_key_is_optional) {
  370. cur_key = (klysc_key_t *)faux_list_data(faux_list_head(keys));
  371. } else {
  372. if (!str)
  373. break;
  374. cur_key = faux_list_kfind(keys, str);
  375. if (!cur_key || cur_key->value)
  376. break;
  377. pexpr->args_num++;
  378. faux_argv_each(&arg);
  379. str = (const char *)faux_argv_current(arg);
  380. }
  381. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  382. // Completion
  383. if (!str) {
  384. char *tmp = NULL;
  385. char *compl_xpath = NULL;
  386. tmp = faux_str_sprintf("%s/%s",
  387. pexpr->xpath, cur_key->node->name);
  388. pline_add_compl(pline,
  389. PCOMPL_TYPE, cur_key->node, tmp);
  390. compl_xpath = klysc_leafref_xpath(iter, tmp);
  391. if (compl_xpath) {
  392. pline_add_compl(pline, PCOMPL_TYPE,
  393. NULL, compl_xpath);
  394. faux_str_free(compl_xpath);
  395. }
  396. faux_str_free(tmp);
  397. break_upper_loop = BOOL_TRUE;
  398. break;
  399. }
  400. escaped = faux_str_c_esc(str);
  401. tmp = faux_str_sprintf("[%s=\"%s\"]",
  402. cur_key->node->name, escaped);
  403. faux_str_free(escaped);
  404. faux_str_cat(&pexpr->xpath, tmp);
  405. faux_str_cat(&pexpr->last_keys, tmp);
  406. faux_str_free(tmp);
  407. cur_key->value = str;
  408. specified_keys_num++;
  409. pexpr->args_num++;
  410. faux_argv_each(&arg);
  411. str = (const char *)faux_argv_current(arg);
  412. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  413. }
  414. if (break_upper_loop) {
  415. faux_list_free(keys);
  416. break;
  417. }
  418. key_iter = faux_list_head(keys);
  419. while((cur_key = (klysc_key_t *)faux_list_each(&key_iter))) {
  420. if (cur_key->value)
  421. continue;
  422. // Completion
  423. if (!str) {
  424. // if ((0 == specified_keys_num) &&
  425. // !opts->first_key_w_stmt &&
  426. // !first_key_is_optional) {
  427. pline_add_compl(pline,
  428. PCOMPL_NODE, cur_key->node, NULL);
  429. }
  430. if (opts->default_keys && cur_key->dflt) {
  431. char *tmp = NULL;
  432. char *escaped = NULL;
  433. escaped = faux_str_c_esc(cur_key->dflt);
  434. tmp = faux_str_sprintf("[%s=\"%s\"]",
  435. cur_key->node->name, escaped);
  436. faux_str_free(escaped);
  437. faux_str_cat(&pexpr->xpath, tmp);
  438. faux_str_cat(&pexpr->last_keys, tmp);
  439. faux_str_free(tmp);
  440. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  441. } else { // Mandatory key is not specified
  442. break_upper_loop = BOOL_TRUE;
  443. }
  444. }
  445. faux_list_free(keys);
  446. }
  447. if (break_upper_loop)
  448. break;
  449. }
  450. pexpr->pat = PAT_LIST_KEY;
  451. // Completion
  452. if (!str) {
  453. pline_add_compl_subtree(pline, module, node);
  454. break;
  455. }
  456. // Next element
  457. node = klysc_find_child(lysc_node_child(node), str);
  458. // Leaf
  459. } else if (node->nodetype & LYS_LEAF) {
  460. struct lysc_node_leaf *leaf =
  461. (struct lysc_node_leaf *)node;
  462. // Next element
  463. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  464. pexpr->pat = PAT_LEAF_EMPTY;
  465. // Completion
  466. if (!str) {
  467. pline_add_compl_subtree(pline,
  468. module, node->parent);
  469. break;
  470. }
  471. // Don't get next argument when argument is not
  472. // really consumed
  473. next_arg = BOOL_FALSE;
  474. } else {
  475. pexpr->pat = PAT_LEAF;
  476. // Completion
  477. if (!str) {
  478. char *compl_xpath = klysc_leafref_xpath(node, pexpr->xpath);
  479. pline_add_compl(pline,
  480. PCOMPL_TYPE, node, compl_xpath);
  481. faux_str_free(compl_xpath);
  482. break;
  483. }
  484. pexpr->pat = PAT_LEAF_VALUE;
  485. // Idenity must have prefix
  486. if (LY_TYPE_IDENT == leaf->type->basetype) {
  487. const char *prefix = NULL;
  488. prefix = klysc_identityref_prefix(
  489. (struct lysc_type_identityref *)
  490. leaf->type, str);
  491. if (prefix)
  492. pexpr->value = faux_str_sprintf(
  493. "%s:", prefix);
  494. }
  495. faux_str_cat(&pexpr->value, str);
  496. }
  497. // Expression was completed
  498. // So rollback (for oneliners)
  499. node = node->parent;
  500. pline_add_expr(pline, rollback_xpath,
  501. rollback_args_num, rollback_list_pos);
  502. rollback = BOOL_TRUE;
  503. // Leaf-list
  504. } else if (node->nodetype & LYS_LEAFLIST) {
  505. char *tmp = NULL;
  506. const char *prefix = NULL;
  507. struct lysc_node_leaflist *leaflist =
  508. (struct lysc_node_leaflist *)node;
  509. pexpr->pat = PAT_LEAFLIST;
  510. pexpr->list_pos = pexpr->args_num;
  511. faux_str_free(pexpr->last_keys);
  512. pexpr->last_keys = NULL;
  513. // Completion
  514. if (!str) {
  515. char *compl_xpath = klysc_leafref_xpath(node, pexpr->xpath);
  516. if (compl_xpath) {
  517. pline_add_compl(pline,
  518. PCOMPL_TYPE, NULL, compl_xpath);
  519. faux_str_free(compl_xpath);
  520. }
  521. pline_add_compl(pline,
  522. PCOMPL_TYPE, node, pexpr->xpath);
  523. break;
  524. }
  525. pexpr->pat = PAT_LEAFLIST_VALUE;
  526. // Idenity must have prefix
  527. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  528. prefix = klysc_identityref_prefix(
  529. (struct lysc_type_identityref *)
  530. leaflist->type, str);
  531. }
  532. tmp = faux_str_sprintf("[.='%s%s%s']",
  533. prefix ? prefix : "", prefix ? ":" : "", str);
  534. faux_str_cat(&pexpr->xpath, tmp);
  535. faux_str_cat(&pexpr->last_keys, str);
  536. faux_str_free(tmp);
  537. pexpr->args_num++;
  538. // Expression was completed
  539. // So rollback (for oneliners)
  540. node = node->parent;
  541. pline_add_expr(pline, rollback_xpath,
  542. rollback_args_num, rollback_list_pos);
  543. rollback = BOOL_TRUE;
  544. // LYS_CHOICE and LYS_CASE can appear while rollback only
  545. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  546. // Don't set pexpr->pat because CHOICE and CASE can't
  547. // appear within data tree (schema only)
  548. // Completion
  549. if (!str) {
  550. pline_add_compl_subtree(pline, module, node);
  551. break;
  552. }
  553. // Next element
  554. node = klysc_find_child(lysc_node_child(node), str);
  555. } else {
  556. break;
  557. }
  558. // Current argument was not consumed.
  559. // Break before getting next arg.
  560. if (!node && !rollback)
  561. break;
  562. if (next_arg)
  563. faux_argv_each(&arg);
  564. } while (BOOL_TRUE);
  565. // There is not-consumed argument so whole pline is invalid
  566. if (faux_argv_current(arg))
  567. pline->invalid = BOOL_TRUE;
  568. faux_str_free(rollback_xpath);
  569. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  570. if (!first_pexpr || !first_pexpr->xpath)
  571. return BOOL_FALSE; // Not found
  572. return BOOL_TRUE;
  573. }
  574. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
  575. {
  576. const struct ly_ctx *ctx = NULL;
  577. struct lys_module *module = NULL;
  578. pline_t *pline = NULL;
  579. uint32_t i = 0;
  580. faux_list_node_t *last_expr_node = NULL;
  581. assert(sess);
  582. if (!sess)
  583. return NULL;
  584. pline = pline_new(sess);
  585. if (!pline)
  586. return NULL;
  587. ctx = sr_session_acquire_context(pline->sess);
  588. if (!ctx)
  589. return NULL;
  590. // Iterate all modules
  591. i = 0;
  592. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  593. if (sr_module_is_internal(module))
  594. continue;
  595. if (!module->compiled)
  596. continue;
  597. if (!module->implemented)
  598. continue;
  599. if (!module->compiled->data)
  600. continue;
  601. if (pline_parse_module(module, argv, pline, opts))
  602. break; // Found
  603. }
  604. sr_session_release_context(pline->sess);
  605. // Last parsed expression can be inactive so remove it from list
  606. last_expr_node = faux_list_tail(pline->exprs);
  607. if (last_expr_node) {
  608. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  609. if (!expr->active)
  610. faux_list_del(pline->exprs, last_expr_node);
  611. }
  612. return pline;
  613. }
  614. static void identityref(struct lysc_ident *ident)
  615. {
  616. LY_ARRAY_COUNT_TYPE u = 0;
  617. if (!ident)
  618. return;
  619. if (!ident->derived) {
  620. printf("%s\n", ident->name);
  621. return;
  622. }
  623. LY_ARRAY_FOR(ident->derived, u) {
  624. identityref(ident->derived[u]);
  625. }
  626. }
  627. static void pline_print_type_completions(const struct lysc_type *type)
  628. {
  629. assert(type);
  630. switch (type->basetype) {
  631. case LY_TYPE_BOOL: {
  632. printf("true\nfalse\n");
  633. break;
  634. }
  635. case LY_TYPE_ENUM: {
  636. const struct lysc_type_enum *t =
  637. (const struct lysc_type_enum *)type;
  638. LY_ARRAY_COUNT_TYPE u = 0;
  639. LY_ARRAY_FOR(t->enums, u) {
  640. printf("%s\n",t->enums[u].name);
  641. }
  642. break;
  643. }
  644. case LY_TYPE_IDENT: {
  645. struct lysc_type_identityref *t =
  646. (struct lysc_type_identityref *)type;
  647. LY_ARRAY_COUNT_TYPE u = 0;
  648. LY_ARRAY_FOR(t->bases, u) {
  649. identityref(t->bases[u]);
  650. }
  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_completions(t->types[u]);
  659. }
  660. break;
  661. }
  662. default:
  663. break;
  664. }
  665. }
  666. static void pline_print_type_help(const struct lysc_node *node,
  667. const struct lysc_type *type)
  668. {
  669. const char *units = NULL;
  670. assert(type);
  671. assert(node);
  672. if (LY_TYPE_UNION == type->basetype) {
  673. struct lysc_type_union *t =
  674. (struct lysc_type_union *)type;
  675. LY_ARRAY_COUNT_TYPE u = 0;
  676. LY_ARRAY_FOR(t->types, u)
  677. pline_print_type_help(node, t->types[u]);
  678. return;
  679. }
  680. if (LY_TYPE_LEAFREF == type->basetype) {
  681. struct lysc_type_leafref *t =
  682. (struct lysc_type_leafref *)type;
  683. pline_print_type_help(node, t->realtype);
  684. return;
  685. }
  686. if (node->nodetype & LYS_LEAF)
  687. units = ((struct lysc_node_leaf *)node)->units;
  688. else if (node->nodetype & LYS_LEAFLIST)
  689. units = ((struct lysc_node_leaflist *)node)->units;
  690. else
  691. return;
  692. if (units) {
  693. printf("%s\n", units);
  694. } else {
  695. switch (type->basetype) {
  696. case LY_TYPE_UINT8: {
  697. printf("<uint8>\n");
  698. break;
  699. }
  700. case LY_TYPE_UINT16: {
  701. printf("<uint16>\n");
  702. break;
  703. }
  704. case LY_TYPE_UINT32: {
  705. printf("<uint32>\n");
  706. break;
  707. }
  708. case LY_TYPE_UINT64: {
  709. printf("<uint64>\n");
  710. break;
  711. }
  712. case LY_TYPE_INT8: {
  713. printf("<int8>\n");
  714. break;
  715. }
  716. case LY_TYPE_INT16: {
  717. printf("<int16>\n");
  718. break;
  719. }
  720. case LY_TYPE_INT32: {
  721. printf("<int32>\n");
  722. break;
  723. }
  724. case LY_TYPE_INT64: {
  725. printf("<int64>\n");
  726. break;
  727. }
  728. case LY_TYPE_STRING: {
  729. printf("<string>\n");
  730. break;
  731. }
  732. case LY_TYPE_BOOL: {
  733. printf("<true/false>\n");
  734. break;
  735. }
  736. case LY_TYPE_DEC64: {
  737. printf("<number>\n");
  738. break;
  739. }
  740. case LY_TYPE_ENUM: {
  741. printf("Enumerated choice\n");
  742. break;
  743. }
  744. case LY_TYPE_IDENT: {
  745. printf("Identity\n");
  746. break;
  747. }
  748. default:
  749. printf("<unknown>\n");
  750. break;
  751. }
  752. }
  753. printf("%s\n", node->dsc ? node->dsc : node->name);
  754. }
  755. void pline_print_completions(const pline_t *pline, bool_t help)
  756. {
  757. faux_list_node_t *iter = NULL;
  758. pcompl_t *pcompl = NULL;
  759. iter = faux_list_head(pline->compls);
  760. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  761. struct lysc_type *type = NULL;
  762. const struct lysc_node *node = pcompl->node;
  763. if (pcompl->xpath && !help) {
  764. sr_val_t *vals = NULL;
  765. size_t val_num = 0;
  766. size_t i = 0;
  767. //printf("%s\n", pcompl->xpath);
  768. sr_get_items(pline->sess, pcompl->xpath,
  769. 0, 0, &vals, &val_num);
  770. for (i = 0; i < val_num; i++) {
  771. char *tmp = sr_val_to_str(&vals[i]);
  772. if (!tmp)
  773. continue;
  774. printf("%s\n", tmp);
  775. free(tmp);
  776. }
  777. sr_free_values(vals, val_num);
  778. }
  779. if (!node)
  780. continue;
  781. // Node
  782. if (PCOMPL_NODE == pcompl->type) {
  783. printf("%s\n", node->name);
  784. if (help) {
  785. if (!node->dsc) {
  786. printf("%s\n", node->name);
  787. } else {
  788. char *dsc = faux_str_getline(node->dsc,
  789. NULL);
  790. printf("%s\n", dsc);
  791. faux_str_free(dsc);
  792. }
  793. }
  794. continue;
  795. }
  796. // Type
  797. if (node->nodetype & LYS_LEAF)
  798. type = ((struct lysc_node_leaf *)node)->type;
  799. else if (node->nodetype & LYS_LEAFLIST)
  800. type = ((struct lysc_node_leaflist *)node)->type;
  801. else
  802. continue;
  803. if (help)
  804. pline_print_type_help(node, type);
  805. else
  806. pline_print_type_completions(type);
  807. }
  808. }