syms.c 21 KB

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