]> git.pond.sub.org Git - empserver/blob - src/client/main.c
Client UTF-8 support.
[empserver] / src / client / main.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  main.c: client main function
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1998
33  */
34
35 #ifdef _NTSDK
36 #undef _NTSDK
37 #endif
38
39 #include "misc.h"
40 #include "proto.h"
41 #include "queue.h"
42 #include "ioqueue.h"
43 #include "tags.h"
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #ifndef _WIN32
50 #include <pwd.h>
51 #endif
52 #include <signal.h>
53 #include <errno.h>
54 #include <time.h>
55 #ifndef _WIN32
56 #include <sys/socket.h>
57 #include <sys/time.h>
58 #include <netinet/in.h>
59 #include <unistd.h>
60 #else
61 #include <windows.h>
62 #include <winsock.h>
63 #include <conio.h>
64 #include <io.h>
65 #endif
66
67 #ifdef _WIN32
68 HANDLE hStdIn;
69 #endif
70
71 #define RETRY   3
72
73 int eight_bit_clean;
74 int interrupt;
75 int sock;
76
77 static void intr(int sig);
78
79
80 int
81 main(int ac, char **av)
82 {
83 #ifdef _WIN32
84     WSADATA WsaData;
85     int err;
86     fd_set readfds;
87     struct timeval tm;
88     DWORD stdinmode;
89     SECURITY_ATTRIBUTES security;
90     int bRedirected = 0;
91     char unamebuf[128];
92 #else
93     fd_set mask;
94     fd_set savemask;
95     int retry = 0;
96 #endif
97     struct ioqueue server;
98     char *argv[128];
99     int i, j;
100     char *ptr;
101     char *auxout_fname;
102     FILE *auxout_fp;
103     struct sockaddr_in sin;
104     int n;
105     char *cname;
106     char *pname;
107     char *uname;
108     int send_kill = 0;
109     int utf8 = 0;
110
111 #ifdef _WIN32
112     err = WSAStartup(0x0101, &WsaData);
113     if (err == SOCKET_ERROR) {
114         printf("WSAStartup Failed\n");
115         return FALSE;
116     }
117 #else
118     FD_ZERO(&mask);
119     FD_ZERO(&savemask);
120 #endif
121     memset(argv, 0, sizeof(argv));
122     saveargv(ac, av, argv);
123     auxout_fname = NULL;
124     auxout_fp = NULL;
125     for (i = j = 1; i < ac; ++i) {
126         ptr = argv[i];
127         if (strcmp(ptr, "-2") == 0) {
128             if (i + 1 >= ac) {
129                 fprintf(stderr, "-2: Missing filename!\n");
130                 exit(1);
131             }
132             auxout_fname = argv[i + 1];
133             ++i;
134             continue;
135         } else if (strcmp(ptr, "-k") == 0) {
136             send_kill = 1;
137             continue;
138         } else if (strcmp(ptr, "-u") == 0) {
139             utf8 = eight_bit_clean = 1;
140             continue;
141         }
142         argv[j] = argv[i];
143         ++j;
144     }
145     ac = j;
146     if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
147         fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
148         exit(1);
149     }
150     getsose();
151     if (!hostport(getenv("EMPIREPORT"), &sin) &&
152         !hostport("empire", &sin) && !hostport(empireport, &sin)) {
153         fprintf(stderr, "No empire port\n");
154         exit(1);
155     }
156     if (!hostaddr(getenv("EMPIREHOST"), &sin) &&
157         !hostaddr(empirehost, &sin)) {
158         fprintf(stderr, "No empire host\n");
159         exit(1);
160     }
161     if ((sock = hostconnect(&sin)) < 0)
162         exit(1);
163     cname = getenv("COUNTRY");
164     if (ac > 1)
165         cname = argv[1];
166     pname = getenv("PLAYER");
167     if (ac > 2)
168         pname = argv[2];
169     uname = getenv("LOGNAME");
170     if (uname == NULL) {
171 #ifndef _WIN32
172         struct passwd *pwd;
173
174         pwd = getpwuid(getuid());
175         if (pwd == NULL) {
176             fprintf(stderr, "You don't exist.  Go away\n");
177             exit(1);
178         }
179         uname = pwd->pw_name;
180 #else
181         DWORD unamesize;
182
183         unamesize = sizeof(unamebuf);
184         if (GetUserName(unamebuf, &unamesize)) {
185             uname = unamebuf;
186             if ((unamesize <= 0 ) || (strlen(uname) <= 0))
187                 uname = "nobody";
188         } else
189             uname = "nobody";
190 #endif
191     }
192     if (!login(sock, uname, cname, pname, send_kill, utf8)) {
193         close(sock);
194         exit(1);
195     }
196     ioq_init(&server, 2048);
197     io_init();
198 #ifndef _WIN32
199     FD_ZERO(&mask);
200     FD_SET(0, &savemask);
201     FD_SET(sock, &savemask);
202 #endif
203     (void)signal(SIGINT, intr);
204 #ifndef _WIN32
205     (void)signal(SIGPIPE, SIG_IGN);
206     while (FD_ISSET(sock, &savemask)) {
207         mask = savemask;
208         n = select(sock + 1, &mask, NULL, NULL, NULL);
209         if (interrupt) {
210             if (!handleintr(sock))
211                 break;
212             errno = 0;
213         }
214         if (n <= 0) {
215             if (errno == EINTR) {
216                 perror("select");
217                 (void)close(sock);
218                 FD_CLR(sock, &savemask);
219             }
220         } else {
221             if (FD_ISSET(0, &mask)) {
222                 if (!termio(0, sock, auxout_fp)) {
223                     if (retry++ >= RETRY) {
224                         FD_CLR(0, &savemask);
225                     }
226                 } else {
227                     retry = 0;
228                 }
229             }
230             if (FD_ISSET(sock, &mask)) {
231                 if (!serverio(sock, &server))
232                     FD_CLR(sock, &savemask);
233                 else
234                     servercmd(&server, auxout_fp);
235             }
236         }
237     }
238 #else
239     bRedirected = 0;
240     tm.tv_sec = 0;
241     tm.tv_usec = 1000;
242
243     if (!_isatty(_fileno(stdin)))
244         bRedirected = 1;
245     else {
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);
253         
254         if (hStdIn == INVALID_HANDLE_VALUE) {
255             printf("Error getting hStdIn.\n");
256             fflush(stdout);
257             exit(-3);
258         }
259         
260         err = GetConsoleMode(hStdIn, &stdinmode);
261         if (!err) {
262             printf("Error getting console mode.\n");
263             fflush(stdout);
264             exit(-4);
265         } else {
266             stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
267             err = SetConsoleMode(hStdIn, stdinmode);
268             if (!err) {
269                 printf("Error setting console mode.\n");
270                 fflush(stdout);
271                 exit(-5);
272             }
273         }
274     }
275     while (1) {
276         FD_ZERO(&readfds);
277         FD_SET(sock, &readfds);
278         n = select(sock + 1, &readfds, NULL, NULL, &tm);
279         if (interrupt) {
280             if (!handleintr(sock))
281                 break;
282             errno = 0;
283         }
284         if (n < 0) {
285             if (errno == EINTR) {
286                 errno = WSAGetLastError();
287                 perror("select");
288                 (void)close(sock);
289                 break;
290             }
291         } else {
292             if (bRedirected == 1) {
293                 if (!termio(0, sock, auxout_fp))
294                     bRedirected = -1;
295             } else if (bRedirected == 0) {
296                 if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
297                     termio(-1, sock, auxout_fp);
298                     FlushConsoleInputBuffer(hStdIn);
299                 }
300             }
301             if (FD_ISSET(sock, &readfds)) {
302                 if (!serverio(sock, &server))
303                     break;
304                 else
305                     servercmd(&server, auxout_fp);
306             }
307         }
308     }
309     if (bRedirected == 0)
310         CloseHandle(hStdIn);
311 #endif
312     ioq_drain(&server);
313     (void)close(sock);
314     return 0;                   /* Shut the compiler up */
315 }
316
317 static void
318 intr(int sig)
319 {
320     interrupt++;
321 #ifdef _WIN32
322     signal(SIGINT, intr);
323 #endif
324 #ifdef hpux
325     signal(SIGINT, intr);
326 #endif
327 }