forked from lnsoso/mudos
/
efuns_main.c
3823 lines (3426 loc) · 82.2 KB
/
efuns_main.c
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
efuns_main.c: this file contains the efunctions called from
inside eval_instruction() in interpret.c. Note: if you are adding
local efunctions that are specific to your driver, you would be better
off adding them to a separate source file. Doing so will make it much
easier for you to upgrade (won't have to patch this file). Be sure
to #include "lpc_incl.h" in that separate source file.
*/
#include "std.h"
#include "efuns_main.h"
#include "file_incl.h"
#include "file.h"
#include "comm.h"
#include "parse.h"
#include "sprintf.h"
#include "backend.h"
#include "port.h"
#include "swap.h"
#include "otable.h"
#include "crc32.h"
#include "reclaim.h"
#include "dumpstat.h"
#include "call_out.h"
#include "ed.h"
#include "md.h"
#include "master.h"
#include "efun_protos.h"
#include "add_action.h"
#ifdef LPC_TO_C
#include "interface.h"
#include "compile_file.h"
#endif
int call_origin = 0;
int data_size PROT((object_t * ob));
void reload_object PROT((object_t * obj));
#ifdef F_ALL_INVENTORY
void
f_all_inventory PROT((void))
{
array_t *vec = all_inventory(sp->u.ob, 0);
free_object(sp->u.ob, "f_all_inventory");
sp->type = T_ARRAY;
sp->u.arr = vec;
}
#endif
#ifdef F_ALLOCATE
void
f_allocate PROT((void))
{
if (st_num_arg == 2) {
(sp-1)->u.arr = allocate_array2((sp-1)->u.number, sp);
pop_stack();
} else {
sp->u.arr = allocate_array(sp->u.number);
}
sp->type = T_ARRAY;
}
#endif
#ifdef F_ALLOCATE_BUFFER
void
f_allocate_buffer PROT((void))
{
buffer_t *buf;
buf = allocate_buffer(sp->u.number);
if (buf) {
pop_stack();
push_refed_buffer(buf);
} else {
assign_svalue(sp, &const0);
}
}
#endif
#ifdef F_ALLOCATE_MAPPING
void
f_allocate_mapping PROT((void))
{
array_t *arr;
if (st_num_arg == 2) {
if ((sp-1)->type != T_ARRAY)
error("Bad argument 1 to allocate_mapping()\n");
if (sp->type == T_ARRAY) {
arr = (sp-1)->u.arr;
if (sp->u.arr->size != arr->size)
error("Arrays passed to allocate_mapping() must be the same size\n");
(sp-1)->u.map = mkmapping(arr, sp->u.arr);
} else {
arr = (sp-1)->u.arr;
(sp-1)->u.map = allocate_mapping2(arr, sp);
}
pop_stack();
free_array(arr);
} else if (sp->type == T_NUMBER) {
sp->u.map = allocate_mapping(sp->u.number);
} else {
error("Bad argument 1 to allocate_mapping()\n");
}
sp->type = T_MAPPING;
}
#endif
#ifdef F_BIND
void
f_bind PROT((void))
{
object_t *ob = sp->u.ob;
funptr_t *old_fp = (sp-1)->u.fp;
funptr_t *new_fp;
svalue_t *res;
if (ob == old_fp->hdr.owner) {
/* no change */
free_object(ob, "bind nop");
sp--;
return;
}
if (old_fp->hdr.type == (FP_LOCAL | FP_NOT_BINDABLE))
error("Illegal to rebind a pointer to a local function.\n");
if (old_fp->hdr.type & FP_NOT_BINDABLE)
error("Illegal to rebind a functional that references globals or local functions.\n");
/* the object doing the binding */
push_object(current_object);
/* the old owner */
push_object(old_fp->hdr.owner);
/* the new owner */
push_object(ob);
res = apply_master_ob(APPLY_VALID_BIND, 3);
if (!MASTER_APPROVED(res))
error("Master object denied permission to bind() function pointer.\n");
new_fp = ALLOCATE(funptr_t, TAG_FUNP, "f_bind");
*new_fp = *old_fp;
new_fp->hdr.ref = 1;
new_fp->hdr.owner = ob; /* one ref from being on stack */
if (new_fp->hdr.args)
new_fp->hdr.args->ref++;
if ((old_fp->hdr.type & 0x0f) == FP_FUNCTIONAL) {
new_fp->f.functional.prog->func_ref++;
debug(d_flag, ("add func ref /%s: now %i\n",
new_fp->f.functional.prog->name,
new_fp->f.functional.prog->func_ref));
}
free_funp(old_fp);
sp--;
sp->u.fp = new_fp;
}
#endif
#ifdef F_CACHE_STATS
static void print_cache_stats P1(outbuffer_t *, ob)
{
outbuf_add(ob, "Function cache information\n");
outbuf_add(ob, "-------------------------------\n");
outbuf_addv(ob, "%% cache hits: %10.2f\n",
100 * ((double) apply_low_cache_hits / apply_low_call_others));
outbuf_addv(ob, "call_others: %10lu\n", apply_low_call_others);
outbuf_addv(ob, "cache hits: %10lu\n", apply_low_cache_hits);
outbuf_addv(ob, "cache size: %10lu\n", APPLY_CACHE_SIZE);
outbuf_addv(ob, "slots used: %10lu\n", apply_low_slots_used);
outbuf_addv(ob, "%% slots used: %10.2f\n",
100 * ((double) apply_low_slots_used / APPLY_CACHE_SIZE));
outbuf_addv(ob, "collisions: %10lu\n", apply_low_collisions);
outbuf_addv(ob, "%% collisions: %10.2f\n",
100 * ((double) apply_low_collisions / apply_low_call_others));
}
void f_cache_stats PROT((void))
{
outbuffer_t ob;
outbuf_zero(&ob);
print_cache_stats(&ob);
outbuf_push(&ob);
}
#endif
#ifdef F__CALL_OTHER
/* enhanced call_other written 930314 by Luke Mewburn <zak@rmit.edu.au> */
void
f__call_other PROT((void))
{
svalue_t *arg;
char *funcname;
int i;
int num_arg = st_num_arg;
object_t *ob;
if (current_object->flags & O_DESTRUCTED) { /* No external calls allowed */
pop_n_elems(num_arg);
push_undefined();
return;
}
arg = sp - num_arg + 1;
if (arg[1].type == T_STRING)
funcname = arg[1].u.string;
else { /* must be T_ARRAY then */
array_t *v = arg[1].u.arr;
svalue_t *sv;
check_for_destr(v);
if (((i = v->size) < 1) || !((sv = v->item)->type == T_STRING))
error("call_other: 1st elem of array for arg 2 must be a string\n");
funcname = sv->u.string;
num_arg = 2 + merge_arg_lists(num_arg - 2, v, 1);
}
if (arg[0].type == T_OBJECT)
ob = arg[0].u.ob;
else if (arg[0].type == T_ARRAY) {
array_t *ret;
ret = call_all_other(arg[0].u.arr, funcname, num_arg - 2);
pop_stack();
free_array(arg->u.arr);
sp->u.arr = ret;
return;
} else {
ob = find_object(arg[0].u.string);
if (!ob || !object_visible(ob))
error("call_other() couldn't find object\n");
}
/* Send the remaining arguments to the function. */
#ifdef TRACE
if (TRACEP(TRACE_CALL_OTHER)) {
do_trace("Call other ", funcname, "\n");
}
#endif
call_origin = ORIGIN_CALL_OTHER;
if (apply_low(funcname, ob, num_arg - 2) == 0) { /* Function not found */
pop_2_elems();
push_undefined();
return;
}
/*
* The result of the function call is on the stack. So is the function
* name and object that was called, though. These have to be removed.
*/
free_svalue(--sp, "f_call_other:1");
free_svalue(--sp, "f_call_other:2");
*sp = *(sp+2);
return;
}
#endif
#ifdef F_CALL_OUT
void
f_call_out PROT((void))
{
svalue_t *arg = sp - st_num_arg + 1;
int num = st_num_arg - 2;
#ifdef CALLOUT_HANDLES
int ret;
if (!(current_object->flags & O_DESTRUCTED)) {
ret = new_call_out(current_object, arg, arg[1].u.number, num, arg + 2);
/* args have been transfered; don't free them;
also don't need to free the int */
sp -= num + 1;
} else {
ret = 0;
pop_n_elems(num);
sp--;
}
/* the function */
free_svalue(sp, "call_out");
put_number(ret);
#else
if (!(current_object->flags & O_DESTRUCTED)) {
new_call_out(current_object, arg, arg[1].u.number, num, arg + 2);
sp -= num + 1;
} else {
pop_n_elems(num);
sp--;
}
free_svalue(sp--, "call_out");
#endif
}
#endif
#ifdef F_CALL_OUT_INFO
void
f_call_out_info PROT((void))
{
push_refed_array(get_all_call_outs());
}
#endif
#if defined(F_CALL_STACK) || defined(F_ORIGIN)
static char *origin_name P1(int, orig) {
/* FIXME: this should use ffs() if available (BSD) */
int i = 0;
static char *origins[] = {
"driver",
"local",
"call_other",
"simul",
"internal",
"efun",
"function pointer",
"functional"
};
while (orig >>= 1) i++;
return origins[i];
}
#endif
#ifdef F_CALL_STACK
void
f_call_stack PROT((void))
{
int i, n = csp - &control_stack[0] + 1;
array_t *ret;
if (sp->u.number < 0 || sp->u.number > 3)
error("First argument of call_stack() must be 0, 1, 2, or 3.\n");
ret = allocate_empty_array(n);
switch (sp->u.number) {
case 0:
ret->item[0].type = T_STRING;
ret->item[0].subtype = STRING_MALLOC;
ret->item[0].u.string = add_slash(current_prog->name);
for (i = 1; i < n; i++) {
ret->item[i].type = T_STRING;
ret->item[i].subtype = STRING_MALLOC;
ret->item[i].u.string = add_slash((csp - i + 1)->prog->name);
}
break;
case 1:
ret->item[0].type = T_OBJECT;
ret->item[0].u.ob = current_object;
add_ref(current_object, "f_call_stack: curr");
for (i = 1; i < n; i++) {
ret->item[i].type = T_OBJECT;
ret->item[i].u.ob = (csp - i + 1)->ob;
add_ref((csp - i + 1)->ob, "f_call_stack");
}
break;
case 2:
for (i = 0; i < n; i++) {
ret->item[i].type = T_STRING;
if (((csp - i)->framekind & FRAME_MASK) == FRAME_FUNCTION) {
program_t *prog = (i ? (csp-i+1)->prog : current_prog);
int index = (csp-i)->fr.table_index;
function_t *cfp = &prog->function_table[index];
ret->item[i].subtype = STRING_SHARED;
ret->item[i].u.string = cfp->name;
ref_string(cfp->name);
} else {
ret->item[i].subtype = STRING_CONSTANT;
ret->item[i].u.string = (((csp - i)->framekind & FRAME_MASK) == FRAME_CATCH) ? "CATCH" : "<function>";
}
}
break;
case 3:
ret->item[0].type = T_STRING;
ret->item[0].subtype = STRING_CONSTANT;
ret->item[0].u.string = origin_name(caller_type);
for (i = 1; i < n; i++) {
ret->item[i].type = T_STRING;
ret->item[i].subtype = STRING_CONSTANT;
ret->item[i].u.string = origin_name((csp-i+1)->caller_type);
}
break;
}
put_array(ret);
}
#endif
#ifdef F_CAPITALIZE
void
f_capitalize PROT((void))
{
if (uislower(sp->u.string[0])) {
unlink_string_svalue(sp);
sp->u.string[0] = toupper((unsigned char)sp->u.string[0]);
}
}
#endif
#ifdef F_CHILDREN
void
f_children PROT((void))
{
array_t *vec;
vec = children(sp->u.string);
free_string_svalue(sp);
put_array(vec);
}
#endif
#ifdef F_CLASSP
void
f_classp PROT((void))
{
if (sp->type == T_CLASS) {
free_class(sp->u.arr);
*sp = const1;
} else {
free_svalue(sp, "f_classp");
*sp = const0;
}
}
#endif
#ifdef F_CLEAR_BIT
void
f_clear_bit PROT((void))
{
char *str;
int len, ind, bit;
if (sp->u.number > MAX_BITS)
error("clear_bit() bit requested : %d > maximum bits: %d\n", sp->u.number, MAX_BITS);
bit = (sp--)->u.number;
if (bit < 0)
error("Bad argument 2 (negative) to clear_bit().\n");
ind = bit / 6;
bit %= 6;
len = SVALUE_STRLEN(sp);
if (ind >= len)
return; /* return first arg unmodified */
unlink_string_svalue(sp);
str = sp->u.string;
if (str[ind] > 0x3f + ' ' || str[ind] < ' ')
error("Illegal bit pattern in clear_bit character %d\n", ind);
str[ind] = ((str[ind] - ' ') & ~(1 << bit)) + ' ';
}
#endif
#ifdef F_CLONEP
void
f_clonep PROT((void))
{
if ((sp->type == T_OBJECT) && (sp->u.ob->flags & O_CLONE)) {
free_object(sp->u.ob, "f_clonep");
*sp = const1;
} else {
free_svalue(sp, "f_clonep");
*sp = const0;
}
}
#endif
#ifdef F__NEW
void
f__new PROT((void))
{
svalue_t *arg = sp - st_num_arg + 1;
object_t *ob;
ob = clone_object(arg->u.string, st_num_arg - 1);
free_string_svalue(sp);
if (ob) {
put_unrefed_undested_object(ob, "f_clone_object");
} else *sp = const0;
}
#endif
#ifdef F_CP
void
f_cp PROT((void))
{
int i;
i = copy_file(sp[-1].u.string, sp[0].u.string);
free_string_svalue(sp--);
free_string_svalue(sp);
put_number(i);
}
#endif
#ifdef F_CRC32
void
f_crc32 PROT((void))
{
int len;
unsigned char *buf;
UINT32 crc;
if (sp->type == T_STRING) {
len = SVALUE_STRLEN(sp);
buf = (unsigned char *) sp->u.string;
#ifndef NO_BUFFER_TYPE
} else if (sp->type == T_BUFFER) {
len = sp->u.buf->size;
buf = sp->u.buf->item;
#endif
} else {
#ifdef NO_BUFFER_TYPE
bad_argument(sp, T_STRING, 1, F_CRC32);
#else
bad_argument(sp, T_STRING | T_BUFFER, 1, F_CRC32);
#endif
}
crc = compute_crc32(buf, len);
free_svalue(sp, "f_crc32");
put_number(crc);
}
#endif
#ifdef F_CTIME
void
f_ctime PROT((void))
{
char *cp, *nl, *p;
int l;
cp = time_string((time_t)sp->u.number);
if ((nl = strchr(cp, '\n')))
l = nl - cp;
else
l = strlen(cp);
p = new_string(l, "f_ctime");
strncpy(p, cp, l);
p[l] = '\0';
put_malloced_string(p);
}
#endif
#ifdef F_DEEP_INHERIT_LIST
void
f_deep_inherit_list PROT((void))
{
array_t *vec;
if (!(sp->u.ob->flags & O_SWAPPED)) {
vec = deep_inherit_list(sp->u.ob);
} else {
vec = &the_null_array;
}
free_object(sp->u.ob, "f_deep_inherit_list");
put_array(vec);
}
#endif
#ifdef DEBUG_MACRO
#ifdef F_SET_DEBUG_LEVEL
void
f_set_debug_level PROT((void))
{
if (sp->type == T_STRING) {
debug_level_set(sp->u.string);
free_string_svalue(sp--);
} else
debug_level = (sp--)->u.number;
}
void
f_clear_debug_level PROT((void)) {
debug_level_clear(sp->u.string);
}
void
f_debug_levels PROT((void)) {
/* not in debug.h since debug.h is included in many places that don't
know about mapping_t */
mapping_t *debug_levels PROT((void));
push_refed_mapping(debug_levels());
}
#endif
#endif
#ifdef F_DEEP_INVENTORY
void
f_deep_inventory PROT((void))
{
array_t *vec;
vec = deep_inventory(sp->u.ob, 0);
free_object(sp->u.ob, "f_deep_inventory");
put_array(vec);
}
#endif
#ifdef F_DESTRUCT
void
f_destruct PROT((void))
{
destruct_object(sp->u.ob);
sp--; /* Ok since the object was removed from the stack */
}
#endif
#ifdef F_DUMPALLOBJ
void
f_dumpallobj PROT((void))
{
if (st_num_arg) {
dumpstat(sp->u.string);
free_string_svalue(sp--);
} else {
dumpstat("/OBJ_DUMP");
}
}
#endif
#ifdef F_ED
void
f_ed PROT((void))
{
if (!command_giver || !command_giver->interactive) {
pop_n_elems(st_num_arg);
return;
}
if (!st_num_arg) {
/* ed() */
ed_start(0, 0, 0, 0, 0);
} else if (st_num_arg == 1) {
/* ed(fname) */
ed_start(sp->u.string, 0, 0, 0, 0);
pop_stack();
} else if (st_num_arg == 2) {
/* ed(fname,exitfn) */
ed_start((sp - 1)->u.string, 0, sp->u.string, 0, current_object);
pop_2_elems();
} else if (st_num_arg == 3) {
/* ed(fname,exitfn,restricted) / ed(fname,writefn,exitfn) */
if (sp->type == T_NUMBER) {
ed_start((sp - 2)->u.string, 0, (sp - 1)->u.string, sp->u.number,
current_object);
} else if (sp->type == T_STRING) {
ed_start((sp - 2)->u.string, (sp - 1)->u.string, sp->u.string, 0,
current_object);
} else {
bad_argument(sp, T_NUMBER | T_STRING, 3, F_ED);
}
pop_3_elems();
} else { /* st_num_arg == 4 */
/* ed(fname,writefn,exitfn,restricted) */
if (!((sp - 1)->type == T_STRING))
bad_argument(sp - 1, T_STRING, 3, F_ED);
if (!(sp->type == T_NUMBER))
bad_argument(sp, T_NUMBER, 4, F_ED);
ed_start((sp - 3)->u.string, (sp - 2)->u.string, (sp - 1)->u.string,
sp->u.number, current_object);
pop_n_elems(4);
}
}
#endif
#ifdef F_ED_CMD
void f_ed_cmd PROT((void))
{
char *res;
if (current_object->flags & O_DESTRUCTED)
error("destructed objects can't use ed.\n");
if (!(current_object->flags & O_IN_EDIT))
error("ed_cmd() called with no ed session active.\n");
res = object_ed_cmd(current_object, sp->u.string);
free_string_svalue(sp);
if (res) {
sp->subtype = STRING_MALLOC;
sp->u.string = res;
} else {
sp->subtype = STRING_CONSTANT;
sp->u.string = "";
}
}
#endif
#ifdef F_ED_START
void f_ed_start PROT((void))
{
char *res;
char *fname;
int restr = 0;
if (st_num_arg == 2)
restr = (sp--)->u.number;
if (st_num_arg)
fname = sp->u.string;
else
fname = 0;
if (current_object->flags & O_DESTRUCTED)
error("destructed objects can't use ed.\n");
if (current_object->flags & O_IN_EDIT)
error("ed_start() called while an ed session is already started.\n");
res = object_ed_start(current_object, fname, restr);
if (fname) free_string_svalue(sp);
else {
STACK_INC;
sp->type = T_STRING;
}
if (res) {
sp->subtype = STRING_MALLOC;
sp->u.string = res;
} else {
sp->subtype = STRING_CONSTANT;
sp->u.string = "";
}
}
#endif
#ifdef F_ENABLE_WIZARD
void
f_enable_wizard PROT((void))
{
if (current_object->interactive)
current_object->flags |= O_IS_WIZARD;
}
#endif
#ifdef F_ERROR
void
f_error PROT((void))
{
int l = SVALUE_STRLEN(sp);
char err_buf[2048];
if (sp->u.string[l - 1] == '\n')
l--;
if (l > 2045) l = 2045;
err_buf[0] = '*';
strncpy(err_buf + 1, sp->u.string, l);
err_buf[l + 1] = '\n';
err_buf[l + 2] = 0;
error_handler(err_buf);
}
#endif
#ifdef F_DISABLE_WIZARD
void
f_disable_wizard PROT((void))
{
if (current_object->interactive)
current_object->flags &= ~O_IS_WIZARD;
}
#endif
#ifdef F_ENVIRONMENT
void
f_environment PROT((void))
{
object_t *ob;
if (st_num_arg) {
if ((ob = sp->u.ob)->flags & O_DESTRUCTED)
error("environment() of destructed object.\n");
ob = ob->super;
free_object((sp--)->u.ob, "f_environment");
} else if (!(current_object->flags & O_DESTRUCTED))
ob = current_object->super;
else error("environment() of destructed object.\n");
if (ob && object_visible(ob)) push_object(ob);
else push_number(0);
}
#endif
#ifdef F_EXEC
void
f_exec PROT((void))
{
int i;
i = replace_interactive((sp - 1)->u.ob, sp->u.ob);
/* They might have been destructed */
if (sp->type == T_OBJECT)
free_object(sp->u.ob, "f_exec:1");
if ((--sp)->type == T_OBJECT)
free_object(sp->u.ob, "f_exec:2");
put_number(i);
}
#endif
#ifdef F_EXPLODE
void
f_explode PROT((void))
{
array_t *vec;
vec = explode_string((sp - 1)->u.string, SVALUE_STRLEN(sp-1),
sp->u.string, SVALUE_STRLEN(sp));
free_string_svalue(sp--);
free_string_svalue(sp);
put_array(vec);
}
#endif
#ifdef F_FILE_NAME
void
f_file_name PROT((void))
{
char *res;
/* This function now returns a leading '/' */
res = (char *) add_slash(sp->u.ob->name);
free_object(sp->u.ob, "f_file_name");
put_malloced_string(res);
}
#endif
#ifdef F_FILE_SIZE
void
f_file_size PROT((void))
{
int i = file_size(sp->u.string);
free_string_svalue(sp);
put_number(i);
}
#endif
#ifdef F_FILTER
void
f_filter PROT((void))
{
svalue_t *arg = sp - st_num_arg + 1;
if (arg->type == T_MAPPING) filter_mapping(arg, st_num_arg);
else if (arg->type == T_STRING) filter_string(arg, st_num_arg);
else filter_array(arg, st_num_arg);
}
#endif
#ifdef F_FIND_CALL_OUT
void
f_find_call_out PROT((void))
{
int i;
#ifdef CALLOUT_HANDLES
if (sp->type == T_NUMBER) {
i = find_call_out_by_handle(sp->u.number);
} else { /* T_STRING */
#endif
i = find_call_out(current_object, sp->u.string);
free_string_svalue(sp);
#ifdef CALLOUT_HANDLES
}
#endif
put_number(i);
}
#endif
#ifdef F_FIND_OBJECT
void
f_find_object PROT((void))
{
object_t *ob;
if ((sp--)->u.number)
ob = find_object(sp->u.string);
else
ob = find_object2(sp->u.string);
free_string_svalue(sp);
if (ob && object_visible(ob)) {
/* find_object only returns undested objects */
put_unrefed_undested_object(ob, "find_object");
} else
*sp = const0;
}
#endif
#ifdef F_FUNCTION_PROFILE
/* f_function_profile: John Garnett, 1993/05/31, 0.9.17.3 */
void
f_function_profile PROT((void))
{
array_t *vec;
mapping_t *map;
program_t *prog;
int nf, j;
object_t *ob;
ob = sp->u.ob;
if (ob->flags & O_SWAPPED) {
load_ob_from_swap(ob);
}
prog = ob->prog;
nf = prog->num_functions_defined;
vec = allocate_empty_array(nf);
for (j = 0; j < nf; j++) {
map = allocate_mapping(3);
add_mapping_pair(map, "calls", prog->function_table[j].calls);
add_mapping_pair(map, "self", prog->function_table[j].self
- prog->function_table[j].children);
add_mapping_pair(map, "children", prog->function_table[j].children);
add_mapping_shared_string(map, "name", prog->function_table[j].name);
vec->item[j].type = T_MAPPING;
vec->item[j].u.map = map;
}
free_object(ob, "f_function_profile");
put_array(vec);
}
#endif
#ifdef F_FUNCTION_EXISTS
void
f_function_exists PROT((void))
{
char *str, *res;
int l;
object_t *ob;
int flag = 0;
if (st_num_arg > 1) {
if (st_num_arg > 2)
flag = (sp--)->u.number;
ob = (sp--)->u.ob;
free_object(ob, "f_function_exists");
} else {
if (current_object->flags & O_DESTRUCTED) {
free_string_svalue(sp);
*sp = const0;
return;
}
ob = current_object;
}
str = function_exists(sp->u.string, ob, flag);
free_string_svalue(sp);
if (str) {
l = SHARED_STRLEN(str) - 2; /* no .c */
res = new_string(l + 1, "function_exists");
res[0] = '/';
strncpy(res + 1, str, l);
res[l + 1] = 0;
sp->subtype = STRING_MALLOC;
sp->u.string = res;
} else *sp = const0;
}
#endif
#ifdef F_GENERATE_SOURCE
void f_generate_source PROT((void))
{
int i;
if (st_num_arg == 2) {
i = generate_source(sp - 1, sp->u.string);
pop_stack();
} else
i = generate_source(sp, 0);
free_svalue(sp, "f_generate_source");
put_number(i);
}
#endif
#ifdef F_GET_CHAR
void
f_get_char PROT((void))
{
svalue_t *arg;
int i, tmp;
int flag;
arg = sp - st_num_arg + 1; /* Points arg at first argument. */
if (st_num_arg == 1 || !(arg[1].type == T_NUMBER)) {
tmp = 0;
flag = 0;
} else {
tmp = 1;
st_num_arg--; /* Don't count the flag as an arg */
flag = arg[1].u.number;
}
st_num_arg--;
i = get_char(arg, flag, st_num_arg, &arg[1 + tmp]);
free_svalue(arg, "f_get_char");
(sp = arg)->type = T_NUMBER;
sp->u.number = i;
}