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.
193 ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
196 struct emp_qelem *qp;
197 struct emp_qelem *head;
200 register s_char *offset;
204 head = &ioq->list.queue;
205 for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
206 io = (struct io *)qp;
207 if ((nbytes = io->nbytes - io->offset) < 0) {
208 /* XXX log something here */
214 memcpy(offset, io->data + io->offset, nbytes);
223 * copy at most cc bytes from ioq to buf,
224 * terminating on the stop character.
227 ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
233 struct emp_qelem *qp;
234 struct emp_qelem *head;
238 head = &ioq->list.queue;
241 for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
242 io = (struct io *)qp;
243 if ((nbytes = io->nbytes - io->offset) <= 0)
245 p = io->data + io->offset;
246 for (n = 0; n < nbytes && p[n] != stopc; n++) ;
255 ioqtobuf(ioq, buf, cc < total ? cc : total);
260 * initialize an iovec to point at max bytes worth
261 * of data from the ioqueue.
265 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
271 struct emp_qelem *qp;
275 qp = ioq->list.queue.q_forw;
276 while (qp != &ioq->list.queue && cc > 0) {
277 io = (struct io *)qp;
278 len = io->nbytes - io->offset;
281 iov->iov_base = io->data + io->offset;
295 * append a buffer to the end of the ioq.
298 appendcc(struct ioqueue *ioq, s_char *buf, int cc)
305 /* determine if any space is left */
306 io = (struct io *)ioq->list.queue.q_back;
307 avail = io->size - io->nbytes;
309 /* append to existing buffer */
310 len = cc > avail ? avail : cc;
311 memcpy(io->data + io->nbytes, buf, len);
315 appendcc(ioq, buf + len, cc - len);
317 /* create a new buffer, minimum bufsize bytes */
318 len = cc > ioq->bufsize ? cc : ioq->bufsize;
320 memcpy(ptr, buf, cc);
321 io = (struct io *)malloc(sizeof(struct io));
326 emp_insque(&io->queue, ioq->list.queue.q_back);
333 * remove cc bytes from ioqueue ioq
334 * free memory, dequeue io elements
335 * which are no longer used.
338 removecc(struct ioqueue *ioq, register int cc)
341 struct emp_qelem *qp;
348 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
349 io = (struct io *)qp;
350 there = io->nbytes - io->offset;
353 (void)emp_remque(&io->queue);
354 (void)free((s_char *)io);
357 if (remain >= there) {
358 /* not enough or exact; free entry */
361 (void)emp_remque(&io->queue);
362 (void)free(io->data);
363 (void)free((s_char *)io);
365 /* too much; increment offset */
366 io->offset += remain;
379 * Make an (output) buffer up to the
380 * maximum size of the buffer.
382 * We don't free the bytes...
385 ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
388 struct emp_qelem *qp;
389 struct emp_qelem *head;
398 head = &ioq->list.queue;
400 for (qp = head->q_forw; (qp != head) && (nleft > 0); qp = qp->q_forw) {
401 io = (struct io *)qp;
402 nbytes = io->nbytes - io->offset;
404 /* Paranoid check for bad buffer. */
408 /* too many bytes, wait till next time. */
412 memcpy(offset, io->data + io->offset, nbytes);
423 * Remove backspaces and DELs from the buffer.
425 * Why? Because I got tired of telneting into the
426 * server and having to type perfectly...
429 loc_StripDels(char *pBuf)
435 for (cp = pBuf; *cp;) {
436 /* Remove Backspace and DEL */
437 if (*cp == '\b' || *cp == '\x8F') {