169990Sdes/*-
2181462Sdes * Copyright (c) 2000-2008 Poul-Henning Kamp
3181462Sdes * Copyright (c) 2000-2008 Dag-Erling Co��dan Sm��rgrav
469990Sdes * All rights reserved.
569990Sdes *
669990Sdes * Redistribution and use in source and binary forms, with or without
769990Sdes * modification, are permitted provided that the following conditions
869990Sdes * are met:
969990Sdes * 1. Redistributions of source code must retain the above copyright
1069990Sdes *    notice, this list of conditions and the following disclaimer
1169990Sdes *    in this position and unchanged.
1269990Sdes * 2. Redistributions in binary form must reproduce the above copyright
1369990Sdes *    notice, this list of conditions and the following disclaimer in the
1469990Sdes *    documentation and/or other materials provided with the distribution.
1569990Sdes *
16181462Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17181462Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18181462Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19181462Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20181462Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21181462Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22181462Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23181462Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24181462Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25181462Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26181462Sdes * SUCH DAMAGE.
2769990Sdes *
2869990Sdes *      $FreeBSD: stable/11/sys/sys/sbuf.h 369620 2021-04-16 19:42:33Z rscheff $
2969990Sdes */
3069990Sdes
3169990Sdes#ifndef _SYS_SBUF_H_
3289121Skbyanc#define	_SYS_SBUF_H_
3369990Sdes
34102227Smike#include <sys/_types.h>
3588950Skbyanc
36212367Smdfstruct sbuf;
37212367Smdftypedef int (sbuf_drain_func)(void *, const char *, int);
38212367Smdf
3969990Sdes/*
4069990Sdes * Structure definition
4169990Sdes */
4269990Sdesstruct sbuf {
4369990Sdes	char		*s_buf;		/* storage buffer */
44212367Smdf	sbuf_drain_func	*s_drain_func;	/* drain function */
45212367Smdf	void		*s_drain_arg;	/* user-supplied drain argument */
46212367Smdf	int		 s_error;	/* current error code */
47269179Sgahr	ssize_t		 s_size;	/* size of storage buffer */
48269179Sgahr	ssize_t		 s_len;		/* current length of string */
4988950Skbyanc#define	SBUF_FIXEDLEN	0x00000000	/* fixed length buffer (default) */
5089121Skbyanc#define	SBUF_AUTOEXTEND	0x00000001	/* automatically extend buffer */
51279992Sian#define	SBUF_INCLUDENUL	0x00000002	/* nulterm byte is counted in len */
52181462Sdes#define	SBUF_USRFLAGMSK	0x0000ffff	/* mask of flags the user may specify */
5389121Skbyanc#define	SBUF_DYNAMIC	0x00010000	/* s_buf must be freed */
5489121Skbyanc#define	SBUF_FINISHED	0x00020000	/* set by sbuf_finish() */
5589121Skbyanc#define	SBUF_DYNSTRUCT	0x00080000	/* sbuf must be freed */
56249377Strociny#define	SBUF_INSECTION	0x00100000	/* set by sbuf_start_section() */
5769990Sdes	int		 s_flags;	/* flags */
58249377Strociny	ssize_t		 s_sect_len;	/* current length of section */
5969990Sdes};
6069990Sdes
61284192Sken#ifndef HD_COLUMN_MASK
62284192Sken#define	HD_COLUMN_MASK	0xff
63284192Sken#define	HD_DELIM_MASK	0xff00
64284192Sken#define	HD_OMIT_COUNT	(1 << 16)
65284192Sken#define	HD_OMIT_HEX	(1 << 17)
66284192Sken#define	HD_OMIT_CHARS	(1 << 18)
67284192Sken#endif /* HD_COLUMN_MASK */
68284192Sken
6974840Sken__BEGIN_DECLS
7069990Sdes/*
7169990Sdes * API functions
7269990Sdes */
7387318Sdesstruct sbuf	*sbuf_new(struct sbuf *, char *, int, int);
74181463Sdes#define		 sbuf_new_auto()				\
75181463Sdes	sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND)
76279992Sianint		 sbuf_get_flags(struct sbuf *);
77279992Sianvoid		 sbuf_clear_flags(struct sbuf *, int);
78279992Sianvoid		 sbuf_set_flags(struct sbuf *, int);
7987318Sdesvoid		 sbuf_clear(struct sbuf *);
80269179Sgahrint		 sbuf_setpos(struct sbuf *, ssize_t);
81131868Sdesint		 sbuf_bcat(struct sbuf *, const void *, size_t);
82131868Sdesint		 sbuf_bcpy(struct sbuf *, const void *, size_t);
8387318Sdesint		 sbuf_cat(struct sbuf *, const char *);
8487318Sdesint		 sbuf_cpy(struct sbuf *, const char *);
85181462Sdesint		 sbuf_printf(struct sbuf *, const char *, ...)
86181462Sdes	__printflike(2, 3);
87181462Sdesint		 sbuf_vprintf(struct sbuf *, const char *, __va_list)
88181462Sdes	__printflike(2, 0);
8987318Sdesint		 sbuf_putc(struct sbuf *, int);
90212367Smdfvoid		 sbuf_set_drain(struct sbuf *, sbuf_drain_func *, void *);
91369620Srscheffint		 sbuf_drain(struct sbuf *);
9287318Sdesint		 sbuf_trim(struct sbuf *);
93221993Sphkint		 sbuf_error(const struct sbuf *);
94212367Smdfint		 sbuf_finish(struct sbuf *);
9587318Sdeschar		*sbuf_data(struct sbuf *);
96221993Sphkssize_t		 sbuf_len(struct sbuf *);
97221993Sphkint		 sbuf_done(const struct sbuf *);
9887318Sdesvoid		 sbuf_delete(struct sbuf *);
99249377Strocinyvoid		 sbuf_start_section(struct sbuf *, ssize_t *);
100249377Strocinyssize_t		 sbuf_end_section(struct sbuf *, ssize_t, size_t, int);
101284192Skenvoid		 sbuf_hexdump(struct sbuf *, const void *, int, const char *,
102284192Sken		     int);
103321107Sngievoid		 sbuf_putbuf(struct sbuf *);
10478077Sdes
10578077Sdes#ifdef _KERNEL
10684097Sdesstruct uio;
10787318Sdesstruct sbuf	*sbuf_uionew(struct sbuf *, struct uio *, int *);
10887318Sdesint		 sbuf_bcopyin(struct sbuf *, const void *, size_t);
10987318Sdesint		 sbuf_copyin(struct sbuf *, const void *, size_t);
11078077Sdes#endif
11174840Sken__END_DECLS
11269990Sdes
11369990Sdes#endif
114