pline.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 <sysrepo.h>
  9. #include <sysrepo/xpath.h>
  10. #include <sysrepo/values.h>
  11. #include <libyang/tree_edit.h>
  12. #include <faux/faux.h>
  13. #include <faux/str.h>
  14. #include <faux/list.h>
  15. #include <faux/argv.h>
  16. #include "sr_copypaste.h"
  17. #include "pline.h"
  18. #define NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST)
  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. return pexpr;
  31. }
  32. static void pexpr_free(pexpr_t *pexpr)
  33. {
  34. if (!pexpr)
  35. return;
  36. faux_str_free(pexpr->xpath);
  37. faux_str_free(pexpr->value);
  38. free(pexpr);
  39. }
  40. static pcompl_t *pcompl_new(void)
  41. {
  42. pcompl_t *pcompl = NULL;
  43. pcompl = faux_zmalloc(sizeof(*pcompl));
  44. assert(pcompl);
  45. if (!pcompl)
  46. return NULL;
  47. // Initialize
  48. pcompl->type = PCOMPL_NODE;
  49. pcompl->node = NULL;
  50. pcompl->xpath = NULL;
  51. return pcompl;
  52. }
  53. static void pcompl_free(pcompl_t *pcompl)
  54. {
  55. if (!pcompl)
  56. return;
  57. faux_str_free(pcompl->xpath);
  58. free(pcompl);
  59. }
  60. pline_t *pline_new(sr_session_ctx_t *sess)
  61. {
  62. pline_t *pline = NULL;
  63. pline = faux_zmalloc(sizeof(*pline));
  64. assert(pline);
  65. if (!pline)
  66. return NULL;
  67. // Init
  68. pline->sess = sess;
  69. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  70. NULL, NULL, (faux_list_free_fn)pexpr_free);
  71. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  72. NULL, NULL, (faux_list_free_fn)pcompl_free);
  73. return pline;
  74. }
  75. void pline_free(pline_t *pline)
  76. {
  77. if (!pline)
  78. return;
  79. faux_list_free(pline->exprs);
  80. faux_list_free(pline->compls);
  81. faux_free(pline);
  82. }
  83. static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath)
  84. {
  85. pexpr_t *pexpr = NULL;
  86. assert(pline);
  87. pexpr = pexpr_new();
  88. if (xpath)
  89. pexpr->xpath = faux_str_dup(xpath);
  90. faux_list_add(pline->exprs, pexpr);
  91. return pexpr;
  92. }
  93. static pexpr_t *pline_current_expr(pline_t *pline)
  94. {
  95. assert(pline);
  96. if (faux_list_len(pline->exprs) == 0)
  97. pline_add_expr(pline, NULL);
  98. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  99. }
  100. static void pline_add_compl(pline_t *pline,
  101. pcompl_type_e type, const struct lysc_node *node, char *xpath)
  102. {
  103. pcompl_t *pcompl = NULL;
  104. assert(pline);
  105. pcompl = pcompl_new();
  106. pcompl->type = type;
  107. pcompl->node = node;
  108. if (xpath)
  109. pcompl->xpath = faux_str_dup(xpath);
  110. faux_list_add(pline->compls, pcompl);
  111. }
  112. static void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
  113. const struct lysc_node *node)
  114. {
  115. const struct lysc_node *subtree = NULL;
  116. const struct lysc_node *iter = NULL;
  117. assert(pline);
  118. assert(module);
  119. if (node)
  120. subtree = lysc_node_child(node);
  121. else
  122. subtree = module->compiled->data;
  123. LY_LIST_FOR(subtree, iter) {
  124. if (!(iter->nodetype & NODETYPE_CONF))
  125. continue;
  126. if (!(iter->flags & LYS_CONFIG_W))
  127. continue;
  128. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  129. }
  130. }
  131. void pline_debug(pline_t *pline)
  132. {
  133. faux_list_node_t *iter = NULL;
  134. pexpr_t *pexpr = NULL;
  135. pcompl_t *pcompl = NULL;
  136. printf("=== Expressions:\n\n");
  137. iter = faux_list_head(pline->exprs);
  138. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  139. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  140. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  141. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  142. printf("\n");
  143. }
  144. printf("=== Completions:\n\n");
  145. iter = faux_list_head(pline->compls);
  146. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  147. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  148. "PCOMPL_NODE" : "PCOMPL_TYPE");
  149. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  150. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  151. printf("\n");
  152. }
  153. }
  154. // Don't use standard lys_find_child() because it checks given module to be
  155. // equal to found node's module. So augmented nodes will not be found.
  156. static const struct lysc_node *find_child(const struct lysc_node *node,
  157. const char *name)
  158. {
  159. const struct lysc_node *iter = NULL;
  160. if (!node)
  161. return NULL;
  162. LY_LIST_FOR(node, iter) {
  163. if (!(iter->nodetype & NODETYPE_CONF))
  164. continue;
  165. if (!(iter->flags & LYS_CONFIG_W))
  166. continue;
  167. if (!faux_str_cmp(iter->name, name))
  168. return iter;
  169. }
  170. return NULL;
  171. }
  172. static struct lysc_ident *find_ident(struct lysc_ident *ident, const char *name)
  173. {
  174. LY_ARRAY_COUNT_TYPE u = 0;
  175. if (!ident)
  176. return NULL;
  177. if (!ident->derived) {
  178. if (!faux_str_cmp(name, ident->name))
  179. return ident;
  180. return NULL;
  181. }
  182. LY_ARRAY_FOR(ident->derived, u) {
  183. struct lysc_ident *identity = find_ident(ident->derived[u], name);
  184. if (identity)
  185. return identity;
  186. }
  187. return NULL;
  188. }
  189. static const char *identityref_prefix(struct lysc_type_identityref *type,
  190. const char *name)
  191. {
  192. LY_ARRAY_COUNT_TYPE u = 0;
  193. assert(type);
  194. LY_ARRAY_FOR(type->bases, u) {
  195. struct lysc_ident *identity = find_ident(type->bases[u], name);
  196. if (identity)
  197. return identity->module->name;
  198. }
  199. return NULL;
  200. }
  201. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  202. pline_t *pline)
  203. {
  204. faux_argv_node_t *arg = faux_argv_iter(argv);
  205. const struct lysc_node *node = NULL;
  206. char *rollback_xpath = NULL;
  207. // Rollback is a mechanism to roll to previous node while
  208. // oneliners parsing
  209. bool_t rollback = BOOL_FALSE;
  210. do {
  211. pexpr_t *pexpr = pline_current_expr(pline);
  212. const char *str = (const char *)faux_argv_current(arg);
  213. bool_t is_rollback = rollback;
  214. bool_t next_arg = BOOL_TRUE;
  215. rollback = BOOL_FALSE;
  216. if (node && !is_rollback) {
  217. char *tmp = NULL;
  218. // Save rollback Xpath (for oneliners) before leaf node
  219. // Only leaf and leaf-list node allows to "rollback"
  220. // the path and add additional statements
  221. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  222. faux_str_free(rollback_xpath);
  223. rollback_xpath = faux_str_dup(pexpr->xpath);
  224. }
  225. // Add current node to Xpath
  226. tmp = faux_str_sprintf("/%s:%s",
  227. node->module->name, node->name);
  228. faux_str_cat(&pexpr->xpath, tmp);
  229. faux_str_free(tmp);
  230. // Activate current expression. Because it really has
  231. // new component
  232. pexpr->active = BOOL_TRUE;
  233. }
  234. // Root of the module
  235. if (!node) {
  236. // Completion
  237. if (!str) {
  238. pline_add_compl_subtree(pline, module, node);
  239. return BOOL_FALSE;
  240. }
  241. // Next element
  242. node = find_child(module->compiled->data, str);
  243. if (!node)
  244. return BOOL_FALSE;
  245. // Container
  246. } else if (node->nodetype & LYS_CONTAINER) {
  247. // Completion
  248. if (!str) {
  249. pline_add_compl_subtree(pline, module, node);
  250. break;
  251. }
  252. // Next element
  253. node = find_child(lysc_node_child(node), str);
  254. // List
  255. } else if (node->nodetype & LYS_LIST) {
  256. const struct lysc_node *iter = NULL;
  257. // Next element
  258. if (!is_rollback) {
  259. bool_t break_upper_loop = BOOL_FALSE;
  260. LY_LIST_FOR(lysc_node_child(node), iter) {
  261. char *tmp = NULL;
  262. struct lysc_node_leaf *leaf =
  263. (struct lysc_node_leaf *)iter;
  264. if (!(iter->nodetype & LYS_LEAF))
  265. continue;
  266. if (!(iter->flags & LYS_KEY))
  267. continue;
  268. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  269. // Completion
  270. if (!str) {
  271. char *tmp = NULL;
  272. tmp = faux_str_sprintf("%s/%s",
  273. pexpr->xpath, leaf->name);
  274. pline_add_compl(pline,
  275. PCOMPL_TYPE, iter, tmp);
  276. faux_str_free(tmp);
  277. break_upper_loop = BOOL_TRUE;
  278. break;
  279. }
  280. tmp = faux_str_sprintf("[%s='%s']",
  281. leaf->name, str);
  282. faux_str_cat(&pexpr->xpath, tmp);
  283. faux_str_free(tmp);
  284. faux_argv_each(&arg);
  285. str = (const char *)faux_argv_current(arg);
  286. }
  287. if (break_upper_loop)
  288. break;
  289. }
  290. // Completion
  291. if (!str) {
  292. pline_add_compl_subtree(pline, module, node);
  293. break;
  294. }
  295. // Next element
  296. node = find_child(lysc_node_child(node), str);
  297. // Leaf
  298. } else if (node->nodetype & LYS_LEAF) {
  299. struct lysc_node_leaf *leaf =
  300. (struct lysc_node_leaf *)node;
  301. // Next element
  302. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  303. // Completion
  304. if (!str) {
  305. pline_add_compl_subtree(pline,
  306. module, node->parent);
  307. break;
  308. }
  309. // Don't get next argument when argument is not
  310. // really consumed
  311. next_arg = BOOL_FALSE;
  312. } else {
  313. // Completion
  314. if (!str) {
  315. pline_add_compl(pline,
  316. PCOMPL_TYPE, node, NULL);
  317. break;
  318. }
  319. // Idenity must have prefix
  320. if (LY_TYPE_IDENT == leaf->type->basetype) {
  321. const char *prefix = NULL;
  322. prefix = identityref_prefix(
  323. (struct lysc_type_identityref *)
  324. leaf->type, str);
  325. if (prefix)
  326. pexpr->value = faux_str_sprintf(
  327. "%s:", prefix);
  328. }
  329. faux_str_cat(&pexpr->value, str);
  330. }
  331. // Expression was completed
  332. // So rollback (for oneliners)
  333. node = node->parent;
  334. pline_add_expr(pline, rollback_xpath);
  335. rollback = BOOL_TRUE;
  336. // Leaf-list
  337. } else if (node->nodetype & LYS_LEAFLIST) {
  338. char *tmp = NULL;
  339. const char *prefix = NULL;
  340. struct lysc_node_leaflist *leaflist =
  341. (struct lysc_node_leaflist *)node;
  342. // Completion
  343. if (!str) {
  344. pline_add_compl(pline,
  345. PCOMPL_TYPE, node, pexpr->xpath);
  346. break;
  347. }
  348. // Idenity must have prefix
  349. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  350. prefix = identityref_prefix(
  351. (struct lysc_type_identityref *)
  352. leaflist->type, str);
  353. }
  354. tmp = faux_str_sprintf("[.='%s%s%s']",
  355. prefix ? prefix : "", prefix ? ":" : "", str);
  356. faux_str_cat(&pexpr->xpath, tmp);
  357. faux_str_free(tmp);
  358. // Expression was completed
  359. // So rollback (for oneliners)
  360. node = node->parent;
  361. pline_add_expr(pline, rollback_xpath);
  362. rollback = BOOL_TRUE;
  363. }
  364. if (next_arg)
  365. faux_argv_each(&arg);
  366. } while (node || rollback);
  367. faux_str_free(rollback_xpath);
  368. return BOOL_TRUE;
  369. }
  370. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, uint32_t flags)
  371. {
  372. const struct ly_ctx *ctx = NULL;
  373. struct lys_module *module = NULL;
  374. pline_t *pline = NULL;
  375. uint32_t i = 0;
  376. assert(sess);
  377. if (!sess)
  378. return NULL;
  379. pline = pline_new(sess);
  380. if (!pline)
  381. return NULL;
  382. ctx = sr_session_acquire_context(pline->sess);
  383. if (!ctx)
  384. return NULL;
  385. // Iterate all modules
  386. i = 0;
  387. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  388. if (sr_module_is_internal(module))
  389. continue;
  390. if (!module->compiled)
  391. continue;
  392. if (!module->implemented)
  393. continue;
  394. if (!module->compiled->data)
  395. continue;
  396. if (pline_parse_module(module, argv, pline))
  397. break; // Found
  398. }
  399. sr_session_release_context(pline->sess);
  400. return pline;
  401. }
  402. static void identityref(struct lysc_ident *ident)
  403. {
  404. LY_ARRAY_COUNT_TYPE u = 0;
  405. if (!ident)
  406. return;
  407. if (!ident->derived) {
  408. printf("%s\n", ident->name);
  409. return;
  410. }
  411. LY_ARRAY_FOR(ident->derived, u) {
  412. identityref(ident->derived[u]);
  413. }
  414. }
  415. static void pline_print_type_completions(const struct lysc_type *type)
  416. {
  417. assert(type);
  418. switch (type->basetype) {
  419. case LY_TYPE_BOOL: {
  420. printf("true\nfalse\n");
  421. break;
  422. }
  423. case LY_TYPE_ENUM: {
  424. const struct lysc_type_enum *t =
  425. (const struct lysc_type_enum *)type;
  426. LY_ARRAY_COUNT_TYPE u = 0;
  427. LY_ARRAY_FOR(t->enums, u) {
  428. printf("%s\n",t->enums[u].name);
  429. }
  430. break;
  431. }
  432. case LY_TYPE_IDENT: {
  433. struct lysc_type_identityref *t =
  434. (struct lysc_type_identityref *)type;
  435. LY_ARRAY_COUNT_TYPE u = 0;
  436. LY_ARRAY_FOR(t->bases, u) {
  437. identityref(t->bases[u]);
  438. }
  439. break;
  440. }
  441. case LY_TYPE_UNION: {
  442. struct lysc_type_union *t =
  443. (struct lysc_type_union *)type;
  444. LY_ARRAY_COUNT_TYPE u = 0;
  445. LY_ARRAY_FOR(t->types, u) {
  446. pline_print_type_completions(t->types[u]);
  447. }
  448. break;
  449. }
  450. default:
  451. break;
  452. }
  453. }
  454. static void pline_print_type_help(const struct lysc_node *node,
  455. const struct lysc_type *type)
  456. {
  457. assert(type);
  458. if (type->basetype != LY_TYPE_UNION)
  459. printf("%s\n", node->name);
  460. switch (type->basetype) {
  461. case LY_TYPE_UINT8: {
  462. printf("Unsigned integer 8bit\n");
  463. break;
  464. }
  465. case LY_TYPE_UINT16: {
  466. printf("Unsigned integer 16bit\n");
  467. break;
  468. }
  469. case LY_TYPE_UINT32: {
  470. printf("Unsigned integer 32bit\n");
  471. break;
  472. }
  473. case LY_TYPE_UINT64: {
  474. printf("Unsigned integer 64bit\n");
  475. break;
  476. }
  477. case LY_TYPE_INT8: {
  478. printf("Integer 8bit\n");
  479. break;
  480. }
  481. case LY_TYPE_INT16: {
  482. printf("Integer 16bit\n");
  483. break;
  484. }
  485. case LY_TYPE_INT32: {
  486. printf("Integer 32bit\n");
  487. break;
  488. }
  489. case LY_TYPE_INT64: {
  490. printf("Integer 64bit\n");
  491. break;
  492. }
  493. case LY_TYPE_STRING: {
  494. printf("String\n");
  495. break;
  496. }
  497. case LY_TYPE_BOOL: {
  498. printf("Boolean true/false\n");
  499. break;
  500. }
  501. case LY_TYPE_DEC64: {
  502. printf("Signed decimal number\n");
  503. break;
  504. }
  505. case LY_TYPE_ENUM: {
  506. printf("Enumerated choice\n");
  507. break;
  508. }
  509. case LY_TYPE_IDENT: {
  510. printf("Identity\n");
  511. break;
  512. }
  513. case LY_TYPE_UNION: {
  514. struct lysc_type_union *t =
  515. (struct lysc_type_union *)type;
  516. LY_ARRAY_COUNT_TYPE u = 0;
  517. LY_ARRAY_FOR(t->types, u) {
  518. pline_print_type_help(node, t->types[u]);
  519. }
  520. break;
  521. }
  522. default:
  523. printf("Unknown\n");
  524. break;
  525. }
  526. }
  527. void pline_print_completions(const pline_t *pline, bool_t help)
  528. {
  529. faux_list_node_t *iter = NULL;
  530. pcompl_t *pcompl = NULL;
  531. iter = faux_list_head(pline->compls);
  532. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  533. struct lysc_type *type = NULL;
  534. const struct lysc_node *node = pcompl->node;
  535. if (pcompl->xpath && !help) {
  536. sr_val_t *vals = NULL;
  537. size_t val_num = 0;
  538. size_t i = 0;
  539. sr_get_items(pline->sess, pcompl->xpath,
  540. 0, 0, &vals, &val_num);
  541. for (i = 0; i < val_num; i++) {
  542. char *tmp = sr_val_to_str(&vals[i]);
  543. if (!tmp)
  544. continue;
  545. printf("%s\n", tmp);
  546. free(tmp);
  547. }
  548. }
  549. if (!node)
  550. continue;
  551. // Node
  552. if (PCOMPL_NODE == pcompl->type) {
  553. printf("%s\n", node->name);
  554. if (help) {
  555. if (!node->dsc) {
  556. printf("%s\n", node->name);
  557. } else {
  558. char *dsc = faux_str_getline(node->dsc,
  559. NULL);
  560. printf("%s\n", dsc);
  561. faux_str_free(dsc);
  562. }
  563. }
  564. continue;
  565. }
  566. // Type
  567. if (node->nodetype & LYS_LEAF)
  568. type = ((struct lysc_node_leaf *)node)->type;
  569. else if (node->nodetype & LYS_LEAFLIST)
  570. type = ((struct lysc_node_leaflist *)node)->type;
  571. else
  572. continue;
  573. if (help)
  574. pline_print_type_help(node, type);
  575. else
  576. pline_print_type_completions(type);
  577. }
  578. }