plugin.c 5.3 KB

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