pline.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. // if ((0 == specified_keys_num) &&
  451. // !opts->first_key_w_stmt &&
  452. // !first_key_is_optional) {
  453. pline_add_compl(pline,
  454. PCOMPL_NODE, cur_key->node, NULL);
  455. }
  456. if (opts->default_keys && cur_key->dflt) {
  457. pexpr_xpath_add_list_key(pexpr,
  458. cur_key->node->name,
  459. cur_key->dflt, BOOL_FALSE);
  460. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  461. } else { // Mandatory key is not specified
  462. break_upper_loop = BOOL_TRUE;
  463. }
  464. }
  465. faux_list_free(keys);
  466. }
  467. if (break_upper_loop)
  468. break;
  469. }
  470. pexpr->pat = PAT_LIST_KEY;
  471. // Completion
  472. if (!str) {
  473. pline_add_compl_subtree(pline, module, node);
  474. break;
  475. }
  476. // Next element
  477. node = klysc_find_child(lysc_node_child(node), str);
  478. // Leaf
  479. } else if (node->nodetype & LYS_LEAF) {
  480. struct lysc_node_leaf *leaf =
  481. (struct lysc_node_leaf *)node;
  482. // Next element
  483. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  484. pexpr->pat = PAT_LEAF_EMPTY;
  485. // Completion
  486. if (!str) {
  487. pline_add_compl_subtree(pline,
  488. module, node->parent);
  489. break;
  490. }
  491. // Don't get next argument when argument is not
  492. // really consumed
  493. next_arg = BOOL_FALSE;
  494. } else {
  495. pexpr->pat = PAT_LEAF;
  496. // Completion
  497. if (!str) {
  498. char *compl_xpath = klysc_leafref_xpath(node, pexpr->xpath);
  499. pline_add_compl(pline,
  500. PCOMPL_TYPE, node, compl_xpath);
  501. faux_str_free(compl_xpath);
  502. break;
  503. }
  504. pexpr->pat = PAT_LEAF_VALUE;
  505. // Idenity must have prefix
  506. if (LY_TYPE_IDENT == leaf->type->basetype) {
  507. const char *prefix = NULL;
  508. prefix = klysc_identityref_prefix(
  509. (struct lysc_type_identityref *)
  510. leaf->type, str);
  511. if (prefix)
  512. pexpr->value = faux_str_sprintf(
  513. "%s:", prefix);
  514. }
  515. faux_str_cat(&pexpr->value, str);
  516. }
  517. // Expression was completed
  518. // So rollback (for oneliners)
  519. node = node->parent;
  520. pline_add_expr(pline, rollback_xpath,
  521. rollback_args_num, rollback_list_pos);
  522. rollback = BOOL_TRUE;
  523. // Leaf-list
  524. } else if (node->nodetype & LYS_LEAFLIST) {
  525. const char *prefix = NULL;
  526. struct lysc_node_leaflist *leaflist =
  527. (struct lysc_node_leaflist *)node;
  528. pexpr->pat = PAT_LEAFLIST;
  529. pexpr->list_pos = pexpr->args_num;
  530. faux_str_free(pexpr->last_keys);
  531. pexpr->last_keys = NULL;
  532. // Completion
  533. if (!str) {
  534. char *compl_xpath = klysc_leafref_xpath(node, pexpr->xpath);
  535. if (compl_xpath) {
  536. pline_add_compl(pline,
  537. PCOMPL_TYPE, NULL, compl_xpath);
  538. faux_str_free(compl_xpath);
  539. }
  540. pline_add_compl(pline,
  541. PCOMPL_TYPE, node, pexpr->xpath);
  542. break;
  543. }
  544. pexpr->pat = PAT_LEAFLIST_VALUE;
  545. // Idenity must have prefix
  546. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  547. prefix = klysc_identityref_prefix(
  548. (struct lysc_type_identityref *)
  549. leaflist->type, str);
  550. }
  551. pexpr_xpath_add_leaflist_key(pexpr, prefix, str);
  552. // Expression was completed
  553. // So rollback (for oneliners)
  554. node = node->parent;
  555. pline_add_expr(pline, rollback_xpath,
  556. rollback_args_num, rollback_list_pos);
  557. rollback = BOOL_TRUE;
  558. // LYS_CHOICE and LYS_CASE can appear while rollback only
  559. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  560. // Don't set pexpr->pat because CHOICE and CASE can't
  561. // appear within data tree (schema only)
  562. // Completion
  563. if (!str) {
  564. pline_add_compl_subtree(pline, module, node);
  565. break;
  566. }
  567. // Next element
  568. node = klysc_find_child(lysc_node_child(node), str);
  569. } else {
  570. break;
  571. }
  572. // Current argument was not consumed.
  573. // Break before getting next arg.
  574. if (!node && !rollback)
  575. break;
  576. if (next_arg)
  577. faux_argv_each(&arg);
  578. } while (BOOL_TRUE);
  579. // There is not-consumed argument so whole pline is invalid
  580. if (faux_argv_current(arg))
  581. pline->invalid = BOOL_TRUE;
  582. faux_str_free(rollback_xpath);
  583. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  584. if (!first_pexpr || !first_pexpr->xpath)
  585. return BOOL_FALSE; // Not found
  586. return BOOL_TRUE;
  587. }
  588. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
  589. {
  590. const struct ly_ctx *ctx = NULL;
  591. struct lys_module *module = NULL;
  592. pline_t *pline = NULL;
  593. uint32_t i = 0;
  594. faux_list_node_t *last_expr_node = NULL;
  595. assert(sess);
  596. if (!sess)
  597. return NULL;
  598. pline = pline_new(sess);
  599. if (!pline)
  600. return NULL;
  601. ctx = sr_session_acquire_context(pline->sess);
  602. if (!ctx)
  603. return NULL;
  604. // Iterate all modules
  605. i = 0;
  606. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  607. if (sr_module_is_internal(module))
  608. continue;
  609. if (!module->compiled)
  610. continue;
  611. if (!module->implemented)
  612. continue;
  613. if (!module->compiled->data)
  614. continue;
  615. if (pline_parse_module(module, argv, pline, opts))
  616. break; // Found
  617. }
  618. sr_session_release_context(pline->sess);
  619. // Last parsed expression can be inactive so remove it from list
  620. last_expr_node = faux_list_tail(pline->exprs);
  621. if (last_expr_node) {
  622. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  623. if (!expr->active)
  624. faux_list_del(pline->exprs, last_expr_node);
  625. }
  626. return pline;
  627. }
  628. static void identityref_compl(struct lysc_ident *ident)
  629. {
  630. LY_ARRAY_COUNT_TYPE u = 0;
  631. if (!ident)
  632. return;
  633. if (!ident->derived) {
  634. printf("%s\n", ident->name);
  635. return;
  636. }
  637. LY_ARRAY_FOR(ident->derived, u) {
  638. identityref_compl(ident->derived[u]);
  639. }
  640. }
  641. static void identityref_help(struct lysc_ident *ident)
  642. {
  643. LY_ARRAY_COUNT_TYPE u = 0;
  644. if (!ident)
  645. return;
  646. if (!ident->derived) {
  647. printf("%s\n%s\n", ident->name,
  648. ident->dsc ? ident->dsc : ident->name);
  649. return;
  650. }
  651. LY_ARRAY_FOR(ident->derived, u) {
  652. identityref_help(ident->derived[u]);
  653. }
  654. }
  655. static void pline_print_type_completions(const struct lysc_type *type)
  656. {
  657. assert(type);
  658. switch (type->basetype) {
  659. case LY_TYPE_BOOL: {
  660. printf("true\nfalse\n");
  661. break;
  662. }
  663. case LY_TYPE_ENUM: {
  664. const struct lysc_type_enum *t =
  665. (const struct lysc_type_enum *)type;
  666. LY_ARRAY_COUNT_TYPE u = 0;
  667. LY_ARRAY_FOR(t->enums, u) {
  668. printf("%s\n",t->enums[u].name);
  669. }
  670. break;
  671. }
  672. case LY_TYPE_IDENT: {
  673. struct lysc_type_identityref *t =
  674. (struct lysc_type_identityref *)type;
  675. LY_ARRAY_COUNT_TYPE u = 0;
  676. LY_ARRAY_FOR(t->bases, u) {
  677. identityref_compl(t->bases[u]);
  678. }
  679. break;
  680. }
  681. case LY_TYPE_UNION: {
  682. struct lysc_type_union *t =
  683. (struct lysc_type_union *)type;
  684. LY_ARRAY_COUNT_TYPE u = 0;
  685. LY_ARRAY_FOR(t->types, u) {
  686. pline_print_type_completions(t->types[u]);
  687. }
  688. break;
  689. }
  690. default:
  691. break;
  692. }
  693. }
  694. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  695. {
  696. struct lysc_range *range = NULL;
  697. LY_ARRAY_COUNT_TYPE u = 0;
  698. char *r = NULL;
  699. assert(type);
  700. range = ((struct lysc_type_num *)type)->range;
  701. // Show defaults
  702. if (!range) {
  703. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  704. return;
  705. }
  706. // Range
  707. faux_str_cat(&r, "[");
  708. LY_ARRAY_FOR(range->parts, u) {
  709. char *t = NULL;
  710. if (u != 0)
  711. faux_str_cat(&r, "|");
  712. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  713. range->parts[u].min_u64, range->parts[u].max_u64);
  714. faux_str_cat(&r, t);
  715. faux_str_free(t);
  716. }
  717. faux_str_cat(&r, "]\n");
  718. printf("%s", r);
  719. faux_free(r);
  720. }
  721. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  722. {
  723. struct lysc_range *range = NULL;
  724. LY_ARRAY_COUNT_TYPE u = 0;
  725. char *r = NULL;
  726. assert(type);
  727. range = ((struct lysc_type_num *)type)->range;
  728. // Show defaults
  729. if (!range) {
  730. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  731. return;
  732. }
  733. // Range
  734. faux_str_cat(&r, "[");
  735. LY_ARRAY_FOR(range->parts, u) {
  736. char *t = NULL;
  737. if (u != 0)
  738. faux_str_cat(&r, "|");
  739. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  740. range->parts[u].min_64, range->parts[u].max_64);
  741. faux_str_cat(&r, t);
  742. faux_str_free(t);
  743. }
  744. faux_str_cat(&r, "]\n");
  745. printf("%s", r);
  746. faux_free(r);
  747. }
  748. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  749. {
  750. struct lysc_range *range = NULL;
  751. uint8_t fraction_digits = 0;
  752. LY_ARRAY_COUNT_TYPE u = 0;
  753. char *r = NULL;
  754. int64_t div = 1;
  755. uint8_t i = 0;
  756. assert(type);
  757. range = ((struct lysc_type_dec *)type)->range;
  758. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  759. for (i = 0; i < fraction_digits; i++)
  760. div = div * 10;
  761. // Show defaults
  762. if (!range) {
  763. printf("[%.*f..%.*f]\n",
  764. fraction_digits, (double)def_min / div,
  765. fraction_digits, (double)def_max / div);
  766. return;
  767. }
  768. // Range
  769. faux_str_cat(&r, "[");
  770. LY_ARRAY_FOR(range->parts, u) {
  771. char *t = NULL;
  772. if (u != 0)
  773. faux_str_cat(&r, "|");
  774. t = faux_str_sprintf("%.*f..%.*f",
  775. fraction_digits, (double)range->parts[u].min_64 / div,
  776. fraction_digits, (double)range->parts[u].max_64 / div);
  777. faux_str_cat(&r, t);
  778. faux_str_free(t);
  779. }
  780. faux_str_cat(&r, "]\n");
  781. printf("%s", r);
  782. faux_free(r);
  783. }
  784. static void str_range(const struct lysc_type *type)
  785. {
  786. struct lysc_range *range = NULL;
  787. LY_ARRAY_COUNT_TYPE u = 0;
  788. char *r = NULL;
  789. assert(type);
  790. range = ((struct lysc_type_str *)type)->length;
  791. // Show defaults
  792. if (!range) {
  793. printf("<string>\n");
  794. return;
  795. }
  796. // Range
  797. faux_str_cat(&r, "<string[");
  798. LY_ARRAY_FOR(range->parts, u) {
  799. char *t = NULL;
  800. if (u != 0)
  801. faux_str_cat(&r, "|");
  802. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  803. range->parts[u].min_u64, range->parts[u].max_u64);
  804. faux_str_cat(&r, t);
  805. faux_str_free(t);
  806. }
  807. faux_str_cat(&r, "]>\n");
  808. printf("%s", r);
  809. faux_free(r);
  810. }
  811. static void pline_print_type_help(const struct lysc_node *node,
  812. const struct lysc_type *type)
  813. {
  814. const char *units = NULL;
  815. assert(type);
  816. assert(node);
  817. if (node->nodetype & LYS_LEAF)
  818. units = ((struct lysc_node_leaf *)node)->units;
  819. else if (node->nodetype & LYS_LEAFLIST)
  820. units = ((struct lysc_node_leaflist *)node)->units;
  821. else
  822. return;
  823. if (units) {
  824. printf("%s\n", units);
  825. } else {
  826. switch (type->basetype) {
  827. case LY_TYPE_UINT8:
  828. uint_range(type, 0, UCHAR_MAX);
  829. break;
  830. case LY_TYPE_UINT16:
  831. uint_range(type, 0, USHRT_MAX);
  832. break;
  833. case LY_TYPE_UINT32:
  834. uint_range(type, 0, UINT_MAX);
  835. break;
  836. case LY_TYPE_UINT64:
  837. uint_range(type, 0, ULLONG_MAX);
  838. break;
  839. case LY_TYPE_INT8:
  840. int_range(type, CHAR_MIN, CHAR_MAX);
  841. break;
  842. case LY_TYPE_INT16:
  843. int_range(type, SHRT_MIN, SHRT_MAX);
  844. break;
  845. case LY_TYPE_INT32:
  846. int_range(type, INT_MIN, INT_MAX);
  847. break;
  848. case LY_TYPE_INT64:
  849. int_range(type, LLONG_MIN, LLONG_MAX);
  850. break;
  851. case LY_TYPE_DEC64:
  852. dec_range(type, LLONG_MIN, LLONG_MAX);
  853. break;
  854. case LY_TYPE_STRING:
  855. str_range(type);
  856. break;
  857. case LY_TYPE_BOOL:
  858. printf("<true/false>\n");
  859. break;
  860. case LY_TYPE_LEAFREF: {
  861. const struct lysc_type_leafref *t =
  862. (const struct lysc_type_leafref *)type;
  863. pline_print_type_help(node, t->realtype);
  864. break;
  865. }
  866. case LY_TYPE_UNION: {
  867. const struct lysc_type_union *t =
  868. (const struct lysc_type_union *)type;
  869. LY_ARRAY_COUNT_TYPE u = 0;
  870. LY_ARRAY_FOR(t->types, u)
  871. pline_print_type_help(node, t->types[u]);
  872. break;
  873. }
  874. case LY_TYPE_ENUM: {
  875. const struct lysc_type_enum *t =
  876. (const struct lysc_type_enum *)type;
  877. LY_ARRAY_COUNT_TYPE u = 0;
  878. LY_ARRAY_FOR(t->enums, u)
  879. printf("%s\n%s\n",
  880. t->enums[u].name,
  881. t->enums[u].dsc ? t->enums[u].dsc : t->enums[u].name);
  882. return; // Because it prints whole info itself
  883. break;
  884. }
  885. case LY_TYPE_IDENT: {
  886. struct lysc_type_identityref *t =
  887. (struct lysc_type_identityref *)type;
  888. LY_ARRAY_COUNT_TYPE u = 0;
  889. LY_ARRAY_FOR(t->bases, u)
  890. identityref_help(t->bases[u]);
  891. return; // Because it prints whole info itself
  892. break;
  893. }
  894. default:
  895. printf("<unknown>\n");
  896. break;
  897. }
  898. }
  899. printf("%s\n", node->dsc ? node->dsc : node->name);
  900. }
  901. void pline_print_completions(const pline_t *pline, bool_t help)
  902. {
  903. faux_list_node_t *iter = NULL;
  904. pcompl_t *pcompl = NULL;
  905. iter = faux_list_head(pline->compls);
  906. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  907. struct lysc_type *type = NULL;
  908. const struct lysc_node *node = pcompl->node;
  909. if (pcompl->xpath && !help) {
  910. sr_val_t *vals = NULL;
  911. size_t val_num = 0;
  912. size_t i = 0;
  913. //printf("%s\n", pcompl->xpath);
  914. sr_get_items(pline->sess, pcompl->xpath,
  915. 0, 0, &vals, &val_num);
  916. for (i = 0; i < val_num; i++) {
  917. char *tmp = sr_val_to_str(&vals[i]);
  918. if (!tmp)
  919. continue;
  920. printf("%s\n", tmp);
  921. free(tmp);
  922. }
  923. sr_free_values(vals, val_num);
  924. }
  925. if (!node)
  926. continue;
  927. // Node
  928. if (PCOMPL_NODE == pcompl->type) {
  929. printf("%s\n", node->name);
  930. if (help) {
  931. if (!node->dsc) {
  932. printf("%s\n", node->name);
  933. } else {
  934. char *dsc = faux_str_getline(node->dsc,
  935. NULL);
  936. printf("%s\n", dsc);
  937. faux_str_free(dsc);
  938. }
  939. }
  940. continue;
  941. }
  942. // Type
  943. if (node->nodetype & LYS_LEAF)
  944. type = ((struct lysc_node_leaf *)node)->type;
  945. else if (node->nodetype & LYS_LEAFLIST)
  946. type = ((struct lysc_node_leaflist *)node)->type;
  947. else
  948. continue;
  949. if (help)
  950. pline_print_type_help(node, type);
  951. else
  952. pline_print_type_completions(type);
  953. }
  954. }