syms.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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_verify(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. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  551. fprintf(stderr, "Can't connect to candidate data store\n");
  552. goto err;
  553. }
  554. // Validate candidate config
  555. if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
  556. fprintf(stderr, "Invalid candidate configuration\n");
  557. goto err;
  558. }
  559. ret = 0;
  560. err:
  561. sr_disconnect(conn);
  562. return ret;
  563. }
  564. int srp_commit(kcontext_t *context)
  565. {
  566. int ret = -1;
  567. sr_conn_ctx_t *conn = NULL;
  568. sr_session_ctx_t *sess = NULL;
  569. assert(context);
  570. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  571. fprintf(stderr, "Can't connect to data repository\n");
  572. return -1;
  573. }
  574. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  575. fprintf(stderr, "Can't connect to candidate data store\n");
  576. goto err;
  577. }
  578. // Copy candidate to running-config
  579. if (sr_session_switch_ds(sess, SR_DS_RUNNING)) {
  580. fprintf(stderr, "Can't connect to running-config data store\n");
  581. goto err;
  582. }
  583. if (sr_copy_config(sess, NULL, SRP_REPO_EDIT, 0) != SR_ERR_OK) {
  584. fprintf(stderr, "Can't commit to running-config\n");
  585. goto err;
  586. }
  587. // Copy running-config to startup-config
  588. if (sr_session_switch_ds(sess, SR_DS_STARTUP)) {
  589. fprintf(stderr, "Can't connect to startup-config data store\n");
  590. goto err;
  591. }
  592. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  593. fprintf(stderr, "Can't store data to startup-config\n");
  594. goto err;
  595. }
  596. ret = 0;
  597. err:
  598. sr_disconnect(conn);
  599. return ret;
  600. }
  601. int srp_rollback(kcontext_t *context)
  602. {
  603. int ret = -1;
  604. sr_conn_ctx_t *conn = NULL;
  605. sr_session_ctx_t *sess = NULL;
  606. assert(context);
  607. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  608. fprintf(stderr, "Can't connect to data repository\n");
  609. return -1;
  610. }
  611. // Copy running-config to candidate config
  612. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  613. fprintf(stderr, "Can't connect to candidate data store\n");
  614. goto err;
  615. }
  616. if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
  617. fprintf(stderr, "Can't rollback to running-config\n");
  618. goto err;
  619. }
  620. ret = 0;
  621. err:
  622. sr_disconnect(conn);
  623. return ret;
  624. }
  625. int srp_show_xml(kcontext_t *context)
  626. {
  627. int ret = -1;
  628. faux_argv_t *args = NULL;
  629. pline_t *pline = NULL;
  630. sr_conn_ctx_t *conn = NULL;
  631. sr_session_ctx_t *sess = NULL;
  632. pexpr_t *expr = NULL;
  633. faux_argv_t *cur_path = NULL;
  634. sr_data_t *data = NULL;
  635. struct ly_out *out = NULL;
  636. struct lyd_node *child = NULL;
  637. assert(context);
  638. if (sr_connect(SR_CONN_DEFAULT, &conn))
  639. return -1;
  640. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  641. sr_disconnect(conn);
  642. return -1;
  643. }
  644. cur_path = (faux_argv_t *)srp_udata_path(context);
  645. args = param2argv(cur_path, kcontext_pargv(context), "path");
  646. pline = pline_parse(sess, args, srp_udata_flags(context));
  647. faux_argv_free(args);
  648. if (pline->invalid) {
  649. fprintf(stderr, "Invalid 'show' request\n");
  650. goto err;
  651. }
  652. if (faux_list_len(pline->exprs) > 1) {
  653. fprintf(stderr, "Can't process more than one object\n");
  654. goto err;
  655. }
  656. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  657. if (!(expr->pat & PT_EDIT)) {
  658. fprintf(stderr, "Illegal expression for 'show' operation\n");
  659. goto err;
  660. }
  661. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  662. fprintf(stderr, "Can't get specified subtree\n");
  663. goto err;
  664. }
  665. ly_out_new_file(stdout, &out);
  666. lyd_print_tree(out, data->tree, LYD_XML, 0);
  667. ly_out_free(out, NULL, 0);
  668. // child = lyd_child(data->tree);
  669. // if (child) {
  670. // ly_out_new_file(stdout, &out);
  671. // lyd_print_all(out, child, LYD_XML, 0);
  672. // }
  673. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  674. if (meta)
  675. printf("META\n");
  676. sr_release_data(data);
  677. ret = 0;
  678. err:
  679. pline_free(pline);
  680. sr_disconnect(conn);
  681. return ret;
  682. }
  683. static int show(kcontext_t *context, sr_datastore_t ds)
  684. {
  685. int ret = -1;
  686. faux_argv_t *args = NULL;
  687. pline_t *pline = NULL;
  688. sr_conn_ctx_t *conn = NULL;
  689. sr_session_ctx_t *sess = NULL;
  690. pexpr_t *expr = NULL;
  691. faux_argv_t *cur_path = NULL;
  692. char *xpath = NULL;
  693. assert(context);
  694. if (sr_connect(SR_CONN_DEFAULT, &conn))
  695. return -1;
  696. if (sr_session_start(conn, ds, &sess)) {
  697. sr_disconnect(conn);
  698. return -1;
  699. }
  700. cur_path = (faux_argv_t *)srp_udata_path(context);
  701. if (kpargv_find(kcontext_pargv(context), "path") || cur_path) {
  702. args = param2argv(cur_path, kcontext_pargv(context), "path");
  703. pline = pline_parse(sess, args, srp_udata_flags(context));
  704. faux_argv_free(args);
  705. if (pline->invalid) {
  706. fprintf(stderr, "Invalid 'show' request\n");
  707. goto err;
  708. }
  709. if (faux_list_len(pline->exprs) > 1) {
  710. fprintf(stderr, "Can't process more than one object\n");
  711. goto err;
  712. }
  713. if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
  714. fprintf(stderr, "Can't get expression\n");
  715. goto err;
  716. }
  717. if (!(expr->pat & PT_EDIT)) {
  718. fprintf(stderr, "Illegal expression for 'show' operation\n");
  719. goto err;
  720. }
  721. if (!expr->xpath) {
  722. fprintf(stderr, "Empty expression for 'show' operation\n");
  723. goto err;
  724. }
  725. xpath = expr->xpath;
  726. }
  727. show_xpath(sess, xpath, srp_udata_flags(context));
  728. ret = 0;
  729. err:
  730. pline_free(pline);
  731. sr_disconnect(conn);
  732. return ret;
  733. }
  734. int srp_show(kcontext_t *context)
  735. {
  736. return show(context, SRP_REPO_EDIT);
  737. }
  738. int srp_show_running(kcontext_t *context)
  739. {
  740. sr_conn_ctx_t *conn = NULL;
  741. sr_session_ctx_t *sess = NULL;
  742. if (sr_connect(SR_CONN_DEFAULT, &conn))
  743. return -1;
  744. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  745. sr_disconnect(conn);
  746. return -1;
  747. }
  748. show_xpath(sess, NULL, srp_udata_flags(context));
  749. sr_disconnect(conn);
  750. return 0;
  751. }
  752. int srp_deactivate(kcontext_t *context)
  753. {
  754. int ret = -1;
  755. faux_argv_t *args = NULL;
  756. pline_t *pline = NULL;
  757. sr_conn_ctx_t *conn = NULL;
  758. sr_session_ctx_t *sess = NULL;
  759. pexpr_t *expr = NULL;
  760. faux_argv_t *cur_path = NULL;
  761. sr_data_t *data = NULL;
  762. const struct ly_ctx *ctx = NULL;
  763. assert(context);
  764. if (sr_connect(SR_CONN_DEFAULT, &conn))
  765. return -1;
  766. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  767. sr_disconnect(conn);
  768. return -1;
  769. }
  770. cur_path = (faux_argv_t *)srp_udata_path(context);
  771. args = param2argv(cur_path, kcontext_pargv(context), "path");
  772. pline = pline_parse(sess, args, srp_udata_flags(context));
  773. faux_argv_free(args);
  774. if (pline->invalid) {
  775. fprintf(stderr, "Invalid 'show' request\n");
  776. goto err;
  777. }
  778. if (faux_list_len(pline->exprs) > 1) {
  779. fprintf(stderr, "Can't process more than one object\n");
  780. goto err;
  781. }
  782. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  783. if (!(expr->pat & PT_DEL)) {
  784. fprintf(stderr, "Illegal expression for 'show' operation\n");
  785. goto err;
  786. }
  787. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  788. fprintf(stderr, "Can't get specified subtree\n");
  789. goto err;
  790. }
  791. if (lyd_new_meta(LYD_CTX(data->tree), data->tree, NULL,
  792. "junos-configuration-metadata:active", "false", 0, NULL)) {
  793. fprintf(stderr, "Can't deactivate\n");
  794. goto err;
  795. }
  796. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  797. if (meta)
  798. printf("META\n");
  799. if (sr_has_changes(sess))
  800. fprintf(stderr, "Has changes\n");
  801. if (sr_apply_changes(sess, 0)) {
  802. fprintf(stderr, "Can't apply changes\n");
  803. }
  804. sr_release_data(data);
  805. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  806. fprintf(stderr, "Can't get specified subtree\n");
  807. goto err;
  808. }
  809. struct ly_out *out = NULL;
  810. ly_out_new_file(stdout, &out);
  811. lyd_print_tree(out, data->tree, LYD_XML, 0);
  812. ly_out_free(out, NULL, 0);
  813. sr_release_data(data);
  814. ret = 0;
  815. err:
  816. pline_free(pline);
  817. sr_disconnect(conn);
  818. return ret;
  819. }