]> git.pond.sub.org Git - empserver/blob - src/client/main.c
client: Rewrite readline configuration
[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-2015
34  *     Tom Dickson-Hunt, 2010
35  *     Martin Haukeli, 2015
36  */
37
38 #include <config.h>
39
40 #include <stdlib.h>
41 #include <string.h>
42 #ifdef _WIN32
43 #include <windows.h>
44 #include <shlobj.h>
45 #include "sys/socket.h"
46 #else
47 #include <pwd.h>
48 #endif
49 #include <unistd.h>
50 #include "misc.h"
51 #include "version.h"
52
53 #ifdef _WIN32
54 #define getuid() 0
55 #define getpwuid(uid) ((void)(uid), w32_getpw())
56 #define sysdep_init() w32_sysdep_init()
57
58 struct passwd {
59     char *pw_name;
60     char *pw_dir;
61 };
62
63 static struct passwd *w32_getpw(void);
64 static void w32_sysdep_init(void);
65 #else  /* !_WIN32 */
66 #define sysdep_init() ((void)0)
67 #endif  /* !_WIN32 */
68
69 static void
70 print_usage(char *program_name)
71 {
72     printf("Usage: %s [OPTION]...[COUNTRY [PASSWORD]]\n"
73            "  -2 FILE         Append log of session to FILE\n"
74            "  -k              Kill connection\n"
75            "  -r              Restricted mode, no redirections\n"
76            "  -s [HOST:]PORT  Specify server HOST and PORT\n"
77            "  -u              Use UTF-8\n"
78 #ifdef HAVE_LIBREADLINE
79            "  -H              Save readline command history to file\n"
80 #endif /* HAVE_LIBREADLINE */
81            "  -h              display this help and exit\n"
82            "  -v              display version information and exit\n",
83            program_name);
84 }
85
86 int
87 main(int argc, char **argv)
88 {
89     int opt;
90     char *auxfname = NULL;
91     int use_history_file = 0;
92     int send_kill = 0;
93     char *host = NULL;
94     char *port = NULL;
95     int utf8 = 0;
96     char **ap;
97     char *country;
98     char *passwd;
99     char *uname;
100     char *udir;
101     char *colon;
102     int sock;
103     char *history_file;
104
105     while ((opt = getopt(argc, argv, "2:Hkrs:uhv")) != EOF) {
106         switch (opt) {
107         case '2':
108             auxfname = optarg;
109             break;
110 #ifdef HAVE_LIBREADLINE
111         case 'H':
112             use_history_file = 1;
113             break;
114 #endif /* HAVE_LIBREADLINE */
115         case 'k':
116             send_kill = 1;
117             break;
118         case 'r':
119             restricted = 1;
120             break;
121         case 's':
122             port = strdup(optarg);
123             colon = strrchr(port, ':');
124             if (colon) {
125                 *colon = 0;
126                 host = port;
127                 port = colon + 1;
128             }
129             break;
130         case 'u':
131             utf8 = eight_bit_clean = 1;
132             break;
133         case 'h':
134             print_usage(argv[0]);
135             exit(0);
136         case 'v':
137             printf("%s\n\n%s", version, legal);
138             exit(0);
139         default:
140             fprintf(stderr, "Try -h for help.\n");
141             exit(1);
142         }
143     }
144
145     ap = argv + optind;
146     if (*ap)
147         country = *ap++;
148     else
149         country = getenv("COUNTRY");
150     if (*ap)
151         passwd = *ap++;
152     else
153         passwd = getenv("PLAYER");
154     if (!port)
155         port = getenv("EMPIREPORT");
156     if (!port)
157         port = empireport;
158     if (!host)
159         host = getenv("EMPIREHOST");
160     if (!host)
161         host = empirehost;
162     uname = getenv("LOGNAME");
163     udir = getenv("HOME");
164     if (!uname || !udir) {
165         struct passwd *pwd;
166
167         pwd = getpwuid(getuid());
168         if (pwd == NULL) {
169             fprintf(stderr, "You don't exist.  Go away\n");
170             exit(1);
171         }
172         if (!uname)
173             uname = pwd->pw_name;
174         if (!udir)
175             udir = pwd->pw_dir;
176     }
177     if (*ap) {
178         fprintf(stderr, "%s: extra operand %s\n", argv[0], *ap);
179         fprintf(stderr, "Try -h for help.\n");
180         exit(1);
181     }
182
183     getsose();
184     if (auxfname && (auxfp = fopen(auxfname, "a")) == NULL) {
185         fprintf(stderr, "Unable to open %s for append\n", auxfname);
186         exit(1);
187     }
188
189     sysdep_init();
190
191     sock = tcp_connect(host, port);
192
193     if (use_history_file) {
194         /* FIXME don't truncate udir */
195         history_file = malloc(1024);
196         strncpy(history_file, udir, 1000);
197         strcat(history_file, "/.empire.history");
198     }
199
200     if (!login(sock, uname, country, passwd, send_kill, utf8))
201         exit(1);
202
203     if (play(sock, history_file) < 0)
204         exit(1);
205
206     return 0;
207 }
208
209 #ifdef _WIN32
210 /*
211  * Get Windows user name and directory
212  */
213 static struct passwd *
214 w32_getpw(void)
215 {
216     static char unamebuf[128];
217     static char udirbuf[MAX_PATH];
218     static struct passwd pwd;
219     DWORD unamesize;
220
221     unamesize = sizeof(unamebuf);
222     if (GetUserName(unamebuf, &unamesize)) {
223         pwd.pw_name = unamebuf;
224         if (unamesize == 0 || strlen(unamebuf) == 0)
225             pwd.pw_name = "nobody";
226     } else
227         pwd.pw_name = "nobody";
228     if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, udirbuf))
229         && strlen(udirbuf) == 0)
230         pwd.pw_dir = udirbuf;
231     return &pwd;
232 }
233
234 static void
235 w32_sysdep_init(void)
236 {
237     int err;
238
239     /*
240      * stdout is unbuffered under Windows if connected to a character
241      * device, and putchar() screws up when printing multibyte strings
242      * bytewise to an unbuffered stream.  Switch stdout to line-
243      * buffered mode.  Unfortunately, ISO C allows implementations to
244      * screw that up, and of course Windows does.  Manual flushing
245      * after each prompt is required.
246      */
247     setvbuf(stdout, NULL, _IOLBF, 4096);
248
249     err = w32_socket_init();
250     if (err != 0) {
251         fprintf(stderr, "WSAStartup Failed, error code %d\n", err);
252         exit(1);
253     }
254 }
255 #endif