1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-09-04 12:42:09 +03:00

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

This commit is contained in:
bel
2014-09-12 22:52:17 +02:00
parent b32232d2e1
commit d25755d4d0
2 changed files with 60 additions and 8 deletions

View File

@@ -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 <fcntl.h>
#include <io.h>
static int MakeConsole() {
DWORD err;
int ok = (GetConsoleWindow() != NULL);
@@ -1472,9 +1497,22 @@ static int MakeConsole() {
}
AttachConsole(GetCurrentProcessId());
}
ok = (GetConsoleWindow() != NULL);
if (ok) {
freopen("CON", "a", stdin);
freopen("CON", "a", stdout);
freopen("CON", "a", stderr);
ok = (GetConsoleWindow() != NULL);
}
}
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 <Cocoa/Cocoa.h>

View File

@@ -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";