pline.c 23 KB

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