Browse Source

Move run_lua to mod_lua

bel 8 years ago
parent
commit
079d2d0ae1
3 changed files with 111 additions and 72 deletions
  1. 2 1
      src/civetweb_private_lua.h
  2. 24 71
      src/main.c
  3. 85 0
      src/mod_lua.inl

+ 2 - 1
src/civetweb_private_lua.h

@@ -5,6 +5,7 @@
 #ifndef CIVETWEB_PRIVATE_LUA_H
 #define CIVETWEB_PRIVATE_LUA_H
 
-void civetweb_open_lua_libs(lua_State *L);
+int run_lua(const char *file_name);
+
 
 #endif

+ 24 - 71
src/main.c

@@ -150,7 +150,7 @@ static char g_server_base_name[40]; /* Set by init_server_name() */
 static const char *g_server_name;   /* Set by init_server_name() */
 static const char *g_icon_name;     /* Set by init_server_name() */
 #ifdef USE_LUA
-static const char *g_lua_script;    /* Set by init_server_name() */
+static const char *g_lua_script; /* Set by init_server_name() */
 #endif
 static char g_config_file_name[PATH_MAX] =
     "";                          /* Set by process_command_line_arguments() */
@@ -171,12 +171,14 @@ static struct tuser_data
 #define CONFIG_FILE2 "/usr/local/etc/civetweb.conf"
 #endif
 
-enum { OPTION_TITLE, 
-       OPTION_ICON, 
+enum {
+	OPTION_TITLE,
+	OPTION_ICON,
 #ifdef USE_LUA
-       OPTION_LUA_SCRIPT, 
+	OPTION_LUA_SCRIPT,
 #endif
-       NUM_MAIN_OPTIONS };
+	NUM_MAIN_OPTIONS
+};
 
 static struct mg_option main_config_options[] = {
     {"title", CONFIG_TYPE_STRING, NULL},
@@ -666,7 +668,6 @@ init_server_name(int argc, const char *argv[])
 		}
 	}
 #endif
-
 }
 
 
