plugin.c 6.4 KB

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