syms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <syslog.h>
  6. #include <faux/faux.h>
  7. #include <faux/str.h>
  8. #include <faux/argv.h>
  9. #include <faux/list.h>
  10. #include <faux/error.h>
  11. #include <klish/khelper.h>
  12. #include <klish/kplugin.h>
  13. #include <klish/kentry.h>
  14. #include <klish/kscheme.h>
  15. #include <klish/kcontext.h>
  16. #include <klish/kpargv.h>
  17. #include <sysrepo.h>
  18. #include <sysrepo/xpath.h>
  19. #include "pline.h"
  20. #include "syms.h"
  21. static faux_argv_t *param2argv(const faux_argv_t *cur_path,
  22. const kpargv_t *pargv, const char *entry_name)
  23. {
  24. faux_list_node_t *iter = NULL;
  25. faux_list_t *pargs = NULL;
  26. faux_argv_t *args = NULL;
  27. kparg_t *parg = NULL;
  28. assert(pargv);
  29. if (!pargv)
  30. return NULL;
  31. pargs = kpargv_find_multi(pargv, entry_name);
  32. if (cur_path)
  33. args = faux_argv_dup(cur_path);
  34. else
  35. args = faux_argv_new();
  36. iter = faux_list_head(pargs);
  37. while ((parg = (kparg_t *)faux_list_each(&iter))) {
  38. faux_argv_add(args, kparg_value(parg));
  39. }
  40. faux_list_free(pargs);
  41. return args;
  42. }
  43. // Candidate from pargv contains possible begin of current word (that must be
  44. // completed). kpargv's list don't contain candidate but only already parsed
  45. // words.
  46. static int srp_compl_or_help(kcontext_t *context, bool_t help)
  47. {
  48. faux_argv_t *args = NULL;
  49. pline_t *pline = NULL;
  50. sr_conn_ctx_t *conn = NULL;
  51. sr_session_ctx_t *sess = NULL;
  52. const char *entry_name = NULL;
  53. faux_argv_t *cur_path = NULL;
  54. kplugin_t *plugin = NULL;
  55. assert(context);
  56. if (sr_connect(SR_CONN_DEFAULT, &conn))
  57. return -1;
  58. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  59. sr_disconnect(conn);
  60. return -1;
  61. }
  62. plugin = kcontext_plugin(context);
  63. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  64. entry_name = kentry_name(kcontext_candidate_entry(context));
  65. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  66. pline = pline_parse(sess, args, 0);
  67. faux_argv_free(args);
  68. pline_print_completions(pline, help);
  69. pline_free(pline);
  70. sr_disconnect(conn);
  71. return 0;
  72. }
  73. int srp_compl(kcontext_t *context)
  74. {
  75. return srp_compl_or_help(context, BOOL_FALSE);
  76. }
  77. int srp_help(kcontext_t *context)
  78. {
  79. return srp_compl_or_help(context, BOOL_TRUE);
  80. }
  81. int srp_prompt_edit_path(kcontext_t *context)
  82. {
  83. faux_argv_t *cur_path = NULL;
  84. kplugin_t *plugin = NULL;
  85. char *path = NULL;
  86. assert(context);
  87. plugin = kcontext_plugin(context);
  88. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  89. if (cur_path)
  90. path = faux_argv_line(cur_path);
  91. printf("[edit%s%s]\n", path ? " " : "", path ? path : "");
  92. faux_str_free(path);
  93. return 0;
  94. }
  95. static int srp_check_type(kcontext_t *context,
  96. pt_e not_accepted_nodes,
  97. size_t max_expr_num)
  98. {
  99. int ret = -1;
  100. faux_argv_t *args = NULL;
  101. pline_t *pline = NULL;
  102. sr_conn_ctx_t *conn = NULL;
  103. sr_session_ctx_t *sess = NULL;
  104. const char *entry_name = NULL;
  105. const char *value = NULL;
  106. pexpr_t *expr = NULL;
  107. size_t expr_num = 0;
  108. faux_argv_t *cur_path = NULL;
  109. kplugin_t *plugin = NULL;
  110. assert(context);
  111. if (sr_connect(SR_CONN_DEFAULT, &conn))
  112. return -1;
  113. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  114. sr_disconnect(conn);
  115. return -1;
  116. }
  117. plugin = kcontext_plugin(context);
  118. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  119. entry_name = kentry_name(kcontext_candidate_entry(context));
  120. value = kcontext_candidate_value(context);
  121. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  122. if (value)
  123. faux_argv_add(args, value);
  124. pline = pline_parse(sess, args, 0);
  125. faux_argv_free(args);
  126. if (pline->invalid)
  127. goto err;
  128. expr_num = faux_list_len(pline->exprs);
  129. if (expr_num < 1)
  130. goto err;
  131. if ((max_expr_num > 0) && // '0' means unlimited
  132. (expr_num > max_expr_num))
  133. goto err;
  134. expr = pline_current_expr(pline);
  135. if (expr->pat & not_accepted_nodes)
  136. goto err;
  137. ret = 0;
  138. err:
  139. pline_free(pline);
  140. sr_disconnect(conn);
  141. return ret;
  142. }
  143. int srp_PLINE_SET(kcontext_t *context)
  144. {
  145. return srp_check_type(context, PT_NOT_SET, 0);
  146. }
  147. int srp_PLINE_DEL(kcontext_t *context)
  148. {
  149. return srp_check_type(context, PT_NOT_DEL, 1);
  150. }
  151. int srp_PLINE_EDIT(kcontext_t *context)
  152. {
  153. return srp_check_type(context, PT_NOT_EDIT, 1);
  154. }
  155. int srp_PLINE_INSERT_FROM(kcontext_t *context)
  156. {
  157. return srp_check_type(context, PT_NOT_INSERT, 1);
  158. }
  159. static faux_argv_t *assemble_insert_to(sr_session_ctx_t *sess, const kpargv_t *pargv,
  160. faux_argv_t *cur_path, const char *candidate_value)
  161. {
  162. faux_argv_t *args = NULL;
  163. faux_argv_t *insert_to = NULL;
  164. pline_t *pline = NULL;
  165. pexpr_t *expr = NULL;
  166. size_t i = 0;
  167. assert(sess);
  168. args = param2argv(cur_path, pargv, "from_path");
  169. pline = pline_parse(sess, args, 0);
  170. expr = pline_current_expr(pline);
  171. for (i = 0; i < (expr->args_num - expr->list_pos); i++) {
  172. faux_argv_node_t *iter = faux_argv_iterr(args);
  173. faux_argv_del(args, iter);
  174. }
  175. insert_to = param2argv(args, pargv, "to_path");
  176. faux_argv_free(args);
  177. if (candidate_value)
  178. faux_argv_add(insert_to, candidate_value);
  179. pline_free(pline);
  180. return insert_to;
  181. }
  182. int srp_PLINE_INSERT_TO(kcontext_t *context)
  183. {
  184. int ret = -1;
  185. faux_argv_t *args = NULL;
  186. pline_t *pline = NULL;
  187. sr_conn_ctx_t *conn = NULL;
  188. sr_session_ctx_t *sess = NULL;
  189. const char *value = NULL;
  190. pexpr_t *expr = NULL;
  191. size_t expr_num = 0;
  192. faux_argv_t *cur_path = NULL;
  193. kplugin_t *plugin = NULL;
  194. assert(context);
  195. if (sr_connect(SR_CONN_DEFAULT, &conn))
  196. return -1;
  197. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  198. sr_disconnect(conn);
  199. return -1;
  200. }
  201. plugin = kcontext_plugin(context);
  202. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  203. value = kcontext_candidate_value(context);
  204. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  205. cur_path, value);
  206. pline = pline_parse(sess, args, 0);
  207. faux_argv_free(args);
  208. if (pline->invalid)
  209. goto err;
  210. expr_num = faux_list_len(pline->exprs);
  211. if (expr_num != 1)
  212. goto err;
  213. expr = pline_current_expr(pline);
  214. if (expr->pat & PT_NOT_INSERT)
  215. goto err;
  216. ret = 0;
  217. err:
  218. pline_free(pline);
  219. sr_disconnect(conn);
  220. return ret;
  221. }
  222. static int srp_compl_or_help_insert_to(kcontext_t *context, bool_t help)
  223. {
  224. faux_argv_t *args = NULL;
  225. pline_t *pline = NULL;
  226. sr_conn_ctx_t *conn = NULL;
  227. sr_session_ctx_t *sess = NULL;
  228. faux_argv_t *cur_path = NULL;
  229. kplugin_t *plugin = NULL;
  230. assert(context);
  231. if (sr_connect(SR_CONN_DEFAULT, &conn))
  232. return -1;
  233. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  234. sr_disconnect(conn);
  235. return -1;
  236. }
  237. plugin = kcontext_plugin(context);
  238. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  239. args = assemble_insert_to(sess, kcontext_parent_pargv(context),
  240. cur_path, NULL);
  241. pline = pline_parse(sess, args, 0);
  242. faux_argv_free(args);
  243. pline_print_completions(pline, help);
  244. pline_free(pline);
  245. sr_disconnect(conn);
  246. return 0;
  247. }
  248. int srp_compl_insert_to(kcontext_t *context)
  249. {
  250. return srp_compl_or_help_insert_to(context, BOOL_FALSE);
  251. }
  252. int srp_help_insert_to(kcontext_t *context)
  253. {
  254. return srp_compl_or_help_insert_to(context, BOOL_TRUE);
  255. }
  256. int srp_set(kcontext_t *context)
  257. {
  258. int ret = 0;
  259. faux_argv_t *args = NULL;
  260. pline_t *pline = NULL;
  261. sr_conn_ctx_t *conn = NULL;
  262. sr_session_ctx_t *sess = NULL;
  263. faux_list_node_t *iter = NULL;
  264. pexpr_t *expr = NULL;
  265. size_t err_num = 0;
  266. faux_argv_t *cur_path = NULL;
  267. kplugin_t *plugin = NULL;
  268. assert(context);
  269. if (sr_connect(SR_CONN_DEFAULT, &conn))
  270. return -1;
  271. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  272. sr_disconnect(conn);
  273. return -1;
  274. }
  275. plugin = kcontext_plugin(context);
  276. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  277. args = param2argv(cur_path, kcontext_pargv(context), "path");
  278. pline = pline_parse(sess, args, 0);
  279. faux_argv_free(args);
  280. if (pline->invalid) {
  281. fprintf(stderr, "Invalid set request\n");
  282. ret = -1;
  283. goto cleanup;
  284. }
  285. iter = faux_list_head(pline->exprs);
  286. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  287. if (!(expr->pat & PT_SET)) {
  288. err_num++;
  289. fprintf(stderr, "Illegal expression for set operation\n");
  290. break;
  291. }
  292. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  293. SR_ERR_OK) {
  294. err_num++;
  295. fprintf(stderr, "Can't set data\n");
  296. break;
  297. }
  298. }
  299. if (sr_has_changes(sess)) {
  300. if (err_num > 0)
  301. sr_discard_changes(sess);
  302. else
  303. sr_apply_changes(sess, 0);
  304. }
  305. if (err_num > 0)
  306. ret = -1;
  307. cleanup:
  308. pline_free(pline);
  309. sr_disconnect(conn);
  310. return ret;
  311. }
  312. int srp_del(kcontext_t *context)
  313. {
  314. int ret = 0;
  315. faux_argv_t *args = NULL;
  316. pline_t *pline = NULL;
  317. sr_conn_ctx_t *conn = NULL;
  318. sr_session_ctx_t *sess = NULL;
  319. pexpr_t *expr = NULL;
  320. size_t err_num = 0;
  321. faux_argv_t *cur_path = NULL;
  322. kplugin_t *plugin = NULL;
  323. assert(context);
  324. if (sr_connect(SR_CONN_DEFAULT, &conn))
  325. return -1;
  326. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  327. sr_disconnect(conn);
  328. return -1;
  329. }
  330. plugin = kcontext_plugin(context);
  331. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  332. args = param2argv(cur_path, kcontext_pargv(context), "path");
  333. pline = pline_parse(sess, args, 0);
  334. faux_argv_free(args);
  335. if (pline->invalid) {
  336. fprintf(stderr, "Invalid 'del' request\n");
  337. ret = -1;
  338. goto cleanup;
  339. }
  340. if (faux_list_len(pline->exprs) > 1) {
  341. fprintf(stderr, "Can't delete more than one object\n");
  342. ret = -1;
  343. goto cleanup;
  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. ret = -1;
  349. goto cleanup;
  350. }
  351. if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
  352. fprintf(stderr, "Can't delete data\n");
  353. ret = -1;
  354. goto cleanup;
  355. }
  356. sr_apply_changes(sess, 0);
  357. cleanup:
  358. pline_free(pline);
  359. sr_disconnect(conn);
  360. return ret;
  361. }
  362. int srp_edit(kcontext_t *context)
  363. {
  364. int ret = 0;
  365. faux_argv_t *args = NULL;
  366. pline_t *pline = NULL;
  367. sr_conn_ctx_t *conn = NULL;
  368. sr_session_ctx_t *sess = NULL;
  369. pexpr_t *expr = NULL;
  370. size_t err_num = 0;
  371. faux_argv_t *cur_path = NULL;
  372. kplugin_t *plugin = NULL;
  373. assert(context);
  374. if (sr_connect(SR_CONN_DEFAULT, &conn))
  375. return -1;
  376. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  377. sr_disconnect(conn);
  378. return -1;
  379. }
  380. plugin = kcontext_plugin(context);
  381. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  382. args = param2argv(cur_path, kcontext_pargv(context), "path");
  383. pline = pline_parse(sess, args, 0);
  384. if (pline->invalid) {
  385. fprintf(stderr, "Invalid 'edit' request\n");
  386. ret = -1;
  387. goto cleanup;
  388. }
  389. if (faux_list_len(pline->exprs) > 1) {
  390. fprintf(stderr, "Can't process more than one object\n");
  391. ret = -1;
  392. goto cleanup;
  393. }
  394. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  395. if (!(expr->pat & PT_EDIT)) {
  396. fprintf(stderr, "Illegal expression for 'edit' operation\n");
  397. ret = -1;
  398. goto cleanup;
  399. }
  400. if (sr_set_item_str(sess, expr->xpath, NULL, NULL, 0) != SR_ERR_OK) {
  401. fprintf(stderr, "Can't set editing data\n");
  402. ret = -1;
  403. goto cleanup;
  404. }
  405. sr_apply_changes(sess, 0);
  406. // Set new current path
  407. faux_argv_free(cur_path);
  408. kplugin_set_udata(plugin, args);
  409. cleanup:
  410. if (ret < 0)
  411. faux_argv_free(args);
  412. pline_free(pline);
  413. sr_disconnect(conn);
  414. return ret;
  415. }
  416. int srp_top(kcontext_t *context)
  417. {
  418. faux_argv_t *cur_path = NULL;
  419. kplugin_t *plugin = NULL;
  420. assert(context);
  421. plugin = kcontext_plugin(context);
  422. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  423. faux_argv_free(cur_path);
  424. kplugin_set_udata(plugin, NULL);
  425. return 0;
  426. }
  427. int srp_up(kcontext_t *context)
  428. {
  429. sr_conn_ctx_t *conn = NULL;
  430. sr_session_ctx_t *sess = NULL;
  431. faux_argv_t *cur_path = NULL;
  432. kplugin_t *plugin = NULL;
  433. faux_argv_node_t *iter = NULL;
  434. assert(context);
  435. plugin = kcontext_plugin(context);
  436. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  437. if (!cur_path)
  438. return -1; // It's top level and can't level up
  439. if (sr_connect(SR_CONN_DEFAULT, &conn))
  440. return -1;
  441. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  442. sr_disconnect(conn);
  443. return -1;
  444. }
  445. // Remove last arguments one by one and wait for legal edit-like pline
  446. while (faux_argv_len(cur_path) > 0) {
  447. pline_t *pline = NULL;
  448. pexpr_t *expr = NULL;
  449. size_t len = 0;
  450. iter = faux_argv_iterr(cur_path);
  451. faux_argv_del(cur_path, iter);
  452. pline = pline_parse(sess, cur_path, 0);
  453. if (pline->invalid) {
  454. pline_free(pline);
  455. continue;
  456. }
  457. len = faux_list_len(pline->exprs);
  458. if (len != 1) {
  459. pline_free(pline);
  460. continue;
  461. }
  462. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  463. if (!(expr->pat & PT_EDIT)) {
  464. pline_free(pline);
  465. continue;
  466. }
  467. // Here new path is ok
  468. pline_free(pline);
  469. break;
  470. }
  471. // Don't store empty path
  472. while (faux_argv_len(cur_path) == 0) {
  473. faux_argv_free(cur_path);
  474. kplugin_set_udata(plugin, NULL);
  475. }
  476. sr_disconnect(conn);
  477. return 0;
  478. }
  479. int srp_insert(kcontext_t *context)
  480. {
  481. int ret = -1;
  482. pline_t *pline = NULL;
  483. pline_t *pline_to = NULL;
  484. sr_conn_ctx_t *conn = NULL;
  485. sr_session_ctx_t *sess = NULL;
  486. faux_list_node_t *iter = NULL;
  487. pexpr_t *expr = NULL;
  488. pexpr_t *expr_to = NULL;
  489. faux_argv_t *cur_path = NULL;
  490. kplugin_t *plugin = NULL;
  491. faux_argv_t *insert_from = NULL;
  492. faux_argv_t *insert_to = NULL;
  493. sr_move_position_t position = SR_MOVE_LAST;
  494. kpargv_t *pargv = NULL;
  495. const char *list_keys = NULL;
  496. const char *leaflist_value = NULL;
  497. assert(context);
  498. if (sr_connect(SR_CONN_DEFAULT, &conn))
  499. return -1;
  500. if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
  501. sr_disconnect(conn);
  502. return -1;
  503. }
  504. plugin = kcontext_plugin(context);
  505. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  506. pargv = kcontext_pargv(context);
  507. // 'from' argument
  508. insert_from = param2argv(cur_path, pargv, "from_path");
  509. pline = pline_parse(sess, insert_from, 0);
  510. faux_argv_free(insert_from);
  511. if (pline->invalid) {
  512. fprintf(stderr, "Invalid 'from' expression\n");
  513. goto err;
  514. }
  515. if (faux_list_len(pline->exprs) > 1) {
  516. fprintf(stderr, "Can't process more than one object\n");
  517. goto err;
  518. }
  519. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  520. if (!(expr->pat & PT_INSERT)) {
  521. fprintf(stderr, "Illegal 'from' expression for 'insert' operation\n");
  522. goto err;
  523. }
  524. // Position
  525. if (kpargv_find(pargv, "first"))
  526. position = SR_MOVE_FIRST;
  527. else if (kpargv_find(pargv, "last"))
  528. position = SR_MOVE_LAST;
  529. else if (kpargv_find(pargv, "before"))
  530. position = SR_MOVE_BEFORE;
  531. else if (kpargv_find(pargv, "after"))
  532. position = SR_MOVE_AFTER;
  533. else {
  534. fprintf(stderr, "Illegal 'position' argument\n");
  535. goto err;
  536. }
  537. // 'to' argument
  538. if ((SR_MOVE_BEFORE == position) || (SR_MOVE_AFTER == position)) {
  539. insert_to = assemble_insert_to(sess, pargv, cur_path, NULL);
  540. pline_to = pline_parse(sess, insert_to, 0);
  541. faux_argv_free(insert_to);
  542. if (pline_to->invalid) {
  543. fprintf(stderr, "Invalid 'to' expression\n");
  544. goto err;
  545. }
  546. if (faux_list_len(pline_to->exprs) > 1) {
  547. fprintf(stderr, "Can't process more than one object\n");
  548. goto err;
  549. }
  550. expr_to = (pexpr_t *)faux_list_data(faux_list_head(pline_to->exprs));
  551. if (!(expr_to->pat & PT_INSERT)) {
  552. fprintf(stderr, "Illegal 'to' expression for 'insert' operation\n");
  553. goto err;
  554. }
  555. if (PAT_LIST_KEY == expr_to->pat)
  556. list_keys = expr_to->last_keys;
  557. else // PATH_LEAFLIST_VALUE
  558. leaflist_value = expr_to->last_keys;
  559. }
  560. if (sr_move_item(sess, expr->xpath, position,
  561. list_keys, leaflist_value, NULL, 0) != SR_ERR_OK) {
  562. fprintf(stderr, "Can't move element\n");
  563. goto err;
  564. }
  565. sr_apply_changes(sess, 0);
  566. ret = 0;
  567. err:
  568. pline_free(pline);
  569. pline_free(pline_to);
  570. sr_disconnect(conn);
  571. return ret;
  572. }