syms.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. static faux_argv_t *param2argv(const faux_argv_t *cur_path,
  21. const kpargv_t *pargv, const char *entry_name)
  22. {
  23. faux_list_node_t *iter = NULL;
  24. faux_list_t *pargs = NULL;
  25. faux_argv_t *args = NULL;
  26. kparg_t *parg = NULL;
  27. assert(pargv);
  28. if (!pargv)
  29. return NULL;
  30. pargs = kpargv_find_multi(pargv, entry_name);
  31. if (cur_path)
  32. args = faux_argv_dup(cur_path);
  33. else
  34. args = faux_argv_new();
  35. iter = faux_list_head(pargs);
  36. while ((parg = (kparg_t *)faux_list_each(&iter))) {
  37. faux_argv_add(args, kparg_value(parg));
  38. }
  39. faux_list_free(pargs);
  40. return args;
  41. }
  42. // Candidate from pargv contains possible begin of current word (that must be
  43. // completed). kpargv's list don't contain candidate but only already parsed
  44. // words.
  45. static int srp_compl_or_help(kcontext_t *context, bool_t help)
  46. {
  47. faux_argv_t *args = NULL;
  48. pline_t *pline = NULL;
  49. sr_conn_ctx_t *conn = NULL;
  50. sr_session_ctx_t *sess = NULL;
  51. const char *entry_name = NULL;
  52. faux_argv_t *cur_path = NULL;
  53. kplugin_t *plugin = NULL;
  54. assert(context);
  55. if (sr_connect(SR_CONN_DEFAULT, &conn))
  56. return -1;
  57. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  58. sr_disconnect(conn);
  59. return -1;
  60. }
  61. plugin = kcontext_plugin(context);
  62. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  63. entry_name = kentry_name(kcontext_candidate_entry(context));
  64. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  65. pline = pline_parse(sess, args, 0);
  66. faux_argv_free(args);
  67. pline_print_completions(pline, help);
  68. pline_free(pline);
  69. sr_disconnect(conn);
  70. return 0;
  71. }
  72. int srp_compl(kcontext_t *context)
  73. {
  74. return srp_compl_or_help(context, BOOL_FALSE);
  75. }
  76. int srp_help(kcontext_t *context)
  77. {
  78. return srp_compl_or_help(context, BOOL_TRUE);
  79. }
  80. int srp_prompt_edit_path(kcontext_t *context)
  81. {
  82. faux_argv_t *cur_path = NULL;
  83. kplugin_t *plugin = NULL;
  84. char *path = NULL;
  85. assert(context);
  86. plugin = kcontext_plugin(context);
  87. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  88. if (cur_path)
  89. path = faux_argv_line(cur_path);
  90. printf("[edit%s%s]\n", path ? " " : "", path ? path : "");
  91. faux_str_free(path);
  92. return 0;
  93. }
  94. static int srp_check_type(kcontext_t *context,
  95. pt_e not_accepted_nodes,
  96. size_t max_expr_num)
  97. {
  98. faux_argv_t *args = NULL;
  99. pline_t *pline = NULL;
  100. sr_conn_ctx_t *conn = NULL;
  101. sr_session_ctx_t *sess = NULL;
  102. const char *entry_name = NULL;
  103. const char *value = NULL;
  104. pexpr_t *expr = NULL;
  105. size_t expr_num = 0;
  106. faux_argv_t *cur_path = NULL;
  107. kplugin_t *plugin = NULL;
  108. assert(context);
  109. if (sr_connect(SR_CONN_DEFAULT, &conn))
  110. return -1;
  111. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  112. sr_disconnect(conn);
  113. return -1;
  114. }
  115. plugin = kcontext_plugin(context);
  116. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  117. entry_name = kentry_name(kcontext_candidate_entry(context));
  118. value = kcontext_candidate_value(context);
  119. args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
  120. if (value)
  121. faux_argv_add(args, value);
  122. pline = pline_parse(sess, args, 0);
  123. faux_argv_free(args);
  124. if (pline->invalid)
  125. return -1;
  126. expr_num = faux_list_len(pline->exprs);
  127. if (expr_num < 1)
  128. return -1;
  129. if ((max_expr_num > 0) && // '0' means unlimited
  130. (expr_num > max_expr_num))
  131. return -1;
  132. expr = pline_current_expr(pline);
  133. if (expr->pat & not_accepted_nodes)
  134. return -1;
  135. pline_free(pline);
  136. sr_disconnect(conn);
  137. return 0;
  138. }
  139. int srp_PLINE_SET(kcontext_t *context)
  140. {
  141. return srp_check_type(context, PT_NOT_SET, 0);
  142. }
  143. int srp_PLINE_DEL(kcontext_t *context)
  144. {
  145. return srp_check_type(context, PT_NOT_DEL, 1);
  146. }
  147. int srp_PLINE_EDIT(kcontext_t *context)
  148. {
  149. return srp_check_type(context, PT_NOT_EDIT, 1);
  150. }
  151. int srp_set(kcontext_t *context)
  152. {
  153. int ret = 0;
  154. faux_argv_t *args = NULL;
  155. pline_t *pline = NULL;
  156. sr_conn_ctx_t *conn = NULL;
  157. sr_session_ctx_t *sess = NULL;
  158. faux_list_node_t *iter = NULL;
  159. pexpr_t *expr = NULL;
  160. size_t err_num = 0;
  161. faux_argv_t *cur_path = NULL;
  162. kplugin_t *plugin = NULL;
  163. assert(context);
  164. if (sr_connect(SR_CONN_DEFAULT, &conn))
  165. return -1;
  166. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  167. sr_disconnect(conn);
  168. return -1;
  169. }
  170. plugin = kcontext_plugin(context);
  171. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  172. args = param2argv(cur_path, kcontext_pargv(context), "path");
  173. pline = pline_parse(sess, args, 0);
  174. faux_argv_free(args);
  175. if (pline->invalid) {
  176. fprintf(stderr, "Invalid set request\n");
  177. ret = -1;
  178. goto cleanup;
  179. }
  180. iter = faux_list_head(pline->exprs);
  181. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  182. if (!(expr->pat & PT_SET)) {
  183. err_num++;
  184. fprintf(stderr, "Illegal expression for set operation\n");
  185. break;
  186. }
  187. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  188. SR_ERR_OK) {
  189. err_num++;
  190. fprintf(stderr, "Can't set data\n");
  191. break;
  192. }
  193. }
  194. if (sr_has_changes(sess)) {
  195. if (err_num > 0)
  196. sr_discard_changes(sess);
  197. else
  198. sr_apply_changes(sess, 0);
  199. }
  200. if (err_num > 0)
  201. ret = -1;
  202. cleanup:
  203. pline_free(pline);
  204. sr_disconnect(conn);
  205. return ret;
  206. }
  207. int srp_del(kcontext_t *context)
  208. {
  209. int ret = 0;
  210. faux_argv_t *args = NULL;
  211. pline_t *pline = NULL;
  212. sr_conn_ctx_t *conn = NULL;
  213. sr_session_ctx_t *sess = NULL;
  214. pexpr_t *expr = NULL;
  215. size_t err_num = 0;
  216. faux_argv_t *cur_path = NULL;
  217. kplugin_t *plugin = NULL;
  218. assert(context);
  219. if (sr_connect(SR_CONN_DEFAULT, &conn))
  220. return -1;
  221. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  222. sr_disconnect(conn);
  223. return -1;
  224. }
  225. plugin = kcontext_plugin(context);
  226. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  227. args = param2argv(cur_path, kcontext_pargv(context), "path");
  228. pline = pline_parse(sess, args, 0);
  229. faux_argv_free(args);
  230. if (pline->invalid) {
  231. fprintf(stderr, "Invalid 'del' request\n");
  232. ret = -1;
  233. goto cleanup;
  234. }
  235. if (faux_list_len(pline->exprs) > 1) {
  236. fprintf(stderr, "Can't delete more than one object\n");
  237. ret = -1;
  238. goto cleanup;
  239. }
  240. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  241. if (!(expr->pat & PT_DEL)) {
  242. fprintf(stderr, "Illegal expression for 'del' operation\n");
  243. ret = -1;
  244. goto cleanup;
  245. }
  246. if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
  247. fprintf(stderr, "Can't delete data\n");
  248. ret = -1;
  249. goto cleanup;
  250. }
  251. sr_apply_changes(sess, 0);
  252. cleanup:
  253. pline_free(pline);
  254. sr_disconnect(conn);
  255. return ret;
  256. }
  257. int srp_edit(kcontext_t *context)
  258. {
  259. int ret = 0;
  260. faux_argv_t *args = NULL;
  261. pline_t *pline = NULL;
  262. sr_conn_ctx_t *conn = NULL;
  263. sr_session_ctx_t *sess = 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, SR_DS_RUNNING, &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. if (pline->invalid) {
  280. fprintf(stderr, "Invalid 'edit' request\n");
  281. ret = -1;
  282. goto cleanup;
  283. }
  284. if (faux_list_len(pline->exprs) > 1) {
  285. fprintf(stderr, "Can't process more than one object\n");
  286. ret = -1;
  287. goto cleanup;
  288. }
  289. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  290. if (!(expr->pat & PT_EDIT)) {
  291. fprintf(stderr, "Illegal expression for 'edit' operation\n");
  292. ret = -1;
  293. goto cleanup;
  294. }
  295. if (sr_set_item_str(sess, expr->xpath, NULL, NULL, 0) != SR_ERR_OK) {
  296. fprintf(stderr, "Can't set editing data\n");
  297. ret = -1;
  298. goto cleanup;
  299. }
  300. sr_apply_changes(sess, 0);
  301. // Set new current path
  302. faux_argv_free(cur_path);
  303. kplugin_set_udata(plugin, args);
  304. cleanup:
  305. if (ret < 0)
  306. faux_argv_free(args);
  307. pline_free(pline);
  308. sr_disconnect(conn);
  309. return ret;
  310. }
  311. int srp_top(kcontext_t *context)
  312. {
  313. faux_argv_t *cur_path = NULL;
  314. kplugin_t *plugin = NULL;
  315. assert(context);
  316. plugin = kcontext_plugin(context);
  317. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  318. faux_argv_free(cur_path);
  319. kplugin_set_udata(plugin, NULL);
  320. return 0;
  321. }
  322. int srp_up(kcontext_t *context)
  323. {
  324. sr_conn_ctx_t *conn = NULL;
  325. sr_session_ctx_t *sess = NULL;
  326. faux_argv_t *cur_path = NULL;
  327. kplugin_t *plugin = NULL;
  328. faux_argv_node_t *iter = NULL;
  329. assert(context);
  330. plugin = kcontext_plugin(context);
  331. cur_path = (faux_argv_t *)kplugin_udata(plugin);
  332. if (!cur_path)
  333. return -1; // It's top level and can't level up
  334. if (sr_connect(SR_CONN_DEFAULT, &conn))
  335. return -1;
  336. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  337. sr_disconnect(conn);
  338. return -1;
  339. }
  340. // Remove last arguments one by one and wait for legal edit-like pline
  341. while (faux_argv_len(cur_path) > 0) {
  342. pline_t *pline = NULL;
  343. pexpr_t *expr = NULL;
  344. size_t len = 0;
  345. iter = faux_argv_iterr(cur_path);
  346. faux_argv_del(cur_path, iter);
  347. pline = pline_parse(sess, cur_path, 0);
  348. if (pline->invalid) {
  349. pline_free(pline);
  350. continue;
  351. }
  352. len = faux_list_len(pline->exprs);
  353. if (len != 1) {
  354. pline_free(pline);
  355. continue;
  356. }
  357. expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  358. if (!(expr->pat & PT_EDIT)) {
  359. pline_free(pline);
  360. continue;
  361. }
  362. // Here new path is ok
  363. pline_free(pline);
  364. break;
  365. }
  366. // Don't store empty path
  367. while (faux_argv_len(cur_path) == 0) {
  368. faux_argv_free(cur_path);
  369. kplugin_set_udata(plugin, NULL);
  370. }
  371. sr_disconnect(conn);
  372. return 0;
  373. }