CHDK_DE Vorschauversion  Trunk Rev. 6014
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
loadlib.c-Dateireferenz
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
+ Include-Abhängigkeitsdiagramm für loadlib.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define loadlib_c
 
#define LUA_LIB
 
#define LUA_POF   "luaopen_"
 
#define LUA_OFSEP   "_"
 
#define LIBPREFIX   "LOADLIB: "
 
#define POF   LUA_POF
 
#define LIB_FAIL   "open"
 
#define ERRLIB   1
 
#define ERRFUNC   2
 
#define setprogdir(L)   ((void)0)
 
#define sentinel   ((void *)&sentinel_)
 
#define AUXMARK   "\1"
 

Funktionen

static int readable (const char *filename)
 
static const char * pushnexttemplate (lua_State *L, const char *path)
 
static const char * findfile (lua_State *L, const char *name, const char *pname)
 
static void loaderror (lua_State *L, const char *filename)
 
static int loader_Lua (lua_State *L)
 
static int loader_preload (lua_State *L)
 
static int ll_require (lua_State *L)
 
static void setpath (lua_State *L, const char *fieldname, __attribute__((unused)) const char *envname, const char *def)
 
LUALIB_API int luaopen_package (lua_State *L)
 

Variablen

static const int sentinel_ = 0
 
static const luaL_Reg pk_funcs []
 
static const luaL_Reg ll_funcs []
 
static const lua_CFunction loaders []
 

Makro-Dokumentation

#define AUXMARK   "\1"

Definiert in Zeile 596 der Datei loadlib.c.

#define ERRFUNC   2

Definiert in Zeile 40 der Datei loadlib.c.

#define ERRLIB   1

Definiert in Zeile 39 der Datei loadlib.c.

#define LIB_FAIL   "open"

Definiert in Zeile 35 der Datei loadlib.c.

#define LIBPREFIX   "LOADLIB: "

Definiert in Zeile 32 der Datei loadlib.c.

#define loadlib_c

Definiert in Zeile 16 der Datei loadlib.c.

#define LUA_LIB

Definiert in Zeile 17 der Datei loadlib.c.

#define LUA_OFSEP   "_"

Definiert in Zeile 29 der Datei loadlib.c.

#define LUA_POF   "luaopen_"

Definiert in Zeile 26 der Datei loadlib.c.

#define POF   LUA_POF

Definiert in Zeile 34 der Datei loadlib.c.

#define sentinel   ((void *)&sentinel_)

Definiert in Zeile 455 der Datei loadlib.c.

#define setprogdir (   L)    ((void)0)

Definiert in Zeile 42 der Datei loadlib.c.

Dokumentation der Funktionen

static const char* findfile ( lua_State L,
const char *  name,
const char *  pname 
)
static

Definiert in Zeile 359 der Datei loadlib.c.

360  {
361  const char *path;
362  name = luaL_gsub(L, name, ".", LUA_DIRSEP);
363  lua_getfield(L, LUA_ENVIRONINDEX, pname);
364  path = lua_tostring(L, -1);
365  if (path == NULL)
366  luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
367  lua_pushliteral(L, ""); /* error accumulator */
368  while ((path = pushnexttemplate(L, path)) != NULL) {
369  const char *filename;
370  filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
371  lua_remove(L, -2); /* remove path template */
372  if (readable(filename)) /* does file exist and is readable? */
373  return filename; /* return that file name */
374  lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
375  lua_remove(L, -2); /* remove file name */
376  lua_concat(L, 2); /* add entry to possible error message */
377  }
378  return NULL; /* not found */
379 }
static int ll_require ( lua_State L)
static

Definiert in Zeile 458 der Datei loadlib.c.

458  {
459  const char *name = luaL_checkstring(L, 1);
460  int i;
461  lua_settop(L, 1); /* _LOADED table will be at index 2 */
462  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
463  lua_getfield(L, 2, name);
464  if (lua_toboolean(L, -1)) { /* is it there? */
465  if (lua_touserdata(L, -1) == sentinel) /* check loops */
466  luaL_error(L, "loop or previous error loading module " LUA_QS, name);
467  return 1; /* package is already loaded */
468  }
469  /* else must load it; iterate over available loaders */
470  lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
471  if (!lua_istable(L, -1))
472  luaL_error(L, LUA_QL("package.loaders") " must be a table");
473  lua_pushliteral(L, ""); /* error message accumulator */
474  for (i=1; ; i++) {
475  lua_rawgeti(L, -2, i); /* get a loader */
476  if (lua_isnil(L, -1))
477  luaL_error(L, "module " LUA_QS " not found:%s",
478  name, lua_tostring(L, -2));
479  lua_pushstring(L, name);
480  lua_call(L, 1, 1); /* call it */
481  if (lua_isfunction(L, -1)) /* did it find module? */
482  break; /* module loaded successfully */
483  else if (lua_isstring(L, -1)) /* loader returned error message? */
484  lua_concat(L, 2); /* accumulate it */
485  else
486  lua_pop(L, 1);
487  }
489  lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
490  lua_pushstring(L, name); /* pass name as argument to module */
491  lua_call(L, 1, 1); /* run loaded module */
492  if (!lua_isnil(L, -1)) /* non-nil return? */
493  lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
494  lua_getfield(L, 2, name);
495  if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
496  lua_pushboolean(L, 1); /* use true as result */
497  lua_pushvalue(L, -1); /* extra copy to be returned */
498  lua_setfield(L, 2, name); /* _LOADED[name] = true */
499  }
500  return 1;
501 }
static int loader_Lua ( lua_State L)
static

