plugin.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. *
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <assert.h>
  8. #include <faux/faux.h>
  9. #include <klish/kplugin.h>
  10. #include <klish/kcontext.h>
  11. #include <sysrepo.h>
  12. #include <sysrepo/xpath.h>
  13. #include "private.h"
  14. const uint8_t kplugin_sysrepo_major = KPLUGIN_MAJOR;
  15. const uint8_t kplugin_sysrepo_minor = KPLUGIN_MINOR;
  16. int kplugin_sysrepo_init(kcontext_t *context)
  17. {
  18. kplugin_t *plugin = NULL;
  19. ksym_t *sym = NULL;
  20. int err = SR_ERR_OK;
  21. sr_conn_ctx_t *conn = NULL;
  22. sr_session_ctx_t *sess = NULL;
  23. srp_udata_t *udata = NULL;
  24. assert(context);
  25. plugin = kcontext_plugin(context);
  26. assert(plugin);
  27. // Symbols
  28. // Types
  29. kplugin_add_syms(plugin, ksym_new_ext("PLINE_SET", srp_PLINE_SET,
  30. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  31. kplugin_add_syms(plugin, ksym_new_ext("PLINE_DEL", srp_PLINE_DEL,
  32. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  33. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT", srp_PLINE_EDIT,
  34. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  35. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_FROM", srp_PLINE_INSERT_FROM,
  36. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  37. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_TO", srp_PLINE_INSERT_TO,
  38. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  39. // Completion/Help/Prompt
  40. kplugin_add_syms(plugin, ksym_new_ext("srp_compl", srp_compl,
  41. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  42. kplugin_add_syms(plugin, ksym_new_ext("srp_help", srp_help,
  43. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  44. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert_to", srp_compl_insert_to,
  45. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  46. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert_to", srp_help_insert_to,
  47. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  48. kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
  49. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  50. // Operations
  51. kplugin_add_syms(plugin, ksym_new("srp_set", srp_set));
  52. kplugin_add_syms(plugin, ksym_new("srp_del", srp_del));
  53. // Note: 'edit', 'top', 'up' must be sync to set current path
  54. kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
  55. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  56. kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
  57. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  58. kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
  59. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  60. kplugin_add_syms(plugin, ksym_new("srp_insert", srp_insert));
  61. kplugin_add_syms(plugin, ksym_new("srp_commit", srp_commit));
  62. kplugin_add_syms(plugin, ksym_new("srp_rollback", srp_rollback));
  63. kplugin_add_syms(plugin, ksym_new("srp_show", srp_show));
  64. kplugin_add_syms(plugin, ksym_new("srp_show_running", srp_show_running));
  65. kplugin_add_syms(plugin, ksym_new("srp_deactivate", srp_deactivate));
  66. // User-data initialization
  67. udata = faux_zmalloc(sizeof(*udata));
  68. assert(udata);
  69. udata->path = NULL;
  70. udata->flags = 0;
  71. kplugin_set_udata(plugin, udata);
  72. return 0;
  73. }
  74. int kplugin_sysrepo_fini(kcontext_t *context)
  75. {
  76. srp_udata_t *udata = NULL;
  77. assert(context);
  78. // Free plugin's user-data
  79. udata = (srp_udata_t *)kcontext_udata(context);
  80. assert(udata);
  81. if (udata->path)
  82. faux_argv_free(udata->path);
  83. faux_free(udata);
  84. return 0;
  85. }
  86. uint32_t srp_udata_flags(kcontext_t *context)
  87. {
  88. srp_udata_t *udata = NULL;
  89. assert(context);
  90. udata = (srp_udata_t *)kcontext_udata(context);
  91. assert(udata);
  92. return udata->flags;
  93. }
  94. faux_argv_t *srp_udata_path(kcontext_t *context)
  95. {
  96. srp_udata_t *udata = NULL;
  97. assert(context);
  98. udata = (srp_udata_t *)kcontext_udata(context);
  99. assert(udata);
  100. return udata->path;
  101. }
  102. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  103. {
  104. srp_udata_t *udata = NULL;
  105. assert(context);
  106. udata = (srp_udata_t *)kcontext_udata(context);
  107. assert(udata);
  108. if (udata->path)
  109. faux_argv_free(udata->path);
  110. udata->path = path;
  111. }