-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimulate.c
More file actions
2376 lines (2083 loc) · 69.5 KB
/
Copy pathsimulate.c
File metadata and controls
2376 lines (2083 loc) · 69.5 KB
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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#define SUPPRESS_COMPILER_INLINES
#include "std.h"
#include "rc/rc.h"
#include "command.h"
#include "frame.h"
#include "interpret.h"
#include "addr_resolver.h"
#include "main.h"
#include "simulate.h"
#include "simul_efun.h"
#include "efuns/uids.h"
#include "lpc/array.h"
#include "lpc/functional.h"
#include "lpc/mapping.h"
#include "lpc/object.h"
#include "lpc/operator.h"
#include "lpc/otable.h"
#include "lpc/program.h"
#include "lpc/program/disassemble.h"
#include "lpc/program/binaries.h"
#include "lpc/include/origin.h"
#include "lpc/include/runtime_config.h"
#include "misc/filepath.h"
#include "socket/socket_efuns.h"
#ifdef HAVE_CURL
#include "curl/curl_efuns.h"
#endif
#include "call_out.h"
#include "ed.h"
#include <sys/stat.h>
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif /* HAVE_STDARG_H */
/* -1 indicates that we have never had a master object. This is so the
* simul_efun object can load before the master. */
object_t *master_ob = 0;
object_t *obj_list, *obj_list_destruct;
replace_ob_t *obj_list_replace = 0;
object_t *current_object; /* The object interpreting a function. */
object_t *previous_ob; /* The object that called the current_object. */
object_t *command_giver; /* Where the current command came from. */
object_t *current_interactive; /* The user who caused this execution */
static int give_uid_to_object (object_t *);
static int init_object (object_t *);
static object_t *load_virtual_object (const char *);
static char *make_new_name (const char *);
static void send_say (object_t *, const char *, array_t *);
/*********************************************************************/
/**
* @brief Check that a string is legal for printing.
*
* If the string is too long, an error is raised.
* @param s The string to check.
*/
static void check_legal_string (const char *s) {
if (strlen (s) >= LARGEST_PRINTABLE_STRING)
{
error ("*Printable strings limited to length of %d.\n",
LARGEST_PRINTABLE_STRING);
}
}
/**
* @brief Give the correct uid and euid to a created object.
*
* An object must have a uid. The euid may be NULL.
*/
static int give_uid_to_object (object_t * ob) {
svalue_t *ret;
const char *creator_name = NULL;
/* before master object is loaded */
if (mud_state() < MS_MUDLIB_LIMBO)
{
ob->uid = add_uid ("NONAME");
ob->euid = NULL;
return 1;
}
/* ask master object who the creator of this object is */
push_malloced_string (add_slash (ob->name));
ret = APPLY_SLOT_MASTER_CALL (APPLY_CREATOR_FILE, 1);
if (ret == (svalue_t *) - 1)
{
APPLY_SLOT_FINISH_CALL();
destruct_object (ob);
error ("*Can't load objects without a master object.");
return 1;
}
if (ret && ret->type == T_STRING)
creator_name = SVALUE_STRPTR(ret);
if (!creator_name)
creator_name = "NONAME";
/*
* Now we are sure that we have a creator name. It is a stack slot that lives
* until APPLY_SLOT_FINISH_CALL() below, so it is safe to refer to it after the call.
*/
if (current_object)
{
if (current_object->uid && strcmp (current_object->uid->name, creator_name) == 0)
{
/*
* The loaded object has the same uid as the loader.
*/
ob->uid = current_object->uid;
opt_info (2, "object /%s is granted uid \"%s\" by creator /%s.", ob->name, ob->uid->name, current_object->name);
APPLY_SLOT_FINISH_CALL();
return 1;
}
#ifdef AUTO_TRUST_BACKBONE
if (backbone_uid && !strcmp (backbone_uid->name, creator_name))
{
/*
* The object is loaded from backbone. This is trusted, so we let it
* inherit the value of eff_user.
*/
ob->uid = current_object->euid;
ob->euid = current_object->euid;
opt_info (2, "object /%s is granted uid and euid \"%s\" by backbone.", ob->name, ob->uid->name);
APPLY_SLOT_FINISH_CALL();
return 1;
}
#endif
}
/*
* The object is not loaded from backbone, nor from from the loading
* objects path. That should be an object defined by another wizard. It
* can't be trusted, so we give it the same uid as the creator. Also give
* it eff_user 0, which means that user 'a' can't use objects from user
* 'b' to load new objects nor modify files owned by user 'b'.
*
* If this effect is wanted, user 'b' must let his object do 'seteuid()' to
* himself. That is the case for most rooms.
*/
ob->uid = add_uid (creator_name);
ob->euid = NULL;
opt_info (2, "object /%s is granted uid \"%s\".", ob->name, ob->uid->name);
APPLY_SLOT_FINISH_CALL();
return 1;
}
static int init_object (object_t * ob) {
return give_uid_to_object (ob);
}
static object_t *load_virtual_object (const char *name) {
svalue_t *v;
object_t *result = 0;
if (mud_state() < MS_MUDLIB_LIMBO)
return 0;
push_malloced_string (add_slash (name));
v = APPLY_SLOT_MASTER_CALL (APPLY_COMPILE_OBJECT, 1);
if (v && (v->type == T_OBJECT))
result = v->u.ob;
APPLY_SLOT_FINISH_CALL();
return result;
}
/**
* @brief Set the master object.
*
* This function sets the master object for the MUD driver.
* It also retrieves and assigns the root and backbone user IDs
*
* @param ob The new master object.
*/
void set_master (object_t * ob) {
int first_load = (!master_ob);
svalue_t *ret;
const char *uid = NULL;
if (ob && ob->flags & O_DESTRUCTED)
error ("Bad master object\n");
if (master_ob)
{
/* release reference to the old master_ob */
assert (master_ob->ref > 1);
free_object (master_ob, "set_master");
}
if (!(master_ob = ob))
return;
/* Make sure master_ob is never made a dangling pointer. */
add_ref (master_ob, "set_master");
opt_trace (TT_EVAL|1, "master object ref = %d", master_ob->ref);
ret = APPLY_SLOT_MASTER_CALL (APPLY_GET_ROOT_UID, 0);
if (ret && (ret->type == T_STRING))
uid = SVALUE_STRPTR(ret);
if (first_load)
{
if (uid)
{
master_ob->uid = set_root_uid (uid);
master_ob->euid = master_ob->uid;
}
APPLY_SLOT_FINISH_CALL();
/* The backbone UID is set only when the master object is first loaded.
* If the master object changes later, the backbone UID remains the same
* because there could be already objects created with that UID.
*
* Retain the original backbone UID to allow new objects created by
* backbone (as indicated by creator_file) to receive UID and EUID of
* current_object.
*/
ret = APPLY_SLOT_MASTER_CALL (APPLY_GET_BACKBONE_UID, 0);
if (ret && (ret->type == T_STRING))
set_backbone_uid (SVALUE_STRPTR(ret));
APPLY_SLOT_FINISH_CALL();
}
else if (uid)
{
master_ob->uid = add_uid (uid);
master_ob->euid = master_ob->uid;
APPLY_SLOT_FINISH_CALL();
}
else
{
APPLY_SLOT_FINISH_CALL();
}
}
/**
* @brief Strip leading slashes and check for double slashes in a file name.
*/
static const char *strip_and_check_name (const char *src) {
const char *p;
while (*src == '/')
src++; /* strip leading slashes */
p = src;
while (*p)
{
if (*p == '/' && *(p + 1) == '/')
return 0; /* double slash not allowed */
p++;
}
return src;
}
/* prevents infinite inherit loops.
No, mark-and-sweep solution won't work. Exercise for reader. */
static int num_objects_this_thread = 0;
void reset_load_object_limits() {
num_objects_this_thread = 0;
}
/**
* @brief Load an object definition from file. If the object wants to inherit
* from an object that is not loaded, discard all, load the inherited object,
* and reload again.
*
* In LPMud 3.0 when loading inherited objects, their reset() is not called.
* - why is this?? it makes no sense and causes a problem when a developer
* inherits code from a real used item. Say a room for example. In this case
* the room is loaded but is never set up properly, so when someone enters it
* it's all messed up. Realistically, I know that it's pretty bad style to
* inherit from an object that's actually being used and isn't just a building
* block, but I see no reason for this limitation. It happens, and when it
* does occur, produces mysterious results than can be hard to track down.
* for now, I've reenabled resetting. We'll see if anything breaks. -WF
*
* Save the command_giver, because reset() in the new object might change it.
*
* @param name_or_path The otable name or path of the object to load. Leading slashes
* are stripped. Extension ".c" is added if not present. Nested paths
* supported (e.g., "path/to/object.c"). The resulting object_t.name
* strips leading slashes and ".c" extension.
* @param pre_text [NEOLITH-EXTENSION] Optional LPC source code to compile.
* If NULL, a source file is required. If non-NULL, compiles from this
* string and the source file becomes optional. This enables unit
* testing without filesystem scaffolding.
* Example: load_object("test.c", "void create() { }\n");
* @return The loaded object, or NULL if it could not be loaded.
*
* @note Object naming: "user.c" → name="user", "path/to/obj.c" → name="path/to/obj"
* @note Requires master_ob to be initialized first (via init_master()).
* @note Enforces inherit chain depth limit (__INHERIT_CHAIN_SIZE__ config).
*/
object_t* load_object (const char *name_or_path, const char *pre_text) {
int f;
program_t *prog = NULL;
object_t *ob, *save_command_giver = command_giver;
svalue_t *mret;
struct stat c_st;
char source_file[PATH_MAX], otable_name[PATH_MAX - 2];
char source_path[PATH_MAX];
const char *mudlib_dir;
if (++num_objects_this_thread > CONFIG_INT (__INHERIT_CHAIN_SIZE__))
error ("*Inherit chain too deep: > %d when trying to load '%s'.", CONFIG_INT (__INHERIT_CHAIN_SIZE__), name_or_path);
if (mud_state() >= MS_MUDLIB_LIMBO)
{
if (current_object && current_object != master_ob && current_object->euid == NULL)
error ("*Can't load objects when no effective user.");
}
/* Canonicalize object name and the file path (sandboxing) */
if (!make_otable_name (name_or_path, otable_name, sizeof (otable_name)))
error ("*Filenames with consecutive /'s in them aren't allowed (%s).", name_or_path);
memset (source_file, 0, sizeof (source_file));
(void) strncpy (source_file, otable_name, sizeof(source_file) - 1);
(void) strncat (source_file, ".c", sizeof(source_file) - strlen(source_file) - 1);
/* Reject illegal path names before any filesystem or virtual-object lookup. */
if (!legal_path (source_file))
{
debug_message ("Illegal pathname: /%s\n", source_file);
error ("*Illegal path name '/%s'.", source_file);
return 0;
}
opt_trace (TT_COMPILE|2, "legal_path passed: \"%s\"", source_file);
/* Source file I/O must be anchored to a verified absolute mudlib directory.
* CWD-dependent relative fallback is deprecated and rejected.
*/
mudlib_dir = MAIN_OPTION(mudlib_dir_absolute);
if (!(mudlib_dir && *mudlib_dir))
{
error ("*Cannot load '/%s' without an initialized mudlib_dir_absolute.", source_file);
}
if (!filepath_join (mudlib_dir, source_file, source_path, sizeof (source_path)))
{
error ("*Source file path too long for '/%s'.", source_file);
}
opt_trace(TT_COMPILE|1, "load_object: \"%s\"", source_file);
if (stat (source_path, &c_st) == -1)
{
if ((ob = load_virtual_object (otable_name)))
{
/* A virtual object is returned by the master object.
* We don't care about its actual filename, just the object.
* Replace the object's name with the requested name and update it in the object hash table.
*/
remove_object_hash (ob);
if (ob->name)
FREE (ob->name);
ob->name = alloc_cstring (otable_name, "load_object");
enter_object_hash (ob);
ob->flags |= O_VIRTUAL;
ob->load_time = current_time;
num_objects_this_thread--;
return ob;
}
else if (!pre_text)
{
num_objects_this_thread--;
return 0;
}
}
/* Get the program by loading from binary or compiling from the source.
* Skip binary load if pre_text is provided, since the cached binary was compiled
* without the injected code.
*/
if (!pre_text)
prog = load_binary (source_file, 0);
if (!prog && !inherit_file)
{
opt_trace (TT_COMPILE|2, "no binary found, compiling: \"%s\"", source_file);
/* maybe move this section into compile_file? */
#ifdef _WIN32
f = FILE_OPEN (source_path, O_RDONLY | O_TEXT);
#else
f = FILE_OPEN (source_path, O_RDONLY);
#endif
if (f == -1 && !pre_text) /* [NEOLITH-EXTENSION] if pre_text is specified, the source file is optional */
{
debug_perror ("open()", source_path);
error ("*Could not read the file '/%s'.", source_file);
}
/* compile LPC program from the source, optionally using pre_text */
prog = compile_file (f, source_file, pre_text);
update_compile_av (total_lines);
total_lines = 0;
if (f != -1)
FILE_CLOSE (f);
}
/* Sorry, can't handle objects without programs yet. */
if (inherit_file == 0 && (num_parse_error > 0 || prog == 0))
{
if (prog)
free_prog (prog, 1);
if (num_parse_error == 0 && prog == 0)
error ("*No program in object '/%s'!", otable_name);
error ("*Error in loading object '/%s':", otable_name);
}
/*
* This is an iterative process. If this object wants to inherit an
* unloaded object, then discard current object, load the object to be
* inherited and reload the current object again. The global variable
* "inherit_file" will be set by grammar.y to point to a file name.
*/
if (inherit_file)
{
object_t *inh_obj;
char inhbuf[MAX_OBJECT_NAME_SIZE];
if (!strip_name (inherit_file, inhbuf, sizeof inhbuf))
strcpy (inhbuf, inherit_file);
FREE (inherit_file);
inherit_file = 0;
if (prog)
{
free_prog (prog, 1);
prog = 0;
}
if (strcmp (inhbuf, otable_name) == 0)
{
error ("*Illegal to inherit self.");
}
if ((inh_obj = lookup_object_hash (inhbuf)))
{
IF_DEBUG (fatal ("*****Inherited object is already loaded!"));
}
else
{
opt_trace (TT_COMPILE|2, "loading inherit file: /%s", inhbuf);
inh_obj = load_object (inhbuf, 0);
}
if (!inh_obj)
error ("*Inherited file '/%s' does not exist!", inhbuf);
/*
* Yes, the following is necessary. It is possible that when we
* loaded the inherited object, it loaded this object from it's
* create function. Without this check, that would crash the driver.
* -Beek
*/
if (!(ob = lookup_object_hash (otable_name)))
{
ob = load_object (otable_name, 0);
/* sigh, loading the inherited file removed us */
if (!ob)
{
num_objects_this_thread--;
return 0;
}
ob->load_time = current_time;
}
num_objects_this_thread--;
return ob;
}
opt_trace (TT_COMPILE|2, "creating object: \"/%s\"", otable_name);
ob = get_empty_object (prog->num_variables_total);
/* Shared string is no good here */
ob->name = alloc_cstring (otable_name, "load_object");
ob->prog = prog;
ob->flags |= O_WILL_RESET; /* must be before reset is first called */
ob->next_all = obj_list;
obj_list = ob;
opt_trace (TT_COMPILE|2, "adding to otable: \"%s\"", otable_name);
enter_object_hash (ob); /* add name to fast object lookup table */
if (mud_state() >= MS_MUDLIB_LIMBO)
{
opt_trace (TT_COMPILE|3, "calling master apply: valid_object() for: \"%s\"", otable_name);
push_object (ob);
mret = APPLY_SLOT_MASTER_CALL (APPLY_VALID_OBJECT, 1);
if (mret && !MASTER_APPROVED (mret))
{
APPLY_SLOT_FINISH_CALL();
destruct_object (ob);
error ("*master::%s() denied permission to load '/%s'.", APPLY_VALID_OBJECT, otable_name);
}
APPLY_SLOT_FINISH_CALL();
}
if (init_object (ob))
{
opt_trace (TT_COMPILE|3, "calling object create(): \"%s\"", otable_name);
call_create (ob, 0);
}
if (!(ob->flags & O_DESTRUCTED) && function_exists (APPLY_CLEAN_UP, ob, 1))
{
ob->flags |= O_WILL_CLEAN_UP;
}
command_giver = save_command_giver;
ob->load_time = current_time;
num_objects_this_thread--;
return ob;
}
/**
* @brief Create a new name for a cloned object by appending #number to the original name.
*
* The number is incremented with each call to this function.
* @param str The original object name.
* @return A new string with the cloned object name.
*/
static char *make_new_name (const char *str) {
static int i = 1;
char *p = DXALLOC (strlen (str) + 10, TAG_OBJ_NAME, "make_new_name");
(void) snprintf (p, strlen (str) + 10, "%s#%d", str, i);
i++;
return p;
}
/*
* Save the command_giver, because reset() in the new object might change
* it.
*/
object_t *clone_object (const char *str1, int num_arg) {
object_t *ob, *new_ob;
object_t *save_command_giver = command_giver;
if (current_object && current_object->euid == 0)
{
if (current_object != master_ob)
error ("*Attempt to create object without effective UID.");
}
num_objects_this_thread = 0;
ob = find_or_load_object (str1);
if (ob && !object_visible (ob))
ob = 0;
/*
* If the object self-destructed...
*/
if (ob == 0)
{ /* fix from 3.1.1 */
pop_n_elems (num_arg);
return (0);
}
if (ob->flags & O_CLONE)
{
if (!(ob->flags & O_VIRTUAL) || strrchr (str1, '#'))
error ("*Cannot clone from a clone!");
else
{
/*
* well... it's a virtual object. So now we're going to "clone"
* it.
*/
pop_n_elems (num_arg); /* possibly this should be smarter */
/* but then, this whole section is a
kludge and should be looked at.
Note that create() never gets called
in clones of virtual objects.
-Beek */
if (!(str1 = strip_and_check_name (str1)))
error ("*Filenames with consecutive /'s in them aren't allowed (%s).", str1);
if (ob->ref == 1 && !ob->super && !ob->contains)
{
/*
* ob unused so reuse it instead to save space. (possibly
* loaded just for cloning)
*/
new_ob = ob;
}
else
{
/* can't reuse, so load another */
if (!(new_ob = load_virtual_object (str1)))
return 0;
}
remove_object_hash (new_ob);
if (new_ob->name)
FREE (new_ob->name);
/* Now set the file name of the specified object correctly... */
new_ob->name = make_new_name (str1);
enter_object_hash (new_ob);
new_ob->flags |= O_VIRTUAL;
new_ob->load_time = current_time;
command_giver = save_command_giver;
return (new_ob);
/*
* we can skip all of the stuff below since we were already
* cloned once to have gotten to this stage.
*/
}
}
/* We do not want the heart beat to be running for unused copied objects */
if (ob->flags & O_HEART_BEAT)
(void) set_heart_beat (ob, 0);
new_ob = get_empty_object (ob->prog->num_variables_total);
new_ob->name = make_new_name (ob->name);
opt_trace (TT_MEMORY|3, "clone object name: \"/%s\"", new_ob->name);
new_ob->flags |= (O_CLONE | (ob->flags & (O_WILL_CLEAN_UP | O_WILL_RESET)));
new_ob->load_time = ob->load_time;
new_ob->prog = ob->prog;
reference_prog (ob->prog, "clone_object");
DEBUG_CHECK (!current_object, "clone_object() from no current_object !\n");
init_object (new_ob);
new_ob->next_all = obj_list;
obj_list = new_ob;
opt_info (1, "cloning object /%s", obj_list->name);
enter_object_hash (new_ob); /* Add name to fast object lookup table */
call_create (new_ob, num_arg);
command_giver = save_command_giver;
/* Never know what can happen ! :-( */
if (new_ob->flags & O_DESTRUCTED)
return (0);
return (new_ob);
}
static void replace_programs () {
replace_ob_t *r_ob, *r_next;
int i, num_fewer, offset;
svalue_t *svp;
for (r_ob = obj_list_replace; r_ob; r_ob = r_next)
{
program_t *old_prog;
num_fewer =
r_ob->ob->prog->num_variables_total -
r_ob->new_prog->num_variables_total;
tot_alloc_object_size -= num_fewer * sizeof (svalue_t[1]);
if ((offset = r_ob->var_offset))
{
svp = r_ob->ob->variables;
/* move our variables up to the top */
for (i = 0; i < r_ob->new_prog->num_variables_total; i++)
{
free_svalue (svp, "replace_programs");
*svp = *(svp + offset);
*(svp + offset) = const0u;
svp++;
}
/* free the rest */
for (i = 0; i < num_fewer; i++)
{
free_svalue (svp, "replace_programs");
*svp++ = const0u;
}
}
else
{
/* We just need to remove the last num_fewer variables */
svp = &r_ob->ob->variables[r_ob->new_prog->num_variables_total];
for (i = 0; i < num_fewer; i++)
{
free_svalue (svp, "replace_programs");
*svp++ = const0u;
}
}
r_ob->new_prog->ref++;
old_prog = r_ob->ob->prog;
r_ob->ob->prog = r_ob->new_prog;
r_next = r_ob->next;
free_prog (old_prog, 1);
FREE ((char *) r_ob);
}
obj_list_replace = (replace_ob_t *) 0;
}
object_t* environment (svalue_t * arg) {
object_t *ob = current_object;
if (arg && arg->type == T_OBJECT)
ob = arg->u.ob;
if (ob == 0 || ob->super == 0 || (ob->flags & O_DESTRUCTED))
return 0;
if (ob->flags & O_DESTRUCTED)
error ("*environment() of destructed object.");
return ob->super;
}
/*
* With no argument, present() looks in the inventory of the current_object,
* the inventory of our super, and our super.
* If the second argument is nonzero, only the inventory of that object
* is searched.
*/
static object_t *object_present2 (const char *, object_t *);
object_t* object_present (svalue_t * v, object_t * ob) {
svalue_t *ret;
object_t *ret_ob;
int specific = 0;
if (ob == 0)
ob = current_object;
else
specific = 1;
if (ob->flags & O_DESTRUCTED)
return 0;
if (v->type == T_OBJECT)
{
if (specific)
{
if (v->u.ob->super == ob)
return v->u.ob;
else
return 0;
}
if (v->u.ob->super == ob ||
(v->u.ob->super == ob->super && ob->super != 0))
return v->u.ob->super;
return 0;
}
ret_ob = object_present2 (SVALUE_STRPTR(v), ob->contains);
if (ret_ob)
return ret_ob;
if (specific)
return 0;
if (ob->super)
{
push_svalue (v);
ret = APPLY_SLOT_CALL (APPLY_ID, ob->super, 1, ORIGIN_DRIVER);
if (ob->super->flags & O_DESTRUCTED)
{
APPLY_SLOT_FINISH_CALL();
return 0;
}
if (!IS_ZERO (ret))
{
APPLY_SLOT_FINISH_CALL();
return ob->super;
}
APPLY_SLOT_FINISH_CALL();
return object_present2 (SVALUE_STRPTR(v), ob->super->contains);
}
return 0;
}
/**
* Help function for object_present().
* Looks for an object named 'str' in the inventory 'ob'.
* An optional number following the object name indicates which one to find.
* For example, "sword 2" finds the second sword in the inventory.
* @param str The name of the object to find, possibly with a number suffix.
* @param ob The inventory to search in.
* @return The found object, or NULL if not found.
*/
static object_t* object_present2 (const char *str, object_t * ob) {
svalue_t *ret;
malloc_str_t name;
size_t count = 0, length;
if ((length = strlen (str)))
{
const unsigned char* scan = (const unsigned char*)str + length - 1;
if (isdigit (*scan))
{
do
{
scan--;
}
while (scan > (const unsigned char*)str && isdigit (*scan));
if (*scan == ' ')
{
count = atoi ((const char*)scan + 1) - 1;
length = scan - (const unsigned char*)str;
}
}
}
for (; ob; ob = ob->next_inv)
{
name = new_string (length, "object_present2");
memcpy (name, str, length);
name[length] = 0;
push_malloced_string (name);
ret = APPLY_SLOT_CALL (APPLY_ID, ob, 1, ORIGIN_DRIVER);
if (ob->flags & O_DESTRUCTED)
{
APPLY_SLOT_FINISH_CALL();
return 0;
}
if (IS_ZERO (ret))
{
APPLY_SLOT_FINISH_CALL();
continue;
}
APPLY_SLOT_FINISH_CALL();
if (count-- > 0)
continue;
return ob;
}
return 0;
}
/**
* @brief Load and initialize the master object with optional pre-text.
* @param master_file Path to master file.
* @param pre_text Optional pre-text (extra code prepended before master source).
*
* If pre_text is provided, it is prepended to the master source code during compilation,
* allowing tests to inject custom code (e.g., instrumentation, mock applies) into the master.
*/
void init_master (const char *master_file, const char *pre_text) {
char buf[PATH_MAX];
object_t *new_ob;
if (!master_file || !master_file[0])
{
/* If master file was not specified correctly, we don't expect the logger would
* work either, so print to stderr instead.
*/
fprintf (stderr, "No master object specified in config file.\n");
exit (-1);
}
opt_info(1, "Loading master object: %s\n", master_file);
if (!strip_name (master_file, buf, sizeof (buf)))
{
fprintf (stderr, "Illegal master file name '%s'", master_file);
exit(-1);
}
if (master_file[strlen (master_file) - 2] != '.')
strncat (buf, ".c", sizeof(buf) - strlen(buf) - 1);
new_ob = load_object (buf, pre_text);
if (new_ob == 0)
{
fprintf (stderr, "The master file %s was not loaded.\n", master_file);
exit (-1);
}
set_master (new_ob);
}
static object_t *saved_name_ob = NULL;
static char *saved_name_value = "";
static object_t *vital_destruct_guard = NULL;
static void fix_object_names (void) {
if (saved_name_ob)
{
saved_name_ob->name = saved_name_value;
saved_name_ob = NULL;
saved_name_value = "";
vital_destruct_guard = NULL;
}
}
static object_t *restrict_destruct;
void reset_destruct_object_limits() {
restrict_destruct = NULL;
}
/**
* Remove an object. It is first moved into the \c ob_list_destruct linked
* list, and not really deallocated until later. (see destruct2()).
* @param ob The object to destruct.
*/
void destruct_object (object_t * ob) {
object_t **pp;
int removed;
object_t *super;
object_t *save_restrict_destruct = restrict_destruct;
/*
* Destruction is a two-stage process in this driver:
* 1) detach and sanitize runtime references immediately (this function)
* 2) defer final object variable/prog release to remove_destructed_objects()
*
* This ordering keeps stack/apply code safe when destruction happens during
* nested LPC execution.
*/
DEBUG_CHECK (!ob, "destruct_object() called with NULL pointer.\n");
/*
* Guard against illegal recursive destruct requests coming from move_or_destruct.
* The restrict_destruct token is temporary and intentionally narrow in scope.
*/
opt_trace (TT_EVAL|1, "start destructing: /%s", ob->name);
if (restrict_destruct && restrict_destruct != ob)
error ("*Only this_object() can be destructed from move_or_destruct.");
/*
* simul_efun is special: bytecode embeds simul indexes, so while master exists
* we reject direct simul destruction to avoid corrupting live call targets.
*/
if ((ob == simul_efun_ob) && master_ob)
{
/* simul efun object is a special object that the F_SIMUL_EFUN instruction in compiled LPC
* opcodes relies on the correct associations of simul_num and the function defined in
* simul_efun_ob. If both master_object and simul_efun_object exist, then
* destructing simul_efun_object would break this association and possibly corrupt the
* program of master object.
*
* In Neolith, we allow the simul_efun_object to be destructed only when the
* master_object does not exist, such as during mudlib reloading. More validations are
* checked in the set_simul_efun() function when replacing the simul_efun_ob with a new object.
*/
error ("*Cannot destruct simul_efun_object while master_object exists.");
}
#ifdef PACKAGE_SOCKETS
/*
* check if object has an efun socket referencing it for a callback. if
* so, close the efun socket.
*/
if (ob->flags & O_EFUN_SOCKET)
{
close_referencing_sockets (ob);
}
#endif
#ifdef HAVE_CURL
close_curl_handles (ob);
#endif
if (ob->flags & O_DESTRUCTED)
{
/* Idempotent API: repeated destruction requests are treated as no-op. */
opt_trace (TT_EVAL|1, "object /%s already destructed", ob->name);
return;
}
/*
* Drop stack-visible references first so active execution no longer points
* at this object as a live target.
*/
remove_object_from_stack (ob);
/*
* Evict inventory before unlinking from world lists.
* This preserves move/destruct semantics for contained objects and keeps
* environment callbacks consistent.
*/
super = ob->super;
while (ob->contains)
{
object_t *otmp = ob->contains;
/*
* An error here will not leave destruct() in an inconsistent
* stage.
*/