pline.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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_CHOICE | LYS_CASE)) {
  138. pline_add_compl_subtree(pline, module, iter);
  139. continue;
  140. }
  141. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  142. }
  143. }
  144. void pline_debug(pline_t *pline)
  145. {
  146. faux_list_node_t *iter = NULL;
  147. pexpr_t *pexpr = NULL;
  148. pcompl_t *pcompl = NULL;
  149. printf("====== Pline:\n\n");
  150. printf("invalid = %s\n", pline->invalid ? "true" : "false");
  151. printf("\n");
  152. printf("=== Expressions:\n\n");
  153. iter = faux_list_head(pline->exprs);
  154. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  155. char *pat = NULL;
  156. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  157. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  158. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  159. switch (pexpr->pat) {
  160. case 0x0001:
  161. pat = "NONE";
  162. break;
  163. case 0x0002:
  164. pat = "CONTAINER";
  165. break;
  166. case 0x0004:
  167. pat = "LIST";
  168. break;
  169. case 0x0008:
  170. pat = "LIST_KEY";
  171. break;
  172. case 0x0010:
  173. pat = "LIST_KEY_INCOMPLETED";
  174. break;
  175. case 0x0020:
  176. pat = "LEAF";
  177. break;
  178. case 0x0040:
  179. pat = "LEAF_VALUE";
  180. break;
  181. case 0x0080:
  182. pat = "LEAF_EMPTY";
  183. break;
  184. case 0x0100:
  185. pat = "LEAFLIST";
  186. break;
  187. case 0x0200:
  188. pat = "LEAFLIST_VALUE";
  189. break;
  190. default:
  191. pat = "UNKNOWN";
  192. break;
  193. }
  194. printf("pexpr.pat = %s\n", pat);
  195. printf("pexpr.args_num = %lu\n", pexpr->args_num);
  196. printf("pexpr.list_pos = %lu\n", pexpr->list_pos);
  197. printf("pexpr.last_keys = %s\n", pexpr->last_keys ? pexpr->last_keys : "NULL");
  198. printf("\n");
  199. }
  200. printf("=== Completions:\n\n");
  201. iter = faux_list_head(pline->compls);
  202. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  203. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  204. "PCOMPL_NODE" : "PCOMPL_TYPE");
  205. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  206. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  207. printf("\n");
  208. }
  209. }
  210. // Don't use standard lys_find_child() because it checks given module to be
  211. // equal to found node's module. So augmented nodes will not be found.
  212. static const struct lysc_node *find_child(const struct lysc_node *node,
  213. const char *name)
  214. {
  215. const struct lysc_node *iter = NULL;
  216. if (!node)
  217. return NULL;
  218. LY_LIST_FOR(node, iter) {
  219. if (!(iter->nodetype & SRP_NODETYPE_CONF))
  220. continue;
  221. if (!(iter->flags & LYS_CONFIG_W))
  222. continue;
  223. // Special case. LYS_CHOICE and LYS_CASE must search for
  224. // specified name inside themselfs.
  225. if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
  226. const struct lysc_node *node_in = NULL;
  227. node_in = find_child(lysc_node_child(iter), name);
  228. if (node_in)
  229. return node_in;
  230. continue;
  231. }
  232. if (!faux_str_cmp(iter->name, name))
  233. return iter;
  234. }
  235. return NULL;
  236. }
  237. static struct lysc_ident *find_ident(struct lysc_ident *ident, const char *name)
  238. {
  239. LY_ARRAY_COUNT_TYPE u = 0;
  240. if (!ident)
  241. return NULL;
  242. if (!ident->derived) {
  243. if (!faux_str_cmp(name, ident->name))
  244. return ident;
  245. return NULL;
  246. }
  247. LY_ARRAY_FOR(ident->derived, u) {
  248. struct lysc_ident *identity = find_ident(ident->derived[u], name);
  249. if (identity)
  250. return identity;
  251. }
  252. return NULL;
  253. }
  254. static const char *identityref_prefix(struct lysc_type_identityref *type,
  255. const char *name)
  256. {
  257. LY_ARRAY_COUNT_TYPE u = 0;
  258. assert(type);
  259. LY_ARRAY_FOR(type->bases, u) {
  260. struct lysc_ident *identity = find_ident(type->bases[u], name);
  261. if (identity)
  262. return identity->module->name;
  263. }
  264. return NULL;
  265. }
  266. size_t list_num_of_keys(const struct lysc_node *node)
  267. {
  268. const struct lysc_node *iter = NULL;
  269. size_t num = 0;
  270. assert(node);
  271. if (!node)
  272. return 0;
  273. if (!(node->nodetype & LYS_LIST))
  274. return 0;
  275. LY_LIST_FOR(lysc_node_child(node), iter) {
  276. if (!(iter->nodetype & LYS_LEAF))
  277. continue;
  278. if (!(iter->flags & LYS_KEY))
  279. continue;
  280. num++;
  281. }
  282. return num;
  283. }
  284. // Get module name by internal prefix. Sysrepo requests use module names but not
  285. // prefixes.
  286. static const char *module_by_prefix(const struct lysp_module *parsed, const char *prefix)
  287. {
  288. LY_ARRAY_COUNT_TYPE u = 0;
  289. if (!parsed)
  290. return NULL;
  291. if (!prefix)
  292. return NULL;
  293. // Try prefix of module itself
  294. if (faux_str_cmp(prefix, parsed->mod->prefix) == 0)
  295. return parsed->mod->name;
  296. // Try imported modules
  297. LY_ARRAY_FOR(parsed->imports, u) {
  298. const struct lysp_import *import = &parsed->imports[u];
  299. if (faux_str_cmp(prefix, import->prefix) == 0)
  300. return import->name;
  301. }
  302. return NULL;
  303. }
  304. static char *remap_xpath_prefixes(const char *orig_xpath, const struct lysp_module *parsed)
  305. {
  306. char *remaped = NULL;
  307. const char *pos = orig_xpath;
  308. const char *start = orig_xpath;
  309. char *cached_prefix = NULL;
  310. char *cached_module = NULL;
  311. if (!orig_xpath)
  312. return NULL;
  313. while (*pos != '\0') {
  314. if (*pos == '/') {
  315. faux_str_catn(&remaped, start, pos - start + 1);
  316. start = pos + 1;
  317. } else if (*pos == ':') {
  318. if (pos != start) {
  319. char *prefix = faux_str_dupn(start, pos - start);
  320. if (cached_prefix && (faux_str_cmp(prefix, cached_prefix) == 0)) {
  321. faux_str_cat(&remaped, cached_module);
  322. faux_str_free(prefix);
  323. } else {
  324. const char *module = module_by_prefix(parsed, prefix);
  325. if (module) {
  326. faux_str_cat(&remaped, module);
  327. faux_str_free(cached_prefix);
  328. faux_str_free(cached_module);
  329. cached_prefix = prefix;
  330. cached_module = faux_str_dup(module);
  331. } else {
  332. faux_str_cat(&remaped, prefix);
  333. faux_str_free(prefix);
  334. }
  335. }
  336. }
  337. faux_str_cat(&remaped, ":");
  338. start = pos + 1;
  339. }
  340. pos++;
  341. }
  342. if (start != pos)
  343. faux_str_catn(&remaped, start, pos - start);
  344. faux_str_free(cached_prefix);
  345. faux_str_free(cached_module);
  346. return remaped;
  347. }
  348. static const char *cut_front_ups(const char *orig_xpath, size_t *up_num)
  349. {
  350. const char *xpath = orig_xpath;
  351. const char *needle = "../";
  352. size_t needle_len = strlen(needle);
  353. size_t num = 0;
  354. if (!xpath)
  355. return NULL;
  356. while (faux_str_cmpn(xpath, needle, needle_len) == 0) {
  357. num++;
  358. xpath += needle_len;
  359. }
  360. if (up_num)
  361. *up_num = num;
  362. return xpath;
  363. }
  364. static char *cut_trailing_components(const char *orig_xpath, size_t up_num)
  365. {
  366. const char *xpath = NULL;
  367. char *res = NULL;
  368. size_t num = 0;
  369. if (!orig_xpath)
  370. return NULL;
  371. xpath = orig_xpath + strlen(orig_xpath);
  372. while (xpath >= orig_xpath) {
  373. if (*xpath == '/')
  374. num++;
  375. if (num == up_num) {
  376. res = faux_str_dupn(orig_xpath, xpath - orig_xpath + 1);
  377. break;
  378. }
  379. xpath--;
  380. }
  381. return res;
  382. }
  383. static char *leafref_xpath(const struct lysc_node *node, const char *node_path)
  384. {
  385. char *compl_xpath = NULL;
  386. const struct lysc_type *type = NULL;
  387. const struct lysc_type_leafref *leafref = NULL;
  388. const char *orig_xpath = NULL;
  389. char *remaped_xpath = NULL;
  390. const char *tmp = NULL;
  391. size_t up_num = 0;
  392. if (!node)
  393. return NULL;
  394. if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  395. return NULL;
  396. if (node->nodetype & LYS_LEAF)
  397. type = ((const struct lysc_node_leaf *)node)->type;
  398. else
  399. type = ((const struct lysc_node_leaflist *)node)->type;
  400. if (type->basetype != LY_TYPE_LEAFREF)
  401. return NULL;
  402. leafref = (const struct lysc_type_leafref *)type;
  403. orig_xpath = lyxp_get_expr(leafref->path);
  404. if (!orig_xpath)
  405. return NULL;
  406. remaped_xpath = remap_xpath_prefixes(orig_xpath, node->module->parsed);
  407. if (remaped_xpath[0] == '/') // Absolute path
  408. return remaped_xpath;
  409. // Relative path
  410. if (!node_path) {
  411. faux_str_free(remaped_xpath);
  412. return NULL;
  413. }
  414. tmp = cut_front_ups(remaped_xpath, &up_num);
  415. compl_xpath = cut_trailing_components(node_path, up_num);
  416. if (!compl_xpath) {
  417. faux_str_free(remaped_xpath);
  418. return NULL;
  419. }
  420. faux_str_cat(&compl_xpath, tmp);
  421. faux_str_free(remaped_xpath);
  422. return compl_xpath;
  423. }
  424. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  425. pline_t *pline, pline_opts_t *opts)
  426. {
  427. faux_argv_node_t *arg = faux_argv_iter(argv);
  428. const struct lysc_node *node = NULL;
  429. char *rollback_xpath = NULL;
  430. size_t rollback_args_num = 0;
  431. size_t rollback_list_pos = 0;
  432. // Rollback is a mechanism to roll to previous node while
  433. // oneliners parsing
  434. bool_t rollback = BOOL_FALSE;
  435. pexpr_t *first_pexpr = NULL;
  436. // It's necessary because upper function can use the same pline object
  437. // for another modules before. It uses the same object to collect
  438. // possible completions. But pline is really invalid only when all
  439. // modules don't recognize argument.
  440. pline->invalid = BOOL_FALSE;
  441. do {
  442. pexpr_t *pexpr = pline_current_expr(pline);
  443. const char *str = (const char *)faux_argv_current(arg);
  444. bool_t is_rollback = rollback;
  445. bool_t next_arg = BOOL_TRUE;
  446. rollback = BOOL_FALSE;
  447. if (node && !is_rollback) {
  448. char *tmp = NULL;
  449. // Save rollback Xpath (for oneliners) before leaf node
  450. // Only leaf and leaf-list node allows to "rollback"
  451. // the path and add additional statements
  452. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  453. faux_str_free(rollback_xpath);
  454. rollback_xpath = faux_str_dup(pexpr->xpath);
  455. rollback_args_num = pexpr->args_num;
  456. rollback_list_pos = pexpr->list_pos;
  457. }
  458. // Add current node to Xpath
  459. tmp = faux_str_sprintf("/%s:%s",
  460. node->module->name, node->name);
  461. faux_str_cat(&pexpr->xpath, tmp);
  462. faux_str_free(tmp);
  463. pexpr->args_num++;
  464. // Activate current expression. Because it really has
  465. // new component
  466. pexpr->active = BOOL_TRUE;
  467. }
  468. // Root of the module
  469. if (!node) {
  470. // Completion
  471. if (!str) {
  472. pline_add_compl_subtree(pline, module, node);
  473. break;
  474. }
  475. // Next element
  476. node = find_child(module->compiled->data, str);
  477. if (!node)
  478. break;
  479. // Container
  480. } else if (node->nodetype & LYS_CONTAINER) {
  481. pexpr->pat = PAT_CONTAINER;
  482. // Completion
  483. if (!str) {
  484. pline_add_compl_subtree(pline, module, node);
  485. break;
  486. }
  487. // Next element
  488. node = find_child(lysc_node_child(node), str);
  489. // List
  490. } else if (node->nodetype & LYS_LIST) {
  491. const struct lysc_node *iter = NULL;
  492. pexpr->pat = PAT_LIST;
  493. pexpr->list_pos = pexpr->args_num;
  494. faux_str_free(pexpr->last_keys);
  495. pexpr->last_keys = NULL;
  496. // Next element
  497. if (!is_rollback) {
  498. bool_t break_upper_loop = BOOL_FALSE;
  499. bool_t first_key = BOOL_TRUE;
  500. LY_LIST_FOR(lysc_node_child(node), iter) {
  501. char *tmp = NULL;
  502. char *escaped = NULL;
  503. struct lysc_node_leaf *leaf =
  504. (struct lysc_node_leaf *)iter;
  505. if (!(iter->nodetype & LYS_LEAF))
  506. continue;
  507. if (!(iter->flags & LYS_KEY))
  508. continue;
  509. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  510. // Parse statement if necessary
  511. if (opts->keys_w_stmt &&
  512. (!first_key ||
  513. (first_key && opts->first_key_w_stmt))) {
  514. // Completion
  515. if (!str) {
  516. pline_add_compl(pline,
  517. PCOMPL_NODE, iter, NULL);
  518. break_upper_loop = BOOL_TRUE;
  519. break;
  520. }
  521. pexpr->args_num++;
  522. faux_argv_each(&arg);
  523. str = (const char *)faux_argv_current(arg);
  524. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  525. }
  526. first_key = BOOL_FALSE;
  527. // Completion
  528. if (!str) {
  529. char *tmp = NULL;
  530. char *compl_xpath = NULL;
  531. tmp = faux_str_sprintf("%s/%s",
  532. pexpr->xpath, leaf->name);
  533. pline_add_compl(pline,
  534. PCOMPL_TYPE, iter, tmp);
  535. compl_xpath = leafref_xpath(iter, tmp);
  536. if (compl_xpath) {
  537. pline_add_compl(pline, PCOMPL_TYPE,
  538. NULL, compl_xpath);
  539. faux_str_free(compl_xpath);
  540. }
  541. faux_str_free(tmp);
  542. break_upper_loop = BOOL_TRUE;
  543. break;
  544. }
  545. escaped = faux_str_c_esc(str);
  546. tmp = faux_str_sprintf("[%s=\"%s\"]",
  547. leaf->name, escaped);
  548. faux_str_free(escaped);
  549. faux_str_cat(&pexpr->xpath, tmp);
  550. faux_str_cat(&pexpr->last_keys, tmp);
  551. faux_str_free(tmp);
  552. pexpr->args_num++;
  553. faux_argv_each(&arg);
  554. str = (const char *)faux_argv_current(arg);
  555. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  556. }
  557. if (break_upper_loop)
  558. break;
  559. }
  560. pexpr->pat = PAT_LIST_KEY;
  561. // Completion
  562. if (!str) {
  563. pline_add_compl_subtree(pline, module, node);
  564. break;
  565. }
  566. // Next element
  567. node = find_child(lysc_node_child(node), str);
  568. // Leaf
  569. } else if (node->nodetype & LYS_LEAF) {
  570. struct lysc_node_leaf *leaf =
  571. (struct lysc_node_leaf *)node;
  572. // Next element
  573. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  574. pexpr->pat = PAT_LEAF_EMPTY;
  575. // Completion
  576. if (!str) {
  577. pline_add_compl_subtree(pline,
  578. module, node->parent);
  579. break;
  580. }
  581. // Don't get next argument when argument is not
  582. // really consumed
  583. next_arg = BOOL_FALSE;
  584. } else {
  585. pexpr->pat = PAT_LEAF;
  586. // Completion
  587. if (!str) {
  588. char *compl_xpath = leafref_xpath(node, pexpr->xpath);
  589. pline_add_compl(pline,
  590. PCOMPL_TYPE, node, compl_xpath);
  591. faux_str_free(compl_xpath);
  592. break;
  593. }
  594. pexpr->pat = PAT_LEAF_VALUE;
  595. // Idenity must have prefix
  596. if (LY_TYPE_IDENT == leaf->type->basetype) {
  597. const char *prefix = NULL;
  598. prefix = identityref_prefix(
  599. (struct lysc_type_identityref *)
  600. leaf->type, str);
  601. if (prefix)
  602. pexpr->value = faux_str_sprintf(
  603. "%s:", prefix);
  604. }
  605. faux_str_cat(&pexpr->value, str);
  606. }
  607. // Expression was completed
  608. // So rollback (for oneliners)
  609. node = node->parent;
  610. pline_add_expr(pline, rollback_xpath,
  611. rollback_args_num, rollback_list_pos);
  612. rollback = BOOL_TRUE;
  613. // Leaf-list
  614. } else if (node->nodetype & LYS_LEAFLIST) {
  615. char *tmp = NULL;
  616. const char *prefix = NULL;
  617. struct lysc_node_leaflist *leaflist =
  618. (struct lysc_node_leaflist *)node;
  619. pexpr->pat = PAT_LEAFLIST;
  620. pexpr->list_pos = pexpr->args_num;
  621. faux_str_free(pexpr->last_keys);
  622. pexpr->last_keys = NULL;
  623. // Completion
  624. if (!str) {
  625. char *compl_xpath = leafref_xpath(node, pexpr->xpath);
  626. if (compl_xpath) {
  627. pline_add_compl(pline,
  628. PCOMPL_TYPE, NULL, compl_xpath);
  629. faux_str_free(compl_xpath);
  630. }
  631. pline_add_compl(pline,
  632. PCOMPL_TYPE, node, pexpr->xpath);
  633. break;
  634. }
  635. pexpr->pat = PAT_LEAFLIST_VALUE;
  636. // Idenity must have prefix
  637. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  638. prefix = identityref_prefix(
  639. (struct lysc_type_identityref *)
  640. leaflist->type, str);
  641. }
  642. tmp = faux_str_sprintf("[.='%s%s%s']",
  643. prefix ? prefix : "", prefix ? ":" : "", str);
  644. faux_str_cat(&pexpr->xpath, tmp);
  645. faux_str_cat(&pexpr->last_keys, str);
  646. faux_str_free(tmp);
  647. pexpr->args_num++;
  648. // Expression was completed
  649. // So rollback (for oneliners)
  650. node = node->parent;
  651. pline_add_expr(pline, rollback_xpath,
  652. rollback_args_num, rollback_list_pos);
  653. rollback = BOOL_TRUE;
  654. // LYS_CHOICE and LYS_CASE can appear while rollback only
  655. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  656. // Don't set pexpr->pat because CHOICE and CASE can't
  657. // appear within data tree (schema only)
  658. // Completion
  659. if (!str) {
  660. pline_add_compl_subtree(pline, module, node);
  661. break;
  662. }
  663. // Next element
  664. node = find_child(lysc_node_child(node), str);
  665. } else {
  666. break;
  667. }
  668. // Current argument was not consumed.
  669. // Break before getting next arg.
  670. if (!node && !rollback)
  671. break;
  672. if (next_arg)
  673. faux_argv_each(&arg);
  674. } while (BOOL_TRUE);
  675. // There is not-consumed argument so whole pline is invalid
  676. if (faux_argv_current(arg))
  677. pline->invalid = BOOL_TRUE;
  678. faux_str_free(rollback_xpath);
  679. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  680. if (!first_pexpr || !first_pexpr->xpath)
  681. return BOOL_FALSE; // Not found
  682. return BOOL_TRUE;
  683. }
  684. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
  685. {
  686. const struct ly_ctx *ctx = NULL;
  687. struct lys_module *module = NULL;
  688. pline_t *pline = NULL;
  689. uint32_t i = 0;
  690. faux_list_node_t *last_expr_node = NULL;
  691. assert(sess);
  692. if (!sess)
  693. return NULL;
  694. pline = pline_new(sess);
  695. if (!pline)
  696. return NULL;
  697. ctx = sr_session_acquire_context(pline->sess);
  698. if (!ctx)
  699. return NULL;
  700. // Iterate all modules
  701. i = 0;
  702. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  703. if (sr_module_is_internal(module))
  704. continue;
  705. if (!module->compiled)
  706. continue;
  707. if (!module->implemented)
  708. continue;
  709. if (!module->compiled->data)
  710. continue;
  711. if (pline_parse_module(module, argv, pline, opts))
  712. break; // Found
  713. }
  714. sr_session_release_context(pline->sess);
  715. // Last parsed expression can be inactive so remove it from list
  716. last_expr_node = faux_list_tail(pline->exprs);
  717. if (last_expr_node) {
  718. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  719. if (!expr->active)
  720. faux_list_del(pline->exprs, last_expr_node);
  721. }
  722. return pline;
  723. }
  724. static void identityref(struct lysc_ident *ident)
  725. {
  726. LY_ARRAY_COUNT_TYPE u = 0;
  727. if (!ident)
  728. return;
  729. if (!ident->derived) {
  730. printf("%s\n", ident->name);
  731. return;
  732. }
  733. LY_ARRAY_FOR(ident->derived, u) {
  734. identityref(ident->derived[u]);
  735. }
  736. }
  737. static void pline_print_type_completions(const struct lysc_type *type)
  738. {
  739. assert(type);
  740. switch (type->basetype) {
  741. case LY_TYPE_BOOL: {
  742. printf("true\nfalse\n");
  743. break;
  744. }
  745. case LY_TYPE_ENUM: {
  746. const struct lysc_type_enum *t =
  747. (const struct lysc_type_enum *)type;
  748. LY_ARRAY_COUNT_TYPE u = 0;
  749. LY_ARRAY_FOR(t->enums, u) {
  750. printf("%s\n",t->enums[u].name);
  751. }
  752. break;
  753. }
  754. case LY_TYPE_IDENT: {
  755. struct lysc_type_identityref *t =
  756. (struct lysc_type_identityref *)type;
  757. LY_ARRAY_COUNT_TYPE u = 0;
  758. LY_ARRAY_FOR(t->bases, u) {
  759. identityref(t->bases[u]);
  760. }
  761. break;
  762. }
  763. case LY_TYPE_UNION: {
  764. struct lysc_type_union *t =
  765. (struct lysc_type_union *)type;
  766. LY_ARRAY_COUNT_TYPE u = 0;
  767. LY_ARRAY_FOR(t->types, u) {
  768. pline_print_type_completions(t->types[u]);
  769. }
  770. break;
  771. }
  772. default:
  773. break;
  774. }
  775. }
  776. static void pline_print_type_help(const struct lysc_node *node,
  777. const struct lysc_type *type)
  778. {
  779. assert(type);
  780. if ((type->basetype != LY_TYPE_UNION) &&
  781. (type->basetype != LY_TYPE_LEAFREF))
  782. printf("%s\n", node->name);
  783. switch (type->basetype) {
  784. case LY_TYPE_UINT8: {
  785. printf("Unsigned integer 8bit\n");
  786. break;
  787. }
  788. case LY_TYPE_UINT16: {
  789. printf("Unsigned integer 16bit\n");
  790. break;
  791. }
  792. case LY_TYPE_UINT32: {
  793. printf("Unsigned integer 32bit\n");
  794. break;
  795. }
  796. case LY_TYPE_UINT64: {
  797. printf("Unsigned integer 64bit\n");
  798. break;
  799. }
  800. case LY_TYPE_INT8: {
  801. printf("Integer 8bit\n");
  802. break;
  803. }
  804. case LY_TYPE_INT16: {
  805. printf("Integer 16bit\n");
  806. break;
  807. }
  808. case LY_TYPE_INT32: {
  809. printf("Integer 32bit\n");
  810. break;
  811. }
  812. case LY_TYPE_INT64: {
  813. printf("Integer 64bit\n");
  814. break;
  815. }
  816. case LY_TYPE_STRING: {
  817. printf("String\n");
  818. break;
  819. }
  820. case LY_TYPE_BOOL: {
  821. printf("Boolean true/false\n");
  822. break;
  823. }
  824. case LY_TYPE_DEC64: {
  825. printf("Signed decimal number\n");
  826. break;
  827. }
  828. case LY_TYPE_ENUM: {
  829. printf("Enumerated choice\n");
  830. break;
  831. }
  832. case LY_TYPE_IDENT: {
  833. printf("Identity\n");
  834. break;
  835. }
  836. case LY_TYPE_UNION: {
  837. struct lysc_type_union *t =
  838. (struct lysc_type_union *)type;
  839. LY_ARRAY_COUNT_TYPE u = 0;
  840. LY_ARRAY_FOR(t->types, u) {
  841. pline_print_type_help(node, t->types[u]);
  842. }
  843. break;
  844. }
  845. case LY_TYPE_LEAFREF: {
  846. struct lysc_type_leafref *t =
  847. (struct lysc_type_leafref *)type;
  848. pline_print_type_help(node, t->realtype);
  849. }
  850. break;
  851. default:
  852. printf("Unknown\n");
  853. break;
  854. }
  855. }
  856. void pline_print_completions(const pline_t *pline, bool_t help)
  857. {
  858. faux_list_node_t *iter = NULL;
  859. pcompl_t *pcompl = NULL;
  860. iter = faux_list_head(pline->compls);
  861. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  862. struct lysc_type *type = NULL;
  863. const struct lysc_node *node = pcompl->node;
  864. if (pcompl->xpath && !help) {
  865. sr_val_t *vals = NULL;
  866. size_t val_num = 0;
  867. size_t i = 0;
  868. //printf("%s\n", pcompl->xpath);
  869. sr_get_items(pline->sess, pcompl->xpath,
  870. 0, 0, &vals, &val_num);
  871. for (i = 0; i < val_num; i++) {
  872. char *tmp = sr_val_to_str(&vals[i]);
  873. if (!tmp)
  874. continue;
  875. printf("%s\n", tmp);
  876. free(tmp);
  877. }
  878. sr_free_values(vals, val_num);
  879. }
  880. if (!node)
  881. continue;
  882. // Node
  883. if (PCOMPL_NODE == pcompl->type) {
  884. printf("%s\n", node->name);
  885. if (help) {
  886. if (!node->dsc) {
  887. printf("%s\n", node->name);
  888. } else {
  889. char *dsc = faux_str_getline(node->dsc,
  890. NULL);
  891. printf("%s\n", dsc);
  892. faux_str_free(dsc);
  893. }
  894. }
  895. continue;
  896. }
  897. // Type
  898. if (node->nodetype & LYS_LEAF)
  899. type = ((struct lysc_node_leaf *)node)->type;
  900. else if (node->nodetype & LYS_LEAFLIST)
  901. type = ((struct lysc_node_leaflist *)node)->type;
  902. else
  903. continue;
  904. if (help)
  905. pline_print_type_help(node, type);
  906. else
  907. pline_print_type_completions(type);
  908. }
  909. }