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;
96 struct ioqueue server;
100 s_char *auxout_fname;
102 struct sockaddr_in sin;
110 err = WSAStartup(0x0101, &WsaData);
111 if (err == SOCKET_ERROR) {
112 printf("WSAStartup Failed\n");
119 memset(argv, 0, sizeof(argv));
120 saveargv(ac, av, argv);
123 for (i = j = 1; i < ac; ++i) {
125 if (strcmp(ptr, "-2") == 0) {
127 fprintf(stderr, "-2: Missing filename!\n");
130 auxout_fname = argv[i + 1];
133 } else if (strcmp(ptr, "-k") == 0) {
141 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
142 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
146 if (!hostport(getenv("EMPIREPORT"), &sin) &&
147 !hostport("empire", &sin) && !hostport(empireport, &sin)) {
148 fprintf(stderr, "No empire port\n");
151 if (!hostaddr(getenv("EMPIREHOST"), &sin) &&
152 !hostaddr(empirehost, &sin)) {
153 fprintf(stderr, "No empire host\n");
156 if ((sock = hostconnect(&sin)) < 0)
158 cname = getenv("COUNTRY");
161 pname = getenv("PLAYER");
164 uname = getenv("LOGNAME");
169 pwd = getpwuid(getuid());
171 fprintf(stderr, "You don't exist. Go away\n");
174 uname = pwd->pw_name;
178 unamesize = sizeof(unamebuf);
179 if (GetUserName(unamebuf, &unamesize)) {
181 if ((unamesize <= 0 ) || (strlen(uname) <= 0))
187 if (!login(sock, uname, cname, pname, send_kill)) {
191 ioq_init(&server, 2048);
195 FD_SET(0, &savemask);
196 FD_SET(sock, &savemask);
198 (void)signal(SIGINT, intr);
200 (void)signal(SIGPIPE, SIG_IGN);
201 while (FD_ISSET(sock, &savemask)) {
203 n = select(sock + 1, &mask, (fd_set *)0, (fd_set *)0,
204 (struct timeval *)0);
206 if (!handleintr(sock))
211 if (errno == EINTR) {
214 FD_CLR(sock, &savemask);
217 if (FD_ISSET(0, &mask)) {
218 if (!termio(0, sock, auxout_fp)) {
219 if (retry++ >= RETRY) {
220 FD_CLR(0, &savemask);
226 if (FD_ISSET(sock, &mask)) {
227 if (!serverio(sock, &server))
228 FD_CLR(sock, &savemask);
230 servercmd(&server, auxout_fp);
239 if (!_isatty(_fileno(stdin)))
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");
256 err = GetConsoleMode(hStdIn, &stdinmode);
258 printf("Error getting console mode.\n");
262 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
263 err = SetConsoleMode(hStdIn, stdinmode);
265 printf("Error setting console mode.\n");
273 FD_SET(sock, &readfds);
274 n = select(sock + 1, &readfds, (fd_set *) 0, (fd_set *) 0,
275 (struct timeval *)&tm);
277 if (!handleintr(sock))
282 if (errno == EINTR) {
283 errno = WSAGetLastError();
289 if (bRedirected == 1) {
290 if (!termio(0, sock, auxout_fp))
292 } else if (bRedirected == 0) {
293 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
294 termio(-1, sock, auxout_fp);
295 FlushConsoleInputBuffer(hStdIn);
298 if (FD_ISSET(sock, &readfds)) {
299 if (!serverio(sock, &server))
302 servercmd(&server, auxout_fp);
306 if (bRedirected == 0)
311 return 0; /* Shut the compiler up */
319 signal(SIGINT, intr);
322 signal(SIGINT, intr);