pline.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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 pexpr_xpath_add_node(pexpr_t *pexpr,
  214. const char *prefix, const char *name)
  215. {
  216. char *tmp = NULL;
  217. assert(pexpr);
  218. assert(prefix);
  219. assert(name);
  220. tmp = faux_str_sprintf("/%s:%s", prefix, name);
  221. faux_str_cat(&pexpr->xpath, tmp);
  222. faux_str_free(tmp);
  223. pexpr->args_num++;
  224. // Activate current expression. Because it really has
  225. // new component
  226. pexpr->active = BOOL_TRUE;
  227. return BOOL_TRUE;
  228. }
  229. static bool_t pexpr_xpath_add_list_key(pexpr_t *pexpr,
  230. const char *key, const char *value, bool_t inc_args_num)
  231. {
  232. char *tmp = NULL;
  233. char *escaped = NULL;
  234. assert(pexpr);
  235. assert(key);
  236. assert(value);
  237. escaped = faux_str_c_esc(value);
  238. tmp = faux_str_sprintf("[%s=\"%s\"]", key, escaped);
  239. faux_str_free(escaped);
  240. faux_str_cat(&pexpr->xpath, tmp);
  241. faux_str_cat(&pexpr->last_keys, tmp);
  242. faux_str_free(tmp);
  243. if (inc_args_num)
  244. pexpr->args_num++;
  245. return BOOL_TRUE;
  246. }
  247. static bool_t pexpr_xpath_add_leaflist_key(pexpr_t *pexpr,
  248. const char *prefix, const char *value)
  249. {
  250. char *tmp = NULL;
  251. assert(pexpr);
  252. assert(value);
  253. tmp = faux_str_sprintf("[.='%s%s%s']",
  254. prefix ? prefix : "", prefix ? ":" : "", value);
  255. faux_str_cat(&pexpr->xpath, tmp);
  256. faux_str_cat(&pexpr->last_keys, value);
  257. faux_str_free(tmp);
  258. pexpr->args_num++;
  259. return BOOL_TRUE;
  260. }
  261. static void pline_add_compl_leafref(pline_t *pline, const struct lysc_node *node,
  262. const struct lysc_type *type, const char *xpath)
  263. {
  264. if (!type)
  265. return;
  266. if (!node)
  267. return;
  268. if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  269. return;
  270. switch (type->basetype) {
  271. case LY_TYPE_UNION: {
  272. struct lysc_type_union *t =
  273. (struct lysc_type_union *)type;
  274. LY_ARRAY_COUNT_TYPE u = 0;
  275. LY_ARRAY_FOR(t->types, u) {
  276. pline_add_compl_leafref(pline, node, t->types[u], xpath);
  277. }
  278. break;
  279. }
  280. case LY_TYPE_LEAFREF: {
  281. char *compl_xpath = klysc_leafref_xpath(node, type, xpath);
  282. pline_add_compl(pline, PCOMPL_TYPE, NULL, compl_xpath);
  283. faux_str_free(compl_xpath);
  284. break;
  285. }
  286. default:
  287. break;
  288. }
  289. }
  290. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  291. pline_t *pline, pline_opts_t *opts)
  292. {
  293. faux_argv_node_t *arg = faux_argv_iter(argv);
  294. const struct lysc_node *node = NULL;
  295. char *rollback_xpath = NULL;
  296. size_t rollback_args_num = 0;
  297. size_t rollback_list_pos = 0;
  298. // Rollback is a mechanism to roll to previous node while
  299. // oneliners parsing
  300. bool_t rollback = BOOL_FALSE;
  301. pexpr_t *first_pexpr = NULL;
  302. // It's necessary because upper function can use the same pline object
  303. // for another modules before. It uses the same object to collect
  304. // possible completions. But pline is really invalid only when all
  305. // modules don't recognize argument.
  306. pline->invalid = BOOL_FALSE;
  307. do {
  308. pexpr_t *pexpr = pline_current_expr(pline);
  309. const char *str = (const char *)faux_argv_current(arg);
  310. bool_t is_rollback = rollback;
  311. bool_t next_arg = BOOL_TRUE;
  312. rollback = BOOL_FALSE;
  313. if (node && !is_rollback) {
  314. // Save rollback Xpath (for oneliners) before leaf node
  315. // Only leaf and leaf-list node allows to "rollback"
  316. // the path and add additional statements
  317. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  318. faux_str_free(rollback_xpath);
  319. rollback_xpath = faux_str_dup(pexpr->xpath);
  320. rollback_args_num = pexpr->args_num;
  321. rollback_list_pos = pexpr->list_pos;
  322. }
  323. // Add current node to Xpath
  324. pexpr_xpath_add_node(pexpr,
  325. node->module->name, node->name);
  326. }
  327. // Root of the module
  328. if (!node) {
  329. // Completion
  330. if (!str) {
  331. pline_add_compl_subtree(pline, module, node);
  332. break;
  333. }
  334. // Next element
  335. node = klysc_find_child(module->compiled->data, str);
  336. if (!node)
  337. break;
  338. // Container
  339. } else if (node->nodetype & LYS_CONTAINER) {
  340. pexpr->pat = PAT_CONTAINER;
  341. // Completion
  342. if (!str) {
  343. pline_add_compl_subtree(pline, module, node);
  344. break;
  345. }
  346. // Next element
  347. node = klysc_find_child(lysc_node_child(node), str);
  348. // List
  349. } else if (node->nodetype & LYS_LIST) {
  350. const struct lysc_node *iter = NULL;
  351. pexpr->pat = PAT_LIST;
  352. pexpr->list_pos = pexpr->args_num;
  353. faux_str_free(pexpr->last_keys);
  354. pexpr->last_keys = NULL;
  355. // Next element
  356. if (!is_rollback) {
  357. bool_t break_upper_loop = BOOL_FALSE;
  358. // Keys without statement. Positional parameters.
  359. if (!opts->keys_w_stmt) {
  360. LY_LIST_FOR(lysc_node_child(node), iter) {
  361. struct lysc_node_leaf *leaf =
  362. (struct lysc_node_leaf *)iter;
  363. if (!(iter->nodetype & LYS_LEAF))
  364. continue;
  365. if (!(iter->flags & LYS_KEY))
  366. continue;
  367. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  368. // Completion
  369. if (!str) {
  370. char *tmp = faux_str_sprintf("%s/%s",
  371. pexpr->xpath, leaf->name);
  372. pline_add_compl(pline,
  373. PCOMPL_TYPE, iter, tmp);
  374. pline_add_compl_leafref(pline, iter,
  375. leaf->type, tmp);
  376. faux_str_free(tmp);
  377. break_upper_loop = BOOL_TRUE;
  378. break;
  379. }
  380. pexpr_xpath_add_list_key(pexpr,
  381. leaf->name, str, BOOL_TRUE);
  382. faux_argv_each(&arg);
  383. str = (const char *)faux_argv_current(arg);
  384. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  385. }
  386. // Keys with statements. Arbitrary order of keys.
  387. } else {
  388. faux_list_t *keys = NULL;
  389. unsigned int specified_keys_num = 0;
  390. klysc_key_t *cur_key = NULL;
  391. bool_t first_key = BOOL_TRUE;
  392. bool_t first_key_is_optional = BOOL_FALSE;
  393. faux_list_node_t *key_iter = NULL;
  394. // List keys
  395. keys = faux_list_new(FAUX_LIST_UNSORTED,
  396. FAUX_LIST_UNIQUE,
  397. klysc_key_compare,
  398. klysc_key_kcompare,
  399. (faux_list_free_fn)faux_free);
  400. LY_LIST_FOR(lysc_node_child(node), iter) {
  401. struct lysc_node_leaf *leaf =
  402. (struct lysc_node_leaf *)iter;
  403. klysc_key_t *key = NULL;
  404. if (!(iter->nodetype & LYS_LEAF))
  405. continue;
  406. if (!(iter->flags & LYS_KEY))
  407. continue;
  408. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  409. key = faux_zmalloc(sizeof(*key));
  410. assert(key);
  411. key->node = iter;
  412. if (opts->default_keys &&
  413. (key->dflt = klysc_node_ext_default(iter))) {
  414. if (first_key)
  415. first_key_is_optional = BOOL_TRUE;
  416. }
  417. faux_list_add(keys, key);
  418. first_key = BOOL_FALSE;
  419. }
  420. while (specified_keys_num < faux_list_len(keys)) {
  421. // First key without statement. Must be mandatory.
  422. if ((0 == specified_keys_num) &&
  423. !opts->first_key_w_stmt &&
  424. !first_key_is_optional) {
  425. cur_key = (klysc_key_t *)faux_list_data(faux_list_head(keys));
  426. } else {
  427. if (!str)
  428. break;
  429. cur_key = faux_list_kfind(keys, str);
  430. if (!cur_key || cur_key->value)
  431. break;
  432. pexpr->args_num++;
  433. faux_argv_each(&arg);
  434. str = (const char *)faux_argv_current(arg);
  435. }
  436. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  437. // Completion
  438. if (!str) {
  439. char *tmp = faux_str_sprintf("%s/%s",
  440. pexpr->xpath,
  441. cur_key->node->name);
  442. pline_add_compl(pline, PCOMPL_TYPE,
  443. cur_key->node, tmp);
  444. pline_add_compl_leafref(pline, cur_key->node,
  445. ((const struct lysc_node_leaf *)cur_key->node)->type,
  446. tmp);
  447. faux_str_free(tmp);
  448. break_upper_loop = BOOL_TRUE;
  449. break;
  450. }
  451. pexpr_xpath_add_list_key(pexpr,
  452. cur_key->node->name, str, BOOL_TRUE);
  453. cur_key->value = str;
  454. specified_keys_num++;
  455. faux_argv_each(&arg);
  456. str = (const char *)faux_argv_current(arg);
  457. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  458. }
  459. if (break_upper_loop) {
  460. faux_list_free(keys);
  461. break;
  462. }
  463. key_iter = faux_list_head(keys);
  464. while((cur_key = (klysc_key_t *)faux_list_each(&key_iter))) {
  465. if (cur_key->value)
  466. continue;
  467. // Completion
  468. if (!str)
  469. pline_add_compl(pline,
  470. PCOMPL_NODE, cur_key->node, NULL);
  471. if (opts->default_keys && cur_key->dflt) {
  472. pexpr_xpath_add_list_key(pexpr,
  473. cur_key->node->name,
  474. cur_key->dflt, BOOL_FALSE);
  475. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  476. } else { // Mandatory key is not specified
  477. break_upper_loop = BOOL_TRUE;
  478. }
  479. }
  480. faux_list_free(keys);
  481. }
  482. if (break_upper_loop)
  483. break;
  484. }
  485. pexpr->pat = PAT_LIST_KEY;
  486. // Completion
  487. if (!str) {
  488. pline_add_compl_subtree(pline, module, node);
  489. break;
  490. }
  491. // Next element
  492. node = klysc_find_child(lysc_node_child(node), str);
  493. // Leaf
  494. } else if (node->nodetype & LYS_LEAF) {
  495. struct lysc_node_leaf *leaf =
  496. (struct lysc_node_leaf *)node;
  497. // Next element
  498. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  499. pexpr->pat = PAT_LEAF_EMPTY;
  500. // Completion
  501. if (!str) {
  502. pline_add_compl_subtree(pline,
  503. module, node->parent);
  504. break;
  505. }
  506. // Don't get next argument when argument is not
  507. // really consumed
  508. next_arg = BOOL_FALSE;
  509. } else {
  510. pexpr->pat = PAT_LEAF;
  511. // Completion
  512. if (!str) {
  513. pline_add_compl(pline, PCOMPL_TYPE, node, NULL);
  514. pline_add_compl_leafref(pline, node,
  515. leaf->type, pexpr->xpath);
  516. break;
  517. }
  518. pexpr->pat = PAT_LEAF_VALUE;
  519. // Idenity must have prefix
  520. if (LY_TYPE_IDENT == leaf->type->basetype) {
  521. const char *prefix = NULL;
  522. prefix = klysc_identityref_prefix(
  523. (struct lysc_type_identityref *)
  524. leaf->type, str);
  525. if (prefix)
  526. pexpr->value = faux_str_sprintf(
  527. "%s:", prefix);
  528. }
  529. faux_str_cat(&pexpr->value, str);
  530. }
  531. // Expression was completed
  532. // So rollback (for oneliners)
  533. node = node->parent;
  534. pline_add_expr(pline, rollback_xpath,
  535. rollback_args_num, rollback_list_pos);
  536. rollback = BOOL_TRUE;
  537. // Leaf-list
  538. } else if (node->nodetype & LYS_LEAFLIST) {
  539. const char *prefix = NULL;
  540. struct lysc_node_leaflist *leaflist =
  541. (struct lysc_node_leaflist *)node;
  542. pexpr->pat = PAT_LEAFLIST;
  543. pexpr->list_pos = pexpr->args_num;
  544. faux_str_free(pexpr->last_keys);
  545. pexpr->last_keys = NULL;
  546. // Completion
  547. if (!str) {
  548. pline_add_compl(pline, PCOMPL_TYPE, node, pexpr->xpath);
  549. pline_add_compl_leafref(pline, node,
  550. leaflist->type, pexpr->xpath);
  551. break;
  552. }
  553. pexpr->pat = PAT_LEAFLIST_VALUE;
  554. // Idenity must have prefix
  555. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  556. prefix = klysc_identityref_prefix(
  557. (struct lysc_type_identityref *)
  558. leaflist->type, str);
  559. }
  560. pexpr_xpath_add_leaflist_key(pexpr, prefix, str);
  561. // Expression was completed
  562. // So rollback (for oneliners)
  563. node = node->parent;
  564. pline_add_expr(pline, rollback_xpath,
  565. rollback_args_num, rollback_list_pos);
  566. rollback = BOOL_TRUE;
  567. // LYS_CHOICE and LYS_CASE can appear while rollback only
  568. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  569. // Don't set pexpr->pat because CHOICE and CASE can't
  570. // appear within data tree (schema only)
  571. // Completion
  572. if (!str) {
  573. pline_add_compl_subtree(pline, module, node);
  574. break;
  575. }
  576. // Next element
  577. node = klysc_find_child(lysc_node_child(node), str);
  578. } else {
  579. break;
  580. }
  581. // Current argument was not consumed.
  582. // Break before getting next arg.
  583. if (!node && !rollback)
  584. break;
  585. if (next_arg)
  586. faux_argv_each(&arg);
  587. } while (BOOL_TRUE);
  588. // There is not-consumed argument so whole pline is invalid
  589. if (faux_argv_current(arg))
  590. pline->invalid = BOOL_TRUE;
  591. faux_str_free(rollback_xpath);
  592. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  593. if (!first_pexpr || !first_pexpr->xpath)
  594. return BOOL_FALSE; // Not found
  595. return BOOL_TRUE;
  596. }
  597. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
  598. {
  599. const struct ly_ctx *ctx = NULL;
  600. struct lys_module *module = NULL;
  601. pline_t *pline = NULL;
  602. uint32_t i = 0;
  603. faux_list_node_t *last_expr_node = NULL;
  604. assert(sess);
  605. if (!sess)
  606. return NULL;
  607. pline = pline_new(sess);
  608. if (!pline)
  609. return NULL;
  610. ctx = sr_session_acquire_context(pline->sess);
  611. if (!ctx)
  612. return NULL;
  613. // Iterate all modules
  614. i = 0;
  615. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  616. if (sr_module_is_internal(module))
  617. continue;
  618. if (!module->compiled)
  619. continue;
  620. if (!module->implemented)
  621. continue;
  622. if (!module->compiled->data)
  623. continue;
  624. if (pline_parse_module(module, argv, pline, opts))
  625. break; // Found
  626. }
  627. sr_session_release_context(pline->sess);
  628. // Last parsed expression can be inactive so remove it from list
  629. last_expr_node = faux_list_tail(pline->exprs);
  630. if (last_expr_node) {
  631. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  632. if (!expr->active)
  633. faux_list_del(pline->exprs, last_expr_node);
  634. }
  635. return pline;
  636. }
  637. static void identityref_compl(struct lysc_ident *ident)
  638. {
  639. LY_ARRAY_COUNT_TYPE u = 0;
  640. if (!ident)
  641. return;
  642. if (!ident->derived) {
  643. printf("%s\n", ident->name);
  644. return;
  645. }
  646. LY_ARRAY_FOR(ident->derived, u) {
  647. identityref_compl(ident->derived[u]);
  648. }
  649. }
  650. static void identityref_help(struct lysc_ident *ident)
  651. {
  652. LY_ARRAY_COUNT_TYPE u = 0;
  653. if (!ident)
  654. return;
  655. if (!ident->derived) {
  656. printf("%s\n%s\n", ident->name,
  657. ident->dsc ? ident->dsc : ident->name);
  658. return;
  659. }
  660. LY_ARRAY_FOR(ident->derived, u) {
  661. identityref_help(ident->derived[u]);
  662. }
  663. }
  664. static void pline_print_type_completions(const struct lysc_type *type)
  665. {
  666. assert(type);
  667. switch (type->basetype) {
  668. case LY_TYPE_BOOL: {
  669. printf("true\nfalse\n");
  670. break;
  671. }
  672. case LY_TYPE_ENUM: {
  673. const struct lysc_type_enum *t =
  674. (const struct lysc_type_enum *)type;
  675. LY_ARRAY_COUNT_TYPE u = 0;
  676. LY_ARRAY_FOR(t->enums, u) {
  677. printf("%s\n",t->enums[u].name);
  678. }
  679. break;
  680. }
  681. case LY_TYPE_IDENT: {
  682. struct lysc_type_identityref *t =
  683. (struct lysc_type_identityref *)type;
  684. LY_ARRAY_COUNT_TYPE u = 0;
  685. LY_ARRAY_FOR(t->bases, u) {
  686. identityref_compl(t->bases[u]);
  687. }
  688. break;
  689. }
  690. case LY_TYPE_UNION: {
  691. struct lysc_type_union *t =
  692. (struct lysc_type_union *)type;
  693. LY_ARRAY_COUNT_TYPE u = 0;
  694. LY_ARRAY_FOR(t->types, u) {
  695. pline_print_type_completions(t->types[u]);
  696. }
  697. break;
  698. }
  699. case LY_TYPE_LEAFREF: {
  700. struct lysc_type_leafref *t =
  701. (struct lysc_type_leafref *)type;
  702. pline_print_type_completions(t->realtype);
  703. break;
  704. }
  705. default:
  706. break;
  707. }
  708. }
  709. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  710. {
  711. struct lysc_range *range = NULL;
  712. LY_ARRAY_COUNT_TYPE u = 0;
  713. char *r = NULL;
  714. assert(type);
  715. range = ((struct lysc_type_num *)type)->range;
  716. // Show defaults
  717. if (!range) {
  718. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  719. return;
  720. }
  721. // Range
  722. faux_str_cat(&r, "[");
  723. LY_ARRAY_FOR(range->parts, u) {
  724. char *t = NULL;
  725. if (u != 0)
  726. faux_str_cat(&r, "|");
  727. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  728. range->parts[u].min_u64, range->parts[u].max_u64);
  729. faux_str_cat(&r, t);
  730. faux_str_free(t);
  731. }
  732. faux_str_cat(&r, "]\n");
  733. printf("%s", r);
  734. faux_free(r);
  735. }
  736. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  737. {
  738. struct lysc_range *range = NULL;
  739. LY_ARRAY_COUNT_TYPE u = 0;
  740. char *r = NULL;
  741. assert(type);
  742. range = ((struct lysc_type_num *)type)->range;
  743. // Show defaults
  744. if (!range) {
  745. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  746. return;
  747. }
  748. // Range
  749. faux_str_cat(&r, "[");
  750. LY_ARRAY_FOR(range->parts, u) {
  751. char *t = NULL;
  752. if (u != 0)
  753. faux_str_cat(&r, "|");
  754. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  755. range->parts[u].min_64, range->parts[u].max_64);
  756. faux_str_cat(&r, t);
  757. faux_str_free(t);
  758. }
  759. faux_str_cat(&r, "]\n");
  760. printf("%s", r);
  761. faux_free(r);
  762. }
  763. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  764. {
  765. struct lysc_range *range = NULL;
  766. uint8_t fraction_digits = 0;
  767. LY_ARRAY_COUNT_TYPE u = 0;
  768. char *r = NULL;
  769. int64_t div = 1;
  770. uint8_t i = 0;
  771. assert(type);
  772. range = ((struct lysc_type_dec *)type)->range;
  773. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  774. for (i = 0; i < fraction_digits; i++)
  775. div = div * 10;
  776. // Show defaults
  777. if (!range) {
  778. printf("[%.*f..%.*f]\n",
  779. fraction_digits, (double)def_min / div,
  780. fraction_digits, (double)def_max / div);
  781. return;
  782. }
  783. // Range
  784. faux_str_cat(&r, "[");
  785. LY_ARRAY_FOR(range->parts, u) {
  786. char *t = NULL;
  787. if (u != 0)
  788. faux_str_cat(&r, "|");
  789. t = faux_str_sprintf("%.*f..%.*f",
  790. fraction_digits, (double)range->parts[u].min_64 / div,
  791. fraction_digits, (double)range->parts[u].max_64 / div);
  792. faux_str_cat(&r, t);
  793. faux_str_free(t);
  794. }
  795. faux_str_cat(&r, "]\n");
  796. printf("%s", r);
  797. faux_free(r);
  798. }
  799. static void str_range(const struct lysc_type *type)
  800. {
  801. struct lysc_range *range = NULL;
  802. LY_ARRAY_COUNT_TYPE u = 0;
  803. char *r = NULL;
  804. assert(type);
  805. range = ((struct lysc_type_str *)type)->length;
  806. // Show defaults
  807. if (!range) {
  808. printf("<string>\n");
  809. return;
  810. }
  811. // Range
  812. faux_str_cat(&r, "<string[");
  813. LY_ARRAY_FOR(range->parts, u) {
  814. char *t = NULL;
  815. if (u != 0)
  816. faux_str_cat(&r, "|");
  817. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  818. range->parts[u].min_u64, range->parts[u].max_u64);
  819. faux_str_cat(&r, t);
  820. faux_str_free(t);
  821. }
  822. faux_str_cat(&r, "]>\n");
  823. printf("%s", r);
  824. faux_free(r);
  825. }
  826. static void pline_print_type_help(const struct lysc_node *node,
  827. const struct lysc_type *type)
  828. {
  829. const char *units = NULL;
  830. assert(type);
  831. assert(node);
  832. if (node->nodetype & LYS_LEAF)
  833. units = ((struct lysc_node_leaf *)node)->units;
  834. else if (node->nodetype & LYS_LEAFLIST)
  835. units = ((struct lysc_node_leaflist *)node)->units;
  836. else
  837. return;
  838. if (units) {
  839. printf("%s\n", units);
  840. } else {
  841. switch (type->basetype) {
  842. case LY_TYPE_UINT8:
  843. uint_range(type, 0, UCHAR_MAX);
  844. break;
  845. case LY_TYPE_UINT16:
  846. uint_range(type, 0, USHRT_MAX);
  847. break;
  848. case LY_TYPE_UINT32:
  849. uint_range(type, 0, UINT_MAX);
  850. break;
  851. case LY_TYPE_UINT64:
  852. uint_range(type, 0, ULLONG_MAX);
  853. break;
  854. case LY_TYPE_INT8:
  855. int_range(type, CHAR_MIN, CHAR_MAX);
  856. break;
  857. case LY_TYPE_INT16:
  858. int_range(type, SHRT_MIN, SHRT_MAX);
  859. break;
  860. case LY_TYPE_INT32:
  861. int_range(type, INT_MIN, INT_MAX);
  862. break;
  863. case LY_TYPE_INT64:
  864. int_range(type, LLONG_MIN, LLONG_MAX);
  865. break;
  866. case LY_TYPE_DEC64:
  867. dec_range(type, LLONG_MIN, LLONG_MAX);
  868. break;
  869. case LY_TYPE_STRING:
  870. str_range(type);
  871. break;
  872. case LY_TYPE_BOOL:
  873. printf("<true/false>\n");
  874. break;
  875. case LY_TYPE_LEAFREF: {
  876. const struct lysc_type_leafref *t =
  877. (const struct lysc_type_leafref *)type;
  878. pline_print_type_help(node, t->realtype);
  879. return; // Because it prints whole info itself
  880. }
  881. case LY_TYPE_UNION: {
  882. const struct lysc_type_union *t =
  883. (const struct lysc_type_union *)type;
  884. LY_ARRAY_COUNT_TYPE u = 0;
  885. LY_ARRAY_FOR(t->types, u)
  886. pline_print_type_help(node, t->types[u]);
  887. return; // Because it prints whole info itself
  888. }
  889. case LY_TYPE_ENUM: {
  890. const struct lysc_type_enum *t =
  891. (const struct lysc_type_enum *)type;
  892. LY_ARRAY_COUNT_TYPE u = 0;
  893. LY_ARRAY_FOR(t->enums, u)
  894. printf("%s\n%s\n",
  895. t->enums[u].name,
  896. t->enums[u].dsc ? t->enums[u].dsc : t->enums[u].name);
  897. return; // Because it prints whole info itself
  898. }
  899. case LY_TYPE_IDENT: {
  900. struct lysc_type_identityref *t =
  901. (struct lysc_type_identityref *)type;
  902. LY_ARRAY_COUNT_TYPE u = 0;
  903. LY_ARRAY_FOR(t->bases, u)
  904. identityref_help(t->bases[u]);
  905. return; // Because it prints whole info itself
  906. }
  907. default:
  908. printf("<unknown>\n");
  909. break;
  910. }
  911. }
  912. printf("%s\n", node->dsc ? node->dsc : node->name);
  913. }
  914. void pline_print_completions(const pline_t *pline, bool_t help)
  915. {
  916. faux_list_node_t *iter = NULL;
  917. pcompl_t *pcompl = NULL;
  918. iter = faux_list_head(pline->compls);
  919. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  920. struct lysc_type *type = NULL;
  921. const struct lysc_node *node = pcompl->node;
  922. if (pcompl->xpath && !help) {
  923. sr_val_t *vals = NULL;
  924. size_t val_num = 0;
  925. size_t i = 0;
  926. //printf("%s\n", pcompl->xpath);
  927. sr_get_items(pline->sess, pcompl->xpath,
  928. 0, 0, &vals, &val_num);
  929. for (i = 0; i < val_num; i++) {
  930. char *tmp = sr_val_to_str(&vals[i]);
  931. if (!tmp)
  932. continue;
  933. printf("%s\n", tmp);
  934. free(tmp);
  935. }
  936. sr_free_values(vals, val_num);
  937. }
  938. if (!node)
  939. continue;
  940. // Node
  941. if (PCOMPL_NODE == pcompl->type) {
  942. printf("%s\n", node->name);
  943. if (help) {
  944. if (!node->dsc) {
  945. printf("%s\n", node->name);
  946. } else {
  947. char *dsc = faux_str_getline(node->dsc,
  948. NULL);
  949. printf("%s\n", dsc);
  950. faux_str_free(dsc);
  951. }
  952. }
  953. continue;
  954. }
  955. // Type
  956. if (node->nodetype & LYS_LEAF)
  957. type = ((struct lysc_node_leaf *)node)->type;
  958. else if (node->nodetype & LYS_LEAFLIST)
  959. type = ((struct lysc_node_leaflist *)node)->type;
  960. else
  961. continue;
  962. if (help)
  963. pline_print_type_help(node, type);
  964. else
  965. pline_print_type_completions(type);
  966. }
  967. }