syms.c 22 KB

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