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:
52 #include <sys/socket.h>
53 #include <netinet/in.h>
95 INPUT_RECORD InpBuffer[10];
98 SECURITY_ATTRIBUTES security;
101 extern s_char *getenv();
102 extern s_char empireport[];
103 extern s_char empirehost[];
106 struct ioqueue server;
110 s_char *auxout_fname;
116 struct sockaddr_in sin;
124 err = WSAStartup(0x0101, &WsaData);
125 if (err == SOCKET_ERROR) {
126 printf("WSAStartup Failed\n");
133 memset(argv, 0, sizeof(argv));
134 saveargv(ac, av, argv);
137 for (i = j = 1; i < ac; ++i) {
139 if (strcmp(ptr, "-2") == 0) {
141 fprintf(stderr, "-2: Missing filename!\n");
144 auxout_fname = argv[i + 1];
147 } else if (strcmp(ptr, "-k") == 0) {
155 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
156 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
160 pwd = getpwuid(getuid());
162 fprintf(stderr, "You don't exist. Go away\n");
167 if (!hostport(getenv("EMPIREPORT"), &sin) &&
168 !hostport("empire", &sin) && !hostport(empireport, &sin)) {
169 fprintf(stderr, "No empire port\n");
172 if (!hostaddr(getenv("EMPIREHOST"), &sin) &&
173 !hostaddr(empirehost, &sin)) {
174 fprintf(stderr, "No empire host\n");
177 if ((sock = hostconnect(&sin)) < 0)
179 cname = getenv("COUNTRY");
180 pname = getenv("PLAYER");
186 if (!login(sock, pwd->pw_name, cname, pname, send_kill)) {
188 if (!login(sock, "win-empcl2.8", cname, pname, send_kill)) {
193 ioq_init(&server, 2048);
197 FD_SET(0, &savemask);
198 FD_SET(sock, &savemask);
200 (void)signal(SIGINT, intr);
202 (void)signal(SIGPIPE, SIG_IGN);
203 while (FD_ISSET(sock, &savemask)) {
205 n = select(sock + 1, &mask, (fd_set *)0, (fd_set *)0,
206 (struct timeval *)0);
208 if (!handleintr(sock))
213 if (errno == EINTR) {
216 FD_CLR(sock, &savemask);
219 if (FD_ISSET(0, &mask)) {
220 if (!termio(0, sock, auxout_fp)) {
221 if (retry++ >= RETRY) {
222 FD_CLR(0, &savemask);
228 if (FD_ISSET(sock, &mask)) {
229 if (!serverio(sock, &server))
230 FD_CLR(sock, &savemask);
232 servercmd(&server, auxout_fp);
240 security.nLength = sizeof(SECURITY_ATTRIBUTES);
241 security.lpSecurityDescriptor = NULL;
242 security.bInheritHandle = TRUE;
243 hStdIn = CreateFile("CONIN$",
244 GENERIC_READ | GENERIC_WRITE,
245 FILE_SHARE_READ | FILE_SHARE_WRITE,
246 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
248 if (hStdIn == INVALID_HANDLE_VALUE) {
249 printf("Error getting hStdIn.\n");
252 err = GetConsoleMode(hStdIn, &stdinmode);
254 printf("Error getting console mode.\n");
257 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
258 err = SetConsoleMode(hStdIn, stdinmode);
260 printf("Error setting console mode.\n");
265 FD_SET(sock, &readfds);
266 n = select(sock + 1, &readfds, (fd_set *) 0, (fd_set *) 0,
267 (struct timeval *)&tm);
269 if (errno == EINTR) {
270 errno = WSAGetLastError();
276 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
277 if (!termio(-1, sock, auxout_fp)) {
278 if (retry++ >= RETRY) {
284 FlushConsoleInputBuffer(hStdIn);
286 if (FD_ISSET(sock, &readfds)) {
287 if (!serverio(sock, &server))
290 servercmd(&server, auxout_fp);
299 return 0; /* Shut the compiler up */
311 signal(SIGINT, intr);