syms.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <syslog.h>
  6. #include <faux/faux.h>
  7. #include <faux/str.h>
  8. #include <faux/argv.h>
  9. #include <faux/list.h>
  10. #include <faux/error.h>
  11. #include <klish/khelper.h>
  12. #include <klish/kplugin.h>
  13. #include <klish/kentry.h>
  14. #include <klish/kscheme.h>
  15. #include <klish/kcontext.h>
  16. #include <klish/kpargv.h>
  17. #include <sysrepo.h>
  18. #include <sysrepo/xpath.h>
  19. #include "sr_copypaste.h"
  20. #include "pline.h"
  21. #include "syms.h"
  22. static faux_argv_t *param2argv(const faux_argv_t *cur_path,
  23. const kpargv_t *pargv, const char *entry_name)
  24. {
  25. faux_list_node_t *iter = NULL;
  26. faux_list_t *pargs = NULL;
  27. faux_argv_t *args = NULL;
  28. kparg_t *parg = NULL;
  29. assert(pargv);
  30. if (!pargv)
  31. return NULL;
  32. pargs = kpargv_find_multi(pargv, entry_name);
  33. if (cur_path)
  34. args = faux_argv_dup(cur_path);
  35. else
  36. args = faux_argv_new();
  37. iter = faux_list_head(pargs);
  38. while ((parg = (kparg_t *)faux_list_each(&iter))) {
  39. faux_argv_add(args, kparg_value(parg));
  40. }
  41. faux_list_free(pargs);
  42. return args;
  43. }
  44. // Candidate from pargv contains possible begin of current word (that must be
  45. // completed). kpargv's list don't contain candidate but only already parsed
  46. // words.
  47. static int srp_compl_or_help(kcontext_t *context, bool_t help)
  48. {
  49. faux_argv_t *args = NULL;
  50. pline_t *pline = NULL;
  51. sr_conn_ctx_t *conn = NULL;
  52. sr_session_ctx_t *sess = NULL;
  53. const char *entry_name = NULL;
  54. faux_argv_t *cur_path = NULL;
  55. kplugin_t *plugin = NULL;
  56. assert(context);
  57. if (sr_connect(SR_CONN_DEFAULT, &conn))
  58. return -1;
  59. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  60. sr_disconnect(conn);
  61. return -1;
  62. }
  63. plugin = kcontext_plugin(context);
  64. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  65. entry_name = kentry_name(kcontext_candidate_entry(context));
  66. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  67. pline = pline_parse(sess, args, 0);
  68. faux_argv_free(args);
  69. pline_print_completions(pline, help);
  70. pline_free(pline);
  71. sr_disconnect(conn);
  72. return 0;
  73. }
  74. int srp_compl(kcontext_t *context)
  75. {
  76. return srp_compl_or_help(context, BOOL_FALSE);
  77. }
  78. int srp_help(kcontext_t *context)
  79. {
  80. return srp_compl_or_help(context, BOOL_TRUE);
  81. }
  82. int srp_prompt_edit_path(kcontext_t *context)
  83. {
  84. faux_argv_t *cur_path = NULL;
  85. kplugin_t *plugin = NULL;
  86. char *path = NULL;
  87. assert(context);
  88. plugin = kcontext_plugin(context);
  89. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  90. if (cur_path)
  91. path = faux_argv_line(cur_path);
  92. printf("[edit%s%s]\n", path ? " " : "", path ? path : "");
  93. faux_str_free(path);
  94. return 0;
  95. }
  96. static int srp_check_type(kcontext_t *context,
  97. pt_e not_accepted_nodes,
  98. size_t max_expr_num)
  99. {
  100. int ret = -1;
  101. faux_argv_t *args = NULL;
  102. pline_t *pline = NULL;
  103. sr_conn_ctx_t *conn = NULL;
  104. sr_session_ctx_t *sess = NULL;
  105. const char *entry_name = NULL;
  106. const char *value = NULL;
  107. pexpr_t *expr = NULL;
  108. size_t expr_num = 0;
  109. faux_argv_t *cur_path = NULL;
  110. kplugin_t *plugin = NULL;
  111. assert(context);
  112. if (sr_connect(SR_CONN_DEFAULT, &conn))
  113. return -1;
  114. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  115. sr_disconnect(conn);
  116. return -1;
  117. }
  118. plugin = kcontext_plugin(context);
  119. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  120. entry_name = kentry_name(kcontext_candidate_entry(context));
  121. value = kcontext_candidate_value(context);
  122. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  123. if (value)
  124. faux_argv_add(args, value);
  125. pline = pline_parse(sess, args, 0);
  126. faux_argv_free(args);
  127. if (pline->invalid)
  128. goto err;
  129. expr_num = faux_list_len(pline->exprs);
  130. if (expr_num < 1)
  131. goto err;
  132. if ((max_expr_num > 0) && // '0' means unlimited
  133. (expr_num > max_expr_num))
  134. goto err;
  135. expr = pline_current_expr(pline);
  136. if (expr->pat & not_accepted_nodes)
  137. goto err;
  138. ret = 0;
  139. err:
  140. pline_free(pline);
  141. sr_disconnect(conn);
  142. return ret;
  143. }
  144. int srp_PLINE_SET(kcontext_t *context)
  145. {
  146. return srp_check_type(context, PT_NOT_SET, 0);
  147. }
  148. int srp_PLINE_DEL(kcontext_t *context)
  149. {
  150. return srp_check_type(context, PT_NOT_DEL, 1);
  151. }
  152. int srp_PLINE_EDIT(kcontext_t *context)
  153. {
  154. return srp_check_type(context, PT_NOT_EDIT, 1);
  155. }
  156. int srp_PLINE_INSERT_FROM(kcontext_t *context)
  157. {
  158. return srp_check_type(context, PT_NOT_INSERT, 1);
  159. }
  160. static faux_argv_t *assemble_insert_to(sr_session_ctx_t *sess, const kpargv_t *pargv,
  161. faux_argv_t *cur_path, const char *candidate_value)
  162. {
  163. faux_argv_t *args = NULL;
  164. faux_argv_t *insert_to = NULL;
  165. pline_t *pline = NULL;
  166. pexpr_t *expr = NULL;
  167. size_t i = 0;
  168. assert(sess);
  169. args = param2argv(cur_path, pargv, "from_path");
  170. pline = pline_parse(sess, args, 0);
  171. expr = pline_current_expr(pline);
  172. for (i = 0; i < (expr->args_num - expr->list_pos); i++) {
  173. faux_argv_node_t *iter = faux_argv_iterr(args);
  174. faux_argv_del(args, iter);
  175. }
  176. insert_to = param2argv(args, pargv, "to_path");
  177. faux_argv_free(args);
  178. if (candidate_value)
  179. faux_argv_add(insert_to, candidate_value);
  180. pline_free(pline);
  181. return insert_to;
  182. }
  183. int srp_PLINE_INSERT_TO(kcontext_t *context)
  184. {
  185. int ret = -1;
  186. faux_argv_t *args = NULL;
  187. pline_t *pline = NULL;
  188. sr_conn_ctx_t *conn = NULL;
  189. sr_session_ctx_t *sess = NULL;
  190. const char *value = NULL;
  191. pexpr_t *expr = NULL;
  192. size_t expr_num = 0;
  193. faux_argv_t *cur_path = NULL;
  194. kplugin_t *plugin = NULL;
  195. assert(context);
  196. if (sr_connect(SR_CONN_DEFAULT, &conn))
  197. return -1;
  198. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  199. sr_disconnect(conn);
  200. return -1;
  201. }
  202. plugin = kcontext_plugin(context);
  203. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  204. value = kcontext_candidate_value(context);
  205. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  206. cur_path, value);
  207. pline = pline_parse(sess, args, 0);
  208. faux_argv_free(args);
  209. if (pline->invalid)
  210. goto err;
  211. expr_num = faux_list_len(pline->exprs);
  212. if (expr_num != 1)
  213. goto err;
  214. expr = pline_current_expr(pline);
  215. if (expr->pat & PT_NOT_INSERT)
  216. goto err;
  217. ret = 0;
  218. err:
  219. pline_free(pline);
  220. sr_disconnect(conn);
  221. return ret;
  222. }
  223. static int srp_compl_or_help_insert_to(kcontext_t *context, bool_t help)
  224. {
  225. faux_argv_t *args = NULL;
  226. pline_t *pline = NULL;
  227. sr_conn_ctx_t *conn = NULL;
  228. sr_session_ctx_t *sess = NULL;
  229. faux_argv_t *cur_path = NULL;
  230. kplugin_t *plugin = NULL;
  231. assert(context);
  232. if (sr_connect(SR_CONN_DEFAULT, &conn))
  233. return -1;
  234. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  235. sr_disconnect(conn);
  236. return -1;
  237. }
  238. plugin = kcontext_plugin(context);
  239. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  240. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  241. cur_path, NULL);
  242. pline = pline_parse(sess, args, 0);
  243. faux_argv_free(args);
  244. pline_print_completions(pline, help);
  245. pline_free(pline);
  246. sr_disconnect(conn);
  247. return 0;
  248. }
  249. int srp_compl_insert_to(kcontext_t *context)
  250. {
  251. return srp_compl_or_help_insert_to(context, BOOL_FALSE);
  252. }
  253. int srp_help_insert_to(kcontext_t *context)
  254. {
  255. return srp_compl_or_help_insert_to(context, BOOL_TRUE);
  256. }
  257. int srp_set(kcontext_t *context)
  258. {
  259. int ret = 0;
  260. faux_argv_t *args = NULL;
  261. pline_t *pline = NULL;
  262. sr_conn_ctx_t *conn = NULL;
  263. sr_session_ctx_t *sess = NULL;
  264. faux_list_node_t *iter = NULL;
  265. pexpr_t *expr = NULL;
  266. size_t err_num = 0;
  267. faux_argv_t *cur_path = NULL;
  268. kplugin_t *plugin = NULL;
  269. assert(context);
  270. if (sr_connect(SR_CONN_DEFAULT, &conn))
  271. return -1;
  272. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  273. sr_disconnect(conn);
  274. return -1;
  275. }
  276. plugin = kcontext_plugin(context);
  277. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  278. args = param2argv(cur_path, kcontext_pargv(context), "path");
  279. pline = pline_parse(sess, args, 0);
  280. faux_argv_free(args);
  281. if (pline->invalid) {
  282. fprintf(stderr, "Invalid set request\n");
  283. ret = -1;
  284. goto cleanup;
  285. }
  286. iter = faux_list_head(pline->exprs);
  287. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  288. if (!(expr->pat & PT_SET)) {
  289. err_num++;
  290. fprintf(stderr, "Illegal expression for set operation\n");
  291. break;
  292. }
  293. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  294. SR_ERR_OK) {
  295. err_num++;
  296. fprintf(stderr, "Can't set data\n");
  297. break;
  298. }
  299. }
  300. if (sr_has_changes(sess)) {
  301. if (err_num > 0)
  302. sr_discard_changes(sess);
  303. else
  304. sr_apply_changes(sess, 0);
  305. }
  306. if (err_num > 0)
  307. ret = -1;
  308. cleanup:
  309. pline_free(pline);
  310. sr_disconnect(conn);
  311. return ret;
  312. }
  313. int srp_del(kcontext_t *context)
  314. {
  315. int ret = -1;
  316. faux_argv_t *args = NULL;
  317. pline_t *pline = NULL;
  318. sr_conn_ctx_t *conn = NULL;
  319. sr_session_ctx_t *sess = NULL;
  320. pexpr_t *expr = NULL;
  321. size_t err_num = 0;
  322. faux_argv_t *cur_path = NULL;
  323. kplugin_t *plugin = NULL;
  324. assert(context);
  325. if (sr_connect(SR_CONN_DEFAULT, &conn))
  326. return -1;
  327. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  328. sr_disconnect(conn);
  329. return -1;
  330. }
  331. plugin = kcontext_plugin(context);
  332. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  333. args = param2argv(cur_path, kcontext_pargv(context), "path");
  334. pline = pline_parse(sess, args, 0);
  335. faux_argv_free(args);
  336. if (pline->invalid) {
  337. fprintf(stderr, "Invalid 'del' request\n");
  338. goto err;
  339. }
  340. if (faux_list_len(pline->exprs) > 1) {
  341. fprintf(stderr, "Can't delete more than one object\n");
  342. goto err;
  343. }
  344. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  345. if (!(expr->pat & PT_DEL)) {
  346. fprintf(stderr, "Illegal expression for 'del' operation\n");
  347. goto err;
  348. }
  349. if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
  350. fprintf(stderr, "Can't delete data\n");
  351. goto err;
  352. }
  353. sr_apply_changes(sess, 0);
  354. ret = 0;
  355. err:
  356. pline_free(pline);
  357. sr_disconnect(conn);
  358. return ret;
  359. }
  360. int srp_edit(kcontext_t *context)
  361. {
  362. int ret = -1;
  363. faux_argv_t *args = NULL;
  364. pline_t *pline = NULL;
  365. sr_conn_ctx_t *conn = NULL;
  366. sr_session_ctx_t *sess = NULL;
  367. pexpr_t *expr = NULL;
  368. size_t err_num = 0;
  369. faux_argv_t *cur_path = NULL;
  370. kplugin_t *plugin = NULL;
  371. assert(context);
  372. if (sr_connect(SR_CONN_DEFAULT, &conn))
  373. return -1;
  374. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  375. sr_disconnect(conn);
  376. return -1;
  377. }
  378. plugin = kcontext_plugin(context);
  379. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  380. args = param2argv(cur_path, kcontext_pargv(context), "path");
  381. pline = pline_parse(sess, args, 0);
  382. if (pline->invalid) {
  383. fprintf(stderr, "Invalid 'edit' request\n");
  384. goto err;
  385. }
  386. if (faux_list_len(pline->exprs) > 1) {
  387. fprintf(stderr, "Can't process more than one object\n");
  388. goto err;
  389. }
  390. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  391. if (!(expr->pat & PT_EDIT)) {
  392. fprintf(stderr, "Illegal expression for 'edit' operation\n");
  393. goto err;
  394. }
  395. if (sr_set_item_str(sess, expr->xpath, NULL, NULL, 0) != SR_ERR_OK) {
  396. fprintf(stderr, "Can't set editing data\n");
  397. goto err;
  398. }
  399. sr_apply_changes(sess, 0);
  400. // Set new current path
  401. faux_argv_free(cur_path);
  402. kplugin_set_udata(plugin, args);
  403. ret = 0;
  404. err:
  405. if (ret < 0)
  406. faux_argv_free(args);
  407. pline_free(pline);
  408. sr_disconnect(conn);
  409. return ret;
  410. }
  411. int srp_top(kcontext_t *context)
  412. {
  413. faux_argv_t *cur_path = NULL;
  414. kplugin_t *plugin = NULL;
  415. assert(context);
  416. plugin = kcontext_plugin(context);
  417. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  418. faux_argv_free(cur_path);
  419. kplugin_set_udata(plugin, NULL);
  420. return 0;
  421. }
  422. int srp_up(kcontext_t *context)
  423. {
  424. sr_conn_ctx_t *conn = NULL;
  425. sr_session_ctx_t *sess = NULL;
  426. faux_argv_t *cur_path = NULL;
  427. kplugin_t *plugin = NULL;
  428. faux_argv_node_t *iter = NULL;
  429. assert(context);
  430. plugin = kcontext_plugin(context);
  431. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  432. if (!cur_path)
  433. return -1; // It's top level and can't level up
  434. if (sr_connect(SR_CONN_DEFAULT, &conn))
  435. return -1;
  436. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  437. sr_disconnect(conn);
  438. return -1;
  439. }
  440. // Remove last arguments one by one and wait for legal edit-like pline
  441. while (faux_argv_len(cur_path) > 0) {
  442. pline_t *pline = NULL;
  443. pexpr_t *expr = NULL;
  444. size_t len = 0;
  445. iter = faux_argv_iterr(cur_path);
  446. faux_argv_del(cur_path, iter);
  447. pline = pline_parse(sess, cur_path, 0);
  448. if (pline->invalid) {
  449. pline_free(pline);
  450. continue;
  451. }
  452. len = faux_list_len(pline->exprs);
  453. if (len != 1) {
  454. pline_free(pline);
  455. continue;
  456. }
  457. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  458. if (!(expr->pat & PT_EDIT)) {
  459. pline_free(pline);
  460. continue;
  461. }
  462. // Here new path is ok
  463. pline_free(pline);
  464. break;
  465. }
  466. // Don't store empty path
  467. while (faux_argv_len(cur_path) == 0) {
  468. faux_argv_free(cur_path);
  469. kplugin_set_udata(plugin, NULL);
  470. }
  471. sr_disconnect(conn);
  472. return 0;
  473. }
  474. int srp_insert(kcontext_t *context)
  475. {
  476. int ret = -1;
  477. pline_t *pline = NULL;
  478. pline_t *pline_to = NULL;
  479. sr_conn_ctx_t *conn = NULL;
  480. sr_session_ctx_t *sess = NULL;
  481. faux_list_node_t *iter = NULL;
  482. pexpr_t *expr = NULL;
  483. pexpr_t *expr_to = NULL;
  484. faux_argv_t *cur_path = NULL;
  485. kplugin_t *plugin = NULL;
  486. faux_argv_t *insert_from = NULL;
  487. faux_argv_t *insert_to = NULL;
  488. sr_move_position_t position = SR_MOVE_LAST;
  489. kpargv_t *pargv = NULL;
  490. const char *list_keys = NULL;
  491. const char *leaflist_value = NULL;
  492. assert(context);
  493. if (sr_connect(SR_CONN_DEFAULT, &conn))
  494. return -1;
  495. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  496. sr_disconnect(conn);
  497. return -1;
  498. }
  499. plugin = kcontext_plugin(context);
  500. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  501. pargv = kcontext_pargv(context);
  502. // 'from' argument
  503. insert_from = param2argv(cur_path, pargv, "from_path");
  504. pline = pline_parse(sess, insert_from, 0);
  505. faux_argv_free(insert_from);
  506. if (pline->invalid) {
  507. fprintf(stderr, "Invalid 'from' expression\n");
  508. goto err;
  509. }
  510. if (faux_list_len(pline->exprs) > 1) {
  511. fprintf(stderr, "Can't process more than one object\n");
  512. goto err;
  513. }
  514. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  515. if (!(expr->pat & PT_INSERT)) {
  516. fprintf(stderr, "Illegal 'from' expression for 'insert' operation\n");
  517. goto err;
  518. }
  519. // Position
  520. if (kpargv_find(pargv, "first"))
  521. position = SR_MOVE_FIRST;
  522. else if (kpargv_find(pargv, "last"))
  523. position = SR_MOVE_LAST;
  524. else if (kpargv_find(pargv, "before"))
  525. position = SR_MOVE_BEFORE;
  526. else if (kpargv_find(pargv, "after"))
  527. position = SR_MOVE_AFTER;
  528. else {
  529. fprintf(stderr, "Illegal 'position' argument\n");
  530. goto err;
  531. }
  532. // 'to' argument
  533. if ((SR_MOVE_BEFORE == position) || (SR_MOVE_AFTER == position)) {
  534. insert_to = assemble_insert_to(sess, pargv, cur_path, NULL);
  535. pline_to = pline_parse(sess, insert_to, 0);
  536. faux_argv_free(insert_to);
  537. if (pline_to->invalid) {
  538. fprintf(stderr, "Invalid 'to' expression\n");
  539. goto err;
  540. }
  541. if (faux_list_len(pline_to->exprs) > 1) {
  542. fprintf(stderr, "Can't process more than one object\n");
  543. goto err;
  544. }
  545. expr_to = (pexpr_t *)faux_list_data(faux_list_head(pline_to->exprs));
  546. if (!(expr_to->pat & PT_INSERT)) {
  547. fprintf(stderr, "Illegal 'to' expression for 'insert' operation\n");
  548. goto err;
  549. }
  550. if (PAT_LIST_KEY == expr_to->pat)
  551. list_keys = expr_to->last_keys;
  552. else // PATH_LEAFLIST_VALUE
  553. leaflist_value = expr_to->last_keys;
  554. }
  555. if (sr_move_item(sess, expr->xpath, position,
  556. list_keys, leaflist_value, NULL, 0) != SR_ERR_OK) {
  557. fprintf(stderr, "Can't move element\n");
  558. goto err;
  559. }
  560. sr_apply_changes(sess, 0);
  561. ret = 0;
  562. err:
  563. pline_free(pline);
  564. pline_free(pline_to);
  565. sr_disconnect(conn);
  566. return ret;
  567. }
  568. int srp_commit(kcontext_t *context)
  569. {
  570. int ret = -1;
  571. sr_conn_ctx_t *conn = NULL;
  572. sr_session_ctx_t *sess = NULL;
  573. assert(context);
  574. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  575. fprintf(stderr, "Can't connect to data repository\n");
  576. return -1;
  577. }
  578. // Validate candidate config
  579. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  580. fprintf(stderr, "Can't connect to candidate data store\n");
  581. goto err;
  582. }
  583. if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
  584. fprintf(stderr, "Invalid candidate configuration\n");
  585. goto err;
  586. }
  587. // Copy candidate to running-config
  588. if (sr_session_switch_ds(sess, SR_DS_RUNNING)) {
  589. fprintf(stderr, "Can't connect to running-config data store\n");
  590. goto err;
  591. }
  592. if (sr_copy_config(sess, NULL, SRP_REPO_EDIT, 0) != SR_ERR_OK) {
  593. fprintf(stderr, "Can't commit to running-config\n");
  594. goto err;
  595. }
  596. // Copy running-config to startup-config
  597. if (sr_session_switch_ds(sess, SR_DS_STARTUP)) {
  598. fprintf(stderr, "Can't connect to startup-config data store\n");
  599. goto err;
  600. }
  601. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  602. fprintf(stderr, "Can't store data to startup-config\n");
  603. goto err;
  604. }
  605. ret = 0;
  606. err:
  607. sr_disconnect(conn);
  608. return ret;
  609. }
  610. int srp_rollback(kcontext_t *context)
  611. {
  612. int ret = -1;
  613. sr_conn_ctx_t *conn = NULL;
  614. sr_session_ctx_t *sess = NULL;
  615. assert(context);
  616. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  617. fprintf(stderr, "Can't connect to data repository\n");
  618. return -1;
  619. }
  620. // Copy running-config to candidate config
  621. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  622. fprintf(stderr, "Can't connect to candidate data store\n");
  623. goto err;
  624. }
  625. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  626. fprintf(stderr, "Can't rollback to running-config\n");
  627. goto err;
  628. }
  629. ret = 0;
  630. err:
  631. sr_disconnect(conn);
  632. return ret;
  633. }
  634. int srp_show(kcontext_t *context)
  635. {
  636. int ret = -1;
  637. faux_argv_t *args = NULL;
  638. pline_t *pline = NULL;
  639. sr_conn_ctx_t *conn = NULL;
  640. sr_session_ctx_t *sess = NULL;
  641. pexpr_t *expr = NULL;
  642. faux_argv_t *cur_path = NULL;
  643. kplugin_t *plugin = NULL;
  644. sr_data_t *data = NULL;
  645. struct ly_out *out = NULL;
  646. struct lyd_node *child = NULL;
  647. assert(context);
  648. if (sr_connect(SR_CONN_DEFAULT, &conn))
  649. return -1;
  650. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  651. sr_disconnect(conn);
  652. return -1;
  653. }
  654. plugin = kcontext_plugin(context);
  655. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  656. args = param2argv(cur_path, kcontext_pargv(context), "path");
  657. pline = pline_parse(sess, args, 0);
  658. faux_argv_free(args);
  659. if (pline->invalid) {
  660. fprintf(stderr, "Invalid 'show' request\n");
  661. goto err;
  662. }
  663. if (faux_list_len(pline->exprs) > 1) {
  664. fprintf(stderr, "Can't process more than one object\n");
  665. goto err;
  666. }
  667. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  668. if (!(expr->pat & PT_EDIT)) {
  669. fprintf(stderr, "Illegal expression for 'show' operation\n");
  670. goto err;
  671. }
  672. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  673. fprintf(stderr, "Can't get specified subtree\n");
  674. goto err;
  675. }
  676. ly_out_new_file(stdout, &out);
  677. lyd_print_tree(out, data->tree, LYD_XML, 0);
  678. ly_out_free(out, NULL, 0);
  679. // child = lyd_child(data->tree);
  680. // if (child) {
  681. // ly_out_new_file(stdout, &out);
  682. // lyd_print_all(out, child, LYD_XML, 0);
  683. // }
  684. sr_release_data(data);
  685. ret = 0;
  686. err:
  687. pline_free(pline);
  688. sr_disconnect(conn);
  689. return ret;
  690. }