]> git.pond.sub.org Git - empserver/blob - src/client/main.c
Update copyright notice
[empserver] / src / client / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, 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            "  -s [HOST:]PORT  Specify server HOST and PORT\n"
73            "  -u              Use UTF-8\n"
74            "  -h              display this help and exit\n"
75            "  -v              display version information and exit\n",
76            program_name);
77 }
78
79 int
80 main(int argc, char **argv)
81 {
82     int opt;
83     char *auxfname = NULL;
84     int send_kill = 0;
85     char *host = NULL;
86     char *port = NULL;
87     int utf8 = 0;
88     char **ap;
89     char *country;
90     char *passwd;
91     char *uname;
92     char *colon;
93     int sock;
94
95     while ((opt = getopt(argc, argv, "2:ks:uhv")) != EOF) {
96         switch (opt) {
97         case '2':
98             auxfname = optarg;
99             break;
100         case 'k':
101             send_kill = 1;
102             break;
103         case 's':
104             port = strdup(optarg);
105             colon = strrchr(port, ':');
106             if (colon) {
107                 *colon = 0;
108                 host = port;
109                 port = colon + 1;
110             }
111             break;
112         case 'u':
113             utf8 = eight_bit_clean = 1;
114             break;
115         case 'h':
116             print_usage(argv[0]);
117             exit(0);
118         case 'v':
119             printf("%s\n\n%s", version, legal);
120             exit(0);
121         default:
122             fprintf(stderr, "Try -h for help.\n");
123             exit(1);
124         }
125     }
126
127     ap = argv + optind;
128     if (*ap)
129         country = *ap++;
130     else
131         country = getenv("COUNTRY");
132     if (*ap)
133         passwd = *ap++;
134     else
135         passwd = getenv("PLAYER");
136     if (!port)
137         port = getenv("EMPIREPORT");
138     if (!port)
139         port = empireport;
140     if (!host)
141         host = getenv("EMPIREHOST");
142     if (!host)
143         host = empirehost;
144     uname = getenv("LOGNAME");
145     if (uname == NULL) {
146         struct passwd *pwd;
147
148         pwd = getpwuid(getuid());
149         if (pwd == NULL) {
150             fprintf(stderr, "You don't exist.  Go away\n");
151             exit(1);
152         }
153         uname = pwd->pw_name;
154     }
155     if (*ap) {
156         fprintf(stderr, "%s: extra operand %s\n", argv[0], *ap);
157         fprintf(stderr, "Try -h for help.\n");
158         exit(1);
159     }
160
161     getsose();
162     if (auxfname && (auxfp = fopen(auxfname, "a")) == NULL) {
163         fprintf(stderr, "Unable to open %s for append\n", auxfname);
164         exit(1);
165     }
166
167     sysdep_init();
168
169     sock = tcp_connect(host, port);
170
171     if (!login(sock, uname, country, passwd, send_kill, utf8))
172         exit(1);
173
174     if (play(sock) < 0)
175         exit(1);
176
177     return 0;
178 }
179
180 #ifdef _WIN32
181 /*
182  * Get Windows user name
183  */
184 static struct passwd *
185 w32_getpw(void)
186 {
187     static char unamebuf[128];
188     static struct passwd pwd;
189     DWORD unamesize;
190
191     unamesize = sizeof(unamebuf);
192     if (GetUserName(unamebuf, &unamesize)) {
193         pwd.pw_name = unamebuf;
194         if (unamesize == 0 || strlen(unamebuf) == 0)
195             pwd.pw_name = "nobody";
196     } else
197         pwd.pw_name = "nobody";
198     return &pwd;
199 }
200
201 static void
202 w32_sysdep_init(void)
203 {
204     int err;
205
206     /*
207      * stdout is unbuffered under Windows if connected to a character
208      * device, and putchar() screws up when printing multibyte strings
209      * bytewise to an unbuffered stream.  Switch stdout to line-
210      * buffered mode.  Unfortunately, ISO C allows implementations to
211      * screw that up, and of course Windows does.  Manual flushing
212      * after each prompt is required.
213      */
214     setvbuf(stdout, NULL, _IOLBF, 4096);
215
216     err = w32_socket_init();
217     if (err != 0) {
218         fprintf(stderr, "WSAStartup Failed, error code %d\n", err);
219         exit(1);
220     }
221 }
222 #endif