]> git.pond.sub.org Git - empserver/blob - src/client/main.c
74ab4271150993b1f34046f4726df564458282a4
[empserver] / src / client / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  main.c: client main function
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1998
32  *     Ron Koenderink, 2004-2007
33  *     Markus Armbruster, 2005-2010
34  *     Tom Dickson-Hunt, 2010
35  */
36
37 #include <config.h>
38
39 #include <stdlib.h>
40 #include <string.h>
41 #ifdef _WIN32
42 #include <windows.h>
43 #include "sys/socket.h"
44 #else
45 #include <pwd.h>
46 #endif
47 #include <unistd.h>
48 #include "misc.h"
49 #include "version.h"
50
51 #ifdef _WIN32
52 #define getuid() 0
53 #define getpwuid(uid) ((void)(uid), w32_getpw())
54 #define sysdep_init() w32_sysdep_init()
55
56 struct passwd {
57     char *pw_name;
58 };
59
60 static struct passwd *w32_getpw(void);
61 static void w32_sysdep_init(void);
62 #else  /* !_WIN32 */
63 #define sysdep_init() ((void)0)
64 #endif  /* !_WIN32 */
65
66 static void
67 print_usage(char *program_name)
68 {
69     printf("Usage: %s [OPTION]...[COUNTRY [PASSWORD]]\n"
70            "  -2 FILE         Append log of session to FILE\n"
71            "  -k              Kill connection\n"
72            "  -r              Restricted mode, no redirections\n"
73            "  -s [HOST:]PORT  Specify server HOST and PORT\n"
74            "  -u              Use UTF-8\n"
75            "  -h              display this help and exit\n"
76            "  -v              display version information and exit\n",
77            program_name);
78 }
79
80 int
81 main(int argc, char **argv)
82 {
83     int opt;
84     char *auxfname = NULL;
85     int send_kill = 0;
86     char *host = NULL;
87     char *port = NULL;
88     int utf8 = 0;
89     char **ap;
90     char *country;
91     char *passwd;
92     char *uname;
93     char *colon;
94     int sock;
95
96     while ((opt = getopt(argc, argv, "2:krs:uhv")) != EOF) {
97         switch (opt) {
98         case '2':
99             auxfname = optarg;
100             break;
101         case 'k':
102             send_kill = 1;
103             break;
104         case 'r':
105             restricted = 1;
106             break;
107         case 's':
108             port = strdup(optarg);
109             colon = strrchr(port, ':');
110             if (colon) {
111                 *colon = 0;
112                 host = port;
113                 port = colon + 1;
114             }
115             break;
116         case 'u':
117             utf8 = eight_bit_clean = 1;
118             break;
119         case 'h':
120             print_usage(argv[0]);
121             exit(0);
122         case 'v':
123             printf("%s\n\n%s", version, legal);
124             exit(0);
125         default:
126             fprintf(stderr, "Try -h for help.\n");
127             exit(1);
128         }
129     }
130
131     ap = argv + optind;
132     if (*ap)
133         country = *ap++;
134     else
135         country = getenv("COUNTRY");
136     if (*ap)
137         passwd = *ap++;
138     else
139         passwd = getenv("PLAYER");
140     if (!port)
141         port = getenv("EMPIREPORT");
142     if (!port)
143         port = empireport;
144     if (!host)
145         host = getenv("EMPIREHOST");
146     if (!host)
147         host = empirehost;
148     uname = getenv("LOGNAME");
149     if (uname == NULL) {
150         struct passwd *pwd;
151
152         pwd = getpwuid(getuid());
153         if (pwd == NULL) {
154             fprintf(stderr, "You don't exist.  Go away\n");
155             exit(1);
156         }
157         uname = pwd->pw_name;
158     }
159     if (*ap) {
160         fprintf(stderr, "%s: extra operand %s\n", argv[0], *ap);
161         fprintf(stderr, "Try -h for help.\n");
162         exit(1);
163     }
164
165     getsose();
166     if (auxfname && (auxfp = fopen(auxfname, "a")) == NULL) {
167         fprintf(stderr, "Unable to open %s for append\n", auxfname);
168         exit(1);
169     }
170
171     sysdep_init();
172
173     sock = tcp_connect(host, port);
174
175     if (!login(sock, uname, country, passwd, send_kill, utf8))
176         exit(1);
177
178     if (play(sock) < 0)
179         exit(1);
180
181     return 0;
182 }
183
184 #ifdef _WIN32
185 /*
186  * Get Windows user name
187  */
188 static struct passwd *
189 w32_getpw(void)
190 {
191     static char unamebuf[128];
192     static struct passwd pwd;
193     DWORD unamesize;
194
195     unamesize = sizeof(unamebuf);
196     if (GetUserName(unamebuf, &unamesize)) {
197         pwd.pw_name = unamebuf;
198         if (unamesize == 0 || strlen(unamebuf) == 0)
199             pwd.pw_name = "nobody";
200     } else
201         pwd.pw_name = "nobody";
202     return &pwd;
203 }
204
205 static void
206 w32_sysdep_init(void)
207 {
208     int err;
209
210     /*
211      * stdout is unbuffered under Windows if connected to a character
212      * device, and putchar() screws up when printing multibyte strings
213      * bytewise to an unbuffered stream.  Switch stdout to line-
214      * buffered mode.  Unfortunately, ISO C allows implementations to
215      * screw that up, and of course Windows does.  Manual flushing
216      * after each prompt is required.
217      */
218     setvbuf(stdout, NULL, _IOLBF, 4096);
219
220     err = w32_socket_init();
221     if (err != 0) {
222         fprintf(stderr, "WSAStartup Failed, error code %d\n", err);
223         exit(1);
224     }
225 }
226 #endif