xref: /freebsd/share/man/man9/sbuf.9 (revision e90ede1cc89c112923a99f431ba38b2178e15422)
1863edaecSDag-Erling Smørgrav.\"-
26e18fca1SUlrich Spörlein.\" Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
3863edaecSDag-Erling Smørgrav.\" All rights reserved.
4863edaecSDag-Erling Smørgrav.\"
5863edaecSDag-Erling Smørgrav.\" Redistribution and use in source and binary forms, with or without
6863edaecSDag-Erling Smørgrav.\" modification, are permitted provided that the following conditions
7863edaecSDag-Erling Smørgrav.\" are met:
8863edaecSDag-Erling Smørgrav.\" 1. Redistributions of source code must retain the above copyright
9863edaecSDag-Erling Smørgrav.\"    notice, this list of conditions and the following disclaimer.
10863edaecSDag-Erling Smørgrav.\" 2. Redistributions in binary form must reproduce the above copyright
11863edaecSDag-Erling Smørgrav.\"    notice, this list of conditions and the following disclaimer in the
12863edaecSDag-Erling Smørgrav.\"    documentation and/or other materials provided with the distribution.
13863edaecSDag-Erling Smørgrav.\"
14863edaecSDag-Erling Smørgrav.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15863edaecSDag-Erling Smørgrav.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16863edaecSDag-Erling Smørgrav.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17863edaecSDag-Erling Smørgrav.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18863edaecSDag-Erling Smørgrav.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19863edaecSDag-Erling Smørgrav.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20863edaecSDag-Erling Smørgrav.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21863edaecSDag-Erling Smørgrav.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22863edaecSDag-Erling Smørgrav.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23863edaecSDag-Erling Smørgrav.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24863edaecSDag-Erling Smørgrav.\" SUCH DAMAGE.
25863edaecSDag-Erling Smørgrav.\"
26863edaecSDag-Erling Smørgrav.\" $FreeBSD$
27863edaecSDag-Erling Smørgrav.\"
287beb104dSEnji Cooper.Dd April 5, 2017
295f95f24bSRuslan Ermilov.Dt SBUF 9
303d45e180SRuslan Ermilov.Os
31863edaecSDag-Erling Smørgrav.Sh NAME
3281ae4b8dSRuslan Ermilov.Nm sbuf ,
33863edaecSDag-Erling Smørgrav.Nm sbuf_new ,
3472fba9d7SChristian Brueffer.Nm sbuf_new_auto ,
357c2dd57bSMatthew D Fleming.Nm sbuf_new_for_sysctl ,
36e499b74dSDag-Erling Smørgrav.Nm sbuf_clear ,
37f4d28142SIan Lepore.Nm sbuf_get_flags ,
38f4d28142SIan Lepore.Nm sbuf_set_flags ,
39f4d28142SIan Lepore.Nm sbuf_clear_flags ,
40863edaecSDag-Erling Smørgrav.Nm sbuf_setpos ,
41b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcat ,
42b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcopyin ,
43b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcpy ,
44863edaecSDag-Erling Smørgrav.Nm sbuf_cat ,
45b0def2b5SDag-Erling Smørgrav.Nm sbuf_copyin ,
46863edaecSDag-Erling Smørgrav.Nm sbuf_cpy ,
47863edaecSDag-Erling Smørgrav.Nm sbuf_printf ,
48371c0c84SKelly Yancey.Nm sbuf_vprintf ,
49863edaecSDag-Erling Smørgrav.Nm sbuf_putc ,
504351ba27SMatthew D Fleming.Nm sbuf_set_drain ,
51371c0c84SKelly Yancey.Nm sbuf_trim ,
524d369413SMatthew D Fleming.Nm sbuf_error ,
53863edaecSDag-Erling Smørgrav.Nm sbuf_finish ,
54863edaecSDag-Erling Smørgrav.Nm sbuf_data ,
55863edaecSDag-Erling Smørgrav.Nm sbuf_len ,
56c5f9218bSPoul-Henning Kamp.Nm sbuf_done ,
57f02db7b4SMikolaj Golub.Nm sbuf_delete ,
58f02db7b4SMikolaj Golub.Nm sbuf_start_section ,
595672fac9SKenneth D. Merry.Nm sbuf_end_section ,
60388f3ce6SScott Long.Nm sbuf_hexdump ,
61388f3ce6SScott Long.Nm sbuf_putbuf
626ab7244aSPoul-Henning Kamp.Nd safe string composition
63863edaecSDag-Erling Smørgrav.Sh SYNOPSIS
6432eef9aeSRuslan Ermilov.In sys/types.h
6532eef9aeSRuslan Ermilov.In sys/sbuf.h
664351ba27SMatthew D Fleming.Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ;
674351ba27SMatthew D Fleming.Pp
688a5bc771SBruce Evans.Ft struct sbuf *
69a6848fd5SEnji Cooper.Fo sbuf_new
70a6848fd5SEnji Cooper.Fa "struct sbuf *s"
71a6848fd5SEnji Cooper.Fa "char *buf"
72a6848fd5SEnji Cooper.Fa "int length"
73a6848fd5SEnji Cooper.Fa "int flags"
74a6848fd5SEnji Cooper.Fc
7572fba9d7SChristian Brueffer.Ft struct sbuf *
76a6848fd5SEnji Cooper.Fo sbuf_new_auto
77a6848fd5SEnji Cooper.Fa "void"
78a6848fd5SEnji Cooper.Fc
79e499b74dSDag-Erling Smørgrav.Ft void
80a6848fd5SEnji Cooper.Fo sbuf_clear
81a6848fd5SEnji Cooper.Fa "struct sbuf *s"
82a6848fd5SEnji Cooper.Fc
83863edaecSDag-Erling Smørgrav.Ft int
84a6848fd5SEnji Cooper.Fo sbuf_get_flags
85a6848fd5SEnji Cooper.Fa "struct sbuf *s"
86a6848fd5SEnji Cooper.Fc
87f4d28142SIan Lepore.Ft void
88a6848fd5SEnji Cooper.Fo sbuf_set_flags
89a6848fd5SEnji Cooper.Fa "struct sbuf *s"
90a6848fd5SEnji Cooper.Fa "int flags"
91a6848fd5SEnji Cooper.Fc
92f4d28142SIan Lepore.Ft void
93a6848fd5SEnji Cooper.Fo sbuf_clear_flags
94a6848fd5SEnji Cooper.Fa "struct sbuf *s"
95a6848fd5SEnji Cooper.Fa "int flags"
96a6848fd5SEnji Cooper.Fc
97f4d28142SIan Lepore.Ft int
98a6848fd5SEnji Cooper.Fo sbuf_setpos
99a6848fd5SEnji Cooper.Fa "struct sbuf *s"
100a6848fd5SEnji Cooper.Fa "int pos"
101a6848fd5SEnji Cooper.Fc
102863edaecSDag-Erling Smørgrav.Ft int
103a6848fd5SEnji Cooper.Fo sbuf_bcat
104a6848fd5SEnji Cooper.Fa "struct sbuf *s"
105a6848fd5SEnji Cooper.Fa "const void *buf"
106a6848fd5SEnji Cooper.Fa "size_t len"
107a6848fd5SEnji Cooper.Fc
108b0def2b5SDag-Erling Smørgrav.Ft int
109a6848fd5SEnji Cooper.Fo sbuf_bcpy
110a6848fd5SEnji Cooper.Fa "struct sbuf *s"
111a6848fd5SEnji Cooper.Fa "const void *buf"
112a6848fd5SEnji Cooper.Fa "size_t len"
113a6848fd5SEnji Cooper.Fc
114b0def2b5SDag-Erling Smørgrav.Ft int
115a6848fd5SEnji Cooper.Fo sbuf_cat
116a6848fd5SEnji Cooper.Fa "struct sbuf *s"
117a6848fd5SEnji Cooper.Fa "const char *str"
118a6848fd5SEnji Cooper.Fc
119863edaecSDag-Erling Smørgrav.Ft int
120a6848fd5SEnji Cooper.Fo sbuf_cpy
121a6848fd5SEnji Cooper.Fa "struct sbuf *s"
122a6848fd5SEnji Cooper.Fa "const char *str"
123a6848fd5SEnji Cooper.Fc
124b0def2b5SDag-Erling Smørgrav.Ft int
125a6848fd5SEnji Cooper.Fo sbuf_printf
126a6848fd5SEnji Cooper.Fa "struct sbuf *s"
127a6848fd5SEnji Cooper.Fa "const char *fmt" "..."
128a6848fd5SEnji Cooper.Fc
129863edaecSDag-Erling Smørgrav.Ft int
130a6848fd5SEnji Cooper.Fo sbuf_vprintf
131a6848fd5SEnji Cooper.Fa "struct sbuf *s"
132a6848fd5SEnji Cooper.Fa "const char *fmt"
133a6848fd5SEnji Cooper.Fa "va_list ap"
134a6848fd5SEnji Cooper.Fc
135371c0c84SKelly Yancey.Ft int
136a6848fd5SEnji Cooper.Fo sbuf_putc
137a6848fd5SEnji Cooper.Fa "struct sbuf *s"
138a6848fd5SEnji Cooper.Fa "int c"
139a6848fd5SEnji Cooper.Fc
1404351ba27SMatthew D Fleming.Ft void
141a6848fd5SEnji Cooper.Fo sbuf_set_drain
142a6848fd5SEnji Cooper.Fa "struct sbuf *s"
143a6848fd5SEnji Cooper.Fa "sbuf_drain_func *func"
144a6848fd5SEnji Cooper.Fa "void *arg"
145a6848fd5SEnji Cooper.Fc
146863edaecSDag-Erling Smørgrav.Ft int
147a6848fd5SEnji Cooper.Fo sbuf_trim
148a6848fd5SEnji Cooper.Fa "struct sbuf *s"
149a6848fd5SEnji Cooper.Fc
150371c0c84SKelly Yancey.Ft int
151a6848fd5SEnji Cooper.Fo sbuf_error
152a6848fd5SEnji Cooper.Fa "struct sbuf *s"
153a6848fd5SEnji Cooper.Fc
1544351ba27SMatthew D Fleming.Ft int
155a6848fd5SEnji Cooper.Fo sbuf_finish
156a6848fd5SEnji Cooper.Fa "struct sbuf *s"
157a6848fd5SEnji Cooper.Fc
158863edaecSDag-Erling Smørgrav.Ft char *
159a6848fd5SEnji Cooper.Fo sbuf_data
160a6848fd5SEnji Cooper.Fa "struct sbuf *s"
161a6848fd5SEnji Cooper.Fc
162b397b9b7SMikolaj Golub.Ft ssize_t
163a6848fd5SEnji Cooper.Fo sbuf_len
164a6848fd5SEnji Cooper.Fa "struct sbuf *s"
165a6848fd5SEnji Cooper.Fc
166c5f9218bSPoul-Henning Kamp.Ft int
167a6848fd5SEnji Cooper.Fo sbuf_done
168a6848fd5SEnji Cooper.Fa "struct sbuf *s"
169a6848fd5SEnji Cooper.Fc
170863edaecSDag-Erling Smørgrav.Ft void
171a6848fd5SEnji Cooper.Fo sbuf_delete
172a6848fd5SEnji Cooper.Fa "struct sbuf *s"
173a6848fd5SEnji Cooper.Fc
174f02db7b4SMikolaj Golub.Ft void
175a6848fd5SEnji Cooper.Fo sbuf_start_section
176a6848fd5SEnji Cooper.Fa "struct sbuf *s"
177a6848fd5SEnji Cooper.Fa "ssize_t *old_lenp"
178a6848fd5SEnji Cooper.Fc
179f02db7b4SMikolaj Golub.Ft ssize_t
180a6848fd5SEnji Cooper.Fo sbuf_end_section
181a6848fd5SEnji Cooper.Fa "struct sbuf *s"
182a6848fd5SEnji Cooper.Fa "ssize_t old_len"
183a6848fd5SEnji Cooper.Fa "size_t pad"
184a6848fd5SEnji Cooper.Fa "int c"
185a6848fd5SEnji Cooper.Fc
1865672fac9SKenneth D. Merry.Ft void
1875672fac9SKenneth D. Merry.Fo sbuf_hexdump
1885672fac9SKenneth D. Merry.Fa "struct sbuf *sb"
1895672fac9SKenneth D. Merry.Fa "void *ptr"
1905672fac9SKenneth D. Merry.Fa "int length"
1915672fac9SKenneth D. Merry.Fa "const char *hdr"
1925672fac9SKenneth D. Merry.Fa "int flags"
1935672fac9SKenneth D. Merry.Fc
194388f3ce6SScott Long.Ft void
195a6848fd5SEnji Cooper.Fo sbuf_putbuf
196a6848fd5SEnji Cooper.Fa "struct sbuf *s"
197a6848fd5SEnji Cooper.Fc
198*e90ede1cSEnji Cooper.Fd #ifdef _KERNEL
199*e90ede1cSEnji Cooper.In sys/types.h
200*e90ede1cSEnji Cooper.In sys/sbuf.h
201*e90ede1cSEnji Cooper.Ft int
202*e90ede1cSEnji Cooper.Fo sbuf_bcopyin
203*e90ede1cSEnji Cooper.Fa "struct sbuf *s"
204*e90ede1cSEnji Cooper.Fa "const void *uaddr"
205*e90ede1cSEnji Cooper.Fa "size_t len"
206*e90ede1cSEnji Cooper.Fc
207*e90ede1cSEnji Cooper.Ft int
208*e90ede1cSEnji Cooper.Fo sbuf_copyin
209*e90ede1cSEnji Cooper.Fa "struct sbuf *s"
210*e90ede1cSEnji Cooper.Fa "const void *uaddr"
211*e90ede1cSEnji Cooper.Fa "size_t len"
212*e90ede1cSEnji Cooper.Fc
2137c2dd57bSMatthew D Fleming.In sys/sysctl.h
2147c2dd57bSMatthew D Fleming.Ft struct sbuf *
215a6848fd5SEnji Cooper.Fo sbuf_new_for_sysctl
216a6848fd5SEnji Cooper.Fa "struct sbuf *s"
217a6848fd5SEnji Cooper.Fa "char *buf"
218a6848fd5SEnji Cooper.Fa "int length"
219a6848fd5SEnji Cooper.Fa "struct sysctl_req *req"
220a6848fd5SEnji Cooper.Fc
221*e90ede1cSEnji Cooper.Fd #endif	/* _KERNEL */
222863edaecSDag-Erling Smørgrav.Sh DESCRIPTION
223863edaecSDag-Erling SmørgravThe
22481ae4b8dSRuslan Ermilov.Nm
2256ab7244aSPoul-Henning Kampfamily of functions allows one to safely allocate, compose and
2266ab7244aSPoul-Henning Kamprelease strings in kernel or user space.
2276ab7244aSPoul-Henning Kamp.Pp
228863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures
229863edaecSDag-Erling Smørgravcalled
230863edaecSDag-Erling Smørgrav.Fa sbufs ,
231863edaecSDag-Erling Smørgravdefined in
232fe08efe6SRuslan Ermilov.In sys/sbuf.h .
233863edaecSDag-Erling Smørgrav.Pp
2346ab7244aSPoul-Henning KampAny errors encountered during the allocation or composition of the
2356ab7244aSPoul-Henning Kampstring will be latched in the data structure,
2366ab7244aSPoul-Henning Kampmaking a single error test at the end of the composition
2376ab7244aSPoul-Henning Kampsufficient to determine success or failure of the entire process.
2386ab7244aSPoul-Henning Kamp.Pp
239863edaecSDag-Erling SmørgravThe
240863edaecSDag-Erling Smørgrav.Fn sbuf_new
241863edaecSDag-Erling Smørgravfunction initializes the
242863edaecSDag-Erling Smørgrav.Fa sbuf
243863edaecSDag-Erling Smørgravpointed to by its first argument.
244d6479358SDag-Erling SmørgravIf that pointer is
245d6479358SDag-Erling Smørgrav.Dv NULL ,
246d6479358SDag-Erling Smørgrav.Fn sbuf_new
247d6479358SDag-Erling Smørgravallocates a
248d68b700aSRuslan Ermilov.Vt struct sbuf
249d6479358SDag-Erling Smørgravusing
250d6479358SDag-Erling Smørgrav.Xr malloc 9 .
251863edaecSDag-Erling SmørgravThe
252863edaecSDag-Erling Smørgrav.Fa buf
253863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string;
254863edaecSDag-Erling Smørgravif it is
255863edaecSDag-Erling Smørgrav.Dv NULL ,
256863edaecSDag-Erling Smørgrav.Fn sbuf_new
257863edaecSDag-Erling Smørgravwill allocate one using
258863edaecSDag-Erling Smørgrav.Xr malloc 9 .
259863edaecSDag-Erling SmørgravThe
260863edaecSDag-Erling Smørgrav.Fa length
261371c0c84SKelly Yanceyis the initial size of the storage buffer.
262863edaecSDag-Erling SmørgravThe fourth argument,
263863edaecSDag-Erling Smørgrav.Fa flags ,
264371c0c84SKelly Yanceymay be comprised of the following flags:
2653b0536faSRuslan Ermilov.Bl -tag -width ".Dv SBUF_AUTOEXTEND"
266371c0c84SKelly Yancey.It Dv SBUF_FIXEDLEN
2673b0536faSRuslan ErmilovThe storage buffer is fixed at its initial size.
268371c0c84SKelly YanceyAttempting to extend the sbuf beyond this size results in an overflow condition.
269371c0c84SKelly Yancey.It Dv SBUF_AUTOEXTEND
270371c0c84SKelly YanceyThis indicates that the storage buffer may be extended as necessary, so long
271371c0c84SKelly Yanceyas resources allow, to hold additional data.
272f4d28142SIan Lepore.It Dv SBUF_INCLUDENUL
273f4d28142SIan LeporeThis causes the final nulterm byte to be counted in the length of the data.
274371c0c84SKelly Yancey.El
275863edaecSDag-Erling Smørgrav.Pp
276863edaecSDag-Erling SmørgravNote that if
277863edaecSDag-Erling Smørgrav.Fa buf
278863edaecSDag-Erling Smørgravis not
279863edaecSDag-Erling Smørgrav.Dv NULL ,
280863edaecSDag-Erling Smørgravit must point to an array of at least
281863edaecSDag-Erling Smørgrav.Fa length
282863edaecSDag-Erling Smørgravcharacters.
28391c9d24eSDag-Erling SmørgravThe result of accessing that array directly while it is in use by the
28491c9d24eSDag-Erling Smørgravsbuf is undefined.
28591c9d24eSDag-Erling Smørgrav.Pp
28691c9d24eSDag-Erling SmørgravThe
28772fba9d7SChristian Brueffer.Fn sbuf_new_auto
28872fba9d7SChristian Bruefferfunction is a shortcut for creating a completely dynamic
28972fba9d7SChristian Brueffer.Nm .
29072fba9d7SChristian BruefferIt is the equivalent of calling
29172fba9d7SChristian Brueffer.Fn sbuf_new
29272fba9d7SChristian Bruefferwith values
29372fba9d7SChristian Brueffer.Dv NULL ,
29472fba9d7SChristian Brueffer.Dv NULL ,
29572fba9d7SChristian Brueffer.Dv 0 ,
29672fba9d7SChristian Bruefferand
29772fba9d7SChristian Brueffer.Dv SBUF_AUTOEXTEND .
29872fba9d7SChristian Brueffer.Pp
29972fba9d7SChristian BruefferThe
3007c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl
3017c2dd57bSMatthew D Flemingfunction will set up an sbuf with a drain function to use
3027c2dd57bSMatthew D Fleming.Fn SYSCTL_OUT
3037c2dd57bSMatthew D Flemingwhen the internal buffer fills.
30400f0e671SMatthew D FlemingNote that if the various functions which append to an sbuf are used while
30500f0e671SMatthew D Fleminga non-sleepable lock is held, the user buffer should be wired using
30600f0e671SMatthew D Fleming.Fn sysctl_wire_old_buffer .
3077c2dd57bSMatthew D Fleming.Pp
3087c2dd57bSMatthew D FlemingThe
30991c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
31091c9d24eSDag-Erling Smørgravfunction clears the
31191c9d24eSDag-Erling Smørgrav.Fa sbuf
31291c9d24eSDag-Erling Smørgravand frees any memory allocated for it.
31391c9d24eSDag-Erling SmørgravThere must be a call to
31491c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
31591c9d24eSDag-Erling Smørgravfor every call to
31691c9d24eSDag-Erling Smørgrav.Fn sbuf_new .
31791c9d24eSDag-Erling SmørgravAny attempt to access the sbuf after it has been deleted will fail.
318863edaecSDag-Erling Smørgrav.Pp
319863edaecSDag-Erling SmørgravThe
320e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
321e499b74dSDag-Erling Smørgravfunction invalidates the contents of the
322e499b74dSDag-Erling Smørgrav.Fa sbuf
323e499b74dSDag-Erling Smørgravand resets its position to zero.
324e499b74dSDag-Erling Smørgrav.Pp
325e499b74dSDag-Erling SmørgravThe
326f4d28142SIan Lepore.Fn sbuf_get_flags
327f4d28142SIan Leporefunction returns the current user flags.
328f4d28142SIan LeporeThe
329f4d28142SIan Lepore.Fn sbuf_set_flags
330f4d28142SIan Leporeand
331f4d28142SIan Lepore.Fn sbuf_get_flags
332f4d28142SIan Leporefunctions set or clear one or more user flags, respectively.
333f4d28142SIan LeporeThe user flags are described under the
334f4d28142SIan Lepore.Fn sbuf_new
335f4d28142SIan Leporefunction.
336f4d28142SIan Lepore.Pp
337f4d28142SIan LeporeThe
338863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
339863edaecSDag-Erling Smørgravfunction sets the
340863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's
341371c0c84SKelly Yanceyend position to
342863edaecSDag-Erling Smørgrav.Fa pos ,
343863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the
344863edaecSDag-Erling Smørgravstorage buffer.
345371c0c84SKelly YanceyThis effectively truncates the sbuf at the new position.
346863edaecSDag-Erling Smørgrav.Pp
347863edaecSDag-Erling SmørgravThe
348b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat
349b0def2b5SDag-Erling Smørgravfunction appends the first
350b0def2b5SDag-Erling Smørgrav.Fa len
3514aa90de3SDag-Erling Smørgravbytes from the buffer
3524aa90de3SDag-Erling Smørgrav.Fa buf
353b0def2b5SDag-Erling Smørgravto the
354b0def2b5SDag-Erling Smørgrav.Fa sbuf .
355b0def2b5SDag-Erling Smørgrav.Pp
356b0def2b5SDag-Erling SmørgravThe
357b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin
358b0def2b5SDag-Erling Smørgravfunction copies
359b0def2b5SDag-Erling Smørgrav.Fa len
360b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the
361b0def2b5SDag-Erling Smørgrav.Fa sbuf .
362b0def2b5SDag-Erling Smørgrav.Pp
363b0def2b5SDag-Erling SmørgravThe
364b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy
365b0def2b5SDag-Erling Smørgravfunction replaces the contents of the
366b0def2b5SDag-Erling Smørgrav.Fa sbuf
367b0def2b5SDag-Erling Smørgravwith the first
368b0def2b5SDag-Erling Smørgrav.Fa len
3694aa90de3SDag-Erling Smørgravbytes from the buffer
3704aa90de3SDag-Erling Smørgrav.Fa buf .
371b0def2b5SDag-Erling Smørgrav.Pp
372b0def2b5SDag-Erling SmørgravThe
373863edaecSDag-Erling Smørgrav.Fn sbuf_cat
374b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string
375863edaecSDag-Erling Smørgrav.Fa str
376863edaecSDag-Erling Smørgravto the
377863edaecSDag-Erling Smørgrav.Fa sbuf
378863edaecSDag-Erling Smørgravat the current position.
379863edaecSDag-Erling Smørgrav.Pp
380863edaecSDag-Erling SmørgravThe
3814351ba27SMatthew D Fleming.Fn sbuf_set_drain
3824351ba27SMatthew D Flemingfunction sets a drain function
3834351ba27SMatthew D Fleming.Fa func
3844351ba27SMatthew D Flemingfor the
3854351ba27SMatthew D Fleming.Fa sbuf ,
3864351ba27SMatthew D Flemingand records a pointer
3874351ba27SMatthew D Fleming.Fa arg
3884351ba27SMatthew D Flemingto be passed to the drain on callback.
3894351ba27SMatthew D FlemingThe drain function cannot be changed while
3904351ba27SMatthew D Fleming.Fa sbuf_len
3914351ba27SMatthew D Flemingis non-zero.
3924351ba27SMatthew D Fleming.Pp
3934351ba27SMatthew D FlemingThe registered drain function
3944351ba27SMatthew D Fleming.Vt sbuf_drain_func
3954351ba27SMatthew D Flemingwill be called with the argument
3964351ba27SMatthew D Fleming.Fa arg
3974351ba27SMatthew D Flemingprovided to
3984351ba27SMatthew D Fleming.Fn sbuf_set_drain ,
3994351ba27SMatthew D Fleminga pointer
4004351ba27SMatthew D Fleming.Fa data
4014351ba27SMatthew D Flemingto a byte string that is the contents of the sbuf, and the length
4024351ba27SMatthew D Fleming.Fa len
4034351ba27SMatthew D Flemingof the data.
4044351ba27SMatthew D FlemingIf the drain function exists, it will be called when the sbuf internal
4054351ba27SMatthew D Flemingbuffer is full, or on behalf of
4064351ba27SMatthew D Fleming.Fn sbuf_finish .
4074351ba27SMatthew D FlemingThe drain function may drain some or all of the data, but must drain
4084351ba27SMatthew D Flemingat least 1 byte.
4094351ba27SMatthew D FlemingThe return value from the drain function, if positive, indicates how
4104351ba27SMatthew D Flemingmany bytes were drained.
4114351ba27SMatthew D FlemingIf negative, the return value indicates the negative error code which
4124351ba27SMatthew D Flemingwill be returned from this or a later call to
4134351ba27SMatthew D Fleming.Fn sbuf_finish .
4144351ba27SMatthew D FlemingThe returned drained length cannot be zero.
4154351ba27SMatthew D FlemingTo do unbuffered draining, initialize the sbuf with a two-byte buffer.
4164351ba27SMatthew D FlemingThe drain will be called for every byte added to the sbuf.
4174351ba27SMatthew D FlemingThe
4184351ba27SMatthew D Fleming.Fn sbuf_bcopyin ,
4194351ba27SMatthew D Fleming.Fn sbuf_copyin ,
4204351ba27SMatthew D Fleming.Fn sbuf_trim ,
4214351ba27SMatthew D Flemingand
4224351ba27SMatthew D Fleming.Fn sbuf_data
4234351ba27SMatthew D Flemingfunctions cannot be used on an sbuf with a drain.
4244351ba27SMatthew D Fleming.Pp
4254351ba27SMatthew D FlemingThe
426b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin
427b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland
428b0def2b5SDag-Erling Smørgravaddress into the
429b0def2b5SDag-Erling Smørgrav.Fa sbuf .
430b0def2b5SDag-Erling SmørgravIf the
431b0def2b5SDag-Erling Smørgrav.Fa len
432b0def2b5SDag-Erling Smørgravargument is non-zero, no more than
433b0def2b5SDag-Erling Smørgrav.Fa len
434b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise
435b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the
436b0def2b5SDag-Erling Smørgrav.Fa sbuf ,
437b0def2b5SDag-Erling Smørgravis copied.
438b0def2b5SDag-Erling Smørgrav.Pp
439b0def2b5SDag-Erling SmørgravThe
440863edaecSDag-Erling Smørgrav.Fn sbuf_cpy
441863edaecSDag-Erling Smørgravfunction replaces the contents of the
442863edaecSDag-Erling Smørgrav.Fa sbuf
443b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string
444863edaecSDag-Erling Smørgrav.Fa str .
445863edaecSDag-Erling SmørgravThis is equivalent to calling
446863edaecSDag-Erling Smørgrav.Fn sbuf_cat
447863edaecSDag-Erling Smørgravwith a fresh
448863edaecSDag-Erling Smørgrav.Fa sbuf
449863edaecSDag-Erling Smørgravor one which position has been reset to zero with
450e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
451e499b74dSDag-Erling Smørgravor
452863edaecSDag-Erling Smørgrav.Fn sbuf_setpos .
453863edaecSDag-Erling Smørgrav.Pp
454863edaecSDag-Erling SmørgravThe
455863edaecSDag-Erling Smørgrav.Fn sbuf_printf
456863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed
457863edaecSDag-Erling Smørgravto by
458863edaecSDag-Erling Smørgrav.Fa fmt
459863edaecSDag-Erling Smørgravand appends the resulting string to the
460863edaecSDag-Erling Smørgrav.Fa sbuf
461863edaecSDag-Erling Smørgravat the current position.
462863edaecSDag-Erling Smørgrav.Pp
463863edaecSDag-Erling SmørgravThe
464371c0c84SKelly Yancey.Fn sbuf_vprintf
465371c0c84SKelly Yanceyfunction behaves the same as
466371c0c84SKelly Yancey.Fn sbuf_printf
467371c0c84SKelly Yanceyexcept that the arguments are obtained from the variable-length argument list
468371c0c84SKelly Yancey.Fa ap .
469371c0c84SKelly Yancey.Pp
470371c0c84SKelly YanceyThe
471863edaecSDag-Erling Smørgrav.Fn sbuf_putc
472863edaecSDag-Erling Smørgravfunction appends the character
473863edaecSDag-Erling Smørgrav.Fa c
474863edaecSDag-Erling Smørgravto the
475863edaecSDag-Erling Smørgrav.Fa sbuf
476863edaecSDag-Erling Smørgravat the current position.
477863edaecSDag-Erling Smørgrav.Pp
478863edaecSDag-Erling SmørgravThe
479371c0c84SKelly Yancey.Fn sbuf_trim
480371c0c84SKelly Yanceyfunction removes trailing whitespace from the
481371c0c84SKelly Yancey.Fa sbuf .
482371c0c84SKelly Yancey.Pp
483371c0c84SKelly YanceyThe
4844d369413SMatthew D Fleming.Fn sbuf_error
4854d369413SMatthew D Flemingfunction returns any error value that the
4864d369413SMatthew D Fleming.Fa sbuf
4874d369413SMatthew D Flemingmay have accumulated, either from the drain function, or ENOMEM if the
488e499b74dSDag-Erling Smørgrav.Fa sbuf
489e499b74dSDag-Erling Smørgravoverflowed.
4904d369413SMatthew D FlemingThis function is generally not needed and instead the error code from
4914d369413SMatthew D Fleming.Fn sbuf_finish
4924d369413SMatthew D Flemingis the preferred way to discover whether an sbuf had an error.
493e499b74dSDag-Erling Smørgrav.Pp
494e499b74dSDag-Erling SmørgravThe
495863edaecSDag-Erling Smørgrav.Fn sbuf_finish
4964351ba27SMatthew D Flemingfunction will call the attached drain function if one exists until all
4974351ba27SMatthew D Flemingthe data in the
498863edaecSDag-Erling Smørgrav.Fa sbuf
4994351ba27SMatthew D Flemingis flushed.
5004351ba27SMatthew D FlemingIf there is no attached drain,
5014351ba27SMatthew D Fleming.Fn sbuf_finish
5024351ba27SMatthew D FlemingNUL-terminates the
5034351ba27SMatthew D Fleming.Fa sbuf .
5044351ba27SMatthew D FlemingIn either case it marks the
5054351ba27SMatthew D Fleming.Fa sbuf
5064351ba27SMatthew D Flemingas finished, which means that it may no longer be modified using
507863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
508863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
5093393f8daSKenneth D. Merry.Fn sbuf_cpy ,
510863edaecSDag-Erling Smørgrav.Fn sbuf_printf
511863edaecSDag-Erling Smørgravor
512db960cf0SMatthew D Fleming.Fn sbuf_putc ,
513db960cf0SMatthew D Fleminguntil
514db960cf0SMatthew D Fleming.Fn sbuf_clear
515db960cf0SMatthew D Flemingis used to reset the sbuf.
516863edaecSDag-Erling Smørgrav.Pp
517863edaecSDag-Erling SmørgravThe
518863edaecSDag-Erling Smørgrav.Fn sbuf_data
5194351ba27SMatthew D Flemingfunction returns the actual string;
520e499b74dSDag-Erling Smørgrav.Fn sbuf_data
521e499b74dSDag-Erling Smørgravonly works on a finished
522863edaecSDag-Erling Smørgrav.Fa sbuf .
5234351ba27SMatthew D FlemingThe
524c0825cd5SJaakko Heinonen.Fn sbuf_len
525c0825cd5SJaakko Heinonenfunction returns the length of the string.
5264351ba27SMatthew D FlemingFor an
5274351ba27SMatthew D Fleming.Fa sbuf
5284351ba27SMatthew D Flemingwith an attached drain,
5294351ba27SMatthew D Fleming.Fn sbuf_len
5304351ba27SMatthew D Flemingreturns the length of the un-drained data.
5314351ba27SMatthew D Fleming.Fn sbuf_done
5324351ba27SMatthew D Flemingreturns non-zero if the
5334351ba27SMatthew D Fleming.Fa sbuf
5344351ba27SMatthew D Flemingis finished.
535f02db7b4SMikolaj Golub.Pp
536f02db7b4SMikolaj GolubThe
537f02db7b4SMikolaj Golub.Fn sbuf_start_section
538f02db7b4SMikolaj Goluband
539f02db7b4SMikolaj Golub.Fn sbuf_end_section
540f02db7b4SMikolaj Golubfunctions may be used for automatic section alignment.
541f02db7b4SMikolaj GolubThe arguments
542f02db7b4SMikolaj Golub.Fa pad
543f02db7b4SMikolaj Goluband
544f02db7b4SMikolaj Golub.Fa c
545f02db7b4SMikolaj Golubspecify the padding size and a character used for padding.
546f02db7b4SMikolaj GolubThe arguments
547f02db7b4SMikolaj Golub.Fa old_lenp
548f02db7b4SMikolaj Goluband
549f02db7b4SMikolaj Golub.Fa old_len
550f02db7b4SMikolaj Golubare to save and restore the current section length when nested sections
551f02db7b4SMikolaj Golubare used.
552f02db7b4SMikolaj GolubFor the top level section
553f02db7b4SMikolaj Golub.Dv NULL
554f02db7b4SMikolaj Goluband \-1 can be specified for
555f02db7b4SMikolaj Golub.Fa old_lenp
556f02db7b4SMikolaj Goluband
557f02db7b4SMikolaj Golub.Fa old_len
558f02db7b4SMikolaj Golubrespectively.
5595672fac9SKenneth D. Merry.Pp
5605672fac9SKenneth D. MerryThe
5615672fac9SKenneth D. Merry.Fn sbuf_hexdump
5625672fac9SKenneth D. Merryfunction prints an array of bytes to the supplied sbuf, along with an ASCII
5635672fac9SKenneth D. Merryrepresentation of the bytes if possible.
5645672fac9SKenneth D. MerrySee the
5655672fac9SKenneth D. Merry.Xr hexdump 3
5665672fac9SKenneth D. Merryman page for more details on the interface.
567388f3ce6SScott Long.Pp
568388f3ce6SScott LongThe
569388f3ce6SScott Long.Fn sbuf_putbuf
570388f3ce6SScott Longfunction printfs the sbuf to stdout if in userland, and to the console
571388f3ce6SScott Longand log if in the kernel.
572388f3ce6SScott LongIt does not drain the buffer or update any pointers.
573863edaecSDag-Erling Smørgrav.Sh NOTES
574863edaecSDag-Erling SmørgravIf an operation caused an
575863edaecSDag-Erling Smørgrav.Fa sbuf
576e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the
577e499b74dSDag-Erling Smørgrav.Fa sbuf
578e499b74dSDag-Erling Smørgravis finished using
579e499b74dSDag-Erling Smørgrav.Fn sbuf_finish
580e499b74dSDag-Erling Smørgravor reset using
581e499b74dSDag-Erling Smørgrav.Fn sbuf_clear ,
582e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the
583e499b74dSDag-Erling Smørgravsize of its storage buffer using
584863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
585863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using
586863edaecSDag-Erling Smørgrav.Fn sbuf_cpy .
5874351ba27SMatthew D Fleming.Pp
5884351ba27SMatthew D FlemingDrains in user-space will not always function as indicated.
5894351ba27SMatthew D FlemingWhile the drain function will be called immediately on overflow from
5904351ba27SMatthew D Flemingthe
5914351ba27SMatthew D Fleming.Fa sbuf_putc ,
5924351ba27SMatthew D Fleming.Fa sbuf_bcat ,
5934351ba27SMatthew D Fleming.Fa sbuf_cat
5944351ba27SMatthew D Flemingfunctions,
5954351ba27SMatthew D Fleming.Fa sbuf_printf
5964351ba27SMatthew D Flemingand
5974351ba27SMatthew D Fleming.Fa sbuf_vprintf
5984351ba27SMatthew D Flemingcurrently have no way to determine whether there will be an overflow
5994351ba27SMatthew D Fleminguntil after it occurs, and cannot do a partial expansion of the format
6004351ba27SMatthew D Flemingstring.
6014351ba27SMatthew D FlemingThus when using libsbuf the buffer may be extended to allow completion
6024351ba27SMatthew D Flemingof a single printf call, even though a drain is attached.
603863edaecSDag-Erling Smørgrav.Sh RETURN VALUES
60481ae4b8dSRuslan ErmilovThe
605863edaecSDag-Erling Smørgrav.Fn sbuf_new
60681ae4b8dSRuslan Ermilovfunction returns
607d6479358SDag-Erling Smørgrav.Dv NULL
608d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new
609d6479358SDag-Erling Smørgrav.Fa sbuf
610863edaecSDag-Erling Smørgravotherwise.
611863edaecSDag-Erling Smørgrav.Pp
61281ae4b8dSRuslan ErmilovThe
613863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
61481ae4b8dSRuslan Ermilovfunction returns \-1 if
615863edaecSDag-Erling Smørgrav.Fa pos
616863edaecSDag-Erling Smørgravwas invalid, and zero otherwise.
617863edaecSDag-Erling Smørgrav.Pp
61881ae4b8dSRuslan ErmilovThe
619863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
620863edaecSDag-Erling Smørgrav.Fn sbuf_cpy ,
621863edaecSDag-Erling Smørgrav.Fn sbuf_printf ,
622371c0c84SKelly Yancey.Fn sbuf_putc ,
623863edaecSDag-Erling Smørgravand
624371c0c84SKelly Yancey.Fn sbuf_trim
62581ae4b8dSRuslan Ermilovfunctions
626863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise.
627863edaecSDag-Erling Smørgrav.Pp
62881ae4b8dSRuslan ErmilovThe
6294d369413SMatthew D Fleming.Fn sbuf_error
6304d369413SMatthew D Flemingfunction returns a non-zero value if the buffer has an overflow or
6314d369413SMatthew D Flemingdrain error, and zero otherwise.
632e499b74dSDag-Erling Smørgrav.Pp
63381ae4b8dSRuslan ErmilovThe
634863edaecSDag-Erling Smørgrav.Fn sbuf_len
635569dd0f3SJaakko Heinonenfunction returns \-1 if the buffer overflowed.
63649091c48SPoul-Henning Kamp.Pp
63781ae4b8dSRuslan ErmilovThe
63849091c48SPoul-Henning Kamp.Fn sbuf_copyin
63981ae4b8dSRuslan Ermilovfunction
64049091c48SPoul-Henning Kampreturns \-1 if copying string from userland failed, and number of bytes
64149091c48SPoul-Henning Kampcopied otherwise.
6426ab7244aSPoul-Henning Kamp.Pp
6434351ba27SMatthew D FlemingThe
644f02db7b4SMikolaj Golub.Fn sbuf_end_section
645f02db7b4SMikolaj Golubfunction returns the section length or \-1 if the buffer has an error.
646f02db7b4SMikolaj Golub.Pp
647f02db7b4SMikolaj GolubThe
6486ab7244aSPoul-Henning Kamp.Fn sbuf_finish 9
6496ab7244aSPoul-Henning Kampfunction (the kernel version) returns ENOMEM if the sbuf overflowed before
6506ab7244aSPoul-Henning Kampbeing finished,
6514351ba27SMatthew D Flemingor returns the error code from the drain if one is attached.
6526ab7244aSPoul-Henning Kamp.Pp
6536ab7244aSPoul-Henning KampThe
6546ab7244aSPoul-Henning Kamp.Fn sbuf_finish 3
6556ab7244aSPoul-Henning Kampfunction (the userland version)
6566ab7244aSPoul-Henning Kampwill return zero for success and \-1 and set errno on error.
6576ab7244aSPoul-Henning Kamp.Sh EXAMPLES
6586ab7244aSPoul-Henning Kamp.Bd -literal -compact
6597beb104dSEnji Cooper#include <sys/types.h>
6606ab7244aSPoul-Henning Kamp#include <sys/sbuf.h>
6616ab7244aSPoul-Henning Kamp
6626ab7244aSPoul-Henning Kampstruct sbuf *sb;
6636ab7244aSPoul-Henning Kamp
6646ab7244aSPoul-Henning Kampsb = sbuf_new_auto();
665c920aa7aSPoul-Henning Kampsbuf_cat(sb, "Customers found:\en");
6666ab7244aSPoul-Henning KampTAILQ_FOREACH(foo, &foolist, list) {
667c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "   %4d %s\en", foo->index, foo->name);
668c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Address: %s\en", foo->address);
669c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Zip: %s\en", foo->zipcode);
6706ab7244aSPoul-Henning Kamp}
671c5a0813dSJaakko Heinonenif (sbuf_finish(sb) != 0) /* Check for any and all errors */
6726ab7244aSPoul-Henning Kamp	err(1, "Could not generate message");
6736ab7244aSPoul-Henning Kamptransmit_msg(sbuf_data(sb), sbuf_len(sb));
6746ab7244aSPoul-Henning Kampsbuf_delete(sb);
6756ab7244aSPoul-Henning Kamp.Ed
676863edaecSDag-Erling Smørgrav.Sh SEE ALSO
6775672fac9SKenneth D. Merry.Xr hexdump 3 ,
678863edaecSDag-Erling Smørgrav.Xr printf 3 ,
679863edaecSDag-Erling Smørgrav.Xr strcat 3 ,
680d68b700aSRuslan Ermilov.Xr strcpy 3 ,
681d68b700aSRuslan Ermilov.Xr copyin 9 ,
682e05f3134SAndrew R. Reiter.Xr copyinstr 9 ,
683e05f3134SAndrew R. Reiter.Xr printf 9
684863edaecSDag-Erling Smørgrav.Sh HISTORY
685863edaecSDag-Erling SmørgravThe
68681ae4b8dSRuslan Ermilov.Nm
687863edaecSDag-Erling Smørgravfamily of functions first appeared in
68822cbe5e3SDag-Erling Smørgrav.Fx 4.4 .
689863edaecSDag-Erling Smørgrav.Sh AUTHORS
690863edaecSDag-Erling Smørgrav.An -nosplit
691863edaecSDag-Erling SmørgravThe
69281ae4b8dSRuslan Ermilov.Nm
693863edaecSDag-Erling Smørgravfamily of functions was designed by
6948a7314fcSBaptiste Daroussin.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
695863edaecSDag-Erling Smørgravand implemented by
6968a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
697e499b74dSDag-Erling SmørgravAdditional improvements were suggested by
6988a7314fcSBaptiste Daroussin.An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org .
699371c0c84SKelly YanceyAuto-extend support added by
7008a7314fcSBaptiste Daroussin.An Kelly Yancey Aq Mt kbyanc@FreeBSD.org .
7014351ba27SMatthew D FlemingDrain functionality added by
7028a7314fcSBaptiste Daroussin.An Matthew Fleming Aq Mt mdf@FreeBSD.org .
703863edaecSDag-Erling Smørgrav.Pp
704863edaecSDag-Erling SmørgravThis manual page was written by
7058a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
706