-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.cc
432 lines (409 loc) · 10.4 KB
/
init.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
#define ICI_CORE
#include "archiver.h"
#include "buf.h"
#include "exec.h"
#include "fwd.h"
#include "map.h"
#include "pcre.h"
#include "ref.h"
#include "str.h"
#if defined(_WIN32)
#include <io.h>
#else
#include <sys/types.h>
#include <unistd.h>
#endif
namespace ici
{
constexpr size_t INITIAL_ATOMSZ = 1024; // Must be power of two
constexpr size_t INITIAL_OBJS = 4096;
extern cfunc *ici_funcs[];
static int mapici_init(objwsup *);
extern int sys_init(objwsup *);
extern int net_init(objwsup *);
/*
* Perform basic interpreter setup. Return non-zero on failure, usual
* conventions.
*
* After calling this the scope stack has a struct for autos on it, and
* the super of that is for statics. That struct for statics is where
* global definitions that are likely to be visible to all code tend
* to get set. All the intrinsic functions for example. It forms the
* extern scope of any files parsed at the top level.
*
* In systems supporting threads, on exit, the global ICI mutex has been
* acquired (with enter()).
*
* This --func-- forms part of the --ici-api--.
*/
int init()
{
static bool init_done = false;
if (init_done)
{
return 0;
}
cfunc **cfp;
map *scope;
objwsup *externs;
exec *x;
int i;
double pi = 3.14159265358979323848;
/*
* Just make sure our universal headers are really the size we
* hope they are. Nothing actually assumes this. But it would
* represent a significant inefficiency if they were padded.
*/
assert(sizeof(object) == 4);
/*
* The following assertion is only valid on some architectures.
* In particular it is wrong for systems that use 64-bit aligned
* pointers.
*
assert(offsetof(objwsup, o_super) == 4);
*/
#ifndef NDEBUG
{
char v[80];
/*
* Check that the #defines of version number are in sync with our version
* string from conf.c.
*/
sprintf(v, "@(#)ICI %d.%d.%d", major_version, minor_version, release_number);
assert(strncmp(v, version_string, strlen(v)) == 0);
}
#endif
init_types();
if (chkbuf(1024))
{
return 1;
}
if ((atoms = (object **)ici_nalloc(INITIAL_ATOMSZ * sizeof(object *))) == nullptr)
{
return 1;
}
atomsz = INITIAL_ATOMSZ;
memset((char *)atoms, 0, atomsz * sizeof(object *));
if ((objs = (object **)ici_nalloc(INITIAL_OBJS * sizeof(object *))) == nullptr)
{
return 1;
}
memset((char *)objs, 0, INITIAL_OBJS * sizeof(object *));
objs_limit = objs + INITIAL_OBJS;
objs_top = objs;
for (i = 0; i < (int)nels(small_ints); ++i)
{
if ((small_ints[i] = new_int(i)) == nullptr)
{
return -1;
}
}
o_zero = small_ints[0];
o_one = small_ints[1];
if (init_sstrings())
{
return 1;
}
pcre_free = ici_free;
pcre_malloc = (void *(*)(size_t))ici_alloc;
if ((scope = new_map()) == nullptr)
{
return 1;
}
if ((scope->o_super = externs = objwsupof(new_map())) == nullptr)
{
return 1;
}
decref(externs);
if ((x = new_exec()) == nullptr)
{
return 1;
}
enter(x);
rego(&os);
rego(&xs);
rego(&vs);
if (engine_stack_check())
{
return 1;
}
vs.push(scope, with_decref);
if (mapici_init(externs))
{
return 1;
}
for (cfp = ici_funcs; *cfp != nullptr; ++cfp)
{
if (assign_cfuncs(externs, *cfp))
{
return 1;
}
}
if (sys_init(externs))
{
return 1;
}
if (net_init(externs))
{
return 1;
}
if (archive_init())
{
return 1;
};
init_signals();
init_exec();
if (set_val(externs, SS(_stdin), 'u', stdin) || set_val(externs, SS(_stdout), 'u', stdout) ||
set_val(externs, SS(_stderr), 'u', stderr) || set_val(externs, SS(pi), 'f', &pi))
{
return 1;
}
#ifndef NOSTARTUPFILE
if (call(SS(load), "o", SS(core)))
{
return 1;
}
#endif
init_done = true;
return 0;
}
/*
* Check that the seperately compiled module that calls this function has been
* compiled against a compatible versions of the ICI core that is now trying
* to load it. An external module should call this like:
*
* if (ici::check_interface(version_number, back_compat_version, "myname"))
* return nullptr;
*
* As soon as it can on load. ICI_VER and ICI_BACK_COMPAT_VER come from ici.h
* at the time that module was compiled. This functions compares the values
* passed from the external modules with the values the core was compiled
* with, and fails (usual conventions) if there is any incompatibility.
*
* This --func-- forms part of the --ici-api--.
*/
int check_interface(unsigned long mver, unsigned long bver, char const *name)
{
if (version_number < mver)
{
/*
* Ooh, I'm an old ICI being called by a newer module. Does the module
* think I'm recent enought?
*/
if (version_number >= bver)
{
return 0;
}
}
else
{
/*
* I'm a relatively up-to-date ICI, but is that module new enough
* for me?
*/
if (mver >= back_compat_version)
{
return 0;
}
}
return set_error("%s module was built for ICI %d.%d.%d, which is incompatible with this version %d.%d.%d", name,
(int)(mver >> 24), (int)(mver >> 16) & 0xFF, (int)(mver & 0xFFFF), major_version, minor_version,
release_number);
}
static int push_path_elements(array *a, const char *path); /* Forward. */
#define PUSH(A, B) \
do \
{ \
if (push_path_elements((A), (B))) \
{ \
return 1; \
} \
} \
while (0)
/*
* Push one or more file names from path, seperated by the local system
* seperator character (eg. : or ;), onto a. Usual error conventions.
*/
static int push_path_elements(array *a, const char *path)
{
const char *p;
const char *q;
str *s;
object **e;
for (p = path; *p != '\0'; p = *q == '\0' ? q : q + 1)
{
if ((q = strchr(p, ICI_PATH_SEP)) == nullptr)
{
q = p + strlen(p);
}
if ((s = new_str(p, q - p)) == nullptr)
{
return 1;
}
/*
* Don't add duplicates...
*/
for (e = a->a_base; e < a->a_top; ++e)
{
if (*e == s)
{
goto skip;
}
}
/*
* Don't add inaccessable dirs...
*/
if (access(s->s_chars, 0) != 0)
{
goto skip;
}
if (a->push_checked(s))
{
decref(s);
return 1;
}
skip:
decref(s);
}
return 0;
}
#if defined(_WIN32)
/*
* Push path elements specific to Windows onto the array a (which is the ICI
* path array used for finding dynamically loaded modules and stuff). These
* are in addition to the ICIPATH environment variable. We try to mimic
* the search behaviour of LoadLibrary() (that being the Windows thing to
* do).
*/
static int push_os_path_elements(array *a)
{
char fname[MAX_PATH];
char *p;
if (GetModuleFileName(nullptr, fname, sizeof fname - 10) > 0)
{
if ((p = strrchr(fname, ICI_DIR_SEP)) != nullptr)
{
*p = '\0';
}
PUSH(a, fname);
p = fname + strlen(fname);
*p++ = ICI_DIR_SEP;
strcpy(p, "ici");
PUSH(a, fname);
}
PUSH(a, ".");
if (GetSystemDirectory(fname, sizeof fname - 10) > 0)
{
PUSH(a, fname);
p = fname + strlen(fname);
*p++ = ICI_DIR_SEP;
strcpy(p, "ici");
PUSH(a, fname);
}
if (GetWindowsDirectory(fname, sizeof fname - 10) > 0)
{
PUSH(a, fname);
p = fname + strlen(fname);
*p++ = ICI_DIR_SEP;
strcpy(p, "ici");
PUSH(a, fname);
}
if ((p = getenv("PATH")) != nullptr)
{
PUSH(a, p);
}
return 0;
}
#else
/*
* Assume a UNIX-like sytem.
*
* Push path elements specific to UNIX-like systems onto the array a (which
* is the ICI path array used for finding dynamically loaded modules and
* stuff). These are in addition to the ICIPATH environment variable.
*/
static int push_os_path_elements(array *a)
{
char *p;
char *q;
char *path;
char fname[FILENAME_MAX];
PUSH(a, "/usr/lib/ici:/usr/local/lib/ici:/opt/lib/ici:/opt/ici/lib/ici:.");
if ((path = getenv("PATH")) != nullptr)
{
for (p = path; *p != '\0'; p = *q == '\0' ? q : q + 1)
{
if ((q = strchr(p, ':')) == nullptr)
{
q = p + strlen(p);
}
if (q - 4 < p || memcmp(q - 4, "/bin", 4) != 0 || q - p >= FILENAME_MAX - 10)
{
continue;
}
memcpy(fname, p, (q - p) - 4);
strcpy(fname + (q - p) - 4, "/lib/ici");
PUSH(a, fname);
}
}
#ifdef ICI_CONFIG_PREFIX
/*
* Put a configuration defined location on, if there is one..
*/
PUSH(a, ICI_CONFIG_PREFIX "/lib/ici");
#endif
return 0;
}
#endif /* End of selection of which push_os_path_elements() to use */
/*
* Set the path variable in externs to be an array of all the directories
* that should be searched in for ICI extension modules and stuff.
*/
ref<array> init_path()
{
ref<array> a = new_array();
if (a)
{
if (char *path = getenv("ICIPATH"))
{
if (push_path_elements(a, path))
{
return nullptr;
}
}
push_os_path_elements(a);
}
return a;
}
static int mapici_init(objwsup *externs)
{
ref<str> ver = new_str_nul_term(version_string);
if (!ver)
{
return 1;
}
auto path = init_path();
if (!path)
{
return 1;
}
ref<map> mapici = new_map();
if (!mapici)
{
return 1;
}
if (mapici->assign(SS(version), ver))
{
return 1;
}
if (mapici->assign(SS(path), path))
{
return 1;
}
if (externs->assign(SS(_ici), mapici))
{
return 1;
}
return 0;
}
} // namespace ici