Просмотр исходного кода

Make Civetweb Lua state available from the command line, e.g. for easier script development

bel 10 лет назад
Родитель
Сommit
d25755d4d0
2 измененных файлов с 60 добавлено и 8 удалено
  1. 50 4
      src/main.c
  2. 10 4
      src/mod_lua.inl

+ 50 - 4
src/main.c

@@ -514,7 +514,8 @@ static void verify_existence(char **options, const char *option_name,
 static void set_absolute_path(char *options[], const char *option_name,
 static void set_absolute_path(char *options[], const char *option_name,
                               const char *path_to_civetweb_exe)
                               const char *path_to_civetweb_exe)
 {
 {
-    char path[PATH_MAX] = "", abs[PATH_MAX] = "", *option_value;
+    char path[PATH_MAX] = "", abs[PATH_MAX] = "";
+    const char *option_value;
     const char *p;
     const char *p;
 
 
     /* Check whether option is already set */
     /* Check whether option is already set */
@@ -543,13 +544,23 @@ static void set_absolute_path(char *options[], const char *option_name,
     }
     }
 }
 }
 
 
+
+#ifdef USE_LUA
+#define main luatest_main
+#define luaL_openlibs lua_civet_openlibs
+extern void lua_civet_openlibs(struct lua_State *L);
+#include "../src/third_party/lua-5.2.3/src/lua.c"
+#undef main
+#endif
+
+
 static void start_civetweb(int argc, char *argv[])
 static void start_civetweb(int argc, char *argv[])
 {
 {
     struct mg_callbacks callbacks;
     struct mg_callbacks callbacks;
     char *options[2*MAX_OPTIONS+1];
     char *options[2*MAX_OPTIONS+1];
     int i;
     int i;
 
 
-    /* Edit passwords file if -A option is specified */
+    /* Edit passwords file, if -A option is specified */
     if (argc > 1 && !strcmp(argv[1], "-A")) {
     if (argc > 1 && !strcmp(argv[1], "-A")) {
         if (argc != 6) {
         if (argc != 6) {
             show_usage_and_exit();
             show_usage_and_exit();
@@ -558,6 +569,17 @@ static void start_civetweb(int argc, char *argv[])
              EXIT_SUCCESS : EXIT_FAILURE);
              EXIT_SUCCESS : EXIT_FAILURE);
     }
     }
 
 
+    /* Call Lua with additional Civetweb specific Lua functions, if -L option is specified */
+    if (argc > 1 && !strcmp(argv[1], "-L")) {
+#ifdef WIN32
+        MakeConsole();
+#endif
+#ifdef USE_LUA
+        exit(luatest_main(argc-1, &argv[1]));
+#endif
+        exit(EXIT_FAILURE);
+    }
+
     /* Show usage if -h or --help options are specified */
     /* Show usage if -h or --help options are specified */
     if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
     if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
         show_usage_and_exit();
         show_usage_and_exit();
@@ -1458,6 +1480,9 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
     return DefWindowProc(hWnd, msg, wParam, lParam);
     return DefWindowProc(hWnd, msg, wParam, lParam);
 }
 }
 
 
+#include <fcntl.h>
+#include <io.h>
+
 static int MakeConsole() {
 static int MakeConsole() {
     DWORD err;
     DWORD err;
     int ok = (GetConsoleWindow() != NULL);
     int ok = (GetConsoleWindow() != NULL);
@@ -1472,9 +1497,22 @@ static int MakeConsole() {
             }
             }
             AttachConsole(GetCurrentProcessId());
             AttachConsole(GetCurrentProcessId());
         }
         }
-        freopen("CON", "a", stdout);
-        freopen("CON", "a", stderr);
+
         ok = (GetConsoleWindow() != NULL);
         ok = (GetConsoleWindow() != NULL);
+        if (ok) {
+            freopen("CON", "a", stdin);
+            freopen("CON", "a", stdout);
+            freopen("CON", "a", stderr);
+        }
+    }
+    if (ok) {
+        CONSOLE_SCREEN_BUFFER_INFO coninfo;
+
+        SetConsoleTitle(server_name);
+
+        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
+        if (coninfo.dwSize.Y<500) coninfo.dwSize.Y = 500;
+        SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
     }
     }
     return ok;
     return ok;
 }
 }
@@ -1519,6 +1557,14 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show)
     /* Return the WM_QUIT value. */
     /* Return the WM_QUIT value. */
     return (int) msg.wParam;
     return (int) msg.wParam;
 }
 }
+
+#if defined(CONSOLE)
+void main(void)
+{
+    WinMain(0, 0, 0, 0);
+}
+#endif
+
 #elif defined(USE_COCOA)
 #elif defined(USE_COCOA)
 #import <Cocoa/Cocoa.h>
 #import <Cocoa/Cocoa.h>
 
 

+ 10 - 4
src/mod_lua.inl

@@ -948,11 +948,11 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
 
 
 static void prepare_lua_environment(struct mg_context * ctx, struct mg_connection *conn, struct lua_websock_data *conn_list, lua_State *L, const char *script_name, int lua_env_type)
 static void prepare_lua_environment(struct mg_context * ctx, struct mg_connection *conn, struct lua_websock_data *conn_list, lua_State *L, const char *script_name, int lua_env_type)
 {
 {
-    const char * preload_file = ((conn != NULL) ? conn->ctx->config[LUA_PRELOAD_FILE] : NULL);
-
     extern void luaL_openlibs(lua_State *);
     extern void luaL_openlibs(lua_State *);
     luaL_openlibs(L);
     luaL_openlibs(L);
 
 
+    assert(ctx);
+
 #ifdef USE_LUA_SQLITE3
 #ifdef USE_LUA_SQLITE3
     {
     {
         extern int luaopen_lsqlite3(lua_State *);
         extern int luaopen_lsqlite3(lua_State *);
@@ -1061,8 +1061,8 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
         "debug.traceback(e, 1)) end"));
         "debug.traceback(e, 1)) end"));
 
 
     /* Preload */
     /* Preload */
-    if ((preload_file != NULL) && (*preload_file != 0)) {
-        IGNORE_UNUSED_RESULT(luaL_dofile(L, preload_file));
+    if (ctx->config[LUA_PRELOAD_FILE] != NULL) {
+        IGNORE_UNUSED_RESULT(luaL_dofile(L, ctx->config[LUA_PRELOAD_FILE]));
     }
     }
 
 
     if (ctx->callbacks.init_lua != NULL) {
     if (ctx->callbacks.init_lua != NULL) {
@@ -1070,6 +1070,12 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
     }
     }
 }
 }
 
 
+void lua_civet_openlibs(lua_State *L)
+{
+    static struct mg_context fake_ctx;
+    prepare_lua_environment(&fake_ctx, NULL, NULL, L, NULL, 0);
+}
+
 static int lua_error_handler(lua_State *L)
 static int lua_error_handler(lua_State *L)
 {
 {
     const char *error_msg =  lua_isstring(L, -1) ?  lua_tostring(L, -1) : "?\n";
     const char *error_msg =  lua_isstring(L, -1) ?  lua_tostring(L, -1) : "?\n";