2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * ioqueue.c: Read and write i/o queues
30 * Known contributors to this file:
35 * Read and write onto io queues. Note that
36 * the io queues don't actually do any writing;
37 * that is left for a higher level.
41 #include <stdlib.h> /* malloc free */
42 #include <sys/types.h>
50 static int ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc,
53 static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov,
56 static int ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc);
57 static int appendcc(struct ioqueue *ioq, s_char *buf, int cc);
58 static int removecc(struct ioqueue *ioq, register int cc);
61 static void loc_StripDels(char *pBuf);
69 ioq = (struct ioqueue *)malloc(sizeof(struct ioqueue));
70 emp_initque(&ioq->list.queue);
81 ioq_destroy(struct ioqueue *ioq)
83 #if !defined(aix) && !defined(NeXT)
84 /* ioq_drain doesn't work under aix or NeXT... dunno why --ts */
91 ioq_drain(struct ioqueue *ioq)
96 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
98 (void)emp_remque(&io->queue);
99 (void)free((s_char *)io->data);
100 (void)free((s_char *)io);
107 * copy batch of pointers into the passed
108 * iovec, but don't actually dequeue the data.
109 * return # of iovec initialized.
113 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
117 return ioqtoiov(ioq, iov, cc);
122 * Copy the specified number of characters into the buffer
123 * provided, without actually dequeueing the data. Return
124 * number of bytes actually found.
127 ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
129 return ioqtobuf(ioq, buf, cc);
133 ioq_dequeue(struct ioqueue *ioq, int cc)
135 return removecc(ioq, cc);
139 ioq_append(struct ioqueue *ioq, s_char *buf, int cc)
141 appendcc(ioq, buf, cc);
145 ioq_qsize(struct ioqueue *ioq)
151 * read a line of text up to (but not including)
152 * the newline. return -1 and read nothing if
153 * no input is available
156 ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
161 nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
167 /* remove the newline too */
168 removecc(ioq, nbytes + 1);
177 ioq_puts(struct ioqueue *ioq, s_char *buf)
179 return appendcc(ioq, buf, strlen(buf));
183 * all the rest are local to this module
188 * copy cc bytes from ioq to buf.
189 * this routine doesn't free memory; this is
190 * left for a higher level.
194 ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
197 struct emp_qelem *qp;
198 struct emp_qelem *head;
201 register s_char *offset;
205 head = &ioq->list.queue;
206 for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
207 io = (struct io *)qp;
208 if ((nbytes = io->nbytes - io->offset) < 0) {
209 /* XXX log something here */
215 bcopy(io->data + io->offset, offset, nbytes);
224 * copy at most cc bytes from ioq to buf,
225 * terminating on the stop character.
229 ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
235 struct emp_qelem *qp;
236 struct emp_qelem *head;
240 head = &ioq->list.queue;
243 for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
244 io = (struct io *)qp;
245 if ((nbytes = io->nbytes - io->offset) <= 0)
247 p = io->data + io->offset;
248 for (n = 0; n < nbytes && p[n] != stopc; n++) ;
257 ioqtobuf(ioq, buf, cc < total ? cc : total);
262 * initialize an iovec to point at max bytes worth
263 * of data from the ioqueue.
268 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
274 struct emp_qelem *qp;
278 qp = ioq->list.queue.q_forw;
279 while (qp != &ioq->list.queue && cc > 0) {
280 io = (struct io *)qp;
281 len = io->nbytes - io->offset;
284 iov->iov_base = io->data + io->offset;
298 * append a buffer to the end of the ioq.
302 appendcc(struct ioqueue *ioq, s_char *buf, int cc)
309 /* determine if any space is left */
310 io = (struct io *)ioq->list.queue.q_back;
311 avail = io->size - io->nbytes;
313 /* append to existing buffer */
314 len = cc > avail ? avail : cc;
315 bcopy(buf, io->data + io->nbytes, len);
319 appendcc(ioq, buf + len, cc - len);
321 /* create a new buffer, minimum bufsize bytes */
322 len = cc > ioq->bufsize ? cc : ioq->bufsize;
325 io = (struct io *)malloc(sizeof(struct io));
330 emp_insque(&io->queue, ioq->list.queue.q_back);
337 * remove cc bytes from ioqueue ioq
338 * free memory, dequeue io elements
339 * which are no longer used.
343 removecc(struct ioqueue *ioq, register int cc)
346 struct emp_qelem *qp;
353 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
354 io = (struct io *)qp;
355 there = io->nbytes - io->offset;
358 (void)emp_remque(&io->queue);
359 (void)free((s_char *)io);
362 if (remain >= there) {
363 /* not enough or exact; free entry */
366 (void)emp_remque(&io->queue);
367 (void)free(io->data);
368 (void)free((s_char *)io);
370 /* too much; increment offset */
371 io->offset += remain;
384 * Make an (output) buffer up to the
385 * maximum size of the buffer.
387 * We don't free the bytes...
390 ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
393 struct emp_qelem *qp;
394 struct emp_qelem *head;
403 head = &ioq->list.queue;
405 for (qp = head->q_forw; (qp != head) && (nleft > 0); qp = qp->q_forw) {
406 io = (struct io *)qp;
407 nbytes = io->nbytes - io->offset;
409 /* Paranoid check for bad buffer. */
413 /* too many bytes, wait till next time. */
417 bcopy(io->data + io->offset, offset, nbytes);
428 * Remove backspaces and DELs from the buffer.
430 * Why? Because I got tired of telneting into the
431 * server and having to type perfectly...
434 loc_StripDels(char *pBuf)
440 for (cp = pBuf; *cp;) {
441 /* Remove Backspace and DEL */
442 if (*cp == '\b' || *cp == '\x8F') {