pline.c 25 KB

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