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-2007
49 #include <sys/types.h>
68 static volatile sig_atomic_t interrupt;
69 static void intr(int sig);
70 static int handleintr(int);
73 print_usage(char *program_name)
75 printf("Usage: %s [OPTION]...[COUNTRY [PASSWORD]]\n"
76 " -2 FILE Append log of session to FILE\n"
77 " -k Kill connection\n"
79 " -h display this help and exit\n"
80 " -v display version information and exit\n",
85 main(int argc, char **argv)
88 char *auxfname = NULL;
99 WORD wVersionRequested;
105 SECURITY_ATTRIBUTES security;
114 struct ioqueue server;
115 FILE *auxout_fp = NULL;
120 * stdout is unbuffered under Windows if connected to a character
121 * device, and putchar() screws up when printing multibyte strings
122 * bytewise to an unbuffered stream. Switch stdout to line-
123 * buffered mode. Unfortunately, ISO C allows implementations to
124 * screw that up, and of course Windows does. Manual flushing
125 * after each prompt is required.
127 setvbuf(stdout, NULL, _IOLBF, 4096);
128 wVersionRequested = MAKEWORD(2, 0);
129 err = WSAStartup(wVersionRequested, &WsaData);
131 printf("WSAStartup Failed, error code %d\n", err);
139 while ((opt = getopt(argc, argv, "2:kuhv")) != EOF) {
148 utf8 = eight_bit_clean = 1;
151 print_usage(argv[0]);
154 printf("%s\n\n%s", version, legal);
157 print_usage(argv[0]);
166 country = getenv("COUNTRY");
170 passwd = getenv("PLAYER");
171 port = getenv("EMPIREPORT");
174 host = getenv("EMPIREHOST");
177 uname = getenv("LOGNAME");
182 pwd = getpwuid(getuid());
184 fprintf(stderr, "You don't exist. Go away\n");
187 uname = pwd->pw_name;
191 unamesize = sizeof(unamebuf);
192 if (GetUserName(unamebuf, &unamesize)) {
194 if ((unamesize <= 0 ) || (strlen(uname) <= 0))
202 if (auxfname && (auxout_fp = fopen(auxfname, "a")) == NULL) {
203 fprintf(stderr, "Unable to open %s for append\n", auxfname);
207 sock = tcp_connect(host, port);
209 if (!login(sock, uname, country, passwd, send_kill, utf8)) {
217 ioq_init(&server, 2048);
221 FD_SET(0, &savemask);
222 FD_SET(sock, &savemask);
223 sigemptyset(&sa.sa_mask);
225 sa.sa_handler = intr;
226 sigaction(SIGINT, &sa, NULL);
227 sa.sa_handler = SIG_IGN;
228 sigaction(SIGPIPE, &sa, NULL);
229 while (FD_ISSET(sock, &savemask)) {
231 n = select(sock + 1, &mask, NULL, NULL, NULL);
233 if (!handleintr(sock))
238 if (errno == EINTR) {
241 FD_CLR(sock, &savemask);
244 if (FD_ISSET(0, &mask)) {
245 if (!termio(0, sock, auxout_fp)) {
246 if (retry++ >= RETRY) {
247 FD_CLR(0, &savemask);
253 if (FD_ISSET(sock, &mask)) {
254 if (!serverio(sock, &server))
255 FD_CLR(sock, &savemask);
257 servercmd(&server, auxout_fp);
262 signal(SIGINT, intr);
268 if (!isatty(fileno(stdin)))
271 security.nLength = sizeof(SECURITY_ATTRIBUTES);
272 security.lpSecurityDescriptor = NULL;
273 security.bInheritHandle = TRUE;
274 hStdIn = CreateFile("CONIN$",
275 GENERIC_READ | GENERIC_WRITE,
276 FILE_SHARE_READ | FILE_SHARE_WRITE,
277 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
279 if (hStdIn == INVALID_HANDLE_VALUE) {
280 printf("Error getting hStdIn.\n");
285 err = GetConsoleMode(hStdIn, &stdinmode);
287 printf("Error getting console mode.\n");
291 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
292 err = SetConsoleMode(hStdIn, stdinmode);
294 printf("Error setting console mode.\n");
302 FD_SET(sock, &readfds);
303 n = select(sock + 1, &readfds, NULL, NULL, &tm);
305 if (!handleintr(sock))
310 if (errno == EINTR) {
311 errno = WSAGetLastError();
313 (void)closesocket(sock);
317 if (bRedirected == 1) {
318 if (!termio(0, sock, auxout_fp))
320 } else if (bRedirected == 0) {
321 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
322 termio(-1, sock, auxout_fp);
323 FlushConsoleInputBuffer(hStdIn);
326 if (FD_ISSET(sock, &readfds)) {
327 if (!serverio(sock, &server))
330 servercmd(&server, auxout_fp);
334 if (bRedirected == 0)
339 (void)closesocket(sock);
343 return 0; /* Shut the compiler up */
351 signal(SIGINT, intr);
359 /* tacky, but it works */
361 if (write(s, "\naborted\n", 1 + 7 + 1) <= 0)
363 if (send(s, "\naborted\n", 1 + 7 + 1, 0) <= 0)