-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.c
416 lines (366 loc) · 9.55 KB
/
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <locale.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <unistd.h>
#include "memory.h"
#include "config.h"
#include "lint.h"
#include "interpret.h"
#include "object.h"
#include "lex.h"
#include "exec.h"
#include "mudstat.h"
#include "comm1.h"
#include "simulate.h"
#include "main.h"
#include "backend.h"
#include "random.h"
#include "hash.h"
extern int current_line;
int d_flag = 0; /* Run with debug */
int t_flag = 0; /* Disable heart beat and reset */
int e_flag = 0; /* Load empty, without castles. */
int s_flag = 0; /* Make statistics and dump to /MUDstatistics */
int no_ip_demon = 0;
int unlimited = 0; /* Run without eval cost limits */
int comp_flag = 0; /* Trace compilations */
int warnobsoleteflag = 0;
char *flag = NULL;
int driver_mtime;
char *mudlib_path = MUD_LIB;
char *default_charset = DEFAULT_CHARSET;
void init_signals(void);
void create_object(struct object *ob);
void init_call_out(void);
#ifdef YYDEBUG
extern int yydebug;
#endif
int port_number = PORTNO;
#ifdef CATCH_UDP_PORT
int udp_port = CATCH_UDP_PORT;
#endif
#ifdef SERVICE_PORT
int service_port = SERVICE_PORT;
#endif
char *reserved_area; /* reserved for malloc() */
struct svalue const0, const1, constempty;
struct closure funcempty;
double consts[5];
extern struct object *vbfc_object;
extern struct object *master_ob, *auto_ob;
void
usage(int argc, char **argv)
{
fprintf(stderr, "Usage: %s -m <mudlib> -t <telnet port> -u <udp port> -p <service port>\n", argv[0]);
fprintf(stderr, "Additional flags:\n");
fprintf(stderr, " -f <flag> Flag sent to mudlib before preloading\n");
fprintf(stderr, " -d <flag> Set debug flag\n");
fprintf(stderr, " -D <define> Add a pre define\n");
fprintf(stderr, " -O Warn of use of obsolete functions\n");
fprintf(stderr, " -e Skip preloading\n");
fprintf(stderr, " -c Additional information about file compilation\n");
fprintf(stderr, " -l Unlimited eval cost\n");
fprintf(stderr, " -S Enable mudstatus\n");
fprintf(stderr, " -y YY debugging\n");
fprintf(stderr, " -N Do not start reverse lookup daemon\n");
fprintf(stderr, "\n");
exit(1);
}
void
parse_args(int argc, char **argv)
{
int ch;
while ((ch = getopt(argc, argv, "h?m:p:t:u:f:d:D:OeclNSy")) != -1)
{
switch (ch)
{
case 'm':
mudlib_path = strdup(optarg);
break;
case 't':
port_number = atoi(optarg);
break;
case 'u':
#ifdef CATCH_UDP_PORT
udp_port = atoi(optarg);
#endif
break;
case 'p':
#ifdef SERVICE_PORT
service_port = atoi(optarg);
#endif
break;
case 'f':
flag = strdup(optarg);
continue;
case 'e':
e_flag++;
continue;
case 'D':
add_pre_define(optarg);
continue;
case 'O':
warnobsoleteflag++;
continue;
case 'd':
d_flag = atoi(optarg);
continue;
case 'c':
comp_flag++;
continue;
case 'l':
unlimited++;
continue;
case 'N':
no_ip_demon++;
continue;
case 'S':
s_flag++;
mudstatus_set(1, -1, -1); /* Statistics, default limits */
continue;
case 'y':
#ifdef YYDEBUG
yydebug = 1;
#endif
default:
case '?':
usage(argc, argv);
break;
}
}
}
int
main(int argc, char **argv)
{
extern int game_is_being_shut_down;
char *p;
int i = 0;
struct svalue *ret;
extern struct svalue catch_value;
extern void init_cfuns(void);
struct gdexception exception_frame;
(void)setlinebuf(stdout);
parse_args(argc, argv);
const0.type = T_NUMBER; const0.u.number = 0;
const1.type = T_NUMBER; const1.u.number = 1;
constempty.type = T_FUNCTION; constempty.u.func = &funcempty;
funcempty.funtype = FUN_EMPTY;
catch_value = const0;
/*
* Check that the definition of EXTRACT_UCHAR() is correct.
*/
p = (char *)&i;
*p = -10;
if (EXTRACT_UCHAR(p) != 0x100 - 10)
{
(void)fprintf(stderr, "Bad definition of EXTRACT_UCHAR() in config.h.\n");
exit(1);
}
#ifdef DEFAULT_LC_ALL
if (NULL == setlocale(LC_ALL, DEFAULT_LC_ALL)) {
fprintf(stderr, "Unable to set LC_ALL to: %s.\n", DEFAULT_LC_ALL);
}
#endif
#ifdef DEFAULT_LC_CTYPE
if (NULL == setlocale(LC_CTYPE, DEFAULT_LC_CTYPE)) {
fprintf(stderr, "Unable to set LC_CTYPE to: %s.\n", DEFAULT_LC_CTYPE);
}
#endif
set_current_time();
#ifdef PROFILE_LPC
set_profile_timebase(60.0); /* One minute */
#endif
#if RESERVED_SIZE > 0
reserved_area = malloc(RESERVED_SIZE);
#endif
init_random();
init_tasks();
query_load_av();
init_num_args();
init_machine();
init_cfuns();
init_hash();
/*
* Set up the signal handling.
*/
init_signals();
if (chdir(mudlib_path) == -1) {
(void)fprintf(stderr, "Bad mudlib directory: %s\n", MUD_LIB);
exit(1);
}
if (setjmp(exception_frame.e_context))
{
clear_state();
add_message("Anomaly in the fabric of world space.\n");
}
else
{
exception_frame.e_exception = NULL;
exception_frame.e_catch = 0;
exception = &exception_frame;
auto_ob = 0;
master_ob = 0;
if ((auto_ob = load_object("secure/auto", 1, 0, 0)) != NULL)
{
add_ref(auto_ob, "main");
auto_ob->prog->flags |= PRAGMA_RESIDENT;
}
get_simul_efun();
master_ob = load_object("secure/master", 1, 0, 0);
if (master_ob)
{
/*
* Make sure master_ob is never made a dangling pointer.
* Look at apply_master_ob() for more details.
*/
add_ref(master_ob, "main");
master_ob->prog->flags |= PRAGMA_RESIDENT;
resolve_master_fkntab();
create_object(master_ob);
load_parse_information();
clear_state();
}
}
exception = NULL;
if (auto_ob == 0)
{
(void)fprintf(stderr, "The file secure/auto must be loadable.\n");
exit(1);
}
if (master_ob == 0)
{
(void)fprintf(stderr, "The file secure/master must be loadable.\n");
exit(1);
}
set_inc_list(apply_master_ob(M_DEFINE_INCLUDE_DIRS, 0));
{
struct svalue* ret1;
ret1 = apply_master_ob(M_PREDEF_DEFINES, 0);
if (ret1 && ret1->type == T_POINTER)
{
int ii;
for (ii = 0; ii < ret1->u.vec->size; ii++)
if (ret1->u.vec->item[ii].type == T_STRING)
{
add_pre_define(ret1->u.vec->item[ii].u.string);
}
}
}
if (flag != NULL)
{
printf("Applying driver flag: %s\n", flag);
push_string(flag, STRING_MSTRING);
(void)apply_master_ob(M_FLAG, 1);
if (game_is_being_shut_down)
{
(void)fprintf(stderr, "Shutdown by master object.\n");
exit(0);
}
}
/*
* See to it that the mud name is always defined in compiled files
*/
ret = apply_master_ob(M_GET_MUD_NAME, 0);
if (ret && ret->type == T_STRING)
{
struct lpc_predef_s *tmp;
tmp = (struct lpc_predef_s *)
xalloc(sizeof(struct lpc_predef_s));
if (!tmp)
fatal("xalloc failed\n");
tmp->flag = string_copy(ret->u.string);
tmp->next = lpc_predefs;
lpc_predefs = tmp;
}
ret = apply_master_ob(M_GET_VBFC_OBJECT, 0);
if (ret && ret->type == T_OBJECT)
{
vbfc_object = ret->u.ob;
INCREF(vbfc_object->ref);
}
else
vbfc_object = 0;
if (game_is_being_shut_down)
exit(1);
init_call_out();
preload_objects(e_flag);
(void)apply_master_ob(M_FINAL_BOOT, 0);
mainloop();
return 0;
}
char *
string_copy(char *str)
{
size_t len = strlen(str) + 1;
char *p = xalloc(len);
if (p == NULL)
return NULL;
strncpy(p, str, len);
return p;
}
/*VARARGS1*/
void
debug_message(char *fmt, ...)
{
va_list argp;
char *f;
static FILE *fp = NULL;
char deb[110];
char name[100];
if (fp == NULL) {
(void)gethostname(name,sizeof name);
if ((f = strchr(name, '.')) != NULL)
*f = '\0';
snprintf(deb, sizeof(deb), "%s.debug.log", name);
fp = fopen(deb, "w");
if (fp == NULL) {
perror(deb);
abort();
}
}
va_start(argp, fmt);
vfprintf(fp, fmt, argp);
va_end(argp);
fflush(fp);
}
void
debug_message_svalue(struct svalue *v)
{
if (v == 0)
{
debug_message("<NULL>");
return;
}
switch(v->type)
{
case T_NUMBER:
debug_message("%lld", v->u.number);
return;
case T_FLOAT:
debug_message("%.18g", v->u.real);
return;
case T_STRING:
debug_message("\"%s\"", v->u.string);
return;
case T_OBJECT:
debug_message("OBJ(%s)", v->u.ob->name);
return;
case T_LVALUE:
debug_message("Pointer to ");
debug_message_svalue(v->u.lvalue);
return;
default:
debug_message("<INVALID>\n");
return;
}
}
int slow_shut_down_to_do = 0;