]> git.pond.sub.org Git - empserver/blob - src/lib/gen/ioqueue.c
Update copyright notice.
[empserver] / src / lib / gen / ioqueue.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  ioqueue.c: Read and write i/o queues
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 /*
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.
38  */
39
40 #include <stdio.h>
41 #include <stdlib.h>             /* malloc free */
42 #include <sys/types.h>
43 #if !defined(_WIN32)
44 #include <sys/uio.h>
45 #endif
46 #include "misc.h"
47 #include "queue.h"
48 #include "ioqueue.h"
49
50 static int ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc,
51                      register int stopc);
52 #if !defined(_WIN32)
53 static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov,
54                     register int max);
55 #endif
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);
59
60 #if defined(_WIN32)
61 static void loc_StripDels(char *pBuf);
62 #endif
63
64 struct ioqueue *
65 ioq_create(int size)
66 {
67     struct ioqueue *ioq;
68
69     ioq = (struct ioqueue *)malloc(sizeof(struct ioqueue));
70     emp_initque(&ioq->list.queue);
71     ioq->list.nbytes = 0;
72     ioq->list.offset = 0;
73     ioq->list.size = 0;
74     ioq->list.data = 0;
75     ioq->bufsize = size;
76     ioq->cc = 0;
77     return ioq;
78 }
79
80 void
81 ioq_destroy(struct ioqueue *ioq)
82 {
83 #if !defined(aix) && !defined(NeXT)
84 /* ioq_drain doesn't work under aix or NeXT... dunno why --ts */
85     ioq_drain(ioq);
86 #endif /* aix */
87     free((s_char *)ioq);
88 }
89
90 void
91 ioq_drain(struct ioqueue *ioq)
92 {
93     struct emp_qelem *qp;
94     struct io *io;
95
96     while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
97         io = (struct io *)qp;
98         emp_remque(&io->queue);
99         free(io->data);
100         free(io);
101     }
102
103     ioq->cc = 0;
104 }
105
106 /*
107  * copy batch of pointers into the passed
108  * iovec, but don't actually dequeue the data.
109  * return # of iovec initialized.
110  */
111 #if !defined(_WIN32)
112 int
113 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
114 {
115     if (ioq->cc <= 0)
116         return 0;
117     return ioqtoiov(ioq, iov, cc);
118 }
119 #endif
120
121 /*
122  * Copy the specified number of characters into the buffer
123  * provided, without actually dequeueing the data.  Return
124  * number of bytes actually found.
125  */
126 int
127 ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
128 {
129     return ioqtobuf(ioq, buf, cc);
130 }
131
132 int
133 ioq_dequeue(struct ioqueue *ioq, int cc)
134 {
135     return removecc(ioq, cc);
136 }
137
138 void
139 ioq_append(struct ioqueue *ioq, s_char *buf, int cc)
140 {
141     appendcc(ioq, buf, cc);
142 }
143
144 int
145 ioq_qsize(struct ioqueue *ioq)
146 {
147     return ioq->cc;
148 }
149
150 /*
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
154  */
155 int
156 ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
157 {
158     int nbytes;
159     int actual;
160
161     nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
162     if (nbytes >= 0) {
163         actual = nbytes;
164         if (actual > cc - 1)
165             actual = cc - 1;
166         buf[actual] = '\0';
167         /* remove the newline too */
168         removecc(ioq, nbytes + 1);
169 #if defined(_WIN32)
170         loc_StripDels(buf);
171 #endif
172     }
173     return nbytes;
174 }
175
176 int
177 ioq_puts(struct ioqueue *ioq, s_char *buf)
178 {
179     return appendcc(ioq, buf, strlen(buf));
180 }
181
182 /*
183  * all the rest are local to this module
184  */
185
186
187 /*
188  * copy cc bytes from ioq to buf.
189  * this routine doesn't free memory; this is
190  * left for a higher level.
191  */
192 static int
193 ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
194 {
195     struct io *io;
196     struct emp_qelem *qp;
197     struct emp_qelem *head;
198     register int nbytes;
199     register int nleft;
200     register s_char *offset;
201
202     nleft = cc;
203     offset = buf;
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 */
209             continue;
210         }
211         if (nbytes > 0) {
212             if (nleft < nbytes)
213                 nbytes = nleft;
214             memcpy(offset, io->data + io->offset, nbytes);
215             offset += nbytes;
216             nleft -= nbytes;
217         }
218     }
219     return offset - buf;
220 }
221
222 /*
223  * copy at most cc bytes from ioq to buf,
224  * terminating on the stop character.
225  */
226 static int
227 ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
228 {
229     register int nbytes;
230     register s_char *p;
231     register int n;
232     struct io *io;
233     struct emp_qelem *qp;
234     struct emp_qelem *head;
235     int total;
236     int found;
237
238     head = &ioq->list.queue;
239     found = 0;
240     total = 0;
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)
244             continue;
245         p = io->data + io->offset;
246         for (n = 0; n < nbytes && p[n] != stopc; n++) ;
247         total += n;
248         if (n < nbytes) {
249             found++;
250             break;
251         }
252     }
253     if (found == 0)
254         return -1;
255     ioqtobuf(ioq, buf, cc < total ? cc : total);
256     return total;
257 }
258
259 /*
260  * initialize an iovec to point at max bytes worth
261  * of data from the ioqueue.
262  */
263 #if !defined(_WIN32)
264 static int
265 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
266 {
267     struct io *io;
268     register int cc;
269     register int niov;
270     register int len;
271     struct emp_qelem *qp;
272
273     cc = max;
274     niov = 0;
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;
279         if (len > cc)
280             len = cc;
281         iov->iov_base = io->data + io->offset;
282         iov->iov_len = len;
283         cc -= len;
284         niov++;
285         iov++;
286         qp = qp->q_forw;
287         if (niov >= 16)
288             break;
289     }
290     return niov;
291 }
292 #endif
293
294 /*
295  * append a buffer to the end of the ioq.
296  */
297 static int
298 appendcc(struct ioqueue *ioq, s_char *buf, int cc)
299 {
300     struct io *io;
301     int len;
302     s_char *ptr;
303     int avail;
304
305     /* determine if any space is left */
306     io = (struct io *)ioq->list.queue.q_back;
307     avail = io->size - io->nbytes;
308     if (avail > 0) {
309         /* append to existing buffer */
310         len = cc > avail ? avail : cc;
311         memcpy(io->data + io->nbytes, buf, len);
312         io->nbytes += len;
313         ioq->cc += len;
314         if (avail < cc)
315             appendcc(ioq, buf + len, cc - len);
316     } else {
317         /* create a new buffer, minimum bufsize bytes */
318         len = cc > ioq->bufsize ? cc : ioq->bufsize;
319         ptr = malloc(len);
320         memcpy(ptr, buf, cc);
321         io = (struct io *)malloc(sizeof(struct io));
322         io->nbytes = cc;
323         io->size = len;
324         io->offset = 0;
325         io->data = ptr;
326         emp_insque(&io->queue, ioq->list.queue.q_back);
327         ioq->cc += cc;
328     }
329     return cc;
330 }
331
332 /*
333  * remove cc bytes from ioqueue ioq
334  * free memory, dequeue io elements
335  * which are no longer used.
336  */
337 static int
338 removecc(struct ioqueue *ioq, register int cc)
339 {
340     struct io *io;
341     struct emp_qelem *qp;
342     register int nbytes;
343     register int there;
344     register int remain;
345
346     nbytes = 0;
347     remain = cc;
348     while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
349         io = (struct io *)qp;
350         there = io->nbytes - io->offset;
351         if (there < 0) {
352             /* error */
353             emp_remque(&io->queue);
354             free(io);
355             continue;
356         }
357         if (remain >= there) {
358             /* not enough or exact; free entry */
359             nbytes += there;
360             remain -= there;
361             emp_remque(&io->queue);
362             free(io->data);
363             free(io);
364         } else {
365             /* too much; increment offset */
366             io->offset += remain;
367             nbytes += remain;
368             remain = 0;
369         }
370         if (remain <= 0)
371             break;
372     }
373     ioq->cc -= nbytes;
374     return nbytes;
375 }
376
377 #if defined(_WIN32)
378 /*
379  * Make an (output) buffer up to the
380  * maximum size of the buffer.
381  *
382  * We don't free the bytes...
383  */
384 int
385 ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
386 {
387     struct io *io;
388     struct emp_qelem *qp;
389     struct emp_qelem *head;
390     int nbytes;
391     int nleft;
392     int ncopied;
393     s_char *offset;
394
395     ncopied = 0;
396     nleft = nBufLen;
397     offset = pBuf;
398     head = &ioq->list.queue;
399
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;
403         if (nbytes < 0) {
404             /* Paranoid check for bad buffer. */
405             continue;
406         }
407
408         /* too many bytes, wait till next time. */
409         if (nbytes > nleft)
410             break;
411
412         memcpy(offset, io->data + io->offset, nbytes);
413         offset += nbytes;
414         nleft -= nbytes;
415         ncopied += nbytes;
416     }
417     return ncopied;
418 }
419 #endif /* _WIN32 */
420
421 #if defined(_WIN32)
422 /*
423  * Remove backspaces and DELs from the buffer.
424  *
425  * Why?  Because I got tired of telneting into the
426  * server and having to type perfectly...
427  */
428 static void
429 loc_StripDels(char *pBuf)
430 {
431     char *cp;
432     char *dp;
433     char *sp;
434
435     for (cp = pBuf; *cp;) {
436         /* Remove Backspace and DEL */
437         if (*cp == '\b' || *cp == '\x8F') {
438             if (cp == pBuf)
439                 dp = cp;
440             else
441                 dp = cp - 1;
442             sp = cp + 1;
443             while (*sp)
444                 *dp++ = *sp++;
445             *dp = 0;
446         } else
447             cp++;
448     }
449 }
450 #endif