pline.c 27 KB

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