2 * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Co\95dan 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>
31 #include <sys/param.h>
34 /* #include <ctype.h> */
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
39 #include <sys/uio_internal.h>
40 #include <sys/systm.h>
53 /* MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers"); */
54 #define SBMALLOC(size) _MALLOC(size, M_SBUF, M_WAITOK)
55 #define SBFREE(buf) FREE(buf, M_SBUF)
58 #define SBMALLOC(size) malloc(size)
59 #define SBFREE(buf) free(buf)
60 #define min(x,y) MIN(x,y)
67 #define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC)
68 #define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT)
69 #define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED)
70 #define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED)
71 #define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1)
72 #define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1)
73 #define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND)
78 #define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
79 #define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
81 #define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
82 #define SBUF_MAXEXTENDSIZE PAGE_SIZE
83 #define SBUF_MAXEXTENDINCR PAGE_SIZE
88 #if defined(KERNEL) && defined(INVARIANTS)
90 _assert_sbuf_integrity(const char *fun
, struct sbuf
*s
)
93 ("%s called with a NULL sbuf pointer", fun
));
94 KASSERT(s
->s_buf
!= NULL
,
95 ("%s called with uninitialized or corrupt sbuf", fun
));
96 KASSERT(s
->s_len
< s
->s_size
,
97 ("wrote past end of sbuf (%d >= %d)", s
->s_len
, s
->s_size
));
101 _assert_sbuf_state(const char *fun
, struct sbuf
*s
, int state
)
103 KASSERT((s
->s_flags
& SBUF_FINISHED
) == state
,
104 ("%s called with %sfinished or corrupt sbuf", fun
,
105 (state
? "un" : "")));
107 #define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
108 #define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i))
109 #else /* KERNEL && INVARIANTS */
110 #define assert_sbuf_integrity(s) do { } while (0)
111 #define assert_sbuf_state(s, i) do { } while (0)
112 #endif /* KERNEL && INVARIANTS */
115 sbuf_extendsize(int size
)
119 newsize
= SBUF_MINEXTENDSIZE
;
120 while (newsize
< size
) {
121 if (newsize
< (int)SBUF_MAXEXTENDSIZE
)
124 newsize
+= SBUF_MAXEXTENDINCR
;
135 sbuf_extend(struct sbuf
*s
, int addlen
)
140 if (!SBUF_CANEXTEND(s
))
143 newsize
= sbuf_extendsize(s
->s_size
+ addlen
);
144 newbuf
= (char *)SBMALLOC(newsize
);
147 bcopy(s
->s_buf
, newbuf
, s
->s_size
);
148 if (SBUF_ISDYNAMIC(s
))
151 SBUF_SETFLAG(s
, SBUF_DYNAMIC
);
158 * Initialize an sbuf.
159 * If buf is non-NULL, it points to a static or already-allocated string
160 * big enough to hold at least length characters.
163 sbuf_new(struct sbuf
*s
, char *buf
, int length
, int flags
)
166 ("attempt to create an sbuf of negative length (%d)", length
));
167 KASSERT((flags
& ~SBUF_USRFLAGMSK
) == 0,
168 ("%s called with invalid flags", __func__
));
170 flags
&= SBUF_USRFLAGMSK
;
172 s
= (struct sbuf
*)SBMALLOC(sizeof *s
);
177 SBUF_SETFLAG(s
, SBUF_DYNSTRUCT
);
187 if (flags
& SBUF_AUTOEXTEND
)
188 s
->s_size
= sbuf_extendsize(s
->s_size
);
189 s
->s_buf
= (char *)SBMALLOC(s
->s_size
);
190 if (s
->s_buf
== NULL
) {
191 if (SBUF_ISDYNSTRUCT(s
))
195 SBUF_SETFLAG(s
, SBUF_DYNAMIC
);
201 * Create an sbuf with uio data
204 sbuf_uionew(struct sbuf
*s
, struct uio
*uio
, int *error
)
207 ("%s called with NULL uio pointer", __func__
));
208 KASSERT(error
!= NULL
,
209 ("%s called with NULL error pointer", __func__
));
211 s
= sbuf_new(s
, NULL
, uio_resid(uio
) + 1, 0);
216 *error
= uiomove(s
->s_buf
, uio_resid(uio
), uio
);
221 s
->s_len
= s
->s_size
- 1;
228 * Clear an sbuf and reset its position.
231 sbuf_clear(struct sbuf
*s
)
233 assert_sbuf_integrity(s
);
234 /* don't care if it's finished or not */
236 SBUF_CLEARFLAG(s
, SBUF_FINISHED
);
237 SBUF_CLEARFLAG(s
, SBUF_OVERFLOWED
);
242 * Set the sbuf's end position to an arbitrary value.
243 * Effectively truncates the sbuf at the new position.
246 sbuf_setpos(struct sbuf
*s
, int pos
)
248 assert_sbuf_integrity(s
);
249 assert_sbuf_state(s
, 0);
252 ("attempt to seek to a negative position (%d)", pos
));
253 KASSERT(pos
< s
->s_size
,
254 ("attempt to seek past end of sbuf (%d >= %d)", pos
, s
->s_size
));
256 if (pos
< 0 || pos
> s
->s_len
)
263 * Append a byte string to an sbuf.
266 sbuf_bcat(struct sbuf
*s
, const void *buf
, size_t len
)
268 const char *str
= buf
;
270 assert_sbuf_integrity(s
);
271 assert_sbuf_state(s
, 0);
273 if (SBUF_HASOVERFLOWED(s
))
277 if (!SBUF_HASROOM(s
) && sbuf_extend(s
, len
) < 0)
279 s
->s_buf
[s
->s_len
++] = *str
++;
282 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
290 * Copy a byte string from userland into an sbuf.
293 sbuf_bcopyin(struct sbuf
*s
, const void *uaddr
, size_t len
)
295 assert_sbuf_integrity(s
);
296 assert_sbuf_state(s
, 0);
298 if (SBUF_HASOVERFLOWED(s
))
303 if (len
> (unsigned) SBUF_FREESPACE(s
)) {
304 sbuf_extend(s
, len
- SBUF_FREESPACE(s
));
305 len
= min(len
, SBUF_FREESPACE(s
));
307 if (copyin(CAST_USER_ADDR_T(uaddr
), s
->s_buf
+ s
->s_len
, len
) != 0)
316 * Copy a byte string into an sbuf.
319 sbuf_bcpy(struct sbuf
*s
, const void *buf
, size_t len
)
321 assert_sbuf_integrity(s
);
322 assert_sbuf_state(s
, 0);
325 return (sbuf_bcat(s
, buf
, len
));
329 * Append a string to an sbuf.
332 sbuf_cat(struct sbuf
*s
, const char *str
)
334 assert_sbuf_integrity(s
);
335 assert_sbuf_state(s
, 0);
337 if (SBUF_HASOVERFLOWED(s
))
341 if (!SBUF_HASROOM(s
) && sbuf_extend(s
, strlen(str
)) < 0)
343 s
->s_buf
[s
->s_len
++] = *str
++;
346 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
354 * Append a string from userland to an sbuf.
357 sbuf_copyin(struct sbuf
*s
, const void *uaddr
, size_t len
)
361 assert_sbuf_integrity(s
);
362 assert_sbuf_state(s
, 0);
364 if (SBUF_HASOVERFLOWED(s
))
368 len
= SBUF_FREESPACE(s
); /* XXX return 0? */
369 if (len
> (unsigned) SBUF_FREESPACE(s
)) {
371 len
= min(len
, SBUF_FREESPACE(s
));
373 switch (copyinstr(CAST_USER_ADDR_T(uaddr
), s
->s_buf
+ s
->s_len
, len
+ 1, &done
)) {
375 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
378 s
->s_len
+= done
- 1;
381 return (-1); /* XXX */
389 * Copy a string into an sbuf.
392 sbuf_cpy(struct sbuf
*s
, const char *str
)
394 assert_sbuf_integrity(s
);
395 assert_sbuf_state(s
, 0);
398 return (sbuf_cat(s
, str
));
402 * Format the given argument list and append the resulting string to an sbuf.
405 sbuf_vprintf(struct sbuf
*s
, const char *fmt
, va_list ap
)
407 __builtin_va_list ap_copy
; /* XXX tduffy - blame on him */
410 assert_sbuf_integrity(s
);
411 assert_sbuf_state(s
, 0);
414 ("%s called with a NULL format string", __func__
));
416 if (SBUF_HASOVERFLOWED(s
))
420 va_copy(ap_copy
, ap
);
421 len
= vsnprintf(&s
->s_buf
[s
->s_len
], SBUF_FREESPACE(s
) + 1,
424 } while (len
> SBUF_FREESPACE(s
) &&
425 sbuf_extend(s
, len
- SBUF_FREESPACE(s
)) == 0);
428 * s->s_len is the length of the string, without the terminating nul.
429 * When updating s->s_len, we must subtract 1 from the length that
430 * we passed into vsnprintf() because that length includes the
433 * vsnprintf() returns the amount that would have been copied,
434 * given sufficient space, hence the min() calculation below.
436 s
->s_len
+= min(len
, SBUF_FREESPACE(s
));
437 if (!SBUF_HASROOM(s
) && !SBUF_CANEXTEND(s
))
438 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
440 KASSERT(s
->s_len
< s
->s_size
,
441 ("wrote past end of sbuf (%d >= %d)", s
->s_len
, s
->s_size
));
443 if (SBUF_HASOVERFLOWED(s
))
449 * Format the given arguments and append the resulting string to an sbuf.
452 sbuf_printf(struct sbuf
*s
, const char *fmt
, ...)
458 result
= sbuf_vprintf(s
, fmt
, ap
);
464 * Append a character to an sbuf.
467 sbuf_putc(struct sbuf
*s
, int c
)
469 assert_sbuf_integrity(s
);
470 assert_sbuf_state(s
, 0);
472 if (SBUF_HASOVERFLOWED(s
))
475 if (!SBUF_HASROOM(s
) && sbuf_extend(s
, 1) < 0) {
476 SBUF_SETFLAG(s
, SBUF_OVERFLOWED
);
480 s
->s_buf
[s
->s_len
++] = c
;
487 return (ch
== ' ' || ch
== '\n' || ch
== '\t');
491 * Trim whitespace characters from end of an sbuf.
494 sbuf_trim(struct sbuf
*s
)
496 assert_sbuf_integrity(s
);
497 assert_sbuf_state(s
, 0);
499 if (SBUF_HASOVERFLOWED(s
))
502 while (s
->s_len
&& isspace(s
->s_buf
[s
->s_len
-1]))
509 * Check if an sbuf overflowed
512 sbuf_overflowed(struct sbuf
*s
)
514 return SBUF_HASOVERFLOWED(s
);
518 * Finish off an sbuf.
521 sbuf_finish(struct sbuf
*s
)
523 assert_sbuf_integrity(s
);
524 assert_sbuf_state(s
, 0);
526 s
->s_buf
[s
->s_len
] = '\0';
527 SBUF_CLEARFLAG(s
, SBUF_OVERFLOWED
);
528 SBUF_SETFLAG(s
, SBUF_FINISHED
);
532 * Return a pointer to the sbuf data.
535 sbuf_data(struct sbuf
*s
)
537 assert_sbuf_integrity(s
);
538 assert_sbuf_state(s
, SBUF_FINISHED
);
544 * Return the length of the sbuf data.
547 sbuf_len(struct sbuf
*s
)
549 assert_sbuf_integrity(s
);
550 /* don't care if it's finished or not */
552 if (SBUF_HASOVERFLOWED(s
))
558 * Clear an sbuf, free its buffer if necessary.
561 sbuf_delete(struct sbuf
*s
)
565 assert_sbuf_integrity(s
);
566 /* don't care if it's finished or not */
568 if (SBUF_ISDYNAMIC(s
))
570 isdyn
= SBUF_ISDYNSTRUCT(s
);
577 * Check if an sbuf has been finished.
580 sbuf_done(struct sbuf
*s
)
583 return(SBUF_ISFINISHED(s
));