# HG changeset patch # User Matti Hamalainen # Date 1226639757 -7200 # Node ID 3ab7751fdad159ac1d4ed8968ea9139421892f64 # Parent 4086088e95dcd5b48fbab9c242a86834f8e17142 MingW compatibility, with one ugly kludge. diff -r 4086088e95dc -r 3ab7751fdad1 Makefile --- a/Makefile Thu Nov 13 16:46:15 2008 +0200 +++ b/Makefile Fri Nov 14 07:15:57 2008 +0200 @@ -9,7 +9,7 @@ # C-compiler, flags and linker flags CC=gcc CFLAGS=-DTH_NO_DEFAULTS -DHAVE_STRING_H -DHAVE_INT_TYPES -LDFLAGS= +LDFLAGS=-lcurses #CFLAGS += -DHAVE_STDINT_H #CFLAGS += -DHAVE_SYS_TYPES_H diff -r 4086088e95dc -r 3ab7751fdad1 Makefile.gen --- a/Makefile.gen Thu Nov 13 16:46:15 2008 +0200 +++ b/Makefile.gen Fri Nov 14 07:15:57 2008 +0200 @@ -18,7 +18,7 @@ $(COMP) -c -o $@ $< $(NNCHAT_BIN): nnchat.c libnnchat.o th_util.o th_string.o th_args.o - $(COMP) -o $@ $+ $(LDFLAGS) -lcurses + $(COMP) -o $@ $+ $(LDFLAGS) # # Special targets diff -r 4086088e95dc -r 3ab7751fdad1 Makefile.w32 --- a/Makefile.w32 Thu Nov 13 16:46:15 2008 +0200 +++ b/Makefile.w32 Fri Nov 14 07:15:57 2008 +0200 @@ -7,8 +7,8 @@ # C-compiler, flags and linker flags CC=gcc -CFLAGS=-DHAVE_STRING_H -LDFLAGS=-L/usr/local/i386-mingw32/lib/ -liberty -lws2_32 -s +CFLAGS=-DHAVE_STRING_H -Ipdcurses +LDFLAGS=-L/usr/local/i386-mingw32/lib/ pdcurses/pdcurses.a -liberty -lws2_32 -s # Miscellaneous RM=rm diff -r 4086088e95dc -r 3ab7751fdad1 libnnchat.h --- a/libnnchat.h Thu Nov 13 16:46:15 2008 +0200 +++ b/libnnchat.h Fri Nov 14 07:15:57 2008 +0200 @@ -9,7 +9,7 @@ #include #include #ifdef __WIN32 -#warning Compiling for Windows +#include #include #else #include diff -r 4086088e95dc -r 3ab7751fdad1 nnchat.c --- a/nnchat.c Thu Nov 13 16:46:15 2008 +0200 +++ b/nnchat.c Fri Nov 14 07:15:57 2008 +0200 @@ -10,10 +10,11 @@ #include #include #ifdef __WIN32 +#define usleep(t) Sleep((t) / 1000) +/* Undefine because both windows.h and curses.h #define it */ +#undef MOUSE_MOVED +#endif #include -#else -#include -#endif #define SET_MAX_BACKBUF (1024) @@ -56,13 +57,6 @@ const int optListN = (sizeof(optList) / sizeof(optList[0])); -void argShowHelp() -{ - th_args_help(stdout, optList, optListN, th_prog_name, - "[options] "); -} - - int getColor(char *str) { char *p = str; @@ -82,6 +76,14 @@ return (len == 6) ? val : -1; } + +void argShowHelp() +{ + th_args_help(stdout, optList, optListN, th_prog_name, + "[options] "); +} + + BOOL argHandleOpt(const int optN, char *optArg, char *currArg) { switch (optN) { @@ -147,6 +149,36 @@ return TRUE; } +#ifdef __WIN32 +const char *hstrerror(int err) +{ + static char buf[64]; + snprintf(buf, sizeof(buf), "Error #%d", err); + return buf; +} + +int getSocketErrno(void) +{ + return WSAGetLastError(); +} + +const char *getSocketErrStr(int err) +{ + static char buf[64]; + snprintf(buf, sizeof(buf), "Error #%d", err); + return buf; +} +#else +int getSocketErrno(void) +{ + return errno; +} + +const char *getSocketErrStr(int err) +{ + return strerror(err); +} +#endif void updateStatus(BOOL insertMode) { @@ -573,6 +605,9 @@ exitProg = FALSE, colorSet = FALSE, cursesInit = FALSE, +#if __WIN32 + networkInit = FALSE, +#endif insertMode = TRUE; struct timeval tv; fd_set sockfds; @@ -603,17 +638,6 @@ return -2; -#ifdef __WIN32 - { - /* Initialize WinSock, if needed */ - WSADATA wsaData; - int err = WSAStartup(0x0200, &wsaData); - if (err != 0) { - THERR("Could not initialize WinSock DLL: %d\n", err); - return -3; - } -#endif - /* Open logfile */ if (optLogFilename) { THMSG(1, "Opening logfile '%s'\n", optLogFilename); @@ -624,16 +648,29 @@ } } +#ifdef __WIN32 + { + /* Initialize WinSock, if needed */ + WSADATA wsaData; + int err = WSAStartup(0x0101, &wsaData); + if (err != 0) { + THERR("Could not initialize WinSock DLL: %d\n", err); + goto err_exit; + } + networkInit = TRUE; +#endif + /* Okay ... */ THMSG(1, "Trying to resolve host '%s' ...\n", optServer); tmpHost = gethostbyname(optServer); if (tmpHost == NULL) { THERR("Could not resolve hostname: %s.\n", hstrerror(h_errno)); - return -3; + goto err_exit; } THMSG(2, "True hostname: %s\n", tmpHost->h_name); + #if 0 /* To emulate the official client, we first make a request for * policy file, even though we don't use it for anything... @@ -671,6 +708,9 @@ /* Initialize NCurses */ if (!optDaemon) { + if (LINES < 0 || LINES > 1000) LINES = 24; + if (COLS < 0 || COLS > 1000) COLS = 80; + LINES=24; initscr(); raw(); keypad(stdscr, TRUE); @@ -710,7 +750,6 @@ updateStatus(insertMode); } - /* Enter mainloop */ FD_ZERO(&sockfds); FD_SET(tmpSocket, &sockfds); @@ -724,8 +763,10 @@ tv.tv_usec = SET_DELAY_USEC; tmpfds = sockfds; if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) { - if (errno != EINTR && errno != ERESTART) { - printMsg("Error occured in select(sockfds): %d, %s\n", errno, strerror(errno)); + int res = getSocketErrno(); + if (res != EINTR) { + printMsg("Error occured in select(sockfds): %d, %s\n", + res, getSocketErrStr(res)); isError = TRUE; } } else if (FD_ISSET(tmpSocket, &tmpfds)) { @@ -735,7 +776,8 @@ gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); if (gotBuf < 0) { - printMsg("Error in recv: %s\n", strerror(errno)); + int res = getSocketErrno(); + printMsg("Error in recv: %d, %s\n", res, getSocketErrStr(res)); isError = TRUE; } else if (gotBuf == 0) { printMsg("Server closed connection.\n"); @@ -955,6 +997,11 @@ closeConnection(tmpSocket); +#ifdef __WIN32 + if (networkInit) + WSACleanup(); +#endif + THMSG(1, "Connection terminated.\n"); if (optLogFile) { @@ -964,3 +1011,8 @@ return 0; } + +#ifdef __WIN32 +/* This is a really weird kludge ... */ +} +#endif