]> git.pond.sub.org Git - empserver/blob - src/client/main.c
Revamp client's Windows POSIX compatibility code
[empserver] / src / client / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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-2005
34  *     Markus Armbruster, 2005-2009
35  */
36
37 #include <config.h>
38
39 #include <stdlib.h>
40 #ifndef _WIN32
41 #include <pwd.h>
42 #include <unistd.h>
43 #endif
44 #include "misc.h"
45 #include "version.h"
46
47 #ifdef _WIN32
48 #define getuid() 0
49 #define getpwuid(uid) ((void)(uid), w32_getpw())
50 #define sysdep_init() w32_sysdep_init()
51 #else
52 #define sysdep_init() ((void)0)
53 #endif
54
55 static void
56 print_usage(char *program_name)
57 {
58     printf("Usage: %s [OPTION]...[COUNTRY [PASSWORD]]\n"
59            "  -2 FILE         Append log of session to FILE\n"
60            "  -k              Kill connection\n"
61            "  -u              Use UTF-8\n"
62            "  -h              display this help and exit\n"
63            "  -v              display version information and exit\n",
64            program_name);
65 }
66
67 int
68 main(int argc, char **argv)
69 {
70     int opt;
71     char *auxfname = NULL;
72     int send_kill = 0;
73     int utf8 = 0;
74     char **ap;
75     char *country;
76     char *passwd;
77     char *uname;
78     char *host;
79     char *port;
80     int sock;
81
82     while ((opt = getopt(argc, argv, "2:kuhv")) != EOF) {
83         switch (opt) {
84         case '2':
85             auxfname = optarg;
86             break;
87         case 'k':
88             send_kill = 1;
89             break;
90         case 'u':
91             utf8 = eight_bit_clean = 1;
92             break;
93         case 'h':
94             print_usage(argv[0]);
95             exit(0);
96         case 'v':
97             printf("%s\n\n%s", version, legal);
98             exit(0);
99         default:
100             print_usage(argv[0]);
101             exit(1);
102         }
103     }
104
105     ap = argv + optind;
106     if (*ap)
107         country = *ap++;
108     else
109         country = getenv("COUNTRY");
110     if (*ap)
111         passwd = *ap++;
112     else
113         passwd = getenv("PLAYER");
114     port = getenv("EMPIREPORT");
115     if (!port)
116         port = empireport;
117     host = getenv("EMPIREHOST");
118     if (!host)
119         host = empirehost;
120     uname = getenv("LOGNAME");
121     if (uname == NULL) {
122         struct passwd *pwd;
123
124         pwd = getpwuid(getuid());
125         if (pwd == NULL) {
126             fprintf(stderr, "You don't exist.  Go away\n");
127             exit(1);
128         }
129         uname = pwd->pw_name;
130     }
131
132     getsose();
133     if (auxfname && (auxfp = fopen(auxfname, "a")) == NULL) {
134         fprintf(stderr, "Unable to open %s for append\n", auxfname);
135         exit(1);
136     }
137
138     sysdep_init();
139
140     sock = tcp_connect(host, port);
141
142     if (!login(sock, uname, country, passwd, send_kill, utf8))
143         exit(1);
144
145     if (play(sock) < 0)
146         exit(1);
147
148     return 0;
149 }