syms.c 16 KB

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