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
51 #include <sys/types.h>
69 static volatile sig_atomic_t interrupt;
70 static void intr(int sig);
71 static int handleintr(int);
74 main(int ac, char **av)
77 WORD wVersionRequested;
83 SECURITY_ATTRIBUTES security;
92 struct ioqueue server;
109 * stdout is unbuffered under Windows if connected to a character
110 * device, and putchar() screws up when printing multibyte strings
111 * bytewise to an unbuffered stream. Switch stdout to line-
112 * buffered mode. Unfortunately, ISO C allows implementations to
113 * screw that up, and of course Windows does. Manual flushing
114 * after each prompt is required.
116 setvbuf(stdout, NULL, _IOLBF, 4096);
117 wVersionRequested = MAKEWORD(2, 0);
118 err = WSAStartup(wVersionRequested, &WsaData);
120 printf("WSAStartup Failed, error code %d\n", err);
127 memset(argv, 0, sizeof(argv));
128 saveargv(ac, av, argv);
131 for (i = j = 1; i < ac; ++i) {
133 if (strcmp(ptr, "-2") == 0) {
135 fprintf(stderr, "-2: Missing filename!\n");
138 auxout_fname = argv[i + 1];
141 } else if (strcmp(ptr, "-k") == 0) {
144 } else if (strcmp(ptr, "-u") == 0) {
145 utf8 = eight_bit_clean = 1;
152 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
153 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
157 port = getenv("EMPIREPORT");
160 host = getenv("EMPIREHOST");
163 sock = tcp_connect(host, port);
164 cname = getenv("COUNTRY");
167 pname = getenv("PLAYER");
170 uname = getenv("LOGNAME");
175 pwd = getpwuid(getuid());
177 fprintf(stderr, "You don't exist. Go away\n");
180 uname = pwd->pw_name;
184 unamesize = sizeof(unamebuf);
185 if (GetUserName(unamebuf, &unamesize)) {
187 if ((unamesize <= 0 ) || (strlen(uname) <= 0))
193 if (!login(sock, uname, cname, pname, send_kill, utf8)) {
201 ioq_init(&server, 2048);
205 FD_SET(0, &savemask);
206 FD_SET(sock, &savemask);
207 sigemptyset(&sa.sa_mask);
209 sa.sa_handler = intr;
210 sigaction(SIGINT, &sa, NULL);
211 sa.sa_handler = SIG_IGN;
212 sigaction(SIGPIPE, &sa, NULL);
213 while (FD_ISSET(sock, &savemask)) {
215 n = select(sock + 1, &mask, NULL, NULL, NULL);
217 if (!handleintr(sock))
222 if (errno == EINTR) {
225 FD_CLR(sock, &savemask);
228 if (FD_ISSET(0, &mask)) {
229 if (!termio(0, sock, auxout_fp)) {
230 if (retry++ >= RETRY) {
231 FD_CLR(0, &savemask);
237 if (FD_ISSET(sock, &mask)) {
238 if (!serverio(sock, &server))
239 FD_CLR(sock, &savemask);
241 servercmd(&server, auxout_fp);
246 signal(SIGINT, intr);
252 if (!isatty(fileno(stdin)))
255 security.nLength = sizeof(SECURITY_ATTRIBUTES);
256 security.lpSecurityDescriptor = NULL;
257 security.bInheritHandle = TRUE;
258 hStdIn = CreateFile("CONIN$",
259 GENERIC_READ | GENERIC_WRITE,
260 FILE_SHARE_READ | FILE_SHARE_WRITE,
261 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
263 if (hStdIn == INVALID_HANDLE_VALUE) {
264 printf("Error getting hStdIn.\n");
269 err = GetConsoleMode(hStdIn, &stdinmode);
271 printf("Error getting console mode.\n");
275 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
276 err = SetConsoleMode(hStdIn, stdinmode);
278 printf("Error setting console mode.\n");
286 FD_SET(sock, &readfds);
287 n = select(sock + 1, &readfds, NULL, NULL, &tm);
289 if (!handleintr(sock))
294 if (errno == EINTR) {
295 errno = WSAGetLastError();
297 (void)closesocket(sock);
301 if (bRedirected == 1) {
302 if (!termio(0, sock, auxout_fp))
304 } else if (bRedirected == 0) {
305 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
306 termio(-1, sock, auxout_fp);
307 FlushConsoleInputBuffer(hStdIn);
310 if (FD_ISSET(sock, &readfds)) {
311 if (!serverio(sock, &server))
314 servercmd(&server, auxout_fp);
318 if (bRedirected == 0)
323 (void)closesocket(sock);
327 return 0; /* Shut the compiler up */
335 signal(SIGINT, intr);
343 /* tacky, but it works */
345 if (write(s, "\naborted\n", 1 + 7 + 1) <= 0)
347 if (send(s, "\naborted\n", 1 + 7 + 1, 0) <= 0)