pline.c 22 KB

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