]> git.pond.sub.org Git - empserver/blobdiff - src/client/expect.c
Update copyright notice
[empserver] / src / client / expect.c
index 96dac3c9fd575ce0054cceb34c12312e70b386e3..b435a783e6f8a9f7efb04f30ace8549381ae59f4 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, 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,
  *  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/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  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 "misc.h"
-#include "fnlist.h"
+#include <config.h>
 
+#include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 #include <sys/types.h>
-#ifndef _WIN32
 #include <sys/socket.h>
 #include <unistd.h>
-#else
-#include <winsock.h>
-#endif
+#include "misc.h"
+#include "proto.h"
 
 int
-expect(s, match, buf)
-       int     s;
-       int     match;
-       s_char  *buf;
+recvline(int s, char *buf)
 {
-       int     size;
-       s_char  *p;
-       int     n;
-       int     code;
-       int     newline;
-       s_char  *ptr;
-       int     cc;
+    int sz = 1024;
+    char *bp;
+    char ch;
+    ssize_t n;
 
-       size = 1024;
-#ifndef _WIN32
-       (void) alarm(30);
-#endif
-       ptr = buf;
-       n = recv(s, ptr, size, MSG_PEEK);
-       if (n <= 0) {
-               fprintf(stderr, "Expecting code %d\n", match);
-#ifdef _WIN32
-               errno = WSAGetLastError();
-#endif
-               perror("recv");
-               return 0;
+    bp = buf;
+    for (;;) {
+       n = read(s, &ch, 1);
+       if (n < 0) {
+           if (errno != EINTR) {
+               perror("read");
+               exit(1);
+           }
+           continue;
        }
-       size -= n;
-       buf[n] = '\0';
-       if ((p = strchr(ptr, '\n')) == 0) {
-               do {
-#ifndef _WIN32
-                       cc = read(s, ptr, n);
-#else
-                       cc = recv(s, ptr, n, 0);
-#endif
-                       if (cc < 0) {
-#ifdef _WIN32
-               errno = WSAGetLastError();
-#endif
-                               perror("expect: read");
-                               return 0;
-                       }
-                       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) {
-                               fprintf(stderr, "Expecting %d, got %s\n",
-                                       match, buf);
-                               return 0;
-                       }
-                       size -= n;
-                       ptr[n] = '\0';
-               } while ((p = index(ptr, '\n')) == 0);
-               newline = 1 + p - buf;
-               *p = 0;
-       } else
-               newline = 1 + p - ptr;
-#ifndef _WIN32
-       cc = read(s, buf, newline);
-#else
-       cc = recv(s, buf, newline, 0);
-#endif
-       if (cc < 0) {
-#ifdef _WIN32
-               errno = WSAGetLastError();
-#endif
-               perror("expect: read #2");
-               return 0;
-       }
-       if (cc != newline) {
-               fprintf(stderr, "expect: short read #2 (%d not %d)\n",
-                       cc, newline);
-               return 0;
-       }
-       buf[newline] = '\0';
-#ifndef _WIN32
-       (void) alarm(0);
-#endif
-       if (!isxdigit(*buf)) {
-               fprintf(stderr, "Expecting %d, got %s\n", match, buf);
-               return 0;
-       }
-       if (isdigit(*buf))
-               code = *buf - '0';
-       else {
-               if (isupper(*buf))
-                       *buf = tolower(*buf);
-               code = 10 + *buf - 'a';
-       }
-       if (code == match)
-               return 1;
-       return 0;
+       if (n == 0)
+           return -1;
+       if (ch == '\n')
+           break;
+       if (bp < buf + sz - 2)
+           *bp++ = ch;
+       /* else silently truncate */
+    }
+
+    *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;
+    }
+    if (id > C_LAST)
+       id = -1;
+    return id;
+}
+
+int
+expect(int s, int match, char *buf)
+{
+    return recvline(s, buf) == match;
 }
 
 void
-sendcmd(s, cmd, arg)
-       int     s;
-       int     cmd;
-       s_char  *arg;
+sendcmd(int s, char *cmd, char *arg)
 {
-       extern  struct fn fnlist[];
-       s_char  buf[128];
-       int     cc;
-       int     len;
+    char buf[128];
+    char *p;
+    ssize_t n;
+    int len;
 
-       (void) sprintf(buf, "%s %s\n", fnlist[cmd].name, arg != 0 ? arg : "");
-       len = strlen(buf);
-#ifndef _WIN32
-       cc = write(s, buf, len);
-#else
-       cc = send(s, buf, len, 0);
-#endif
-       if (cc < 0) {
-#ifdef _WIN32
-               errno = WSAGetLastError();
-#endif
+    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);
+    }
+    p = buf;
+    while (len > 0) {
+       n = write(s, buf, len);
+       if (n < 0) {
+           if (errno != EINTR) {
                perror("sendcmd: write");
+               exit(1);
+           }
+           n = 0;
        }
-       if (cc != len) {
-               fprintf(stderr, "sendcmd: short write (%d not %d)\n", cc, len);
-       }
+       p += n;
+       len -= n;
+    }
 }