2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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>
54 #include <sys/socket.h>
56 #include <netinet/in.h>
74 static volatile sig_atomic_t interrupt;
75 static void intr(int sig);
76 static int handleintr(int);
79 main(int ac, char **av)
87 SECURITY_ATTRIBUTES security;
95 struct ioqueue server;
101 struct sockaddr_in sin;
113 * stdout is unbuffered under Windows if connected to a character
114 * device, and putchar() screws up when printing multibyte strings
115 * bytewise to an unbuffered stream. Switch stdout to line-
116 * buffered mode. Unfortunately, ISO C allows implementations to
117 * screw that up, and of course Windows does. Manual flushing
118 * after each prompt is required.
120 setvbuf(stdout, NULL, _IOLBF, 4096);
121 err = WSAStartup(0x0101, &WsaData);
122 if (err == SOCKET_ERROR) {
123 printf("WSAStartup Failed\n");
130 memset(argv, 0, sizeof(argv));
131 saveargv(ac, av, argv);
134 for (i = j = 1; i < ac; ++i) {
136 if (strcmp(ptr, "-2") == 0) {
138 fprintf(stderr, "-2: Missing filename!\n");
141 auxout_fname = argv[i + 1];
144 } else if (strcmp(ptr, "-k") == 0) {
147 } else if (strcmp(ptr, "-u") == 0) {
148 utf8 = eight_bit_clean = 1;
155 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
156 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
160 port = getenv("EMPIREPORT");
163 if (!hostport(port, &sin)) {
164 fprintf(stderr, "Can't resolve Empire port %s\n", port);
167 host = getenv("EMPIREHOST");
170 if (!hostaddr(host, &sin)) {
171 fprintf(stderr, "Can't resolve Empire host %s\n", host);
174 if ((sock = hostconnect(&sin)) < 0) {
175 perror("Can't connect to Empire server");
178 cname = getenv("COUNTRY");
181 pname = getenv("PLAYER");
184 uname = getenv("LOGNAME");
189 pwd = getpwuid(getuid());
191 fprintf(stderr, "You don't exist. Go away\n");
194 uname = pwd->pw_name;
198 unamesize = sizeof(unamebuf);
199 if (GetUserName(unamebuf, &unamesize)) {
201 if ((unamesize <= 0 ) || (strlen(uname) <= 0))
207 if (!login(sock, uname, cname, pname, send_kill, utf8)) {
211 ioq_init(&server, 2048);
215 FD_SET(0, &savemask);
216 FD_SET(sock, &savemask);
218 (void)signal(SIGINT, intr);
220 (void)signal(SIGPIPE, SIG_IGN);
221 while (FD_ISSET(sock, &savemask)) {
223 n = select(sock + 1, &mask, NULL, NULL, NULL);
225 if (!handleintr(sock))
230 if (errno == EINTR) {
233 FD_CLR(sock, &savemask);
236 if (FD_ISSET(0, &mask)) {
237 if (!termio(0, sock, auxout_fp)) {
238 if (retry++ >= RETRY) {
239 FD_CLR(0, &savemask);
245 if (FD_ISSET(sock, &mask)) {
246 if (!serverio(sock, &server))
247 FD_CLR(sock, &savemask);
249 servercmd(&server, auxout_fp);
258 if (!_isatty(_fileno(stdin)))
261 security.nLength = sizeof(SECURITY_ATTRIBUTES);
262 security.lpSecurityDescriptor = NULL;
263 security.bInheritHandle = TRUE;
264 hStdIn = CreateFile("CONIN$",
265 GENERIC_READ | GENERIC_WRITE,
266 FILE_SHARE_READ | FILE_SHARE_WRITE,
267 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
269 if (hStdIn == INVALID_HANDLE_VALUE) {
270 printf("Error getting hStdIn.\n");
275 err = GetConsoleMode(hStdIn, &stdinmode);
277 printf("Error getting console mode.\n");
281 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
282 err = SetConsoleMode(hStdIn, stdinmode);
284 printf("Error setting console mode.\n");
292 FD_SET(sock, &readfds);
293 n = select(sock + 1, &readfds, NULL, NULL, &tm);
295 if (!handleintr(sock))
300 if (errno == EINTR) {
301 errno = WSAGetLastError();
307 if (bRedirected == 1) {
308 if (!termio(0, sock, auxout_fp))
310 } else if (bRedirected == 0) {
311 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
312 termio(-1, sock, auxout_fp);
313 FlushConsoleInputBuffer(hStdIn);
316 if (FD_ISSET(sock, &readfds)) {
317 if (!serverio(sock, &server))
320 servercmd(&server, auxout_fp);
324 if (bRedirected == 0)
329 return 0; /* Shut the compiler up */
337 signal(SIGINT, intr);
340 signal(SIGINT, intr);
348 /* tacky, but it works */
350 if (write(s, "\naborted\n", 1 + 7 + 1) <= 0)
352 if (send(s, "\naborted\n", 1 + 7 + 1, 0) <= 0)