From 0a3dd0f840ff12e312601804252e0c85066a91ce Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 6 Jan 2010 01:56:42 +0000 Subject: builds now on windows using MinGW and MSYS removed Visual Studio Files added signal handler for Windows --- README | 16 +-- src/Makefile | 1 + src/anylike.c | 6 +- src/configure | 51 +++++--- src/datatypes.h | 13 -- src/log.c | 6 +- src/log.h | 2 +- src/log_targets.h | 2 +- src/sig_handler.c | 169 ++++++++++++++++++++++-- src/win32/anylike.sln | 20 --- src/win32/anylike.suo | Bin 17920 -> 0 bytes src/win32/anylike.vcproj | 275 ---------------------------------------- src/win32/make_lua_bytecode.bat | 10 -- src/win32/make_version_h.bat | 22 ---- 14 files changed, 201 insertions(+), 392 deletions(-) delete mode 100644 src/win32/anylike.sln delete mode 100644 src/win32/anylike.suo delete mode 100644 src/win32/anylike.vcproj delete mode 100644 src/win32/make_lua_bytecode.bat delete mode 100644 src/win32/make_version_h.bat diff --git a/README b/README index 2da8920..4b605ee 100644 --- a/README +++ b/README @@ -92,19 +92,9 @@ Notes: Building on Windows ------------------- -Download Lua 5.1 from www.lua.org and extract it inside the anylike -root directory. Open the vcproj file inside src\win32 directory. Open -the Visual Studio Prompt from the Tools menu and change to the Lua -directory you just created (i.e.: cd c:\anylike\lua-5.1.4\). -Use the following command to build lua: +anylike can be built on Windows using MinGW and MSYS.. -etc\luavs.bat - -After this there should be a lua51.lib as well as a lua.exe and a luac.exe -inside the src directory of the Lua tree. -Close the Command Prompt select the target (Release or Debug) and compile -the project. Now there should be an anylike.exe inside src\win32\Release or -src\win32\Debug depending on the target you have selected. +tba ... Installing @@ -131,4 +121,4 @@ This also removes the config files Usage: ====== -tba.. \ No newline at end of file +tba.. diff --git a/src/Makefile b/src/Makefile index 208b35b..cbd0900 100644 --- a/src/Makefile +++ b/src/Makefile @@ -101,6 +101,7 @@ clean: rm -f $(LUA_BYTECODE_OBJ) rm -f $(LUA_BYTECODE_OBJ:%.o=%.c) rm -f $(EXECUTABLE) + rm -f $(EXECUTABLE).exe cleanall: clean $(MAKE) --directory="../doc/" clean diff --git a/src/anylike.c b/src/anylike.c index 70f03c5..637c527 100644 --- a/src/anylike.c +++ b/src/anylike.c @@ -49,7 +49,7 @@ #include #endif -#ifndef _MSC_VER +#ifndef WINVER #include "daemon.h" #endif @@ -243,7 +243,7 @@ int main(int argc, char* argv[]) log_printf(NOTICE, "just started..."); options_parse_post(&opt); -#ifndef _MSC_VER +#ifndef WINVER priv_info_t priv; if(opt.username_) if(priv_init(&priv, opt.username_, opt.groupname_)) { @@ -263,7 +263,7 @@ int main(int argc, char* argv[]) } #endif -#ifndef _MSC_VER +#ifndef WINVER FILE* pid_file = NULL; if(opt.pid_file_) { pid_file = fopen(opt.pid_file_, "w"); diff --git a/src/configure b/src/configure index f5e7050..b07e4fd 100755 --- a/src/configure +++ b/src/configure @@ -127,6 +127,17 @@ case $TARGET in CFLAGS=$CFLAGS' -I/usr/local/include' LDFLAGS=$LDFLAGS' -L/usr/local/lib' ;; + MINGW*) + CFLAGS=$CFLAGS' -DWINVER=0x501 -D_WIN32_WINNT -D_WIN32' + LDFLAGS=$LDFLAGS' -lwsock32' + TARGET=mingw + CRYPTO_LIB=openssl + if [ -z "$LUA_DIR" ]; then + echo "No Lua tree specified, use --with-lua" + exit 1 + fi + echo "WARNING: don't use install target on this platform" + ;; *) echo "platform not supported" exit 1; @@ -247,27 +258,33 @@ SBINDIR := $SBINDIR ETCDIR := $ETCDIR EOF -if [ $INSTALLMANPAGE -eq 1 ]; then - echo "MANDIR := $MANDIR" >> include.mk - echo "installing manpage" -else - echo "not installing manpage" -fi +if [ "$TARGET" != "mingw" ]; then + if [ $INSTALLMANPAGE -eq 1 ]; then + echo "MANDIR := $MANDIR" >> include.mk + echo "installing manpage" + else + echo "not installing manpage" + fi -if [ $INSTALLEXAMPLES -eq 1 ]; then - echo "EXAMPLESDIR := $EXAMPLESDIR" >> include.mk - echo "installing example files" -else - echo "not installing example files" -fi + if [ $INSTALLEXAMPLES -eq 1 ]; then + echo "EXAMPLESDIR := $EXAMPLESDIR" >> include.mk + echo "installing example files" + else + echo "not installing example files" + fi -VERSION=`cat ../version` -if which svn >/dev/null; then - SVN_REV=`svn info | grep "^Revision: " | awk '{print($2)}'` - if [ -n "$SVN_REV" ]; then - VERSION="$VERSION (svn$SVN_REV)" + VERSION=`cat ../version` + if which svn >/dev/null; then + SVN_REV=`svn info | grep "^Revision: " | awk '{print($2)}'` + if [ -n "$SVN_REV" ]; then + VERSION="$VERSION (svn$SVN_REV)" + fi fi + +else + VERSION=`cat ../version` fi + HOSTNAME=`hostname` DATE=`date +"%d.%m.%Y %H:%M:%S %Z"` diff --git a/src/datatypes.h b/src/datatypes.h index a38c8ef..051d62e 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ #ifndef ANYLIKE_datatypes_h_INCLUDED #define ANYLIKE_datatypes_h_INCLUDED -#ifndef _MSC_VER #include typedef uint8_t u_int8_t; @@ -43,18 +42,6 @@ typedef uint64_t u_int64_t; /* typedef int16_t int16_t; */ /* typedef int32_t int32_t; */ /* typedef int64_t int64_t; */ -#else -#pragma once - -typedef signed __int8 int8_t; -typedef unsigned __int8 u_int8_t; -typedef signed __int16 int16_t; -typedef unsigned __int16 u_int16_t; -typedef signed __int32 int32_t; -typedef unsigned __int32 u_int32_t; -typedef signed __int64 int64_t; -typedef unsigned __int64 u_int64_t; -#endif struct buffer_struct { u_int32_t length_; diff --git a/src/log.c b/src/log.c index d686f35..7031069 100644 --- a/src/log.c +++ b/src/log.c @@ -37,7 +37,7 @@ #include #include -#ifndef _MSC_VER +#ifndef WINVER #define SYSLOG_NAMES #include #endif @@ -65,7 +65,7 @@ log_target_type_t log_target_parse_type(const char* conf) if(!conf) return TARGET_UNKNOWN; -#ifndef _MSC_VER +#ifndef WINVER if(!strncmp(conf, "syslog", 6)) return TARGET_SYSLOG; #endif if(!strncmp(conf, "file", 4)) return TARGET_FILE; @@ -97,7 +97,7 @@ int log_targets_add(log_targets_t* targets, const char* conf) log_target_t* new_target = NULL; int duplicates_allowed = 0; switch(log_target_parse_type(conf)) { -#ifndef _MSC_VER +#ifndef WINVER case TARGET_SYSLOG: new_target = log_target_syslog_new(); break; #endif case TARGET_FILE: new_target = log_target_file_new(); duplicates_allowed = 1; break; diff --git a/src/log.h b/src/log.h index 3c642f4..0f05c08 100644 --- a/src/log.h +++ b/src/log.h @@ -40,7 +40,7 @@ typedef enum log_prio_enum log_prio_t; const char* log_prio_to_string(log_prio_t prio); -#ifndef _MSC_VER +#ifndef WINVER enum log_target_type_enum { TARGET_SYSLOG , TARGET_STDOUT, TARGET_STDERR, TARGET_FILE , TARGET_UNKNOWN }; #else enum log_target_type_enum { TARGET_STDOUT, TARGET_STDERR, TARGET_FILE , TARGET_UNKNOWN }; diff --git a/src/log_targets.h b/src/log_targets.h index 3dbe421..b0be138 100644 --- a/src/log_targets.h +++ b/src/log_targets.h @@ -57,7 +57,7 @@ static char* get_time_formatted() return time_string; } -#ifndef _MSC_VER +#ifndef WINVER enum syslog_facility_enum { USER = LOG_USER, MAIL = LOG_MAIL, DAEMON = LOG_DAEMON, AUTH = LOG_AUTH, SYSLOG = LOG_SYSLOG, LPR = LOG_LPR, diff --git a/src/sig_handler.c b/src/sig_handler.c index 69dc0a7..461c341 100644 --- a/src/sig_handler.c +++ b/src/sig_handler.c @@ -37,21 +37,18 @@ #include "log.h" -#ifndef _MSC_VER +#include "sig_handler.h" +#include + +#ifndef WINVER + #include #include #include #include -#include - -#include "sig_handler.h" - -#include - static int sig_pipe_fds[2]; - static void sig_handler(int sig) { sigset_t set; @@ -159,21 +156,165 @@ void signal_stop() close(sig_pipe_fds[0]); close(sig_pipe_fds[1]); } + #else -int signal_init() + +#include +#include + +static SOCKET sig_sock_fds[2]; +static SOCKET server_sock; + +static DWORD WINAPI signal_connect_thread(void* p) { - // nothing yet + u_int16_t port = *((u_int16_t*)p); + + SOCKET client; + client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if(client == INVALID_SOCKET) { + log_printf(ERROR, "Error at socket(): 0x%04X\n", WSAGetLastError()); + return -1; + } + + struct sockaddr_in server; + server.sin_family = AF_INET; + server.sin_addr.s_addr = inet_addr("127.0.0.1"); + server.sin_port = port; + + int cnt = 0; + int ret = SOCKET_ERROR; + do { + Sleep(100); + ret = connect(client, (SOCKADDR*) &server, sizeof(server)); + cnt++; + } + while(ret != 0 && cnt < 10); + + if(ret != 0) { + log_printf(ERROR, "connect() failed: 0x%04X", WSAGetLastError()); + closesocket(client); + return -1; + } + + log_printf(DEBUG, "sig_handler connected to port %d", port); + + sig_sock_fds[1] = client; + return 0; } +static BOOL WINAPI signal_ctrl_handler(DWORD ctrlType) +{ + char c = (char)ctrlType; + send(sig_sock_fds[1], &c, 1, 0); + return 1; +} + +int signal_init() +{ + WSADATA wsaData; + int ret = WSAStartup(MAKEWORD(2,2), &wsaData); + if(ret != NO_ERROR) { + log_printf(ERROR, "Error at WSAStartup(): 0x%04X", WSAGetLastError()); + return -1; + } + + server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if(server_sock == INVALID_SOCKET) { + log_printf(ERROR, "Error at socket(): 0x%04X\n", WSAGetLastError()); + WSACleanup(); + return -1; + } + + struct sockaddr_in service; + service.sin_family = AF_INET; + service.sin_addr.s_addr = inet_addr("127.0.0.1"); + service.sin_port = 0; + + if(bind(server_sock, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR) { + log_printf(ERROR, "bind() failed: 0x%04X", WSAGetLastError()); + closesocket(server_sock); + WSACleanup(); + return -1; + } + + struct sockaddr_in hugo; + int hugo_len = sizeof(hugo); + if(getsockname(server_sock, (SOCKADDR*) &hugo, &hugo_len) == SOCKET_ERROR) { + log_printf(ERROR, "getsockname() failed: 0x%04X", WSAGetLastError()); + closesocket(server_sock); + WSACleanup(); + return -1; + } + + if(listen(server_sock, 1) == SOCKET_ERROR) { + log_printf(ERROR, "listen() failed: 0x%04X", WSAGetLastError()); + closesocket(server_sock); + WSACleanup(); + return -1; + } + + log_printf(DEBUG, "sig_handler listening on port %d", hugo.sin_port); + + HANDLE t = CreateThread(NULL, 0, signal_connect_thread, &(hugo.sin_port), 0, NULL); + if(t == INVALID_HANDLE_VALUE) { + log_printf(ERROR, "CreateThread() failed: 0x%04X", GetLastError()); + closesocket(server_sock); + WSACleanup(); + return -1; + } + + sig_sock_fds[0] = accept(server_sock, NULL, NULL); + if(sig_sock_fds[0] == INVALID_SOCKET) { + log_printf(ERROR, "accept() failed: 0x%04X", WSAGetLastError()); + closesocket(server_sock); + WSACleanup(); + WaitForSingleObject(t, INFINITE); + return -1; + } + + WaitForSingleObject(t, INFINITE); + + if(!SetConsoleCtrlHandler(signal_ctrl_handler, 1)) { + log_printf(ERROR, "adding console ctrl handler failed: 0x%04X" , GetLastError()); + } + + return sig_sock_fds[0]; +} + int signal_handle() { - // nothing yet - return 0; + char c; + int ret = recv(sig_sock_fds[0], &c, 1, 0); + if(ret == 1) { + switch(c) { + case CTRL_C_EVENT: log_printf(NOTICE, "CTRL-C Event received, exitting"); return 1; + case CTRL_BREAK_EVENT: log_printf(NOTICE, "CTRL-Break Event received, ignoring"); return 1; + case CTRL_CLOSE_EVENT: log_printf(NOTICE, "Close Event received, exitting"); return 1; + case CTRL_LOGOFF_EVENT: log_printf(NOTICE, "LogOff Event received, exitting"); return 1; + case CTRL_SHUTDOWN_EVENT: log_printf(NOTICE, "Shutdown Event received, exitting"); return 1; + default: log_printf(NOTICE, "unknown event received 0x%02X, ignoring", c); return 0; + } + } + + if(ret == 0) { + log_printf(ERROR, "signal socket closed normally, exitting"); + return 1; + } + + log_printf(ERROR, "signal socket closed with error 0x%04X, exitting", GetLastError()); + return 1; } void signal_stop() { - // nothing yet + if(!SetConsoleCtrlHandler(signal_ctrl_handler, 0)) { + log_printf(ERROR, "removeing console ctrl handler failed: 0x%04X" , GetLastError()); + } + closesocket(sig_sock_fds[0]); + closesocket(sig_sock_fds[1]); + closesocket(server_sock); + WSACleanup(); + return; } -#endif \ No newline at end of file +#endif diff --git a/src/win32/anylike.sln b/src/win32/anylike.sln deleted file mode 100644 index fc673bc..0000000 --- a/src/win32/anylike.sln +++ /dev/null @@ -1,20 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual C++ Express 2008 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "anylike", "anylike.vcproj", "{5C3917F1-A0E2-46CE-AC2F-BB59A69AC320}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Release|Win32 = Release|Win32 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {5C3917F1-A0E2-46CE-AC2F-BB59A69AC320}.Debug|Win32.ActiveCfg = Debug|Win32 - {5C3917F1-A0E2-46CE-AC2F-BB59A69AC320}.Debug|Win32.Build.0 = Debug|Win32 - {5C3917F1-A0E2-46CE-AC2F-BB59A69AC320}.Release|Win32.ActiveCfg = Release|Win32 - {5C3917F1-A0E2-46CE-AC2F-BB59A69AC320}.Release|Win32.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/src/win32/anylike.suo b/src/win32/anylike.suo deleted file mode 100644 index 83681ee..0000000 Binary files a/src/win32/anylike.suo and /dev/null differ diff --git a/src/win32/anylike.vcproj b/src/win32/anylike.vcproj deleted file mode 100644 index 3240d64..0000000 --- a/src/win32/anylike.vcproj +++ /dev/null @@ -1,275 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/win32/make_lua_bytecode.bat b/src/win32/make_lua_bytecode.bat deleted file mode 100644 index f9a8e61..0000000 --- a/src/win32/make_lua_bytecode.bat +++ /dev/null @@ -1,10 +0,0 @@ -@echo off - -@setlocal -@set LUATOP=%1 -@set LUA_BYTECODE=anylike.lc -@set LUA_BYTECODE_SRC=anylike_lua_bytecode -@set LUA_SRC=main_loop.lua echo_server.lua - -%LUATOP%\src\luac.exe -o %LUA_BYTECODE% %LUA_SRC% -%LUATOP%\src\lua.exe ..\tools\bin2c.lua %LUA_BYTECODE% %LUA_BYTECODE_SRC% > %LUA_BYTECODE_SRC%.c \ No newline at end of file diff --git a/src/win32/make_version_h.bat b/src/win32/make_version_h.bat deleted file mode 100644 index f68f52e..0000000 --- a/src/win32/make_version_h.bat +++ /dev/null @@ -1,22 +0,0 @@ -@echo off - -@setlocal -@set /p VERSION=<..\version -@set OUTPUT_FILE=version.h - -echo /* > %OUTPUT_FILE% -echo * anylike version info >> %OUTPUT_FILE% -echo * >> %OUTPUT_FILE% -echo * this file was created automatically >> %OUTPUT_FILE% -echo * do not edit this file directly >> %OUTPUT_FILE% -echo * use win32/make_version_h.bat instead >> %OUTPUT_FILE% -echo */ >> %OUTPUT_FILE% -echo. >> %OUTPUT_FILE% -echo #ifndef ANYLIKE_version_h_INCLUDED >> %OUTPUT_FILE% -echo #define ANYLIKE_version_h_INCLUDED >> %OUTPUT_FILE% -echo. >> %OUTPUT_FILE% -echo #define VERSION_STRING_0 "anylike version %VERSION%" >> %OUTPUT_FILE% -echo #define VERSION_STRING_1 "built on %COMPUTERNAME%, %DATE% %TIME%" >> %OUTPUT_FILE% -echo. >> %OUTPUT_FILE% -echo #endif >> %OUTPUT_FILE% - -- cgit v1.2.3