ekg2  GIT master
scripts.h
Idź do dokumentacji tego pliku.
1 #ifndef EKG_SCRIPTS_H
2 #define EKG_SCRIPTS_H
3 #include <sys/types.h>
4 
5 #include "commands.h"
6 #include "plugins.h"
7 #include "protocol.h"
8 #include "stuff.h"
9 #include "vars.h"
10 #include "queries.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #define SCRIPT_HANDLE_UNBIND -666
17 #define MAX_ARGS QUERY_ARGS_MAX+1
18 
19 typedef enum {
28 
29 typedef struct script {
30  struct script *next;
31 
32  void *lang;
33  char *name;
34  char *path;
35  void *priv_data;
36  int inited;
37 } script_t;
38 extern script_t *scripts;
39 
40 typedef struct {
43  int removed;
44  void *priv_data;
46 
47 typedef struct {
49  plugin_t *self;
50  void *priv_data;
52 
53 typedef struct {
55  variable_t *self;
56 
57  char *name;
58  char *value;
59  void *priv_data;
60 } script_var_t;
61 
62 typedef struct {
64  query_t *self;
65  int argc;
66  int argv_type[MAX_ARGS];
67 
68  int real_argc;
69  void *priv_data;
70  int hack;
72 
73 typedef struct {
75  command_t *self;
76  void *priv_data;
78 
79 typedef struct {
81  watch_t *self;
82  int removed;
83  void *data;
84  void *priv_data;
86 
87 typedef int (scriptlang_initialize_t)();
88 typedef int (scriptlang_finalize_t)();
89 typedef int (script_load_t)(script_t *);
90 typedef int (script_unload_t)(script_t *);
91 typedef int (script_handler_command_t)(script_t *, script_command_t *, char **);
92 typedef int (script_handler_timer_t) (script_t *, script_timer_t *, int);
93 typedef int (script_handler_var_t) (script_t *, script_var_t *, char *);
94 typedef int (script_handler_query_t) (script_t *, script_query_t *, void **);
95 typedef int (script_handler_watch_t) (script_t *, script_watch_t *, int, int, int);
96 
97 typedef int (script_free_bind_t) (script_t *, void *, int, void *, ...);
98 
99 typedef struct scriptlang {
100  struct scriptlang *next;
101 
102  char *name; /* perl, python, php *g* and so on. */
103  char *ext; /* .pl, .py, .php ... */
105 
108 
112 
118 
119  void *priv_data;
120 } scriptlang_t;
121 extern scriptlang_t *scriptlang;
122 
123 #define SCRIPT_FINDER(bool)\
124  script_t *scr = NULL;\
125  scriptlang_t *slang = NULL;\
126  \
127  for (scr = scripts; scr; scr = scr->next) {\
128  slang = scr->lang;\
129  if (bool)\
130  return scr;\
131  }\
132  return NULL;
133 
134 /* XXX: Split *_watches() into normal and line-ones.
135  *
136  * Until then we abort the build if sizeof(void*) > sizeof(long int), as
137  * in that case the pointer would get corrupted when being passed around
138  * as a long using type-punning.
139  *
140  * This trick was stolen from the Linux kernel. See
141  * http://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/
142  * for an explanation.
143  */
144 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
145 
146 #define SCRIPT_DEFINE(x, y)\
147  extern int x##_load(script_t *);\
148  extern int x##_unload(script_t *);\
149  \
150  extern int x##_commands(script_t *, script_command_t *, char **);\
151  extern int x##_timers(script_t *, script_timer_t *, int );\
152  extern int x##_variable_changed(script_t *, script_var_t *, char *);\
153  extern int x##_query(script_t *, script_query_t *, void **);\
154  extern int x##_watches(script_t *, script_watch_t *, int, int, long int);\
155  void x##_dummy_sanity_check() { BUILD_BUG_ON(sizeof(void *) > sizeof(long)); };\
156  \
157  extern int x##_bind_free(script_t *, void *, int type, void *, ...);\
158  \
159  scriptlang_t x##_lang = { \
160  name: #x, \
161  plugin: &x##_plugin, \
162  ext: y, \
163 \
164  init: x##_initialize,\
165  deinit: x##_finalize, \
166 \
167  script_load: x##_load, \
168  script_unload: x##_unload, \
169  script_free_bind: x##_bind_free,\
170 \
171  script_handler_query : x##_query,\
172  script_handler_command: x##_commands,\
173  script_handler_timer : x##_timers,\
174  script_handler_var : x##_variable_changed,\
175  script_handler_watch : x##_watches,\
176  }
177 
178 #define script_private_get(s) (s->priv_data)
179 #define script_private_set(s, p) (s->priv_data = p)
180 
181 #ifndef EKG2_WIN32_NOFUNCTION
183 
186 int script_load(scriptlang_t *s, char *name);
187 
190 
191 int scripts_init();
192 
193 script_t *script_find(scriptlang_t *s, char *name);
194 
195 int script_query_unbind(script_query_t *squery, int from);
196 int script_command_unbind(script_command_t *scr_comm, int free);
197 int script_timer_unbind(script_timer_t *stimer, int free);
198 int script_var_unbind(script_var_t *data, int free);
199 int script_watch_unbind(script_watch_t *temp, int free);
200 
201 script_command_t *script_command_bind(scriptlang_t *s, script_t *scr, char *command, char *params, char *possibilities, void *handler);
202 script_timer_t *script_timer_bind(scriptlang_t *s, script_t *scr, int freq, void *handler);
203 script_query_t *script_query_bind(scriptlang_t *s, script_t *scr, char *qname, void *handler);
204 script_var_t *script_var_add_full(scriptlang_t *s, script_t *scr, char *name, int type, char *value, void *handler);
205 script_var_t *script_var_add(scriptlang_t *s, script_t *scr, char *name, char *value, void *handler);
206 script_watch_t *script_watch_add(scriptlang_t *s, script_t *scr, int fd, int type, void *handler, void *data);
207 script_plugin_t *script_plugin_init(scriptlang_t *s, script_t *scr, char *name, plugin_class_t pclass, void *handler);
208 
209 void script_variables_free();
211 #endif
212 
213 #define SCRIPT_UNBIND_HANDLER(type, args...) \
214 {\
215  SCRIPT_HANDLER_HEADER(script_free_bind_t);\
216  SCRIPT_HANDLER_FOOTER(script_free_bind, type, temp->priv_data, args);\
217 }
218 
219 /* BINDING && UNBINDING */
220 
221 #define SCRIPT_BIND_HEADER(x) \
222  x *temp; \
223  if (scr && scr->inited != 1) {\
224  debug_error("[script_bind_error] script not inited!\n"); \
225  return NULL; \
226  } \
227  temp = xmalloc(sizeof(x)); \
228  temp->scr = scr;\
229  temp->priv_data = handler;
230 
231 #define SCRIPT_BIND_FOOTER(y) \
232  if (!temp->self) {\
233  debug("[script_bind_error] (before adding to %s) ERROR! retcode = 0x%x\n", #y, temp->self);\
234  xfree(temp);\
235  return NULL;\
236  }\
237  list_add(&y, temp);\
238  return temp;
239 
240 /* HANDLERS */
241 #define SCRIPT_HANDLER_HEADER(x) \
242  script_t *_scr;\
243  scriptlang_t *_slang;\
244  x *_handler;\
245  int ret = SCRIPT_HANDLE_UNBIND;
246 
247 /* TODO: quietmode */
248 #define SCRIPT_HANDLER_FOOTER(y, _args...) \
249  if ((_scr = temp->scr) && ((_slang = _scr->lang))) _handler = _slang->y;\
250  else _handler = temp->priv_data;\
251  if (_handler)\
252  ret = _handler(_scr, temp, _args); \
253  else {\
254  debug("[%s] (_handler == NULL)\n", #y);\
255  ret = SCRIPT_HANDLE_UNBIND;\
256  }\
257  \
258  if (ret == SCRIPT_HANDLE_UNBIND) { debug("[%s] script or scriptlang want to delete this handler\n", #y); }\
259  if (ret == SCRIPT_HANDLE_UNBIND)
260 
261 #define SCRIPT_HANDLER_MULTI_FOOTER(y, _args...)\
262 /* foreach y->list->next do SCRIPT_HANDLER_FOOTER(y->list->data, _args); */\
263  SCRIPT_HANDLER_FOOTER(y, _args)
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 
269 #endif
Definition: scripts.h:79
void script_variables_write()
Definition: scripts.c:453
script_free_bind_t * script_free_bind
Definition: scripts.h:111
int script_var_unbind(script_var_t *data, int free)
Definition: scripts.c:556
#define MAX_ARGS
Definition: scripts.h:17
script_t * scr
Definition: scripts.h:63
struct scriptlang * next
Definition: scripts.h:100
struct scriptlang scriptlang_t
void * priv_data
Definition: scripts.h:84
Definition: scripts.h:29
void * priv_data
Definition: scripts.h:69
script_query_t * script_query_bind(scriptlang_t *s, script_t *scr, char *qname, void *handler)
Definition: scripts.c:652
int script_unload_name(scriptlang_t *s, char *name)
Definition: scripts.c:297
script_t * scr
Definition: scripts.h:74
Definition: commands.h:63
int script_list(scriptlang_t *s)
Definition: scripts.c:176
Definition: scripts.h:24
int hack
Definition: scripts.h:70
int script_load(scriptlang_t *s, char *name)
Definition: scripts.c:337
Definition: scripts.h:47
int script_timer_unbind(script_timer_t *stimer, int free)
Definition: scripts.c:535
script_handler_var_t * script_handler_var
Definition: scripts.h:116
char * path
Definition: scripts.h:34
int argc
Definition: scripts.h:65
script_t * scr
Definition: scripts.h:54
int script_unload_lang(scriptlang_t *s)
Definition: scripts.c:319
int removed
Definition: scripts.h:82
Definition: sources.c:39
int() script_unload_t(script_t *)
Definition: scripts.h:90
script_handler_timer_t * script_handler_timer
Definition: scripts.h:115
script_watch_t * script_watch_add(scriptlang_t *s, script_t *scr, int fd, int type, void *handler, void *data)
Definition: scripts.c:644
Definition: plugins.h:76
script_t * scripts
Definition: scripts.c:21
script_command_t * script_command_bind(scriptlang_t *s, script_t *scr, char *command, char *params, char *possibilities, void *handler)
Definition: scripts.c:615
int() script_handler_command_t(script_t *, script_command_t *, char **)
Definition: scripts.h:91
struct script * next
Definition: scripts.h:30
void * priv_data
Definition: scripts.h:59
void * priv_data
Definition: scripts.h:44
Definition: scripts.h:53
char * name
Definition: scripts.h:102
script_plugin_t * script_plugin_init(scriptlang_t *s, script_t *scr, char *name, plugin_class_t pclass, void *handler)
Definition: scripts.c:622
int real_argc
Definition: scripts.h:68
int scriptlang_unregister(scriptlang_t *s)
Definition: scripts.c:76
int() scriptlang_finalize_t()
Definition: scripts.h:88
script_var_t * script_var_add_full(scriptlang_t *s, script_t *scr, char *name, int type, char *value, void *handler)
Definition: scripts.c:566
void script_variables_free()
Definition: scripts.c:437
int script_watch_unbind(script_watch_t *temp, int free)
Definition: scripts.c:545
#define params(x)
Definition: irc.c:1893
int script_query_unbind(script_query_t *squery, int from)
Definition: scripts.c:508
void * lang
Definition: scripts.h:32
char * value
Definition: scripts.h:58
int() script_handler_watch_t(script_t *, script_watch_t *, int, int, int)
Definition: scripts.h:95
plugin_class_t
Definition: plugins.h:40
script_t * scr
Definition: scripts.h:41
Definition: scripts.h:99
int() script_free_bind_t(script_t *, void *, int, void *,...)
Definition: scripts.h:97
int script_command_unbind(script_command_t *scr_comm, int free)
Definition: scripts.c:498
script_type_t
Definition: scripts.h:19
void * priv_data
Definition: scripts.h:50
script_handler_query_t * script_handler_query
Definition: scripts.h:113
struct script script_t
int inited
Definition: scripts.h:36
scriptlang_initialize_t * init
Definition: scripts.h:106
script_timer_t * script_timer_bind(scriptlang_t *s, script_t *scr, int freq, void *handler)
Definition: scripts.c:634
Definition: scripts.h:20
void * priv_data
Definition: scripts.h:35
script_t * script_find(scriptlang_t *s, char *name)
Definition: scripts.c:292
scriptlang_t * scriptlang
Definition: scripts.c:27
Definition: scripts.h:22
Definition: scripts.h:73
char * ext
Definition: scripts.h:103
int removed
Definition: scripts.h:43
Definition: scripts.h:23
int() script_load_t(script_t *)
Definition: scripts.h:89
Definition: scripts.h:21
int() script_handler_timer_t(script_t *, script_timer_t *, int)
Definition: scripts.h:92
Definition: plugins.h:140
script_unload_t * script_unload
Definition: scripts.h:110
Definition: scripts.h:40
script_t * scr
Definition: scripts.h:48
Definition: sources.h:78
plugin_t * plugin
Definition: scripts.h:104
scriptlang_finalize_t * deinit
Definition: scripts.h:107
script_handler_command_t * script_handler_command
Definition: scripts.h:114
void * data
Definition: scripts.h:83
char * name
Definition: scripts.h:33
char * name
Definition: scripts.h:57
#define s
Definition: scripts.h:25
abort_handler handler
Definition: abort.c:24
int scripts_init()
Definition: scripts.c:951
Definition: scripts.h:26
script_load_t * script_load
Definition: scripts.h:109
int() script_handler_var_t(script_t *, script_var_t *, char *)
Definition: scripts.h:93
void * priv_data
Definition: scripts.h:76
int() scriptlang_initialize_t()
Definition: scripts.h:87
script_var_t * script_var_add(scriptlang_t *s, script_t *scr, char *name, char *value, void *handler)
Definition: scripts.c:610
Definition: vars.h:52
script_handler_watch_t * script_handler_watch
Definition: scripts.h:117
script_t * scr
Definition: scripts.h:80
int() script_handler_query_t(script_t *, script_query_t *, void **)
Definition: scripts.h:94
void * priv_data
Definition: scripts.h:119
Definition: scripts.h:62
int scriptlang_register(scriptlang_t *s)
Definition: scripts.c:65