Annotation of src/usr.bin/make/buf.h, Revision 1.22
1.15 espie 1: #ifndef _BUF_H
2: #define _BUF_H
3:
1.22 ! espie 4: /* $OpenBSD: buf.h,v 1.21 2012/10/02 10:29:30 espie Exp $ */
1.14 espie 5: /* $NetBSD: buf.h,v 1.7 1996/12/31 17:53:22 christos Exp $ */
6:
7: /*
8: * Copyright (c) 1999 Marc Espie.
9: *
10: * Extensive code changes for the OpenBSD project.
11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
15: * 1. Redistributions of source code must retain the above copyright
16: * notice, this list of conditions and the following disclaimer.
17: * 2. Redistributions in binary form must reproduce the above copyright
18: * notice, this list of conditions and the following disclaimer in the
19: * documentation and/or other materials provided with the distribution.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
22: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
25: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32: */
1.1 deraadt 33:
34: /*
35: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
36: * Copyright (c) 1988, 1989 by Adam de Boor
37: * Copyright (c) 1989 by Berkeley Softworks
38: * All rights reserved.
39: *
40: * This code is derived from software contributed to Berkeley by
41: * Adam de Boor.
42: *
43: * Redistribution and use in source and binary forms, with or without
44: * modification, are permitted provided that the following conditions
45: * are met:
46: * 1. Redistributions of source code must retain the above copyright
47: * notice, this list of conditions and the following disclaimer.
48: * 2. Redistributions in binary form must reproduce the above copyright
49: * notice, this list of conditions and the following disclaimer in the
50: * documentation and/or other materials provided with the distribution.
1.16 millert 51: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 52: * may be used to endorse or promote products derived from this software
53: * without specific prior written permission.
54: *
55: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65: * SUCH DAMAGE.
66: *
1.14 espie 67: * from: @(#)buf.h 8.1 (Berkeley) 6/6/93
1.1 deraadt 68: */
69:
1.15 espie 70: /*
71: * buf
72: * Support for extensible char buffers.
73: * One adds chars to a buffer, then retrieves the contents using
74: * Buf_Retrieve (no copy involved), or releases the memory using
75: * Buf_Destroy.
1.1 deraadt 76: */
77:
78:
1.13 espie 79: /* Internal data structures and functions. BUFFER is visible so
80: * that users can allocate the memory themselves. */
1.11 espie 81: typedef struct Buffer_ {
1.10 espie 82: char *buffer; /* The buffer itself. */
83: char *inPtr; /* Place to write to. */
84: char *endPtr; /* End of allocated space. */
1.11 espie 85: } BUFFER;
86:
1.8 espie 87: /* Internal support for Buf_AddChar. */
1.14 espie 88: extern void BufOverflow(Buffer);
1.8 espie 89:
1.15 espie 90:
1.10 espie 91: /* User interface */
92:
1.15 espie 93: /* Buf_AddChars(buf, n, str);
94: * Adds n chars to buffer buf starting from str. */
1.14 espie 95: extern void Buf_AddChars(Buffer, size_t, const char *);
1.21 espie 96: /* Buf_Truncate(buffer, length) */
97: #define Buf_Truncate(bp, len) ((void)((bp)->inPtr = (bp)->buffer + (len)))
1.15 espie 98: /* Buf_Reset(buf);
99: * Empties buffer. */
1.21 espie 100: #define Buf_Reset(bp) Buf_Truncate(bp, 0)
1.15 espie 101: /* n = Buf_Size(buf);
102: * Returns number of chars currently in buf.
1.13 espie 103: * Doesn't include the null-terminating char. */
104: #define Buf_Size(bp) ((size_t)((bp)->inPtr - (bp)->buffer))
1.15 espie 105: /* Buf_Init(buf, init);
1.17 espie 106: * Initializes a buffer, to hold approximately init chars.
1.15 espie 107: * Set init to 0 if you have no idea. */
1.14 espie 108: extern void Buf_Init(Buffer, size_t);
1.15 espie 109: /* Buf_Destroy(buf);
110: * Nukes a buffer and all its resources. */
1.14 espie 111: #define Buf_Destroy(bp) ((void)free((bp)->buffer))
1.15 espie 112: /* str = Buf_Retrieve(buf);
113: * Retrieves data from a buffer, as a NULL terminated string. */
1.13 espie 114: #define Buf_Retrieve(bp) (*(bp)->inPtr = '\0', (bp)->buffer)
1.17 espie 115: /* Buf_AddChar(buf, c);
1.15 espie 116: * Adds a single char to buffer. */
1.14 espie 117: #define Buf_AddChar(bp, byte) \
118: do { \
1.10 espie 119: if ((bp)->endPtr - (bp)->inPtr <= 1) \
1.8 espie 120: BufOverflow(bp); \
121: *(bp)->inPtr++ = (byte); \
1.14 espie 122: } while (0)
1.1 deraadt 123:
1.15 espie 124: /* Buf_AddSpace(buf);
125: * Adds a space to buffer. */
1.14 espie 126: #define Buf_AddSpace(b) Buf_AddChar((b), ' ')
1.15 espie 127: /* Buf_AddString(buf, str);
128: * Adds the contents of a NULL terminated string to buffer. */
1.7 espie 129: #define Buf_AddString(b, s) Buf_AddChars((b), strlen(s), (s))
1.15 espie 130: /* Buf_Addi(buf, s, e);
131: * Adds characters between s and e to buffer. */
132: #define Buf_Addi(b, s, e) Buf_AddChars((b), (e) - (s), (s))
1.20 espie 133:
134: extern void Buf_printf(Buffer, const char *, ...);
135: #define Buf_puts(b, s) Buf_AddChars((b), strlen(s), (s))
1.1 deraadt 136:
137: #endif /* _BUF_H */