config data available to lua script

This commit is contained in:
whottgen
2004-09-21 13:30:47 +00:00
parent 1f5605b795
commit da7bdfa3f9
2 changed files with 24 additions and 4 deletions

View File

@ -62,9 +62,10 @@ typedef struct lua_worker_handle_s lua_worker_handle_t;
int lua_init(cfgl_t *cfg, void **handle) { int lua_init(cfgl_t *cfg, void **handle) {
lua_container_handle_t *lch; lua_container_handle_t *lch;
char *lualibname, *lualibnames; char *lualibname, *lualibnames;
int res; int res, cnt;
char *lua_filename; char *lua_filename;
const luaL_reg *lualib; const luaL_reg *lualib;
cfgl_t *cfgl_iter;
lch = (lua_container_handle_t*) malloc(sizeof(lua_container_handle_t)); lch = (lua_container_handle_t*) malloc(sizeof(lua_container_handle_t));
lch->cfg = cfg; lch->cfg = cfg;
@ -87,7 +88,8 @@ int lua_init(cfgl_t *cfg, void **handle) {
for (lualib = lualibs; lualib->func != NULL; lualib++) { for (lualib = lualibs; lualib->func != NULL; lualib++) {
if (0 == strcmp(lualib->name, lualibname)) { if (0 == strcmp(lualib->name, lualibname)) {
lualib->func(lch->l); res = lualib->func(lch->l);
/* syslog(LOG_DEBUG, "lua_init %s loaded, result %d", lualibname, res); */
lua_settop(lch->l, 0); lua_settop(lch->l, 0);
break; break;
} }
@ -103,15 +105,32 @@ int lua_init(cfgl_t *cfg, void **handle) {
} }
syslog(LOG_DEBUG, "lua_init: lua file %s loaded and compiled", lua_filename); syslog(LOG_DEBUG, "lua_init: lua file %s loaded and compiled", lua_filename);
lua_newtable(lch->l);
cnt = 0;
for (cfgl_iter = lch->cfg; cfgl_iter != NULL; cfgl_iter = cfgl_iter->next) {
lua_pushstring(lch->l, cfgl_iter->name);
lua_pushstring(lch->l, cfgl_iter->value);
lua_rawset(lch->l, -3);
cnt++;
}
lua_pushliteral(lch->l, "n"); /* Pushes the literal */
lua_pushnumber(lch->l, cnt); /* Pushes the total number of cells */
lua_rawset(lch->l, -3); /* Stores the pair in the table */
lua_setglobal(lch->l, "config");
res = lua_pcall(lch->l, 0, 0, 0); res = lua_pcall(lch->l, 0, 0, 0);
if (0 != res) { if (0 != res) {
syslog(LOG_ERR, "lua_init: unable to execute lua file %s", lua_filename); syslog(LOG_ERR, "lua_init: unable to execute lua file %s", lua_filename);
lua_close(lch->l); lua_close(lch->l);
free(lch); free(lch);
return -1;
} }
syslog(LOG_DEBUG, "lua_init: lua file %s executed", lua_filename); syslog(LOG_DEBUG, "lua_init: lua file %s executed", lua_filename);
*handle = lch; *handle = lch;
return 0; return 0;
} }

View File

@ -1,5 +1,6 @@
function f (i) function f (i)
io.write("[lua] output from lua\n") io.write("[lua] output from lua\n")
io.write("[lua] ", i, "\n") io.write("[lua] ", i, "\n")
io.write("[lua] entrypoint: ", config["entrypoint"], "\n")
return 1, string.upper(i) return 1, string.upper(i)
end end