aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-07-13 16:02:42 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-07-13 16:02:42 -0300
commitd6ff5d9f46be3db99dd3fec0c12504b7ba1854e1 (patch)
tree268a21e18ee7d43fd9147f89b6039b1b11b2536d
parenta05190fa3b5c3685b0772f0235f136e05587fa9c (diff)
downloadlua-d6ff5d9f46be3db99dd3fec0c12504b7ba1854e1.tar.gz
lua-d6ff5d9f46be3db99dd3fec0c12504b7ba1854e1.tar.bz2
lua-d6ff5d9f46be3db99dd3fec0c12504b7ba1854e1.zip
searchpath is "private affair" of loadlib
-rw-r--r--lauxlib.c35
-rw-r--r--lauxlib.h5
-rw-r--r--loadlib.c62
3 files changed, 48 insertions, 54 deletions
diff --git a/lauxlib.c b/lauxlib.c
index ace7df42..8e954149 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.139 2005/07/11 16:41:51 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.140 2005/07/12 14:32:48 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*/
@@ -325,16 +325,6 @@ LUALIB_API int luaL_getn (lua_State *L, int t) {
325/* }====================================================== */ 325/* }====================================================== */
326 326
327 327
328static const char *pushnexttemplate (lua_State *L, const char *path) {
329 const char *l;
330 while (*path == *LUA_PATHSEP) path++; /* skip separators */
331 if (*path == '\0') return NULL; /* no more templates */
332 l = strchr(path, *LUA_PATHSEP); /* find next separator */
333 if (l == NULL) l = path+strlen(path);
334 lua_pushlstring(L, path, l - path); /* template */
335 return l;
336}
337
338 328
339LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, 329LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
340 const char *r) { 330 const char *r) {
@@ -353,29 +343,6 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
353} 343}
354 344
355 345
356static int readable (const char *fname) {
357 FILE *f = fopen(fname, "r"); /* try to open file */
358 if (f == NULL) return 0; /* open failed */
359 fclose(f);
360 return 1;
361}
362
363
364LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
365 const char *path) {
366 const char *p = path;
367 for (;;) {
368 const char *fname;
369 if ((p = pushnexttemplate(L, p)) == NULL)
370 return NULL;
371 fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
372 lua_remove(L, -2); /* remove path template */
373 if (readable(fname)) /* does file exist and is readable? */
374 return fname; /* return that file name */
375 lua_pop(L, 1); /* remove file name */
376 }
377}
378
379 346
380LUALIB_API const char *luaL_getfield (lua_State *L, int idx, 347LUALIB_API const char *luaL_getfield (lua_State *L, int idx,
381 const char *fname) { 348 const char *fname) {
diff --git a/lauxlib.h b/lauxlib.h
index e6a6dca3..00042a9f 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.78 2005/05/31 14:25:18 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.79 2005/05/31 14:34:02 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*/
@@ -62,9 +62,6 @@ LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
62LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 62LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
63 const char *const lst[]); 63 const char *const lst[]);
64 64
65LUALIB_API const char *(luaL_searchpath) (lua_State *L, const char *name,
66 const char *path);
67
68LUALIB_API int (luaL_ref) (lua_State *L, int t); 65LUALIB_API int (luaL_ref) (lua_State *L, int t);
69LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 66LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
70 67
diff --git a/loadlib.c b/loadlib.c
index ff1c1ffc..0001abb2 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.32 2005/07/11 16:41:57 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.33 2005/07/12 21:17:46 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**
@@ -329,15 +329,41 @@ static int ll_loadlib (lua_State *L) {
329*/ 329*/
330 330
331 331
332static int readable (const char *fname) {
333 FILE *f = fopen(fname, "r"); /* try to open file */
334 if (f == NULL) return 0; /* open failed */
335 fclose(f);
336 return 1;
337}
338
339
340static const char *pushnexttemplate (lua_State *L, const char *path) {
341 const char *l;
342 while (*path == *LUA_PATHSEP) path++; /* skip separators */
343 if (*path == '\0') return NULL; /* no more templates */
344 l = strchr(path, *LUA_PATHSEP); /* find next separator */
345 if (l == NULL) l = path + strlen(path);
346 lua_pushlstring(L, path, l - path); /* template */
347 return l;
348}
349
350
332static const char *findfile (lua_State *L, const char *pname) { 351static const char *findfile (lua_State *L, const char *pname) {
333 const char *name = luaL_checkstring(L, 1);
334 const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
335 const char *path; 352 const char *path;
353 const char *name = luaL_checkstring(L, 1);
354 name = luaL_gsub(L, name, ".", LUA_DIRSEP);
336 lua_getfield(L, LUA_ENVIRONINDEX, pname); 355 lua_getfield(L, LUA_ENVIRONINDEX, pname);
337 path = lua_tostring(L, -1); 356 path = lua_tostring(L, -1);
338 if (path == NULL) 357 if (path == NULL)
339 luaL_error(L, LUA_QL("package.%s") " must be a string", pname); 358 luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
340 return luaL_searchpath(L, fname, path); 359 while ((path = pushnexttemplate(L, path)) != NULL) {
360 const char *fname;
361 fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
362 if (readable(fname)) /* does file exist and is readable? */
363 return fname; /* return that file name */
364 lua_pop(L, 2); /* remove path template and file name */
365 }
366 return NULL; /* not found */
341} 367}
342 368
343 369
@@ -414,16 +440,21 @@ static int require_aux (lua_State *L, const char *name) {
414} 440}
415 441
416 442
417static void ll_error (lua_State *L, const char *name) { 443static void require_check (lua_State *L, const char *name) {
418 const char *msg; 444 if (!require_aux(L, name)) { /* error? */
419 lua_settop(L, 1); 445 /* build and show error message */
420 lua_getfield(L, LUA_ENVIRONINDEX, "path"); 446 const char *msg;
421 lua_getfield(L, LUA_ENVIRONINDEX, "cpath"); 447 lua_settop(L, 1);
422 msg = lua_pushfstring(L, "package " LUA_QS " not found in following paths:\n" 448 lua_getfield(L, LUA_ENVIRONINDEX, "path");
423 " Lua path: %s\n C path: %s\n", name, 449 lua_getfield(L, LUA_ENVIRONINDEX, "cpath");
424 lua_tostring(L, -2), lua_tostring(L, -1)); 450 msg = lua_pushfstring(L,
425 msg = luaL_gsub(L, msg, LUA_PATHSEP, "\n "); 451 "package " LUA_QS " not found in following paths:\n"
426 luaL_error(L, msg); 452 " Lua path: %s\n"
453 " C path: %s\n", name,
454 lua_tostring(L, -2), lua_tostring(L, -1));
455 msg = luaL_gsub(L, msg, LUA_PATHSEP, "\n ");
456 luaL_error(L, msg);
457 }
427} 458}
428 459
429 460
@@ -436,8 +467,7 @@ static int ll_require (lua_State *L) {
436 lua_pushlstring(L, name, pt - name); 467 lua_pushlstring(L, name, pt - name);
437 require_aux(L, lua_tostring(L, -1)); 468 require_aux(L, lua_tostring(L, -1));
438 } 469 }
439 if (!require_aux(L, name)) /* load module itself */ 470 require_check(L, name); /* load module itself */
440 ll_error(L, name);
441 return 1; 471 return 1;
442} 472}
443 473