ekg2
 All Struktury Danych Pliki Funkcje Zmienne Definicje typów Wyliczenia Wartości wyliczeń Definicje Grupay Strony
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 {
42  struct timer *self;
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 
184 int script_list(scriptlang_t *s);
185 int script_unload_name(scriptlang_t *s, char *name);
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(scriptlang_t *s, script_t *scr, char *name, char *value, void *handler);
205 script_watch_t *script_watch_add(scriptlang_t *s, script_t *scr, int fd, int type, void *handler, void *data);
206 script_plugin_t *script_plugin_init(scriptlang_t *s, script_t *scr, char *name, plugin_class_t pclass, void *handler);
207 
208 int script_variables_free(int free);
210 #endif
211 
212 #define SCRIPT_UNBIND_HANDLER(type, args...) \
213 {\
214  SCRIPT_HANDLER_HEADER(script_free_bind_t);\
215  SCRIPT_HANDLER_FOOTER(script_free_bind, type, temp->priv_data, args);\
216 }
217 
218 /* BINDING && UNBINDING */
219 
220 #define SCRIPT_BIND_HEADER(x) \
221  x *temp; \
222  if (scr && scr->inited != 1) {\
223  debug_error("[script_bind_error] script not inited!\n"); \
224  return NULL; \
225  } \
226  temp = xmalloc(sizeof(x)); \
227  temp->scr = scr;\
228  temp->priv_data = handler;
229 
230 #define SCRIPT_BIND_FOOTER(y) \
231  if (!temp->self) {\
232  debug("[script_bind_error] (before adding to %s) ERROR! retcode = 0x%x\n", #y, temp->self);\
233  xfree(temp);\
234  return NULL;\
235  }\
236  list_add(&y, temp);\
237  return temp;
238 
239 /* HANDLERS */
240 #define SCRIPT_HANDLER_HEADER(x) \
241  script_t *_scr;\
242  scriptlang_t *_slang;\
243  x *_handler;\
244  int ret = SCRIPT_HANDLE_UNBIND;
245 
246 /* TODO: quietmode */
247 #define SCRIPT_HANDLER_FOOTER(y, _args...) \
248  if ((_scr = temp->scr) && ((_slang = _scr->lang))) _handler = _slang->y;\
249  else _handler = temp->priv_data;\
250  if (_handler)\
251  ret = _handler(_scr, temp, _args); \
252  else {\
253  debug("[%s] (_handler == NULL)\n", #y);\
254  ret = SCRIPT_HANDLE_UNBIND;\
255  }\
256  \
257  if (ret == SCRIPT_HANDLE_UNBIND) { debug("[%s] script or scriptlang want to delete this handler\n", #y); }\
258  if (ret == SCRIPT_HANDLE_UNBIND)
259 
260 #define SCRIPT_HANDLER_MULTI_FOOTER(y, _args...)\
261 /* foreach y->list->next do SCRIPT_HANDLER_FOOTER(y->list->data, _args); */\
262  SCRIPT_HANDLER_FOOTER(y, _args)
263 
264 #ifdef __cplusplus
265 }
266 #endif
267 
268 #endif