diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-11-09 13:18:04 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-11-09 13:18:04 -0200 |
commit | 30cea671a74f26c9485387445ed6d962d9277fae (patch) | |
tree | 1c6adbc91170c415c51f676a6a5b99df6c81a448 /loadlib.c | |
parent | a716ae1f50764aa8f785b0fd85a7f533e53756e0 (diff) | |
download | lua-30cea671a74f26c9485387445ed6d962d9277fae.tar.gz lua-30cea671a74f26c9485387445ed6d962d9277fae.tar.bz2 lua-30cea671a74f26c9485387445ed6d962d9277fae.zip |
configurable 'directory separator' in 'package.searchpath'
Diffstat (limited to 'loadlib.c')
-rw-r--r-- | loadlib.c | 45 |
1 files changed, 34 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.100 2011/07/05 12:49:35 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.101 2011/11/06 13:59:12 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 | ** |
@@ -9,6 +9,14 @@ | |||
9 | */ | 9 | */ |
10 | 10 | ||
11 | 11 | ||
12 | /* | ||
13 | ** if needed, includes windows header before everything else | ||
14 | */ | ||
15 | #if defined(LUA_DL_DLL) | ||
16 | #include <windows.h> | ||
17 | #endif | ||
18 | |||
19 | |||
12 | #include <stdlib.h> | 20 | #include <stdlib.h> |
13 | #include <string.h> | 21 | #include <string.h> |
14 | 22 | ||
@@ -62,6 +70,20 @@ | |||
62 | #endif | 70 | #endif |
63 | 71 | ||
64 | 72 | ||
73 | /* | ||
74 | ** LUA_CSUBSEP is the character that replaces dots in submodule names | ||
75 | ** when searching for a C loader. | ||
76 | ** LUA_LSUBSEP is the character that replaces dots in submodule names | ||
77 | ** when searching for a Lua loader. | ||
78 | */ | ||
79 | #if !defined(LUA_CSUBSEP) | ||
80 | #define LUA_CSUBSEP LUA_DIRSEP | ||
81 | #endif | ||
82 | |||
83 | #if !defined(LUA_LSUBSEP) | ||
84 | #define LUA_LSUBSEP LUA_DIRSEP | ||
85 | #endif | ||
86 | |||
65 | 87 | ||
66 | /* prefix for open functions in C libraries */ | 88 | /* prefix for open functions in C libraries */ |
67 | #define LUA_POF "luaopen_" | 89 | #define LUA_POF "luaopen_" |
@@ -133,8 +155,6 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { | |||
133 | ** ======================================================================= | 155 | ** ======================================================================= |
134 | */ | 156 | */ |
135 | 157 | ||
136 | #include <windows.h> | ||
137 | |||
138 | #undef setprogdir | 158 | #undef setprogdir |
139 | 159 | ||
140 | /* | 160 | /* |
@@ -322,9 +342,10 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { | |||
322 | 342 | ||
323 | static const char *searchpath (lua_State *L, const char *name, | 343 | static const char *searchpath (lua_State *L, const char *name, |
324 | const char *path, | 344 | const char *path, |
325 | const char *sep) { | 345 | const char *sep, |
346 | const char *dirsep) { | ||
326 | if (*sep != '\0') /* non-empty separator? */ | 347 | if (*sep != '\0') /* non-empty separator? */ |
327 | name = luaL_gsub(L, name, sep, LUA_DIRSEP); /* replace it by proper one */ | 348 | name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ |
328 | lua_pushliteral(L, ""); /* error accumulator */ | 349 | lua_pushliteral(L, ""); /* error accumulator */ |
329 | while ((path = pushnexttemplate(L, path)) != NULL) { | 350 | while ((path = pushnexttemplate(L, path)) != NULL) { |
330 | const char *filename = luaL_gsub(L, lua_tostring(L, -1), | 351 | const char *filename = luaL_gsub(L, lua_tostring(L, -1), |
@@ -343,7 +364,8 @@ static const char *searchpath (lua_State *L, const char *name, | |||
343 | static int ll_searchpath (lua_State *L) { | 364 | static int ll_searchpath (lua_State *L) { |
344 | const char *f = searchpath(L, luaL_checkstring(L, 1), | 365 | const char *f = searchpath(L, luaL_checkstring(L, 1), |
345 | luaL_checkstring(L, 2), | 366 | luaL_checkstring(L, 2), |
346 | luaL_optstring(L, 3, ".")); | 367 | luaL_optstring(L, 3, "."), |
368 | luaL_optstring(L, 4, LUA_DIRSEP)); | ||
347 | if (f != NULL) return 1; | 369 | if (f != NULL) return 1; |
348 | else { /* error message is on top of the stack */ | 370 | else { /* error message is on top of the stack */ |
349 | lua_pushnil(L); | 371 | lua_pushnil(L); |
@@ -354,13 +376,14 @@ static int ll_searchpath (lua_State *L) { | |||
354 | 376 | ||
355 | 377 | ||
356 | static const char *findfile (lua_State *L, const char *name, | 378 | static const char *findfile (lua_State *L, const char *name, |
357 | const char *pname) { | 379 | const char *pname, |
380 | const char *dirsep) { | ||
358 | const char *path; | 381 | const char *path; |
359 | lua_getfield(L, lua_upvalueindex(1), pname); | 382 | lua_getfield(L, lua_upvalueindex(1), pname); |
360 | path = lua_tostring(L, -1); | 383 | path = lua_tostring(L, -1); |
361 | if (path == NULL) | 384 | if (path == NULL) |
362 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); | 385 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); |
363 | return searchpath(L, name, path, "."); | 386 | return searchpath(L, name, path, ".", dirsep); |
364 | } | 387 | } |
365 | 388 | ||
366 | 389 | ||
@@ -379,7 +402,7 @@ static int checkload (lua_State *L, int stat, const char *filename) { | |||
379 | static int searcher_Lua (lua_State *L) { | 402 | static int searcher_Lua (lua_State *L) { |
380 | const char *filename; | 403 | const char *filename; |
381 | const char *name = luaL_checkstring(L, 1); | 404 | const char *name = luaL_checkstring(L, 1); |
382 | filename = findfile(L, name, "path"); | 405 | filename = findfile(L, name, "path", LUA_LSUBSEP); |
383 | if (filename == NULL) return 1; /* module not found in this path */ | 406 | if (filename == NULL) return 1; /* module not found in this path */ |
384 | return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); | 407 | return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); |
385 | } | 408 | } |
@@ -405,7 +428,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) { | |||
405 | 428 | ||
406 | static int searcher_C (lua_State *L) { | 429 | static int searcher_C (lua_State *L) { |
407 | const char *name = luaL_checkstring(L, 1); | 430 | const char *name = luaL_checkstring(L, 1); |
408 | const char *filename = findfile(L, name, "cpath"); | 431 | const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); |
409 | if (filename == NULL) return 1; /* module not found in this path */ | 432 | if (filename == NULL) return 1; /* module not found in this path */ |
410 | return checkload(L, (loadfunc(L, filename, name) == 0), filename); | 433 | return checkload(L, (loadfunc(L, filename, name) == 0), filename); |
411 | } | 434 | } |
@@ -418,7 +441,7 @@ static int searcher_Croot (lua_State *L) { | |||
418 | int stat; | 441 | int stat; |
419 | if (p == NULL) return 0; /* is root */ | 442 | if (p == NULL) return 0; /* is root */ |
420 | lua_pushlstring(L, name, p - name); | 443 | lua_pushlstring(L, name, p - name); |
421 | filename = findfile(L, lua_tostring(L, -1), "cpath"); | 444 | filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); |
422 | if (filename == NULL) return 1; /* root not found */ | 445 | if (filename == NULL) return 1; /* root not found */ |
423 | if ((stat = loadfunc(L, filename, name)) != 0) { | 446 | if ((stat = loadfunc(L, filename, name)) != 0) { |
424 | if (stat != ERRFUNC) | 447 | if (stat != ERRFUNC) |