@@ -779,63 +780,12 @@ set_absolute_path(char *options[],
 
 #ifdef USE_LUA
 
-/* TODO: Move to civetweb.c, use config lua_background_script, start in mg_start, allow access to server state, set mg.sleep or use timer */
+/* TODO: Move to civetweb.c (done), use config lua_background_script (open:
+ * move from here), start in mg_start, allow access to server state, set
+ * mg.sleep or use timer */
 
-#include "civetweb_lua.h"
 #include "civetweb_private_lua.h"
 
-static int
-run_lua(const char *file_name)
-{
-	struct lua_State *L;
-	int lua_ret;
-	int func_ret = EXIT_FAILURE;
-	const char *lua_err_txt;
-
-#ifdef WIN32
-	(void)MakeConsole();
-#endif
-
-	L = luaL_newstate();
-	if (L == NULL) {
-		fprintf(stderr, "Error: Cannot create Lua state\n");
-		return EXIT_FAILURE;
-	}
-	civetweb_open_lua_libs(L);
-
-	lua_ret = luaL_loadfile(L, file_name);
-	if (lua_ret != LUA_OK) {
-		/* Error when loading the file (e.g. file not found, out of memory, ...)
-		 */
-		lua_err_txt = lua_tostring(L, -1);
-		fprintf(stderr, "Error loading file %s: %s\n", file_name, lua_err_txt);
-	} else {
-		/* The script file is loaded, now call it */
-		lua_ret = lua_pcall(L,
-		                    /* no arguments */ 0,
-		                    /* zero or one return value */ 1,
-		                    /* errors as strint return value */ 0);
-		if (lua_ret != LUA_OK) {
-			/* Error when executing the script */
-			lua_err_txt = lua_tostring(L, -1);
-			fprintf(stderr,
-			        "Error running file %s: %s\n",
-			        file_name,
-			        lua_err_txt);
-		} else {
-			/* Script executed */
-			if (lua_type(L, -1) == LUA_TNUMBER) {
-				func_ret = (int)lua_tonumber(L, -1);
-			} else {
-				func_ret = EXIT_SUCCESS;
-			}
-		}
-	}
-	lua_close(L);
-
-	return func_ret;
-}
-
 static void *
 run_lua_thread(void *file_name)
 {
@@ -935,6 +885,9 @@ start_civetweb(int argc, char *argv[])
 		if (argc != 3) {
 			show_usage_and_exit(argv[0]);
 		}
+#ifdef WIN32
+		(void)MakeConsole();
+#endif
 		exit(run_lua(argv[2]));
 #else
 		show_server_name();
@@ -991,17 +944,17 @@ start_civetweb(int argc, char *argv[])
 #ifdef USE_LUA
 	verify_existence(options, "lua_preload_file", 0);
 
-    if (g_lua_script) {
-        struct stat st;
-        if ((stat(g_lua_script, &st) != 0) || (S_ISDIR(st.st_mode))) {
-            fprintf(stderr, "\nError: lua_script not found\n");
-            exit(EXIT_FAILURE);
-        }
-	    if (0!=mg_start_thread(run_lua_thread, (void *)g_lua_script)) {
-            fprintf(stderr, "\nError: Cannot create thread for lua_script\n");
-            exit(EXIT_FAILURE);
-        }
-    }
+	if (g_lua_script) {
+		struct stat st;
+		if ((stat(g_lua_script, &st) != 0) || (S_ISDIR(st.st_mode))) {
+			fprintf(stderr, "\nError: lua_script not found\n");
+			exit(EXIT_FAILURE);
+		}
+		if (0 != mg_start_thread(run_lua_thread, (void *)g_lua_script)) {
+			fprintf(stderr, "\nError: Cannot create thread for lua_script\n");
+			exit(EXIT_FAILURE);
+		}
+	}
 #endif
 
 	/* Setup signal handler: quit on Ctrl-C */

+ 85 - 0
src/mod_lua.inl

@@ -1887,6 +1887,90 @@ lua_websocket_close(struct mg_connection *conn, void *ws_arg)
 }
 #endif
 
+
+lua_State *
+mg_prepare_lua_context_script(const char *file_name,
+                              struct mg_context *ctx,
+                              char *ebuf,
+                              size_t ebuf_len)
+{
+	struct lua_State *L;
+	int lua_ret;
+	const char *lua_err_txt;
+
+	L = luaL_newstate();
+	if (L == NULL) {
+		mg_snprintf(NULL,
+		            NULL, /* No truncation check for ebuf */
+		            ebuf,
+		            ebuf_len,
+		            "Error: %s",
+		            "Cannot create Lua state");
+		return 0;
+	}
+	civetweb_open_lua_libs(L);
+
+	lua_ret = luaL_loadfile(L, file_name);
+	if (lua_ret != LUA_OK) {
+		/* Error when loading the file (e.g. file not found, out of memory, ...)
+		 */
+		lua_err_txt = lua_tostring(L, -1);
+		mg_snprintf(NULL,
+		            NULL, /* No truncation check for ebuf */
+		            ebuf,
+		            ebuf_len,
+		            "Error loading file %s: %s\n",
+		            file_name,
+		            lua_err_txt);
+		return 0;
+	}
+
+	/* The script file is loaded, now call it */
+	lua_ret = lua_pcall(L,
+	                    /* no arguments */ 0,
+	                    /* zero or one return value */ 1,
+	                    /* errors as strint return value */ 0);
+
+	if (lua_ret != LUA_OK) {
+		/* Error when executing the script */
+		lua_err_txt = lua_tostring(L, -1);
+		mg_snprintf(NULL,
+		            NULL, /* No truncation check for ebuf */
+		            ebuf,
+		            ebuf_len,
+		            "Error running file %s: %s\n",
+		            file_name,
+		            lua_err_txt);
+		return 0;
+	}
+	/*	lua_close(L); must be done somewhere else */
+
+	return L;
+}
+
+
+int
+run_lua(const char *file_name)
+{
+	int func_ret = EXIT_FAILURE;
+	char ebuf[512] = {0};
+	lua_State *L =
+	    mg_prepare_lua_context_script(file_name, NULL, ebuf, sizeof(ebuf));
+	if (L) {
+		/* Script executed */
+		if (lua_type(L, -1) == LUA_TNUMBER) {
+			func_ret = (int)lua_tonumber(L, -1);
+		} else {
+			func_ret = EXIT_SUCCESS;
+		}
+		lua_close(L);
+	} else {
+		fprintf(stderr, "%s\n", ebuf);
+	}
+	return func_ret;
+}
+
+
 static void *lib_handle_uuid = NULL;
 
 static void
@@ -1900,6 +1984,7 @@ lua_init_optional_libraries(void)
 #endif
 }
 
+
 static void
 lua_exit_optional_libraries(void)
 {