aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-06-29 13:57:56 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-06-29 13:57:56 -0300
commit753625c3f377b013d00a8bd20f4492338e2d87b8 (patch)
tree71375a4499b90c171a1e050f0d085b89502431af
parent42b74ccf1d8c2bb181be6900b068a3a011e106a9 (diff)
downloadlua-753625c3f377b013d00a8bd20f4492338e2d87b8.tar.gz
lua-753625c3f377b013d00a8bd20f4492338e2d87b8.tar.bz2
lua-753625c3f377b013d00a8bd20f4492338e2d87b8.zip
new interface for search-path function
-rw-r--r--lauxlib.c55
-rw-r--r--lauxlib.h8
-rw-r--r--luaconf.h4
3 files changed, 34 insertions, 33 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 24a73324..9f84a77b 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.116 2004/06/17 14:06:52 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.117 2004/06/21 20:05:29 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*/
@@ -354,41 +354,42 @@ static const char *pushnexttemplate (lua_State *L, const char *path) {
354} 354}
355 355
356 356
357static void pushcomposename (lua_State *L) { 357static const char *gsub (lua_State *L, const char *s, const char *p,
358 const char *tem = lua_tostring(L, -1); 358 const char *r) {
359 const char *wild; 359 const char *wild;
360 int n = 1; 360 int l = strlen(p);
361 while ((wild = strchr(tem, LUA_PATH_MARK)) != NULL) { 361 luaL_Buffer b;
362 /* is there stack space for prefix, name, and eventual last suffix? */ 362 luaL_buffinit(L, &b);
363 luaL_checkstack(L, 3, "too many marks in a path component"); 363 while ((wild = strstr(s, p)) != NULL) {
364 lua_pushlstring(L, tem, wild - tem); /* push prefix */ 364 luaL_addlstring(&b, s, wild - s); /* push prefix */
365 lua_pushvalue(L, 1); /* push package name (in place of MARK) */ 365 luaL_addstring(&b, r); /* push replacement in place of pattern */
366 tem = wild + 1; /* continue after MARK */ 366 s = wild + l; /* continue after p */
367 n += 2;
368 } 367 }
369 lua_pushstring(L, tem); /* push last suffix (`n' already includes this) */ 368 luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */
370 lua_concat(L, n); 369 luaL_pushresult(&b);
370 return lua_tostring(L, -1);
371} 371}
372 372
373 373
374LUALIB_API int luaL_searchpath (lua_State *L, const char *name, 374LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
375 const char *path, luaL_Loader f, void *data) { 375 const char *path) {
376 int status; 376 FILE *f;
377 const char *p = path; 377 const char *p = path;
378 int top = lua_gettop(L); 378 for (;;) {
379 do { 379 const char *fname;
380 lua_settop(L, top);
381 if ((p = pushnexttemplate(L, p)) == NULL) { 380 if ((p = pushnexttemplate(L, p)) == NULL) {
382 lua_pushfstring(L, "cound not find `%s' in path `%s'", name, path); 381 lua_pushfstring(L, "no readable `%s' in path `%s'", name, path);
383 return LUA_ERRFILE; 382 return NULL;
384 } 383 }
385 pushcomposename(L); 384 fname = gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
386 lua_remove(L, -2); /* remove path template */ 385 lua_remove(L, -2); /* remove path template */
387 lua_assert(lua_gettop(L) == top + 1); 386 f = fopen(fname, "r"); /* try to read it */
388 status = f(data, lua_tostring(L, -1)); /* try to load it */ 387 if (f) {
389 lua_remove(L, top+1); /* remove file name */ 388 fclose(f);
390 } while (status == LUA_ERRFILE); 389 return fname;
391 return status; 390 }
391 lua_pop(L, 1); /* remove file name */
392 }
392} 393}
393 394
394 395
diff --git a/lauxlib.h b/lauxlib.h
index ba83d14a..176aa240 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.66 2004/06/02 17:37:03 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.67 2004/06/21 20:05:29 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*/
@@ -25,7 +25,7 @@ typedef struct luaL_reg {
25} luaL_reg; 25} luaL_reg;
26 26
27 27
28typedef int (*luaL_Loader)(void *data, const char *name); 28typedef int (*luaL_Loader)(lua_State *L, const char *name);
29 29
30LUALIB_API void luaL_openlib (lua_State *L, const char *libname, 30LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
31 const luaL_reg *l, int nup); 31 const luaL_reg *l, int nup);
@@ -56,8 +56,8 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...);
56 56
57LUALIB_API int luaL_findstring (const char *st, const char *const lst[]); 57LUALIB_API int luaL_findstring (const char *st, const char *const lst[]);
58 58
59LUALIB_API int luaL_searchpath (lua_State *L, const char *name, 59LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
60 const char *path, luaL_Loader f, void *data); 60 const char *path);
61 61
62LUALIB_API int luaL_ref (lua_State *L, int t); 62LUALIB_API int luaL_ref (lua_State *L, int t);
63LUALIB_API void luaL_unref (lua_State *L, int t, int ref); 63LUALIB_API void luaL_unref (lua_State *L, int t, int ref);
diff --git a/luaconf.h b/luaconf.h
index dcc96cd1..a8bcec4a 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luaconf.h,v 1.6 2004/06/02 19:07:55 roberto Exp roberto $ 2** $Id: luaconf.h,v 1.7 2004/06/23 15:57:29 roberto Exp roberto $
3** Configuration file for Lua 3** Configuration file for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -271,7 +271,7 @@
271#define LUA_PATH_SEP ';' 271#define LUA_PATH_SEP ';'
272 272
273/* wild char in each template */ 273/* wild char in each template */
274#define LUA_PATH_MARK '?' 274#define LUA_PATH_MARK "?"
275 275
276/* default path */ 276/* default path */
277#define LUA_PATH_DEFAULT "?;?.lua" 277#define LUA_PATH_DEFAULT "?;?.lua"