-
Notifications
You must be signed in to change notification settings - Fork 17
/
functions.cc
500 lines (422 loc) · 12 KB
/
functions.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/******************************************************************************
Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
#include "my-stdarg.h"
#include "bf_register.h"
#include "config.h"
#include "db_io.h"
#include "functions.h"
#include "list.h"
#include "log.h"
#include "map.h"
#include "server.h"
#include "storage.h"
#include "streams.h"
#include "structures.h"
#include "unparse.h"
#include "utils.h"
/*****************************************************************************
* This is the table of procedures that register MOO built-in functions. To
* add new built-in functions to the server, add to the list below the name of
* a C function that will register your new MOO built-ins; your C function will
* be called exactly once, during server initialization. Also add a
* declaration of that C function to `bf_register.h' and add the necessary .c
* files to the `CSRCS' line in the Makefile.
****************************************************************************/
typedef void (*registry) ();
static registry bi_function_registries[] =
{
#ifdef ENABLE_GC
register_gc,
#endif
register_collection,
register_disassemble,
register_extensions,
register_execute,
register_functions,
register_list,
register_log,
register_map,
register_numbers,
register_objects,
register_property,
register_server,
register_tasks,
register_verbs,
register_yajl,
register_base64,
register_fileio,
register_system,
register_exec,
register_crypto
};
void
register_bi_functions()
{
int loop, num_registries =
sizeof(bi_function_registries) / sizeof(bi_function_registries[0]);
for (loop = 0; loop < num_registries; loop++)
(void) (*(bi_function_registries[loop])) ();
}
/*** register ***/
struct bft_entry {
const char *name;
const char *protect_str;
const char *verb_str;
int minargs;
int maxargs;
var_type *prototype;
bf_type func;
bf_read_type read;
bf_write_type write;
int _protected;
};
static struct bft_entry bf_table[MAX_FUNC];
static unsigned top_bf_table = 0;
static unsigned
register_common(const char *name, int minargs, int maxargs, bf_type func,
bf_read_type read, bf_write_type write, va_list args)
{
int va_index;
int num_arg_types = maxargs == -1 ? minargs : maxargs;
static Stream *s = 0;
if (!s)
s = new_stream(30);
if (top_bf_table == MAX_FUNC) {
errlog("too many functions. %s cannot be registered.\n", name);
return 0;
}
bf_table[top_bf_table].name = str_dup(name);
stream_printf(s, "protect_%s", name);
bf_table[top_bf_table].protect_str = str_dup(reset_stream(s));
stream_printf(s, "bf_%s", name);
bf_table[top_bf_table].verb_str = str_dup(reset_stream(s));
bf_table[top_bf_table].minargs = minargs;
bf_table[top_bf_table].maxargs = maxargs;
bf_table[top_bf_table].func = func;
bf_table[top_bf_table].read = read;
bf_table[top_bf_table].write = write;
bf_table[top_bf_table]._protected = 0;
if (num_arg_types > 0)
bf_table[top_bf_table].prototype =
(var_type *)mymalloc(num_arg_types * sizeof(var_type), M_PROTOTYPE);
else
bf_table[top_bf_table].prototype = 0;
for (va_index = 0; va_index < num_arg_types; va_index++)
bf_table[top_bf_table].prototype[va_index] = (var_type)va_arg(args, int);
return top_bf_table++;
}
unsigned
register_function(const char *name, int minargs, int maxargs,
bf_type func,...)
{
va_list args;
unsigned ans;
va_start(args, func);
ans = register_common(name, minargs, maxargs, func, 0, 0, args);
va_end(args);
return ans;
}
unsigned
register_function_with_read_write(const char *name, int minargs, int maxargs,
bf_type func, bf_read_type read,
bf_write_type write,...)
{
va_list args;
unsigned ans;
va_start(args, write);
ans = register_common(name, minargs, maxargs, func, read, write, args);
va_end(args);
return ans;
}
/*** looking up functions -- by name or num ***/
static const char *func_not_found_msg = "no such function";
const char *
name_func_by_num(unsigned n)
{ /* used by unparse only */
if (n >= top_bf_table)
return func_not_found_msg;
else
return bf_table[n].name;
}
unsigned
number_func_by_name(const char *name)
{ /* used by parser only */
unsigned i;
for (i = 0; i < top_bf_table; i++)
if (!mystrcasecmp(name, bf_table[i].name))
return i;
return FUNC_NOT_FOUND;
}
/*** calling built-in functions ***/
package
call_bi_func(unsigned n, Var arglist, Byte func_pc,
Objid progr, void *vdata)
/* requires arglist.type == TYPE_LIST
call_bi_func will free arglist */
{
struct bft_entry *f;
if (n >= top_bf_table) {
errlog("CALL_BI_FUNC: Unknown function number: %d\n", n);
free_var(arglist);
return no_var_pack();
}
f = bf_table + n;
if (func_pc == 1) { /* check arg types and count *ONLY* for first entry */
int k, max;
Var *args = arglist.v.list;
/*
* Check permissions, if protected
*/
if ((!caller().is_obj() || caller().v.obj != SYSTEM_OBJECT) && f->_protected) {
/* Try calling #0:bf_FUNCNAME(@ARGS) instead */
enum error e = call_verb2(SYSTEM_OBJECT, f->verb_str, Var::new_obj(SYSTEM_OBJECT), arglist, 0);
if (e == E_NONE)
return tail_call_pack();
if (e == E_MAXREC || !is_wizard(progr)) {
free_var(arglist);
return make_error_pack(e == E_MAXREC ? e : E_PERM);
}
}
/*
* Check argument count
* (Can't always check in the compiler, because of @)
*/
if (args[0].v.num < f->minargs
|| (f->maxargs != -1 && args[0].v.num > f->maxargs)) {
free_var(arglist);
return make_error_pack(E_ARGS);
}
/*
* Check argument types
*/
max = (f->maxargs == -1) ? f->minargs : args[0].v.num;
for (k = 0; k < max; k++) {
var_type proto = f->prototype[k];
var_type arg = args[k + 1].type;
if (!(proto == TYPE_ANY
|| (proto == TYPE_NUMERIC && (arg == TYPE_INT
|| arg == TYPE_FLOAT))
|| proto == arg)) {
free_var(arglist);
return make_error_pack(E_TYPE);
}
}
} else if (func_pc == 2 && vdata == &call_bi_func) {
/* This is a return from calling #0:bf_FUNCNAME(@ARGS); return what
* it returned. If it errored, what we do will be ignored.
*/
return make_var_pack(arglist);
}
/*
* do the function
*/
return (*(f->func)) (arglist, func_pc, vdata, progr);
/* f->func is responsible for freeing/using up arglist. */
}
void
write_bi_func_data(void *vdata, Byte f_id)
{
if (f_id >= top_bf_table)
errlog("WRITE_BI_FUNC_DATA: Unknown function number: %d\n", f_id);
else if (bf_table[f_id].write)
(*(bf_table[f_id].write)) (vdata);
}
static Byte *pc_for_bi_func_data_being_read;
Byte *
pc_for_bi_func_data(void)
{
return pc_for_bi_func_data_being_read;
}
int
read_bi_func_data(Byte f_id, void **bi_func_state, Byte * bi_func_pc)
{
pc_for_bi_func_data_being_read = bi_func_pc;
if (f_id >= top_bf_table) {
errlog("READ_BI_FUNC_DATA: Unknown function number: %d\n", f_id);
*bi_func_state = 0;
return 0;
} else if (bf_table[f_id].read) {
*bi_func_state = (*(bf_table[f_id].read)) ();
if (*bi_func_state == 0) {
errlog("READ_BI_FUNC_DATA: Can't read data for %s()\n",
bf_table[f_id].name);
return 0;
}
} else {
*bi_func_state = 0;
/* The following code checks for the easily-detectable case of the
* bug described in the Version 1.8.0p4 entry in ChangeLog.txt.
*/
if (*bi_func_pc == 2 && dbio_input_version == DBV_Float
&& strcmp(bf_table[f_id].name, "eval") != 0) {
oklog("LOADING: Warning: patching bogus return to `%s()'\n",
bf_table[f_id].name);
oklog(" (See 1.8.0p4 ChangeLog.txt entry for details.)\n");
*bi_func_pc = 0;
}
}
return 1;
}
package
make_abort_pack(enum abort_reason reason)
{
package p;
p.kind = package::BI_KILL;
p.u.ret.type = TYPE_INT;
p.u.ret.v.num = reason;
return p;
}
package
make_error_pack(enum error err)
{
return make_raise_pack(err, unparse_error(err), zero);
}
package
make_raise_pack(enum error err, const char *msg, Var value)
{
package p;
p.kind = package::BI_RAISE;
p.u.raise.code.type = TYPE_ERR;
p.u.raise.code.v.err = err;
p.u.raise.msg = str_dup(msg);
p.u.raise.value = value;
return p;
}
package
make_var_pack(Var v)
{
package p;
p.kind = package::BI_RETURN;
p.u.ret = v;
return p;
}
package
no_var_pack(void)
{
return make_var_pack(zero);
}
package
make_call_pack(Byte pc, void *data)
{
package p;
p.kind = package::BI_CALL;
p.u.call.pc = pc;
p.u.call.data = data;
return p;
}
package
tail_call_pack(void)
{
return make_call_pack(0, 0);
}
package
make_suspend_pack(enum error(*proc) (vm, void *), void *data)
{
package p;
p.kind = package::BI_SUSPEND;
p.u.susp.proc = proc;
p.u.susp.data = data;
return p;
}
static Var
function_description(int i)
{
struct bft_entry entry;
Var v, vv;
int j, nargs;
entry = bf_table[i];
v = new_list(4);
v.v.list[1].type = TYPE_STR;
v.v.list[1].v.str = str_ref(entry.name);
v.v.list[2].type = TYPE_INT;
v.v.list[2].v.num = entry.minargs;
v.v.list[3].type = TYPE_INT;
v.v.list[3].v.num = entry.maxargs;
nargs = entry.maxargs == -1 ? entry.minargs : entry.maxargs;
vv = v.v.list[4] = new_list(nargs);
for (j = 0; j < nargs; j++) {
int proto = entry.prototype[j];
vv.v.list[j + 1].type = TYPE_INT;
vv.v.list[j + 1].v.num = proto < 0 ? proto : (proto & TYPE_DB_MASK);
}
return v;
}
static package
bf_function_info(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
unsigned int i;
if (arglist.v.list[0].v.num == 1) {
i = number_func_by_name(arglist.v.list[1].v.str);
if (i == FUNC_NOT_FOUND) {
free_var(arglist);
return make_error_pack(E_INVARG);
}
r = function_description(i);
} else {
r = new_list(top_bf_table);
for (i = 0; i < top_bf_table; i++)
r.v.list[i + 1] = function_description(i);
}
free_var(arglist);
return make_var_pack(r);
}
static void
load_server_protect_function_flags(void)
{
unsigned int i;
for (i = 0; i < top_bf_table; i++) {
bf_table[i]._protected
= server_flag_option(bf_table[i].protect_str, 0);
}
oklog("Loaded protect cache for %d builtin functions\n", i);
}
int32 _server_int_option_cache[SVO__CACHE_SIZE];
void
load_server_options(void)
{
int value;
load_server_protect_function_flags();
# define _BP_DO(PROPERTY, property) \
_server_int_option_cache[SVO_PROTECT_##PROPERTY] \
= server_flag_option("protect_" #property, 0); \
BUILTIN_PROPERTIES(_BP_DO);
# undef _BP_DO
# define _SVO_DO(SVO_MISC_OPTION, misc_option, \
kind, DEFAULT, CANONICALIZE) \
value = server_##kind##_option(#misc_option, DEFAULT); \
CANONICALIZE; \
_server_int_option_cache[SVO_MISC_OPTION] = value; \
SERVER_OPTIONS_CACHED_MISC(_SVO_DO, value);
# undef _SVO_DO
}
static package
bf_load_server_options(Var arglist, Byte next, void *vdata, Objid progr)
{
free_var(arglist);
if (!is_wizard(progr)) {
return make_error_pack(E_PERM);
}
load_server_options();
return no_var_pack();
}
void
register_functions(void)
{
register_function("function_info", 0, 1, bf_function_info, TYPE_STR);
register_function("load_server_options", 0, 0, bf_load_server_options);
}