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