]> git.pond.sub.org Git - empserver/blobdiff - src/client/expect.c
Update copyright notice
[empserver] / src / client / expect.c
index 5ea380a057dc801829e2efff74c4baf60e0fb010..b435a783e6f8a9f7efb04f30ace8549381ae59f4 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  expect.c: Read from the socket, expecting to see a particular code.
- * 
+ *
  *  Known contributors to this file:
- *      Steve McClure, 1998
+ *     Steve McClure, 1998
+ *     Markus Armbruster, 2007-2013
  */
 
 #include <config.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#ifndef _WIN32
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <unistd.h>
-#endif
 #include "misc.h"
-
-#ifdef _WIN32
-#define recv(sock, buffer, buf_size, flags) \
-       w32_recv((sock), (buffer), (buf_size), (flags))
-#define read(sock, buffer, buf_size) \
-       w32_recv((sock), (buffer), (buf_size), 0)
-#define write(sock, buffer, buf_size) \
-       w32_send((sock), (buffer), (buf_size), 0)
-#endif
+#include "proto.h"
 
 int
 recvline(int s, char *buf)
 {
-    int size;
-    char *p;
-    int n;
-    int newline;
-    char *ptr;
-    int cc;
+    int sz = 1024;
+    char *bp;
+    char ch;
+    ssize_t n;
 
-    size = 1024;
-    (void)alarm(30);
-    ptr = buf;
-    n = recv(s, ptr, size, MSG_PEEK);
-    if (n <= 0) {
-       perror("recv");
-       return 0;
-    }
-    size -= n;
-    buf[n] = '\0';
-    if ((p = strchr(ptr, '\n')) == NULL) {
-       do {
-           cc = read(s, ptr, n);
-           if (cc < 0) {
-               perror("expect: read");
-               return 0;
+    bp = buf;
+    for (;;) {
+       n = read(s, &ch, 1);
+       if (n < 0) {
+           if (errno != EINTR) {
+               perror("read");
+               exit(1);
            }
-           if (cc != n) {
-               fprintf(stderr, "expect: short read (%d not %d)\n", cc, n);
-               return 0;
-           }
-           ptr += n;
-           if ((n = recv(s, ptr, size, MSG_PEEK)) <= 0) {
-               perror("recv");
-               return 0;
-           }
-           size -= n;
-           ptr[n] = '\0';
-       } while ((p = strchr(ptr, '\n')) == 0);
-       newline = 1 + p - buf;
-       *p = 0;
-    } else
-       newline = 1 + p - ptr;
-    cc = read(s, buf, newline);
-    if (cc < 0) {
-       perror("expect: read #2");
-       return 0;
+           continue;
+       }
+       if (n == 0)
+           return -1;
+       if (ch == '\n')
+           break;
+       if (bp < buf + sz - 2)
+           *bp++ = ch;
+       /* else silently truncate */
     }
-    if (cc != newline) {
-       fprintf(stderr, "expect: short read #2 (%d not %d)\n",
-               cc, newline);
-       return 0;
-    }
-    buf[newline] = '\0';
-    (void)alarm(0);
-    if (!isxdigit(buf[0]) || buf[1] != ' ') {
-       fprintf(stderr, "Malformed line %s\n", buf);
-       return 0;
+
+    *bp++ = ch;
+    *bp = 0;
+    return parseid(buf);
+}
+
+int
+parseid(char *line)
+{
+    char *end;
+    long id;
+
+    id = strtol(line, &end, 36);
+    if (end == line || *end != ' ') {
+       fprintf(stderr, "Malformed id in line %s", line);
+       id = -1;
     }
-    return strtol(buf, NULL, 16);
+    if (id > C_LAST)
+       id = -1;
+    return id;
 }
 
 int
 expect(int s, int match, char *buf)
 {
-    int code = recvline(s, buf);
-    return code == match;
+    return recvline(s, buf) == match;
 }
 
 void
 sendcmd(int s, char *cmd, char *arg)
 {
     char buf[128];
-    int cc;
+    char *p;
+    ssize_t n;
     int len;
 
-    (void)sprintf(buf, "%s %s\n", cmd, arg != NULL ? arg : "");
-    len = strlen(buf);
-    cc = write(s, buf, len);
-    if (cc < 0) {
-       perror("sendcmd: write");
+    len = snprintf(buf, sizeof(buf), "%s%s%s\n",
+                  cmd, arg ? " " : "", arg ? arg : "");
+    if (len >= (int)sizeof(buf)) {
+       fprintf(stderr, "%s too long\n", cmd);
+       exit(1);
     }
-    if (cc != len) {
-       fprintf(stderr, "sendcmd: short write (%d not %d)\n", cc, len);
+    p = buf;
+    while (len > 0) {
+       n = write(s, buf, len);
+       if (n < 0) {
+           if (errno != EINTR) {
+               perror("sendcmd: write");
+               exit(1);
+           }
+           n = 0;
+       }
+       p += n;
+       len -= n;
     }
 }