2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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:
47 #include <sys/types.h>
55 #include <sys/socket.h>
57 #include <netinet/in.h>
99 INPUT_RECORD InpBuffer[10];
102 SECURITY_ATTRIBUTES security;
105 extern s_char empireport[];
106 extern s_char empirehost[];
109 struct ioqueue server;
113 s_char *auxout_fname;
119 struct sockaddr_in sin;
127 err = WSAStartup(0x0101, &WsaData);
128 if (err == SOCKET_ERROR) {
129 printf("WSAStartup Failed\n");
136 memset(argv, 0, sizeof(argv));
137 saveargv(ac, av, argv);
140 for (i = j = 1; i < ac; ++i) {
142 if (strcmp(ptr, "-2") == 0) {
144 fprintf(stderr, "-2: Missing filename!\n");
147 auxout_fname = argv[i + 1];
150 } else if (strcmp(ptr, "-k") == 0) {
158 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
159 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
163 pwd = getpwuid(getuid());
165 fprintf(stderr, "You don't exist. Go away\n");
170 if (!hostport(getenv("EMPIREPORT"), &sin) &&
171 !hostport("empire", &sin) && !hostport(empireport, &sin)) {
172 fprintf(stderr, "No empire port\n");
175 if (!hostaddr(getenv("EMPIREHOST"), &sin) &&
176 !hostaddr(empirehost, &sin)) {
177 fprintf(stderr, "No empire host\n");
180 if ((sock = hostconnect(&sin)) < 0)
182 cname = getenv("COUNTRY");
183 pname = getenv("PLAYER");
189 if (!login(sock, pwd->pw_name, cname, pname, send_kill)) {
191 if (!login(sock, "win-empcl2.8", cname, pname, send_kill)) {
196 ioq_init(&server, 2048);
200 FD_SET(0, &savemask);
201 FD_SET(sock, &savemask);
203 (void)signal(SIGINT, intr);
205 (void)signal(SIGPIPE, SIG_IGN);
206 while (FD_ISSET(sock, &savemask)) {
208 n = select(sock + 1, &mask, (fd_set *)0, (fd_set *)0,
209 (struct timeval *)0);
211 if (!handleintr(sock))
216 if (errno == EINTR) {
219 FD_CLR(sock, &savemask);
222 if (FD_ISSET(0, &mask)) {
223 if (!termio(0, sock, auxout_fp)) {
224 if (retry++ >= RETRY) {
225 FD_CLR(0, &savemask);
231 if (FD_ISSET(sock, &mask)) {
232 if (!serverio(sock, &server))
233 FD_CLR(sock, &savemask);
235 servercmd(&server, auxout_fp);
243 security.nLength = sizeof(SECURITY_ATTRIBUTES);
244 security.lpSecurityDescriptor = NULL;
245 security.bInheritHandle = TRUE;
246 hStdIn = CreateFile("CONIN$",
247 GENERIC_READ | GENERIC_WRITE,
248 FILE_SHARE_READ | FILE_SHARE_WRITE,
249 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
251 if (hStdIn == INVALID_HANDLE_VALUE) {
252 printf("Error getting hStdIn.\n");
255 err = GetConsoleMode(hStdIn, &stdinmode);
257 printf("Error getting console mode.\n");
260 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
261 err = SetConsoleMode(hStdIn, stdinmode);
263 printf("Error setting console mode.\n");
268 FD_SET(sock, &readfds);
269 n = select(sock + 1, &readfds, (fd_set *) 0, (fd_set *) 0,
270 (struct timeval *)&tm);
272 if (errno == EINTR) {
273 errno = WSAGetLastError();
279 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
280 if (!termio(-1, sock, auxout_fp)) {
281 if (retry++ >= RETRY) {
287 FlushConsoleInputBuffer(hStdIn);
289 if (FD_ISSET(sock, &readfds)) {
290 if (!serverio(sock, &server))
293 servercmd(&server, auxout_fp);
302 return 0; /* Shut the compiler up */
314 signal(SIGINT, intr);