plugin.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. udata->opts.enable_nacm = BOOL_FALSE;
  87. udata->opts.oneliners = BOOL_TRUE;
  88. parse_plugin_conf(kplugin_conf(plugin), &udata->opts);
  89. kplugin_set_udata(plugin, udata);
  90. // Logging
  91. ly_log_options(LY_LOSTORE);
  92. return 0;
  93. }
  94. int kplugin_sysrepo_fini(kcontext_t *context)
  95. {
  96. srp_udata_t *udata = NULL;
  97. assert(context);
  98. // Free plugin's user-data
  99. udata = (srp_udata_t *)kcontext_udata(context);
  100. assert(udata);
  101. if (udata->path)
  102. faux_argv_free(udata->path);
  103. faux_free(udata);
  104. return 0;
  105. }
  106. pline_opts_t *srp_udata_opts(kcontext_t *context)
  107. {
  108. srp_udata_t *udata = NULL;
  109. assert(context);
  110. udata = (srp_udata_t *)kcontext_udata(context);
  111. assert(udata);
  112. return &udata->opts;
  113. }
  114. faux_argv_t *srp_udata_path(kcontext_t *context)
  115. {
  116. srp_udata_t *udata = NULL;
  117. assert(context);
  118. udata = (srp_udata_t *)kcontext_udata(context);
  119. assert(udata);
  120. return udata->path;
  121. }
  122. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  123. {
  124. srp_udata_t *udata = NULL;
  125. assert(context);
  126. udata = (srp_udata_t *)kcontext_udata(context);
  127. assert(udata);
  128. if (udata->path)
  129. faux_argv_free(udata->path);
  130. udata->path = path;
  131. }
  132. static int parse_plugin_conf(const char *conf, pline_opts_t *opts)
  133. {
  134. faux_ini_t *ini = NULL;
  135. const char *val = NULL;
  136. if (!opts)
  137. return -1;
  138. if (!conf)
  139. return 0; // Use defaults
  140. ini = faux_ini_new();
  141. if (!faux_ini_parse_str(ini, conf)) {
  142. faux_ini_free(ini);
  143. return -1;
  144. }
  145. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  146. if (faux_str_cmp(val, "y") == 0)
  147. opts->show_brackets = BOOL_TRUE;
  148. else if (faux_str_cmp(val, "n") == 0)
  149. opts->show_brackets = BOOL_FALSE;
  150. }
  151. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  152. if (faux_str_cmp(val, "y") == 0)
  153. opts->show_semicolons = BOOL_TRUE;
  154. else if (faux_str_cmp(val, "n") == 0)
  155. opts->show_semicolons = BOOL_FALSE;
  156. }
  157. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  158. if (faux_str_cmp(val, "y") == 0)
  159. opts->first_key_w_stmt = BOOL_TRUE;
  160. else if (faux_str_cmp(val, "n") == 0)
  161. opts->first_key_w_stmt = BOOL_FALSE;
  162. }
  163. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  164. if (faux_str_cmp(val, "y") == 0)
  165. opts->keys_w_stmt = BOOL_TRUE;
  166. else if (faux_str_cmp(val, "n") == 0)
  167. opts->keys_w_stmt = BOOL_FALSE;
  168. }
  169. if ((val = faux_ini_find(ini, "Colorize"))) {
  170. if (faux_str_cmp(val, "y") == 0)
  171. opts->colorize = BOOL_TRUE;
  172. else if (faux_str_cmp(val, "n") == 0)
  173. opts->colorize = BOOL_FALSE;
  174. }
  175. if ((val = faux_ini_find(ini, "Indent"))) {
  176. unsigned char indent = 0;
  177. if (faux_conv_atouc(val, &indent, 10))
  178. opts->indent = indent;
  179. }
  180. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  181. if (faux_str_cmp(val, "y") == 0)
  182. opts->default_keys = BOOL_TRUE;
  183. else if (faux_str_cmp(val, "n") == 0)
  184. opts->default_keys = BOOL_FALSE;
  185. }
  186. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  187. if (faux_str_cmp(val, "y") == 0)
  188. opts->show_default_keys = BOOL_TRUE;
  189. else if (faux_str_cmp(val, "n") == 0)
  190. opts->show_default_keys = BOOL_FALSE;
  191. }
  192. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  193. if (faux_str_cmp(val, "y") == 0)
  194. opts->hide_passwords = BOOL_TRUE;
  195. else if (faux_str_cmp(val, "n") == 0)
  196. opts->hide_passwords = BOOL_FALSE;
  197. }
  198. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  199. if (faux_str_cmp(val, "y") == 0)
  200. opts->enable_nacm = BOOL_TRUE;
  201. else if (faux_str_cmp(val, "n") == 0)
  202. opts->enable_nacm = BOOL_FALSE;
  203. }
  204. if ((val = faux_ini_find(ini, "Oneliners"))) {
  205. if (faux_str_cmp(val, "y") == 0)
  206. opts->oneliners = BOOL_TRUE;
  207. else if (faux_str_cmp(val, "n") == 0)
  208. opts->oneliners = BOOL_FALSE;
  209. }
  210. faux_ini_free(ini);
  211. return 0;
  212. }