syms.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 "private.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. assert(context);
  55. if (sr_connect(SR_CONN_DEFAULT, &conn))
  56. return -1;
  57. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  58. sr_disconnect(conn);
  59. return -1;
  60. }
  61. cur_path = (faux_argv_t *)srp_udata_path(context);
  62. entry_name = kentry_name(kcontext_candidate_entry(context));
  63. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  64. pline = pline_parse(sess, args, srp_udata_flags(context));
  65. faux_argv_free(args);
  66. pline_print_completions(pline, help);
  67. pline_free(pline);
  68. sr_disconnect(conn);
  69. return 0;
  70. }
  71. int srp_compl(kcontext_t *context)
  72. {
  73. return srp_compl_or_help(context, BOOL_FALSE);
  74. }
  75. int srp_help(kcontext_t *context)
  76. {
  77. return srp_compl_or_help(context, BOOL_TRUE);
  78. }
  79. int srp_prompt_edit_path(kcontext_t *context)
  80. {
  81. faux_argv_t *cur_path = NULL;
  82. char *path = NULL;
  83. assert(context);
  84. cur_path = (faux_argv_t *)srp_udata_path(context);
  85. if (cur_path)
  86. path = faux_argv_line(cur_path);
  87. printf("[edit%s%s]\n", path ? " " : "", path ? path : "");
  88. faux_str_free(path);
  89. return 0;
  90. }
  91. static int srp_check_type(kcontext_t *context,
  92. pt_e not_accepted_nodes,
  93. size_t max_expr_num)
  94. {
  95. int ret = -1;
  96. faux_argv_t *args = NULL;
  97. pline_t *pline = NULL;
  98. sr_conn_ctx_t *conn = NULL;
  99. sr_session_ctx_t *sess = NULL;
  100. const char *entry_name = NULL;
  101. const char *value = NULL;
  102. pexpr_t *expr = NULL;
  103. size_t expr_num = 0;
  104. faux_argv_t *cur_path = NULL;
  105. assert(context);
  106. if (sr_connect(SR_CONN_DEFAULT, &conn))
  107. return -1;
  108. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  109. sr_disconnect(conn);
  110. return -1;
  111. }
  112. cur_path = (faux_argv_t *)srp_udata_path(context);
  113. entry_name = kentry_name(kcontext_candidate_entry(context));
  114. value = kcontext_candidate_value(context);
  115. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  116. if (value)
  117. faux_argv_add(args, value);
  118. pline = pline_parse(sess, args, srp_udata_flags(context));
  119. faux_argv_free(args);
  120. if (pline->invalid)
  121. goto err;
  122. expr_num = faux_list_len(pline->exprs);
  123. if (expr_num < 1)
  124. goto err;
  125. if ((max_expr_num > 0) && // '0' means unlimited
  126. (expr_num > max_expr_num))
  127. goto err;
  128. expr = pline_current_expr(pline);
  129. if (expr->pat & not_accepted_nodes)
  130. goto err;
  131. ret = 0;
  132. err:
  133. pline_free(pline);
  134. sr_disconnect(conn);
  135. return ret;
  136. }
  137. int srp_PLINE_SET(kcontext_t *context)
  138. {
  139. return srp_check_type(context, PT_NOT_SET, 0);
  140. }
  141. int srp_PLINE_DEL(kcontext_t *context)
  142. {
  143. return srp_check_type(context, PT_NOT_DEL, 1);
  144. }
  145. int srp_PLINE_EDIT(kcontext_t *context)
  146. {
  147. return srp_check_type(context, PT_NOT_EDIT, 1);
  148. }
  149. int srp_PLINE_INSERT_FROM(kcontext_t *context)
  150. {
  151. return srp_check_type(context, PT_NOT_INSERT, 1);
  152. }
  153. static faux_argv_t *assemble_insert_to(sr_session_ctx_t *sess, const kpargv_t *pargv,
  154. faux_argv_t *cur_path, const char *candidate_value, uint32_t flags)
  155. {
  156. faux_argv_t *args = NULL;
  157. faux_argv_t *insert_to = NULL;
  158. pline_t *pline = NULL;
  159. pexpr_t *expr = NULL;
  160. size_t i = 0;
  161. assert(sess);
  162. args = param2argv(cur_path, pargv, "from_path");
  163. pline = pline_parse(sess, args, flags);
  164. expr = pline_current_expr(pline);
  165. for (i = 0; i < (expr->args_num - expr->list_pos); i++) {
  166. faux_argv_node_t *iter = faux_argv_iterr(args);
  167. faux_argv_del(args, iter);
  168. }
  169. insert_to = param2argv(args, pargv, "to_path");
  170. faux_argv_free(args);
  171. if (candidate_value)
  172. faux_argv_add(insert_to, candidate_value);
  173. pline_free(pline);
  174. return insert_to;
  175. }
  176. int srp_PLINE_INSERT_TO(kcontext_t *context)
  177. {
  178. int ret = -1;
  179. faux_argv_t *args = NULL;
  180. pline_t *pline = NULL;
  181. sr_conn_ctx_t *conn = NULL;
  182. sr_session_ctx_t *sess = NULL;
  183. const char *value = NULL;
  184. pexpr_t *expr = NULL;
  185. size_t expr_num = 0;
  186. faux_argv_t *cur_path = NULL;
  187. assert(context);
  188. if (sr_connect(SR_CONN_DEFAULT, &conn))
  189. return -1;
  190. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  191. sr_disconnect(conn);
  192. return -1;
  193. }
  194. cur_path = (faux_argv_t *)srp_udata_path(context);
  195. value = kcontext_candidate_value(context);
  196. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  197. cur_path, value, srp_udata_flags(context));
  198. pline = pline_parse(sess, args, srp_udata_flags(context));
  199. faux_argv_free(args);
  200. if (pline->invalid)
  201. goto err;
  202. expr_num = faux_list_len(pline->exprs);
  203. if (expr_num != 1)
  204. goto err;
  205. expr = pline_current_expr(pline);
  206. if (expr->pat & PT_NOT_INSERT)
  207. goto err;
  208. ret = 0;
  209. err:
  210. pline_free(pline);
  211. sr_disconnect(conn);
  212. return ret;
  213. }
  214. static int srp_compl_or_help_insert_to(kcontext_t *context, bool_t help)
  215. {
  216. faux_argv_t *args = NULL;
  217. pline_t *pline = NULL;
  218. sr_conn_ctx_t *conn = NULL;
  219. sr_session_ctx_t *sess = NULL;
  220. faux_argv_t *cur_path = NULL;
  221. assert(context);
  222. if (sr_connect(SR_CONN_DEFAULT, &conn))
  223. return -1;
  224. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  225. sr_disconnect(conn);
  226. return -1;
  227. }
  228. cur_path = (faux_argv_t *)srp_udata_path(context);
  229. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  230. cur_path, NULL, srp_udata_flags(context));
  231. pline = pline_parse(sess, args, srp_udata_flags(context));
  232. faux_argv_free(args);
  233. pline_print_completions(pline, help);
  234. pline_free(pline);
  235. sr_disconnect(conn);
  236. return 0;
  237. }
  238. int srp_compl_insert_to(kcontext_t *context)
  239. {
  240. return srp_compl_or_help_insert_to(context, BOOL_FALSE);
  241. }
  242. int srp_help_insert_to(kcontext_t *context)
  243. {
  244. return srp_compl_or_help_insert_to(context, BOOL_TRUE);
  245. }
  246. int srp_set(kcontext_t *context)
  247. {
  248. int ret = 0;
  249. faux_argv_t *args = NULL;
  250. pline_t *pline = NULL;
  251. sr_conn_ctx_t *conn = NULL;
  252. sr_session_ctx_t *sess = NULL;
  253. faux_list_node_t *iter = NULL;
  254. pexpr_t *expr = NULL;
  255. size_t err_num = 0;
  256. faux_argv_t *cur_path = NULL;
  257. assert(context);
  258. if (sr_connect(SR_CONN_DEFAULT, &conn))
  259. return -1;
  260. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  261. sr_disconnect(conn);
  262. return -1;
  263. }
  264. cur_path = (faux_argv_t *)srp_udata_path(context);
  265. args = param2argv(cur_path, kcontext_pargv(context), "path");
  266. pline = pline_parse(sess, args, srp_udata_flags(context));
  267. faux_argv_free(args);
  268. if (pline->invalid) {
  269. fprintf(stderr, "Invalid set request\n");
  270. ret = -1;
  271. goto cleanup;
  272. }
  273. iter = faux_list_head(pline->exprs);
  274. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  275. if (!(expr->pat & PT_SET)) {
  276. err_num++;
  277. fprintf(stderr, "Illegal expression for set operation\n");
  278. break;
  279. }
  280. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  281. SR_ERR_OK) {
  282. err_num++;
  283. fprintf(stderr, "Can't set data\n");
  284. break;
  285. }
  286. }
  287. if (sr_has_changes(sess)) {
  288. if (err_num > 0)
  289. sr_discard_changes(sess);
  290. else
  291. sr_apply_changes(sess, 0);
  292. }
  293. if (err_num > 0)
  294. ret = -1;
  295. cleanup:
  296. pline_free(pline);
  297. sr_disconnect(conn);
  298. return ret;
  299. }
  300. int srp_del(kcontext_t *context)
  301. {
  302. int ret = -1;
  303. faux_argv_t *args = NULL;
  304. pline_t *pline = NULL;
  305. sr_conn_ctx_t *conn = NULL;
  306. sr_session_ctx_t *sess = NULL;
  307. pexpr_t *expr = NULL;
  308. size_t err_num = 0;
  309. faux_argv_t *cur_path = NULL;
  310. assert(context);
  311. if (sr_connect(SR_CONN_DEFAULT, &conn))
  312. return -1;
  313. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  314. sr_disconnect(conn);
  315. return -1;
  316. }
  317. cur_path = (faux_argv_t *)srp_udata_path(context);
  318. args = param2argv(cur_path, kcontext_pargv(context), "path");
  319. pline = pline_parse(sess, args, srp_udata_flags(context));
  320. faux_argv_free(args);
  321. if (pline->invalid) {
  322. fprintf(stderr, "Invalid 'del' request\n");
  323. goto err;
  324. }
  325. if (faux_list_len(pline->exprs) > 1) {
  326. fprintf(stderr, "Can't delete more than one object\n");
  327. goto err;
  328. }
  329. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  330. if (!(expr->pat & PT_DEL)) {
  331. fprintf(stderr, "Illegal expression for 'del' operation\n");
  332. goto err;
  333. }
  334. if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
  335. fprintf(stderr, "Can't delete data\n");
  336. goto err;
  337. }
  338. sr_apply_changes(sess, 0);
  339. ret = 0;
  340. err:
  341. pline_free(pline);
  342. sr_disconnect(conn);
  343. return ret;
  344. }
  345. int srp_edit(kcontext_t *context)
  346. {
  347. int ret = -1;
  348. faux_argv_t *args = NULL;
  349. pline_t *pline = NULL;
  350. sr_conn_ctx_t *conn = NULL;
  351. sr_session_ctx_t *sess = NULL;
  352. pexpr_t *expr = NULL;
  353. size_t err_num = 0;
  354. faux_argv_t *cur_path = NULL;
  355. assert(context);
  356. if (sr_connect(SR_CONN_DEFAULT, &conn))
  357. return -1;
  358. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  359. sr_disconnect(conn);
  360. return -1;
  361. }
  362. cur_path = (faux_argv_t *)srp_udata_path(context);
  363. args = param2argv(cur_path, kcontext_pargv(context), "path");
  364. pline = pline_parse(sess, args, srp_udata_flags(context));
  365. if (pline->invalid) {
  366. fprintf(stderr, "Invalid 'edit' request\n");
  367. goto err;
  368. }
  369. if (faux_list_len(pline->exprs) > 1) {
  370. fprintf(stderr, "Can't process more than one object\n");
  371. goto err;
  372. }
  373. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  374. if (!(expr->pat & PT_EDIT)) {
  375. fprintf(stderr, "Illegal expression for 'edit' operation\n");
  376. goto err;
  377. }
  378. if (sr_set_item_str(sess, expr->xpath, NULL, NULL, 0) != SR_ERR_OK) {
  379. fprintf(stderr, "Can't set editing data\n");
  380. goto err;
  381. }
  382. sr_apply_changes(sess, 0);
  383. // Set new current path
  384. srp_udata_set_path(context, args);
  385. ret = 0;
  386. err:
  387. if (ret < 0)
  388. faux_argv_free(args);
  389. pline_free(pline);
  390. sr_disconnect(conn);
  391. return ret;
  392. }
  393. int srp_top(kcontext_t *context)
  394. {
  395. assert(context);
  396. srp_udata_set_path(context, NULL);
  397. return 0;
  398. }
  399. int srp_up(kcontext_t *context)
  400. {
  401. sr_conn_ctx_t *conn = NULL;
  402. sr_session_ctx_t *sess = NULL;
  403. faux_argv_t *cur_path = NULL;
  404. faux_argv_node_t *iter = NULL;
  405. assert(context);
  406. cur_path = (faux_argv_t *)srp_udata_path(context);
  407. if (!cur_path)
  408. return -1; // It's top level and can't level up
  409. if (sr_connect(SR_CONN_DEFAULT, &conn))
  410. return -1;
  411. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  412. sr_disconnect(conn);
  413. return -1;
  414. }
  415. // Remove last arguments one by one and wait for legal edit-like pline
  416. while (faux_argv_len(cur_path) > 0) {
  417. pline_t *pline = NULL;
  418. pexpr_t *expr = NULL;
  419. size_t len = 0;
  420. iter = faux_argv_iterr(cur_path);
  421. faux_argv_del(cur_path, iter);
  422. pline = pline_parse(sess, cur_path, srp_udata_flags(context));
  423. if (pline->invalid) {
  424. pline_free(pline);
  425. continue;
  426. }
  427. len = faux_list_len(pline->exprs);
  428. if (len != 1) {
  429. pline_free(pline);
  430. continue;
  431. }
  432. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  433. if (!(expr->pat & PT_EDIT)) {
  434. pline_free(pline);
  435. continue;
  436. }
  437. // Here new path is ok
  438. pline_free(pline);
  439. break;
  440. }
  441. // Don't store empty path
  442. if (faux_argv_len(cur_path) == 0)
  443. srp_udata_set_path(context, NULL);
  444. sr_disconnect(conn);
  445. return 0;
  446. }
  447. int srp_insert(kcontext_t *context)
  448. {
  449. int ret = -1;
  450. pline_t *pline = NULL;
  451. pline_t *pline_to = NULL;
  452. sr_conn_ctx_t *conn = NULL;
  453. sr_session_ctx_t *sess = NULL;
  454. faux_list_node_t *iter = NULL;
  455. pexpr_t *expr = NULL;
  456. pexpr_t *expr_to = NULL;
  457. faux_argv_t *cur_path = NULL;
  458. faux_argv_t *insert_from = NULL;
  459. faux_argv_t *insert_to = NULL;
  460. sr_move_position_t position = SR_MOVE_LAST;
  461. kpargv_t *pargv = NULL;
  462. const char *list_keys = NULL;
  463. const char *leaflist_value = NULL;
  464. assert(context);
  465. if (sr_connect(SR_CONN_DEFAULT, &conn))
  466. return -1;
  467. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  468. sr_disconnect(conn);
  469. return -1;
  470. }
  471. cur_path = (faux_argv_t *)srp_udata_path(context);
  472. pargv = kcontext_pargv(context);
  473. // 'from' argument
  474. insert_from = param2argv(cur_path, pargv, "from_path");
  475. pline = pline_parse(sess, insert_from, srp_udata_flags(context));
  476. faux_argv_free(insert_from);
  477. if (pline->invalid) {
  478. fprintf(stderr, "Invalid 'from' expression\n");
  479. goto err;
  480. }
  481. if (faux_list_len(pline->exprs) > 1) {
  482. fprintf(stderr, "Can't process more than one object\n");
  483. goto err;
  484. }
  485. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  486. if (!(expr->pat & PT_INSERT)) {
  487. fprintf(stderr, "Illegal 'from' expression for 'insert' operation\n");
  488. goto err;
  489. }
  490. // Position
  491. if (kpargv_find(pargv, "first"))
  492. position = SR_MOVE_FIRST;
  493. else if (kpargv_find(pargv, "last"))
  494. position = SR_MOVE_LAST;
  495. else if (kpargv_find(pargv, "before"))
  496. position = SR_MOVE_BEFORE;
  497. else if (kpargv_find(pargv, "after"))
  498. position = SR_MOVE_AFTER;
  499. else {
  500. fprintf(stderr, "Illegal 'position' argument\n");
  501. goto err;
  502. }
  503. // 'to' argument
  504. if ((SR_MOVE_BEFORE == position) || (SR_MOVE_AFTER == position)) {
  505. insert_to = assemble_insert_to(sess, pargv, cur_path,
  506. NULL, srp_udata_flags(context));
  507. pline_to = pline_parse(sess, insert_to, srp_udata_flags(context));
  508. faux_argv_free(insert_to);
  509. if (pline_to->invalid) {
  510. fprintf(stderr, "Invalid 'to' expression\n");
  511. goto err;
  512. }
  513. if (faux_list_len(pline_to->exprs) > 1) {
  514. fprintf(stderr, "Can't process more than one object\n");
  515. goto err;
  516. }
  517. expr_to = (pexpr_t *)faux_list_data(faux_list_head(pline_to->exprs));
  518. if (!(expr_to->pat & PT_INSERT)) {
  519. fprintf(stderr, "Illegal 'to' expression for 'insert' operation\n");
  520. goto err;
  521. }
  522. if (PAT_LIST_KEY == expr_to->pat)
  523. list_keys = expr_to->last_keys;
  524. else // PATH_LEAFLIST_VALUE
  525. leaflist_value = expr_to->last_keys;
  526. }
  527. if (sr_move_item(sess, expr->xpath, position,
  528. list_keys, leaflist_value, NULL, 0) != SR_ERR_OK) {
  529. fprintf(stderr, "Can't move element\n");
  530. goto err;
  531. }
  532. sr_apply_changes(sess, 0);
  533. ret = 0;
  534. err:
  535. pline_free(pline);
  536. pline_free(pline_to);
  537. sr_disconnect(conn);
  538. return ret;
  539. }
  540. int srp_commit(kcontext_t *context)
  541. {
  542. int ret = -1;
  543. sr_conn_ctx_t *conn = NULL;
  544. sr_session_ctx_t *sess = NULL;
  545. assert(context);
  546. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  547. fprintf(stderr, "Can't connect to data repository\n");
  548. return -1;
  549. }
  550. // Validate candidate config
  551. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  552. fprintf(stderr, "Can't connect to candidate data store\n");
  553. goto err;
  554. }
  555. if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
  556. fprintf(stderr, "Invalid candidate configuration\n");
  557. goto err;
  558. }
  559. // Copy candidate to running-config
  560. if (sr_session_switch_ds(sess, SR_DS_RUNNING)) {
  561. fprintf(stderr, "Can't connect to running-config data store\n");
  562. goto err;
  563. }
  564. if (sr_copy_config(sess, NULL, SRP_REPO_EDIT, 0) != SR_ERR_OK) {
  565. fprintf(stderr, "Can't commit to running-config\n");
  566. goto err;
  567. }
  568. // Copy running-config to startup-config
  569. if (sr_session_switch_ds(sess, SR_DS_STARTUP)) {
  570. fprintf(stderr, "Can't connect to startup-config data store\n");
  571. goto err;
  572. }
  573. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  574. fprintf(stderr, "Can't store data to startup-config\n");
  575. goto err;
  576. }
  577. ret = 0;
  578. err:
  579. sr_disconnect(conn);
  580. return ret;
  581. }
  582. int srp_rollback(kcontext_t *context)
  583. {
  584. int ret = -1;
  585. sr_conn_ctx_t *conn = NULL;
  586. sr_session_ctx_t *sess = NULL;
  587. assert(context);
  588. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  589. fprintf(stderr, "Can't connect to data repository\n");
  590. return -1;
  591. }
  592. // Copy running-config to candidate config
  593. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  594. fprintf(stderr, "Can't connect to candidate data store\n");
  595. goto err;
  596. }
  597. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  598. fprintf(stderr, "Can't rollback to running-config\n");
  599. goto err;
  600. }
  601. ret = 0;
  602. err:
  603. sr_disconnect(conn);
  604. return ret;
  605. }
  606. int srp_show_xml(kcontext_t *context)
  607. {
  608. int ret = -1;
  609. faux_argv_t *args = NULL;
  610. pline_t *pline = NULL;
  611. sr_conn_ctx_t *conn = NULL;
  612. sr_session_ctx_t *sess = NULL;
  613. pexpr_t *expr = NULL;
  614. faux_argv_t *cur_path = NULL;
  615. sr_data_t *data = NULL;
  616. struct ly_out *out = NULL;
  617. struct lyd_node *child = NULL;
  618. assert(context);
  619. if (sr_connect(SR_CONN_DEFAULT, &conn))
  620. return -1;
  621. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  622. sr_disconnect(conn);
  623. return -1;
  624. }
  625. cur_path = (faux_argv_t *)srp_udata_path(context);
  626. args = param2argv(cur_path, kcontext_pargv(context), "path");
  627. pline = pline_parse(sess, args, srp_udata_flags(context));
  628. faux_argv_free(args);
  629. if (pline->invalid) {
  630. fprintf(stderr, "Invalid 'show' request\n");
  631. goto err;
  632. }
  633. if (faux_list_len(pline->exprs) > 1) {
  634. fprintf(stderr, "Can't process more than one object\n");
  635. goto err;
  636. }
  637. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  638. if (!(expr->pat & PT_EDIT)) {
  639. fprintf(stderr, "Illegal expression for 'show' operation\n");
  640. goto err;
  641. }
  642. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  643. fprintf(stderr, "Can't get specified subtree\n");
  644. goto err;
  645. }
  646. ly_out_new_file(stdout, &out);
  647. lyd_print_tree(out, data->tree, LYD_XML, 0);
  648. ly_out_free(out, NULL, 0);
  649. // child = lyd_child(data->tree);
  650. // if (child) {
  651. // ly_out_new_file(stdout, &out);
  652. // lyd_print_all(out, child, LYD_XML, 0);
  653. // }
  654. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  655. if (meta)
  656. printf("META\n");
  657. sr_release_data(data);
  658. ret = 0;
  659. err:
  660. pline_free(pline);
  661. sr_disconnect(conn);
  662. return ret;
  663. }
  664. static int show(kcontext_t *context, sr_datastore_t ds)
  665. {
  666. int ret = -1;
  667. faux_argv_t *args = NULL;
  668. pline_t *pline = NULL;
  669. sr_conn_ctx_t *conn = NULL;
  670. sr_session_ctx_t *sess = NULL;
  671. pexpr_t *expr = NULL;
  672. faux_argv_t *cur_path = NULL;
  673. char *xpath = NULL;
  674. assert(context);
  675. if (sr_connect(SR_CONN_DEFAULT, &conn))
  676. return -1;
  677. if (sr_session_start(conn, ds, &sess)) {
  678. sr_disconnect(conn);
  679. return -1;
  680. }
  681. cur_path = (faux_argv_t *)srp_udata_path(context);
  682. if (kpargv_find(kcontext_pargv(context), "path") || cur_path) {
  683. args = param2argv(cur_path, kcontext_pargv(context), "path");
  684. pline = pline_parse(sess, args, srp_udata_flags(context));
  685. faux_argv_free(args);
  686. if (pline->invalid) {
  687. fprintf(stderr, "Invalid 'show' request\n");
  688. goto err;
  689. }
  690. if (faux_list_len(pline->exprs) > 1) {
  691. fprintf(stderr, "Can't process more than one object\n");
  692. goto err;
  693. }
  694. if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
  695. fprintf(stderr, "Can't get expression\n");
  696. goto err;
  697. }
  698. if (!(expr->pat & PT_EDIT)) {
  699. fprintf(stderr, "Illegal expression for 'show' operation\n");
  700. goto err;
  701. }
  702. if (!expr->xpath) {
  703. fprintf(stderr, "Empty expression for 'show' operation\n");
  704. goto err;
  705. }
  706. xpath = expr->xpath;
  707. }
  708. show_xpath(sess, xpath, srp_udata_flags(context));
  709. ret = 0;
  710. err:
  711. pline_free(pline);
  712. sr_disconnect(conn);
  713. return ret;
  714. }
  715. int srp_show(kcontext_t *context)
  716. {
  717. return show(context, SRP_REPO_EDIT);
  718. }
  719. int srp_show_running(kcontext_t *context)
  720. {
  721. sr_conn_ctx_t *conn = NULL;
  722. sr_session_ctx_t *sess = NULL;
  723. if (sr_connect(SR_CONN_DEFAULT, &conn))
  724. return -1;
  725. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  726. sr_disconnect(conn);
  727. return -1;
  728. }
  729. show_xpath(sess, NULL, srp_udata_flags(context));
  730. sr_disconnect(conn);
  731. return 0;
  732. }
  733. int srp_deactivate(kcontext_t *context)
  734. {
  735. int ret = -1;
  736. faux_argv_t *args = NULL;
  737. pline_t *pline = NULL;
  738. sr_conn_ctx_t *conn = NULL;
  739. sr_session_ctx_t *sess = NULL;
  740. pexpr_t *expr = NULL;
  741. faux_argv_t *cur_path = NULL;
  742. sr_data_t *data = NULL;
  743. const struct ly_ctx *ctx = NULL;
  744. assert(context);
  745. if (sr_connect(SR_CONN_DEFAULT, &conn))
  746. return -1;
  747. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  748. sr_disconnect(conn);
  749. return -1;
  750. }
  751. cur_path = (faux_argv_t *)srp_udata_path(context);
  752. args = param2argv(cur_path, kcontext_pargv(context), "path");
  753. pline = pline_parse(sess, args, srp_udata_flags(context));
  754. faux_argv_free(args);
  755. if (pline->invalid) {
  756. fprintf(stderr, "Invalid 'show' request\n");
  757. goto err;
  758. }
  759. if (faux_list_len(pline->exprs) > 1) {
  760. fprintf(stderr, "Can't process more than one object\n");
  761. goto err;
  762. }
  763. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  764. if (!(expr->pat & PT_DEL)) {
  765. fprintf(stderr, "Illegal expression for 'show' operation\n");
  766. goto err;
  767. }
  768. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  769. fprintf(stderr, "Can't get specified subtree\n");
  770. goto err;
  771. }
  772. if (lyd_new_meta(LYD_CTX(data->tree), data->tree, NULL,
  773. "junos-configuration-metadata:active", "false", 0, NULL)) {
  774. fprintf(stderr, "Can't deactivate\n");
  775. goto err;
  776. }
  777. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  778. if (meta)
  779. printf("META\n");
  780. if (sr_has_changes(sess))
  781. fprintf(stderr, "Has changes\n");
  782. if (sr_apply_changes(sess, 0)) {
  783. fprintf(stderr, "Can't apply changes\n");
  784. }
  785. sr_release_data(data);
  786. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  787. fprintf(stderr, "Can't get specified subtree\n");
  788. goto err;
  789. }
  790. struct ly_out *out = NULL;
  791. ly_out_new_file(stdout, &out);
  792. lyd_print_tree(out, data->tree, LYD_XML, 0);
  793. ly_out_free(out, NULL, 0);
  794. sr_release_data(data);
  795. ret = 0;
  796. err:
  797. pline_free(pline);
  798. sr_disconnect(conn);
  799. return ret;
  800. }