| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* main.d - ecl boot proccess |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
/******************************** IMPORTS *****************************/ |
| 16 |
|
| 17 |
#include <ecl/ecl.h> |
| 18 |
#include <limits.h> |
| 19 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 20 |
# include <windows.h> |
| 21 |
# include <shellapi.h> |
| 22 |
# define MAXPATHLEN 512 |
| 23 |
#endif |
| 24 |
#ifndef MAXPATHLEN |
| 25 |
# ifdef PATH_MAX |
| 26 |
# define MAXPATHLEN PATH_MAX |
| 27 |
# else |
| 28 |
# define NO_PATH_MAX |
| 29 |
# include <unistd.h> |
| 30 |
# endif |
| 31 |
#endif |
| 32 |
#ifdef ECL_USE_MPROTECT |
| 33 |
# include <sys/mman.h> |
| 34 |
# ifndef MAP_FAILED |
| 35 |
# define MAP_FAILED -1 |
| 36 |
# endif |
| 37 |
#endif |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <ecl/cache.h> |
| 42 |
#include <ecl/internal.h> |
| 43 |
#include <ecl/ecl-inl.h> |
| 44 |
|
| 45 |
|
| 46 |
#include "ecl_features.h" |
| 47 |
#include "iso_latin_names.h" |
| 48 |
|
| 49 |
/******************************* EXPORTS ******************************/ |
| 50 |
|
| 51 |
const char *ecl_self; |
| 52 |
static struct cl_env_struct first_env; |
| 53 |
|
| 54 |
/************************ GLOBAL INITIALIZATION ***********************/ |
| 55 |
|
| 56 |
|
| 57 |
/* HEAP */ |
| 58 |
|
| 59 |
#if ECL_FIXNUM_BITS <= 32 |
| 60 |
/* 1GB */ |
| 61 |
#define HEAP_SIZE_DEFAULT 1073741824L |
| 62 |
#else |
| 63 |
/* 4GB */ |
| 64 |
#define HEAP_SIZE_DEFAULT 4294967296L |
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
static int ARGC; |
| 69 |
static char **ARGV; |
| 70 |
/* INV: see ecl_option enum in external.h */ |
| 71 |
cl_fixnum ecl_option_values[ECL_OPT_LIMIT+1] = { |
| 72 |
#ifdef GBC_BOEHM_GENGC |
| 73 |
1, /* ECL_OPT_INCREMENTAL_GC */ |
| 74 |
#else |
| 75 |
0, /* ECL_OPT_INCREMENTAL_GC */ |
| 76 |
#endif |
| 77 |
1, /* ECL_OPT_TRAP_SIGSEGV */ |
| 78 |
1, /* ECL_OPT_TRAP_SIGFPE */ |
| 79 |
1, /* ECL_OPT_TRAP_SIGINT */ |
| 80 |
1, /* ECL_OPT_TRAP_SIGILL */ |
| 81 |
1, /* ECL_OPT_TRAP_SIGBUS */ |
| 82 |
1, /* ECL_OPT_TRAP_SIGPIPE */ |
| 83 |
1, /* ECL_OPT_TRAP_INTERRUPT_SIGNAL */ |
| 84 |
#if defined(ECL_THREADS) && defined(HAVE_SIGPROCMASK) |
| 85 |
1, /* ECL_OPT_SIGNAL_HANDLING_THREAD */ |
| 86 |
#else |
| 87 |
0, /* ECL_OPT_SIGNAL_HANDLING_THREAD */ |
| 88 |
#endif |
| 89 |
16, /* ECL_OPT_SIGNAL_QUEUE_SIZE */ |
| 90 |
0, /* ECL_OPT_BOOTED */ |
| 91 |
8192, /* ECL_OPT_BIND_STACK_SIZE */ |
| 92 |
1024, /* ECL_OPT_BIND_STACK_SAFETY_AREA */ |
| 93 |
2048, /* ECL_OPT_FRAME_STACK_SIZE */ |
| 94 |
128, /* ECL_OPT_FRAME_STACK_SAFETY_AREA */ |
| 95 |
32768, /* ECL_OPT_LISP_STACK_SIZE */ |
| 96 |
128, /* ECL_OPT_LISP_STACK_SAFETY_AREA */ |
| 97 |
ECL_DEFAULT_C_STACK_SIZE, /* ECL_OPT_C_STACK_SIZE */ |
| 98 |
4*sizeof(cl_index)*1024, /* ECL_OPT_C_STACK_SAFETY_AREA */ |
| 99 |
HEAP_SIZE_DEFAULT, /* ECL_OPT_HEAP_SIZE */ |
| 100 |
1024*1024, /* ECL_OPT_HEAP_SAFETY_AREA */ |
| 101 |
0, /* ECL_OPT_THREAD_INTERRUPT_SIGNAL */ |
| 102 |
1, /* ECL_OPT_SET_GMP_MEMORY_FUNCTIONS */ |
| 103 |
1, /* ECL_OPT_USE_SETMODE_ON_FILES */ |
| 104 |
0}; |
| 105 |
|
| 106 |
#if !defined(GBC_BOEHM) |
| 107 |
static char stdin_buf[BUFSIZ]; |
| 108 |
static char stdout_buf[BUFSIZ]; |
| 109 |
#endif |
| 110 |
|
| 111 |
cl_fixnum |
| 112 |
ecl_get_option(int option) |
| 113 |
{ |
| 114 |
if (option >= ECL_OPT_LIMIT || option < 0) { |
| 115 |
FEerror("Invalid boot option ~D", 1, ecl_make_fixnum(option)); |
| 116 |
} |
| 117 |
return ecl_option_values[option]; |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
ecl_set_option(int option, cl_fixnum value) |
| 122 |
{ |
| 123 |
if (option > ECL_OPT_LIMIT || option < 0) { |
| 124 |
FEerror("Invalid boot option ~D", 1, ecl_make_fixnum(option)); |
| 125 |
} else { |
| 126 |
if (option < ECL_OPT_BOOTED && |
| 127 |
ecl_option_values[ECL_OPT_BOOTED]) { |
| 128 |
FEerror("Cannot change option ~D while ECL is running", |
| 129 |
1, ecl_make_fixnum(option)); |
| 130 |
} |
| 131 |
ecl_option_values[option] = value; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
init_env_mp(cl_env_ptr env) |
| 137 |
{ |
| 138 |
#if defined(ECL_THREADS) |
| 139 |
env->cleanup = 0; |
| 140 |
#else |
| 141 |
env->own_process = ECL_NIL; |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
static void |
| 146 |
init_env_int(cl_env_ptr env) |
| 147 |
{ |
| 148 |
env->interrupt_struct = ecl_alloc(sizeof(*env->interrupt_struct)); |
| 149 |
env->interrupt_struct->pending_interrupt = ECL_NIL; |
| 150 |
#ifdef ECL_THREADS |
| 151 |
ecl_mutex_init(&env->interrupt_struct->signal_queue_lock, FALSE); |
| 152 |
#endif |
| 153 |
{ |
| 154 |
int size = ecl_option_values[ECL_OPT_SIGNAL_QUEUE_SIZE]; |
| 155 |
env->interrupt_struct->signal_queue = cl_make_list(1, ecl_make_fixnum(size)); |
| 156 |
} |
| 157 |
env->fault_address = env; |
| 158 |
env->trap_fpe_bits = 0; |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
init_env_ffi(cl_env_ptr env) |
| 163 |
{ |
| 164 |
#ifdef HAVE_LIBFFI |
| 165 |
env->ffi_args_limit = 0; |
| 166 |
env->ffi_types = 0; |
| 167 |
env->ffi_values = 0; |
| 168 |
env->ffi_values_ptrs = 0; |
| 169 |
#endif |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
init_env_aux(cl_env_ptr env) |
| 174 |
{ |
| 175 |
/* Reader */ |
| 176 |
env->string_pool = ECL_NIL; |
| 177 |
env->packages_to_be_created = ECL_NIL; |
| 178 |
env->packages_to_be_created_p = ECL_NIL; |
| 179 |
/* Format (written in C) */ |
| 180 |
#if !defined(ECL_CMU_FORMAT) |
| 181 |
env->fmt_aux_stream = ecl_make_string_output_stream(64, 1); |
| 182 |
#endif |
| 183 |
/* Bignum arithmetic */ |
| 184 |
ecl_init_bignum_registers(env); |
| 185 |
/* Bytecodes compiler environment */ |
| 186 |
env->c_env = NULL; |
| 187 |
/* CLOS caches */ |
| 188 |
env->method_cache = ecl_make_cache(64, 4096); |
| 189 |
env->slot_cache = ecl_make_cache(3, 4096); |
| 190 |
} |
| 191 |
|
| 192 |
void |
| 193 |
ecl_init_first_env(cl_env_ptr env) |
| 194 |
{ |
| 195 |
env->default_sigmask = cl_core.default_sigmask; |
| 196 |
#ifdef ECL_THREADS |
| 197 |
init_threads(); |
| 198 |
#else |
| 199 |
ecl_cs_init(env); |
| 200 |
#endif |
| 201 |
init_env_mp(env); |
| 202 |
init_env_int(env); |
| 203 |
init_env_aux(env); |
| 204 |
init_env_ffi(env); |
| 205 |
init_stacks(env); |
| 206 |
} |
| 207 |
|
| 208 |
void |
| 209 |
ecl_init_env(cl_env_ptr env) |
| 210 |
{ |
| 211 |
init_env_mp(env); |
| 212 |
init_env_int(env); |
| 213 |
init_env_aux(env); |
| 214 |
init_env_ffi(env); |
| 215 |
init_stacks(env); |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
_ecl_dealloc_env(cl_env_ptr env) |
| 220 |
{ |
| 221 |
/* Environment cleanup. This is required because the environment is allocated |
| 222 |
* using mmap or some other method. */ |
| 223 |
free_stacks(env); |
| 224 |
#ifdef ECL_THREADS |
| 225 |
ecl_mutex_destroy(&env->interrupt_struct->signal_queue_lock); |
| 226 |
#endif |
| 227 |
#if defined(ECL_USE_MPROTECT) |
| 228 |
if (munmap(env, sizeof(*env))) |
| 229 |
ecl_internal_error("Unable to deallocate environment structure."); |
| 230 |
#elif defined(ECL_USE_GUARD_PAGE) |
| 231 |
if (!VirtualFree(env, 0, MEM_RELEASE)) |
| 232 |
ecl_internal_error("Unable to deallocate environment structure."); |
| 233 |
#else |
| 234 |
ecl_free_unsafe(env); |
| 235 |
#endif |
| 236 |
} |
| 237 |
|
| 238 |
cl_env_ptr |
| 239 |
_ecl_alloc_env(cl_env_ptr parent) |
| 240 |
{ |
| 241 |
/* |
| 242 |
* Allocates the lisp environment for a thread. Depending on which |
| 243 |
* mechanism we use for detecting delayed signals, we may allocate |
| 244 |
* the environment using mmap or the garbage collector. |
| 245 |
* |
| 246 |
* Note that at this point we are not allocating any other memory |
| 247 |
* which is stored via a pointer in the environment. If we would do |
| 248 |
* that, an unlucky interrupt by the gc before the allocated |
| 249 |
* environment is registered in cl_core.processes could lead to |
| 250 |
* memory being freed because the gc is not aware of the pointer to |
| 251 |
* the allocated memory in the environment. |
| 252 |
*/ |
| 253 |
cl_env_ptr output; |
| 254 |
#if defined(ECL_USE_MPROTECT) |
| 255 |
output = (cl_env_ptr) mmap(0, sizeof(*output), PROT_READ | PROT_WRITE, |
| 256 |
MAP_ANON | MAP_PRIVATE, -1, 0); |
| 257 |
if (output == MAP_FAILED) |
| 258 |
ecl_internal_error("Unable to allocate environment structure."); |
| 259 |
#else |
| 260 |
# if defined(ECL_USE_GUARD_PAGE) |
| 261 |
output = VirtualAlloc(0, sizeof(*output), MEM_COMMIT, PAGE_READWRITE); |
| 262 |
if (output == NULL) |
| 263 |
ecl_internal_error("Unable to allocate environment structure."); |
| 264 |
# else |
| 265 |
output = ecl_malloc(sizeof(*output)); |
| 266 |
if (output == NULL) |
| 267 |
ecl_internal_error("Unable to allocate environment structure."); |
| 268 |
# endif |
| 269 |
#endif |
| 270 |
{ |
| 271 |
size_t bytes = cl_core.default_sigmask_bytes; |
| 272 |
if (bytes == 0) { |
| 273 |
output->default_sigmask = 0; |
| 274 |
} else if (parent) { |
| 275 |
output->default_sigmask = ecl_alloc_atomic(bytes); |
| 276 |
memcpy(output->default_sigmask, parent->default_sigmask, bytes); |
| 277 |
} else { |
| 278 |
output->default_sigmask = cl_core.default_sigmask; |
| 279 |
} |
| 280 |
} |
| 281 |
for (cl_index i = 0; i < ECL_BIGNUM_REGISTER_NUMBER; i++) { |
| 282 |
output->big_register[i] = ECL_NIL; |
| 283 |
} |
| 284 |
output->method_cache = output->slot_cache = NULL; |
| 285 |
output->interrupt_struct = NULL; |
| 286 |
/* |
| 287 |
* An uninitialized environment _always_ disables interrupts. They |
| 288 |
* are activated later on by the thread entry point or init_unixint(). |
| 289 |
*/ |
| 290 |
output->disable_interrupts = 1; |
| 291 |
return output; |
| 292 |
} |
| 293 |
|
| 294 |
void |
| 295 |
cl_shutdown(void) |
| 296 |
{ |
| 297 |
if (ecl_option_values[ECL_OPT_BOOTED] > 0) { |
| 298 |
cl_object l = ecl_symbol_value(@'si::*exit-hooks*'); |
| 299 |
cl_object form = cl_list(2, @'funcall', ECL_NIL); |
| 300 |
while (CONSP(l)) { |
| 301 |
ecl_elt_set(form, 1, ECL_CONS_CAR(l)); |
| 302 |
si_safe_eval(3, form, ECL_NIL, OBJNULL); |
| 303 |
l = CDR(l); |
| 304 |
ECL_SET(@'si::*exit-hooks*', l); |
| 305 |
} |
| 306 |
#ifdef ENABLE_DLOPEN |
| 307 |
ecl_library_close_all(); |
| 308 |
#endif |
| 309 |
#ifdef ECL_TCP |
| 310 |
ecl_tcp_close_all(); |
| 311 |
#endif |
| 312 |
} |
| 313 |
ecl_set_option(ECL_OPT_BOOTED, -1); |
| 314 |
} |
| 315 |
|
| 316 |
ecl_def_ct_single_float(default_rehash_size,1.5f,static,const); |
| 317 |
ecl_def_ct_single_float(default_rehash_threshold,0.75f,static,const); |
| 318 |
ecl_def_ct_base_string(str_common_lisp,"COMMON-LISP",11,static,const); |
| 319 |
ecl_def_ct_base_string(str_common_lisp_user,"COMMON-LISP-USER",16,static,const); |
| 320 |
ecl_def_ct_base_string(str_cl,"CL",2,static,const); |
| 321 |
ecl_def_ct_base_string(str_cl_user,"CL-USER",7,static,const); |
| 322 |
ecl_def_ct_base_string(str_LISP,"LISP",4,static,const); |
| 323 |
ecl_def_ct_base_string(str_c,"C",1,static,const); |
| 324 |
ecl_def_ct_base_string(str_compiler,"COMPILER",8,static,const); |
| 325 |
ecl_def_ct_base_string(str_ffi,"FFI",3,static,const); |
| 326 |
ecl_def_ct_base_string(str_keyword,"KEYWORD",7,static,const); |
| 327 |
ecl_def_ct_base_string(str_si,"SI",2,static,const); |
| 328 |
ecl_def_ct_base_string(str_sys,"SYS",3,static,const); |
| 329 |
ecl_def_ct_base_string(str_system,"SYSTEM",6,static,const); |
| 330 |
ecl_def_ct_base_string(str_ext,"EXT",3,static,const); |
| 331 |
ecl_def_ct_base_string(str_clos,"CLOS",4,static,const); |
| 332 |
ecl_def_ct_base_string(str_mop,"MOP",3,static,const); |
| 333 |
ecl_def_ct_base_string(str_mp,"MP",2,static,const); |
| 334 |
ecl_def_ct_base_string(str_multiprocessing,"MULTIPROCESSING",15,static,const); |
| 335 |
#ifdef ECL_CLOS_STREAMS |
| 336 |
ecl_def_ct_base_string(str_gray,"GRAY",4,static,const); |
| 337 |
#endif |
| 338 |
ecl_def_ct_base_string(str_star_dot_star,"*.*",3,static,const); |
| 339 |
ecl_def_ct_base_string(str_rel_star_dot_star,"./*.*",5,static,const); |
| 340 |
ecl_def_ct_base_string(str_empty,"",0,static,const); |
| 341 |
ecl_def_ct_base_string(str_G,"G",1,static,const); |
| 342 |
ecl_def_ct_base_string(str_T,"T",1,static,const); |
| 343 |
#ifdef ENABLE_DLOPEN |
| 344 |
ecl_def_ct_base_string(str_fas,"fas",3,static,const); |
| 345 |
ecl_def_ct_base_string(str_fasl,"fasl",4,static,const); |
| 346 |
#endif |
| 347 |
ecl_def_ct_base_string(str_fasb,"fasb",4,static,const); |
| 348 |
ecl_def_ct_base_string(str_fasc,"fasc",4,static,const); |
| 349 |
ecl_def_ct_base_string(str_FASB,"FASB",4,static,const); |
| 350 |
ecl_def_ct_base_string(str_FASC,"FASC",4,static,const); |
| 351 |
ecl_def_ct_base_string(str_lsp,"lsp",3,static,const); |
| 352 |
ecl_def_ct_base_string(str_LSP,"LSP",3,static,const); |
| 353 |
ecl_def_ct_base_string(str_lisp,"lisp",4,static,const); |
| 354 |
ecl_def_ct_base_string(str_NIL,"NIL",3,static,const); |
| 355 |
ecl_def_ct_base_string(str_slash,"/",1,static,const); |
| 356 |
|
| 357 |
ecl_def_ct_single_float(flt_zero,0,static,const); |
| 358 |
ecl_def_ct_single_float(flt_zero_neg,-0.0,static,const); |
| 359 |
ecl_def_ct_double_float(dbl_zero,0,static,const); |
| 360 |
ecl_def_ct_double_float(dbl_zero_neg,-0.0,static,const); |
| 361 |
ecl_def_ct_long_float(ldbl_zero,0,static,const); |
| 362 |
ecl_def_ct_long_float(ldbl_zero_neg,-0.0l,static,const); |
| 363 |
ecl_def_ct_ratio(plus_half,ecl_make_fixnum(1),ecl_make_fixnum(2),static,const); |
| 364 |
ecl_def_ct_ratio(minus_half,ecl_make_fixnum(-1),ecl_make_fixnum(2),static,const); |
| 365 |
ecl_def_ct_single_float(flt_one,1,static,const); |
| 366 |
ecl_def_ct_single_float(flt_one_neg,-1,static,const); |
| 367 |
ecl_def_ct_single_float(flt_two,2,static,const); |
| 368 |
ecl_def_ct_complex(flt_imag_unit,&flt_zero_data,&flt_one_data,static,const); |
| 369 |
ecl_def_ct_complex(flt_imag_unit_neg,&flt_zero_data,&flt_one_neg_data,static,const); |
| 370 |
ecl_def_ct_complex(flt_imag_two,&flt_zero_data,&flt_two_data,static,const); |
| 371 |
|
| 372 |
struct cl_core_struct cl_core = { |
| 373 |
.packages = ECL_NIL, |
| 374 |
.lisp_package = ECL_NIL, |
| 375 |
.user_package = ECL_NIL, |
| 376 |
.keyword_package = ECL_NIL, |
| 377 |
.system_package = ECL_NIL, |
| 378 |
.ext_package = ECL_NIL, |
| 379 |
.clos_package = ECL_NIL, |
| 380 |
# ifdef ECL_CLOS_STREAMS |
| 381 |
.gray_package = ECL_NIL, |
| 382 |
# endif |
| 383 |
.mp_package = ECL_NIL, |
| 384 |
.c_package = ECL_NIL, |
| 385 |
.ffi_package = ECL_NIL, |
| 386 |
|
| 387 |
.pathname_translations = ECL_NIL, |
| 388 |
.library_pathname = ECL_NIL, |
| 389 |
|
| 390 |
.terminal_io = ECL_NIL, |
| 391 |
.null_stream = ECL_NIL, |
| 392 |
.standard_input = ECL_NIL, |
| 393 |
.standard_output = ECL_NIL, |
| 394 |
.error_output = ECL_NIL, |
| 395 |
.standard_readtable = ECL_NIL, |
| 396 |
.dispatch_reader = ECL_NIL, |
| 397 |
|
| 398 |
.char_names = ECL_NIL, |
| 399 |
.null_string = (cl_object)&str_empty_data, |
| 400 |
|
| 401 |
.plus_half = (cl_object)&plus_half_data, |
| 402 |
.minus_half = (cl_object)&minus_half_data, |
| 403 |
.imag_unit = (cl_object)&flt_imag_unit_data, |
| 404 |
.minus_imag_unit = (cl_object)&flt_imag_unit_neg_data, |
| 405 |
.imag_two = (cl_object)&flt_imag_two_data, |
| 406 |
.singlefloat_zero = (cl_object)&flt_zero_data, |
| 407 |
.doublefloat_zero = (cl_object)&dbl_zero_data, |
| 408 |
.singlefloat_minus_zero = (cl_object)&flt_zero_neg_data, |
| 409 |
.doublefloat_minus_zero = (cl_object)&dbl_zero_neg_data, |
| 410 |
.longfloat_zero = (cl_object)&ldbl_zero_data, |
| 411 |
.longfloat_minus_zero = (cl_object)&ldbl_zero_neg_data, |
| 412 |
|
| 413 |
.gensym_prefix = (cl_object)&str_G_data, |
| 414 |
.gentemp_prefix = (cl_object)&str_T_data, |
| 415 |
.gentemp_counter = ecl_make_fixnum(0), |
| 416 |
|
| 417 |
.Jan1st1970UT = ECL_NIL, |
| 418 |
|
| 419 |
.system_properties = ECL_NIL, |
| 420 |
|
| 421 |
.first_env = &first_env, |
| 422 |
#ifdef ECL_THREADS |
| 423 |
.processes = ECL_NIL, |
| 424 |
#endif |
| 425 |
/* LIBRARIES is an adjustable vector of objects. It behaves as |
| 426 |
a vector of weak pointers thanks to the magic in |
| 427 |
gbc.d/alloc_2.d */ |
| 428 |
.libraries = ECL_NIL, |
| 429 |
|
| 430 |
.max_heap_size = 0, |
| 431 |
.bytes_consed = ECL_NIL, |
| 432 |
.gc_counter = ECL_NIL, |
| 433 |
.gc_stats = 0, |
| 434 |
.path_max = 0, |
| 435 |
#ifdef GBC_BOEHM |
| 436 |
.safety_region = NULL, |
| 437 |
#endif |
| 438 |
|
| 439 |
.default_sigmask = NULL, |
| 440 |
.default_sigmask_bytes = 0, |
| 441 |
|
| 442 |
#ifdef ECL_THREADS |
| 443 |
.last_var_index = 0, |
| 444 |
.reused_indices = ECL_NIL, |
| 445 |
#endif |
| 446 |
.slash = (cl_object)&str_slash_data, |
| 447 |
|
| 448 |
.compiler_dispatch = ECL_NIL, |
| 449 |
|
| 450 |
.rehash_size = (cl_object)&default_rehash_size_data, |
| 451 |
.rehash_threshold = (cl_object)&default_rehash_threshold_data, |
| 452 |
|
| 453 |
.known_signals = ECL_NIL |
| 454 |
}; |
| 455 |
|
| 456 |
#if !defined(ECL_MS_WINDOWS_HOST) |
| 457 |
#define maybe_fix_console_stream(strm) (void)0 |
| 458 |
#else |
| 459 |
static void |
| 460 |
maybe_fix_console_stream(cl_object stream) |
| 461 |
{ |
| 462 |
cl_object external_format; |
| 463 |
if (stream->stream.mode != ecl_smm_io_wcon) { |
| 464 |
si_stream_external_format_set(stream, cl_list(2, ecl_symbol_value(@'ext::*default-external-format*'), @':crlf')); |
| 465 |
return; |
| 466 |
} |
| 467 |
external_format = si_windows_codepage_encoding(); |
| 468 |
if (external_format == @':pass-through') |
| 469 |
fprintf(stderr, |
| 470 |
"Unsupported codepage %d, input/output encoding may be wrong.\n" |
| 471 |
"Use the chcp command to change codepages, e.g. 'chcp 65001' to change to utf-8.\n", |
| 472 |
GetConsoleCP()); |
| 473 |
si_stream_external_format_set(stream, cl_list(2, external_format, @':crlf')); |
| 474 |
stream->stream.eof_char = 26; |
| 475 |
} |
| 476 |
#endif |
| 477 |
|
| 478 |
int |
| 479 |
cl_boot(int argc, char **argv) |
| 480 |
{ |
| 481 |
cl_object aux; |
| 482 |
int i; |
| 483 |
cl_env_ptr env; |
| 484 |
|
| 485 |
i = ecl_option_values[ECL_OPT_BOOTED]; |
| 486 |
if (i) { |
| 487 |
if (i < 0) { |
| 488 |
/* We have called cl_shutdown and want to use ECL again. */ |
| 489 |
ecl_set_option(ECL_OPT_BOOTED, 1); |
| 490 |
} |
| 491 |
return 1; |
| 492 |
} |
| 493 |
|
| 494 |
/*ecl_set_option(ECL_OPT_SIGNAL_HANDLING_THREAD, 0);*/ |
| 495 |
|
| 496 |
#if !defined(GBC_BOEHM) |
| 497 |
setbuf(stdin, stdin_buf); |
| 498 |
setbuf(stdout, stdout_buf); |
| 499 |
#endif |
| 500 |
init_process(); |
| 501 |
|
| 502 |
ARGC = argc; |
| 503 |
ARGV = argv; |
| 504 |
ecl_self = argv[0]; |
| 505 |
|
| 506 |
init_unixint(0); |
| 507 |
init_alloc(0); |
| 508 |
init_big(); |
| 509 |
|
| 510 |
/* |
| 511 |
* Initialize the per-thread data. |
| 512 |
* This cannot come later, because we need to be able to bind |
| 513 |
* ext::*interrupts-enabled* while creating packages. |
| 514 |
*/ |
| 515 |
|
| 516 |
env = cl_core.first_env; |
| 517 |
ecl_init_first_env(env); |
| 518 |
|
| 519 |
/* |
| 520 |
* 1) Initialize symbols and packages |
| 521 |
*/ |
| 522 |
|
| 523 |
ECL_NIL_SYMBOL->symbol.t = t_symbol; |
| 524 |
ECL_NIL_SYMBOL->symbol.value = ECL_NIL; |
| 525 |
ECL_NIL_SYMBOL->symbol.name = str_NIL; |
| 526 |
ECL_NIL_SYMBOL->symbol.cname = ECL_NIL; |
| 527 |
ECL_FMAKUNBOUND(ECL_NIL_SYMBOL); |
| 528 |
ECL_NIL_SYMBOL->symbol.sfdef = ECL_NIL; |
| 529 |
ECL_NIL_SYMBOL->symbol.macfun = ECL_NIL; |
| 530 |
ECL_NIL_SYMBOL->symbol.plist = ECL_NIL; |
| 531 |
ECL_NIL_SYMBOL->symbol.hpack = ECL_NIL; |
| 532 |
ECL_NIL_SYMBOL->symbol.stype = ecl_stp_constant; |
| 533 |
ECL_NIL_SYMBOL->symbol.undef_entry = ecl_undefined_function_entry; |
| 534 |
#ifdef ECL_THREADS |
| 535 |
ECL_NIL_SYMBOL->symbol.binding = ECL_MISSING_SPECIAL_BINDING; |
| 536 |
#endif |
| 537 |
cl_num_symbols_in_core=1; |
| 538 |
|
| 539 |
ECL_T->symbol.t = (short)t_symbol; |
| 540 |
ECL_T->symbol.value = ECL_T; |
| 541 |
ECL_T->symbol.name = str_T; |
| 542 |
ECL_T->symbol.cname = ECL_NIL; |
| 543 |
ECL_FMAKUNBOUND(ECL_T); |
| 544 |
ECL_T->symbol.sfdef = ECL_NIL; |
| 545 |
ECL_T->symbol.macfun = ECL_NIL; |
| 546 |
ECL_T->symbol.plist = ECL_NIL; |
| 547 |
ECL_T->symbol.hpack = ECL_NIL; |
| 548 |
ECL_T->symbol.stype = ecl_stp_constant; |
| 549 |
ECL_T->symbol.undef_entry = ecl_undefined_function_entry; |
| 550 |
#ifdef ECL_THREADS |
| 551 |
ECL_T->symbol.binding = ECL_MISSING_SPECIAL_BINDING; |
| 552 |
#endif |
| 553 |
cl_num_symbols_in_core=2; |
| 554 |
|
| 555 |
#ifdef NO_PATH_MAX |
| 556 |
cl_core.path_max = sysconf(_PC_PATH_MAX); |
| 557 |
#else |
| 558 |
cl_core.path_max = MAXPATHLEN; |
| 559 |
#endif |
| 560 |
|
| 561 |
cl_core.lisp_package = |
| 562 |
ecl_make_package(str_common_lisp, |
| 563 |
cl_list(1, str_cl), |
| 564 |
ECL_NIL, |
| 565 |
ECL_NIL); |
| 566 |
cl_core.user_package = |
| 567 |
ecl_make_package(str_common_lisp_user, |
| 568 |
cl_list(1, str_cl_user), |
| 569 |
ecl_list1(cl_core.lisp_package), |
| 570 |
ECL_NIL); |
| 571 |
cl_core.keyword_package = |
| 572 |
ecl_make_package(str_keyword, ECL_NIL, ECL_NIL, ECL_NIL); |
| 573 |
cl_core.ext_package = |
| 574 |
ecl_make_package(str_ext, |
| 575 |
ECL_NIL, |
| 576 |
ecl_list1(cl_core.lisp_package), |
| 577 |
ECL_NIL); |
| 578 |
cl_core.system_package = |
| 579 |
ecl_make_package(str_si, |
| 580 |
cl_list(2,str_system,str_sys), |
| 581 |
cl_list(2,cl_core.ext_package, |
| 582 |
cl_core.lisp_package), |
| 583 |
ECL_NIL); |
| 584 |
cl_core.c_package = |
| 585 |
ecl_make_package(str_c, |
| 586 |
ecl_list1(str_compiler), |
| 587 |
ecl_list1(cl_core.lisp_package), |
| 588 |
ECL_NIL); |
| 589 |
cl_core.clos_package = |
| 590 |
ecl_make_package(str_clos, |
| 591 |
ecl_list1(str_mop), |
| 592 |
ecl_list1(cl_core.lisp_package), |
| 593 |
ECL_NIL); |
| 594 |
cl_core.mp_package = |
| 595 |
ecl_make_package(str_mp, |
| 596 |
ecl_list1(str_multiprocessing), |
| 597 |
ecl_list1(cl_core.lisp_package), |
| 598 |
ECL_NIL); |
| 599 |
#ifdef ECL_CLOS_STREAMS |
| 600 |
cl_core.gray_package = ecl_make_package(str_gray, |
| 601 |
ECL_NIL, |
| 602 |
ecl_list1(cl_core.lisp_package), |
| 603 |
ECL_NIL); |
| 604 |
#endif |
| 605 |
cl_core.ffi_package = |
| 606 |
ecl_make_package(str_ffi, |
| 607 |
ECL_NIL, |
| 608 |
cl_list(3,cl_core.lisp_package, |
| 609 |
cl_core.system_package, |
| 610 |
cl_core.ext_package), |
| 611 |
ECL_NIL); |
| 612 |
|
| 613 |
ECL_NIL_SYMBOL->symbol.hpack = cl_core.lisp_package; |
| 614 |
cl_import2(ECL_NIL, cl_core.lisp_package); |
| 615 |
cl_export2(ECL_NIL, cl_core.lisp_package); |
| 616 |
|
| 617 |
ECL_T->symbol.hpack = cl_core.lisp_package; |
| 618 |
cl_import2(ECL_T, cl_core.lisp_package); |
| 619 |
cl_export2(ECL_T, cl_core.lisp_package); |
| 620 |
|
| 621 |
/* At exit, clean up */ |
| 622 |
atexit(cl_shutdown); |
| 623 |
|
| 624 |
/* These must come _after_ the packages and NIL/T have been created */ |
| 625 |
init_all_symbols(); |
| 626 |
|
| 627 |
/* We need to enable GC because a lot of stuff is to be created */ |
| 628 |
init_alloc(1); |
| 629 |
|
| 630 |
/* |
| 631 |
* Set *default-pathname-defaults* to a temporary fake value. We |
| 632 |
* will fix this when we have access to the condition system to |
| 633 |
* allow for error recovery when we can't parse the output of |
| 634 |
* getcwd. |
| 635 |
*/ |
| 636 |
ECL_SET(@'*default-pathname-defaults*', |
| 637 |
ecl_make_pathname(ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL, @':local')); |
| 638 |
|
| 639 |
#ifdef ECL_THREADS |
| 640 |
ECL_SET(@'mp::*current-process*', env->own_process); |
| 641 |
#endif |
| 642 |
|
| 643 |
/* |
| 644 |
* Load character names. The following hash table is a map |
| 645 |
* from names to character codes and viceversa. Note that we |
| 646 |
* need EQUALP because it has to be case insensitive. |
| 647 |
*/ |
| 648 |
cl_core.char_names = aux = |
| 649 |
cl__make_hash_table(@'equalp', ecl_make_fixnum(128), /* size */ |
| 650 |
cl_core.rehash_size, |
| 651 |
cl_core.rehash_threshold); |
| 652 |
for (i = 0; char_names[i].elt.self; i++) { |
| 653 |
cl_object name = (cl_object)(char_names + i); |
| 654 |
cl_object code = ecl_make_fixnum(i); |
| 655 |
ecl_sethash(name, aux, code); |
| 656 |
ecl_sethash(code, aux, name); |
| 657 |
} |
| 658 |
for (i = 0; i < extra_char_names_size; i++) { |
| 659 |
cl_object name = (cl_object)(extra_char_names + i); |
| 660 |
cl_object code = ecl_make_fixnum(extra_char_codes[i]); |
| 661 |
ecl_sethash(name, aux, code); |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
* Initialize logical pathname translations. This must come after |
| 666 |
* the character database has been filled. |
| 667 |
*/ |
| 668 |
@si::pathname-translations(2,str_sys, |
| 669 |
ecl_list1(cl_list(2,str_star_dot_star, |
| 670 |
str_rel_star_dot_star))); |
| 671 |
|
| 672 |
/* |
| 673 |
* Initialize constants (strings, numbers and time). |
| 674 |
*/ |
| 675 |
cl_core.system_properties = |
| 676 |
cl__make_hash_table(@'equal', ecl_make_fixnum(1024), /* size */ |
| 677 |
cl_core.rehash_size, |
| 678 |
cl_core.rehash_threshold); |
| 679 |
|
| 680 |
ECL_SET(@'*random-state*', ecl_make_random_state(ECL_T)); |
| 681 |
|
| 682 |
ECL_SET(@'ffi::c-int-max', ecl_make_integer(INT_MAX)); |
| 683 |
ECL_SET(@'ffi::c-int-min', ecl_make_integer(INT_MIN)); |
| 684 |
ECL_SET(@'ffi::c-long-max', ecl_make_integer(LONG_MAX)); |
| 685 |
ECL_SET(@'ffi::c-long-min', ecl_make_integer(LONG_MIN)); |
| 686 |
ECL_SET(@'ffi::c-uint-max', ecl_make_unsigned_integer(UINT_MAX)); |
| 687 |
ECL_SET(@'ffi::c-ulong-max', ecl_make_unsigned_integer(ULONG_MAX)); |
| 688 |
#ifdef ecl_long_long_t |
| 689 |
ECL_SET(@'ffi::c-long-long-min', ecl_make_long_long(LLONG_MIN)); |
| 690 |
ECL_SET(@'ffi::c-long-long-max', ecl_make_long_long(LLONG_MAX)); |
| 691 |
ECL_SET(@'ffi::c-ulong-long-max', ecl_make_ulong_long(ULLONG_MAX)); |
| 692 |
#endif |
| 693 |
|
| 694 |
init_unixtime(); |
| 695 |
|
| 696 |
/* |
| 697 |
* Initialize I/O subsystem. |
| 698 |
*/ |
| 699 |
init_file(); |
| 700 |
init_read(); |
| 701 |
|
| 702 |
ECL_SET(@'*print-case*', @':upcase'); |
| 703 |
|
| 704 |
/* |
| 705 |
* Set up hooks for LOAD, errors and macros. |
| 706 |
*/ |
| 707 |
#ifdef ECL_THREADS |
| 708 |
ECL_SET(@'mp::+load-compile-lock+', |
| 709 |
ecl_make_lock(@'mp::+load-compile-lock+', 1)); |
| 710 |
#endif |
| 711 |
#ifdef ENABLE_DLOPEN |
| 712 |
aux = cl_list(11, |
| 713 |
CONS(ECL_NIL, @'si::load-source'), |
| 714 |
CONS(str_fas, @'si::load-binary'), |
| 715 |
CONS(str_fasl, @'si::load-binary'), |
| 716 |
CONS(str_fasb, @'si::load-binary'), |
| 717 |
CONS(str_FASB, @'si::load-binary'), |
| 718 |
CONS(str_lsp, @'si::load-source'), |
| 719 |
CONS(str_lisp, @'si::load-source'), |
| 720 |
CONS(str_LSP, @'si::load-source'), |
| 721 |
CONS(str_LISP, @'si::load-source'), |
| 722 |
CONS(str_fasc, @'si::load-bytecodes'), |
| 723 |
CONS(str_FASC, @'si::load-bytecodes')); |
| 724 |
#else |
| 725 |
aux = cl_list(7, |
| 726 |
CONS(ECL_NIL, @'si::load-source'), |
| 727 |
CONS(str_lsp, @'si::load-source'), |
| 728 |
CONS(str_lisp, @'si::load-source'), |
| 729 |
CONS(str_LSP, @'si::load-source'), |
| 730 |
CONS(str_LISP, @'si::load-source'), |
| 731 |
CONS(str_fasc, @'si::load-bytecodes'), |
| 732 |
CONS(str_FASC, @'si::load-bytecodes')); |
| 733 |
#endif |
| 734 |
ECL_SET(@'ext::*load-hooks*', aux); |
| 735 |
init_error(); |
| 736 |
init_macros(); |
| 737 |
init_compiler(); |
| 738 |
|
| 739 |
/* |
| 740 |
* Set up infrastructure for CLOS. |
| 741 |
*/ |
| 742 |
ECL_SET(@'si::*class-name-hash-table*', |
| 743 |
cl__make_hash_table(@'eq', ecl_make_fixnum(1024), /* size */ |
| 744 |
cl_core.rehash_size, |
| 745 |
cl_core.rehash_threshold)); |
| 746 |
|
| 747 |
/* |
| 748 |
* Features. |
| 749 |
*/ |
| 750 |
|
| 751 |
ECL_SET(@'LAMBDA-LIST-KEYWORDS', |
| 752 |
cl_list(8, @'&optional', @'&rest', @'&key', @'&allow-other-keys', |
| 753 |
@'&aux', @'&whole', @'&environment', @'&body')); |
| 754 |
|
| 755 |
ECL_SET(@'*features*', cl_read(1, cl_make_string_input_stream(1, feature_names))); |
| 756 |
|
| 757 |
ECL_SET(@'*package*', cl_core.lisp_package); |
| 758 |
|
| 759 |
/* This has to come before init_LSP/CLOS, because we need |
| 760 |
* ecl_clear_compiler_properties() to work in init_CLOS(). */ |
| 761 |
ecl_set_option(ECL_OPT_BOOTED, 1); |
| 762 |
|
| 763 |
ecl_init_module(OBJNULL,init_lib_LSP); |
| 764 |
|
| 765 |
ECL_HANDLER_CASE_BEGIN(env, ecl_list1(@'ext::stream-decoding-error')) { |
| 766 |
ECL_SET(@'*default-pathname-defaults*', si_getcwd(0)); |
| 767 |
} ECL_HANDLER_CASE(1, c) { |
| 768 |
_ecl_funcall3(@'warn', @"Cannot initialize *DEFAULT-PATHNAME-DEFAULTS* with the current directory:~%~A~%", c); |
| 769 |
} ECL_HANDLER_CASE_END; |
| 770 |
|
| 771 |
if (cl_fboundp(@'ext::make-encoding') != ECL_NIL) { |
| 772 |
maybe_fix_console_stream(cl_core.standard_input); |
| 773 |
maybe_fix_console_stream(cl_core.standard_output); |
| 774 |
maybe_fix_console_stream(cl_core.error_output); |
| 775 |
} |
| 776 |
|
| 777 |
/* Jump to top level */ |
| 778 |
ECL_SET(@'*package*', cl_core.user_package); |
| 779 |
init_unixint(1); |
| 780 |
return 1; |
| 781 |
} |
| 782 |
|
| 783 |
/************************* ENVIRONMENT ROUTINES ***********************/ |
| 784 |
|
| 785 |
@(defun ext::quit (&optional (code ecl_make_fixnum(0)) (kill_all_threads ECL_T)) |
| 786 |
@ { |
| 787 |
#ifdef ECL_THREADS |
| 788 |
if (!Null(kill_all_threads)) { |
| 789 |
cl_object this_process = the_env->own_process; |
| 790 |
cl_object p, all_threads = mp_all_processes(); |
| 791 |
for (p = all_threads; !Null(p); p = ECL_CONS_CDR(p)) { |
| 792 |
cl_object process = ECL_CONS_CAR(p); |
| 793 |
if (process != this_process) |
| 794 |
mp_process_kill(process); |
| 795 |
} |
| 796 |
for (p = all_threads; !Null(p); p = ECL_CONS_CDR(p)) { |
| 797 |
cl_object process = ECL_CONS_CAR(p); |
| 798 |
if (process != this_process) |
| 799 |
mp_process_join(process); |
| 800 |
} |
| 801 |
/* FIXME! We need to do this because of a problem in GC |
| 802 |
* When the thread exits, sometimes the dyld library gets |
| 803 |
* called, and if we call dlopen() at the same time we |
| 804 |
* cause ECL to hang */ |
| 805 |
ecl_musleep(1e-3); |
| 806 |
} |
| 807 |
#endif |
| 808 |
ECL_SET(@'ext::*program-exit-code*', code); |
| 809 |
if (the_env->frs_stack.org <= the_env->frs_stack.top) |
| 810 |
ecl_unwind(the_env, the_env->frs_stack.org); |
| 811 |
si_exit(1, code); |
| 812 |
} |
| 813 |
@) |
| 814 |
|
| 815 |
@(defun ext::exit (&optional (code ECL_SYM_VAL(ecl_process_env(),@'ext::*program-exit-code*'))) |
| 816 |
@ |
| 817 |
cl_shutdown(); |
| 818 |
exit(ECL_FIXNUMP(code)? ecl_fixnum(code) : 0); |
| 819 |
@) |
| 820 |
|
| 821 |
cl_object |
| 822 |
si_argc() |
| 823 |
{ |
| 824 |
@(return ecl_make_fixnum(ARGC)); |
| 825 |
} |
| 826 |
|
| 827 |
cl_object |
| 828 |
si_argv(cl_object index) |
| 829 |
{ |
| 830 |
if (ECL_FIXNUMP(index)) { |
| 831 |
cl_fixnum i = ecl_fixnum(index); |
| 832 |
if (i >= 0 && i < ARGC) { |
| 833 |
@(return ecl_make_simple_base_string(ARGV[i],-1)); |
| 834 |
} |
| 835 |
} |
| 836 |
FEerror("Illegal argument index: ~S.", 1, index); |
| 837 |
} |
| 838 |
|
| 839 |
cl_object |
| 840 |
si_pointer(cl_object x) |
| 841 |
{ |
| 842 |
const cl_env_ptr the_env = ecl_process_env(); |
| 843 |
ecl_return1(the_env, ecl_make_unsigned_integer((cl_index)x)); |
| 844 |
} |
| 845 |
|
| 846 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 847 |
void |
| 848 |
ecl_get_commandline_args(int* argc, char*** argv) { |
| 849 |
/* the caller should use LocalFree to release the memory of strings in argv and argv itself */ |
| 850 |
LPWSTR *wArgs; |
| 851 |
int i; |
| 852 |
|
| 853 |
if (argc == NULL || argv == NULL) |
| 854 |
return; |
| 855 |
|
| 856 |
wArgs = CommandLineToArgvW(GetCommandLineW(), argc); |
| 857 |
*argv = (char**)LocalAlloc(0, sizeof(char*)*(*argc)); |
| 858 |
for (i=0; i<*argc; i++) { |
| 859 |
int len = wcslen(wArgs[i]); |
| 860 |
(*argv)[i] = (char*)LocalAlloc(0, 2*(len+1)); |
| 861 |
wcstombs((*argv)[i], wArgs[i], len+1); |
| 862 |
} |
| 863 |
LocalFree(wArgs); |
| 864 |
} |
| 865 |
#endif |