ekg2

ekg/scripts.h

Idź do dokumentacji tego pliku.
00001 #ifndef EKG_SCRIPTS_H
00002 #define EKG_SCRIPTS_H
00003 #include <sys/types.h>
00004 
00005 #include "commands.h"
00006 #include "plugins.h"
00007 #include "protocol.h"
00008 #include "stuff.h"
00009 #include "vars.h"
00010 #include "queries.h"
00011 
00012 #ifdef __cplusplus
00013 extern "C" {
00014 #endif
00015 
00016 #define SCRIPT_HANDLE_UNBIND    -666
00017 #define MAX_ARGS QUERY_ARGS_MAX+1
00018 
00019 typedef enum {
00020         SCRIPT_UNKNOWNTYPE,
00021         SCRIPT_VARTYPE,
00022         SCRIPT_COMMANDTYPE,
00023         SCRIPT_QUERYTYPE,
00024         SCRIPT_TIMERTYPE,
00025         SCRIPT_WATCHTYPE,
00026         SCRIPT_PLUGINTYPE, 
00027 } script_type_t;
00028 
00029 typedef struct script {
00030         struct script   *next;
00031 
00032         void            *lang;
00033         char            *name;
00034         char            *path;
00035         void            *priv_data;
00036         int             inited;
00037 } script_t;
00038 extern script_t         *scripts;
00039 
00040 typedef struct {
00041         script_t        *scr;
00042         struct timer    *self;
00043         int             removed;
00044         void            *priv_data;
00045 } script_timer_t; 
00046 
00047 typedef struct {
00048         script_t        *scr;
00049         plugin_t        *self;
00050         void            *priv_data;
00051 } script_plugin_t;
00052 
00053 typedef struct {
00054         script_t        *scr;
00055         variable_t      *self;
00056 
00057         char            *name;
00058         char            *value;
00059         void            *priv_data;
00060 } script_var_t; 
00061 
00062 typedef struct {
00063         script_t        *scr;
00064         query_t         *self;
00065         int             argc;
00066         int             argv_type[MAX_ARGS];
00067 
00068         int             real_argc;
00069         void            *priv_data;
00070         int             hack;
00071 } script_query_t; 
00072 
00073 typedef struct {
00074         script_t        *scr;
00075         command_t       *self;
00076         void            *priv_data; 
00077 } script_command_t;
00078 
00079 typedef struct {
00080         script_t        *scr;
00081         watch_t         *self; 
00082         int             removed;
00083         void            *data;
00084         void            *priv_data;
00085 } script_watch_t;
00086 
00087 typedef int (scriptlang_initialize_t)();
00088 typedef int (scriptlang_finalize_t)();
00089 typedef int (script_load_t)(script_t *);
00090 typedef int (script_unload_t)(script_t *);
00091 typedef int (script_handler_command_t)(script_t *, script_command_t *, char **);
00092 typedef int (script_handler_timer_t)  (script_t *, script_timer_t *, int);
00093 typedef int (script_handler_var_t)    (script_t *, script_var_t *,   char *);
00094 typedef int (script_handler_query_t)  (script_t *, script_query_t *, void **);
00095 typedef int (script_handler_watch_t)  (script_t *, script_watch_t *, int, int, int);
00096 
00097 typedef int (script_free_bind_t)      (script_t *, void *, int, void *, ...);
00098 
00099 typedef struct scriptlang {
00100         struct scriptlang       *next;
00101 
00102         char     *name;         /* perl, python, php *g* and so on. */
00103         char     *ext;          /*  .pl,    .py, .php ... */
00104         plugin_t *plugin;
00105 
00106         scriptlang_initialize_t *init;
00107         scriptlang_finalize_t   *deinit;
00108 
00109         script_load_t           *script_load;
00110         script_unload_t         *script_unload;
00111         script_free_bind_t      *script_free_bind;
00112 
00113         script_handler_query_t  *script_handler_query;
00114         script_handler_command_t*script_handler_command;
00115         script_handler_timer_t  *script_handler_timer;
00116         script_handler_var_t    *script_handler_var;
00117         script_handler_watch_t  *script_handler_watch;
00118         
00119         void *priv_data;
00120 } scriptlang_t;
00121 extern scriptlang_t *scriptlang;
00122 
00123 #define SCRIPT_FINDER(bool)\
00124         script_t *scr = NULL;\
00125         scriptlang_t *slang = NULL;\
00126         \
00127         for (scr = scripts; scr; scr = scr->next) {\
00128                 slang = scr->lang;\
00129                 if (bool)\
00130                         return scr;\
00131         }\
00132         return NULL;
00133 
00134 /* XXX: Split *_watches() into normal and line-ones.
00135  *
00136  *      Until then we abort the build if sizeof(void*) > sizeof(long int), as
00137  *      in that case the pointer would get corrupted when being passed around
00138  *      as a long using type-punning.
00139  *
00140  *      This trick was stolen from the Linux kernel. See
00141  *      http://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/
00142  *      for an explanation.
00143  */
00144 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
00145 
00146 #define SCRIPT_DEFINE(x, y)\
00147         extern int x##_load(script_t *);\
00148         extern int x##_unload(script_t *);\
00149         \
00150         extern int x##_commands(script_t *, script_command_t *, char **);\
00151         extern int x##_timers(script_t *, script_timer_t *, int );\
00152         extern int x##_variable_changed(script_t *, script_var_t *, char *);\
00153         extern int x##_query(script_t *, script_query_t *, void **);\
00154         extern int x##_watches(script_t *, script_watch_t *, int, int, long int);\
00155         void x##_dummy_sanity_check() { BUILD_BUG_ON(sizeof(void *) > sizeof(long)); };\
00156         \
00157         extern int x##_bind_free(script_t *, void *, int type, void *, ...);\
00158         \
00159         scriptlang_t x##_lang = { \
00160                 name: #x, \
00161                 plugin: &x##_plugin, \
00162                 ext: y, \
00163 \
00164                 init: x##_initialize,\
00165                 deinit: x##_finalize, \
00166 \
00167                 script_load: x##_load, \
00168                 script_unload: x##_unload, \
00169                 script_free_bind: x##_bind_free,\
00170 \
00171                 script_handler_query  : x##_query,\
00172                 script_handler_command: x##_commands,\
00173                 script_handler_timer  : x##_timers,\
00174                 script_handler_var    : x##_variable_changed,\
00175                 script_handler_watch  : x##_watches,\
00176         }
00177 
00178 #define script_private_get(s) (s->priv_data)
00179 #define script_private_set(s, p) (s->priv_data = p)
00180 
00181 #ifndef EKG2_WIN32_NOFUNCTION
00182 int script_unload_lang(scriptlang_t *s);
00183 
00184 int script_list(scriptlang_t *s);
00185 int script_unload_name(scriptlang_t *s, char *name);
00186 int script_load(scriptlang_t *s, char *name);
00187 
00188 int scriptlang_register(scriptlang_t *s);
00189 int scriptlang_unregister(scriptlang_t *s);
00190 
00191 int scripts_init();
00192 
00193 script_t *script_find(scriptlang_t *s, char *name);
00194 
00195 int script_query_unbind(script_query_t *squery, int from);
00196 int script_command_unbind(script_command_t *scr_comm, int free);
00197 int script_timer_unbind(script_timer_t *stimer, int free);
00198 int script_var_unbind(script_var_t *data, int free);
00199 int script_watch_unbind(script_watch_t *temp, int free);
00200 
00201 script_command_t *script_command_bind(scriptlang_t *s, script_t *scr, char *command, char *params, char *possibilities, void *handler);
00202 script_timer_t *script_timer_bind(scriptlang_t *s, script_t *scr, int freq, void *handler);
00203 script_query_t *script_query_bind(scriptlang_t *s, script_t *scr, char *qname, void *handler);
00204 script_var_t *script_var_add(scriptlang_t *s, script_t *scr, char *name, char *value, void *handler);
00205 script_watch_t *script_watch_add(scriptlang_t *s, script_t *scr, int fd, int type, void *handler, void *data);
00206 script_plugin_t *script_plugin_init(scriptlang_t *s, script_t *scr, char *name, plugin_class_t pclass, void *handler);
00207 
00208 int script_variables_free(int free);
00209 int script_variables_write();
00210 #endif
00211 
00212 #define SCRIPT_UNBIND_HANDLER(type, args...) \
00213 {\
00214         SCRIPT_HANDLER_HEADER(script_free_bind_t);\
00215         SCRIPT_HANDLER_FOOTER(script_free_bind, type, temp->priv_data, args);\
00216 }
00217 
00218 /* BINDING && UNBINDING */
00219 
00220 #define SCRIPT_BIND_HEADER(x) \
00221         x *temp; \
00222         if (scr && scr->inited != 1) {\
00223                 debug_error("[script_bind_error] script not inited!\n");        \
00224                 return NULL;    \
00225         }       \
00226         temp = xmalloc(sizeof(x)); \
00227         temp->scr  = scr;\
00228         temp->priv_data = handler;
00229 
00230 #define SCRIPT_BIND_FOOTER(y) \
00231         if (!temp->self) {\
00232                 debug("[script_bind_error] (before adding to %s) ERROR! retcode = 0x%x\n", #y, temp->self);\
00233                 xfree(temp);\
00234                 return NULL;\
00235         }\
00236         list_add(&y, temp);\
00237         return temp;
00238 
00239 /* HANDLERS */
00240 #define SCRIPT_HANDLER_HEADER(x) \
00241         script_t        *_scr;\
00242         scriptlang_t    *_slang;\
00243         x               *_handler;\
00244         int             ret = SCRIPT_HANDLE_UNBIND;
00245 
00246 /* TODO: quietmode */
00247 #define SCRIPT_HANDLER_FOOTER(y, _args...) \
00248         if ((_scr = temp->scr) && ((_slang = _scr->lang)))  _handler = _slang->y;\
00249         else                                                _handler = temp->priv_data;\
00250         if (_handler)\
00251                 ret = _handler(_scr, temp, _args); \
00252         else {\
00253                 debug("[%s] (_handler == NULL)\n", #y);\
00254                 ret = SCRIPT_HANDLE_UNBIND;\
00255         }\
00256         \
00257         if (ret == SCRIPT_HANDLE_UNBIND) { debug("[%s] script or scriptlang want to delete this handler\n", #y); }\
00258         if (ret == SCRIPT_HANDLE_UNBIND)
00259 
00260 #define SCRIPT_HANDLER_MULTI_FOOTER(y, _args...)\
00261 /* foreach y->list->next do SCRIPT_HANDLER_FOOTER(y->list->data, _args); */\
00262         SCRIPT_HANDLER_FOOTER(y, _args)
00263 
00264 #ifdef __cplusplus
00265 }
00266 #endif
00267 
00268 #endif
 All Struktury Danych Pliki Funkcje Zmienne Definicje typów Wyliczenia Wartości wyliczeń Definicje