plugin.c 6.7 KB

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