2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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:
48 #include <sys/types.h>
56 #include <sys/socket.h>
58 #include <netinet/in.h>
76 static void intr(int sig);
80 main(int ac, s_char **av)
88 SECURITY_ATTRIBUTES security;
91 struct ioqueue server;
101 struct sockaddr_in sin;
109 err = WSAStartup(0x0101, &WsaData);
110 if (err == SOCKET_ERROR) {
111 printf("WSAStartup Failed\n");
118 memset(argv, 0, sizeof(argv));
119 saveargv(ac, av, argv);
122 for (i = j = 1; i < ac; ++i) {
124 if (strcmp(ptr, "-2") == 0) {
126 fprintf(stderr, "-2: Missing filename!\n");
129 auxout_fname = argv[i + 1];
132 } else if (strcmp(ptr, "-k") == 0) {
140 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
141 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
145 pwd = getpwuid(getuid());
147 fprintf(stderr, "You don't exist. Go away\n");
152 if (!hostport(getenv("EMPIREPORT"), &sin) &&
153 !hostport("empire", &sin) && !hostport(empireport, &sin)) {
154 fprintf(stderr, "No empire port\n");
157 if (!hostaddr(getenv("EMPIREHOST"), &sin) &&
158 !hostaddr(empirehost, &sin)) {
159 fprintf(stderr, "No empire host\n");
162 if ((sock = hostconnect(&sin)) < 0)
164 cname = getenv("COUNTRY");
165 pname = getenv("PLAYER");
171 if (!login(sock, pwd->pw_name, cname, pname, send_kill)) {
173 if (!login(sock, "nobody", cname, pname, send_kill)) {
178 ioq_init(&server, 2048);
182 FD_SET(0, &savemask);
183 FD_SET(sock, &savemask);
185 (void)signal(SIGINT, intr);
187 (void)signal(SIGPIPE, SIG_IGN);
188 while (FD_ISSET(sock, &savemask)) {
190 n = select(sock + 1, &mask, (fd_set *)0, (fd_set *)0,
191 (struct timeval *)0);
193 if (!handleintr(sock))
198 if (errno == EINTR) {
201 FD_CLR(sock, &savemask);
204 if (FD_ISSET(0, &mask)) {
205 if (!termio(0, sock, auxout_fp)) {
206 if (retry++ >= RETRY) {
207 FD_CLR(0, &savemask);
213 if (FD_ISSET(sock, &mask)) {
214 if (!serverio(sock, &server))
215 FD_CLR(sock, &savemask);
217 servercmd(&server, auxout_fp);
225 security.nLength = sizeof(SECURITY_ATTRIBUTES);
226 security.lpSecurityDescriptor = NULL;
227 security.bInheritHandle = TRUE;
228 hStdIn = CreateFile("CONIN$",
229 GENERIC_READ | GENERIC_WRITE,
230 FILE_SHARE_READ | FILE_SHARE_WRITE,
231 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
233 if (hStdIn == INVALID_HANDLE_VALUE) {
234 printf("Error getting hStdIn.\n");
237 err = GetConsoleMode(hStdIn, &stdinmode);
239 printf("Error getting console mode.\n");
242 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
243 err = SetConsoleMode(hStdIn, stdinmode);
245 printf("Error setting console mode.\n");
250 FD_SET(sock, &readfds);
251 n = select(sock + 1, &readfds, (fd_set *) 0, (fd_set *) 0,
252 (struct timeval *)&tm);
254 if (!handleintr(sock))
259 if (errno == EINTR) {
260 errno = WSAGetLastError();
266 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
267 if (!termio(-1, sock, auxout_fp)) {
268 if (retry++ >= RETRY) {
274 FlushConsoleInputBuffer(hStdIn);
276 if (FD_ISSET(sock, &readfds)) {
277 if (!serverio(sock, &server))
280 servercmd(&server, auxout_fp);
289 return 0; /* Shut the compiler up */
297 signal(SIGINT, intr);
300 signal(SIGINT, intr);