plugin.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * plugin.h
  3. */
  4. #ifndef _clish_plugin_h
  5. #define _clish_plugin_h
  6. #ifdef HAVE_CONFIG_H
  7. #include "config.h"
  8. #endif /* HAVE_CONFIG_H */
  9. #include "clish/macros.h"
  10. #include "lub/types.h"
  11. /* Symbol */
  12. /* Symbol types. Functions with different purposes. */
  13. typedef enum {
  14. CLISH_SYM_TYPE_NONE = 0, /* None */
  15. CLISH_SYM_TYPE_ACTION, /* Common builtin symbol for ACTION tag */
  16. CLISH_SYM_TYPE_ACCESS, /* Callback for "access" field */
  17. CLISH_SYM_TYPE_CONFIG, /* Callback for CONFIG tag */
  18. CLISH_SYM_TYPE_LOG, /* Callback for logging */
  19. CLISH_SYM_TYPE_MAX /* Number of elements */
  20. } clish_sym_type_e;
  21. /* External functions APIs. */
  22. typedef enum {
  23. CLISH_SYM_API_SIMPLE = 0, /* Simple (may be single) API */
  24. CLISH_SYM_API_STDOUT /* Symbol for ACTION tag without "out" argument */
  25. } clish_sym_api_e;
  26. typedef struct clish_sym_s clish_sym_t;
  27. typedef struct clish_plugin_s clish_plugin_t;
  28. /* Plugin types */
  29. /* Name of init function within plugin */
  30. #define CLISH_PLUGIN_INIT_FNAME(name) clish_plugin_##name##_init
  31. #define CLISH_PLUGIN_INIT_NAME_PREFIX "clish_plugin_"
  32. #define CLISH_PLUGIN_INIT_NAME_SUFFIX "_init"
  33. #define CLISH_PLUGIN_INIT_FUNC(name) int name(void *clish_shell, clish_plugin_t *plugin)
  34. #define CLISH_PLUGIN_INIT(name) CLISH_PLUGIN_INIT_FUNC(CLISH_PLUGIN_INIT_FNAME(name))
  35. #define CLISH_PLUGIN_FINI(name) int name(void *clish_shell, clish_plugin_t *plugin)
  36. #define CLISH_PLUGIN_SYM(name) int name(void *clish_context, const char *script, char **out)
  37. #define CLISH_PLUGIN_OSYM(name) int name(void *clish_context, const char *script)
  38. #define CLISH_HOOK_ACCESS(name) int name(void *clish_shell, const char *access)
  39. #define CLISH_HOOK_CONFIG(name) int name(void *clish_context)
  40. #define CLISH_HOOK_LOG(name) int name(void *clish_context, const char *line, int retcode)
  41. typedef CLISH_PLUGIN_INIT_FUNC(clish_plugin_init_t);
  42. typedef CLISH_PLUGIN_FINI(clish_plugin_fini_t);
  43. typedef CLISH_PLUGIN_SYM(clish_hook_action_fn_t);
  44. typedef CLISH_PLUGIN_OSYM(clish_hook_oaction_fn_t);
  45. typedef CLISH_HOOK_ACCESS(clish_hook_access_fn_t);
  46. typedef CLISH_HOOK_CONFIG(clish_hook_config_fn_t);
  47. typedef CLISH_HOOK_LOG(clish_hook_log_fn_t);
  48. /* Helpers */
  49. #define SYM_FN(TYPE,SYM) (*((clish_hook_##TYPE##_fn_t *)(clish_sym__get_func(SYM))))
  50. /* Create an array of builtin plugin's init functions */
  51. struct clish_plugin_builtin_list_s {
  52. const char *name; /* Plugin name */
  53. clish_plugin_init_t *init; /* Plugin init function */
  54. };
  55. typedef struct clish_plugin_builtin_list_s clish_plugin_builtin_list_t;
  56. extern clish_plugin_builtin_list_t clish_plugin_builtin_list[];
  57. /* Symbol */
  58. int clish_sym_compare(const void *first, const void *second);
  59. clish_sym_t *clish_sym_new(const char *name, void *func, int type);
  60. void clish_sym_free(void *instance);
  61. int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src);
  62. _CLISH_SET(sym, const void *, func);
  63. _CLISH_GET(sym, const void *, func);
  64. _CLISH_SET(sym, bool_t, permanent);
  65. _CLISH_GET(sym, bool_t, permanent);
  66. _CLISH_SET(sym, tri_t, expand);
  67. _CLISH_GET(sym, tri_t, expand);
  68. _CLISH_SET_STR(sym, name);
  69. _CLISH_GET_STR(sym, name);
  70. _CLISH_SET(sym, clish_plugin_t *, plugin);
  71. _CLISH_GET(sym, clish_plugin_t *, plugin);
  72. _CLISH_SET(sym, int, type);
  73. _CLISH_GET(sym, int, type);
  74. _CLISH_SET(sym, clish_sym_api_e, api);
  75. _CLISH_GET(sym, clish_sym_api_e, api);
  76. /* Plugin */
  77. clish_plugin_t *clish_plugin_new(const char *name, void *userdata);
  78. void clish_plugin_free(void *instance);
  79. int clish_plugin_load(clish_plugin_t *instance);
  80. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *instance,
  81. const char *name, int type);
  82. clish_sym_t *clish_plugin_add_generic(clish_plugin_t *instance,
  83. void *func, const char *name, int type, bool_t permanent);
  84. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *instance,
  85. clish_hook_action_fn_t *func, const char *name);
  86. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *instance,
  87. clish_hook_action_fn_t *func, const char *name);
  88. clish_sym_t *clish_plugin_add_osym(clish_plugin_t *instance,
  89. clish_hook_oaction_fn_t *func, const char *name);
  90. clish_sym_t *clish_plugin_add_posym(clish_plugin_t *instance,
  91. clish_hook_oaction_fn_t *func, const char *name);
  92. clish_sym_t *clish_plugin_add_hook(clish_plugin_t *instance,
  93. void *func, const char *name, int type);
  94. clish_sym_t *clish_plugin_add_phook(clish_plugin_t *instance,
  95. void *func, const char *name, int type);
  96. void clish_plugin_dump(const clish_plugin_t *instance);
  97. _CLISH_SET(plugin, clish_plugin_fini_t *, fini);
  98. _CLISH_GET(plugin, clish_plugin_fini_t *, fini);
  99. _CLISH_SET(plugin, clish_plugin_init_t *, init);
  100. _CLISH_GET(plugin, clish_plugin_init_t *, init);
  101. _CLISH_GET_STR(plugin, name);
  102. _CLISH_SET_STR(plugin, alias);
  103. _CLISH_GET_STR(plugin, alias);
  104. _CLISH_SET_STR(plugin, file);
  105. _CLISH_GET_STR(plugin, file);
  106. _CLISH_SET_STR(plugin, conf);
  107. _CLISH_GET_STR(plugin, conf);
  108. _CLISH_SET(plugin, bool_t, builtin_flag);
  109. _CLISH_GET(plugin, bool_t, builtin_flag);
  110. _CLISH_SET(plugin, bool_t, rtld_global);
  111. _CLISH_GET(plugin, bool_t, rtld_global);
  112. _CLISH_GET_STR(plugin, pubname);
  113. #endif /* _clish_plugin_h */
  114. /** @} clish_plugin */