plugin.c 6.4 KB

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