plugin.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_deactivate", srp_deactivate));
  70. // User-data initialization
  71. udata = faux_zmalloc(sizeof(*udata));
  72. assert(udata);
  73. udata->path = NULL;
  74. udata->flags = parse_plugin_conf(kplugin_conf(plugin), SRP_DEFAULT_PARSE_OPTS);
  75. kplugin_set_udata(plugin, udata);
  76. return 0;
  77. }
  78. int kplugin_sysrepo_fini(kcontext_t *context)
  79. {
  80. srp_udata_t *udata = NULL;
  81. assert(context);
  82. // Free plugin's user-data
  83. udata = (srp_udata_t *)kcontext_udata(context);
  84. assert(udata);
  85. if (udata->path)
  86. faux_argv_free(udata->path);
  87. faux_free(udata);
  88. return 0;
  89. }
  90. uint32_t srp_udata_flags(kcontext_t *context)
  91. {
  92. srp_udata_t *udata = NULL;
  93. assert(context);
  94. udata = (srp_udata_t *)kcontext_udata(context);
  95. assert(udata);
  96. return udata->flags;
  97. }
  98. faux_argv_t *srp_udata_path(kcontext_t *context)
  99. {
  100. srp_udata_t *udata = NULL;
  101. assert(context);
  102. udata = (srp_udata_t *)kcontext_udata(context);
  103. assert(udata);
  104. return udata->path;
  105. }
  106. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
  107. {
  108. srp_udata_t *udata = NULL;
  109. assert(context);
  110. udata = (srp_udata_t *)kcontext_udata(context);
  111. assert(udata);
  112. if (udata->path)
  113. faux_argv_free(udata->path);
  114. udata->path = path;
  115. }
  116. static uint32_t parse_plugin_conf(const char *conf, uint32_t default_flags)
  117. {
  118. uint32_t flags = default_flags;
  119. faux_ini_t *ini = NULL;
  120. const char *val = NULL;
  121. if (!conf)
  122. return flags;
  123. ini = faux_ini_new();
  124. if (!faux_ini_parse_str(ini, conf)) {
  125. faux_ini_free(ini);
  126. return flags;
  127. }
  128. if ((val = faux_ini_find(ini, "JuniperLikeShow"))) {
  129. if (faux_str_cmp(val, "y") == 0)
  130. flags = flags | PPARSE_JUNIPER_SHOW;
  131. else if (faux_str_cmp(val, "n") == 0)
  132. flags = flags & (~(uint32_t)PPARSE_JUNIPER_SHOW);
  133. }
  134. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  135. if (faux_str_cmp(val, "y") == 0)
  136. flags = flags | PPARSE_FIRST_KEY_W_STMT;
  137. else if (faux_str_cmp(val, "n") == 0)
  138. flags = flags & (~(uint32_t)PPARSE_FIRST_KEY_W_STMT);
  139. }
  140. if ((val = faux_ini_find(ini, "MultiKeysWithStatement"))) {
  141. if (faux_str_cmp(val, "y") == 0)
  142. flags = flags | PPARSE_MULTI_KEYS_W_STMT;
  143. else if (faux_str_cmp(val, "n") == 0)
  144. flags = flags & (~(uint32_t)PPARSE_MULTI_KEYS_W_STMT);
  145. }
  146. faux_ini_free(ini);
  147. return flags;
  148. }