pline.c 14 KB

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