syms.c 22 KB

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