diff --git a/src/main.c b/src/main.c index 20be9002..cb08325b 100644 --- a/src/main.c +++ b/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, 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; /* 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[]) { struct mg_callbacks callbacks; char *options[2*MAX_OPTIONS+1]; 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 != 6) { show_usage_and_exit(); @@ -558,6 +569,17 @@ static void start_civetweb(int argc, char *argv[]) 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 */ if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) { show_usage_and_exit(); @@ -1458,6 +1480,9 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, return DefWindowProc(hWnd, msg, wParam, lParam); } +#include +#include + static int MakeConsole() { DWORD err; int ok = (GetConsoleWindow() != NULL); @@ -1472,9 +1497,22 @@ static int MakeConsole() { } AttachConsole(GetCurrentProcessId()); } - freopen("CON", "a", stdout); - freopen("CON", "a", stderr); + 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; } @@ -1519,6 +1557,14 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) /* Return the WM_QUIT value. */ return (int) msg.wParam; } + +#if defined(CONSOLE) +void main(void) +{ + WinMain(0, 0, 0, 0); +} +#endif + #elif defined(USE_COCOA) #import diff --git a/src/mod_lua.inl b/src/mod_lua.inl index 2559d364..bc95a17d 100644 --- a/src/mod_lua.inl +++ b/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) { - const char * preload_file = ((conn != NULL) ? conn->ctx->config[LUA_PRELOAD_FILE] : NULL); - extern void luaL_openlibs(lua_State *); luaL_openlibs(L); + assert(ctx); + #ifdef USE_LUA_SQLITE3 { 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")); /* 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) { @@ -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) { const char *error_msg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "?\n";