2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * main.c: client main function
30 * Known contributors to this file:
46 #include <sys/types.h>
53 #include <sys/socket.h>
55 #include <netinet/in.h>
73 static volatile sig_atomic_t interrupt;
74 static void intr(int sig);
75 static int handleintr(int);
78 main(int ac, char **av)
86 SECURITY_ATTRIBUTES security;
95 struct ioqueue server;
112 * stdout is unbuffered under Windows if connected to a character
113 * device, and putchar() screws up when printing multibyte strings
114 * bytewise to an unbuffered stream. Switch stdout to line-
115 * buffered mode. Unfortunately, ISO C allows implementations to
116 * screw that up, and of course Windows does. Manual flushing
117 * after each prompt is required.
119 setvbuf(stdout, NULL, _IOLBF, 4096);
120 err = WSAStartup(0x0101, &WsaData);
121 if (err == SOCKET_ERROR) {
122 printf("WSAStartup Failed\n");
129 memset(argv, 0, sizeof(argv));
130 saveargv(ac, av, argv);
133 for (i = j = 1; i < ac; ++i) {
135 if (strcmp(ptr, "-2") == 0) {
137 fprintf(stderr, "-2: Missing filename!\n");
140 auxout_fname = argv[i + 1];
143 } else if (strcmp(ptr, "-k") == 0) {
146 } else if (strcmp(ptr, "-u") == 0) {
147 utf8 = eight_bit_clean = 1;
154 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
155 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
159 port = getenv("EMPIREPORT");
162 host = getenv("EMPIREHOST");
165 sock = tcp_connect(host, port);
166 cname = getenv("COUNTRY");
169 pname = getenv("PLAYER");
172 uname = getenv("LOGNAME");
177 pwd = getpwuid(getuid());
179 fprintf(stderr, "You don't exist. Go away\n");
182 uname = pwd->pw_name;
186 unamesize = sizeof(unamebuf);
187 if (GetUserName(unamebuf, &unamesize)) {
189 if ((unamesize <= 0 ) || (strlen(uname) <= 0))
195 if (!login(sock, uname, cname, pname, send_kill, utf8)) {
199 ioq_init(&server, 2048);
203 FD_SET(0, &savemask);
204 FD_SET(sock, &savemask);
205 sigemptyset(&sa.sa_mask);
207 sa.sa_handler = intr;
208 sigaction(SIGINT, &sa, NULL);
209 sa.sa_handler = SIG_IGN;
210 sigaction(SIGPIPE, &sa, NULL);
211 while (FD_ISSET(sock, &savemask)) {
213 n = select(sock + 1, &mask, NULL, NULL, NULL);
215 if (!handleintr(sock))
220 if (errno == EINTR) {
223 FD_CLR(sock, &savemask);
226 if (FD_ISSET(0, &mask)) {
227 if (!termio(0, sock, auxout_fp)) {
228 if (retry++ >= RETRY) {
229 FD_CLR(0, &savemask);
235 if (FD_ISSET(sock, &mask)) {
236 if (!serverio(sock, &server))
237 FD_CLR(sock, &savemask);
239 servercmd(&server, auxout_fp);
244 signal(SIGINT, intr);
250 if (!_isatty(_fileno(stdin)))
253 security.nLength = sizeof(SECURITY_ATTRIBUTES);
254 security.lpSecurityDescriptor = NULL;
255 security.bInheritHandle = TRUE;
256 hStdIn = CreateFile("CONIN$",
257 GENERIC_READ | GENERIC_WRITE,
258 FILE_SHARE_READ | FILE_SHARE_WRITE,
259 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
261 if (hStdIn == INVALID_HANDLE_VALUE) {
262 printf("Error getting hStdIn.\n");
267 err = GetConsoleMode(hStdIn, &stdinmode);
269 printf("Error getting console mode.\n");
273 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
274 err = SetConsoleMode(hStdIn, stdinmode);
276 printf("Error setting console mode.\n");
284 FD_SET(sock, &readfds);
285 n = select(sock + 1, &readfds, NULL, NULL, &tm);
287 if (!handleintr(sock))
292 if (errno == EINTR) {
293 errno = WSAGetLastError();
299 if (bRedirected == 1) {
300 if (!termio(0, sock, auxout_fp))
302 } else if (bRedirected == 0) {
303 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
304 termio(-1, sock, auxout_fp);
305 FlushConsoleInputBuffer(hStdIn);
308 if (FD_ISSET(sock, &readfds)) {
309 if (!serverio(sock, &server))
312 servercmd(&server, auxout_fp);
316 if (bRedirected == 0)
321 return 0; /* Shut the compiler up */
329 signal(SIGINT, intr);
337 /* tacky, but it works */
339 if (write(s, "\naborted\n", 1 + 7 + 1) <= 0)
341 if (send(s, "\naborted\n", 1 + 7 + 1, 0) <= 0)