syms.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 kpargv_t *pargv, const char *entry_name)
  21. {
  22. faux_list_node_t *iter = NULL;
  23. faux_list_t *pargs = NULL;
  24. faux_argv_t *args = NULL;
  25. kparg_t *parg = NULL;
  26. assert(pargv);
  27. if (!pargv)
  28. return NULL;
  29. pargs = kpargv_find_multi(pargv, entry_name);
  30. args = faux_argv_new();
  31. iter = faux_list_head(pargs);
  32. while ((parg = (kparg_t *)faux_list_each(&iter))) {
  33. faux_argv_add(args, kparg_value(parg));
  34. }
  35. faux_list_free(pargs);
  36. return args;
  37. }
  38. // Candidate from pargv contains possible begin of current word (that must be
  39. // completed). kpargv's list don't contain candidate but only already parsed
  40. // words.
  41. static int srp_compl_or_help(kcontext_t *context, bool_t help)
  42. {
  43. faux_argv_t *args = NULL;
  44. pline_t *pline = NULL;
  45. sr_conn_ctx_t *conn = NULL;
  46. sr_session_ctx_t *sess = NULL;
  47. const char *entry_name = NULL;
  48. assert(context);
  49. if (sr_connect(SR_CONN_DEFAULT, &conn))
  50. return -1;
  51. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  52. sr_disconnect(conn);
  53. return -1;
  54. }
  55. entry_name = kentry_name(kcontext_candidate_entry(context));
  56. args = param2argv(kcontext_parent_pargv(context), entry_name);
  57. pline = pline_parse(sess, args, 0);
  58. faux_argv_free(args);
  59. pline_print_completions(pline, help);
  60. pline_free(pline);
  61. sr_disconnect(conn);
  62. return 0;
  63. }
  64. int srp_compl(kcontext_t *context)
  65. {
  66. return srp_compl_or_help(context, BOOL_FALSE);
  67. }
  68. int srp_help(kcontext_t *context)
  69. {
  70. return srp_compl_or_help(context, BOOL_TRUE);
  71. }
  72. int srp_set(kcontext_t *context)
  73. {
  74. int ret = 0;
  75. faux_argv_t *args = NULL;
  76. pline_t *pline = NULL;
  77. sr_conn_ctx_t *conn = NULL;
  78. sr_session_ctx_t *sess = NULL;
  79. faux_list_node_t *iter = NULL;
  80. pexpr_t *expr = NULL;
  81. size_t err_num = 0;
  82. assert(context);
  83. if (sr_connect(SR_CONN_DEFAULT, &conn))
  84. return -1;
  85. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  86. sr_disconnect(conn);
  87. return -1;
  88. }
  89. args = param2argv(kcontext_pargv(context), "path");
  90. pline = pline_parse(sess, args, 0);
  91. faux_argv_free(args);
  92. if (pline->invalid) {
  93. fprintf(stderr, "Invalid set request\n");
  94. ret = -1;
  95. goto cleanup;
  96. }
  97. iter = faux_list_head(pline->exprs);
  98. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  99. if (!(expr->pat & PT_SET)) {
  100. err_num++;
  101. fprintf(stderr, "Illegal expression for set operation\n");
  102. break;
  103. }
  104. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  105. SR_ERR_OK) {
  106. err_num++;
  107. fprintf(stderr, "Can't set data\n");
  108. break;
  109. }
  110. }
  111. if (sr_has_changes(sess)) {
  112. if (err_num > 0)
  113. sr_discard_changes(sess);
  114. else
  115. sr_apply_changes(sess, 0);
  116. }
  117. if (err_num > 0)
  118. ret = -1;
  119. cleanup:
  120. pline_free(pline);
  121. sr_disconnect(conn);
  122. return ret;
  123. }