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