(main): Rewrite argument parsing to use getopt(). New options -h and
-v. (print_usage): New.
This commit is contained in:
parent
b631cf944f
commit
46c0410e9c
3 changed files with 72 additions and 50 deletions
5
Make.mk
5
Make.mk
|
@ -261,7 +261,7 @@ info.html/%.html: info/%.t
|
||||||
$(server): $(filter src/server/% src/lib/as/% src/lib/commands/% src/lib/player/% src/lib/subs/% src/lib/update/%, $(obj)) $(empth_obj) $(libs) $(empth_lib)
|
$(server): $(filter src/server/% src/lib/as/% src/lib/commands/% src/lib/player/% src/lib/subs/% src/lib/update/%, $(obj)) $(empth_obj) $(libs) $(empth_lib)
|
||||||
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
|
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
|
||||||
|
|
||||||
$(client): $(filter src/client/%, $(obj))
|
$(client): $(filter src/client/%, $(obj)) src/lib/global/version.o
|
||||||
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
|
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
|
||||||
|
|
||||||
$(util): $(libs)
|
$(util): $(libs)
|
||||||
|
@ -330,7 +330,8 @@ dist-client: $(cli_distgen)
|
||||||
$(tarball) $(TARNAME)-client-$(VERSION) \
|
$(tarball) $(TARNAME)-client-$(VERSION) \
|
||||||
-C $(srcdir)/src/client \
|
-C $(srcdir)/src/client \
|
||||||
$(notdir $(filter src/client/%, $(src)) $(cli_distgen)) \
|
$(notdir $(filter src/client/%, $(src)) $(cli_distgen)) \
|
||||||
-C $(srcdir)/include proto.h \
|
-C $(srcdir)/include proto.h version.h \
|
||||||
|
-C $(srcdir)/src/lib/global version.c \
|
||||||
-C $(srcdir)/man empire.6 \
|
-C $(srcdir)/man empire.6 \
|
||||||
-C $(srcdir) COPYING INSTALL install-sh
|
-C $(srcdir) COPYING INSTALL install-sh
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ VPATH = @srcdir@
|
||||||
|
|
||||||
prog = empire$E
|
prog = empire$E
|
||||||
obj = expect.$O host.$O ioqueue.$O ipglob.$O login.$O main.$O queue.$O \
|
obj = expect.$O host.$O ioqueue.$O ipglob.$O login.$O main.$O queue.$O \
|
||||||
servcmd.$O serverio.$O tags.$O termio.$O termlib.$O
|
servcmd.$O serverio.$O tags.$O termio.$O termlib.$O version.$O
|
||||||
|
|
||||||
all: $(prog)
|
all: $(prog)
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ expect.$O: misc.h
|
||||||
host.$O: misc.h
|
host.$O: misc.h
|
||||||
ioqueue.$O: misc.h queue.h ioqueue.h
|
ioqueue.$O: misc.h queue.h ioqueue.h
|
||||||
login.$O: misc.h proto.h
|
login.$O: misc.h proto.h
|
||||||
main.$O: misc.h proto.h queue.h ioqueue.h tags.h
|
main.$O: misc.h proto.h queue.h ioqueue.h tags.h version.h
|
||||||
queue.$O: misc.h queue.h
|
queue.$O: misc.h queue.h
|
||||||
servcmd.$O: misc.h proto.h queue.h ioqueue.h tags.h
|
servcmd.$O: misc.h proto.h queue.h ioqueue.h tags.h
|
||||||
serverio.$O: misc.h queue.h ioqueue.h
|
serverio.$O: misc.h queue.h ioqueue.h
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
* Dave Pare, 1986
|
* Dave Pare, 1986
|
||||||
* Steve McClure, 1998
|
* Steve McClure, 1998
|
||||||
* Ron Koenderink, 2004-2005
|
* Ron Koenderink, 2004-2005
|
||||||
* Markus Armbruster, 2005
|
* Markus Armbruster, 2005-2007
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
@ -54,6 +54,7 @@
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "proto.h"
|
#include "proto.h"
|
||||||
#include "tags.h"
|
#include "tags.h"
|
||||||
|
#include "version.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
HANDLE hStdIn;
|
HANDLE hStdIn;
|
||||||
|
@ -68,9 +69,32 @@ static volatile sig_atomic_t interrupt;
|
||||||
static void intr(int sig);
|
static void intr(int sig);
|
||||||
static int handleintr(int);
|
static int handleintr(int);
|
||||||
|
|
||||||
int
|
static void
|
||||||
main(int ac, char **argv)
|
print_usage(char *program_name)
|
||||||
{
|
{
|
||||||
|
printf("Usage: %s [OPTION]...[COUNTRY [PASSWORD]]\n"
|
||||||
|
" -2 FILE Append log of session to FILE\n"
|
||||||
|
" -k Kill connection\n"
|
||||||
|
" -u Use UTF-8\n"
|
||||||
|
" -h display this help and exit\n"
|
||||||
|
" -v display version information and exit\n",
|
||||||
|
program_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int opt;
|
||||||
|
char *auxfname = NULL;
|
||||||
|
int send_kill = 0;
|
||||||
|
int utf8 = 0;
|
||||||
|
char **ap;
|
||||||
|
char *country;
|
||||||
|
char *passwd;
|
||||||
|
char *uname;
|
||||||
|
char *host;
|
||||||
|
char *port;
|
||||||
|
int sock;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WORD wVersionRequested;
|
WORD wVersionRequested;
|
||||||
WSADATA WsaData;
|
WSADATA WsaData;
|
||||||
|
@ -88,18 +112,8 @@ main(int ac, char **argv)
|
||||||
int retry = 0;
|
int retry = 0;
|
||||||
#endif
|
#endif
|
||||||
struct ioqueue server;
|
struct ioqueue server;
|
||||||
int i, j;
|
FILE *auxout_fp = NULL;
|
||||||
char *ptr;
|
|
||||||
char *auxout_fname;
|
|
||||||
FILE *auxout_fp;
|
|
||||||
int n;
|
int n;
|
||||||
char *cname;
|
|
||||||
char *pname;
|
|
||||||
char *uname;
|
|
||||||
char *host;
|
|
||||||
char *port;
|
|
||||||
int send_kill = 0;
|
|
||||||
int utf8 = 0;
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/*
|
/*
|
||||||
|
@ -121,47 +135,45 @@ main(int ac, char **argv)
|
||||||
FD_ZERO(&mask);
|
FD_ZERO(&mask);
|
||||||
FD_ZERO(&savemask);
|
FD_ZERO(&savemask);
|
||||||
#endif
|
#endif
|
||||||
auxout_fname = NULL;
|
|
||||||
auxout_fp = NULL;
|
while ((opt = getopt(argc, argv, "2:kuhv")) != EOF) {
|
||||||
for (i = j = 1; i < ac; ++i) {
|
switch (opt) {
|
||||||
ptr = argv[i];
|
case '2':
|
||||||
if (strcmp(ptr, "-2") == 0) {
|
auxfname = optarg;
|
||||||
if (i + 1 >= ac) {
|
break;
|
||||||
fprintf(stderr, "-2: Missing filename!\n");
|
case 'k':
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
auxout_fname = argv[i + 1];
|
|
||||||
++i;
|
|
||||||
continue;
|
|
||||||
} else if (strcmp(ptr, "-k") == 0) {
|
|
||||||
send_kill = 1;
|
send_kill = 1;
|
||||||
continue;
|
break;
|
||||||
} else if (strcmp(ptr, "-u") == 0) {
|
case 'u':
|
||||||
utf8 = eight_bit_clean = 1;
|
utf8 = eight_bit_clean = 1;
|
||||||
continue;
|
break;
|
||||||
|
case 'h':
|
||||||
|
print_usage(argv[0]);
|
||||||
|
exit(0);
|
||||||
|
case 'v':
|
||||||
|
printf("%s\n\n%s", version, legal);
|
||||||
|
exit(0);
|
||||||
|
default:
|
||||||
|
print_usage(argv[0]);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
argv[j] = argv[i];
|
|
||||||
++j;
|
|
||||||
}
|
}
|
||||||
ac = j;
|
|
||||||
if (auxout_fname && (auxout_fp = fopen(auxout_fname, "a")) == NULL) {
|
ap = argv + optind;
|
||||||
fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
|
if (*ap)
|
||||||
exit(1);
|
country = *ap++;
|
||||||
}
|
else
|
||||||
getsose();
|
country = getenv("COUNTRY");
|
||||||
|
if (*ap)
|
||||||
|
passwd = *ap++;
|
||||||
|
else
|
||||||
|
passwd = getenv("PLAYER");
|
||||||
port = getenv("EMPIREPORT");
|
port = getenv("EMPIREPORT");
|
||||||
if (!port)
|
if (!port)
|
||||||
port = empireport;
|
port = empireport;
|
||||||
host = getenv("EMPIREHOST");
|
host = getenv("EMPIREHOST");
|
||||||
if (!host)
|
if (!host)
|
||||||
host = empirehost;
|
host = empirehost;
|
||||||
sock = tcp_connect(host, port);
|
|
||||||
cname = getenv("COUNTRY");
|
|
||||||
if (ac > 1)
|
|
||||||
cname = argv[1];
|
|
||||||
pname = getenv("PLAYER");
|
|
||||||
if (ac > 2)
|
|
||||||
pname = argv[2];
|
|
||||||
uname = getenv("LOGNAME");
|
uname = getenv("LOGNAME");
|
||||||
if (uname == NULL) {
|
if (uname == NULL) {
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
@ -185,7 +197,16 @@ main(int ac, char **argv)
|
||||||
uname = "nobody";
|
uname = "nobody";
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
if (!login(sock, uname, cname, pname, send_kill, utf8)) {
|
|
||||||
|
getsose();
|
||||||
|
if (auxfname && (auxout_fp = fopen(auxfname, "a")) == NULL) {
|
||||||
|
fprintf(stderr, "Unable to open %s for append\n", auxfname);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
sock = tcp_connect(host, port);
|
||||||
|
|
||||||
|
if (!login(sock, uname, country, passwd, send_kill, utf8)) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue