client: Lift assignment to @input_fd to recv_output()

On successful execute, servercmd() sets @input_fd to the batch file
descriptor.  Return the file descriptor instead, and let its caller
recv_output() set @input_fd.  This permits giving @input_fd static
linkage.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2017-04-10 22:22:21 +02:00
parent 37e68e5796
commit 8301e0f144
3 changed files with 35 additions and 33 deletions

View file

@ -28,6 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Steve McClure, 1998 * Steve McClure, 1998
* Markus Armbruster, 2004-2017
*/ */
#ifndef MISC_H #ifndef MISC_H
@ -41,7 +42,6 @@
extern char empirehost[]; extern char empirehost[];
extern char empireport[]; extern char empireport[];
extern int eight_bit_clean; extern int eight_bit_clean;
extern int input_fd;
extern FILE *auxfp; extern FILE *auxfp;
extern int restricted; extern int restricted;
@ -62,7 +62,7 @@ int tcp_connect(char *, char *);
int login(int s, char *uname, char *cname, char *cpass, int kill_proc, int); int login(int s, char *uname, char *cname, char *cpass, int kill_proc, int);
int play(int); int play(int);
void sendcmd(int s, char *cmd, char *arg); void sendcmd(int s, char *cmd, char *arg);
void servercmd(int, char *, int); int servercmd(int, char *, int);
void outch(char); void outch(char);
#ifdef _MSC_VER #ifdef _MSC_VER

View file

@ -27,7 +27,7 @@
* play.c: Playing the game * play.c: Playing the game
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2007-2016 * Markus Armbruster, 2007-2017
* Ron Koenderink, 2007-2009 * Ron Koenderink, 2007-2009
*/ */
@ -51,6 +51,19 @@
#include "ringbuf.h" #include "ringbuf.h"
#include "secure.h" #include "secure.h"
#define EOF_COOKIE "ctld\n"
#define INTR_COOKIE "aborted\n"
/*
* Player input file descriptor
* 0 while reading interactive input
* >0 while reading a batch file
* <0 during error handling
*/
static int input_fd;
static volatile sig_atomic_t send_intr; /* need to send INTR_COOKIE */
#ifdef _WIN32 #ifdef _WIN32
static CRITICAL_SECTION signal_critical_section; static CRITICAL_SECTION signal_critical_section;
static LPCRITICAL_SECTION signal_critical_section_ptr = NULL; static LPCRITICAL_SECTION signal_critical_section_ptr = NULL;
@ -301,19 +314,6 @@ w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
#define sysdep_stdin_init() ((void)0) #define sysdep_stdin_init() ((void)0)
#endif #endif
#define EOF_COOKIE "ctld\n"
#define INTR_COOKIE "aborted\n"
/*
* Player input file descriptor
* 0 while reading interactive input
* >0 while reading a batch file
* <0 during error handling
*/
int input_fd;
static volatile sig_atomic_t send_intr; /* need to send INTR_COOKIE */
/* /*
* Receive and process server output from @sock. * Receive and process server output from @sock.
* Return number of characters received on success, -1 on error. * Return number of characters received on success, -1 on error.
@ -341,7 +341,7 @@ recv_output(int sock)
static struct lbuf lbuf; static struct lbuf lbuf;
char buf[4096]; char buf[4096];
ssize_t n; ssize_t n;
int i, ch, len; int i, ch, len, fd;
char *line; char *line;
n = read(sock, buf, sizeof(buf)); n = read(sock, buf, sizeof(buf));
@ -387,7 +387,18 @@ recv_output(int sock)
len = lbuf_putc(&lbuf, ch); len = lbuf_putc(&lbuf, ch);
if (len) { if (len) {
line = lbuf_line(&lbuf); line = lbuf_line(&lbuf);
servercmd(id, line, len); fd = servercmd(id, line, len);
if (fd < 0) {
/* failed execute */
if (input_fd)
close(input_fd);
input_fd = 0;
send_intr = 1;
} else if (fd > 0) {
/* successful execute, switch to batch file */
assert(!input_fd);
input_fd = fd;
}
lbuf_init(&lbuf); lbuf_init(&lbuf);
state = SCANNING_ID; state = SCANNING_ID;
} }
@ -567,11 +578,6 @@ play(int sock)
} }
if (n == 0) if (n == 0)
return 0; return 0;
if (input_fd < 0) {
/* execute failed */
send_intr = 1;
input_fd = 0;
}
} }
} }
} }

View file

@ -30,7 +30,7 @@
* Dave Pare, 1989 * Dave Pare, 1989
* Steve McClure, 1998 * Steve McClure, 1998
* Ron Koenderink, 2005 * Ron Koenderink, 2005
* Markus Armbruster, 2005-2015 * Markus Armbruster, 2005-2017
*/ */
#include <config.h> #include <config.h>
@ -59,7 +59,7 @@ static void doredir(char *p);
static void dopipe(char *p); static void dopipe(char *p);
static int doexecute(char *p); static int doexecute(char *p);
void int
servercmd(int code, char *arg, int len) servercmd(int code, char *arg, int len)
{ {
static int nmin, nbtu, fd; static int nmin, nbtu, fd;
@ -90,15 +90,9 @@ servercmd(int code, char *arg, int len)
break; break;
case C_EXECUTE: case C_EXECUTE:
fd = doexecute(arg); fd = doexecute(arg);
if (fd < 0) { if (fd >= 0)
if (input_fd)
close(input_fd);
} else {
assert(!input_fd);
executing = 1; executing = 1;
} return fd;
input_fd = fd;
break;
case C_EXIT: case C_EXIT:
printf("Exit: %s", arg); printf("Exit: %s", arg);
if (auxfp) if (auxfp)
@ -129,6 +123,8 @@ servercmd(int code, char *arg, int len)
assert(0); assert(0);
break; break;
} }
return 0;
} }
static void static void