]> git.pond.sub.org Git - empserver/commitdiff
client: Lift assignment to @input_fd to recv_output()
authorMarkus Armbruster <armbru@pond.sub.org>
Mon, 10 Apr 2017 20:22:21 +0000 (22:22 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 6 Aug 2017 09:22:29 +0000 (11:22 +0200)
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>
src/client/misc.h
src/client/play.c
src/client/servcmd.c

index 3d1ee84f2c02176fb262d9cb9690f4705c632796..29a548a583b06ee08bcf4027cbae5a99a59e1867 100644 (file)
@@ -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
index 89c2f66a6e602b8a8a1b37b27c949c1c9554bbbd..da5b2616092cba10b1335381397d7ec3e2c88199 100644 (file)
@@ -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
  */
 
 #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;
-           }
        }
     }
 }
index 3aab4318bcba0d7782fa11ddaf304ac09d87922c..c02390dc06a1f3d121d0b30c34951950ec1b54fa 100644 (file)
@@ -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