Annotation of src/usr.bin/make/buf.c, Revision 1.14
1.14 ! espie 1: /* $OpenBSD: buf.c,v 1.13 2000/09/14 13:32:05 espie Exp $ */
1.5 millert 2: /* $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */
1.1 deraadt 3:
4: /*
1.8 espie 5: * Copyright (c) 1999 Marc Espie.
6: *
7: * Extensive code modifications for the OpenBSD project.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
19: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
22: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30:
31: /*
1.1 deraadt 32: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
33: * Copyright (c) 1988, 1989 by Adam de Boor
34: * Copyright (c) 1989 by Berkeley Softworks
35: * All rights reserved.
36: *
37: * This code is derived from software contributed to Berkeley by
38: * Adam de Boor.
39: *
40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
48: * 3. All advertising materials mentioning features or use of this software
49: * must display the following acknowledgement:
50: * This product includes software developed by the University of
51: * California, Berkeley and its contributors.
52: * 4. Neither the name of the University nor the names of its contributors
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: */
68:
69: /*-
70: * buf.c --
71: * Functions for automatically-expanded buffers.
72: */
73:
74: #include "sprite.h"
75: #include "make.h"
76: #include "buf.h"
1.13 espie 77:
78: #ifndef lint
79: #if 0
80: static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
81: #else
82: UNUSED
1.14 ! espie 83: static char rcsid[] = "$OpenBSD: buf.c,v 1.13 2000/09/14 13:32:05 espie Exp $";
1.13 espie 84: #endif
85: #endif /* not lint */
1.1 deraadt 86:
1.11 espie 87: /* BufExpand --
1.1 deraadt 88: * Expand the given buffer to hold the given number of additional
1.11 espie 89: * chars. Makes sure there's room for an extra '\0' char at
90: * the end of the buffer to terminate the string. */
91: #define BufExpand(bp,nb) \
92: do { \
93: size_t occupied = (bp)->inPtr - (bp)->buffer; \
94: size_t size = (bp)->endPtr - (bp)->buffer; \
95: \
96: do { \
97: size *= 2 ; \
98: } while (size - occupied < (nb)+1+BUF_MARGIN); \
99: (bp)->buffer = (bp)->inPtr = (bp)->endPtr = \
100: erealloc((bp)->buffer, size); \
101: (bp)->inPtr += occupied; \
102: (bp)->endPtr += size; \
103: } while (0);
1.1 deraadt 104:
105: #define BUF_DEF_SIZE 256 /* Default buffer size */
1.7 espie 106: #define BUF_MARGIN 256 /* Make sure we are comfortable */
1.1 deraadt 107:
1.9 espie 108: /* Buf_AddChar hard case: buffer must be expanded to accommodate
109: * one more char. */
1.1 deraadt 110: void
1.9 espie 111: BufOverflow(bp)
112: Buffer bp;
1.1 deraadt 113: {
1.8 espie 114: BufExpand(bp, 1);
1.1 deraadt 115: }
1.11 espie 116:
1.1 deraadt 117: void
1.8 espie 118: Buf_AddChars(bp, numBytes, bytesPtr)
119: Buffer bp;
120: size_t numBytes;
121: const char *bytesPtr;
1.1 deraadt 122: {
123:
1.11 espie 124: if (bp->endPtr - bp->inPtr < numBytes+1)
1.9 espie 125: BufExpand(bp, numBytes);
1.1 deraadt 126:
1.8 espie 127: memcpy(bp->inPtr, bytesPtr, numBytes);
1.1 deraadt 128: bp->inPtr += numBytes;
129: }
130:
1.12 espie 131: void
132: Buf_Init(bp, size)
133: Buffer bp; /* New Buffer */
1.8 espie 134: size_t size; /* Initial size for the buffer */
1.1 deraadt 135: {
1.14 ! espie 136: if (size == 0)
1.1 deraadt 137: size = BUF_DEF_SIZE;
1.11 espie 138: bp->inPtr = bp->endPtr = bp->buffer = emalloc(size);
139: bp->endPtr += size;
1.4 millert 140: }
1.11 espie 141:
1.4 millert 142: void
1.8 espie 143: Buf_ReplaceLastChar(buf, byte)
144: Buffer buf; /* buffer to augment */
145: char byte; /* byte to be written */
1.4 millert 146: {
1.11 espie 147: if (buf->inPtr == buf->buffer)
1.8 espie 148: Buf_AddChar(buf, byte);
1.4 millert 149: else
150: *(buf->inPtr - 1) = byte;
1.1 deraadt 151: }