Definiert in Zeile 387 der Datei loadlib.c.

387  {
388  const char *filename;
389  const char *name = luaL_checkstring(L, 1);
390  filename = findfile(L, name, "path");
391  if (filename == NULL) return 1; /* library not found in this path */
392  if (luaL_loadfile(L, filename) != 0)
393  loaderror(L, filename);
394  return 1; /* library loaded successfully */
395 }
static int loader_preload ( lua_State L)
static

Definiert in Zeile 442 der Datei loadlib.c.

442  {
443  const char *name = luaL_checkstring(L, 1);
444  lua_getfield(L, LUA_ENVIRONINDEX, "preload");
445  if (!lua_istable(L, -1))
446  luaL_error(L, LUA_QL("package.preload") " must be a table");
447  lua_getfield(L, -1, name);
448  if (lua_isnil(L, -1)) /* not found? */
449  lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
450  return 1;
451 }
static void loaderror ( lua_State L,
const char *  filename 
)
static

Definiert in Zeile 381 der Datei loadlib.c.

381  {
382  luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
383  lua_tostring(L, 1), filename, lua_tostring(L, -1));
384 }
LUALIB_API int luaopen_package ( lua_State L)

Definiert in Zeile 637 der Datei loadlib.c.

637  {
638  int i;
639 #if 0
640  /* create new type _LOADLIB */
641  luaL_newmetatable(L, "_LOADLIB");
642  lua_pushcfunction(L, gctm);
643  lua_setfield(L, -2, "__gc");
644 #endif
645  /* create `package' table */
647 #if 0
648 #if defined(LUA_COMPAT_LOADLIB)
649  lua_getfield(L, -1, "loadlib");
650  lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
651 #endif
652 #endif
653  lua_pushvalue(L, -1);
655  /* create `loaders' table */
656  lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
657  /* fill it with pre-defined loaders */
658  for (i=0; loaders[i] != NULL; i++) {
659  lua_pushcfunction(L, loaders[i]);
660  lua_rawseti(L, -2, i+1);
661  }
662  lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
663  setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
664  setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
665  /* store config information */
667  LUA_EXECDIR "\n" LUA_IGMARK);
668  lua_setfield(L, -2, "config");
669  /* set field `loaded' */
670  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
671  lua_setfield(L, -2, "loaded");
672  /* set field `preload' */
673  lua_newtable(L);
674  lua_setfield(L, -2, "preload");
676  luaL_register(L, NULL, ll_funcs); /* open lib into global table */
677  lua_pop(L, 1);
678  return 1; /* return 'package' table */
679 }
static const char* pushnexttemplate ( lua_State L,
const char *  path 
)
static

Definiert in Zeile 348 der Datei loadlib.c.

348  {
349  const char *l;
350  while (*path == *LUA_PATHSEP) path++; /* skip separators */
351  if (*path == '\0') return NULL; /* no more templates */
352  l = strchr(path, *LUA_PATHSEP); /* find next separator */
353  if (l == NULL) l = path + strlen(path);
354  lua_pushlstring(L, path, l - path); /* template */
355  return l;
356 }
static int readable ( const char *  filename)
static

Definiert in Zeile 335 der Datei loadlib.c.

335  {
336 #ifdef HOST_LUA
337  FILE *f = fopen(filename, "r"); /* try to open file */
338  if (f == NULL) return 0; /* open failed */
339  fclose(f);
340 #else
341  int f = open(filename,O_RDONLY,0777);
342  if (f == -1) return 0; /* open failed */
343  close(f);
344 #endif
345  return 1;
346 }
static void setpath ( lua_State L,
const char *  fieldname,
__attribute__((unused)) const char *  envname,
const char *  def 
)
static

Definiert in Zeile 598 der Datei loadlib.c.

599  {
600 /* CHDK - no environment vars */
601 #if defined(HDK_VERSION)
602  lua_pushstring(L, def); /* use default */
603 #else
604  const char *path = getenv(envname);
605  if (path == NULL) /* no environment variable? */
606  lua_pushstring(L, def); /* use default */
607  else {
608  /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
609  path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
610  LUA_PATHSEP AUXMARK LUA_PATHSEP);
611  luaL_gsub(L, path, AUXMARK, def);
612  lua_remove(L, -2);
613  }
614 #endif
615  setprogdir(L);
616  lua_setfield(L, -2, fieldname);
617 }

Variablen-Dokumentation

const luaL_Reg ll_funcs[]
static
Initialisierung:
= {
{"require", ll_require},
}

Definiert in Zeile 626 der Datei loadlib.c.

const lua_CFunction loaders[]
static
Initialisierung:

Definiert in Zeile 633 der Datei loadlib.c.

const luaL_Reg pk_funcs[]
static
Initialisierung:
= {
}

Definiert in Zeile 619 der Datei loadlib.c.

const int sentinel_ = 0
static

Definiert in Zeile 454 der Datei loadlib.c.