iobuf.h revision 1.5
1/*	$OpenBSD: iobuf.h,v 1.5 2019/06/12 17:42:53 eric Exp $	*/
2/*
3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18struct ioqbuf {
19	struct ioqbuf	*next;
20	char		*buf;
21	size_t		 size;
22	size_t		 wpos;
23	size_t		 rpos;
24};
25
26struct iobuf {
27	char		*buf;
28	size_t		 max;
29	size_t		 size;
30	size_t		 wpos;
31	size_t		 rpos;
32
33	size_t		 queued;
34	struct ioqbuf	*outq;
35	struct ioqbuf	*outqlast;
36};
37
38#define IOBUF_WANT_READ		-1
39#define IOBUF_WANT_WRITE	-2
40#define IOBUF_CLOSED		-3
41#define IOBUF_ERROR		-4
42#define IOBUF_TLSERROR		-5
43
44int	iobuf_init(struct iobuf *, size_t, size_t);
45void	iobuf_clear(struct iobuf *);
46
47int	iobuf_extend(struct iobuf *, size_t);
48void	iobuf_normalize(struct iobuf *);
49void	iobuf_drop(struct iobuf *, size_t);
50size_t	iobuf_space(struct iobuf *);
51size_t	iobuf_len(struct iobuf *);
52size_t	iobuf_left(struct iobuf *);
53char   *iobuf_data(struct iobuf *);
54char   *iobuf_getline(struct iobuf *, size_t *);
55ssize_t	iobuf_read(struct iobuf *, int);
56ssize_t	iobuf_read_tls(struct iobuf *, void *);
57
58size_t  iobuf_queued(struct iobuf *);
59void*   iobuf_reserve(struct iobuf *, size_t);
60int	iobuf_queue(struct iobuf *, const void*, size_t);
61int	iobuf_queuev(struct iobuf *, const struct iovec *, int);
62int	iobuf_fqueue(struct iobuf *, const char *, ...);
63int	iobuf_vfqueue(struct iobuf *, const char *, va_list);
64int	iobuf_flush(struct iobuf *, int);
65int	iobuf_flush_tls(struct iobuf *, void *);
66ssize_t	iobuf_write(struct iobuf *, int);
67ssize_t	iobuf_write_tls(struct iobuf *, void *);
68