plugin.c 5.0 KB

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