syms.c 22 KB

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