2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * Copyright (c) 1988, 1989 by Adam de Boor
4 * Copyright (c) 1989 by Berkeley Softworks
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
43 static const char rcsid[] =
44 "$FreeBSD: src/usr.bin/make/buf.c,v 1.11 1999/09/11 13:08:01 hoek Exp $";
50 * Functions for automatically-expanded buffers.
58 #define max(a,b) ((a) > (b) ? (a) : (b))
63 * Expand the given buffer to hold the given number of additional
65 * Makes sure there's room for an extra NULL byte at the end of the
66 * buffer in case it holds a string.
68 #define BufExpand(bp,nb) \
69 if (bp->left < (nb)+1) {\
70 int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
71 Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
73 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
74 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
75 (bp)->buffer = newBuf;\
76 (bp)->size = newSize;\
77 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
80 #define BUF_DEF_SIZE 256 /* Default buffer size */
81 #define BUF_ADD_INC 256 /* Expansion increment when Adding */
82 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
85 *-----------------------------------------------------------------------
87 * Add a single byte to the buffer. left is zero or negative.
93 * The buffer may be expanded.
95 *-----------------------------------------------------------------------
98 Buf_OvAddByte (bp, byte)
104 BufExpand (bp, nbytes);
116 *-----------------------------------------------------------------------
118 * Add a number of bytes to the buffer.
126 *-----------------------------------------------------------------------
129 Buf_AddBytes (bp, numBytes, bytesPtr)
132 const Byte *bytesPtr;
135 BufExpand (bp, numBytes);
137 memcpy (bp->inPtr, bytesPtr, numBytes);
138 bp->inPtr += numBytes;
139 bp->left -= numBytes;
148 *-----------------------------------------------------------------------
150 * Place the byte back at the beginning of the buffer.
153 * SUCCESS if the byte was added ok. FAILURE if not.
156 * The byte is stuffed in the buffer and outPtr is decremented.
158 *-----------------------------------------------------------------------
161 Buf_UngetByte (bp, byte)
166 if (bp->outPtr != bp->buffer) {
169 } else if (bp->outPtr == bp->inPtr) {
176 * Yech. have to expand the buffer to stuff this thing in.
177 * We use a different expansion constant because people don't
178 * usually push back many bytes when they're doing it a byte at
181 int numBytes = bp->inPtr - bp->outPtr;
184 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
185 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
186 bp->outPtr = newBuf + BUF_UNGET_INC;
187 bp->inPtr = bp->outPtr + numBytes;
188 free ((char *)bp->buffer);
190 bp->size += BUF_UNGET_INC;
191 bp->left = bp->size - (bp->inPtr - bp->buffer);
198 *-----------------------------------------------------------------------
200 * Push back a series of bytes at the beginning of the buffer.
206 * outPtr is decremented and the bytes copied into the buffer.
208 *-----------------------------------------------------------------------
211 Buf_UngetBytes (bp, numBytes, bytesPtr)
217 if (bp->outPtr - bp->buffer >= numBytes) {
218 bp->outPtr -= numBytes;
219 memcpy (bp->outPtr, bytesPtr, numBytes);
220 } else if (bp->outPtr == bp->inPtr) {
221 Buf_AddBytes (bp, numBytes, bytesPtr);
223 int curNumBytes = bp->inPtr - bp->outPtr;
225 int newBytes = max(numBytes,BUF_UNGET_INC);
227 newBuf = (Byte *)emalloc (bp->size + newBytes);
228 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
229 bp->outPtr = newBuf + newBytes;
230 bp->inPtr = bp->outPtr + curNumBytes;
231 free ((char *)bp->buffer);
233 bp->size += newBytes;
234 bp->left = bp->size - (bp->inPtr - bp->buffer);
235 bp->outPtr -= numBytes;
236 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
241 *-----------------------------------------------------------------------
243 * Return the next byte from the buffer. Actually returns an integer.
246 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
247 * itself if there is one.
250 * outPtr is incremented and both outPtr and inPtr will be reset if
251 * the buffer is emptied.
253 *-----------------------------------------------------------------------
261 if (bp->inPtr == bp->outPtr) {
264 res = (int) *bp->outPtr;
266 if (bp->outPtr == bp->inPtr) {
267 bp->outPtr = bp->inPtr = bp->buffer;
276 *-----------------------------------------------------------------------
278 * Extract a number of bytes from the buffer.
281 * The number of bytes gotten.
284 * The passed array is overwritten.
286 *-----------------------------------------------------------------------
289 Buf_GetBytes (bp, numBytes, bytesPtr)
295 if (bp->inPtr - bp->outPtr < numBytes) {
296 numBytes = bp->inPtr - bp->outPtr;
298 memcpy (bytesPtr, bp->outPtr, numBytes);
299 bp->outPtr += numBytes;
301 if (bp->outPtr == bp->inPtr) {
302 bp->outPtr = bp->inPtr = bp->buffer;
310 *-----------------------------------------------------------------------
312 * Get all the available data at once.
315 * A pointer to the data and the number of bytes available.
320 *-----------------------------------------------------------------------
323 Buf_GetAll (bp, numBytesPtr)
328 if (numBytesPtr != (int *)NULL) {
329 *numBytesPtr = bp->inPtr - bp->outPtr;
336 *-----------------------------------------------------------------------
338 * Throw away bytes in a buffer.
344 * The bytes are discarded.
346 *-----------------------------------------------------------------------
349 Buf_Discard (bp, numBytes)
354 if (bp->inPtr - bp->outPtr <= numBytes) {
355 bp->inPtr = bp->outPtr = bp->buffer;
359 bp->outPtr += numBytes;
364 *-----------------------------------------------------------------------
366 * Returns the number of bytes in the given buffer. Doesn't include
367 * the null-terminating byte.
370 * The number of bytes.
375 *-----------------------------------------------------------------------
381 return (buf->inPtr - buf->outPtr);
385 *-----------------------------------------------------------------------
387 * Initialize a buffer. If no initial size is given, a reasonable
391 * A buffer to be given to other functions in this library.
394 * The buffer is created, the space allocated and pointers
397 *-----------------------------------------------------------------------
401 int size; /* Initial size for the buffer */
403 Buffer bp; /* New Buffer */
405 bp = (Buffer)emalloc(sizeof(*bp));
410 bp->left = bp->size = size;
411 bp->buffer = (Byte *)emalloc(size);
412 bp->inPtr = bp->outPtr = bp->buffer;
419 *-----------------------------------------------------------------------
421 * Nuke a buffer and all its resources.
427 * The buffer is freed.
429 *-----------------------------------------------------------------------
432 Buf_Destroy (buf, freeData)
433 Buffer buf; /* Buffer to destroy */
434 Boolean freeData; /* TRUE if the data should be destroyed as well */
438 free ((char *)buf->buffer);
444 *-----------------------------------------------------------------------
445 * Buf_ReplaceLastByte --
446 * Replace the last byte in a buffer.
452 * If the buffer was empty intially, then a new byte will be added.
453 * Otherwise, the last byte is overwritten.
455 *-----------------------------------------------------------------------
458 Buf_ReplaceLastByte (buf, byte)
459 Buffer buf; /* buffer to augment */
460 int byte; /* byte to be written */
462 if (buf->inPtr == buf->outPtr)
463 Buf_AddByte(buf, byte);
465 *(buf->inPtr - 1) = byte;