plugin.c 5.5 KB

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