| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* load.d - binary loader (contains also open_fasl_data) |
| 6 |
* |
| 7 |
* Copyright (c) 1990 Giuseppe Attardi |
| 8 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 9 |
* |
| 10 |
* See file 'LICENSE' for the copyright details. |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <ecl/ecl.h> |
| 15 |
#include <ecl/ecl-inl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
#include <errno.h> |
| 18 |
|
| 19 |
#ifdef ENABLE_DLOPEN |
| 20 |
cl_object |
| 21 |
si_load_binary(cl_object filename, cl_object verbose, |
| 22 |
cl_object print, cl_object external_format) |
| 23 |
{ |
| 24 |
const cl_env_ptr the_env = ecl_process_env(); |
| 25 |
cl_object block; |
| 26 |
cl_object basename; |
| 27 |
cl_object init_prefix, prefix; |
| 28 |
cl_object output; |
| 29 |
|
| 30 |
/* We need the full pathname */ |
| 31 |
filename = cl_truename(filename); |
| 32 |
|
| 33 |
/* Try to load shared object file */ |
| 34 |
block = ecl_library_open(filename, 1); |
| 35 |
if (block->cblock.handle == NULL) { |
| 36 |
output = ecl_library_error(block); |
| 37 |
goto OUTPUT; |
| 38 |
} |
| 39 |
|
| 40 |
/* Fist try to call "init_CODE()" */ |
| 41 |
init_prefix = _ecl_library_default_entry(); |
| 42 |
block->cblock.entry = |
| 43 |
ecl_library_symbol(block, (char *)init_prefix->base_string.self, 0); |
| 44 |
if (block->cblock.entry != NULL) |
| 45 |
goto GO_ON; |
| 46 |
|
| 47 |
/* Next try to call "init_FILE()" where FILE is the file name */ |
| 48 |
prefix = ecl_cmp_symbol_value(the_env, @'si::*init-function-prefix*'); |
| 49 |
init_prefix = _ecl_library_init_prefix(); |
| 50 |
if (Null(prefix)) { |
| 51 |
prefix = init_prefix; |
| 52 |
} else { |
| 53 |
prefix = @si::base-string-concatenate(3, |
| 54 |
init_prefix, |
| 55 |
prefix, |
| 56 |
@"_"); |
| 57 |
} |
| 58 |
basename = cl_pathname_name(1,filename); |
| 59 |
basename = @si::base-string-concatenate(2, prefix, @string-upcase(1, funcall(4, @'nsubstitute', ECL_CODE_CHAR('_'), ECL_CODE_CHAR('-'), basename))); |
| 60 |
block->cblock.entry = ecl_library_symbol(block, (char*)basename->base_string.self, 0); |
| 61 |
|
| 62 |
if (block->cblock.entry == NULL) { |
| 63 |
output = ecl_library_error(block); |
| 64 |
ecl_library_close(block); |
| 65 |
goto OUTPUT; |
| 66 |
} |
| 67 |
|
| 68 |
GO_ON: |
| 69 |
/* Finally, perform initialization */ |
| 70 |
ecl_init_module(block, (void (*)(cl_object))(block->cblock.entry)); |
| 71 |
output = ECL_NIL; |
| 72 |
OUTPUT: |
| 73 |
ecl_return1(the_env, output); |
| 74 |
} |
| 75 |
#endif /* !ENABLE_DLOPEN */ |
| 76 |
|
| 77 |
cl_object |
| 78 |
si_load_source(cl_object source, cl_object verbose, cl_object print, cl_object external_format) |
| 79 |
{ |
| 80 |
cl_env_ptr the_env = ecl_process_env(); |
| 81 |
cl_object x, strm; |
| 82 |
|
| 83 |
/* Source may be either a stream or a filename */ |
| 84 |
if (ecl_t_of(source) != t_pathname && ecl_t_of(source) != t_base_string) { |
| 85 |
/* INV: if "source" is not a valid stream, file.d will complain */ |
| 86 |
strm = source; |
| 87 |
} |
| 88 |
else { |
| 89 |
strm = ecl_open_stream(source, ecl_smm_input, ECL_NIL, ECL_NIL, 8, |
| 90 |
ECL_STREAM_C_STREAM, external_format); |
| 91 |
if (Null(strm)) { |
| 92 |
@(return ECL_NIL); |
| 93 |
} |
| 94 |
} |
| 95 |
ECL_UNWIND_PROTECT_BEGIN(the_env) { |
| 96 |
cl_object form_index = ecl_make_fixnum(0); |
| 97 |
cl_object pathname = ECL_SYM_VAL(the_env, @'*load-pathname*'); |
| 98 |
cl_object location = CONS(pathname, form_index); |
| 99 |
ecl_bds_bind(the_env, @'ext::*source-location*', location); |
| 100 |
for (;;) { |
| 101 |
form_index = ecl_file_position(strm); |
| 102 |
ECL_RPLACD(location, form_index); |
| 103 |
x = si_read_object_or_ignore(strm, OBJNULL); |
| 104 |
if (x == OBJNULL) |
| 105 |
break; |
| 106 |
if (the_env->nvalues) { |
| 107 |
si_eval_with_env(1, x); |
| 108 |
if (print != ECL_NIL) { |
| 109 |
@write(1, x); |
| 110 |
@terpri(0); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
ecl_bds_unwind1(the_env); |
| 115 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 116 |
/* We do not want to come back here if close_stream fails, |
| 117 |
therefore, first we frs_pop() current jump point, then |
| 118 |
try to close the stream, and then jump to next catch |
| 119 |
point */ |
| 120 |
if (strm != source) |
| 121 |
cl_close(3, strm, @':abort', ECL_T); |
| 122 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 123 |
@(return ECL_NIL); |
| 124 |
} |
| 125 |
|
| 126 |
static cl_object |
| 127 |
read_forms(cl_object stream, cl_object errorp) { |
| 128 |
cl_env_ptr env = ecl_process_env(); |
| 129 |
cl_object forms; |
| 130 |
env->packages_to_be_created_p = ECL_T; |
| 131 |
forms = cl_read(3, stream, errorp, ECL_NIL); |
| 132 |
env->packages_to_be_created_p = ECL_NIL; |
| 133 |
return forms; |
| 134 |
} |
| 135 |
|
| 136 |
cl_object |
| 137 |
si_load_bytecodes(cl_object source, cl_object verbose, cl_object print, cl_object external_format) |
| 138 |
{ |
| 139 |
cl_env_ptr env = ecl_process_env(); |
| 140 |
cl_object forms, strm; |
| 141 |
cl_object old_eptbc = env->packages_to_be_created; |
| 142 |
|
| 143 |
/* Source may be either a stream or a filename */ |
| 144 |
if (ecl_t_of(source) != t_pathname && ecl_t_of(source) != t_base_string) { |
| 145 |
/* INV: if "source" is not a valid stream, file.d will complain */ |
| 146 |
strm = source; |
| 147 |
} else { |
| 148 |
strm = ecl_open_stream(source, ecl_smm_input, ECL_NIL, ECL_NIL, 8, |
| 149 |
ECL_STREAM_C_STREAM, external_format); |
| 150 |
if (Null(strm)) { |
| 151 |
@(return ECL_NIL); |
| 152 |
} |
| 153 |
} |
| 154 |
ECL_UNWIND_PROTECT_BEGIN(env) { |
| 155 |
{ |
| 156 |
cl_object progv_list = ECL_SYM_VAL(env, @'si::+ecl-syntax-progv-list+'); |
| 157 |
cl_index bds_ndx = ecl_progv(env, ECL_CONS_CAR(progv_list), |
| 158 |
ECL_CONS_CDR(progv_list)); |
| 159 |
forms = read_forms(strm, ECL_T); |
| 160 |
ecl_bds_unwind(env, bds_ndx); |
| 161 |
} |
| 162 |
while (!Null(forms)) { |
| 163 |
if (ECL_LISTP(forms)) { |
| 164 |
cl_object x = ECL_CONS_CAR(forms); |
| 165 |
forms = ECL_CONS_CDR(forms); |
| 166 |
if (ecl_t_of(x) == t_bytecodes) { |
| 167 |
_ecl_funcall1(x); |
| 168 |
if (Null(forms)) { |
| 169 |
forms = read_forms(strm, ECL_NIL); |
| 170 |
} |
| 171 |
continue; |
| 172 |
} |
| 173 |
} |
| 174 |
FEerror("Corrupt bytecodes file ~S", 1, source); |
| 175 |
} |
| 176 |
{ |
| 177 |
cl_object x; |
| 178 |
x = cl_set_difference(2, env->packages_to_be_created, old_eptbc); |
| 179 |
old_eptbc = env->packages_to_be_created; |
| 180 |
unlikely_if (!Null(x)) { |
| 181 |
CEerror(ECL_T, |
| 182 |
Null(ECL_CONS_CDR(x))? |
| 183 |
"Package ~A referenced in " |
| 184 |
"compiled file~& ~A~&but has not been created": |
| 185 |
"The packages~& ~A~&were referenced in " |
| 186 |
"compiled file~& ~A~&but have not been created", |
| 187 |
2, x, source); |
| 188 |
} |
| 189 |
} |
| 190 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT { |
| 191 |
/* We do not want to come back here if close_stream fails, |
| 192 |
therefore, first we frs_pop() current jump point, then |
| 193 |
try to close the stream, and then jump to next catch |
| 194 |
point */ |
| 195 |
if (strm != source) { |
| 196 |
cl_close(3, strm, @':abort', ECL_T); |
| 197 |
} |
| 198 |
} ECL_UNWIND_PROTECT_THREAD_SAFE_END; |
| 199 |
@(return ECL_NIL); |
| 200 |
} |
| 201 |
|
| 202 |
@(defun load (source |
| 203 |
&key (verbose ecl_cmp_symbol_value(the_env, @'*load-verbose*')) |
| 204 |
(print ecl_cmp_symbol_value(the_env, @'*load-print*')) |
| 205 |
(if_does_not_exist @':error') |
| 206 |
(external_format @':default') |
| 207 |
(search_list ecl_cmp_symbol_value(the_env, @'si::*load-search-list*')) |
| 208 |
&aux pathname pntype hooks filename function ok file_kind) |
| 209 |
bool not_a_filename = 0; |
| 210 |
@ |
| 211 |
/* If source is a stream, read conventional lisp code from it */ |
| 212 |
if (ecl_t_of(source) != t_pathname && !ecl_stringp(source)) { |
| 213 |
/* INV: if "source" is not a valid stream, file.d will complain */ |
| 214 |
filename = source; |
| 215 |
function = ECL_NIL; |
| 216 |
not_a_filename = 1; |
| 217 |
goto NOT_A_FILENAME; |
| 218 |
} |
| 219 |
/* INV: coerce_to_file_pathname() creates a fresh new pathname object */ |
| 220 |
source = cl_merge_pathnames(1, source); |
| 221 |
pathname = si_coerce_to_file_pathname(source); |
| 222 |
pntype = pathname->pathname.type; |
| 223 |
|
| 224 |
filename = ECL_NIL; |
| 225 |
hooks = ecl_cmp_symbol_value(the_env, @'ext::*load-hooks*'); |
| 226 |
if (Null(pathname->pathname.directory) && |
| 227 |
Null(pathname->pathname.host) && |
| 228 |
Null(pathname->pathname.device) && |
| 229 |
!Null(search_list)) |
| 230 |
{ |
| 231 |
loop_for_in(search_list) { |
| 232 |
cl_object d = CAR(search_list); |
| 233 |
cl_object f = cl_merge_pathnames(2, pathname, d); |
| 234 |
cl_object ok = cl_load(11, f, @':verbose', verbose, |
| 235 |
@':print', print, |
| 236 |
@':if-does-not-exist', ECL_NIL, |
| 237 |
@':external-format', external_format, |
| 238 |
@':search-list', ECL_NIL); |
| 239 |
if (!Null(ok)) { |
| 240 |
@(return ok); |
| 241 |
} |
| 242 |
} end_loop_for_in; |
| 243 |
} |
| 244 |
if (!Null(pntype) && (pntype != @':wild')) { |
| 245 |
/* If filename already has an extension, make sure |
| 246 |
that the file exists */ |
| 247 |
filename = pathname; |
| 248 |
file_kind = si_file_kind(pathname, ECL_T); |
| 249 |
if (file_kind != @':file' && file_kind != @':special') { |
| 250 |
filename = ECL_NIL; |
| 251 |
} else { |
| 252 |
function = cl_cdr(ecl_assoc(pathname->pathname.type, hooks)); |
| 253 |
} |
| 254 |
} else loop_for_in(hooks) { |
| 255 |
/* Otherwise try with known extensions until a matching |
| 256 |
file is found */ |
| 257 |
filename = pathname; |
| 258 |
filename->pathname.type = CAAR(hooks); |
| 259 |
function = CDAR(hooks); |
| 260 |
file_kind = si_file_kind(filename, ECL_T); |
| 261 |
if (file_kind == @':file' || file_kind == @':special') |
| 262 |
break; |
| 263 |
else |
| 264 |
filename = ECL_NIL; |
| 265 |
} end_loop_for_in; |
| 266 |
if (Null(filename)) { |
| 267 |
if (Null(if_does_not_exist)) { |
| 268 |
@(return ECL_NIL); |
| 269 |
} else { |
| 270 |
if (file_kind == @':directory') { |
| 271 |
errno = EISDIR; |
| 272 |
#ifdef ECL_MS_WINDOWS_HOST |
| 273 |
} else { |
| 274 |
/* The functions used by si_file_kind report no useful errors |
| 275 |
* on Windows, so just stick with ENOENT here. */ |
| 276 |
errno = ENOENT; |
| 277 |
#endif |
| 278 |
} |
| 279 |
FEcannot_open(source); |
| 280 |
} |
| 281 |
} |
| 282 |
NOT_A_FILENAME: |
| 283 |
if (verbose != ECL_NIL) { |
| 284 |
cl_format(3, ECL_T, @"~&;;; Loading ~s~%", |
| 285 |
filename); |
| 286 |
} |
| 287 |
ecl_bds_bind(the_env, @'*package*', ecl_cmp_symbol_value(the_env, @'*package*')); |
| 288 |
ecl_bds_bind(the_env, @'*readtable*', ecl_cmp_symbol_value(the_env, @'*readtable*')); |
| 289 |
ecl_bds_bind(the_env, @'*load-pathname*', not_a_filename? ECL_NIL : source); |
| 290 |
ecl_bds_bind(the_env, @'*load-truename*', |
| 291 |
not_a_filename? ECL_NIL : (filename = cl_truename(filename))); |
| 292 |
if (!Null(function)) { |
| 293 |
ok = funcall(5, function, filename, verbose, print, external_format); |
| 294 |
} else { |
| 295 |
#if 0 /* defined(ENABLE_DLOPEN) && !defined(ECL_MS_WINDOWS_HOST)*/ |
| 296 |
/* |
| 297 |
* DISABLED BECAUSE OF SECURITY ISSUES! |
| 298 |
* In systems where we can do this, we try to load the file |
| 299 |
* as a binary. When it fails, we will revert to source |
| 300 |
* loading below. Is this safe? Well, it depends on whether |
| 301 |
* your op.sys. checks integrity of binary exectables or |
| 302 |
* just loads _anything_. |
| 303 |
*/ |
| 304 |
if (not_a_filename) { |
| 305 |
ok = ECL_T; |
| 306 |
} else { |
| 307 |
ok = si_load_binary(filename, verbose, print); |
| 308 |
} |
| 309 |
if (!Null(ok)) |
| 310 |
#endif |
| 311 |
ok = si_load_source(filename, verbose, print, external_format); |
| 312 |
} |
| 313 |
ecl_bds_unwind_n(the_env, 4); |
| 314 |
if (!Null(ok)) |
| 315 |
FEerror("LOAD: Could not load file ~S (Error: ~S)", |
| 316 |
2, filename, ok); |
| 317 |
if (print != ECL_NIL) { |
| 318 |
cl_format(3, ECL_T, @"~&;;; Loading ~s~%", |
| 319 |
filename); |
| 320 |
} |
| 321 |
@(return filename); |
| 322 |
@) |