diff options
Diffstat (limited to 'loadlib.c')
-rw-r--r-- | loadlib.c | 62 |
1 files changed, 46 insertions, 16 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.32 2005/07/11 16:41:57 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.33 2005/07/12 21:17:46 roberto Exp roberto $ |
3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | ** | 5 | ** |
@@ -329,15 +329,41 @@ static int ll_loadlib (lua_State *L) { | |||
329 | */ | 329 | */ |
330 | 330 | ||
331 | 331 | ||
332 | static int readable (const char *fname) { | ||
333 | FILE *f = fopen(fname, "r"); /* try to open file */ | ||
334 | if (f == NULL) return 0; /* open failed */ | ||
335 | fclose(f); | ||
336 | return 1; | ||
337 | } | ||
338 | |||
339 | |||
340 | static const char *pushnexttemplate (lua_State *L, const char *path) { | ||
341 | const char *l; | ||
342 | while (*path == *LUA_PATHSEP) path++; /* skip separators */ | ||
343 | if (*path == '\0') return NULL; /* no more templates */ | ||
344 | l = strchr(path, *LUA_PATHSEP); /* find next separator */ | ||
345 | if (l == NULL) l = path + strlen(path); | ||
346 | lua_pushlstring(L, path, l - path); /* template */ | ||
347 | return l; | ||
348 | } | ||
349 | |||
350 | |||
332 | static const char *findfile (lua_State *L, const char *pname) { | 351 | static const char *findfile (lua_State *L, const char *pname) { |
333 | const char *name = luaL_checkstring(L, 1); | ||
334 | const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
335 | const char *path; | 352 | const char *path; |
353 | const char *name = luaL_checkstring(L, 1); | ||
354 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
336 | lua_getfield(L, LUA_ENVIRONINDEX, pname); | 355 | lua_getfield(L, LUA_ENVIRONINDEX, pname); |
337 | path = lua_tostring(L, -1); | 356 | path = lua_tostring(L, -1); |
338 | if (path == NULL) | 357 | if (path == NULL) |
339 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); | 358 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); |
340 | return luaL_searchpath(L, fname, path); | 359 | while ((path = pushnexttemplate(L, path)) != NULL) { |
360 | const char *fname; | ||
361 | fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | ||
362 | if (readable(fname)) /* does file exist and is readable? */ | ||
363 | return fname; /* return that file name */ | ||
364 | lua_pop(L, 2); /* remove path template and file name */ | ||
365 | } | ||
366 | return NULL; /* not found */ | ||
341 | } | 367 | } |
342 | 368 | ||
343 | 369 | ||
@@ -414,16 +440,21 @@ static int require_aux (lua_State *L, const char *name) { | |||
414 | } | 440 | } |
415 | 441 | ||
416 | 442 | ||
417 | static void ll_error (lua_State *L, const char *name) { | 443 | static void require_check (lua_State *L, const char *name) { |
418 | const char *msg; | 444 | if (!require_aux(L, name)) { /* error? */ |
419 | lua_settop(L, 1); | 445 | /* build and show error message */ |
420 | lua_getfield(L, LUA_ENVIRONINDEX, "path"); | 446 | const char *msg; |
421 | lua_getfield(L, LUA_ENVIRONINDEX, "cpath"); | 447 | lua_settop(L, 1); |
422 | msg = lua_pushfstring(L, "package " LUA_QS " not found in following paths:\n" | 448 | lua_getfield(L, LUA_ENVIRONINDEX, "path"); |
423 | " Lua path: %s\n C path: %s\n", name, | 449 | lua_getfield(L, LUA_ENVIRONINDEX, "cpath"); |
424 | lua_tostring(L, -2), lua_tostring(L, -1)); | 450 | msg = lua_pushfstring(L, |
425 | msg = luaL_gsub(L, msg, LUA_PATHSEP, "\n "); | 451 | "package " LUA_QS " not found in following paths:\n" |
426 | luaL_error(L, msg); | 452 | " Lua path: %s\n" |
453 | " C path: %s\n", name, | ||
454 | lua_tostring(L, -2), lua_tostring(L, -1)); | ||
455 | msg = luaL_gsub(L, msg, LUA_PATHSEP, "\n "); | ||
456 | luaL_error(L, msg); | ||
457 | } | ||
427 | } | 458 | } |
428 | 459 | ||
429 | 460 | ||
@@ -436,8 +467,7 @@ static int ll_require (lua_State *L) { | |||
436 | lua_pushlstring(L, name, pt - name); | 467 | lua_pushlstring(L, name, pt - name); |
437 | require_aux(L, lua_tostring(L, -1)); | 468 | require_aux(L, lua_tostring(L, -1)); |
438 | } | 469 | } |
439 | if (!require_aux(L, name)) /* load module itself */ | 470 | require_check(L, name); /* load module itself */ |
440 | ll_error(L, name); | ||
441 | return 1; | 471 | return 1; |
442 | } | 472 | } |
443 | 473 | ||