]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/ioqueue.c
Update copyright notice
[empserver] / src / lib / gen / ioqueue.c
index c9c20114a6d0b5fdec20920b614c118164c1b0bc..ce2490c7542cae3d2004ee6c185ab9039a392375 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, 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
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  ioqueue.c: Read and write i/o queues
- * 
+ *
  *  Known contributors to this file:
- *     
+ *
  */
 
 /*
  * that is left for a higher level.
  */
 
-#include <stdio.h>
-#include <stdlib.h>            /* malloc free */
-#include <sys/types.h>
-#if !defined(_WIN32)
+#include <config.h>
+
+#include <stdlib.h>
+#include <string.h>
 #include <sys/uio.h>
-#endif
+#include "ioqueue.h"
 #include "misc.h"
 #include "queue.h"
-#include "ioqueue.h"
-
-static int ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc,
-                    register int stopc);
-#if !defined(_WIN32)
-static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov,
-                   register int max);
-#endif
-static int ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc);
-static int appendcc(struct ioqueue *ioq, s_char *buf, int cc);
-static int removecc(struct ioqueue *ioq, register int cc);
 
-#if defined(_WIN32)
-static void loc_StripDels(char *pBuf);
-#endif
+struct io {
+    struct emp_qelem queue;
+    int size;
+    int nbytes;
+    int offset;
+    char *data;
+};
+
+struct ioqueue {
+    struct io list;
+    int bufsize;
+    int cc;
+};
+
+static int ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc);
+static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max);
+static int ioqtobuf(struct ioqueue *ioq, char *buf, int cc);
+static int appendcc(struct ioqueue *ioq, char *buf, int cc);
+static int removecc(struct ioqueue *ioq, int cc);
 
 struct ioqueue *
 ioq_create(int size)
 {
     struct ioqueue *ioq;
 
-    ioq = (struct ioqueue *)malloc(sizeof(struct ioqueue));
+    ioq = malloc(sizeof(struct ioqueue));
     emp_initque(&ioq->list.queue);
     ioq->list.nbytes = 0;
     ioq->list.offset = 0;
     ioq->list.size = 0;
-    ioq->list.data = 0;
+    ioq->list.data = NULL;
     ioq->bufsize = size;
     ioq->cc = 0;
     return ioq;
@@ -80,11 +85,8 @@ ioq_create(int size)
 void
 ioq_destroy(struct ioqueue *ioq)
 {
-#if !defined(aix) && !defined(NeXT)
-/* ioq_drain doesn't work under aix or NeXT... dunno why --ts */
     ioq_drain(ioq);
-#endif /* aix */
-    free((s_char *)ioq);
+    free(ioq);
 }
 
 void
@@ -95,9 +97,9 @@ ioq_drain(struct ioqueue *ioq)
 
     while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
        io = (struct io *)qp;
-       (void)emp_remque(&io->queue);
-       (void)free((s_char *)io->data);
-       (void)free((s_char *)io);
+       emp_remque(&io->queue);
+       free(io->data);
+       free(io);
     }
 
     ioq->cc = 0;
@@ -108,7 +110,6 @@ ioq_drain(struct ioqueue *ioq)
  * iovec, but don't actually dequeue the data.
  * return # of iovec initialized.
  */
-#if !defined(_WIN32)
 int
 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
 {
@@ -116,7 +117,6 @@ ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
        return 0;
     return ioqtoiov(ioq, iov, cc);
 }
-#endif
 
 /*
  * Copy the specified number of characters into the buffer
@@ -124,7 +124,7 @@ ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
  * number of bytes actually found.
  */
 int
-ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
+ioq_peek(struct ioqueue *ioq, char *buf, int cc)
 {
     return ioqtobuf(ioq, buf, cc);
 }
@@ -136,7 +136,7 @@ ioq_dequeue(struct ioqueue *ioq, int cc)
 }
 
 void
-ioq_append(struct ioqueue *ioq, s_char *buf, int cc)
+ioq_append(struct ioqueue *ioq, char *buf, int cc)
 {
     appendcc(ioq, buf, cc);
 }
@@ -153,7 +153,7 @@ ioq_qsize(struct ioqueue *ioq)
  * no input is available
  */
 int
-ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
+ioq_gets(struct ioqueue *ioq, char *buf, int cc)
 {
     int nbytes;
     int actual;
@@ -163,42 +163,35 @@ ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
        actual = nbytes;
        if (actual > cc - 1)
            actual = cc - 1;
+       /* telnet terminates lines with "\r\n", get rid of \r */
+       if (actual > 0 && buf[actual-1] == '\r')
+           actual--;
        buf[actual] = '\0';
        /* remove the newline too */
        removecc(ioq, nbytes + 1);
-#if defined(_WIN32)
-       loc_StripDels(buf);
-#endif
     }
     return nbytes;
 }
 
 int
-ioq_puts(struct ioqueue *ioq, s_char *buf)
+ioq_puts(struct ioqueue *ioq, char *buf)
 {
     return appendcc(ioq, buf, strlen(buf));
 }
 
-/*
- * all the rest are local to this module
- */
-
-
 /*
  * copy cc bytes from ioq to buf.
  * this routine doesn't free memory; this is
  * left for a higher level.
  */
