pline.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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_compl(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_compl(ident->derived[u]);
  626. }
  627. }
  628. static void identityref_help(struct lysc_ident *ident)
  629. {
  630. LY_ARRAY_COUNT_TYPE u = 0;
  631. if (!ident)
  632. return;
  633. if (!ident->derived) {
  634. printf("%s\n%s\n", ident->name,
  635. ident->dsc ? ident->dsc : ident->name);
  636. return;
  637. }
  638. LY_ARRAY_FOR(ident->derived, u) {
  639. identityref_help(ident->derived[u]);
  640. }
  641. }
  642. static void pline_print_type_completions(const struct lysc_type *type)
  643. {
  644. assert(type);
  645. switch (type->basetype) {
  646. case LY_TYPE_BOOL: {
  647. printf("true\nfalse\n");
  648. break;
  649. }
  650. case LY_TYPE_ENUM: {
  651. const struct lysc_type_enum *t =
  652. (const struct lysc_type_enum *)type;
  653. LY_ARRAY_COUNT_TYPE u = 0;
  654. LY_ARRAY_FOR(t->enums, u) {
  655. printf("%s\n",t->enums[u].name);
  656. }
  657. break;
  658. }
  659. case LY_TYPE_IDENT: {
  660. struct lysc_type_identityref *t =
  661. (struct lysc_type_identityref *)type;
  662. LY_ARRAY_COUNT_TYPE u = 0;
  663. LY_ARRAY_FOR(t->bases, u) {
  664. identityref_compl(t->bases[u]);
  665. }
  666. break;
  667. }
  668. case LY_TYPE_UNION: {
  669. struct lysc_type_union *t =
  670. (struct lysc_type_union *)type;
  671. LY_ARRAY_COUNT_TYPE u = 0;
  672. LY_ARRAY_FOR(t->types, u) {
  673. pline_print_type_completions(t->types[u]);
  674. }
  675. break;
  676. }
  677. default:
  678. break;
  679. }
  680. }
  681. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  682. {
  683. struct lysc_range *range = NULL;
  684. LY_ARRAY_COUNT_TYPE u = 0;
  685. char *r = NULL;
  686. assert(type);
  687. range = ((struct lysc_type_num *)type)->range;
  688. // Show defaults
  689. if (!range) {
  690. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  691. return;
  692. }
  693. // Range
  694. faux_str_cat(&r, "[");
  695. LY_ARRAY_FOR(range->parts, u) {
  696. char *t = NULL;
  697. if (u != 0)
  698. faux_str_cat(&r, "|");
  699. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  700. range->parts[u].min_u64, range->parts[u].max_u64);
  701. faux_str_cat(&r, t);
  702. faux_str_free(t);
  703. }
  704. faux_str_cat(&r, "]\n");
  705. printf("%s", r);
  706. faux_free(r);
  707. }
  708. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  709. {
  710. struct lysc_range *range = NULL;
  711. LY_ARRAY_COUNT_TYPE u = 0;
  712. char *r = NULL;
  713. assert(type);
  714. range = ((struct lysc_type_num *)type)->range;
  715. // Show defaults
  716. if (!range) {
  717. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  718. return;
  719. }
  720. // Range
  721. faux_str_cat(&r, "[");
  722. LY_ARRAY_FOR(range->parts, u) {
  723. char *t = NULL;
  724. if (u != 0)
  725. faux_str_cat(&r, "|");
  726. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  727. range->parts[u].min_64, range->parts[u].max_64);
  728. faux_str_cat(&r, t);
  729. faux_str_free(t);
  730. }
  731. faux_str_cat(&r, "]\n");
  732. printf("%s", r);
  733. faux_free(r);
  734. }
  735. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  736. {
  737. struct lysc_range *range = NULL;
  738. uint8_t fraction_digits = 0;
  739. LY_ARRAY_COUNT_TYPE u = 0;
  740. char *r = NULL;
  741. int64_t div = 1;
  742. uint8_t i = 0;
  743. assert(type);
  744. range = ((struct lysc_type_dec *)type)->range;
  745. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  746. for (i = 0; i < fraction_digits; i++)
  747. div = div * 10;
  748. // Show defaults
  749. if (!range) {
  750. printf("[%.*f..%.*f]\n",
  751. fraction_digits, (double)def_min / div,
  752. fraction_digits, (double)def_max / div);
  753. return;
  754. }
  755. // Range
  756. faux_str_cat(&r, "[");
  757. LY_ARRAY_FOR(range->parts, u) {
  758. char *t = NULL;
  759. if (u != 0)
  760. faux_str_cat(&r, "|");
  761. t = faux_str_sprintf("%.*f..%.*f",
  762. fraction_digits, (double)range->parts[u].min_64 / div,
  763. fraction_digits, (double)range->parts[u].max_64 / div);
  764. faux_str_cat(&r, t);
  765. faux_str_free(t);
  766. }
  767. faux_str_cat(&r, "]\n");
  768. printf("%s", r);
  769. faux_free(r);
  770. }
  771. static void str_range(const struct lysc_type *type)
  772. {
  773. struct lysc_range *range = NULL;
  774. LY_ARRAY_COUNT_TYPE u = 0;
  775. char *r = NULL;
  776. assert(type);
  777. range = ((struct lysc_type_str *)type)->length;
  778. // Show defaults
  779. if (!range) {
  780. printf("<string>\n");
  781. return;
  782. }
  783. // Range
  784. faux_str_cat(&r, "<string[");
  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("%" PRIu64 "..%" PRIu64,
  790. range->parts[u].min_u64, range->parts[u].max_u64);
  791. faux_str_cat(&r, t);
  792. faux_str_free(t);
  793. }
  794. faux_str_cat(&r, "]>\n");
  795. printf("%s", r);
  796. faux_free(r);
  797. }
  798. static void pline_print_type_help(const struct lysc_node *node,
  799. const struct lysc_type *type)
  800. {
  801. const char *units = NULL;
  802. assert(type);
  803. assert(node);
  804. if (node->nodetype & LYS_LEAF)
  805. units = ((struct lysc_node_leaf *)node)->units;
  806. else if (node->nodetype & LYS_LEAFLIST)
  807. units = ((struct lysc_node_leaflist *)node)->units;
  808. else
  809. return;
  810. if (units) {
  811. printf("%s\n", units);
  812. } else {
  813. switch (type->basetype) {
  814. case LY_TYPE_UINT8:
  815. uint_range(type, 0, UCHAR_MAX);
  816. break;
  817. case LY_TYPE_UINT16:
  818. uint_range(type, 0, USHRT_MAX);
  819. break;
  820. case LY_TYPE_UINT32:
  821. uint_range(type, 0, UINT_MAX);
  822. break;
  823. case LY_TYPE_UINT64:
  824. uint_range(type, 0, ULLONG_MAX);
  825. break;
  826. case LY_TYPE_INT8:
  827. int_range(type, CHAR_MIN, CHAR_MAX);
  828. break;
  829. case LY_TYPE_INT16:
  830. int_range(type, SHRT_MIN, SHRT_MAX);
  831. break;
  832. case LY_TYPE_INT32:
  833. int_range(type, INT_MIN, INT_MAX);
  834. break;
  835. case LY_TYPE_INT64:
  836. int_range(type, LLONG_MIN, LLONG_MAX);
  837. break;
  838. case LY_TYPE_DEC64:
  839. dec_range(type, LLONG_MIN, LLONG_MAX);
  840. break;
  841. case LY_TYPE_STRING:
  842. str_range(type);
  843. break;
  844. case LY_TYPE_BOOL:
  845. printf("<true/false>\n");
  846. break;
  847. case LY_TYPE_LEAFREF: {
  848. const struct lysc_type_leafref *t =
  849. (const struct lysc_type_leafref *)type;
  850. pline_print_type_help(node, t->realtype);
  851. break;
  852. }
  853. case LY_TYPE_UNION: {
  854. const struct lysc_type_union *t =
  855. (const struct lysc_type_union *)type;
  856. LY_ARRAY_COUNT_TYPE u = 0;
  857. LY_ARRAY_FOR(t->types, u)
  858. pline_print_type_help(node, t->types[u]);
  859. break;
  860. }
  861. case LY_TYPE_ENUM: {
  862. const struct lysc_type_enum *t =
  863. (const struct lysc_type_enum *)type;
  864. LY_ARRAY_COUNT_TYPE u = 0;
  865. LY_ARRAY_FOR(t->enums, u)
  866. printf("%s\n%s\n",
  867. t->enums[u].name,
  868. t->enums[u].dsc ? t->enums[u].dsc : t->enums[u].name);
  869. return; // Because it prints whole info itself
  870. break;
  871. }
  872. case LY_TYPE_IDENT: {
  873. struct lysc_type_identityref *t =
  874. (struct lysc_type_identityref *)type;
  875. LY_ARRAY_COUNT_TYPE u = 0;
  876. LY_ARRAY_FOR(t->bases, u)
  877. identityref_help(t->bases[u]);
  878. return; // Because it prints whole info itself
  879. break;
  880. }
  881. default:
  882. printf("<unknown>\n");
  883. break;
  884. }
  885. }
  886. printf("%s\n", node->dsc ? node->dsc : node->name);
  887. }
  888. void pline_print_completions(const pline_t *pline, bool_t help)
  889. {
  890. faux_list_node_t *iter = NULL;
  891. pcompl_t *pcompl = NULL;
  892. iter = faux_list_head(pline->compls);
  893. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  894. struct lysc_type *type = NULL;
  895. const struct lysc_node *node = pcompl->node;
  896. if (pcompl->xpath && !help) {
  897. sr_val_t *vals = NULL;
  898. size_t val_num = 0;
  899. size_t i = 0;
  900. //printf("%s\n", pcompl->xpath);
  901. sr_get_items(pline->sess, pcompl->xpath,
  902. 0, 0, &vals, &val_num);
  903. for (i = 0; i < val_num; i++) {
  904. char *tmp = sr_val_to_str(&vals[i]);
  905. if (!tmp)
  906. continue;
  907. printf("%s\n", tmp);
  908. free(tmp);
  909. }
  910. sr_free_values(vals, val_num);
  911. }
  912. if (!node)
  913. continue;
  914. // Node
  915. if (PCOMPL_NODE == pcompl->type) {
  916. printf("%s\n", node->name);
  917. if (help) {
  918. if (!node->dsc) {
  919. printf("%s\n", node->name);
  920. } else {
  921. char *dsc = faux_str_getline(node->dsc,
  922. NULL);
  923. printf("%s\n", dsc);
  924. faux_str_free(dsc);
  925. }
  926. }
  927. continue;
  928. }
  929. // Type
  930. if (node->nodetype & LYS_LEAF)
  931. type = ((struct lysc_node_leaf *)node)->type;
  932. else if (node->nodetype & LYS_LEAFLIST)
  933. type = ((struct lysc_node_leaflist *)node)->type;
  934. else
  935. continue;
  936. if (help)
  937. pline_print_type_help(node, type);
  938. else
  939. pline_print_type_completions(type);
  940. }
  941. }