syms.c 26 KB

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