2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * main.c: client main function
30 * Known contributors to this file:
33 * Ron Koenderink, 2004-2005
34 * Markus Armbruster, 2005
49 #include <sys/types.h>
67 static volatile sig_atomic_t interrupt;
68 static void intr(int sig);
69 static int handleintr(int);
72 main(int ac, char **argv)
75 WORD wVersionRequested;
81 SECURITY_ATTRIBUTES security;
90 struct ioqueue server;
106 * stdout is unbuffered under Windows if connected to a character
107 * device, and putchar() screws up when printing multibyte strings
108 * bytewise to an unbuffered stream. Switch stdout to line-
109 * buffered mode. Unfortunately, ISO C allows implementations to
110 * screw that up, and of course Windows does. Manual flushing
111 * after each prompt is required.
113 setvbuf(stdout, NULL, _IOLBF, 4096);
114 wVersionRequested = MAKEWORD(2, 0);
115 err = WSAStartup(wVersionRequested, &WsaData);
117 printf("WSAStartup Failed, error code %d\n", err);
126 for (i = j = 1; i < ac; ++i) {
128 if (strcmp(ptr, "-2") == 0) {
130 fprintf(stderr, "-2: Missing filename!\n");
133 auxout_fname = argv[i + 1];
136 } else if (strcmp(ptr, "-k") == 0) {
139 } else if (strcmp(ptr, "-u") == 0) {
140 utf8 = eight_bit_clean = 1;
147 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
148 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
152 port = getenv("EMPIREPORT");
155 host = getenv("EMPIREHOST");
158 sock = tcp_connect(host, port);
159 cname = getenv("COUNTRY");
162 pname = getenv("PLAYER");
165 uname = getenv("LOGNAME");
170 pwd = getpwuid(getuid());
172 fprintf(stderr, "You don't exist. Go away\n");
175 uname = pwd->pw_name;
179 unamesize = sizeof(unamebuf);
180 if (GetUserName(unamebuf, &unamesize)) {
182 if ((unamesize <= 0 ) || (strlen(uname) <= 0))
188 if (!login(sock, uname, cname, pname, send_kill, utf8)) {
196 ioq_init(&server, 2048);
200 FD_SET(0, &savemask);
201 FD_SET(sock, &savemask);
202 sigemptyset(&sa.sa_mask);
204 sa.sa_handler = intr;
205 sigaction(SIGINT, &sa, NULL);
206 sa.sa_handler = SIG_IGN;
207 sigaction(SIGPIPE, &sa, NULL);
208 while (FD_ISSET(sock, &savemask)) {
210 n = select(sock + 1, &mask, NULL, NULL, NULL);
212 if (!handleintr(sock))
217 if (errno == EINTR) {
220 FD_CLR(sock, &savemask);
223 if (FD_ISSET(0, &mask)) {
224 if (!termio(0, sock, auxout_fp)) {
225 if (retry++ >= RETRY) {
226 FD_CLR(0, &savemask);
232 if (FD_ISSET(sock, &mask)) {
233 if (!serverio(sock, &server))
234 FD_CLR(sock, &savemask);
236 servercmd(&server, auxout_fp);
241 signal(SIGINT, intr);
247 if (!isatty(fileno(stdin)))
250 security.nLength = sizeof(SECURITY_ATTRIBUTES);
251 security.lpSecurityDescriptor = NULL;
252 security.bInheritHandle = TRUE;
253 hStdIn = CreateFile("CONIN$",
254 GENERIC_READ | GENERIC_WRITE,
255 FILE_SHARE_READ | FILE_SHARE_WRITE,
256 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
258 if (hStdIn == INVALID_HANDLE_VALUE) {
259 printf("Error getting hStdIn.\n");
264 err = GetConsoleMode(hStdIn, &stdinmode);
266 printf("Error getting console mode.\n");
270 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
271 err = SetConsoleMode(hStdIn, stdinmode);
273 printf("Error setting console mode.\n");
281 FD_SET(sock, &readfds);
282 n = select(sock + 1, &readfds, NULL, NULL, &tm);
284 if (!handleintr(sock))
289 if (errno == EINTR) {
290 errno = WSAGetLastError();
292 (void)closesocket(sock);
296 if (bRedirected == 1) {
297 if (!termio(0, sock, auxout_fp))
299 } else if (bRedirected == 0) {
300 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
301 termio(-1, sock, auxout_fp);
302 FlushConsoleInputBuffer(hStdIn);
305 if (FD_ISSET(sock, &readfds)) {
306 if (!serverio(sock, &server))
309 servercmd(&server, auxout_fp);
313 if (bRedirected == 0)
318 (void)closesocket(sock);
322 return 0; /* Shut the compiler up */
330 signal(SIGINT, intr);
338 /* tacky, but it works */
340 if (write(s, "\naborted\n", 1 + 7 + 1) <= 0)
342 if (send(s, "\naborted\n", 1 + 7 + 1, 0) <= 0)