aboutsummaryrefslogtreecommitdiff
path: root/tests/iobuf.h
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2019-06-09 18:54:58 +0300
committerSergey Poznyakoff <gray@gnu.org>2019-06-09 18:54:58 +0300
commitc4dbd40477bb658cdc5c5e01b8f80057e587cd88 (patch)
tree73534b2cd2fff99e16c26ed4be8cf115cf4b3960 /tests/iobuf.h
parentc33922f128403f5d05d24f19abeaad2368ce5467 (diff)
downloadpies-c4dbd40477bb658cdc5c5e01b8f80057e587cd88.tar.gz
pies-c4dbd40477bb658cdc5c5e01b8f80057e587cd88.tar.bz2
Test inetd components.
* lib/Makefile.am: Add urlconn.c * lib/libpies.h (url_connect): New proto. * lib/urlconn.c: New file. * src/piesctl.c (shttp_connect): Use url_connect. * tests/.gitignore: Update. * tests/Makefile.am: New auxtool: aux/in.test New test: inet.at * tests/aux/in.test: New file. * tests/inet.at: New file. * tests/iobuf.h: New file. * tests/nt.c: New file. * tests/testsuite.at: Add new test. * tests/to.c: explicitly terminate the child process on timeout.
Diffstat (limited to 'tests/iobuf.h')
-rw-r--r--tests/iobuf.h141
1 files changed, 141 insertions, 0 deletions
diff --git a/tests/iobuf.h b/tests/iobuf.h
new file mode 100644
index 0000000..4e43338
--- /dev/null
+++ b/tests/iobuf.h
@@ -0,0 +1,141 @@
+/* This file is part of GNU Pies.
+ Copyright (C) 2019 Sergey Poznyakoff
+
+ GNU Pies 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 3, or (at your option)
+ any later version.
+
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+#define IO_BUF_SIZE 512
+
+struct iobuf
+{
+ char buf[IO_BUF_SIZE]; /* Initial line buffer */
+ size_t start; /* Start of data */
+ size_t avail; /* Start of available space */
+};
+
+static inline void
+iobuf_reset (struct iobuf *bp)
+{
+ bp->start = bp->avail = 0;
+}
+
+/* Return the count of data bytes in the buffer */
+static inline size_t
+iobuf_data_size (struct iobuf *bp)
+{
+ return bp->avail - bp->start;
+}
+
+static inline char *
+iobuf_data_ptr (struct iobuf *bp)
+{
+ return bp->buf + bp->start;
+}
+
+static inline void
+iobuf_data_advance (struct iobuf *bp, size_t n)
+{
+ bp->start += n;
+ if (bp->start == bp->avail)
+ iobuf_reset (bp);
+}
+
+static inline int
+iobuf_putback (struct iobuf *bp)
+{
+ return bp->buf[--bp->start-1];
+}
+
+static inline int
+iobuf_getc (struct iobuf *bp)
+{
+ int c = -1;
+ if (iobuf_data_size (bp) > 0)
+ {
+ c = *iobuf_data_ptr (bp);
+ iobuf_data_advance(bp, 1);
+ }
+ return c;
+}
+
+/* Return the count of available bytes in the buffer */
+static inline size_t
+iobuf_avail_size (struct iobuf *bp)
+{
+ return sizeof (bp->buf) - bp->avail;
+}
+
+static inline char *
+iobuf_avail_ptr (struct iobuf *bp)
+{
+ return bp->buf + bp->avail;
+}
+
+static inline void
+iobuf_avail_advance (struct iobuf *bp, size_t n)
+{
+ bp->avail += n;
+ if (iobuf_avail_size (bp) == 0 && iobuf_data_size (bp) == 0)
+ iobuf_reset (bp);
+}
+
+static inline int
+iobuf_putc (struct iobuf *bp, int c)
+{
+ if (iobuf_avail_size (bp) > 0)
+ {
+ *iobuf_avail_ptr (bp) = c;
+ iobuf_avail_advance (bp, 1);
+ return c;
+ }
+ return -1;
+}
+
+static inline ssize_t
+iobuf_fill (struct iobuf *bp, int fd)
+{
+ ssize_t n = read (fd, iobuf_avail_ptr (bp), iobuf_avail_size (bp));
+ if (n >= 0)
+ iobuf_avail_advance (bp, n);
+ return n;
+}
+
+static inline ssize_t
+iobuf_flush (struct iobuf *bp, int fd)
+{
+ ssize_t n = write (fd, iobuf_data_ptr (bp), iobuf_data_size (bp));
+ if (n >= 0)
+ iobuf_data_advance (bp, n);
+ return n;
+}
+
+static inline ssize_t
+iobuf_copy (struct iobuf *dst, struct iobuf *src)
+{
+ ssize_t n = iobuf_data_size (src);
+ if (n > 0)
+ {
+ ssize_t avail = iobuf_avail_size (dst);
+ if (avail < n)
+ n = avail;
+ if (n)
+ {
+ memcpy (iobuf_avail_ptr (dst), iobuf_data_ptr (src), n);
+ iobuf_avail_advance (dst, n);
+ iobuf_data_advance (src, n);
+ }
+ }
+ return 0;
+}
+
+

Return to:

Send suggestions and report system problems to the System administrator.