plugin.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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", srp_compl_xpath,
  54. KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
  55. // Operations
  56. kplugin_add_syms(plugin, ksym_new("srp_set", srp_set));
  57. kplugin_add_syms(plugin, ksym_new("srp_del", srp_del));
  58. // Note: 'edit', 'top', 'up' must be sync to set current path
  59. kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
  60. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  61. kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
  62. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  63. kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
  64. KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
  65. kplugin_add_syms(plugin, ksym_new("srp_insert", srp_insert));
  66. kplugin_add_syms(plugin, ksym_new("srp_verify", srp_verify));
  67. kplugin_add_syms(plugin, ksym_new("srp_commit", srp_commit));
  68. kplugin_add_syms(plugin, ksym_new("srp_rollback", srp_rollback));
  69. kplugin_add_syms(plugin, ksym_new("srp_show", srp_show));
  70. kplugin_add_syms(plugin, ksym_new("srp_show_running", srp_show_running));
  71. kplugin_add_syms(plugin, ksym_new("srp_diff", srp_diff));
  72. kplugin_add_syms(plugin, ksym_new("srp_deactivate", srp_deactivate));
  73. // User-data initialization
  74. udata = faux_zmalloc(sizeof(*udata));
  75. assert(udata);
  76. udata->path = NULL;
  77. udata->flags = parse_plugin_conf(kplugin_conf(plugin), SRP_DEFAULT_PARSE_OPTS);
  78. kplugin_set_udata(plugin, udata);
  79. return 0;
  80. }
  81. int kplugin_sysrepo_fini(kcontext_t *context)
  82. {
  83. srp_udata_t *udata = NULL;
  84. assert(context);
  85. // Free plugin's user-data
  86. udata = (srp_udata_t *)kcontext_udata(context);
  87. assert(udata);
  88. if (udata->path)
  89. faux_argv_free(udata->path);
  90. faux_free(udata);
  91. return 0;
  92. }
  93. uint32_t srp_udata_flags(kcontext_t *context)
  94. {
  95. srp_udata_t *udata = NULL;
  96. assert(context);
  97. udata = (srp_udata_t *)kcontext_udata(context);
  98. assert(udata);
  99. return udata->flags;
  100. }
  101. faux_argv_t *srp_udata_path(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->path;
  108. }
  109. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  110. {
  111. srp_udata_t *udata = NULL;
  112. assert(context);
  113. udata = (srp_udata_t *)kcontext_udata(context);
  114. assert(udata);
  115. if (udata->path)
  116. faux_argv_free(udata->path);
  117. udata->path = path;
  118. }
  119. static uint32_t parse_plugin_conf(const char *conf, uint32_t default_flags)
  120. {
  121. uint32_t flags = default_flags;
  122. faux_ini_t *ini = NULL;
  123. const char *val = NULL;
  124. if (!conf)
  125. return flags;
  126. ini = faux_ini_new();
  127. if (!faux_ini_parse_str(ini, conf)) {
  128. faux_ini_free(ini);
  129. return flags;
  130. }
  131. if ((val = faux_ini_find(ini, "JuniperLikeShow"))) {
  132. if (faux_str_cmp(val, "y") == 0)
  133. flags = flags | PPARSE_JUNIPER_SHOW;
  134. else if (faux_str_cmp(val, "n") == 0)
  135. flags = flags & (~(uint32_t)PPARSE_JUNIPER_SHOW);
  136. }
  137. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  138. if (faux_str_cmp(val, "y") == 0)
  139. flags = flags | PPARSE_FIRST_KEY_W_STMT;
  140. else if (faux_str_cmp(val, "n") == 0)
  141. flags = flags & (~(uint32_t)PPARSE_FIRST_KEY_W_STMT);
  142. }
  143. if ((val = faux_ini_find(ini, "MultiKeysWithStatement"))) {
  144. if (faux_str_cmp(val, "y") == 0)
  145. flags = flags | PPARSE_MULTI_KEYS_W_STMT;
  146. else if (faux_str_cmp(val, "n") == 0)
  147. flags = flags & (~(uint32_t)PPARSE_MULTI_KEYS_W_STMT);
  148. }
  149. if ((val = faux_ini_find(ini, "Colorize"))) {
  150. if (faux_str_cmp(val, "y") == 0)
  151. flags = flags | PPARSE_COLORIZE;
  152. else if (faux_str_cmp(val, "n") == 0)
  153. flags = flags & (~(uint32_t)PPARSE_COLORIZE);
  154. }
  155. faux_ini_free(ini);
  156. return flags;
  157. }