plugin.c 6.1 KB

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