pline.c 25 KB

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