pline.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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 <limits.h>
  10. #include <faux/faux.h>
  11. #include <faux/str.h>
  12. #include <faux/list.h>
  13. #include <faux/argv.h>
  14. #include <sysrepo.h>
  15. #include <sysrepo/xpath.h>
  16. #include <sysrepo/values.h>
  17. #include <libyang/tree_edit.h>
  18. #include "private.h"
  19. #include "pline.h"
  20. static pexpr_t *pexpr_new(void)
  21. {
  22. pexpr_t *pexpr = NULL;
  23. pexpr = faux_zmalloc(sizeof(*pexpr));
  24. assert(pexpr);
  25. if (!pexpr)
  26. return NULL;
  27. // Initialize
  28. pexpr->xpath = NULL;
  29. pexpr->value = NULL;
  30. pexpr->active = BOOL_FALSE;
  31. pexpr->pat = PAT_NONE;
  32. pexpr->args_num = 0;
  33. pexpr->list_pos = 0;
  34. pexpr->last_keys = NULL;
  35. return pexpr;
  36. }
  37. static void pexpr_free(pexpr_t *pexpr)
  38. {
  39. if (!pexpr)
  40. return;
  41. faux_str_free(pexpr->xpath);
  42. faux_str_free(pexpr->value);
  43. faux_str_free(pexpr->last_keys);
  44. free(pexpr);
  45. }
  46. static pcompl_t *pcompl_new(void)
  47. {
  48. pcompl_t *pcompl = NULL;
  49. pcompl = faux_zmalloc(sizeof(*pcompl));
  50. assert(pcompl);
  51. if (!pcompl)
  52. return NULL;
  53. // Initialize
  54. pcompl->type = PCOMPL_NODE;
  55. pcompl->node = NULL;
  56. pcompl->xpath = NULL;
  57. return pcompl;
  58. }
  59. static void pcompl_free(pcompl_t *pcompl)
  60. {
  61. if (!pcompl)
  62. return;
  63. faux_str_free(pcompl->xpath);
  64. free(pcompl);
  65. }
  66. pline_t *pline_new(sr_session_ctx_t *sess)
  67. {
  68. pline_t *pline = NULL;
  69. pline = faux_zmalloc(sizeof(*pline));
  70. assert(pline);
  71. if (!pline)
  72. return NULL;
  73. // Init
  74. pline->sess = sess;
  75. pline->invalid = BOOL_FALSE;
  76. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  77. NULL, NULL, (faux_list_free_fn)pexpr_free);
  78. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  79. NULL, NULL, (faux_list_free_fn)pcompl_free);
  80. return pline;
  81. }
  82. void pline_free(pline_t *pline)
  83. {
  84. if (!pline)
  85. return;
  86. faux_list_free(pline->exprs);
  87. faux_list_free(pline->compls);
  88. faux_free(pline);
  89. }
  90. static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath,
  91. size_t args_num, size_t list_pos)
  92. {
  93. pexpr_t *pexpr = NULL;
  94. assert(pline);
  95. pexpr = pexpr_new();
  96. if (xpath)
  97. pexpr->xpath = faux_str_dup(xpath);
  98. pexpr->args_num = args_num;
  99. pexpr->list_pos = list_pos;
  100. faux_list_add(pline->exprs, pexpr);
  101. return pexpr;
  102. }
  103. pexpr_t *pline_current_expr(pline_t *pline)
  104. {
  105. assert(pline);
  106. if (faux_list_len(pline->exprs) == 0)
  107. pline_add_expr(pline, NULL, 0, 0);
  108. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  109. }
  110. static void pline_add_compl(pline_t *pline,
  111. pcompl_type_e type, const struct lysc_node *node, const char *xpath)
  112. {
  113. pcompl_t *pcompl = NULL;
  114. assert(pline);
  115. pcompl = pcompl_new();
  116. pcompl->type = type;
  117. pcompl->node = node;
  118. if (xpath)
  119. pcompl->xpath = faux_str_dup(xpath);
  120. faux_list_add(pline->compls, pcompl);
  121. }
  122. static void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
  123. const struct lysc_node *node)
  124. {
  125. const struct lysc_node *subtree = NULL;
  126. const struct lysc_node *iter = NULL;
  127. assert(pline);
  128. assert(module);
  129. if (node)
  130. subtree = lysc_node_child(node);
  131. else
  132. subtree = module->compiled->data;
  133. LY_LIST_FOR(subtree, iter) {
  134. if (!(iter->nodetype & SRP_NODETYPE_CONF))
  135. continue;
  136. if (!(iter->flags & LYS_CONFIG_W))
  137. continue;
  138. if ((iter->nodetype & LYS_LEAF) && (iter->flags & LYS_KEY))
  139. continue;
  140. if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
  141. pline_add_compl_subtree(pline, module, iter);
  142. continue;
  143. }
  144. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  145. }
  146. }
  147. void pline_debug(pline_t *pline)
  148. {
  149. faux_list_node_t *iter = NULL;
  150. pexpr_t *pexpr = NULL;
  151. pcompl_t *pcompl = NULL;
  152. printf("====== Pline:\n\n");
  153. printf("invalid = %s\n", pline->invalid ? "true" : "false");
  154. printf("\n");
  155. printf("=== Expressions:\n\n");
  156. iter = faux_list_head(pline->exprs);
  157. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  158. char *pat = NULL;
  159. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  160. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  161. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  162. switch (pexpr->pat) {
  163. case 0x0001:
  164. pat = "NONE";
  165. break;
  166. case 0x0002:
  167. pat = "CONTAINER";
  168. break;
  169. case 0x0004:
  170. pat = "LIST";
  171. break;
  172. case 0x0008:
  173. pat = "LIST_KEY";
  174. break;
  175. case 0x0010:
  176. pat = "LIST_KEY_INCOMPLETED";
  177. break;
  178. case 0x0020:
  179. pat = "LEAF";
  180. break;
  181. case 0x0040:
  182. pat = "LEAF_VALUE";
  183. break;
  184. case 0x0080:
  185. pat = "LEAF_EMPTY";
  186. break;
  187. case 0x0100:
  188. pat = "LEAFLIST";
  189. break;
  190. case 0x0200:
  191. pat = "LEAFLIST_VALUE";
  192. break;
  193. default:
  194. pat = "UNKNOWN";
  195. break;
  196. }
  197. printf("pexpr.pat = %s\n", pat);
  198. printf("pexpr.args_num = %lu\n", pexpr->args_num);
  199. printf("pexpr.list_pos = %lu\n", pexpr->list_pos);
  200. printf("pexpr.last_keys = %s\n", pexpr->last_keys ? pexpr->last_keys : "NULL");
  201. printf("\n");
  202. }
  203. printf("=== Completions:\n\n");
  204. iter = faux_list_head(pline->compls);
  205. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  206. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  207. "PCOMPL_NODE" : "PCOMPL_TYPE");
  208. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  209. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  210. printf("\n");
  211. }
  212. }
  213. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  214. pline_t *pline, pline_opts_t *opts)
  215. {
  216. faux_argv_node_t *arg = faux_argv_iter(argv);
  217. const struct lysc_node *node = NULL;
  218. char *rollback_xpath = NULL;
  219. size_t rollback_args_num = 0;
  220. size_t rollback_list_pos = 0;
  221. // Rollback is a mechanism to roll to previous node while
  222. // oneliners parsing
  223. bool_t rollback = BOOL_FALSE;
  224. pexpr_t *first_pexpr = NULL;
  225. // It's necessary because upper function can use the same pline object
  226. // for another modules before. It uses the same object to collect
  227. // possible completions. But pline is really invalid only when all
  228. // modules don't recognize argument.
  229. pline->invalid = BOOL_FALSE;
  230. do {
  231. pexpr_t *pexpr = pline_current_expr(pline);
  232. const char *str = (const char *)faux_argv_current(arg);
  233. bool_t is_rollback = rollback;
  234. bool_t next_arg = BOOL_TRUE;
  235. rollback = BOOL_FALSE;
  236. if (node && !is_rollback) {
  237. char *tmp = NULL;
  238. // Save rollback Xpath (for oneliners) before leaf node
  239. // Only leaf and leaf-list node allows to "rollback"
  240. // the path and add additional statements
  241. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  242. faux_str_free(rollback_xpath);
  243. rollback_xpath = faux_str_dup(pexpr->xpath);
  244. rollback_args_num = pexpr->args_num;
  245. rollback_list_pos = pexpr->list_pos;
  246. }
  247. // Add current node to Xpath
  248. tmp = faux_str_sprintf("/%s:%s",
  249. node->module->name, node->name);
  250. faux_str_cat(&pexpr->xpath, tmp);
  251. faux_str_free(tmp);
  252. pexpr->args_num++;
  253. // Activate current expression. Because it really has
  254. // new component
  255. pexpr->active = BOOL_TRUE;
  256. }
  257. // Root of the module
  258. if (!node) {
  259. // Completion
  260. if (!str) {
  261. pline_add_compl_subtree(pline, module, node);
  262. break;
  263. }
  264. // Next element
  265. node = klysc_find_child(module->compiled->data, str);
  266. if (!node)
  267. break;
  268. // Container
  269. } else if (node->nodetype & LYS_CONTAINER) {
  270. pexpr->pat = PAT_CONTAINER;
  271. // Completion
  272. if (!str) {
  273. pline_add_compl_subtree(pline, module, node);
  274. break;
  275. }
  276. // Next element
  277. node = klysc_find_child(lysc_node_child(node), str);
  278. // List
  279. } else if (node->nodetype & LYS_LIST) {
  280. const struct lysc_node *iter = NULL;
  281. pexpr->pat = PAT_LIST;
  282. pexpr->list_pos = pexpr->args_num;
  283. faux_str_free(pexpr->last_keys);
  284. pexpr->last_keys = NULL;
  285. // Next element
  286. if (!is_rollback) {
  287. bool_t break_upper_loop = BOOL_FALSE;
  288. // Keys without statement. Positional parameters.
  289. if (!opts->keys_w_stmt) {
  290. LY_LIST_FOR(lysc_node_child(node), iter) {
  291. char *tmp = NULL;
  292. char *escaped = NULL;
  293. struct lysc_node_leaf *leaf =
  294. (struct lysc_node_leaf *)iter;
  295. if (!(iter->nodetype & LYS_LEAF))
  296. continue;
  297. if (!(iter->flags & LYS_KEY))
  298. continue;
  299. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  300. // Completion
  301. if (!str) {
  302. char *tmp = NULL;
  303. char *compl_xpath = NULL;
  304. tmp = faux_str_sprintf("%s/%s",
  305. pexpr->xpath, leaf->name);
  306. pline_add_compl(pline,
  307. PCOMPL_TYPE, iter, tmp);
  308. compl_xpath = klysc_leafref_xpath(iter, tmp);
  309. if (compl_xpath) {
  310. pline_add_compl(pline, PCOMPL_TYPE,
  311. NULL, compl_xpath);
  312. faux_str_free(compl_xpath);
  313. }
  314. faux_str_free(tmp);
  315. break_upper_loop = BOOL_TRUE;
  316. break;
  317. }
  318. escaped = faux_str_c_esc(str);
  319. tmp = faux_str_sprintf("[%s=\"%s\"]",
  320. leaf->name, escaped);
  321. faux_str_free(escaped);
  322. faux_str_cat(&pexpr->xpath, tmp);
  323. faux_str_cat(&pexpr->last_keys, tmp);
  324. faux_str_free(tmp);
  325. pexpr->args_num++;
  326. faux_argv_each(&arg);
  327. str = (const char *)faux_argv_current(arg);
  328. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  329. }
  330. // Keys with statements. Arbitrary order of keys.
  331. } else {
  332. faux_list_t *keys = NULL;
  333. unsigned int specified_keys_num = 0;
  334. klysc_key_t *cur_key = NULL;
  335. bool_t first_key = BOOL_TRUE;
  336. bool_t first_key_is_optional = BOOL_FALSE;
  337. faux_list_node_t *key_iter = NULL;
  338. // List keys
  339. keys = faux_list_new(FAUX_LIST_UNSORTED,
  340. FAUX_LIST_UNIQUE,
  341. klysc_key_compare,
  342. klysc_key_kcompare,
  343. (faux_list_free_fn)faux_free);
  344. LY_LIST_FOR(lysc_node_child(node), iter) {
  345. struct lysc_node_leaf *leaf =
  346. (struct lysc_node_leaf *)iter;
  347. klysc_key_t *key = NULL;
  348. if (!(iter->nodetype & LYS_LEAF))
  349. continue;
  350. if (!(iter->flags & LYS_KEY))
  351. continue;
  352. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  353. key = faux_zmalloc(sizeof(*key));
  354. assert(key);
  355. key->node = iter;
  356. if (opts->default_keys &&
  357. (key->dflt = klysc_node_ext_default(iter))) {
  358. if (first_key)
  359. first_key_is_optional = BOOL_TRUE;
  360. }
  361. faux_list_add(keys, key);
  362. first_key = BOOL_FALSE;
  363. }
  364. while (specified_keys_num < faux_list_len(keys)) {
  365. char *tmp = NULL;
  366. char *escaped = NULL;
  367. // First key without statement. Must be mandatory.
  368. if ((0 == specified_keys_num) &&
  369. !opts->first_key_w_stmt &&
  370. !first_key_is_optional) {
  371. cur_key = (klysc_key_t *)faux_list_data(faux_list_head(keys));
  372. } else {
  373. if (!str)
  374. break;
  375. cur_key = faux_list_kfind(keys, str);
  376. if (!cur_key || cur_key->value)
  377. break;
  378. pexpr->args_num++;
  379. faux_argv_each(&arg);
  380. str = (const char *)faux_argv_current(arg);
  381. }
  382. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  383. // Completion
  384. if (!str) {
  385. char *tmp = NULL;
  386. char *compl_xpath = NULL;
  387. tmp = faux_str_sprintf("%s/%s",
  388. pexpr->xpath, cur_key->node->name);
  389. pline_add_compl(pline,
  390. PCOMPL_TYPE, cur_key->node, tmp);
  391. compl_xpath = klysc_leafref_xpath(iter, tmp);
  392. if (compl_xpath) {
  393. pline_add_compl(pline, PCOMPL_TYPE,
  394. NULL, compl_xpath);
  395. faux_str_free(compl_xpath);
  396. }
  397. faux_str_free(tmp);
  398. break_upper_loop = BOOL_TRUE;
  399. break;
  400. }
  401. escaped = faux_str_c_esc(str);
  402. tmp = faux_str_sprintf("[%s=\"%s\"]",
  403. cur_key->node->name, escaped);
  404. faux_str_free(escaped);
  405. faux_str_cat(&pexpr->xpath, tmp);
  406. faux_str_cat(&pexpr->last_keys, tmp);
  407. faux_str_free(tmp);
  408. cur_key->value = str;
  409. specified_keys_num++;
  410. pexpr->args_num++;
  411. faux_argv_each(&arg);
  412. str = (const char *)faux_argv_current(arg);
  413. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  414. }
  415. if (break_upper_loop) {
  416. faux_list_free(keys);
  417. break;
  418. }
  419. key_iter = faux_list_head(keys);
  420. while((cur_key = (klysc_key_t *)faux_list_each(&key_iter))) {
  421. if (cur_key->value)
  422. continue;
  423. // Completion
  424. if (!str) {
  425. // if ((0 == specified_keys_num) &&
  426. // !opts->first_key_w_stmt &&
  427. // !first_key_is_optional) {
  428. pline_add_compl(pline,
  429. PCOMPL_NODE, cur_key->node, NULL);
  430. }
  431. if (opts->default_keys && cur_key->dflt) {
  432. char *tmp = NULL;
  433. char *escaped = NULL;
  434. escaped = faux_str_c_esc(cur_key->dflt);
  435. tmp = faux_str_sprintf("[%s=\"%s\"]",
  436. cur_key->node->name, escaped);
  437. faux_str_free(escaped);
  438. faux_str_cat(&pexpr->xpath, tmp);
  439. faux_str_cat(&pexpr->last_keys, tmp);
  440. faux_str_free(tmp);
  441. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  442. } else { // Mandatory key is not specified
  443. break_upper_loop = BOOL_TRUE;
  444. }
  445. }
  446. faux_list_free(keys);
  447. }
  448. if (break_upper_loop)
  449. break;
  450. }
  451. pexpr->pat = PAT_LIST_KEY;
  452. // Completion
  453. if (!str) {
  454. pline_add_compl_subtree(pline, module, node);
  455. break;
  456. }
  457. // Next element
  458. node = klysc_find_child(lysc_node_child(node), str);
  459. // Leaf
  460. } else if (node->nodetype & LYS_LEAF) {
  461. struct lysc_node_leaf *leaf =
  462. (struct lysc_node_leaf *)node;
  463. // Next element
  464. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  465. pexpr->pat = PAT_LEAF_EMPTY;
  466. // Completion
  467. if (!str) {
  468. pline_add_compl_subtree(pline,
  469. module, node->parent);
  470. break;
  471. }
  472. // Don't get next argument when argument is not
  473. // really consumed
  474. next_arg = BOOL_FALSE;
  475. } else {
  476. pexpr->pat = PAT_LEAF;
  477. // Completion
  478. if (!str) {
  479. char *compl_xpath = klysc_leafref_xpath(node, pexpr->xpath);
  480. pline_add_compl(pline,
  481. PCOMPL_TYPE, node, compl_xpath);
  482. faux_str_free(compl_xpath);
  483. break;
  484. }
  485. pexpr->pat = PAT_LEAF_VALUE;
  486. // Idenity must have prefix
  487. if (LY_TYPE_IDENT == leaf->type->basetype) {
  488. const char *prefix = NULL;
  489. prefix = klysc_identityref_prefix(
  490. (struct lysc_type_identityref *)
  491. leaf->type, str);
  492. if (prefix)
  493. pexpr->value = faux_str_sprintf(
  494. "%s:", prefix);
  495. }
  496. faux_str_cat(&pexpr->value, str);
  497. }
  498. // Expression was completed
  499. // So rollback (for oneliners)
  500. node = node->parent;
  501. pline_add_expr(pline, rollback_xpath,
  502. rollback_args_num, rollback_list_pos);
  503. rollback = BOOL_TRUE;
  504. // Leaf-list
  505. } else if (node->nodetype & LYS_LEAFLIST) {
  506. char *tmp = NULL;
  507. const char *prefix = NULL;
  508. struct lysc_node_leaflist *leaflist =
  509. (struct lysc_node_leaflist *)node;
  510. pexpr->pat = PAT_LEAFLIST;
  511. pexpr->list_pos = pexpr->args_num;
  512. faux_str_free(pexpr->last_keys);
  513. pexpr->last_keys = NULL;
  514. // Completion
  515. if (!str) {
  516. char *compl_xpath = klysc_leafref_xpath(node, pexpr->xpath);
  517. if (compl_xpath) {
  518. pline_add_compl(pline,
  519. PCOMPL_TYPE, NULL, compl_xpath);
  520. faux_str_free(compl_xpath);
  521. }
  522. pline_add_compl(pline,
  523. PCOMPL_TYPE, node, pexpr->xpath);
  524. break;
  525. }
  526. pexpr->pat = PAT_LEAFLIST_VALUE;
  527. // Idenity must have prefix
  528. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  529. prefix = klysc_identityref_prefix(
  530. (struct lysc_type_identityref *)
  531. leaflist->type, str);
  532. }
  533. tmp = faux_str_sprintf("[.='%s%s%s']",
  534. prefix ? prefix : "", prefix ? ":" : "", str);
  535. faux_str_cat(&pexpr->xpath, tmp);
  536. faux_str_cat(&pexpr->last_keys, str);
  537. faux_str_free(tmp);
  538. pexpr->args_num++;
  539. // Expression was completed
  540. // So rollback (for oneliners)
  541. node = node->parent;
  542. pline_add_expr(pline, rollback_xpath,
  543. rollback_args_num, rollback_list_pos);
  544. rollback = BOOL_TRUE;
  545. // LYS_CHOICE and LYS_CASE can appear while rollback only
  546. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  547. // Don't set pexpr->pat because CHOICE and CASE can't
  548. // appear within data tree (schema only)
  549. // Completion
  550. if (!str) {
  551. pline_add_compl_subtree(pline, module, node);
  552. break;
  553. }
  554. // Next element
  555. node = klysc_find_child(lysc_node_child(node), str);
  556. } else {
  557. break;
  558. }
  559. // Current argument was not consumed.
  560. // Break before getting next arg.
  561. if (!node && !rollback)
  562. break;
  563. if (next_arg)
  564. faux_argv_each(&arg);
  565. } while (BOOL_TRUE);
  566. // There is not-consumed argument so whole pline is invalid
  567. if (faux_argv_current(arg))
  568. pline->invalid = BOOL_TRUE;
  569. faux_str_free(rollback_xpath);
  570. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  571. if (!first_pexpr || !first_pexpr->xpath)
  572. return BOOL_FALSE; // Not found
  573. return BOOL_TRUE;
  574. }
  575. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
  576. {
  577. const struct ly_ctx *ctx = NULL;
  578. struct lys_module *module = NULL;
  579. pline_t *pline = NULL;
  580. uint32_t i = 0;
  581. faux_list_node_t *last_expr_node = NULL;
  582. assert(sess);
  583. if (!sess)
  584. return NULL;
  585. pline = pline_new(sess);
  586. if (!pline)
  587. return NULL;
  588. ctx = sr_session_acquire_context(pline->sess);
  589. if (!ctx)
  590. return NULL;
  591. // Iterate all modules
  592. i = 0;
  593. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  594. if (sr_module_is_internal(module))
  595. continue;
  596. if (!module->compiled)
  597. continue;
  598. if (!module->implemented)
  599. continue;
  600. if (!module->compiled->data)
  601. continue;
  602. if (pline_parse_module(module, argv, pline, opts))
  603. break; // Found
  604. }
  605. sr_session_release_context(pline->sess);
  606. // Last parsed expression can be inactive so remove it from list
  607. last_expr_node = faux_list_tail(pline->exprs);
  608. if (last_expr_node) {
  609. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  610. if (!expr->active)
  611. faux_list_del(pline->exprs, last_expr_node);
  612. }
  613. return pline;
  614. }
  615. static void identityref(struct lysc_ident *ident)
  616. {
  617. LY_ARRAY_COUNT_TYPE u = 0;
  618. if (!ident)
  619. return;
  620. if (!ident->derived) {
  621. printf("%s\n", ident->name);
  622. return;
  623. }
  624. LY_ARRAY_FOR(ident->derived, u) {
  625. identityref(ident->derived[u]);
  626. }
  627. }
  628. static void pline_print_type_completions(const struct lysc_type *type)
  629. {
  630. assert(type);
  631. switch (type->basetype) {
  632. case LY_TYPE_BOOL: {
  633. printf("true\nfalse\n");
  634. break;
  635. }
  636. case LY_TYPE_ENUM: {
  637. const struct lysc_type_enum *t =
  638. (const struct lysc_type_enum *)type;
  639. LY_ARRAY_COUNT_TYPE u = 0;
  640. LY_ARRAY_FOR(t->enums, u) {
  641. printf("%s\n",t->enums[u].name);
  642. }
  643. break;
  644. }
  645. case LY_TYPE_IDENT: {
  646. struct lysc_type_identityref *t =
  647. (struct lysc_type_identityref *)type;
  648. LY_ARRAY_COUNT_TYPE u = 0;
  649. LY_ARRAY_FOR(t->bases, u) {
  650. identityref(t->bases[u]);
  651. }
  652. break;
  653. }
  654. case LY_TYPE_UNION: {
  655. struct lysc_type_union *t =
  656. (struct lysc_type_union *)type;
  657. LY_ARRAY_COUNT_TYPE u = 0;
  658. LY_ARRAY_FOR(t->types, u) {
  659. pline_print_type_completions(t->types[u]);
  660. }
  661. break;
  662. }
  663. default:
  664. break;
  665. }
  666. }
  667. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  668. {
  669. struct lysc_range *range = NULL;
  670. LY_ARRAY_COUNT_TYPE u = 0;
  671. char *r = NULL;
  672. assert(type);
  673. range = ((struct lysc_type_num *)type)->range;
  674. // Show defaults
  675. if (!range) {
  676. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  677. return;
  678. }
  679. // Range
  680. faux_str_cat(&r, "[");
  681. LY_ARRAY_FOR(range->parts, u) {
  682. char *t = NULL;
  683. if (u != 0)
  684. faux_str_cat(&r, "|");
  685. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  686. range->parts[u].min_u64, range->parts[u].max_u64);
  687. faux_str_cat(&r, t);
  688. faux_str_free(t);
  689. }
  690. faux_str_cat(&r, "]\n");
  691. printf("%s", r);
  692. faux_free(r);
  693. }
  694. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  695. {
  696. struct lysc_range *range = NULL;
  697. LY_ARRAY_COUNT_TYPE u = 0;
  698. char *r = NULL;
  699. assert(type);
  700. range = ((struct lysc_type_num *)type)->range;
  701. // Show defaults
  702. if (!range) {
  703. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  704. return;
  705. }
  706. // Range
  707. faux_str_cat(&r, "[");
  708. LY_ARRAY_FOR(range->parts, u) {
  709. char *t = NULL;
  710. if (u != 0)
  711. faux_str_cat(&r, "|");
  712. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  713. range->parts[u].min_64, range->parts[u].max_64);
  714. faux_str_cat(&r, t);
  715. faux_str_free(t);
  716. }
  717. faux_str_cat(&r, "]\n");
  718. printf("%s", r);
  719. faux_free(r);
  720. }
  721. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  722. {
  723. struct lysc_range *range = NULL;
  724. uint8_t fraction_digits = 0;
  725. LY_ARRAY_COUNT_TYPE u = 0;
  726. char *r = NULL;
  727. int64_t div = 1;
  728. uint8_t i = 0;
  729. assert(type);
  730. range = ((struct lysc_type_dec *)type)->range;
  731. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  732. for (i = 0; i < fraction_digits; i++)
  733. div = div * 10;
  734. // Show defaults
  735. if (!range) {
  736. printf("[%.*f..%.*f]\n",
  737. fraction_digits, (double)def_min / div,
  738. fraction_digits, (double)def_max / div);
  739. return;
  740. }
  741. // Range
  742. faux_str_cat(&r, "[");
  743. LY_ARRAY_FOR(range->parts, u) {
  744. char *t = NULL;
  745. if (u != 0)
  746. faux_str_cat(&r, "|");
  747. t = faux_str_sprintf("%.*f..%.*f",
  748. fraction_digits, (double)range->parts[u].min_64 / div,
  749. fraction_digits, (double)range->parts[u].max_64 / div);
  750. faux_str_cat(&r, t);
  751. faux_str_free(t);
  752. }
  753. faux_str_cat(&r, "]\n");
  754. printf("%s", r);
  755. faux_free(r);
  756. }
  757. static void str_range(const struct lysc_type *type)
  758. {
  759. struct lysc_range *range = NULL;
  760. LY_ARRAY_COUNT_TYPE u = 0;
  761. char *r = NULL;
  762. assert(type);
  763. range = ((struct lysc_type_str *)type)->length;
  764. // Show defaults
  765. if (!range) {
  766. printf("<string>\n");
  767. return;
  768. }
  769. // Range
  770. faux_str_cat(&r, "<string[");
  771. LY_ARRAY_FOR(range->parts, u) {
  772. char *t = NULL;
  773. if (u != 0)
  774. faux_str_cat(&r, "|");
  775. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  776. range->parts[u].min_u64, range->parts[u].max_u64);
  777. faux_str_cat(&r, t);
  778. faux_str_free(t);
  779. }
  780. faux_str_cat(&r, "]>\n");
  781. printf("%s", r);
  782. faux_free(r);
  783. }
  784. static void pline_print_type_help(const struct lysc_node *node,
  785. const struct lysc_type *type)
  786. {
  787. const char *units = NULL;
  788. assert(type);
  789. assert(node);
  790. if (node->nodetype & LYS_LEAF)
  791. units = ((struct lysc_node_leaf *)node)->units;
  792. else if (node->nodetype & LYS_LEAFLIST)
  793. units = ((struct lysc_node_leaflist *)node)->units;
  794. else
  795. return;
  796. if (units) {
  797. printf("%s\n", units);
  798. } else {
  799. switch (type->basetype) {
  800. case LY_TYPE_UINT8:
  801. uint_range(type, 0, UCHAR_MAX);
  802. break;
  803. case LY_TYPE_UINT16:
  804. uint_range(type, 0, USHRT_MAX);
  805. break;
  806. case LY_TYPE_UINT32:
  807. uint_range(type, 0, UINT_MAX);
  808. break;
  809. case LY_TYPE_UINT64:
  810. uint_range(type, 0, ULLONG_MAX);
  811. break;
  812. case LY_TYPE_INT8:
  813. int_range(type, CHAR_MIN, CHAR_MAX);
  814. break;
  815. case LY_TYPE_INT16:
  816. int_range(type, SHRT_MIN, SHRT_MAX);
  817. break;
  818. case LY_TYPE_INT32:
  819. int_range(type, INT_MIN, INT_MAX);
  820. break;
  821. case LY_TYPE_INT64:
  822. int_range(type, LLONG_MIN, LLONG_MAX);
  823. break;
  824. case LY_TYPE_DEC64:
  825. dec_range(type, LLONG_MIN, LLONG_MAX);
  826. break;
  827. case LY_TYPE_STRING:
  828. str_range(type);
  829. break;
  830. case LY_TYPE_BOOL:
  831. printf("<true/false>\n");
  832. break;
  833. case LY_TYPE_LEAFREF:
  834. struct lysc_type_leafref *t =
  835. (struct lysc_type_leafref *)type;
  836. pline_print_type_help(node, t->realtype);
  837. break;
  838. case LY_TYPE_UNION: {
  839. struct lysc_type_union *t =
  840. (struct lysc_type_union *)type;
  841. LY_ARRAY_COUNT_TYPE u = 0;
  842. LY_ARRAY_FOR(t->types, u)
  843. pline_print_type_help(node, t->types[u]);
  844. break;
  845. }
  846. case LY_TYPE_ENUM:
  847. printf("Enumerated choice\n");
  848. break;
  849. case LY_TYPE_IDENT:
  850. printf("Identity\n");
  851. break;
  852. default:
  853. printf("<unknown>\n");
  854. break;
  855. }
  856. }
  857. printf("%s\n", node->dsc ? node->dsc : node->name);
  858. }
  859. void pline_print_completions(const pline_t *pline, bool_t help)
  860. {
  861. faux_list_node_t *iter = NULL;
  862. pcompl_t *pcompl = NULL;
  863. iter = faux_list_head(pline->compls);
  864. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  865. struct lysc_type *type = NULL;
  866. const struct lysc_node *node = pcompl->node;
  867. if (pcompl->xpath && !help) {
  868. sr_val_t *vals = NULL;
  869. size_t val_num = 0;
  870. size_t i = 0;
  871. //printf("%s\n", pcompl->xpath);
  872. sr_get_items(pline->sess, pcompl->xpath,
  873. 0, 0, &vals, &val_num);
  874. for (i = 0; i < val_num; i++) {
  875. char *tmp = sr_val_to_str(&vals[i]);
  876. if (!tmp)
  877. continue;
  878. printf("%s\n", tmp);
  879. free(tmp);
  880. }
  881. sr_free_values(vals, val_num);
  882. }
  883. if (!node)
  884. continue;
  885. // Node
  886. if (PCOMPL_NODE == pcompl->type) {
  887. printf("%s\n", node->name);
  888. if (help) {
  889. if (!node->dsc) {
  890. printf("%s\n", node->name);
  891. } else {
  892. char *dsc = faux_str_getline(node->dsc,
  893. NULL);
  894. printf("%s\n", dsc);
  895. faux_str_free(dsc);
  896. }
  897. }
  898. continue;
  899. }
  900. // Type
  901. if (node->nodetype & LYS_LEAF)
  902. type = ((struct lysc_node_leaf *)node)->type;
  903. else if (node->nodetype & LYS_LEAFLIST)
  904. type = ((struct lysc_node_leaflist *)node)->type;
  905. else
  906. continue;
  907. if (help)
  908. pline_print_type_help(node, type);
  909. else
  910. pline_print_type_completions(type);
  911. }
  912. }