2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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>
77 static void intr(int sig);
81 main(int ac, char **av)
89 SECURITY_ATTRIBUTES security;
97 struct ioqueue server;
103 struct sockaddr_in sin;
112 err = WSAStartup(0x0101, &WsaData);
113 if (err == SOCKET_ERROR) {
114 printf("WSAStartup Failed\n");
121 memset(argv, 0, sizeof(argv));
122 saveargv(ac, av, argv);
125 for (i = j = 1; i < ac; ++i) {
127 if (strcmp(ptr, "-2") == 0) {
129 fprintf(stderr, "-2: Missing filename!\n");
132 auxout_fname = argv[i + 1];
135 } else if (strcmp(ptr, "-k") == 0) {
138 } else if (strcmp(ptr, "-u") == 0) {
139 utf8 = eight_bit_clean = 1;
146 if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
147 fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
151 if (!hostport(getenv("EMPIREPORT"), &sin) &&
152 !hostport("empire", &sin) && !hostport(empireport, &sin)) {
153 fprintf(stderr, "No empire port\n");
156 if (!hostaddr(getenv("EMPIREHOST"), &sin) &&
157 !hostaddr(empirehost, &sin)) {
158 fprintf(stderr, "No empire host\n");
161 if ((sock = hostconnect(&sin)) < 0)
163 cname = getenv("COUNTRY");
166 pname = getenv("PLAYER");
169 uname = getenv("LOGNAME");
174 pwd = getpwuid(getuid());
176 fprintf(stderr, "You don't exist. Go away\n");
179 uname = pwd->pw_name;
183 unamesize = sizeof(unamebuf);
184 if (GetUserName(unamebuf, &unamesize)) {
186 if ((unamesize <= 0 ) || (strlen(uname) <= 0))
192 if (!login(sock, uname, cname, pname, send_kill, utf8)) {
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, NULL, NULL, NULL);
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);
243 if (!_isatty(_fileno(stdin)))
246 security.nLength = sizeof(SECURITY_ATTRIBUTES);
247 security.lpSecurityDescriptor = NULL;
248 security.bInheritHandle = TRUE;
249 hStdIn = CreateFile("CONIN$",
250 GENERIC_READ | GENERIC_WRITE,
251 FILE_SHARE_READ | FILE_SHARE_WRITE,
252 &security, OPEN_EXISTING, (DWORD) NULL, NULL);
254 if (hStdIn == INVALID_HANDLE_VALUE) {
255 printf("Error getting hStdIn.\n");
260 err = GetConsoleMode(hStdIn, &stdinmode);
262 printf("Error getting console mode.\n");
266 stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
267 err = SetConsoleMode(hStdIn, stdinmode);
269 printf("Error setting console mode.\n");
277 FD_SET(sock, &readfds);
278 n = select(sock + 1, &readfds, NULL, NULL, &tm);
280 if (!handleintr(sock))
285 if (errno == EINTR) {
286 errno = WSAGetLastError();
292 if (bRedirected == 1) {
293 if (!termio(0, sock, auxout_fp))
295 } else if (bRedirected == 0) {
296 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
297 termio(-1, sock, auxout_fp);
298 FlushConsoleInputBuffer(hStdIn);
301 if (FD_ISSET(sock, &readfds)) {
302 if (!serverio(sock, &server))
305 servercmd(&server, auxout_fp);
309 if (bRedirected == 0)
314 return 0; /* Shut the compiler up */
322 signal(SIGINT, intr);
325 signal(SIGINT, intr);