(main, termio): Add the ability to set the username.

Get it from environment variable LOGNAME if set,
else get the user name from the system.

(main) [_DWIN32]: Add stdin redirection ability to the Win32 port.
The client reads until stdin redirection empty and then terminates.
This commit is contained in:
Ron Koenderink 2004-10-19 01:11:31 +00:00
parent 39facfbf53
commit 9faba52ba4
3 changed files with 80 additions and 49 deletions

View file

@ -51,6 +51,8 @@ you should have the right default already.)
setenv COUNTRY country-name (optional) setenv COUNTRY country-name (optional)
.PP .PP
setenv PLAYER representative-name (optional) setenv PLAYER representative-name (optional)
.PP
setenv LOGNAME username (optional)
.SH AUTHORS .SH AUTHORS
.nf .nf
Primary Author is Dave Pare (mr-frog@scam.berkeley.edu) Primary Author is Dave Pare (mr-frog@scam.berkeley.edu)

View file

@ -86,23 +86,24 @@ main(int ac, s_char **av)
struct timeval tm; struct timeval tm;
DWORD stdinmode; DWORD stdinmode;
SECURITY_ATTRIBUTES security; SECURITY_ATTRIBUTES security;
#endif int bRedirected = 0;
char unamebuf[128];
#else
fd_set mask; fd_set mask;
fd_set savemask;
int retry = 0;
#endif
struct ioqueue server; struct ioqueue server;
s_char *argv[128]; s_char *argv[128];
int i, j; int i, j;
s_char *ptr; s_char *ptr;
s_char *auxout_fname; s_char *auxout_fname;
FILE *auxout_fp; FILE *auxout_fp;
#ifndef _WIN32
fd_set savemask;
struct passwd *pwd;
#endif
struct sockaddr_in sin; struct sockaddr_in sin;
int n; int n;
s_char *cname; s_char *cname;
s_char *pname; s_char *pname;
int retry = 0; s_char *uname;
int send_kill = 0; int send_kill = 0;
#ifdef _WIN32 #ifdef _WIN32
@ -141,13 +142,6 @@ main(int ac, s_char **av)
fprintf(stderr, "Unable to open %s for append\n", auxout_fname); fprintf(stderr, "Unable to open %s for append\n", auxout_fname);
exit(1); exit(1);
} }
#ifndef _WIN32
pwd = getpwuid(getuid());
if (pwd == NULL) {
fprintf(stderr, "You don't exist. Go away\n");
exit(1);
}
#endif
getsose(); getsose();
if (!hostport(getenv("EMPIREPORT"), &sin) && if (!hostport(getenv("EMPIREPORT"), &sin) &&
!hostport("empire", &sin) && !hostport(empireport, &sin)) { !hostport("empire", &sin) && !hostport(empireport, &sin)) {
@ -162,23 +156,42 @@ main(int ac, s_char **av)
if ((sock = hostconnect(&sin)) < 0) if ((sock = hostconnect(&sin)) < 0)
exit(1); exit(1);
cname = getenv("COUNTRY"); cname = getenv("COUNTRY");
pname = getenv("PLAYER");
if (ac > 1) if (ac > 1)
cname = argv[1]; cname = argv[1];
pname = getenv("PLAYER");
if (ac > 2) if (ac > 2)
pname = argv[2]; pname = argv[2];
uname = getenv("LOGNAME");
if (uname == NULL) {
#ifndef _WIN32 #ifndef _WIN32
if (!login(sock, pwd->pw_name, cname, pname, send_kill)) { struct passwd *pwd;
pwd = getpwuid(getuid());
if (pwd == NULL) {
fprintf(stderr, "You don't exist. Go away\n");
exit(1);
}
uname = pwd->pw_name;
#else #else
if (!login(sock, "nobody", cname, pname, send_kill)) { DWORD unamesize;
unamesize = sizeof(unamebuf);
if (GetUserName(unamebuf, &unamesize)) {
uname = unamebuf;
if ((unamesize <= 0 ) || (strlen(uname) <= 0))
uname = "nobody";
} else
uname = "nobody";
#endif #endif
}
if (!login(sock, uname, cname, pname, send_kill)) {
close(sock); close(sock);
exit(1); exit(1);
} }
ioq_init(&server, 2048); ioq_init(&server, 2048);
io_init(); io_init();
FD_ZERO(&mask);
#ifndef _WIN32 #ifndef _WIN32
FD_ZERO(&mask);
FD_SET(0, &savemask); FD_SET(0, &savemask);
FD_SET(sock, &savemask); FD_SET(sock, &savemask);
#endif #endif
@ -219,31 +232,41 @@ main(int ac, s_char **av)
} }
} }
#else #else
bRedirected = 0;
tm.tv_sec = 0; tm.tv_sec = 0;
tm.tv_usec = 1000; tm.tv_usec = 1000;
security.nLength = sizeof(SECURITY_ATTRIBUTES); if (!_isatty(_fileno(stdin)))
security.lpSecurityDescriptor = NULL; bRedirected = 1;
security.bInheritHandle = TRUE; else {
hStdIn = CreateFile("CONIN$", security.nLength = sizeof(SECURITY_ATTRIBUTES);
GENERIC_READ | GENERIC_WRITE, security.lpSecurityDescriptor = NULL;
FILE_SHARE_READ | FILE_SHARE_WRITE, security.bInheritHandle = TRUE;
&security, OPEN_EXISTING, (DWORD) NULL, NULL); hStdIn = CreateFile("CONIN$",
GENERIC_READ | GENERIC_WRITE,
if (hStdIn == INVALID_HANDLE_VALUE) { FILE_SHARE_READ | FILE_SHARE_WRITE,
printf("Error getting hStdIn.\n"); &security, OPEN_EXISTING, (DWORD) NULL, NULL);
fflush(stdout);
} if (hStdIn == INVALID_HANDLE_VALUE) {
err = GetConsoleMode(hStdIn, &stdinmode); printf("Error getting hStdIn.\n");
if (!err) { fflush(stdout);
printf("Error getting console mode.\n"); exit(-3);
fflush(stdout); }
}
stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT; err = GetConsoleMode(hStdIn, &stdinmode);
err = SetConsoleMode(hStdIn, stdinmode); if (!err) {
if (!err) { printf("Error getting console mode.\n");
printf("Error setting console mode.\n"); fflush(stdout);
fflush(stdout); exit(-4);
} else {
stdinmode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
err = SetConsoleMode(hStdIn, stdinmode);
if (!err) {
printf("Error setting console mode.\n");
fflush(stdout);
exit(-5);
}
}
} }
while (1) { while (1) {
FD_ZERO(&readfds); FD_ZERO(&readfds);
@ -263,15 +286,14 @@ main(int ac, s_char **av)
break; break;
} }
} else { } else {
if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) { if (bRedirected == 1) {
if (!termio(-1, sock, auxout_fp)) { if (!termio(0, sock, auxout_fp))
if (retry++ >= RETRY) { bRedirected = -1;
; } else if (bRedirected == 0) {
} if (WaitForSingleObject(hStdIn, 10) != WAIT_TIMEOUT) {
} else { termio(-1, sock, auxout_fp);
retry = 0; FlushConsoleInputBuffer(hStdIn);
} }
FlushConsoleInputBuffer(hStdIn);
} }
if (FD_ISSET(sock, &readfds)) { if (FD_ISSET(sock, &readfds)) {
if (!serverio(sock, &server)) if (!serverio(sock, &server))
@ -281,11 +303,11 @@ main(int ac, s_char **av)
} }
} }
} }
CloseHandle(hStdIn); if (bRedirected == 0)
CloseHandle(hStdIn);
#endif #endif
ioq_drain(&server); ioq_drain(&server);
(void)close(sock); (void)close(sock);
exit(0);
return 0; /* Shut the compiler up */ return 0; /* Shut the compiler up */
} }

View file

@ -99,6 +99,13 @@ termio(int fd, int sock, FILE *auxfi)
} }
FlushConsoleInputBuffer(hStdIn); FlushConsoleInputBuffer(hStdIn);
if (n == 0) return 1; if (n == 0) return 1;
} else if (fd == 0) {
if (feof(stdin)) {
sendeof(sock);
return 0;
}
fgets(p, sizeof(buf) - i, stdin);
n = strlen(p);
} else { } else {
n = read(fd, p, sizeof(buf) - i); n = read(fd, p, sizeof(buf) - i);
} }