]> git.pond.sub.org Git - empserver/blobdiff - src/client/play.c
Fix client's command abort feature
[empserver] / src / client / play.c
index 7f5e8f45a1956f14c13dd5922c9ba15263c4325c..c8948591cf01d65414d2a8ec8139a5be8e6d6b46 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#ifndef _WIN32
-#include <sys/select.h>
-#include <unistd.h>
-#else
+#ifdef _WIN32
 #include <process.h>
-#include <io.h>
+#include <sys/socket.h>
+#else
+#include <sys/select.h>
 #endif
+#include <unistd.h>
 #include "linebuf.h"
 #include "misc.h"
 #include "proto.h"
@@ -203,7 +203,7 @@ w32_select(int nfds, fd_set *rdfd, fd_set *wrfd, fd_set *errfd, struct timeval*
     default:
        assert(0);
     }
-    sock = W32_FD_TO_SOCKET(sockfd);
+    sock = w32_fd2socket(sockfd);
 
     assert(wrfd->fd_count == 0
           || (wrfd->fd_count == 1 && wrfd->fd_array[0] == (SOCKET)sockfd));
@@ -294,8 +294,6 @@ w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
     return res;
 }
 #define ring_from_file w32_ring_from_file_or_bounce_buf
-#define read(sock, buffer, buf_size) \
-       w32_recv((sock), (buffer), (buf_size), 0)
 #define select(nfds, rd, wr, error, time) \
        w32_select((nfds), (rd), (wr), (error), (time))
 #define sigemptyset(mask) ((void)0)
@@ -304,7 +302,7 @@ w32_ring_from_file_or_bounce_buf(struct ring *r, int fd)
 #endif
 
 #define EOF_COOKIE "ctld\n"
-#define INTR_COOKIE "\naborted\n"
+#define INTR_COOKIE "aborted\n"
 
 int input_fd;
 int send_eof;                          /* need to send EOF_COOKIE */
@@ -467,6 +465,7 @@ play(int sock)
     struct sigaction sa;
     struct ring inbuf;         /* input buffer, draining to SOCK */
     int eof_fd0;               /* read fd 0 hit EOF? */
+    int input_eol;             /* input ends with '\n'? */
     fd_set rdfd, wrfd;
     int n;
 
@@ -478,7 +477,7 @@ play(int sock)
     sigaction(SIGPIPE, &sa, NULL);
 
     ring_init(&inbuf);
-    eof_fd0 = send_eof = send_intr = 0;
+    eof_fd0 = input_eol = send_eof = send_intr = 0;
     input_fd = 0;
     sysdep_stdin_init();
 
@@ -507,10 +506,13 @@ play(int sock)
            }
        }
 
-       if (send_eof
+       if ((send_eof || send_intr) && !input_eol
+           && ring_putc(&inbuf, '\n') != EOF)
+           input_eol = 1;
+       if (send_eof && input_eol
            && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
            send_eof--;
-       if (send_intr
+       if (send_intr && input_eol
            && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0) {
            send_intr = 0;
            if (input_fd) {
@@ -544,7 +546,8 @@ play(int sock)
                    sa.sa_handler = SIG_DFL;
                    sigaction(SIGINT, &sa, NULL);
                }
-           }
+           } else
+               input_eol = ring_peek(&inbuf, -1) == '\n';
        }
 
        /* send it to the server */