pline.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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 ((first_key && opts->first_key_w_stmt) ||
  512. (!first_key && opts->multi_keys_w_stmt)) {
  513. // Completion
  514. if (!str) {
  515. pline_add_compl(pline,
  516. PCOMPL_NODE, iter, NULL);
  517. break_upper_loop = BOOL_TRUE;
  518. break;
  519. }
  520. pexpr->args_num++;
  521. faux_argv_each(&arg);
  522. str = (const char *)faux_argv_current(arg);
  523. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  524. }
  525. first_key = BOOL_FALSE;
  526. // Completion
  527. if (!str) {
  528. char *tmp = NULL;
  529. char *compl_xpath = NULL;
  530. tmp = faux_str_sprintf("%s/%s",
  531. pexpr->xpath, leaf->name);
  532. pline_add_compl(pline,
  533. PCOMPL_TYPE, iter, tmp);
  534. compl_xpath = leafref_xpath(iter, tmp);
  535. if (compl_xpath) {
  536. pline_add_compl(pline, PCOMPL_TYPE,
  537. NULL, compl_xpath);
  538. faux_str_free(compl_xpath);
  539. }
  540. faux_str_free(tmp);
  541. break_upper_loop = BOOL_TRUE;
  542. break;
  543. }
  544. escaped = faux_str_c_esc(str);
  545. tmp = faux_str_sprintf("[%s=\"%s\"]",
  546. leaf->name, escaped);
  547. faux_str_free(escaped);
  548. faux_str_cat(&pexpr->xpath, tmp);
  549. faux_str_cat(&pexpr->last_keys, tmp);
  550. faux_str_free(tmp);
  551. pexpr->args_num++;
  552. faux_argv_each(&arg);
  553. str = (const char *)faux_argv_current(arg);
  554. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  555. }
  556. if (break_upper_loop)
  557. break;
  558. }
  559. pexpr->pat = PAT_LIST_KEY;
  560. // Completion
  561. if (!str) {
  562. pline_add_compl_subtree(pline, module, node);
  563. break;
  564. }
  565. // Next element
  566. node = find_child(lysc_node_child(node), str);
  567. // Leaf
  568. } else if (node->nodetype & LYS_LEAF) {
  569. struct lysc_node_leaf *leaf =
  570. (struct lysc_node_leaf *)node;
  571. // Next element
  572. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  573. pexpr->pat = PAT_LEAF_EMPTY;
  574. // Completion
  575. if (!str) {
  576. pline_add_compl_subtree(pline,
  577. module, node->parent);
  578. break;
  579. }
  580. // Don't get next argument when argument is not
  581. // really consumed
  582. next_arg = BOOL_FALSE;
  583. } else {
  584. pexpr->pat = PAT_LEAF;
  585. // Completion
  586. if (!str) {
  587. char *compl_xpath = leafref_xpath(node, pexpr->xpath);
  588. pline_add_compl(pline,
  589. PCOMPL_TYPE, node, compl_xpath);
  590. faux_str_free(compl_xpath);
  591. break;
  592. }
  593. pexpr->pat = PAT_LEAF_VALUE;
  594. // Idenity must have prefix
  595. if (LY_TYPE_IDENT == leaf->type->basetype) {
  596. const char *prefix = NULL;
  597. prefix = identityref_prefix(
  598. (struct lysc_type_identityref *)
  599. leaf->type, str);
  600. if (prefix)
  601. pexpr->value = faux_str_sprintf(
  602. "%s:", prefix);
  603. }
  604. faux_str_cat(&pexpr->value, str);
  605. }
  606. // Expression was completed
  607. // So rollback (for oneliners)
  608. node = node->parent;
  609. pline_add_expr(pline, rollback_xpath,
  610. rollback_args_num, rollback_list_pos);
  611. rollback = BOOL_TRUE;
  612. // Leaf-list
  613. } else if (node->nodetype & LYS_LEAFLIST) {
  614. char *tmp = NULL;
  615. const char *prefix = NULL;
  616. struct lysc_node_leaflist *leaflist =
  617. (struct lysc_node_leaflist *)node;
  618. pexpr->pat = PAT_LEAFLIST;
  619. pexpr->list_pos = pexpr->args_num;
  620. faux_str_free(pexpr->last_keys);
  621. pexpr->last_keys = NULL;
  622. // Completion
  623. if (!str) {
  624. char *compl_xpath = leafref_xpath(node, pexpr->xpath);
  625. if (compl_xpath) {
  626. pline_add_compl(pline,
  627. PCOMPL_TYPE, NULL, compl_xpath);
  628. faux_str_free(compl_xpath);
  629. }
  630. pline_add_compl(pline,
  631. PCOMPL_TYPE, node, pexpr->xpath);
  632. break;
  633. }
  634. pexpr->pat = PAT_LEAFLIST_VALUE;
  635. // Idenity must have prefix
  636. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  637. prefix = identityref_prefix(
  638. (struct lysc_type_identityref *)
  639. leaflist->type, str);
  640. }
  641. tmp = faux_str_sprintf("[.='%s%s%s']",
  642. prefix ? prefix : "", prefix ? ":" : "", str);
  643. faux_str_cat(&pexpr->xpath, tmp);
  644. faux_str_cat(&pexpr->last_keys, str);
  645. faux_str_free(tmp);
  646. pexpr->args_num++;
  647. // Expression was completed
  648. // So rollback (for oneliners)
  649. node = node->parent;
  650. pline_add_expr(pline, rollback_xpath,
  651. rollback_args_num, rollback_list_pos);
  652. rollback = BOOL_TRUE;
  653. // LYS_CHOICE and LYS_CASE can appear while rollback only
  654. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  655. // Don't set pexpr->pat because CHOICE and CASE can't
  656. // appear within data tree (schema only)
  657. // Completion
  658. if (!str) {
  659. pline_add_compl_subtree(pline, module, node);
  660. break;
  661. }
  662. // Next element
  663. node = find_child(lysc_node_child(node), str);
  664. } else {
  665. break;
  666. }
  667. // Current argument was not consumed.
  668. // Break before getting next arg.
  669. if (!node && !rollback)
  670. break;
  671. if (next_arg)
  672. faux_argv_each(&arg);
  673. } while (BOOL_TRUE);
  674. // There is not-consumed argument so whole pline is invalid
  675. if (faux_argv_current(arg))
  676. pline->invalid = BOOL_TRUE;
  677. faux_str_free(rollback_xpath);
  678. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  679. if (!first_pexpr || !first_pexpr->xpath)
  680. return BOOL_FALSE; // Not found
  681. return BOOL_TRUE;
  682. }
  683. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
  684. {
  685. const struct ly_ctx *ctx = NULL;
  686. struct lys_module *module = NULL;
  687. pline_t *pline = NULL;
  688. uint32_t i = 0;
  689. faux_list_node_t *last_expr_node = NULL;
  690. assert(sess);
  691. if (!sess)
  692. return NULL;
  693. pline = pline_new(sess);
  694. if (!pline)
  695. return NULL;
  696. ctx = sr_session_acquire_context(pline->sess);
  697. if (!ctx)
  698. return NULL;
  699. // Iterate all modules
  700. i = 0;
  701. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  702. if (sr_module_is_internal(module))
  703. continue;
  704. if (!module->compiled)
  705. continue;
  706. if (!module->implemented)
  707. continue;
  708. if (!module->compiled->data)
  709. continue;
  710. if (pline_parse_module(module, argv, pline, opts))
  711. break; // Found
  712. }
  713. sr_session_release_context(pline->sess);
  714. // Last parsed expression can be inactive so remove it from list
  715. last_expr_node = faux_list_tail(pline->exprs);
  716. if (last_expr_node) {
  717. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  718. if (!expr->active)
  719. faux_list_del(pline->exprs, last_expr_node);
  720. }
  721. return pline;
  722. }
  723. static void identityref(struct lysc_ident *ident)
  724. {
  725. LY_ARRAY_COUNT_TYPE u = 0;
  726. if (!ident)
  727. return;
  728. if (!ident->derived) {
  729. printf("%s\n", ident->name);
  730. return;
  731. }
  732. LY_ARRAY_FOR(ident->derived, u) {
  733. identityref(ident->derived[u]);
  734. }
  735. }
  736. static void pline_print_type_completions(const struct lysc_type *type)
  737. {
  738. assert(type);
  739. switch (type->basetype) {
  740. case LY_TYPE_BOOL: {
  741. printf("true\nfalse\n");
  742. break;
  743. }
  744. case LY_TYPE_ENUM: {
  745. const struct lysc_type_enum *t =
  746. (const struct lysc_type_enum *)type;
  747. LY_ARRAY_COUNT_TYPE u = 0;
  748. LY_ARRAY_FOR(t->enums, u) {
  749. printf("%s\n",t->enums[u].name);
  750. }
  751. break;
  752. }
  753. case LY_TYPE_IDENT: {
  754. struct lysc_type_identityref *t =
  755. (struct lysc_type_identityref *)type;
  756. LY_ARRAY_COUNT_TYPE u = 0;
  757. LY_ARRAY_FOR(t->bases, u) {
  758. identityref(t->bases[u]);
  759. }
  760. break;
  761. }
  762. case LY_TYPE_UNION: {
  763. struct lysc_type_union *t =
  764. (struct lysc_type_union *)type;
  765. LY_ARRAY_COUNT_TYPE u = 0;
  766. LY_ARRAY_FOR(t->types, u) {
  767. pline_print_type_completions(t->types[u]);
  768. }
  769. break;
  770. }
  771. default:
  772. break;
  773. }
  774. }
  775. static void pline_print_type_help(const struct lysc_node *node,
  776. const struct lysc_type *type)
  777. {
  778. assert(type);
  779. if ((type->basetype != LY_TYPE_UNION) &&
  780. (type->basetype != LY_TYPE_LEAFREF))
  781. printf("%s\n", node->name);
  782. switch (type->basetype) {
  783. case LY_TYPE_UINT8: {
  784. printf("Unsigned integer 8bit\n");
  785. break;
  786. }
  787. case LY_TYPE_UINT16: {
  788. printf("Unsigned integer 16bit\n");
  789. break;
  790. }
  791. case LY_TYPE_UINT32: {
  792. printf("Unsigned integer 32bit\n");
  793. break;
  794. }
  795. case LY_TYPE_UINT64: {
  796. printf("Unsigned integer 64bit\n");
  797. break;
  798. }
  799. case LY_TYPE_INT8: {
  800. printf("Integer 8bit\n");
  801. break;
  802. }
  803. case LY_TYPE_INT16: {
  804. printf("Integer 16bit\n");
  805. break;
  806. }
  807. case LY_TYPE_INT32: {
  808. printf("Integer 32bit\n");
  809. break;
  810. }
  811. case LY_TYPE_INT64: {
  812. printf("Integer 64bit\n");
  813. break;
  814. }
  815. case LY_TYPE_STRING: {
  816. printf("String\n");
  817. break;
  818. }
  819. case LY_TYPE_BOOL: {
  820. printf("Boolean true/false\n");
  821. break;
  822. }
  823. case LY_TYPE_DEC64: {
  824. printf("Signed decimal number\n");
  825. break;
  826. }
  827. case LY_TYPE_ENUM: {
  828. printf("Enumerated choice\n");
  829. break;
  830. }
  831. case LY_TYPE_IDENT: {
  832. printf("Identity\n");
  833. break;
  834. }
  835. case LY_TYPE_UNION: {
  836. struct lysc_type_union *t =
  837. (struct lysc_type_union *)type;
  838. LY_ARRAY_COUNT_TYPE u = 0;
  839. LY_ARRAY_FOR(t->types, u) {
  840. pline_print_type_help(node, t->types[u]);
  841. }
  842. break;
  843. }
  844. case LY_TYPE_LEAFREF: {
  845. struct lysc_type_leafref *t =
  846. (struct lysc_type_leafref *)type;
  847. pline_print_type_help(node, t->realtype);
  848. }
  849. break;
  850. default:
  851. printf("Unknown\n");
  852. break;
  853. }
  854. }
  855. void pline_print_completions(const pline_t *pline, bool_t help)
  856. {
  857. faux_list_node_t *iter = NULL;
  858. pcompl_t *pcompl = NULL;
  859. iter = faux_list_head(pline->compls);
  860. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  861. struct lysc_type *type = NULL;
  862. const struct lysc_node *node = pcompl->node;
  863. if (pcompl->xpath && !help) {
  864. sr_val_t *vals = NULL;
  865. size_t val_num = 0;
  866. size_t i = 0;
  867. //printf("%s\n", pcompl->xpath);
  868. sr_get_items(pline->sess, pcompl->xpath,
  869. 0, 0, &vals, &val_num);
  870. for (i = 0; i < val_num; i++) {
  871. char *tmp = sr_val_to_str(&vals[i]);
  872. if (!tmp)
  873. continue;
  874. printf("%s\n", tmp);
  875. free(tmp);
  876. }
  877. sr_free_values(vals, val_num);
  878. }
  879. if (!node)
  880. continue;
  881. // Node
  882. if (PCOMPL_NODE == pcompl->type) {
  883. printf("%s\n", node->name);
  884. if (help) {
  885. if (!node->dsc) {
  886. printf("%s\n", node->name);
  887. } else {
  888. char *dsc = faux_str_getline(node->dsc,
  889. NULL);
  890. printf("%s\n", dsc);
  891. faux_str_free(dsc);
  892. }
  893. }
  894. continue;
  895. }
  896. // Type
  897. if (node->nodetype & LYS_LEAF)
  898. type = ((struct lysc_node_leaf *)node)->type;
  899. else if (node->nodetype & LYS_LEAFLIST)
  900. type = ((struct lysc_node_leaflist *)node)->type;
  901. else
  902. continue;
  903. if (help)
  904. pline_print_type_help(node, type);
  905. else
  906. pline_print_type_completions(type);
  907. }
  908. }