aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lauxlib.c22
-rw-r--r--lbaselib.c16
-rw-r--r--lua.c14
3 files changed, 23 insertions, 29 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 9f84a77b..d465042e 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -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
344static 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
346static const char *pushnexttemplate (lua_State *L, const char *path) { 355static 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
357static const char *gsub (lua_State *L, const char *s, const char *p, 366static 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 */
diff --git a/lbaselib.c b/lbaselib.c
index e8d33229..a7d694ae 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.149 2004/06/21 20:05:29 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.150 2004/06/29 16:58:17 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -459,20 +459,8 @@ static int luaB_newproxy (lua_State *L) {
459*/ 459*/
460 460
461 461
462static const char *getpath (lua_State *L) {
463 const char *path;
464 lua_getglobal(L, LUA_PATH); /* try global variable */
465 path = lua_tostring(L, -1);
466 if (path) return path;
467 path = getenv(LUA_PATH); /* else try environment variable */
468 if (path) return path;
469 return LUA_PATH_DEFAULT; /* else use default */
470}
471
472
473static int luaB_require (lua_State *L) { 462static int luaB_require (lua_State *L) {
474 const char *name = luaL_checkstring(L, 1); 463 const char *name = luaL_checkstring(L, 1);
475 const char *path = getpath(L);
476 const char *fname; 464 const char *fname;
477 int loaded; 465 int loaded;
478 lua_getglobal(L, REQTAB); 466 lua_getglobal(L, REQTAB);
@@ -485,7 +473,7 @@ static int luaB_require (lua_State *L) {
485 /* else must load it; first mark it as loaded */ 473 /* else must load it; first mark it as loaded */
486 lua_pushboolean(L, 1); 474 lua_pushboolean(L, 1);
487 lua_setfield(L, loaded, name); /* _LOADED[name] = true */ 475 lua_setfield(L, loaded, name); /* _LOADED[name] = true */
488 fname = luaL_searchpath(L, name, path); 476 fname = luaL_searchpath(L, name, NULL);
489 if (fname == NULL || luaL_loadfile(L, fname) != 0) 477 if (fname == NULL || luaL_loadfile(L, fname) != 0)
490 return luaL_error(L, "error loading package `%s' (%s)", name, 478 return luaL_error(L, "error loading package `%s' (%s)", name,
491 lua_tostring(L, -1)); 479 lua_tostring(L, -1));
diff --git a/lua.c b/lua.c
index d57db025..dac53e29 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.127 2004/06/16 20:22:43 roberto Exp roberto $ 2** $Id: lua.c,v 1.128 2004/06/17 14:06:52 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -143,15 +143,9 @@ static int dostring (const char *s, const char *name) {
143 143
144 144
145static int load_file (const char *name) { 145static int load_file (const char *name) {
146 lua_getglobal(L, "require"); 146 name = luaL_searchpath(L, name, NULL);
147 if (!lua_isfunction(L, -1)) { /* no `require' defined? */ 147 if (name == NULL) return report(1);
148 lua_pop(L, 1); 148 else return file_input(name);
149 return file_input(name);
150 }
151 else {
152 lua_pushstring(L, name);
153 return report(lcall(1, 1));
154 }
155} 149}
156 150
157 151