pline.c 22 KB

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