syms.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 *pargv2argv(const kpargv_t *pargv)
  21. {
  22. const kentry_t *candidate = NULL;
  23. faux_list_node_t *iter = NULL;
  24. faux_list_t *pargs = NULL;
  25. faux_argv_t *args = NULL;
  26. assert(pargv);
  27. if (!pargv)
  28. return NULL;
  29. pargs = kpargv_pargs(pargv);
  30. candidate = kparg_entry(kpargv_candidate_parg(pargv));
  31. iter = faux_list_tail(pargs);
  32. while (iter) {
  33. faux_list_node_t *prev = faux_list_prev_node(iter);
  34. kparg_t *parg = (kparg_t *)faux_list_data(iter);
  35. if (kparg_entry(parg) != candidate) {
  36. iter = faux_list_next_node(iter);
  37. break;
  38. }
  39. if (!prev)
  40. break;
  41. iter = prev;
  42. }
  43. args = faux_argv_new();
  44. while (iter) {
  45. kparg_t *parg = (kparg_t *)faux_list_data(iter);
  46. faux_argv_add(args, kparg_value(parg));
  47. iter = faux_list_next_node(iter);
  48. }
  49. faux_argv_set_continuable(args, kpargv_continuable(pargv));
  50. return args;
  51. }
  52. // Candidate from pargv contains possible begin of current word (that must be
  53. // completed). kpargv's list don't contain candidate but only already parsed
  54. // words.
  55. int srp_compl(kcontext_t *context)
  56. {
  57. faux_argv_t *args = NULL;
  58. pline_t *pline = NULL;
  59. sr_conn_ctx_t *conn = NULL;
  60. sr_session_ctx_t *sess = NULL;
  61. assert(context);
  62. if (sr_connect(SR_CONN_DEFAULT, &conn))
  63. return -1;
  64. if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
  65. sr_disconnect(conn);
  66. return -1;
  67. }
  68. args = pargv2argv(kcontext_parent_pargv(context));
  69. pline = pline_parse(sess, args, 0);
  70. faux_argv_free(args);
  71. pline_print_completions(pline, BOOL_FALSE);
  72. pline_free(pline);
  73. sr_disconnect(conn);
  74. return 0;
  75. }