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:
parent
37e68e5796
commit
8301e0f144
3 changed files with 35 additions and 33 deletions
|
@ -28,6 +28,7 @@
|
|||
*
|
||||
* Known contributors to this file:
|
||||
* Steve McClure, 1998
|
||||
* Markus Armbruster, 2004-2017
|
||||
*/
|
||||
|
||||
#ifndef MISC_H
|
||||
|
@ -41,7 +42,6 @@
|
|||
extern char empirehost[];
|
||||
extern char empireport[];
|
||||
extern int eight_bit_clean;
|
||||
extern int input_fd;
|
||||
extern FILE *auxfp;
|
||||
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 play(int);
|
||||
void sendcmd(int s, char *cmd, char *arg);
|
||||
void servercmd(int, char *, int);
|
||||
int servercmd(int, char *, int);
|
||||
void outch(char);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
* play.c: Playing the game
|
||||
*
|
||||
* Known contributors to this file:
|
||||
* Markus Armbruster, 2007-2016
|
||||
* Markus Armbruster, 2007-2017
|
||||
* Ron Koenderink, 2007-2009
|
||||
*/
|
||||
|
||||
|
@ -51,6 +51,19 @@
|
|||
#include "ringbuf.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
|
||||
static CRITICAL_SECTION signal_critical_section;
|
||||
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)
|
||||
#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.
|
||||
* Return number of characters received on success, -1 on error.
|
||||
|
@ -341,7 +341,7 @@ recv_output(int sock)
|
|||
static struct lbuf lbuf;
|
||||
char buf[4096];
|
||||
ssize_t n;
|
||||
int i, ch, len;
|
||||
int i, ch, len, fd;
|
||||
char *line;
|
||||
|
||||
n = read(sock, buf, sizeof(buf));
|
||||
|
@ -387,7 +387,18 @@ recv_output(int sock)
|
|||
len = lbuf_putc(&lbuf, ch);
|
||||
if (len) {
|
||||
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);
|
||||
state = SCANNING_ID;
|
||||
}
|
||||
|
@ -567,11 +578,6 @@ play(int sock)
|
|||
}
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (input_fd < 0) {
|
||||
/* execute failed */
|
||||
send_intr = 1;
|
||||
input_fd = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
* Dave Pare, 1989
|
||||
* Steve McClure, 1998
|
||||
* Ron Koenderink, 2005
|
||||
* Markus Armbruster, 2005-2015
|
||||
* Markus Armbruster, 2005-2017
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
@ -59,7 +59,7 @@ static void doredir(char *p);
|
|||
static void dopipe(char *p);
|
||||
static int doexecute(char *p);
|
||||
|
||||
void
|
||||
int
|
||||
servercmd(int code, char *arg, int len)
|
||||
{
|
||||
static int nmin, nbtu, fd;
|
||||
|
@ -90,15 +90,9 @@ servercmd(int code, char *arg, int len)
|
|||
break;
|
||||
case C_EXECUTE:
|
||||
fd = doexecute(arg);
|
||||
if (fd < 0) {
|
||||
if (input_fd)
|
||||
close(input_fd);
|
||||
} else {
|
||||
assert(!input_fd);
|
||||
if (fd >= 0)
|
||||
executing = 1;
|
||||
}
|
||||
input_fd = fd;
|
||||
break;
|
||||
return fd;
|
||||
case C_EXIT:
|
||||
printf("Exit: %s", arg);
|
||||
if (auxfp)
|
||||
|
@ -129,6 +123,8 @@ servercmd(int code, char *arg, int len)
|
|||
assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue