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