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;
44 static char base_path[MAXPATHLEN];
45 static lua_script_t scripts;
49 /* Declare the Lua libraries we wish to use. */
50 /* Note: If you are opening and running a file containing Lua code */
51 /* using 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries */
52 /* used in that file here also. */
53 static const luaL_reg lualibs[] =
55 // { "base", luaopen_base },
59 /* A function to open up all the Lua libraries you declared above. */
60 static void openlualibs(lua_State *l)
63 for( lib = lualibs; lib->func != NULL; lib++) {
73 static int lua_config_base_path (const oconfig_item_t *ci)
75 if( (ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING) ){
76 ERROR("lua plugin: The '%s' config option requires a single string argument", ci->key);
80 strncpy(base_path, ci->values[0].value.string, sizeof(base_path));
82 /* add ending slash if not provided */
83 if( base_path[strlen(base_path) - 1] != '/' ){
84 base_path[strlen(base_path)] = '/';
87 INFO("lua plugin: BasePath = '%s'", base_path);
91 static int lua_config_script (const oconfig_item_t *ci)
93 lua_script_t *script = &scripts;
95 if( (ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING) ){
96 ERROR("lua plugin: The '%s' config option requires a single string argument", ci->key);
100 /* find a free slot for the structure */
101 while( script->next != NULL ){
102 script = script->next;
105 /* build full path : base_path + given path + \0 */
106 script->script_path = malloc(
108 strlen(ci->values[0].value.string) + 1
111 strncpy(script->script_path, base_path, sizeof(script->script_path));
112 strncpy(script->script_path + strlen(base_path), ci->values[0].value.string, sizeof(script->script_path));
114 /* check if the file exists and we can read it */
115 if( access(script->script_path, R_OK) == -1 ) {
116 ERROR("lua plugin: Cannot read file '%s' : %s", script->script_path, strerror(errno));
117 free(script->script_path);
121 /* initialize the lua context */
122 script->lua_state = lua_open();
124 openlualibs(script->lua_state);
126 if( register_exported_functions(script->lua_state) != 0 ) {
127 ERROR("lua plugin: Cannot register exported functions, aborting");
128 free(script->script_path);
132 /* and try to load the file */
133 if( luaL_dofile(script->lua_state, "script.lua") != 0 ) {
134 ERROR("lua plugin: error while loading '%s' => %s\n", script->script_path, lua_tostring(script->lua_state, -1));
135 free(script->script_path);
139 INFO("lua plugin: file '%s' loaded succesfully", script->script_path);
146 // Script script1.lua
147 // Script script2.lua
149 static int lua_config(oconfig_item_t *ci)
153 for (i = 0; i < ci->children_num; i++)
155 oconfig_item_t *child = ci->children + i;
157 if (strcasecmp ("BasePath", child->key) == 0) {
158 lua_config_base_path(child);
160 else if (strcasecmp ("Script", child->key) == 0){
161 lua_config_script(child);
165 WARNING ("network plugin: Option `%s' is not allowed here.",
173 static int lua_init()
175 INFO("Lua plugin loaded.");
180 void module_register()
182 plugin_register_complex_config("lua", lua_config);
183 plugin_register_init("lua", lua_init);