3 * Copyright (C) 2010 Julien Ammous
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; only version 2.1 of the License is
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "configfile.h"
27 #include "utils_cache.h"
29 /* Include the Lua API header files. */
34 #include "lua_exports.c"
36 typedef struct lua_script_s {
40 struct lua_script_s *next;
43 static char base_path[PATH_MAX + 1] = "";
44 static lua_script_t *scripts = NULL;
46 /* Declare the Lua libraries we wish to use.
47 * Note: If you are opening and running a file containing Lua code using
48 * 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries used in
49 * that file here also. */
50 static const luaL_reg lua_load_libs[] =
53 { LUA_DBLIBNAME, luaopen_debug },
55 { LUA_TABLIBNAME, luaopen_table },
56 { LUA_IOLIBNAME, luaopen_io },
57 { LUA_STRLIBNAME, luaopen_string },
58 { LUA_MATHLIBNAME, luaopen_math }
61 static void lua_script_free (lua_script_t *script) /* {{{ */
70 if (script->lua_state != NULL)
72 lua_close (script->lua_state);
73 script->lua_state = NULL;
76 sfree (script->script_path);
79 lua_script_free (next);
80 } /* }}} void lua_script_free */
82 static int lua_script_init (lua_script_t *script) /* {{{ */
86 memset (script, 0, sizeof (*script));
87 script->script_path = NULL;
90 /* initialize the lua context */
91 script->lua_state = lua_open();
92 if (script->lua_state == NULL)
94 ERROR ("lua plugin: lua_open failed.");
98 /* Open up all the Lua libraries declared above. */
99 for (i = 0; i < STATIC_ARRAY_SIZE (lua_load_libs); i++)
103 status = (*lua_load_libs[i].func) (script->lua_state);
105 WARNING ("lua plugin: Loading library \"%s\" failed.",
106 lua_load_libs[i].name);
109 /* Register all the functions we implement in C */
110 register_exported_functions (script->lua_state);
113 } /* }}} int lua_script_init */
115 static int lua_script_load (const char *script_path) /* {{{ */
117 lua_script_t *script;
120 script = malloc (sizeof (*script));
123 ERROR ("lua plugin: malloc failed.");
127 status = lua_script_init (script);
130 lua_script_free (script);
134 script->script_path = strdup (script_path);
135 if (script->script_path == NULL)
137 ERROR ("lua plugin: strdup failed.");
138 lua_script_free (script);
142 status = luaL_loadfile (script->lua_state, script->script_path);
149 case LUA_ERRSYNTAX: errmsg = "Syntax error"; break;
150 case LUA_ERRFILE: errmsg = "File I/O error"; break;
151 case LUA_ERRMEM: errmsg = "Memory allocation error"; break;
152 default: errmsg = "Unexpected error";
155 ERROR ("lua plugin: Loading script \"%s\" failed: %s",
156 script->script_path, errmsg);
158 lua_script_free (script);
162 /* Append this script to the global list of scripts. */
172 while (last->next != NULL)
179 } /* }}} int lua_script_load */
181 static int lua_config_base_path (const oconfig_item_t *ci) /* {{{ */
186 status = cf_util_get_string_buffer (ci, base_path, sizeof (base_path));
190 len = strlen (base_path);
191 while ((len > 0) && (base_path[len - 1] == '/'))
197 DEBUG ("lua plugin: base_path = \"%s\";", base_path);
200 } /* }}} int lua_config_base_path */
202 static int lua_config_script (const oconfig_item_t *ci) /* {{{ */
204 char rel_path[PATH_MAX + 1];
205 char abs_path[PATH_MAX + 1];
208 status = cf_util_get_string_buffer (ci, rel_path, sizeof (rel_path));
212 if (base_path[0] == 0)
213 sstrncpy (abs_path, rel_path, sizeof (abs_path));
215 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", base_path, rel_path);
217 DEBUG ("lua plugin: abs_path = \"%s\";", abs_path);
219 status = lua_script_load (abs_path);
223 INFO("lua plugin: File \"%s\" loaded succesfully", abs_path);
226 } /* }}} int lua_config_script */
231 * Script "script1.lua"
232 * Script "script2.lua"
235 static int lua_config (oconfig_item_t *ci) /* {{{ */
239 for (i = 0; i < ci->children_num; i++)
241 oconfig_item_t *child = ci->children + i;
243 if (strcasecmp ("BasePath", child->key) == 0) {
244 lua_config_base_path(child);
246 else if (strcasecmp ("Script", child->key) == 0){
247 lua_config_script(child);
251 WARNING ("network plugin: Option `%s' is not allowed here.",
257 } /* }}} int lua_config */
259 static int lua_shutdown (void) /* {{{ */
261 lua_script_free (scripts);
265 } /* }}} int lua_shutdown */
267 void module_register()
269 plugin_register_complex_config("lua", lua_config);
270 plugin_register_shutdown("lua", lua_shutdown);
273 /* vim: set sw=2 sts=2 et fdm=marker : */