diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-07-01 11:26:28 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-07-01 11:26:28 -0300 |
commit | 80ec81926c19ff638a65088bb3d5a55d20b55945 (patch) | |
tree | 6630fe28b6b20f83fd69ad64bc71fa4bc546b491 /lauxlib.c | |
parent | 96917ff42a7867e72629d6cd39c818df4c250b91 (diff) | |
download | lua-80ec81926c19ff638a65088bb3d5a55d20b55945.tar.gz lua-80ec81926c19ff638a65088bb3d5a55d20b55945.tar.bz2 lua-80ec81926c19ff638a65088bb3d5a55d20b55945.zip |
`lua.c' also needs the default path, so auxlib should provide it
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 22 |
1 files changed, 17 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.117 2004/06/21 20:05:29 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.118 2004/06/29 16:57:56 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -341,6 +341,15 @@ LUALIB_API int luaL_getn (lua_State *L, int t) { | |||
341 | /* }====================================================== */ | 341 | /* }====================================================== */ |
342 | 342 | ||
343 | 343 | ||
344 | static const char *getpath (lua_State *L) { | ||
345 | const char *path; | ||
346 | lua_getglobal(L, LUA_PATH); /* try global variable */ | ||
347 | path = lua_tostring(L, -1); | ||
348 | if (path) return path; | ||
349 | path = getenv(LUA_PATH); /* else try environment variable */ | ||
350 | if (path) return path; | ||
351 | return LUA_PATH_DEFAULT; /* else use default */ | ||
352 | } | ||
344 | 353 | ||
345 | 354 | ||
346 | static const char *pushnexttemplate (lua_State *L, const char *path) { | 355 | static const char *pushnexttemplate (lua_State *L, const char *path) { |
@@ -354,8 +363,8 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { | |||
354 | } | 363 | } |
355 | 364 | ||
356 | 365 | ||
357 | static const char *gsub (lua_State *L, const char *s, const char *p, | 366 | static const char *luaL_gsub (lua_State *L, const char *s, |
358 | const char *r) { | 367 | const char *p, const char *r) { |
359 | const char *wild; | 368 | const char *wild; |
360 | int l = strlen(p); | 369 | int l = strlen(p); |
361 | luaL_Buffer b; | 370 | luaL_Buffer b; |
@@ -363,7 +372,7 @@ static const char *gsub (lua_State *L, const char *s, const char *p, | |||
363 | while ((wild = strstr(s, p)) != NULL) { | 372 | while ((wild = strstr(s, p)) != NULL) { |
364 | luaL_addlstring(&b, s, wild - s); /* push prefix */ | 373 | luaL_addlstring(&b, s, wild - s); /* push prefix */ |
365 | luaL_addstring(&b, r); /* push replacement in place of pattern */ | 374 | luaL_addstring(&b, r); /* push replacement in place of pattern */ |
366 | s = wild + l; /* continue after p */ | 375 | s = wild + l; /* continue after `p' */ |
367 | } | 376 | } |
368 | luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */ | 377 | luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */ |
369 | luaL_pushresult(&b); | 378 | luaL_pushresult(&b); |
@@ -375,17 +384,20 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | |||
375 | const char *path) { | 384 | const char *path) { |
376 | FILE *f; | 385 | FILE *f; |
377 | const char *p = path; | 386 | const char *p = path; |
387 | if (p == NULL) p = getpath(L); | ||
388 | else lua_pushnil(L); /* to balance item pushed by `getpath' */ | ||
378 | for (;;) { | 389 | for (;;) { |
379 | const char *fname; | 390 | const char *fname; |
380 | if ((p = pushnexttemplate(L, p)) == NULL) { | 391 | if ((p = pushnexttemplate(L, p)) == NULL) { |
381 | lua_pushfstring(L, "no readable `%s' in path `%s'", name, path); | 392 | lua_pushfstring(L, "no readable `%s' in path `%s'", name, path); |
382 | return NULL; | 393 | return NULL; |
383 | } | 394 | } |
384 | fname = gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | 395 | fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); |
385 | lua_remove(L, -2); /* remove path template */ | 396 | lua_remove(L, -2); /* remove path template */ |
386 | f = fopen(fname, "r"); /* try to read it */ | 397 | f = fopen(fname, "r"); /* try to read it */ |
387 | if (f) { | 398 | if (f) { |
388 | fclose(f); | 399 | fclose(f); |
400 | lua_remove(L, -2); /* remove path */ | ||
389 | return fname; | 401 | return fname; |
390 | } | 402 | } |
391 | lua_pop(L, 1); /* remove file name */ | 403 | lua_pop(L, 1); /* remove file name */ |