syms.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 <sysrepo/values.h>
  20. #include "pline.h"
  21. #include "private.h"
  22. #define ERRORMSG "Error: "
  23. static void _log(LY_LOG_LEVEL level, const char *msg, const char *path)
  24. {
  25. fprintf(stderr, ERRORMSG "%s %s\n", msg, path ? path : "");
  26. level = level;
  27. }
  28. int srp_set_log_func(void)
  29. {
  30. ly_set_log_clb(_log, 1);
  31. return 0;
  32. }
  33. static faux_argv_t *param2argv(const faux_argv_t *cur_path,
  34. const kpargv_t *pargv, const char *entry_name)
  35. {
  36. faux_list_node_t *iter = NULL;
  37. faux_list_t *pargs = NULL;
  38. faux_argv_t *args = NULL;
  39. kparg_t *parg = NULL;
  40. assert(pargv);
  41. if (!pargv)
  42. return NULL;
  43. pargs = kpargv_find_multi(pargv, entry_name);
  44. if (cur_path)
  45. args = faux_argv_dup(cur_path);
  46. else
  47. args = faux_argv_new();
  48. iter = faux_list_head(pargs);
  49. while ((parg = (kparg_t *)faux_list_each(&iter))) {
  50. faux_argv_add(args, kparg_value(parg));
  51. }
  52. faux_list_free(pargs);
  53. return args;
  54. }
  55. // Candidate from pargv contains possible begin of current word (that must be
  56. // completed). kpargv's list don't contain candidate but only already parsed
  57. // words.
  58. static int srp_compl_or_help(kcontext_t *context, bool_t help)
  59. {
  60. faux_argv_t *args = NULL;
  61. pline_t *pline = NULL;
  62. sr_conn_ctx_t *conn = NULL;
  63. sr_session_ctx_t *sess = NULL;
  64. const char *entry_name = NULL;
  65. faux_argv_t *cur_path = NULL;
  66. assert(context);
  67. if (sr_connect(SR_CONN_DEFAULT, &conn))
  68. return -1;
  69. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  70. sr_disconnect(conn);
  71. return -1;
  72. }
  73. cur_path = (faux_argv_t *)srp_udata_path(context);
  74. entry_name = kentry_name(kcontext_candidate_entry(context));
  75. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  76. pline = pline_parse(sess, args, srp_udata_opts(context));
  77. faux_argv_free(args);
  78. pline_print_completions(pline, help);
  79. pline_free(pline);
  80. sr_disconnect(conn);
  81. return 0;
  82. }
  83. int srp_compl(kcontext_t *context)
  84. {
  85. return srp_compl_or_help(context, BOOL_FALSE);
  86. }
  87. int srp_help(kcontext_t *context)
  88. {
  89. return srp_compl_or_help(context, BOOL_TRUE);
  90. }
  91. int srp_prompt_edit_path(kcontext_t *context)
  92. {
  93. faux_argv_t *cur_path = NULL;
  94. char *path = NULL;
  95. assert(context);
  96. cur_path = (faux_argv_t *)srp_udata_path(context);
  97. if (cur_path)
  98. path = faux_argv_line(cur_path);
  99. printf("[edit%s%s]\n", path ? " " : "", path ? path : "");
  100. faux_str_free(path);
  101. return 0;
  102. }
  103. static int srp_check_type(kcontext_t *context,
  104. pt_e not_accepted_nodes,
  105. size_t max_expr_num)
  106. {
  107. int ret = -1;
  108. faux_argv_t *args = NULL;
  109. pline_t *pline = NULL;
  110. sr_conn_ctx_t *conn = NULL;
  111. sr_session_ctx_t *sess = NULL;
  112. const char *entry_name = NULL;
  113. const char *value = NULL;
  114. pexpr_t *expr = NULL;
  115. size_t expr_num = 0;
  116. faux_argv_t *cur_path = NULL;
  117. assert(context);
  118. if (sr_connect(SR_CONN_DEFAULT, &conn))
  119. return -1;
  120. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  121. sr_disconnect(conn);
  122. return -1;
  123. }
  124. cur_path = (faux_argv_t *)srp_udata_path(context);
  125. entry_name = kentry_name(kcontext_candidate_entry(context));
  126. value = kcontext_candidate_value(context);
  127. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  128. if (value)
  129. faux_argv_add(args, value);
  130. pline = pline_parse(sess, args, srp_udata_opts(context));
  131. faux_argv_free(args);
  132. if (pline->invalid)
  133. goto err;
  134. expr_num = faux_list_len(pline->exprs);
  135. if (expr_num < 1)
  136. goto err;
  137. if ((max_expr_num > 0) && // '0' means unlimited
  138. (expr_num > max_expr_num))
  139. goto err;
  140. expr = pline_current_expr(pline);
  141. if (expr->pat & not_accepted_nodes)
  142. goto err;
  143. ret = 0;
  144. err:
  145. pline_free(pline);
  146. sr_disconnect(conn);
  147. return ret;
  148. }
  149. int srp_PLINE_SET(kcontext_t *context)
  150. {
  151. return srp_check_type(context, PT_NOT_SET, 0);
  152. }
  153. int srp_PLINE_DEL(kcontext_t *context)
  154. {
  155. return srp_check_type(context, PT_NOT_DEL, 1);
  156. }
  157. int srp_PLINE_EDIT(kcontext_t *context)
  158. {
  159. return srp_check_type(context, PT_NOT_EDIT, 1);
  160. }
  161. int srp_PLINE_INSERT_FROM(kcontext_t *context)
  162. {
  163. return srp_check_type(context, PT_NOT_INSERT, 1);
  164. }
  165. static faux_argv_t *assemble_insert_to(sr_session_ctx_t *sess, const kpargv_t *pargv,
  166. faux_argv_t *cur_path, const char *candidate_value, pline_opts_t *opts)
  167. {
  168. faux_argv_t *args = NULL;
  169. faux_argv_t *insert_to = NULL;
  170. pline_t *pline = NULL;
  171. pexpr_t *expr = NULL;
  172. size_t i = 0;
  173. assert(sess);
  174. args = param2argv(cur_path, pargv, "from_path");
  175. pline = pline_parse(sess, args, opts);
  176. expr = pline_current_expr(pline);
  177. for (i = 0; i < (expr->args_num - expr->list_pos); i++) {
  178. faux_argv_node_t *iter = faux_argv_iterr(args);
  179. faux_argv_del(args, iter);
  180. }
  181. insert_to = param2argv(args, pargv, "to_path");
  182. faux_argv_free(args);
  183. if (candidate_value)
  184. faux_argv_add(insert_to, candidate_value);
  185. pline_free(pline);
  186. return insert_to;
  187. }
  188. int srp_PLINE_INSERT_TO(kcontext_t *context)
  189. {
  190. int ret = -1;
  191. faux_argv_t *args = NULL;
  192. pline_t *pline = NULL;
  193. sr_conn_ctx_t *conn = NULL;
  194. sr_session_ctx_t *sess = NULL;
  195. const char *value = NULL;
  196. pexpr_t *expr = NULL;
  197. size_t expr_num = 0;
  198. faux_argv_t *cur_path = NULL;
  199. assert(context);
  200. if (sr_connect(SR_CONN_DEFAULT, &conn))
  201. return -1;
  202. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  203. sr_disconnect(conn);
  204. return -1;
  205. }
  206. cur_path = (faux_argv_t *)srp_udata_path(context);
  207. value = kcontext_candidate_value(context);
  208. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  209. cur_path, value, srp_udata_opts(context));
  210. pline = pline_parse(sess, args, srp_udata_opts(context));
  211. faux_argv_free(args);
  212. if (pline->invalid)
  213. goto err;
  214. expr_num = faux_list_len(pline->exprs);
  215. if (expr_num != 1)
  216. goto err;
  217. expr = pline_current_expr(pline);
  218. if (expr->pat & PT_NOT_INSERT)
  219. goto err;
  220. ret = 0;
  221. err:
  222. pline_free(pline);
  223. sr_disconnect(conn);
  224. return ret;
  225. }
  226. static int srp_compl_or_help_insert_to(kcontext_t *context, bool_t help)
  227. {
  228. faux_argv_t *args = NULL;
  229. pline_t *pline = NULL;
  230. sr_conn_ctx_t *conn = NULL;
  231. sr_session_ctx_t *sess = NULL;
  232. faux_argv_t *cur_path = NULL;
  233. assert(context);
  234. if (sr_connect(SR_CONN_DEFAULT, &conn))
  235. return -1;
  236. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  237. sr_disconnect(conn);
  238. return -1;
  239. }
  240. cur_path = (faux_argv_t *)srp_udata_path(context);
  241. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  242. cur_path, NULL, srp_udata_opts(context));
  243. pline = pline_parse(sess, args, srp_udata_opts(context));
  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. 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. cur_path = (faux_argv_t *)srp_udata_path(context);
  277. args = param2argv(cur_path, kcontext_pargv(context), "path");
  278. pline = pline_parse(sess, args, srp_udata_opts(context));
  279. faux_argv_free(args);
  280. if (pline->invalid) {
  281. fprintf(stderr, ERRORMSG "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, ERRORMSG "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, ERRORMSG "Can't set data.\n");
  296. break;
  297. }
  298. }
  299. if (err_num > 0)
  300. ret = -1;
  301. if (!sr_has_changes(sess))
  302. goto cleanup;
  303. if (err_num > 0) {
  304. sr_discard_changes(sess);
  305. goto cleanup;
  306. }
  307. sr_apply_changes(sess, 0);
  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. faux_argv_t *cur_path = NULL;
  322. assert(context);
  323. if (sr_connect(SR_CONN_DEFAULT, &conn))
  324. return -1;
  325. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  326. sr_disconnect(conn);
  327. return -1;
  328. }
  329. cur_path = (faux_argv_t *)srp_udata_path(context);
  330. args = param2argv(cur_path, kcontext_pargv(context), "path");
  331. pline = pline_parse(sess, args, srp_udata_opts(context));
  332. faux_argv_free(args);
  333. if (pline->invalid) {
  334. fprintf(stderr, ERRORMSG "Invalid 'del' request.\n");
  335. goto err;
  336. }
  337. if (faux_list_len(pline->exprs) > 1) {
  338. fprintf(stderr, ERRORMSG "Can't delete more than one object.\n");
  339. goto err;
  340. }
  341. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  342. if (!(expr->pat & PT_DEL)) {
  343. fprintf(stderr, ERRORMSG "Illegal expression for 'del' operation.\n");
  344. goto err;
  345. }
  346. if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
  347. fprintf(stderr, ERRORMSG "Can't delete data.\n");
  348. goto err;
  349. }
  350. sr_apply_changes(sess, 0);
  351. ret = 0;
  352. err:
  353. pline_free(pline);
  354. sr_disconnect(conn);
  355. return ret;
  356. }
  357. int srp_edit(kcontext_t *context)
  358. {
  359. int ret = -1;
  360. faux_argv_t *args = NULL;
  361. pline_t *pline = NULL;
  362. sr_conn_ctx_t *conn = NULL;
  363. sr_session_ctx_t *sess = NULL;
  364. pexpr_t *expr = NULL;
  365. faux_argv_t *cur_path = NULL;
  366. assert(context);
  367. if (sr_connect(SR_CONN_DEFAULT, &conn))
  368. return -1;
  369. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  370. sr_disconnect(conn);
  371. return -1;
  372. }
  373. cur_path = (faux_argv_t *)srp_udata_path(context);
  374. args = param2argv(cur_path, kcontext_pargv(context), "path");
  375. pline = pline_parse(sess, args, srp_udata_opts(context));
  376. if (pline->invalid) {
  377. fprintf(stderr, ERRORMSG "Invalid 'edit' request.\n");
  378. goto err;
  379. }
  380. if (faux_list_len(pline->exprs) > 1) {
  381. fprintf(stderr, ERRORMSG "Can't process more than one object.\n");
  382. goto err;
  383. }
  384. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  385. if (!(expr->pat & PT_EDIT)) {
  386. fprintf(stderr, ERRORMSG "Illegal expression for 'edit' operation.\n");
  387. goto err;
  388. }
  389. if (sr_set_item_str(sess, expr->xpath, NULL, NULL, 0) != SR_ERR_OK) {
  390. fprintf(stderr, ERRORMSG "Can't set editing data.\n");
  391. goto err;
  392. }
  393. sr_apply_changes(sess, 0);
  394. // Set new current path
  395. srp_udata_set_path(context, args);
  396. ret = 0;
  397. err:
  398. if (ret < 0)
  399. faux_argv_free(args);
  400. pline_free(pline);
  401. sr_disconnect(conn);
  402. return ret;
  403. }
  404. int srp_top(kcontext_t *context)
  405. {
  406. assert(context);
  407. srp_udata_set_path(context, NULL);
  408. return 0;
  409. }
  410. int srp_up(kcontext_t *context)
  411. {
  412. sr_conn_ctx_t *conn = NULL;
  413. sr_session_ctx_t *sess = NULL;
  414. faux_argv_t *cur_path = NULL;
  415. faux_argv_node_t *iter = NULL;
  416. assert(context);
  417. cur_path = (faux_argv_t *)srp_udata_path(context);
  418. if (!cur_path)
  419. return -1; // It's top level and can't level up
  420. if (sr_connect(SR_CONN_DEFAULT, &conn))
  421. return -1;
  422. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  423. sr_disconnect(conn);
  424. return -1;
  425. }
  426. // Remove last arguments one by one and wait for legal edit-like pline
  427. while (faux_argv_len(cur_path) > 0) {
  428. pline_t *pline = NULL;
  429. pexpr_t *expr = NULL;
  430. size_t len = 0;
  431. iter = faux_argv_iterr(cur_path);
  432. faux_argv_del(cur_path, iter);
  433. pline = pline_parse(sess, cur_path, srp_udata_opts(context));
  434. if (pline->invalid) {
  435. pline_free(pline);
  436. continue;
  437. }
  438. len = faux_list_len(pline->exprs);
  439. if (len != 1) {
  440. pline_free(pline);
  441. continue;
  442. }
  443. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  444. if (!(expr->pat & PT_EDIT)) {
  445. pline_free(pline);
  446. continue;
  447. }
  448. // Here new path is ok
  449. pline_free(pline);
  450. break;
  451. }
  452. // Don't store empty path
  453. if (faux_argv_len(cur_path) == 0)
  454. srp_udata_set_path(context, NULL);
  455. sr_disconnect(conn);
  456. return 0;
  457. }
  458. int srp_insert(kcontext_t *context)
  459. {
  460. int ret = -1;
  461. pline_t *pline = NULL;
  462. pline_t *pline_to = NULL;
  463. sr_conn_ctx_t *conn = NULL;
  464. sr_session_ctx_t *sess = NULL;
  465. pexpr_t *expr = NULL;
  466. pexpr_t *expr_to = NULL;
  467. faux_argv_t *cur_path = NULL;
  468. faux_argv_t *insert_from = NULL;
  469. faux_argv_t *insert_to = NULL;
  470. sr_move_position_t position = SR_MOVE_LAST;
  471. kpargv_t *pargv = NULL;
  472. const char *list_keys = NULL;
  473. const char *leaflist_value = NULL;
  474. assert(context);
  475. if (sr_connect(SR_CONN_DEFAULT, &conn))
  476. return -1;
  477. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  478. sr_disconnect(conn);
  479. return -1;
  480. }
  481. cur_path = (faux_argv_t *)srp_udata_path(context);
  482. pargv = kcontext_pargv(context);
  483. // 'from' argument
  484. insert_from = param2argv(cur_path, pargv, "from_path");
  485. pline = pline_parse(sess, insert_from, srp_udata_opts(context));
  486. faux_argv_free(insert_from);
  487. if (pline->invalid) {
  488. fprintf(stderr, ERRORMSG "Invalid 'from' expression.\n");
  489. goto err;
  490. }
  491. if (faux_list_len(pline->exprs) > 1) {
  492. fprintf(stderr, ERRORMSG "Can't process more than one object.\n");
  493. goto err;
  494. }
  495. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  496. if (!(expr->pat & PT_INSERT)) {
  497. fprintf(stderr, ERRORMSG "Illegal 'from' expression for 'insert' operation.\n");
  498. goto err;
  499. }
  500. // Position
  501. if (kpargv_find(pargv, "first"))
  502. position = SR_MOVE_FIRST;
  503. else if (kpargv_find(pargv, "last"))
  504. position = SR_MOVE_LAST;
  505. else if (kpargv_find(pargv, "before"))
  506. position = SR_MOVE_BEFORE;
  507. else if (kpargv_find(pargv, "after"))
  508. position = SR_MOVE_AFTER;
  509. else {
  510. fprintf(stderr, ERRORMSG "Illegal 'position' argument.\n");
  511. goto err;
  512. }
  513. // 'to' argument
  514. if ((SR_MOVE_BEFORE == position) || (SR_MOVE_AFTER == position)) {
  515. insert_to = assemble_insert_to(sess, pargv, cur_path,
  516. NULL, srp_udata_opts(context));
  517. pline_to = pline_parse(sess, insert_to, srp_udata_opts(context));
  518. faux_argv_free(insert_to);
  519. if (pline_to->invalid) {
  520. fprintf(stderr, ERRORMSG "Invalid 'to' expression.\n");
  521. goto err;
  522. }
  523. if (faux_list_len(pline_to->exprs) > 1) {
  524. fprintf(stderr, ERRORMSG "Can't process more than one object.\n");
  525. goto err;
  526. }
  527. expr_to = (pexpr_t *)faux_list_data(faux_list_head(pline_to->exprs));
  528. if (!(expr_to->pat & PT_INSERT)) {
  529. fprintf(stderr, ERRORMSG "Illegal 'to' expression for 'insert' operation.\n");
  530. goto err;
  531. }
  532. if (PAT_LIST_KEY == expr_to->pat)
  533. list_keys = expr_to->last_keys;
  534. else // PATH_LEAFLIST_VALUE
  535. leaflist_value = expr_to->last_keys;
  536. }
  537. if (sr_move_item(sess, expr->xpath, position,
  538. list_keys, leaflist_value, NULL, 0) != SR_ERR_OK) {
  539. fprintf(stderr, ERRORMSG "Can't move element.\n");
  540. goto err;
  541. }
  542. sr_apply_changes(sess, 0);
  543. ret = 0;
  544. err:
  545. pline_free(pline);
  546. pline_free(pline_to);
  547. sr_disconnect(conn);
  548. return ret;
  549. }
  550. int srp_verify(kcontext_t *context)
  551. {
  552. int ret = -1;
  553. sr_conn_ctx_t *conn = NULL;
  554. sr_session_ctx_t *sess = NULL;
  555. assert(context);
  556. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  557. fprintf(stderr, ERRORMSG "Can't connect to data repository.\n");
  558. return -1;
  559. }
  560. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  561. fprintf(stderr, ERRORMSG "Can't connect to candidate data store.\n");
  562. goto err;
  563. }
  564. // Validate candidate config
  565. if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
  566. fprintf(stderr, ERRORMSG "Invalid candidate configuration.\n");
  567. goto err;
  568. }
  569. ret = 0;
  570. err:
  571. sr_disconnect(conn);
  572. return ret;
  573. }
  574. int srp_commit(kcontext_t *context)
  575. {
  576. int ret = -1;
  577. sr_conn_ctx_t *conn = NULL;
  578. sr_session_ctx_t *sess = NULL;
  579. assert(context);
  580. if (sr_connect(SR_CONN_DEFAULT, &conn)) {
  581. fprintf(stderr, ERRORMSG "Can't connect to data repository.\n");
  582. return -1;
  583. }
  584. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  585. fprintf(stderr, ERRORMSG "Can't connect to candidate data store.\n");
  586. goto err;
  587. }
  588. // Copy candidate to running-config
  589. if (sr_session_switch_ds(sess, SR_DS_RUNNING)) {
  590. fprintf(stderr, ERRORMSG "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, ERRORMSG "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, ERRORMSG "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, ERRORMSG "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_reset(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, ERRORMSG "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, ERRORMSG "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, ERRORMSG "Can't reset 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. sr_data_t *data = NULL;
  645. struct ly_out *out = 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. cur_path = (faux_argv_t *)srp_udata_path(context);
  654. args = param2argv(cur_path, kcontext_pargv(context), "path");
  655. pline = pline_parse(sess, args, srp_udata_opts(context));
  656. faux_argv_free(args);
  657. if (pline->invalid) {
  658. fprintf(stderr, ERRORMSG "Invalid 'show' request.\n");
  659. goto err;
  660. }
  661. if (faux_list_len(pline->exprs) > 1) {
  662. fprintf(stderr, ERRORMSG "Can't process more than one object.\n");
  663. goto err;
  664. }
  665. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  666. if (!(expr->pat & PT_EDIT)) {
  667. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation.\n");
  668. goto err;
  669. }
  670. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  671. fprintf(stderr, ERRORMSG "Can't get specified subtree.\n");
  672. goto err;
  673. }
  674. ly_out_new_file(stdout, &out);
  675. lyd_print_tree(out, data->tree, LYD_XML, 0);
  676. ly_out_free(out, NULL, 0);
  677. // child = lyd_child(data->tree);
  678. // if (child) {
  679. // ly_out_new_file(stdout, &out);
  680. // lyd_print_all(out, child, LYD_XML, 0);
  681. // }
  682. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  683. if (meta)
  684. printf("META\n");
  685. sr_release_data(data);
  686. ret = 0;
  687. err:
  688. pline_free(pline);
  689. sr_disconnect(conn);
  690. return ret;
  691. }
  692. static int show(kcontext_t *context, sr_datastore_t ds)
  693. {
  694. int ret = -1;
  695. faux_argv_t *args = NULL;
  696. pline_t *pline = NULL;
  697. sr_conn_ctx_t *conn = NULL;
  698. sr_session_ctx_t *sess = NULL;
  699. pexpr_t *expr = NULL;
  700. faux_argv_t *cur_path = NULL;
  701. char *xpath = NULL;
  702. assert(context);
  703. if (sr_connect(SR_CONN_DEFAULT, &conn))
  704. return -1;
  705. if (sr_session_start(conn, ds, &sess)) {
  706. sr_disconnect(conn);
  707. return -1;
  708. }
  709. cur_path = (faux_argv_t *)srp_udata_path(context);
  710. if (kpargv_find(kcontext_pargv(context), "path") || cur_path) {
  711. args = param2argv(cur_path, kcontext_pargv(context), "path");
  712. pline = pline_parse(sess, args, srp_udata_opts(context));
  713. faux_argv_free(args);
  714. if (pline->invalid) {
  715. fprintf(stderr, ERRORMSG "Invalid 'show' request.\n");
  716. goto err;
  717. }
  718. if (faux_list_len(pline->exprs) > 1) {
  719. fprintf(stderr, ERRORMSG "Can't process more than one object.\n");
  720. goto err;
  721. }
  722. if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
  723. fprintf(stderr, ERRORMSG "Can't get expression.\n");
  724. goto err;
  725. }
  726. if (!(expr->pat & PT_EDIT)) {
  727. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation.\n");
  728. goto err;
  729. }
  730. if (!expr->xpath) {
  731. fprintf(stderr, ERRORMSG "Empty expression for 'show' operation.\n");
  732. goto err;
  733. }
  734. xpath = expr->xpath;
  735. }
  736. show_xpath(sess, xpath, srp_udata_opts(context));
  737. ret = 0;
  738. err:
  739. pline_free(pline);
  740. sr_disconnect(conn);
  741. return ret;
  742. }
  743. int srp_show(kcontext_t *context)
  744. {
  745. return show(context, SRP_REPO_EDIT);
  746. }
  747. int srp_show_running(kcontext_t *context)
  748. {
  749. sr_conn_ctx_t *conn = NULL;
  750. sr_session_ctx_t *sess = NULL;
  751. if (sr_connect(SR_CONN_DEFAULT, &conn))
  752. return -1;
  753. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  754. sr_disconnect(conn);
  755. return -1;
  756. }
  757. show_xpath(sess, NULL, srp_udata_opts(context));
  758. sr_disconnect(conn);
  759. return 0;
  760. }
  761. int srp_deactivate(kcontext_t *context)
  762. {
  763. int ret = -1;
  764. faux_argv_t *args = NULL;
  765. pline_t *pline = NULL;
  766. sr_conn_ctx_t *conn = NULL;
  767. sr_session_ctx_t *sess = NULL;
  768. pexpr_t *expr = NULL;
  769. faux_argv_t *cur_path = NULL;
  770. sr_data_t *data = NULL;
  771. assert(context);
  772. if (sr_connect(SR_CONN_DEFAULT, &conn))
  773. return -1;
  774. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  775. sr_disconnect(conn);
  776. return -1;
  777. }
  778. cur_path = (faux_argv_t *)srp_udata_path(context);
  779. args = param2argv(cur_path, kcontext_pargv(context), "path");
  780. pline = pline_parse(sess, args, srp_udata_opts(context));
  781. faux_argv_free(args);
  782. if (pline->invalid) {
  783. fprintf(stderr, ERRORMSG "Invalid 'show' request.\n");
  784. goto err;
  785. }
  786. if (faux_list_len(pline->exprs) > 1) {
  787. fprintf(stderr, ERRORMSG "Can't process more than one object.\n");
  788. goto err;
  789. }
  790. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  791. if (!(expr->pat & PT_DEL)) {
  792. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation.\n");
  793. goto err;
  794. }
  795. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  796. fprintf(stderr, ERRORMSG "Can't get specified subtree.\n");
  797. goto err;
  798. }
  799. if (lyd_new_meta(LYD_CTX(data->tree), data->tree, NULL,
  800. "junos-configuration-metadata:active", "false", 0, NULL)) {
  801. fprintf(stderr, ERRORMSG "Can't deactivate.\n");
  802. goto err;
  803. }
  804. struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
  805. if (meta)
  806. printf("META\n");
  807. if (sr_has_changes(sess))
  808. fprintf(stderr, ERRORMSG "Has changes.\n");
  809. if (sr_apply_changes(sess, 0)) {
  810. fprintf(stderr, ERRORMSG "Can't apply changes.\n");
  811. }
  812. sr_release_data(data);
  813. if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
  814. fprintf(stderr, ERRORMSG "Can't get specified subtree.\n");
  815. goto err;
  816. }
  817. struct ly_out *out = NULL;
  818. ly_out_new_file(stdout, &out);
  819. lyd_print_tree(out, data->tree, LYD_XML, 0);
  820. ly_out_free(out, NULL, 0);
  821. sr_release_data(data);
  822. ret = 0;
  823. err:
  824. pline_free(pline);
  825. sr_disconnect(conn);
  826. return ret;
  827. }
  828. int srp_diff(kcontext_t *context)
  829. {
  830. int ret = -1;
  831. pline_t *pline = NULL;
  832. sr_conn_ctx_t *conn = NULL;
  833. sr_session_ctx_t *sess1 = NULL;
  834. sr_session_ctx_t *sess2 = NULL;
  835. sr_data_t *data1 = NULL;
  836. sr_data_t *data2 = NULL;
  837. faux_argv_t *cur_path = NULL;
  838. const char *xpath = NULL;
  839. struct lyd_node *diff = NULL;
  840. assert(context);
  841. if (sr_connect(SR_CONN_DEFAULT, &conn))
  842. return -1;
  843. if (sr_session_start(conn, SR_DS_RUNNING, &sess1)) {
  844. sr_disconnect(conn);
  845. return -1;
  846. }
  847. if (sr_session_start(conn, SRP_REPO_EDIT, &sess2)) {
  848. sr_disconnect(conn);
  849. return -1;
  850. }
  851. cur_path = (faux_argv_t *)srp_udata_path(context);
  852. if (kpargv_find(kcontext_pargv(context), "path") || cur_path) {
  853. faux_argv_t *args = NULL;
  854. pexpr_t *expr = NULL;
  855. args = param2argv(cur_path, kcontext_pargv(context), "path");
  856. pline = pline_parse(sess2, args, srp_udata_opts(context));
  857. faux_argv_free(args);
  858. if (pline->invalid) {
  859. fprintf(stderr, ERRORMSG "Invalid 'show' request.\n");
  860. goto err;
  861. }
  862. if (faux_list_len(pline->exprs) > 1) {
  863. fprintf(stderr, ERRORMSG "Can't process more than one object.\n");
  864. goto err;
  865. }
  866. if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
  867. fprintf(stderr, ERRORMSG "Can't get expression.\n");
  868. goto err;
  869. }
  870. if (!(expr->pat & PT_EDIT)) {
  871. fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation.\n");
  872. goto err;
  873. }
  874. if (!expr->xpath) {
  875. fprintf(stderr, ERRORMSG "Empty expression for 'show' operation.\n");
  876. goto err;
  877. }
  878. xpath = expr->xpath;
  879. }
  880. if (!xpath)
  881. xpath = "/*";
  882. if (sr_get_data(sess1, xpath, 0, 0, 0, &data1) != SR_ERR_OK) {
  883. fprintf(stderr, ERRORMSG "Can't get specified subtree.\n");
  884. goto err;
  885. }
  886. if (sr_get_data(sess2, xpath, 0, 0, 0, &data2) != SR_ERR_OK) {
  887. fprintf(stderr, ERRORMSG "Can't get specified subtree.\n");
  888. goto err;
  889. }
  890. if (lyd_diff_siblings(data1->tree, data2->tree, 0, &diff) != LY_SUCCESS) {
  891. fprintf(stderr, ERRORMSG "Can't generate diff.\n");
  892. goto err;
  893. }
  894. show_subtree(diff, 0, DIFF_OP_NONE, srp_udata_opts(context));
  895. lyd_free_siblings(diff);
  896. ret = 0;
  897. err:
  898. if (data1)
  899. sr_release_data(data1);
  900. if (data2)
  901. sr_release_data(data2);
  902. pline_free(pline);
  903. sr_disconnect(conn);
  904. return ret;
  905. }
  906. static int srp_compl_xpath(kcontext_t *context, const sr_datastore_t datastore)
  907. {
  908. sr_conn_ctx_t *conn = NULL;
  909. sr_session_ctx_t *sess = NULL;
  910. sr_val_t *vals = NULL;
  911. size_t val_num = 0;
  912. size_t i = 0;
  913. const char *script = NULL;
  914. assert(context);
  915. script = kcontext_script(context);
  916. if (faux_str_is_empty(script))
  917. return -1;
  918. if (sr_connect(SR_CONN_DEFAULT, &conn))
  919. return -1;
  920. if (sr_session_start(conn, datastore, &sess)) {
  921. sr_disconnect(conn);
  922. return -1;
  923. }
  924. sr_get_items(sess, script, 0, 0, &vals, &val_num);
  925. for (i = 0; i < val_num; i++) {
  926. char *tmp = sr_val_to_str(&vals[i]);
  927. if (!tmp)
  928. continue;
  929. printf("%s\n", tmp);
  930. free(tmp);
  931. }
  932. sr_free_values(vals, val_num);
  933. sr_disconnect(conn);
  934. return 0;
  935. }
  936. int srp_compl_xpath_running(kcontext_t *context)
  937. {
  938. return srp_compl_xpath(context, SR_DS_RUNNING);
  939. }
  940. int srp_compl_xpath_candidate(kcontext_t *context)
  941. {
  942. return srp_compl_xpath(context, SR_DS_CANDIDATE);
  943. }