-static
-    int
-ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
+static int
+ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
 {
     struct io *io;
     struct emp_qelem *qp;
     struct emp_qelem *head;
-    register int nbytes;
-    register int nleft;
-    register s_char *offset;
+    int nbytes, nleft;
+    char *offset;
 
     nleft = cc;
     offset = buf;
@@ -212,7 +205,7 @@ ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
        if (nbytes > 0) {
            if (nleft < nbytes)
                nbytes = nleft;
-           bcopy(io->data + io->offset, offset, nbytes);
+           memcpy(offset, io->data + io->offset, nbytes);
            offset += nbytes;
            nleft -= nbytes;
        }
@@ -224,13 +217,12 @@ ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
  * copy at most cc bytes from ioq to buf,
  * terminating on the stop character.
  */
-static
-    int
-ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
+static int
+ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc)
 {
-    register int nbytes;
-    register s_char *p;
-    register int n;
+    int nbytes;
+    char *p;
+    int n;
     struct io *io;
     struct emp_qelem *qp;
     struct emp_qelem *head;
@@ -262,15 +254,11 @@ ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
  * initialize an iovec to point at max bytes worth
  * of data from the ioqueue.
  */
-#if !defined(_WIN32)
-static
-    int
-ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
+static int
+ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max)
 {
     struct io *io;
-    register int cc;
-    register int niov;
-    register int len;
+    int cc, niov, len;
     struct emp_qelem *qp;
 
     cc = max;
@@ -292,18 +280,16 @@ ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
     }
     return niov;
 }
-#endif
 
 /*
  * append a buffer to the end of the ioq.
  */
-static
-    int
-appendcc(struct ioqueue *ioq, s_char *buf, int cc)
+static int
+appendcc(struct ioqueue *ioq, char *buf, int cc)
 {
     struct io *io;
     int len;
-    s_char *ptr;
+    char *ptr;
     int avail;
 
     /* determine if any space is left */
@@ -312,7 +298,7 @@ appendcc(struct ioqueue *ioq, s_char *buf, int cc)
     if (avail > 0) {
        /* append to existing buffer */
        len = cc > avail ? avail : cc;
-       bcopy(buf, io->data + io->nbytes, len);
+       memcpy(io->data + io->nbytes, buf, len);
        io->nbytes += len;
        ioq->cc += len;
        if (avail < cc)
@@ -321,8 +307,8 @@ appendcc(struct ioqueue *ioq, s_char *buf, int cc)
        /* create a new buffer, minimum bufsize bytes */
        len = cc > ioq->bufsize ? cc : ioq->bufsize;
        ptr = malloc(len);
-       bcopy(buf, ptr, cc);
-       io = (struct io *)malloc(sizeof(struct io));
+       memcpy(ptr, buf, cc);
+       io = malloc(sizeof(struct io));
        io->nbytes = cc;
        io->size = len;
        io->offset = 0;
@@ -338,15 +324,12 @@ appendcc(struct ioqueue *ioq, s_char *buf, int cc)
  * free memory, dequeue io elements
  * which are no longer used.
  */
-static
-    int
-removecc(struct ioqueue *ioq, register int cc)
+static int
+removecc(struct ioqueue *ioq, int cc)
 {
     struct io *io;
     struct emp_qelem *qp;
-    register int nbytes;
-    register int there;
-    register int remain;
+    int nbytes, there, remain;
 
     nbytes = 0;
     remain = cc;
@@ -355,17 +338,17 @@ removecc(struct ioqueue *ioq, register int cc)
        there = io->nbytes - io->offset;
        if (there < 0) {
            /* error */
-           (void)emp_remque(&io->queue);
-           (void)free((s_char *)io);
+           emp_remque(&io->queue);
+           free(io);
            continue;
        }
        if (remain >= there) {
            /* not enough or exact; free entry */
            nbytes += there;
            remain -= there;
-           (void)emp_remque(&io->queue);
-           (void)free(io->data);
-           (void)free((s_char *)io);
+           emp_remque(&io->queue);
+           free(io->data);
+           free(io);
        } else {
            /* too much; increment offset */
            io->offset += remain;
@@ -378,78 +361,3 @@ removecc(struct ioqueue *ioq, register int cc)
     ioq->cc -= nbytes;
     return nbytes;
 }
-
-#if defined(_WIN32)
-/*
- * Make an (output) buffer up to the
- * maximum size of the buffer.
- *
- * We don't free the bytes...
- */
-int
-ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
-{
-    struct io *io;
-    struct emp_qelem *qp;
-    struct emp_qelem *head;
-    int nbytes;
-    int nleft;
-    int ncopied;
-    s_char *offset;
-
-    ncopied = 0;
-    nleft = nBufLen;
-    offset = pBuf;
-    head = &ioq->list.queue;
-
-    for (qp = head->q_forw; (qp != head) && (nleft > 0); qp = qp->q_forw) {
-       io = (struct io *)qp;
-       nbytes = io->nbytes - io->offset;
-       if (nbytes < 0) {
-           /* Paranoid check for bad buffer. */
-           continue;
-       }
-
-       /* too many bytes, wait till next time. */
-       if (nbytes > nleft)
-           break;
-
-       bcopy(io->data + io->offset, offset, nbytes);
-       offset += nbytes;
-       nleft -= nbytes;
-       ncopied += nbytes;
-    }
-    return ncopied;
-}
-#endif /* _WIN32 */
-
-#if defined(_WIN32)
-/*
- * Remove backspaces and DELs from the buffer.
- *
- * Why?  Because I got tired of telneting into the
- * server and having to type perfectly...
- */
-static void
-loc_StripDels(char *pBuf)
-{
-    char *cp;
-    char *dp;
-    char *sp;
-
-    for (cp = pBuf; *cp;) {
-       /* Remove Backspace and DEL */
-       if (*cp == '\b' || *cp == '\x8F') {
-           if (cp == pBuf)
-               dp = cp;
-           else
-               dp = cp - 1;
-           sp = cp + 1;
-           while (*sp)
-               *dp++ = *sp++;
-           *dp = 0;
-       } else
-           cp++;
-    }
-}
-#endif