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:
46 #include <sys/types.h>
54 #include <sys/socket.h>
56 #include <netinet/in.h>
98 INPUT_RECORD InpBuffer[10];
101 SECURITY_ATTRIBUTES security;
104 extern s_char empireport[];
105 extern s_char empirehost[];
108 struct ioqueue server;
112 s_char *auxout_fname;
118 struct sockaddr_in sin;
126 err = WSAStartup(0x0101, &WsaData);
127 if (err == SOCKET_ERROR) {
128 printf("WSAStartup Failed\n");
135 memset(argv, 0, sizeof(argv));
136 saveargv(ac, av, argv);
139 for (i = j = 1; i < ac; ++i) {
141 if (strcmp(ptr, "-2") == 0) {
143 fprintf(stderr, "-2: Missing filename!\n");
146 auxout_fname = argv[i + 1];
149 } else if (strcmp(ptr, "-k") == 0) {
157 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
158 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
162 pwd = getpwuid(getuid());
164 fprintf(stderr, "You don't exist. Go away\n");
169 if (!hostport(getenv("EMPIREPORT"), &sin) &&
170 !hostport("empire", &sin) && !hostport(empireport, &sin)) {
171 fprintf(stderr, "No empire port\n");
174 if (!hostaddr(getenv("EMPIREHOST"), &sin) &&
175 !hostaddr(empirehost, &sin)) {
176 fprintf(stderr, "No empire host\n");
179 if ((sock = hostconnect(&sin)) < 0)
181 cname = getenv("COUNTRY");
182 pname = getenv("PLAYER");
188 if (!login(sock, pwd->pw_name, cname, pname, send_kill)) {
190 if (!login(sock, "win-empcl2.8", cname, pname, send_kill)) {
195 ioq_init(&server, 2048);
199 FD_SET(0, &savemask);
200 FD_SET(sock, &savemask);
202 (void)signal(SIGINT, intr);
204 (void)signal(SIGPIPE, SIG_IGN);
205 while (FD_ISSET(sock, &savemask)) {
207 n = select(sock + 1, &mask, (fd_set *)0, (fd_set *)0,
208 (struct timeval *)0);
210 if (!handleintr(sock))
215 if (errno == EINTR) {
218 FD_CLR(sock, &savemask);
221 if (FD_ISSET(0, &mask)) {
222 if (!termio(0, sock, auxout_fp)) {
223 if (retry++ >= RETRY) {
224 FD_CLR(0, &savemask);
230 if (FD_ISSET(sock, &mask)) {
231 if (!serverio(sock, &server))
232 FD_CLR(sock, &savemask);
234 servercmd(&server, auxout_fp);
242 security.nLength = sizeof(SECURITY_ATTRIBUTES);
243 security.lpSecurityDescriptor = NULL;
244 security.bInheritHandle = TRUE;
245 hStdIn = CreateFile("CONIN$",
246 GENERIC_READ | GENERIC_WRITE,
247 FILE_SHARE_READ | FILE_SHARE_WRITE,
248 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
250 if (hStdIn == INVALID_HANDLE_VALUE) {
251 printf("Error getting hStdIn.\n");
254 err = GetConsoleMode(hStdIn, &stdinmode);
256 printf("Error getting console mode.\n");
259 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
260 err = SetConsoleMode(hStdIn, stdinmode);
262 printf("Error setting console mode.\n");
267 FD_SET(sock, &readfds);
268 n = select(sock + 1, &readfds, (fd_set *) 0, (fd_set *) 0,
269 (struct timeval *)&tm);
271 if (errno == EINTR) {
272 errno = WSAGetLastError();
278 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
279 if (!termio(-1, sock, auxout_fp)) {
280 if (retry++ >= RETRY) {
286 FlushConsoleInputBuffer(hStdIn);
288 if (FD_ISSET(sock, &readfds)) {
289 if (!serverio(sock, &server))
292 servercmd(&server, auxout_fp);
301 return 0; /* Shut the compiler up */
313 signal(SIGINT, intr);