2 * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/ctype.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
40 #include <machine/stdarg.h>
52 MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
53 #define SBMALLOC(size) malloc(size, M_SBUF, M_WAITOK)
54 #define SBFREE(buf) free(buf, M_SBUF)
57 #define SBMALLOC(size) malloc(size)
58 #define SBFREE(buf) free(buf)
59 #define min(x,y) MIN(x,y)
65 #define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC)
66 #define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT)
67 #define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED)
68 #define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED)
69 #define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1)
70 #define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1)
71 #define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND)
76 #define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
77 #define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
79 #define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
80 #define SBUF_MAXEXTENDSIZE PAGE_SIZE
81 #define SBUF_MAXEXTENDINCR PAGE_SIZE
86 #if defined(_KERNEL) && defined(INVARIANTS)
88 _assert_sbuf_integrity(const char *fun, struct sbuf *s)
91 ("%s called with a NULL sbuf pointer", fun));
92 KASSERT(s->s_buf != NULL,
93 ("%s called with uninitialized or corrupt sbuf", fun));
94 KASSERT(s->s_len < s->s_size,
95 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
99 _assert_sbuf_state(const char *fun, struct sbuf *s, int state)
101 KASSERT((s->s_flags & SBUF_FINISHED) == state,
102 ("%s called with %sfinished or corrupt sbuf", fun,
103 (state ? "un" : "")));
105 #define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
106 #define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i))
107 #else /* _KERNEL && INVARIANTS */
108 #define assert_sbuf_integrity(s) do { } while (0)
109 #define assert_sbuf_state(s, i) do { } while (0)
110 #endif /* _KERNEL && INVARIANTS */
113 sbuf_extendsize(int size)
117 newsize = SBUF_MINEXTENDSIZE;
118 while (newsize < size) {
119 if (newsize < (int)SBUF_MAXEXTENDSIZE)
122 newsize += SBUF_MAXEXTENDINCR;
133 sbuf_extend(struct sbuf *s, int addlen)
138 if (!SBUF_CANEXTEND(s))
141 newsize = sbuf_extendsize(s->s_size + addlen);
142 newbuf = (char *)SBMALLOC(newsize);
145 bcopy(s->s_buf, newbuf, s->s_size);
146 if (SBUF_ISDYNAMIC(s))
149 SBUF_SETFLAG(s, SBUF_DYNAMIC);
156 * Initialize an sbuf.
157 * If buf is non-NULL, it points to a static or already-allocated string
158 * big enough to hold at least length characters.
161 sbuf_new(struct sbuf *s, char *buf, int length, int flags)
164 ("attempt to create an sbuf of negative length (%d)", length));
165 KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
166 ("%s called with invalid flags", __func__));
168 flags &= SBUF_USRFLAGMSK;
170 s = (struct sbuf *)SBMALLOC(sizeof *s);
175 SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
185 if (flags & SBUF_AUTOEXTEND)
186 s->s_size = sbuf_extendsize(s->s_size);
187 s->s_buf = (char *)SBMALLOC(s->s_size);
188 if (s->s_buf == NULL) {
189 if (SBUF_ISDYNSTRUCT(s))
193 SBUF_SETFLAG(s, SBUF_DYNAMIC);
199 * Create an sbuf with uio data
202 sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
205 ("%s called with NULL uio pointer", __func__));
206 KASSERT(error != NULL,
207 ("%s called with NULL error pointer", __func__));
209 s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
214 *error = uiomove(s->s_buf, uio->uio_resid, uio);
219 s->s_len = s->s_size - 1;
226 * Clear an sbuf and reset its position.
229 sbuf_clear(struct sbuf *s)
231 assert_sbuf_integrity(s);
232 /* don't care if it's finished or not */
234 SBUF_CLEARFLAG(s, SBUF_FINISHED);
235 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
240 * Set the sbuf's end position to an arbitrary value.
241 * Effectively truncates the sbuf at the new position.
244 sbuf_setpos(struct sbuf *s, int pos)
246 assert_sbuf_integrity(s);
247 assert_sbuf_state(s, 0);
250 ("attempt to seek to a negative position (%d)", pos));
251 KASSERT(pos < s->s_size,
252 ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
254 if (pos < 0 || pos > s->s_len)
261 * Append a byte string to an sbuf.
264 sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
266 const char *str = buf;
268 assert_sbuf_integrity(s);
269 assert_sbuf_state(s, 0);
271 if (SBUF_HASOVERFLOWED(s))
275 if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0)
277 s->s_buf[s->s_len++] = *str++;
280 SBUF_SETFLAG(s, SBUF_OVERFLOWED);
288 * Copy a byte string from userland into an sbuf.
291 sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
293 assert_sbuf_integrity(s);
294 assert_sbuf_state(s, 0);
296 if (SBUF_HASOVERFLOWED(s))
301 if (len > SBUF_FREESPACE(s)) {
302 sbuf_extend(s, len - SBUF_FREESPACE(s));
303 len = min(len, SBUF_FREESPACE(s));
305 if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
314 * Copy a byte string into an sbuf.
317 sbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
319 assert_sbuf_integrity(s);
320 assert_sbuf_state(s, 0);
323 return (sbuf_bcat(s, buf, len));
327 * Append a string to an sbuf.
330 sbuf_cat(struct sbuf *s, const char *str)
332 assert_sbuf_integrity(s);
333 assert_sbuf_state(s, 0);
335 if (SBUF_HASOVERFLOWED(s))
339 if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0)
341 s->s_buf[s->s_len++] = *str++;
344 SBUF_SETFLAG(s, SBUF_OVERFLOWED);
352 * Append a string from userland to an sbuf.
355 sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
359 assert_sbuf_integrity(s);
360 assert_sbuf_state(s, 0);
362 if (SBUF_HASOVERFLOWED(s))
366 len = SBUF_FREESPACE(s); /* XXX return 0? */
367 if (len > SBUF_FREESPACE(s)) {
369 len = min(len, SBUF_FREESPACE(s));
371 switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
373 SBUF_SETFLAG(s, SBUF_OVERFLOWED);
376 s->s_len += done - 1;
379 return (-1); /* XXX */
387 * Copy a string into an sbuf.
390 sbuf_cpy(struct sbuf *s, const char *str)
392 assert_sbuf_integrity(s);
393 assert_sbuf_state(s, 0);
396 return (sbuf_cat(s, str));
400 * Format the given argument list and append the resulting string to an sbuf.
403 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
408 assert_sbuf_integrity(s);
409 assert_sbuf_state(s, 0);
412 ("%s called with a NULL format string", __func__));
414 if (SBUF_HASOVERFLOWED(s))
418 va_copy(ap_copy, ap);
419 len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
422 } while (len > SBUF_FREESPACE(s) &&
423 sbuf_extend(s, len - SBUF_FREESPACE(s)) == 0);
426 * s->s_len is the length of the string, without the terminating nul.
427 * When updating s->s_len, we must subtract 1 from the length that
428 * we passed into vsnprintf() because that length includes the
431 * vsnprintf() returns the amount that would have been copied,
432 * given sufficient space, hence the min() calculation below.
434 s->s_len += min(len, SBUF_FREESPACE(s));
435 if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
436 SBUF_SETFLAG(s, SBUF_OVERFLOWED);
438 KASSERT(s->s_len < s->s_size,
439 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
441 if (SBUF_HASOVERFLOWED(s))
447 * Format the given arguments and append the resulting string to an sbuf.
450 sbuf_printf(struct sbuf *s, const char *fmt, ...)
456 result = sbuf_vprintf(s, fmt, ap);
462 * Append a character to an sbuf.
465 sbuf_putc(struct sbuf *s, int c)
467 assert_sbuf_integrity(s);
468 assert_sbuf_state(s, 0);
470 if (SBUF_HASOVERFLOWED(s))
473 if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) {
474 SBUF_SETFLAG(s, SBUF_OVERFLOWED);
478 s->s_buf[s->s_len++] = c;
483 * Trim whitespace characters from end of an sbuf.
486 sbuf_trim(struct sbuf *s)
488 assert_sbuf_integrity(s);
489 assert_sbuf_state(s, 0);
491 if (SBUF_HASOVERFLOWED(s))
494 while (s->s_len && isspace(s->s_buf[s->s_len-1]))
501 * Check if an sbuf overflowed
504 sbuf_overflowed(struct sbuf *s)
506 return SBUF_HASOVERFLOWED(s);
510 * Finish off an sbuf.
513 sbuf_finish(struct sbuf *s)
515 assert_sbuf_integrity(s);
516 assert_sbuf_state(s, 0);
518 s->s_buf[s->s_len] = '\0';
519 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
520 SBUF_SETFLAG(s, SBUF_FINISHED);
524 * Return a pointer to the sbuf data.
527 sbuf_data(struct sbuf *s)
529 assert_sbuf_integrity(s);
530 assert_sbuf_state(s, SBUF_FINISHED);
536 * Return the length of the sbuf data.
539 sbuf_len(struct sbuf *s)
541 assert_sbuf_integrity(s);
542 /* don't care if it's finished or not */
544 if (SBUF_HASOVERFLOWED(s))
550 * Clear an sbuf, free its buffer if necessary.
553 sbuf_delete(struct sbuf *s)
557 assert_sbuf_integrity(s);
558 /* don't care if it's finished or not */
560 if (SBUF_ISDYNAMIC(s))
562 isdyn = SBUF_ISDYNSTRUCT(s);
569 * Check if an sbuf has been finished.
572 sbuf_done(struct sbuf *s)
575 return(SBUF_ISFINISHED(s));