syms.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. static int show(kcontext_t *context, sr_datastore_t ds)
  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, ds, &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_show(kcontext_t *context)
  749. {
  750. return show(context, SRP_REPO_EDIT);
  751. }
  752. int srp_show_running(kcontext_t *context)
  753. {
  754. sr_conn_ctx_t *conn = NULL;
  755. sr_session_ctx_t *sess = NULL;
  756. if (sr_connect(SR_CONN_DEFAULT, &conn))
  757. return -1;
  758. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  759. sr_disconnect(conn);
  760. return -1;
  761. }
  762. show_xpath(sess, NULL, SRP_DEFAULT_PARSE_OPTS);
  763. sr_disconnect(conn);
  764. return 0;
  765. }
  766. int srp_deactivate(kcontext_t *context)
  767. {
  768. int ret = -1;
  769. faux_argv_t *args = NULL;
  770. pline_t *pline = NULL;
  771. sr_conn_ctx_t *conn = NULL;
  772. sr_session_ctx_t *sess = NULL;
  773. pexpr_t *expr = NULL;
  774. faux_argv_t *cur_path = NULL;
  775. kplugin_t *plugin = NULL;
  776. sr_data_t *data = NULL;
  777. const struct ly_ctx *ctx = NULL;
  778. assert(context);
  779. if (sr_connect(SR_CONN_DEFAULT, &conn))
  780. return -1;
  781. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  782. sr_disconnect(conn);
  783. return -1;
  784. }
  785. plugin = kcontext_plugin(context);
  786. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  787. args = param2argv(cur_path, kcontext_pargv(context), "path");
  788. pline = pline_parse(sess, args, SRP_DEFAULT_PARSE_OPTS);
  789. faux_argv_free(args);
  790. if (pline->invalid) {
  791. fprintf(stderr, "Invalid 'show' request\n");
  792. goto err;
  793. }
  794. if (faux_list_len(pline->exprs) > 1) {
  795. fprintf(stderr, "Can't process more than one object\n");
  796. goto err;
  797. }
  798. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  799. if (!(expr->pat & PT_DEL)) {
  800. fprintf(stderr, "Illegal expression for 'show' operation\n");
  801. goto err;
  802. }
  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. if (lyd_new_meta(LYD_CTX(data->tree), data->tree, NULL,
  808. "junos-configuration-metadata:active", "false", 0, NULL)) {
  809. fprintf(stderr, "Can't deactivate\n");
  810. goto err;
  811. }
  812. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  813. if (meta)
  814. printf("META\n");
  815. if (sr_has_changes(sess))
  816. fprintf(stderr, "Has changes\n");
  817. if (sr_apply_changes(sess, 0)) {
  818. fprintf(stderr, "Can't apply changes\n");
  819. }
  820. sr_release_data(data);
  821. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  822. fprintf(stderr, "Can't get specified subtree\n");
  823. goto err;
  824. }
  825. struct ly_out *out = NULL;
  826. ly_out_new_file(stdout, &out);
  827. lyd_print_tree(out, data->tree, LYD_XML, 0);
  828. ly_out_free(out, NULL, 0);
  829. sr_release_data(data);
  830. ret = 0;
  831. err:
  832. pline_free(pline);
  833. sr_disconnect(conn);
  834. return ret;
  835. }