aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-07-11 13:41:57 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-07-11 13:41:57 -0300
commite4324f54b9f842f3e1187acf0a3e7f320de518ab (patch)
treeaa03c4bdfd0c0cbac375323446fb092574e047aa
parent3d08aa591ddbfdaff49804baf09ea5044c29c9c9 (diff)
downloadlua-e4324f54b9f842f3e1187acf0a3e7f320de518ab.tar.gz
lua-e4324f54b9f842f3e1187acf0a3e7f320de518ab.tar.bz2
lua-e4324f54b9f842f3e1187acf0a3e7f320de518ab.zip
';;' in a path is replaced by default path + '!' (in Windows) is
replaced by executable's directory
-rw-r--r--loadlib.c64
1 files changed, 46 insertions, 18 deletions
diff --git a/loadlib.c b/loadlib.c
index bf97f55b..9103ccf0 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.30 2005/06/27 17:24:40 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.31 2005/07/05 19:29:03 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**
@@ -38,6 +38,9 @@
38#define LIB_FAIL "open" 38#define LIB_FAIL "open"
39 39
40 40
41#define setprogdir(L) ((void)0)
42
43
41static void ll_unloadlib (void *lib); 44static void ll_unloadlib (void *lib);
42static void *ll_load (lua_State *L, const char *path); 45static void *ll_load (lua_State *L, const char *path);
43static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); 46static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
@@ -86,6 +89,21 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
86*/ 89*/
87 90
88#include <windows.h> 91#include <windows.h>
92#include "Shlwapi.h"
93
94
95#undef setprogdir
96
97void setprogdir (lua_State *L) {
98 char buff[MAX_PATH + 1];
99 DWORD nsize = sizeof(buff)/sizeof(char);
100 DWORD n = GetModuleFileName(NULL, buff, nsize);
101 if (n == 0 || n == nsize)
102 luaL_error(L, "unable to get ModuleFileName");
103 PathRemoveFileSpec(buff);
104 luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
105 lua_remove(L, -2); /* remove original string */
106}
89 107
90 108
91static void pusherror (lua_State *L) { 109static void pusherror (lua_State *L) {
@@ -383,14 +401,18 @@ static void require_aux (lua_State *L, const char *name) {
383 luaL_error(L, "package " LUA_QS " not found", name); 401 luaL_error(L, "package " LUA_QS " not found", name);
384 lua_pushstring(L, name); 402 lua_pushstring(L, name);
385 lua_call(L, 1, 1); /* call it */ 403 lua_call(L, 1, 1); /* call it */
386 if (lua_isnil(L, -1)) lua_pop(L, 1); 404 if (lua_isnil(L, -1)) lua_pop(L, 1); /* did not found module */
387 else break; /* module loaded successfully */ 405 else break; /* module loaded successfully */
388 } 406 }
389 /* mark module as loaded */
390 lua_pushboolean(L, 1); 407 lua_pushboolean(L, 1);
391 lua_setfield(L, loadedtable, name); /* _LOADED[name] = true */ 408 lua_setfield(L, loadedtable, name); /* _LOADED[name] = true */
392 lua_pushstring(L, name); /* pass name as argument to module */ 409 lua_pushstring(L, name); /* pass name as argument to module */
393 lua_call(L, 1, 1); /* run loaded module */ 410 if (lua_pcall(L, 1, 1, 0) != 0) { /* run loaded module */
411 lua_pushnil(L); /* in case of errors... */
412 lua_setfield(L, loadedtable, name); /* ...clear _LOADED[name] */
413 luaL_error(L, "error loading package " LUA_QS " (%s)",
414 name, lua_tostring(L, -1)); /* propagate error */
415 }
394 if (!lua_isnil(L, -1)) /* non-nil return? */ 416 if (!lua_isnil(L, -1)) /* non-nil return? */
395 lua_setfield(L, loadedtable, name); /* _LOADED[name] = returned value */ 417 lua_setfield(L, loadedtable, name); /* _LOADED[name] = returned value */
396 lua_getfield(L, loadedtable, name); /* return _LOADED[name] */ 418 lua_getfield(L, loadedtable, name); /* return _LOADED[name] */
@@ -468,6 +490,22 @@ static int ll_module (lua_State *L) {
468/* }====================================================== */ 490/* }====================================================== */
469 491
470 492
493static void setpath (lua_State *L, const char *fname, const char *envname,
494 const char *def) {
495 const char *path = getenv(envname);
496 if (path == NULL) lua_pushstring(L, def);
497 else {
498 /* replace ";;" by default path */
499 path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
500 LUA_PATHSEP"\1"LUA_PATHSEP);
501 luaL_gsub(L, path, "\1", def);
502 lua_remove(L, -2);
503 }
504 setprogdir(L);
505 lua_setfield(L, -2, fname);
506}
507
508
471static const luaL_reg ll_funcs[] = { 509static const luaL_reg ll_funcs[] = {
472 {"require", ll_require}, 510 {"require", ll_require},
473 {"module", ll_module}, 511 {"module", ll_module},
@@ -476,11 +514,10 @@ static const luaL_reg ll_funcs[] = {
476 514
477 515
478static const lua_CFunction loaders[] = 516static const lua_CFunction loaders[] =
479 {loader_preload, loader_C, loader_Lua, NULL}; 517 {loader_preload, loader_Lua, loader_C, NULL};
480 518
481 519
482LUALIB_API int luaopen_loadlib (lua_State *L) { 520LUALIB_API int luaopen_loadlib (lua_State *L) {
483 const char *path;
484 int i; 521 int i;
485 /* create new type _LOADLIB */ 522 /* create new type _LOADLIB */
486 luaL_newmetatable(L, "_LOADLIB"); 523 luaL_newmetatable(L, "_LOADLIB");
@@ -501,18 +538,9 @@ LUALIB_API int luaopen_loadlib (lua_State *L) {
501 lua_pushcfunction(L, loaders[i]); 538 lua_pushcfunction(L, loaders[i]);
502 lua_rawseti(L, -2, i+1); 539 lua_rawseti(L, -2, i+1);
503 } 540 }
504 /* put it in field `loaders' */ 541 lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
505 lua_setfield(L, -2, "loaders"); 542 setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
506 /* set field `path' */ 543 setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
507 path = getenv(LUA_PATH);
508 if (path == NULL) path = LUA_PATH_DEFAULT;
509 lua_pushstring(L, path);
510 lua_setfield(L, -2, "path");
511 /* set field `cpath' */
512 path = getenv(LUA_CPATH);
513 if (path == NULL) path = LUA_CPATH_DEFAULT;
514 lua_pushstring(L, path);
515 lua_setfield(L, -2, "cpath");
516 /* set field `loaded' */ 544 /* set field `loaded' */
517 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 545 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
518 lua_setfield(L, -2, "loaded"); 546 lua_setfield(L, -2, "loaded");