changeset 68:3ab7751fdad1

MingW compatibility, with one ugly kludge.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Nov 2008 07:15:57 +0200
parents 4086088e95dc
children 79982b0aad97
files Makefile Makefile.gen Makefile.w32 libnnchat.h nnchat.c
diffstat 5 files changed, 83 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- 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
--- 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 <stdio.h>
 #include <unistd.h>
 #ifdef __WIN32
-#warning Compiling for Windows
+#include <windows.h>
 #include <winsock.h>
 #else
 #include <sys/socket.h>
--- 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 <errno.h>
 #include <time.h>
 #ifdef __WIN32
+#define usleep(t) Sleep((t) / 1000)
+/* Undefine because both windows.h and curses.h #define it */
+#undef MOUSE_MOVED
+#endif
 #include <curses.h>
-#else
-#include <ncurses.h>
-#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] <username> <password>");
-}
-
-
 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] <username> <password>");
+}
+
+
 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