xref: /freebsd/share/man/man9/sbuf.9 (revision 7beb104d302348d0e7bf34615df6c02b10b81659)
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.\"
28*7beb104dSEnji 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 *
69e499b74dSDag-Erling Smørgrav.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
7072fba9d7SChristian Brueffer.Ft struct sbuf *
7172fba9d7SChristian Brueffer.Fn sbuf_new_auto
72e499b74dSDag-Erling Smørgrav.Ft void
73e499b74dSDag-Erling Smørgrav.Fn sbuf_clear "struct sbuf *s"
74863edaecSDag-Erling Smørgrav.Ft int
75f4d28142SIan Lepore.Fn sbuf_get_flags "struct sbuf *s"
76f4d28142SIan Lepore.Ft void
77f4d28142SIan Lepore.Fn sbuf_set_flags "struct sbuf *s" "int flags"
78f4d28142SIan Lepore.Ft void
79f4d28142SIan Lepore.Fn sbuf_clear_flags "struct sbuf *s" "int flags"
80f4d28142SIan Lepore.Ft int
81e499b74dSDag-Erling Smørgrav.Fn sbuf_setpos "struct sbuf *s" "int pos"
82863edaecSDag-Erling Smørgrav.Ft int
834aa90de3SDag-Erling Smørgrav.Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len"
84b0def2b5SDag-Erling Smørgrav.Ft int
85b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len"
86b0def2b5SDag-Erling Smørgrav.Ft int
874aa90de3SDag-Erling Smørgrav.Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len"
88b0def2b5SDag-Erling Smørgrav.Ft int
893393f8daSKenneth D. Merry.Fn sbuf_cat "struct sbuf *s" "const char *str"
90863edaecSDag-Erling Smørgrav.Ft int
91b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len"
92b0def2b5SDag-Erling Smørgrav.Ft int
933393f8daSKenneth D. Merry.Fn sbuf_cpy "struct sbuf *s" "const char *str"
94863edaecSDag-Erling Smørgrav.Ft int
958cce2ebaSDag-Erling Smørgrav.Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..."
96863edaecSDag-Erling Smørgrav.Ft int
97371c0c84SKelly Yancey.Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "va_list ap"
98371c0c84SKelly Yancey.Ft int
99863edaecSDag-Erling Smørgrav.Fn sbuf_putc "struct sbuf *s" "int c"
1004351ba27SMatthew D Fleming.Ft void
1014351ba27SMatthew D Fleming.Fn sbuf_set_drain "struct sbuf *s" "sbuf_drain_func *func" "void *arg"
102863edaecSDag-Erling Smørgrav.Ft int
103371c0c84SKelly Yancey.Fn sbuf_trim "struct sbuf *s"
104371c0c84SKelly Yancey.Ft int
1054d369413SMatthew D Fleming.Fn sbuf_error "struct sbuf *s"
1064351ba27SMatthew D Fleming.Ft int
107863edaecSDag-Erling Smørgrav.Fn sbuf_finish "struct sbuf *s"
108863edaecSDag-Erling Smørgrav.Ft char *
109863edaecSDag-Erling Smørgrav.Fn sbuf_data "struct sbuf *s"
110b397b9b7SMikolaj Golub.Ft ssize_t
111863edaecSDag-Erling Smørgrav.Fn sbuf_len "struct sbuf *s"
112c5f9218bSPoul-Henning Kamp.Ft int
113c5f9218bSPoul-Henning Kamp.Fn sbuf_done "struct sbuf *s"
114863edaecSDag-Erling Smørgrav.Ft void
115863edaecSDag-Erling Smørgrav.Fn sbuf_delete "struct sbuf *s"
116f02db7b4SMikolaj Golub.Ft void
117f02db7b4SMikolaj Golub.Fn sbuf_start_section "struct sbuf *s" "ssize_t *old_lenp"
118f02db7b4SMikolaj Golub.Ft ssize_t
119f02db7b4SMikolaj Golub.Fn sbuf_end_section "struct sbuf *s" "ssize_t old_len" "size_t pad" "int c"
1205672fac9SKenneth D. Merry.Ft void
1215672fac9SKenneth D. Merry.Fo sbuf_hexdump
1225672fac9SKenneth D. Merry.Fa "struct sbuf *sb"
1235672fac9SKenneth D. Merry.Fa "void *ptr"
1245672fac9SKenneth D. Merry.Fa "int length"
1255672fac9SKenneth D. Merry.Fa "const char *hdr"
1265672fac9SKenneth D. Merry.Fa "int flags"
1275672fac9SKenneth D. Merry.Fc
128388f3ce6SScott Long.Ft void
129388f3ce6SScott Long.Fn sbuf_putbuf "struct sbuf *s"
1307c2dd57bSMatthew D Fleming.In sys/sysctl.h
1317c2dd57bSMatthew D Fleming.Ft struct sbuf *
1327c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl "struct sbuf *s" "char *buf" "int length" "struct sysctl_req *req"
133863edaecSDag-Erling Smørgrav.Sh DESCRIPTION
134863edaecSDag-Erling SmørgravThe
13581ae4b8dSRuslan Ermilov.Nm
1366ab7244aSPoul-Henning Kampfamily of functions allows one to safely allocate, compose and
1376ab7244aSPoul-Henning Kamprelease strings in kernel or user space.
1386ab7244aSPoul-Henning Kamp.Pp
139863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures
140863edaecSDag-Erling Smørgravcalled
141863edaecSDag-Erling Smørgrav.Fa sbufs ,
142863edaecSDag-Erling Smørgravdefined in
143fe08efe6SRuslan Ermilov.In sys/sbuf.h .
144863edaecSDag-Erling Smørgrav.Pp
1456ab7244aSPoul-Henning KampAny errors encountered during the allocation or composition of the
1466ab7244aSPoul-Henning Kampstring will be latched in the data structure,
1476ab7244aSPoul-Henning Kampmaking a single error test at the end of the composition
1486ab7244aSPoul-Henning Kampsufficient to determine success or failure of the entire process.
1496ab7244aSPoul-Henning Kamp.Pp
150863edaecSDag-Erling SmørgravThe
151863edaecSDag-Erling Smørgrav.Fn sbuf_new
152863edaecSDag-Erling Smørgravfunction initializes the
153863edaecSDag-Erling Smørgrav.Fa sbuf
154863edaecSDag-Erling Smørgravpointed to by its first argument.
155d6479358SDag-Erling SmørgravIf that pointer is
156d6479358SDag-Erling Smørgrav.Dv NULL ,
157d6479358SDag-Erling Smørgrav.Fn sbuf_new
158d6479358SDag-Erling Smørgravallocates a
159d68b700aSRuslan Ermilov.Vt struct sbuf
160d6479358SDag-Erling Smørgravusing
161d6479358SDag-Erling Smørgrav.Xr malloc 9 .
162863edaecSDag-Erling SmørgravThe
163863edaecSDag-Erling Smørgrav.Fa buf
164863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string;
165863edaecSDag-Erling Smørgravif it is
166863edaecSDag-Erling Smørgrav.Dv NULL ,
167863edaecSDag-Erling Smørgrav.Fn sbuf_new
168863edaecSDag-Erling Smørgravwill allocate one using
169863edaecSDag-Erling Smørgrav.Xr malloc 9 .
170863edaecSDag-Erling SmørgravThe
171863edaecSDag-Erling Smørgrav.Fa length
172371c0c84SKelly Yanceyis the initial size of the storage buffer.
173863edaecSDag-Erling SmørgravThe fourth argument,
174863edaecSDag-Erling Smørgrav.Fa flags ,
175371c0c84SKelly Yanceymay be comprised of the following flags:
1763b0536faSRuslan Ermilov.Bl -tag -width ".Dv SBUF_AUTOEXTEND"
177371c0c84SKelly Yancey.It Dv SBUF_FIXEDLEN
1783b0536faSRuslan ErmilovThe storage buffer is fixed at its initial size.
179371c0c84SKelly YanceyAttempting to extend the sbuf beyond this size results in an overflow condition.
180371c0c84SKelly Yancey.It Dv SBUF_AUTOEXTEND
181371c0c84SKelly YanceyThis indicates that the storage buffer may be extended as necessary, so long
182371c0c84SKelly Yanceyas resources allow, to hold additional data.
183f4d28142SIan Lepore.It Dv SBUF_INCLUDENUL
184f4d28142SIan LeporeThis causes the final nulterm byte to be counted in the length of the data.
185371c0c84SKelly Yancey.El
186863edaecSDag-Erling Smørgrav.Pp
187863edaecSDag-Erling SmørgravNote that if
188863edaecSDag-Erling Smørgrav.Fa buf
189863edaecSDag-Erling Smørgravis not
190863edaecSDag-Erling Smørgrav.Dv NULL ,
191863edaecSDag-Erling Smørgravit must point to an array of at least
192863edaecSDag-Erling Smørgrav.Fa length
193863edaecSDag-Erling Smørgravcharacters.
19491c9d24eSDag-Erling SmørgravThe result of accessing that array directly while it is in use by the
19591c9d24eSDag-Erling Smørgravsbuf is undefined.
19691c9d24eSDag-Erling Smørgrav.Pp
19791c9d24eSDag-Erling SmørgravThe
19872fba9d7SChristian Brueffer.Fn sbuf_new_auto
19972fba9d7SChristian Bruefferfunction is a shortcut for creating a completely dynamic
20072fba9d7SChristian Brueffer.Nm .
20172fba9d7SChristian BruefferIt is the equivalent of calling
20272fba9d7SChristian Brueffer.Fn sbuf_new
20372fba9d7SChristian Bruefferwith values
20472fba9d7SChristian Brueffer.Dv NULL ,
20572fba9d7SChristian Brueffer.Dv NULL ,
20672fba9d7SChristian Brueffer.Dv 0 ,
20772fba9d7SChristian Bruefferand
20872fba9d7SChristian Brueffer.Dv SBUF_AUTOEXTEND .
20972fba9d7SChristian Brueffer.Pp
21072fba9d7SChristian BruefferThe
2117c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl
2127c2dd57bSMatthew D Flemingfunction will set up an sbuf with a drain function to use
2137c2dd57bSMatthew D Fleming.Fn SYSCTL_OUT
2147c2dd57bSMatthew D Flemingwhen the internal buffer fills.
21500f0e671SMatthew D FlemingNote that if the various functions which append to an sbuf are used while
21600f0e671SMatthew D Fleminga non-sleepable lock is held, the user buffer should be wired using
21700f0e671SMatthew D Fleming.Fn sysctl_wire_old_buffer .
2187c2dd57bSMatthew D Fleming.Pp
2197c2dd57bSMatthew D FlemingThe
22091c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
22191c9d24eSDag-Erling Smørgravfunction clears the
22291c9d24eSDag-Erling Smørgrav.Fa sbuf
22391c9d24eSDag-Erling Smørgravand frees any memory allocated for it.
22491c9d24eSDag-Erling SmørgravThere must be a call to
22591c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
22691c9d24eSDag-Erling Smørgravfor every call to
22791c9d24eSDag-Erling Smørgrav.Fn sbuf_new .
22891c9d24eSDag-Erling SmørgravAny attempt to access the sbuf after it has been deleted will fail.
229863edaecSDag-Erling Smørgrav.Pp
230863edaecSDag-Erling SmørgravThe
231e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
232e499b74dSDag-Erling Smørgravfunction invalidates the contents of the
233e499b74dSDag-Erling Smørgrav.Fa sbuf
234e499b74dSDag-Erling Smørgravand resets its position to zero.
235e499b74dSDag-Erling Smørgrav.Pp
236e499b74dSDag-Erling SmørgravThe
237f4d28142SIan Lepore.Fn sbuf_get_flags
238f4d28142SIan Leporefunction returns the current user flags.
239f4d28142SIan LeporeThe
240f4d28142SIan Lepore.Fn sbuf_set_flags
241f4d28142SIan Leporeand
242f4d28142SIan Lepore.Fn sbuf_get_flags
243f4d28142SIan Leporefunctions set or clear one or more user flags, respectively.
244f4d28142SIan LeporeThe user flags are described under the
245f4d28142SIan Lepore.Fn sbuf_new
246f4d28142SIan Leporefunction.
247f4d28142SIan Lepore.Pp
248f4d28142SIan LeporeThe
249863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
250863edaecSDag-Erling Smørgravfunction sets the
251863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's
252371c0c84SKelly Yanceyend position to
253863edaecSDag-Erling Smørgrav.Fa pos ,
254863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the
255863edaecSDag-Erling Smørgravstorage buffer.
256371c0c84SKelly YanceyThis effectively truncates the sbuf at the new position.
257863edaecSDag-Erling Smørgrav.Pp
258863edaecSDag-Erling SmørgravThe
259b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat
260b0def2b5SDag-Erling Smørgravfunction appends the first
261b0def2b5SDag-Erling Smørgrav.Fa len
2624aa90de3SDag-Erling Smørgravbytes from the buffer
2634aa90de3SDag-Erling Smørgrav.Fa buf
264b0def2b5SDag-Erling Smørgravto the
265b0def2b5SDag-Erling Smørgrav.Fa sbuf .
266b0def2b5SDag-Erling Smørgrav.Pp
267b0def2b5SDag-Erling SmørgravThe
268b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin
269b0def2b5SDag-Erling Smørgravfunction copies
270b0def2b5SDag-Erling Smørgrav.Fa len
271b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the
272b0def2b5SDag-Erling Smørgrav.Fa sbuf .
273b0def2b5SDag-Erling Smørgrav.Pp
274b0def2b5SDag-Erling SmørgravThe
275b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy
276b0def2b5SDag-Erling Smørgravfunction replaces the contents of the
277b0def2b5SDag-Erling Smørgrav.Fa sbuf
278b0def2b5SDag-Erling Smørgravwith the first
279b0def2b5SDag-Erling Smørgrav.Fa len
2804aa90de3SDag-Erling Smørgravbytes from the buffer
2814aa90de3SDag-Erling Smørgrav.Fa buf .
282b0def2b5SDag-Erling Smørgrav.Pp
283b0def2b5SDag-Erling SmørgravThe
284863edaecSDag-Erling Smørgrav.Fn sbuf_cat
285b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string
286863edaecSDag-Erling Smørgrav.Fa str
287863edaecSDag-Erling Smørgravto the
288863edaecSDag-Erling Smørgrav.Fa sbuf
289863edaecSDag-Erling Smørgravat the current position.
290863edaecSDag-Erling Smørgrav.Pp
291863edaecSDag-Erling SmørgravThe
2924351ba27SMatthew D Fleming.Fn sbuf_set_drain
2934351ba27SMatthew D Flemingfunction sets a drain function
2944351ba27SMatthew D Fleming.Fa func
2954351ba27SMatthew D Flemingfor the
2964351ba27SMatthew D Fleming.Fa sbuf ,
2974351ba27SMatthew D Flemingand records a pointer
2984351ba27SMatthew D Fleming.Fa arg
2994351ba27SMatthew D Flemingto be passed to the drain on callback.
3004351ba27SMatthew D FlemingThe drain function cannot be changed while
3014351ba27SMatthew D Fleming.Fa sbuf_len
3024351ba27SMatthew D Flemingis non-zero.
3034351ba27SMatthew D Fleming.Pp
3044351ba27SMatthew D FlemingThe registered drain function
3054351ba27SMatthew D Fleming.Vt sbuf_drain_func
3064351ba27SMatthew D Flemingwill be called with the argument
3074351ba27SMatthew D Fleming.Fa arg
3084351ba27SMatthew D Flemingprovided to
3094351ba27SMatthew D Fleming.Fn sbuf_set_drain ,
3104351ba27SMatthew D Fleminga pointer
3114351ba27SMatthew D Fleming.Fa data
3124351ba27SMatthew D Flemingto a byte string that is the contents of the sbuf, and the length
3134351ba27SMatthew D Fleming.Fa len
3144351ba27SMatthew D Flemingof the data.
3154351ba27SMatthew D FlemingIf the drain function exists, it will be called when the sbuf internal
3164351ba27SMatthew D Flemingbuffer is full, or on behalf of
3174351ba27SMatthew D Fleming.Fn sbuf_finish .
3184351ba27SMatthew D FlemingThe drain function may drain some or all of the data, but must drain
3194351ba27SMatthew D Flemingat least 1 byte.
3204351ba27SMatthew D FlemingThe return value from the drain function, if positive, indicates how
3214351ba27SMatthew D Flemingmany bytes were drained.
3224351ba27SMatthew D FlemingIf negative, the return value indicates the negative error code which
3234351ba27SMatthew D Flemingwill be returned from this or a later call to
3244351ba27SMatthew D Fleming.Fn sbuf_finish .
3254351ba27SMatthew D FlemingThe returned drained length cannot be zero.
3264351ba27SMatthew D FlemingTo do unbuffered draining, initialize the sbuf with a two-byte buffer.
3274351ba27SMatthew D FlemingThe drain will be called for every byte added to the sbuf.
3284351ba27SMatthew D FlemingThe
3294351ba27SMatthew D Fleming.Fn sbuf_bcopyin ,
3304351ba27SMatthew D Fleming.Fn sbuf_copyin ,
3314351ba27SMatthew D Fleming.Fn sbuf_trim ,
3324351ba27SMatthew D Flemingand
3334351ba27SMatthew D Fleming.Fn sbuf_data
3344351ba27SMatthew D Flemingfunctions cannot be used on an sbuf with a drain.
3354351ba27SMatthew D Fleming.Pp
3364351ba27SMatthew D FlemingThe
337b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin
338b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland
339b0def2b5SDag-Erling Smørgravaddress into the
340b0def2b5SDag-Erling Smørgrav.Fa sbuf .
341b0def2b5SDag-Erling SmørgravIf the
342b0def2b5SDag-Erling Smørgrav.Fa len
343b0def2b5SDag-Erling Smørgravargument is non-zero, no more than
344b0def2b5SDag-Erling Smørgrav.Fa len
345b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise
346b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the
347b0def2b5SDag-Erling Smørgrav.Fa sbuf ,
348b0def2b5SDag-Erling Smørgravis copied.
349b0def2b5SDag-Erling Smørgrav.Pp
350b0def2b5SDag-Erling SmørgravThe
351863edaecSDag-Erling Smørgrav.Fn sbuf_cpy
352863edaecSDag-Erling Smørgravfunction replaces the contents of the
353863edaecSDag-Erling Smørgrav.Fa sbuf
354b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string
355863edaecSDag-Erling Smørgrav.Fa str .
356863edaecSDag-Erling SmørgravThis is equivalent to calling
357863edaecSDag-Erling Smørgrav.Fn sbuf_cat
358863edaecSDag-Erling Smørgravwith a fresh
359863edaecSDag-Erling Smørgrav.Fa sbuf
360863edaecSDag-Erling Smørgravor one which position has been reset to zero with
361e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
362e499b74dSDag-Erling Smørgravor
363863edaecSDag-Erling Smørgrav.Fn sbuf_setpos .
364863edaecSDag-Erling Smørgrav.Pp
365863edaecSDag-Erling SmørgravThe
366863edaecSDag-Erling Smørgrav.Fn sbuf_printf
367863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed
368863edaecSDag-Erling Smørgravto by
369863edaecSDag-Erling Smørgrav.Fa fmt
370863edaecSDag-Erling Smørgravand appends the resulting string to the
371863edaecSDag-Erling Smørgrav.Fa sbuf
372863edaecSDag-Erling Smørgravat the current position.
373863edaecSDag-Erling Smørgrav.Pp
374863edaecSDag-Erling SmørgravThe
375371c0c84SKelly Yancey.Fn sbuf_vprintf
376371c0c84SKelly Yanceyfunction behaves the same as
377371c0c84SKelly Yancey.Fn sbuf_printf
378371c0c84SKelly Yanceyexcept that the arguments are obtained from the variable-length argument list
379371c0c84SKelly Yancey.Fa ap .
380371c0c84SKelly Yancey.Pp
381371c0c84SKelly YanceyThe
382863edaecSDag-Erling Smørgrav.Fn sbuf_putc
383863edaecSDag-Erling Smørgravfunction appends the character
384863edaecSDag-Erling Smørgrav.Fa c
385863edaecSDag-Erling Smørgravto the
386863edaecSDag-Erling Smørgrav.Fa sbuf
387863edaecSDag-Erling Smørgravat the current position.
388863edaecSDag-Erling Smørgrav.Pp
389863edaecSDag-Erling SmørgravThe
390371c0c84SKelly Yancey.Fn sbuf_trim
391371c0c84SKelly Yanceyfunction removes trailing whitespace from the
392371c0c84SKelly Yancey.Fa sbuf .
393371c0c84SKelly Yancey.Pp
394371c0c84SKelly YanceyThe
3954d369413SMatthew D Fleming.Fn sbuf_error
3964d369413SMatthew D Flemingfunction returns any error value that the
3974d369413SMatthew D Fleming.Fa sbuf
3984d369413SMatthew D Flemingmay have accumulated, either from the drain function, or ENOMEM if the
399e499b74dSDag-Erling Smørgrav.Fa sbuf
400e499b74dSDag-Erling Smørgravoverflowed.
4014d369413SMatthew D FlemingThis function is generally not needed and instead the error code from
4024d369413SMatthew D Fleming.Fn sbuf_finish
4034d369413SMatthew D Flemingis the preferred way to discover whether an sbuf had an error.
404e499b74dSDag-Erling Smørgrav.Pp
405e499b74dSDag-Erling SmørgravThe
406863edaecSDag-Erling Smørgrav.Fn sbuf_finish
4074351ba27SMatthew D Flemingfunction will call the attached drain function if one exists until all
4084351ba27SMatthew D Flemingthe data in the
409863edaecSDag-Erling Smørgrav.Fa sbuf
4104351ba27SMatthew D Flemingis flushed.
4114351ba27SMatthew D FlemingIf there is no attached drain,
4124351ba27SMatthew D Fleming.Fn sbuf_finish
4134351ba27SMatthew D FlemingNUL-terminates the
4144351ba27SMatthew D Fleming.Fa sbuf .
4154351ba27SMatthew D FlemingIn either case it marks the
4164351ba27SMatthew D Fleming.Fa sbuf
4174351ba27SMatthew D Flemingas finished, which means that it may no longer be modified using
418863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
419863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
4203393f8daSKenneth D. Merry.Fn sbuf_cpy ,
421863edaecSDag-Erling Smørgrav.Fn sbuf_printf
422863edaecSDag-Erling Smørgravor
423db960cf0SMatthew D Fleming.Fn sbuf_putc ,
424db960cf0SMatthew D Fleminguntil
425db960cf0SMatthew D Fleming.Fn sbuf_clear
426db960cf0SMatthew D Flemingis used to reset the sbuf.
427863edaecSDag-Erling Smørgrav.Pp
428863edaecSDag-Erling SmørgravThe
429863edaecSDag-Erling Smørgrav.Fn sbuf_data
4304351ba27SMatthew D Flemingfunction returns the actual string;
431e499b74dSDag-Erling Smørgrav.Fn sbuf_data
432e499b74dSDag-Erling Smørgravonly works on a finished
433863edaecSDag-Erling Smørgrav.Fa sbuf .
4344351ba27SMatthew D FlemingThe
435c0825cd5SJaakko Heinonen.Fn sbuf_len
436c0825cd5SJaakko Heinonenfunction returns the length of the string.
4374351ba27SMatthew D FlemingFor an
4384351ba27SMatthew D Fleming.Fa sbuf
4394351ba27SMatthew D Flemingwith an attached drain,
4404351ba27SMatthew D Fleming.Fn sbuf_len
4414351ba27SMatthew D Flemingreturns the length of the un-drained data.
4424351ba27SMatthew D Fleming.Fn sbuf_done
4434351ba27SMatthew D Flemingreturns non-zero if the
4444351ba27SMatthew D Fleming.Fa sbuf
4454351ba27SMatthew D Flemingis finished.
446f02db7b4SMikolaj Golub.Pp
447f02db7b4SMikolaj GolubThe
448f02db7b4SMikolaj Golub.Fn sbuf_start_section
449f02db7b4SMikolaj Goluband
450f02db7b4SMikolaj Golub.Fn sbuf_end_section
451f02db7b4SMikolaj Golubfunctions may be used for automatic section alignment.
452f02db7b4SMikolaj GolubThe arguments
453f02db7b4SMikolaj Golub.Fa pad
454f02db7b4SMikolaj Goluband
455f02db7b4SMikolaj Golub.Fa c
456f02db7b4SMikolaj Golubspecify the padding size and a character used for padding.
457f02db7b4SMikolaj GolubThe arguments
458f02db7b4SMikolaj Golub.Fa old_lenp
459f02db7b4SMikolaj Goluband
460f02db7b4SMikolaj Golub.Fa old_len
461f02db7b4SMikolaj Golubare to save and restore the current section length when nested sections
462f02db7b4SMikolaj Golubare used.
463f02db7b4SMikolaj GolubFor the top level section
464f02db7b4SMikolaj Golub.Dv NULL
465f02db7b4SMikolaj Goluband \-1 can be specified for
466f02db7b4SMikolaj Golub.Fa old_lenp
467f02db7b4SMikolaj Goluband
468f02db7b4SMikolaj Golub.Fa old_len
469f02db7b4SMikolaj Golubrespectively.
4705672fac9SKenneth D. Merry.Pp
4715672fac9SKenneth D. MerryThe
4725672fac9SKenneth D. Merry.Fn sbuf_hexdump
4735672fac9SKenneth D. Merryfunction prints an array of bytes to the supplied sbuf, along with an ASCII
4745672fac9SKenneth D. Merryrepresentation of the bytes if possible.
4755672fac9SKenneth D. MerrySee the
4765672fac9SKenneth D. Merry.Xr hexdump 3
4775672fac9SKenneth D. Merryman page for more details on the interface.
478388f3ce6SScott Long.Pp
479388f3ce6SScott LongThe
480388f3ce6SScott Long.Fn sbuf_putbuf
481388f3ce6SScott Longfunction printfs the sbuf to stdout if in userland, and to the console
482388f3ce6SScott Longand log if in the kernel.
483388f3ce6SScott LongIt does not drain the buffer or update any pointers.
484863edaecSDag-Erling Smørgrav.Sh NOTES
485863edaecSDag-Erling SmørgravIf an operation caused an
486863edaecSDag-Erling Smørgrav.Fa sbuf
487e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the
488e499b74dSDag-Erling Smørgrav.Fa sbuf
489e499b74dSDag-Erling Smørgravis finished using
490e499b74dSDag-Erling Smørgrav.Fn sbuf_finish
491e499b74dSDag-Erling Smørgravor reset using
492e499b74dSDag-Erling Smørgrav.Fn sbuf_clear ,
493e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the
494e499b74dSDag-Erling Smørgravsize of its storage buffer using
495863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
496863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using
497863edaecSDag-Erling Smørgrav.Fn sbuf_cpy .
4984351ba27SMatthew D Fleming.Pp
4994351ba27SMatthew D FlemingDrains in user-space will not always function as indicated.
5004351ba27SMatthew D FlemingWhile the drain function will be called immediately on overflow from
5014351ba27SMatthew D Flemingthe
5024351ba27SMatthew D Fleming.Fa sbuf_putc ,
5034351ba27SMatthew D Fleming.Fa sbuf_bcat ,
5044351ba27SMatthew D Fleming.Fa sbuf_cat
5054351ba27SMatthew D Flemingfunctions,
5064351ba27SMatthew D Fleming.Fa sbuf_printf
5074351ba27SMatthew D Flemingand
5084351ba27SMatthew D Fleming.Fa sbuf_vprintf
5094351ba27SMatthew D Flemingcurrently have no way to determine whether there will be an overflow
5104351ba27SMatthew D Fleminguntil after it occurs, and cannot do a partial expansion of the format
5114351ba27SMatthew D Flemingstring.
5124351ba27SMatthew D FlemingThus when using libsbuf the buffer may be extended to allow completion
5134351ba27SMatthew D Flemingof a single printf call, even though a drain is attached.
514863edaecSDag-Erling Smørgrav.Sh RETURN VALUES
51581ae4b8dSRuslan ErmilovThe
516863edaecSDag-Erling Smørgrav.Fn sbuf_new
51781ae4b8dSRuslan Ermilovfunction returns
518d6479358SDag-Erling Smørgrav.Dv NULL
519d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new
520d6479358SDag-Erling Smørgrav.Fa sbuf
521863edaecSDag-Erling Smørgravotherwise.
522863edaecSDag-Erling Smørgrav.Pp
52381ae4b8dSRuslan ErmilovThe
524863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
52581ae4b8dSRuslan Ermilovfunction returns \-1 if
526863edaecSDag-Erling Smørgrav.Fa pos
527863edaecSDag-Erling Smørgravwas invalid, and zero otherwise.
528863edaecSDag-Erling Smørgrav.Pp
52981ae4b8dSRuslan ErmilovThe
530863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
531863edaecSDag-Erling Smørgrav.Fn sbuf_cpy ,
532863edaecSDag-Erling Smørgrav.Fn sbuf_printf ,
533371c0c84SKelly Yancey.Fn sbuf_putc ,
534863edaecSDag-Erling Smørgravand
535371c0c84SKelly Yancey.Fn sbuf_trim
53681ae4b8dSRuslan Ermilovfunctions
537863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise.
538863edaecSDag-Erling Smørgrav.Pp
53981ae4b8dSRuslan ErmilovThe
5404d369413SMatthew D Fleming.Fn sbuf_error
5414d369413SMatthew D Flemingfunction returns a non-zero value if the buffer has an overflow or
5424d369413SMatthew D Flemingdrain error, and zero otherwise.
543e499b74dSDag-Erling Smørgrav.Pp
54481ae4b8dSRuslan ErmilovThe
545863edaecSDag-Erling Smørgrav.Fn sbuf_len
546569dd0f3SJaakko Heinonenfunction returns \-1 if the buffer overflowed.
54749091c48SPoul-Henning Kamp.Pp
54881ae4b8dSRuslan ErmilovThe
54949091c48SPoul-Henning Kamp.Fn sbuf_copyin
55081ae4b8dSRuslan Ermilovfunction
55149091c48SPoul-Henning Kampreturns \-1 if copying string from userland failed, and number of bytes
55249091c48SPoul-Henning Kampcopied otherwise.
5536ab7244aSPoul-Henning Kamp.Pp
5544351ba27SMatthew D FlemingThe
555f02db7b4SMikolaj Golub.Fn sbuf_end_section
556f02db7b4SMikolaj Golubfunction returns the section length or \-1 if the buffer has an error.
557f02db7b4SMikolaj Golub.Pp
558f02db7b4SMikolaj GolubThe
5596ab7244aSPoul-Henning Kamp.Fn sbuf_finish 9
5606ab7244aSPoul-Henning Kampfunction (the kernel version) returns ENOMEM if the sbuf overflowed before
5616ab7244aSPoul-Henning Kampbeing finished,
5624351ba27SMatthew D Flemingor returns the error code from the drain if one is attached.
5636ab7244aSPoul-Henning Kamp.Pp
5646ab7244aSPoul-Henning KampThe
5656ab7244aSPoul-Henning Kamp.Fn sbuf_finish 3
5666ab7244aSPoul-Henning Kampfunction (the userland version)
5676ab7244aSPoul-Henning Kampwill return zero for success and \-1 and set errno on error.
5686ab7244aSPoul-Henning Kamp.Sh EXAMPLES
5696ab7244aSPoul-Henning Kamp.Bd -literal -compact
570*7beb104dSEnji Cooper#include <sys/types.h>
5716ab7244aSPoul-Henning Kamp#include <sys/sbuf.h>
5726ab7244aSPoul-Henning Kamp
5736ab7244aSPoul-Henning Kampstruct sbuf *sb;
5746ab7244aSPoul-Henning Kamp
5756ab7244aSPoul-Henning Kampsb = sbuf_new_auto();
576c920aa7aSPoul-Henning Kampsbuf_cat(sb, "Customers found:\en");
5776ab7244aSPoul-Henning KampTAILQ_FOREACH(foo, &foolist, list) {
578c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "   %4d %s\en", foo->index, foo->name);
579c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Address: %s\en", foo->address);
580c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Zip: %s\en", foo->zipcode);
5816ab7244aSPoul-Henning Kamp}
582c5a0813dSJaakko Heinonenif (sbuf_finish(sb) != 0) /* Check for any and all errors */
5836ab7244aSPoul-Henning Kamp	err(1, "Could not generate message");
5846ab7244aSPoul-Henning Kamptransmit_msg(sbuf_data(sb), sbuf_len(sb));
5856ab7244aSPoul-Henning Kampsbuf_delete(sb);
5866ab7244aSPoul-Henning Kamp.Ed
587863edaecSDag-Erling Smørgrav.Sh SEE ALSO
5885672fac9SKenneth D. Merry.Xr hexdump 3 ,
589863edaecSDag-Erling Smørgrav.Xr printf 3 ,
590863edaecSDag-Erling Smørgrav.Xr strcat 3 ,
591d68b700aSRuslan Ermilov.Xr strcpy 3 ,
592d68b700aSRuslan Ermilov.Xr copyin 9 ,
593e05f3134SAndrew R. Reiter.Xr copyinstr 9 ,
594e05f3134SAndrew R. Reiter.Xr printf 9
595863edaecSDag-Erling Smørgrav.Sh HISTORY
596863edaecSDag-Erling SmørgravThe
59781ae4b8dSRuslan Ermilov.Nm
598863edaecSDag-Erling Smørgravfamily of functions first appeared in
59922cbe5e3SDag-Erling Smørgrav.Fx 4.4 .
600863edaecSDag-Erling Smørgrav.Sh AUTHORS
601863edaecSDag-Erling Smørgrav.An -nosplit
602863edaecSDag-Erling SmørgravThe
60381ae4b8dSRuslan Ermilov.Nm
604863edaecSDag-Erling Smørgravfamily of functions was designed by
6058a7314fcSBaptiste Daroussin.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
606863edaecSDag-Erling Smørgravand implemented by
6078a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
608e499b74dSDag-Erling SmørgravAdditional improvements were suggested by
6098a7314fcSBaptiste Daroussin.An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org .
610371c0c84SKelly YanceyAuto-extend support added by
6118a7314fcSBaptiste Daroussin.An Kelly Yancey Aq Mt kbyanc@FreeBSD.org .
6124351ba27SMatthew D FlemingDrain functionality added by
6138a7314fcSBaptiste Daroussin.An Matthew Fleming Aq Mt mdf@FreeBSD.org .
614863edaecSDag-Erling Smørgrav.Pp
615863edaecSDag-Erling SmørgravThis manual page was written by
6168a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
617