/[cvs]/eggdrop1.9/src/script.c
ViewVC logotype

Contents of /eggdrop1.9/src/script.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.15 - (show annotations) (download) (as text)
Tue Mar 26 01:06:22 2002 UTC (17 years, 8 months ago) by ite
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +0 -0 lines
File MIME type: text/x-chdr
FILE REMOVED
* moved script API to libeggdrop (without using the bot context)
* fixed a wrong function name in perlscript
* do not use the bot context in registry for now

1 #include <stdio.h>
2 #include <string.h> /* strlen() */
3 #include <stdlib.h> /* calloc(), malloc() */
4
5 #include <eggdrop/eggdrop.h>
6 #include "script_api.h"
7 #include "script.h"
8 #include "lib/egglib/mstack.h"
9
10 static Function load_script, link_int, unlink_int, link_str, unlink_str, create_cmd, delete_cmd;
11 static void *load_script_h, *link_int_h, *unlink_int_h, *link_str_h, *unlink_str_h, *create_cmd_h, *delete_cmd_h;
12
13 static mstack_t *script_events;
14
15 int script_load(char *fname);
16 int script_create_cmd_table(script_command_t *table);
17
18 static script_command_t my_script_cmds[] = {
19 {"", "loadscript", script_load, NULL, 1, "s", "filename", SCRIPT_INTEGER, 0},
20 {0}
21 };
22
23 typedef struct {
24 int type;
25 int arg1, arg2, arg3;
26 } script_event_t;
27
28 static void add_event(int type, int arg1, int arg2, int arg3)
29 {
30 script_event_t *event = (script_event_t *)malloc(sizeof(*event));
31 event->type = type;
32 event->arg1 = arg1;
33 event->arg2 = arg2;
34 event->arg3 = arg3;
35 mstack_push(script_events, event);
36 }
37
38 static int my_create_cmd(void *ignore, script_command_t *info)
39 {
40 add_event(SCRIPT_EVENT_CREATE_CMD, (int) info, 0, 0);
41 return(0);
42 }
43
44 static int my_link_int(void *ignore, script_int_t *i, int flags)
45 {
46 add_event(SCRIPT_EVENT_LINK_INT, (int) i, flags, 0);
47 return(0);
48 }
49
50 static int my_link_str(void *ignore, script_str_t *str, int flags)
51 {
52 add_event(SCRIPT_EVENT_LINK_STR, (int) str, flags, 0);
53 return(0);
54 }
55
56 static int my_playback(void *ignore, Function *table)
57 {
58 script_event_t *event;
59 int i, version, max;
60
61 version = (int) *table;
62 if (version != 1) return(0);
63 table++;
64
65 max = (int) *table;
66 table++;
67 for (i = 0; i < script_events->len; i++) {
68 event = (script_event_t *)script_events->stack[i];
69 if (event->type < max && table[event->type]) {
70 (table[event->type])(NULL, event->arg1, event->arg2, event->arg3);
71 }
72 }
73 return(0);
74 }
75
76 static registry_simple_chain_t my_functions[] = {
77 {"script", NULL, 0},
78 {"create cmd", my_create_cmd, 2},
79 {"link int", my_link_int, 3},
80 {"link str", my_link_str, 3},
81 {"playback", my_playback, 2},
82 {0}
83 };
84
85 int script_init(eggdrop_t *egg)
86 {
87 script_events = mstack_new(0);
88 registry_add_simple_chains(egg, my_functions);
89 registry_lookup(egg, "script", "load script", &load_script, &load_script_h);
90 registry_lookup(egg, "script", "link int", &link_int, &link_int_h);
91 registry_lookup(egg, "script", "unlink int", &unlink_int, &unlink_int_h);
92 registry_lookup(egg, "script", "link str", &link_str, &link_str_h);
93 registry_lookup(egg, "script", "unlink str", &unlink_str, &unlink_str_h);
94 registry_lookup(egg, "script", "create cmd", &create_cmd, &create_cmd_h);
95 registry_lookup(egg, "script", "delete cmd", &delete_cmd, &delete_cmd_h);
96
97 script_create_cmd_table(my_script_cmds);
98
99 return(0);
100 }
101
102 int script_load(char *fname)
103 {
104 load_script(load_script_h, fname);
105 return(0);
106 }
107
108 int script_link_int_table(script_int_t *table)
109 {
110 script_int_t *intval;
111
112 for (intval = table; intval->class && intval->name; intval++) {
113 link_int(link_int_h, intval, 0);
114 }
115 return(0);
116
117 }
118
119 int script_unlink_int_table(script_int_t *table)
120 {
121 script_int_t *intval;
122
123 for (intval = table; intval->class && intval->name; intval++) {
124 unlink_int(unlink_int_h, intval);
125 }
126 return(0);
127
128 }
129
130 int script_link_str_table(script_str_t *table)
131 {
132 script_str_t *str;
133
134 for (str = table; str->class && str->name; str++) {
135 link_str(link_str_h, str, 0);
136 }
137 return(0);
138
139 }
140
141 int script_unlink_str_table(script_str_t *table)
142 {
143 script_str_t *str;
144
145 for (str = table; str->class && str->name; str++) {
146 unlink_str(unlink_str_h, str);
147 }
148 return(0);
149
150 }
151
152 int script_create_cmd_table(script_command_t *table)
153 {
154 script_command_t *cmd;
155
156 for (cmd = table; cmd->class && cmd->name; cmd++) {
157 create_cmd(create_cmd_h, cmd);
158 }
159 return(0);
160 }
161
162 int script_delete_cmd_table(script_command_t *table)
163 {
164
165 script_command_t *cmd;
166
167 for (cmd = table; cmd->class && cmd->name; cmd++) {
168 delete_cmd(delete_cmd_h, cmd);
169 }
170 return(0);
171 }
172
173 int script_create_simple_cmd_table(script_simple_command_t *table)
174 {
175 script_command_t *cmd;
176 char *class;
177
178 /* First entry gives the class. */
179 class = table->name;
180 table++;
181
182 while (table->name) {
183 cmd = (script_command_t *)calloc(1, sizeof(*cmd));
184 cmd->class = class;
185 cmd->name = table->name;
186 cmd->callback = table->callback;
187 cmd->nargs = strlen(table->syntax);
188 cmd->syntax = table->syntax;
189 cmd->syntax_error = table->syntax_error;
190 cmd->retval_type = table->retval_type;
191 create_cmd(create_cmd_h, cmd);
192 table++;
193 }
194 return(0);
195 }
196
197 script_var_t *script_string(char *str, int len)
198 {
199 script_var_t *var = (script_var_t *)malloc(sizeof(*var));
200 var->type = SCRIPT_STRING | SCRIPT_FREE_VAR;
201 var->value = (void *)str;
202 if (len < 0) len = strlen(str);
203 var->len = len;
204 return(var);
205 }
206
207 script_var_t *script_int(int val)
208 {
209 script_var_t *var = (script_var_t *)malloc(sizeof(*var));
210 var->type = SCRIPT_INTEGER | SCRIPT_FREE_VAR;
211 var->value = (void *)val;
212 return(var);
213 }
214
215 script_var_t *script_list(int nitems, ...)
216 {
217 script_var_t *list;
218
219 list = (script_var_t *)malloc(sizeof(*list));
220 list->type = SCRIPT_ARRAY | SCRIPT_FREE | SCRIPT_VAR | SCRIPT_FREE_VAR;
221 list->len = nitems;
222 if (nitems > 0) {
223 list->value = malloc(nitems * sizeof(script_var_t *));
224 memmove(list->value, &nitems + 1, nitems * sizeof(script_var_t *));
225 }
226 else list->value = NULL;
227 return(list);
228 }
229
230 int script_list_append(script_var_t *list, script_var_t *item)
231 {
232 list->value = realloc(list->value, sizeof(item) * (list->len+1));
233 ((script_var_t **)list->value)[list->len] = item;
234 list->len++;
235 return(0);
236 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23