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 *private;
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 *private;
00045 } script_timer_t;
00046
00047 typedef struct {
00048 script_t *scr;
00049 plugin_t *self;
00050 void *private;
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 *private;
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 void *private;
00068 int hack;
00069 } script_query_t;
00070
00071 typedef struct {
00072 script_t *scr;
00073 command_t *self;
00074 void *private;
00075 } script_command_t;
00076
00077 typedef struct {
00078 script_t *scr;
00079 watch_t *self;
00080 int removed;
00081 void *data;
00082 void *private;
00083 } script_watch_t;
00084
00085 typedef int (scriptlang_initialize_t)();
00086 typedef int (scriptlang_finalize_t)();
00087 typedef int (script_load_t)(script_t *);
00088 typedef int (script_unload_t)(script_t *);
00089 typedef int (script_handler_command_t)(script_t *, script_command_t *, char **);
00090 typedef int (script_handler_timer_t) (script_t *, script_timer_t *, int);
00091 typedef int (script_handler_var_t) (script_t *, script_var_t *, char *);
00092 typedef int (script_handler_query_t) (script_t *, script_query_t *, void **);
00093 typedef int (script_handler_watch_t) (script_t *, script_watch_t *, int, int, int);
00094
00095 typedef int (script_free_bind_t) (script_t *, void *, int, void *, ...);
00096
00097 typedef struct scriptlang {
00098 struct scriptlang *next;
00099
00100 char *name;
00101 char *ext;
00102 plugin_t *plugin;
00103
00104 scriptlang_initialize_t *init;
00105 scriptlang_finalize_t *deinit;
00106
00107 script_load_t *script_load;
00108 script_unload_t *script_unload;
00109 script_free_bind_t *script_free_bind;
00110
00111 script_handler_query_t *script_handler_query;
00112 script_handler_command_t*script_handler_command;
00113 script_handler_timer_t *script_handler_timer;
00114 script_handler_var_t *script_handler_var;
00115 script_handler_watch_t *script_handler_watch;
00116
00117 void *private;
00118 } scriptlang_t;
00119 extern scriptlang_t *scriptlang;
00120
00121 #define SCRIPT_FINDER(bool)\
00122 script_t *scr = NULL;\
00123 scriptlang_t *slang = NULL;\
00124 \
00125 for (scr = scripts; scr; scr = scr->next) {\
00126 slang = scr->lang;\
00127 if (bool)\
00128 return scr;\
00129 }\
00130 return NULL;
00131
00132
00133
00134 #warning "POSSIBLE EPIC FAIL: when void* > long int, WATCH_LINE pointer may be shortened"
00135
00136 #define SCRIPT_DEFINE(x, y)\
00137 extern int x##_load(script_t *);\
00138 extern int x##_unload(script_t *);\
00139 \
00140 extern int x##_commands(script_t *, script_command_t *, char **);\
00141 extern int x##_timers(script_t *, script_timer_t *, int );\
00142 extern int x##_variable_changed(script_t *, script_var_t *, char *);\
00143 extern int x##_query(script_t *, script_query_t *, void **);\
00144 extern int x##_watches(script_t *, script_watch_t *, int, int, long int);\
00145 \
00146 extern int x##_bind_free(script_t *, void *, int type, void *, ...);\
00147 \
00148 scriptlang_t x##_lang = { \
00149 name: #x, \
00150 plugin: &x##_plugin, \
00151 ext: y, \
00152 \
00153 init: x##_initialize,\
00154 deinit: x##_finalize, \
00155 \
00156 script_load: x##_load, \
00157 script_unload: x##_unload, \
00158 script_free_bind: x##_bind_free,\
00159 \
00160 script_handler_query : x##_query,\
00161 script_handler_command: x##_commands,\
00162 script_handler_timer : x##_timers,\
00163 script_handler_var : x##_variable_changed,\
00164 script_handler_watch : x##_watches,\
00165 }
00166
00167 #define script_private_get(s) (s->private)
00168 #define script_private_set(s, p) (s->private = p)
00169
00170 #ifndef EKG2_WIN32_NOFUNCTION
00171 int script_unload_lang(scriptlang_t *s);
00172
00173 int script_list(scriptlang_t *s);
00174 int script_unload_name(scriptlang_t *s, char *name);
00175 int script_load(scriptlang_t *s, char *name);
00176
00177 int scriptlang_register(scriptlang_t *s);
00178 int scriptlang_unregister(scriptlang_t *s);
00179
00180 int scripts_init();
00181
00182 script_t *script_find(scriptlang_t *s, char *name);
00183
00184 int script_query_unbind(script_query_t *squery, int from);
00185 int script_command_unbind(script_command_t *scr_comm, int free);
00186 int script_timer_unbind(script_timer_t *stimer, int free);
00187 int script_var_unbind(script_var_t *data, int free);
00188 int script_watch_unbind(script_watch_t *temp, int free);
00189
00190 script_command_t *script_command_bind(scriptlang_t *s, script_t *scr, char *command, void *handler);
00191 script_timer_t *script_timer_bind(scriptlang_t *s, script_t *scr, int freq, void *handler);
00192 script_query_t *script_query_bind(scriptlang_t *s, script_t *scr, char *qname, void *handler);
00193 script_var_t *script_var_add(scriptlang_t *s, script_t *scr, char *name, char *value, void *handler);
00194 script_watch_t *script_watch_add(scriptlang_t *s, script_t *scr, int fd, int type, void *handler, void *data);
00195 script_plugin_t *script_plugin_init(scriptlang_t *s, script_t *scr, char *name, plugin_class_t pclass, void *handler);
00196
00197 int script_variables_free(int free);
00198 int script_variables_write();
00199 #endif
00200
00201 #define SCRIPT_UNBIND_HANDLER(type, args...) \
00202 {\
00203 SCRIPT_HANDLER_HEADER(script_free_bind_t);\
00204 SCRIPT_HANDLER_FOOTER(script_free_bind, type, temp->private, args);\
00205 }
00206
00207
00208
00209 #define SCRIPT_BIND_HEADER(x) \
00210 x *temp; \
00211 if (scr && scr->inited != 1) {\
00212 debug_error("[script_bind_error] script not inited!\n"); \
00213 return NULL; \
00214 } \
00215 temp = xmalloc(sizeof(x)); \
00216 temp->scr = scr;\
00217 temp->private = handler;
00218
00219 #define SCRIPT_BIND_FOOTER(y) \
00220 if (!temp->self) {\
00221 debug("[script_bind_error] (before adding to %s) ERROR! retcode = 0x%x\n", #y, temp->self);\
00222 xfree(temp);\
00223 return NULL;\
00224 }\
00225 list_add(&y, temp);\
00226 return temp;
00227
00228
00229 #define SCRIPT_HANDLER_HEADER(x) \
00230 script_t *_scr;\
00231 scriptlang_t *_slang;\
00232 x *_handler;\
00233 int ret = SCRIPT_HANDLE_UNBIND;
00234
00235
00236 #define SCRIPT_HANDLER_FOOTER(y, _args...) \
00237 if ((_scr = temp->scr) && ((_slang = _scr->lang))) _handler = _slang->y;\
00238 else _handler = temp->private;\
00239 if (_handler)\
00240 ret = _handler(_scr, temp, _args); \
00241 else {\
00242 debug("[%s] (_handler == NULL)\n", #y);\
00243 ret = SCRIPT_HANDLE_UNBIND;\
00244 }\
00245 \
00246 if (ret == SCRIPT_HANDLE_UNBIND) { debug("[%s] script or scriptlang want to delete this handler\n", #y); }\
00247 if (ret == SCRIPT_HANDLE_UNBIND)
00248
00249 #define SCRIPT_HANDLER_MULTI_FOOTER(y, _args...)\
00250 \
00251 SCRIPT_HANDLER_FOOTER(y, _args)
00252
00253 #ifdef __cplusplus
00254 }
00255 #endif
00256
00257 #endif