plugin.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <faux/str.h>
  10. #include <faux/ini.h>
  11. #include <faux/conv.h>
  12. #include <klish/kplugin.h>
  13. #include <klish/kcontext.h>
  14. #include "private.h"
  15. const uint8_t kplugin_sysrepo_major = KPLUGIN_MAJOR;
  16. const uint8_t kplugin_sysrepo_minor = KPLUGIN_MINOR;
  17. static int parse_plugin_conf(const char *conf, pline_opts_t *opts);
  18. int kplugin_sysrepo_init(kcontext_t *context)
  19. {
  20. kplugin_t *plugin = NULL;
  21. ksym_t *sym = NULL;
  22. int err = SR_ERR_OK;
  23. sr_conn_ctx_t *conn = NULL;
  24. sr_session_ctx_t *sess = NULL;
  25. srp_udata_t *udata = NULL;
  26. assert(context);
  27. plugin = kcontext_plugin(context);
  28. assert(plugin);
  29. // Symbols
  30. // Types
  31. kplugin_add_syms(plugin, ksym_new_ext("PLINE_SET", srp_PLINE_SET,
  32. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  33. kplugin_add_syms(plugin, ksym_new_ext("PLINE_DEL", srp_PLINE_DEL,
  34. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  35. kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT", srp_PLINE_EDIT,
  36. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  37. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_FROM", srp_PLINE_INSERT_FROM,
  38. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  39. kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_TO", srp_PLINE_INSERT_TO,
  40. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  41. // Completion/Help/Prompt
  42. kplugin_add_syms(plugin, ksym_new_ext("srp_compl", srp_compl,
  43. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  44. kplugin_add_syms(plugin, ksym_new_ext("srp_help", srp_help,
  45. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  46. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_insert_to", srp_compl_insert_to,
  47. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  48. kplugin_add_syms(plugin, ksym_new_ext("srp_help_insert_to", srp_help_insert_to,
  49. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  50. kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
  51. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  52. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath_running", srp_compl_xpath_running,
  53. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  54. kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath_candidate", srp_compl_xpath_candidate,
  55. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  56. // Operations
  57. kplugin_add_syms(plugin, ksym_new("srp_set", srp_set));
  58. kplugin_add_syms(plugin, ksym_new("srp_del", srp_del));
  59. // Note: 'edit', 'top', 'up' must be sync to set current path
  60. kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
  61. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  62. kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
  63. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  64. kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
  65. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  66. kplugin_add_syms(plugin, ksym_new("srp_insert", srp_insert));
  67. kplugin_add_syms(plugin, ksym_new("srp_verify", srp_verify));
  68. kplugin_add_syms(plugin, ksym_new("srp_commit", srp_commit));
  69. kplugin_add_syms(plugin, ksym_new("srp_rollback", srp_rollback));
  70. kplugin_add_syms(plugin, ksym_new("srp_show", srp_show));
  71. kplugin_add_syms(plugin, ksym_new("srp_show_running", srp_show_running));
  72. kplugin_add_syms(plugin, ksym_new("srp_diff", srp_diff));
  73. kplugin_add_syms(plugin, ksym_new("srp_deactivate", srp_deactivate));
  74. // User-data initialization
  75. udata = faux_zmalloc(sizeof(*udata));
  76. assert(udata);
  77. udata->path = NULL;
  78. // Settings
  79. udata->opts.begin_bracket = '{';
  80. udata->opts.end_bracket = '}';
  81. udata->opts.show_brackets = BOOL_TRUE;
  82. udata->opts.show_semicolons = BOOL_TRUE;
  83. udata->opts.first_key_w_stmt = BOOL_FALSE;
  84. udata->opts.keys_w_stmt = BOOL_TRUE;
  85. udata->opts.colorize = BOOL_TRUE;
  86. udata->opts.indent = 2;
  87. udata->opts.default_keys = BOOL_FALSE;
  88. udata->opts.hide_passwords = BOOL_TRUE;
  89. parse_plugin_conf(kplugin_conf(plugin), &udata->opts);
  90. kplugin_set_udata(plugin, udata);
  91. // Logging
  92. srp_set_log_func();
  93. return 0;
  94. }
  95. int kplugin_sysrepo_fini(kcontext_t *context)
  96. {
  97. srp_udata_t *udata = NULL;
  98. assert(context);
  99. // Free plugin's user-data
  100. udata = (srp_udata_t *)kcontext_udata(context);
  101. assert(udata);
  102. if (udata->path)
  103. faux_argv_free(udata->path);
  104. faux_free(udata);
  105. return 0;
  106. }
  107. pline_opts_t *srp_udata_opts(kcontext_t *context)
  108. {
  109. srp_udata_t *udata = NULL;
  110. assert(context);
  111. udata = (srp_udata_t *)kcontext_udata(context);
  112. assert(udata);
  113. return &udata->opts;
  114. }
  115. faux_argv_t *srp_udata_path(kcontext_t *context)
  116. {
  117. srp_udata_t *udata = NULL;
  118. assert(context);
  119. udata = (srp_udata_t *)kcontext_udata(context);
  120. assert(udata);
  121. return udata->path;
  122. }
  123. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  124. {
  125. srp_udata_t *udata = NULL;
  126. assert(context);
  127. udata = (srp_udata_t *)kcontext_udata(context);
  128. assert(udata);
  129. if (udata->path)
  130. faux_argv_free(udata->path);
  131. udata->path = path;
  132. }
  133. static int parse_plugin_conf(const char *conf, pline_opts_t *opts)
  134. {
  135. faux_ini_t *ini = NULL;
  136. const char *val = NULL;
  137. if (!opts)
  138. return -1;
  139. if (!conf)
  140. return 0; // Use defaults
  141. ini = faux_ini_new();
  142. if (!faux_ini_parse_str(ini, conf)) {
  143. faux_ini_free(ini);
  144. return -1;
  145. }
  146. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  147. if (faux_str_cmp(val, "y") == 0)
  148. opts->show_brackets = BOOL_TRUE;
  149. else if (faux_str_cmp(val, "n") == 0)
  150. opts->show_brackets = BOOL_FALSE;
  151. }
  152. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  153. if (faux_str_cmp(val, "y") == 0)
  154. opts->show_semicolons = BOOL_TRUE;
  155. else if (faux_str_cmp(val, "n") == 0)
  156. opts->show_semicolons = BOOL_FALSE;
  157. }
  158. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  159. if (faux_str_cmp(val, "y") == 0)
  160. opts->first_key_w_stmt = BOOL_TRUE;
  161. else if (faux_str_cmp(val, "n") == 0)
  162. opts->first_key_w_stmt = BOOL_FALSE;
  163. }
  164. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  165. if (faux_str_cmp(val, "y") == 0)
  166. opts->keys_w_stmt = BOOL_TRUE;
  167. else if (faux_str_cmp(val, "n") == 0)
  168. opts->keys_w_stmt = BOOL_FALSE;
  169. }
  170. if ((val = faux_ini_find(ini, "Colorize"))) {
  171. if (faux_str_cmp(val, "y") == 0)
  172. opts->colorize = BOOL_TRUE;
  173. else if (faux_str_cmp(val, "n") == 0)
  174. opts->colorize = BOOL_FALSE;
  175. }
  176. if ((val = faux_ini_find(ini, "Indent"))) {
  177. unsigned char indent = 0;
  178. if (faux_conv_atouc(val, &indent, 10))
  179. opts->indent = indent;
  180. }
  181. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  182. if (faux_str_cmp(val, "y") == 0)
  183. opts->default_keys = BOOL_TRUE;
  184. else if (faux_str_cmp(val, "n") == 0)
  185. opts->default_keys = BOOL_FALSE;
  186. }
  187. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  188. if (faux_str_cmp(val, "y") == 0)
  189. opts->hide_passwords = BOOL_TRUE;
  190. else if (faux_str_cmp(val, "n") == 0)
  191. opts->hide_passwords = BOOL_FALSE;
  192. }
  193. faux_ini_free(ini);
  194. return 0;
  195. }