Annotation of src/usr.bin/make/buf.c, Revision 1.15
1.15 ! espie 1: /* $OpenPackages$ */
! 2: /* $OpenBSD: buf.c,v 1.7 1999/10/05 21:59:00 espie Exp $ */
! 3: /* $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */
1.1 deraadt 4:
5: /*
1.8 espie 6: * Copyright (c) 1999 Marc Espie.
7: *
1.15 ! espie 8: * Extensive code changes for the OpenBSD project.
1.8 espie 9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
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: *
19: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
23: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.15 ! espie 77: #include "stats.h"
1.13 espie 78:
79: #ifndef lint
80: #if 0
81: static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
82: #else
83: UNUSED
1.15 ! espie 84: static char rcsid[] = "$OpenBSD: buf.c,v 1.7 1999/10/05 21:59:00 espie Exp $";
1.13 espie 85: #endif
86: #endif /* not lint */
1.1 deraadt 87:
1.15 ! espie 88:
! 89: #ifdef STATS_BUF
! 90: #define DO_STAT_BUF(bp, nb) \
! 91: STAT_BUFS_EXPANSION++; \
! 92: if ((bp)->endPtr - (bp)->buffer == 1) \
! 93: STAT_WEIRD_INEFFICIENT++;
! 94: #else
! 95: #define DO_STAT_BUF(a, b)
! 96: #endif
! 97:
1.11 espie 98: /* BufExpand --
1.15 ! espie 99: * Expand the given buffer to hold the given number of additional
! 100: * chars. Makes sure there's room for an extra '\0' char at
! 101: * the end of the buffer to terminate the string. */
! 102: #define BufExpand(bp,nb) \
1.11 espie 103: do { \
104: size_t occupied = (bp)->inPtr - (bp)->buffer; \
105: size_t size = (bp)->endPtr - (bp)->buffer; \
1.15 ! espie 106: DO_STAT_BUF(bp, nb); \
1.11 espie 107: \
1.15 ! espie 108: do { \
! 109: size *= 2 ; \
1.11 espie 110: } while (size - occupied < (nb)+1+BUF_MARGIN); \
111: (bp)->buffer = (bp)->inPtr = (bp)->endPtr = \
1.15 ! espie 112: erealloc((bp)->buffer, size); \
1.11 espie 113: (bp)->inPtr += occupied; \
114: (bp)->endPtr += size; \
115: } while (0);
1.1 deraadt 116:
1.15 ! espie 117: #define BUF_DEF_SIZE 256 /* Default buffer size */
1.7 espie 118: #define BUF_MARGIN 256 /* Make sure we are comfortable */
1.1 deraadt 119:
1.15 ! espie 120: /* Buf_AddChar hard case: buffer must be expanded to accommodate
1.9 espie 121: * one more char. */
1.1 deraadt 122: void
1.9 espie 123: BufOverflow(bp)
124: Buffer bp;
1.1 deraadt 125: {
1.8 espie 126: BufExpand(bp, 1);
1.1 deraadt 127: }
1.11 espie 128:
1.15 ! espie 129:
1.1 deraadt 130: void
1.8 espie 131: Buf_AddChars(bp, numBytes, bytesPtr)
1.15 ! espie 132: Buffer bp;
1.8 espie 133: size_t numBytes;
1.15 ! espie 134: const char *bytesPtr;
1.1 deraadt 135: {
136:
1.11 espie 137: if (bp->endPtr - bp->inPtr < numBytes+1)
1.9 espie 138: BufExpand(bp, numBytes);
1.1 deraadt 139:
1.8 espie 140: memcpy(bp->inPtr, bytesPtr, numBytes);
1.1 deraadt 141: bp->inPtr += numBytes;
142: }
143:
1.15 ! espie 144:
1.12 espie 145: void
146: Buf_Init(bp, size)
1.15 ! espie 147: Buffer bp; /* New Buffer to initialize */
! 148: size_t size; /* Initial size for the buffer */
1.1 deraadt 149: {
1.15 ! espie 150: #ifdef STATS_BUF
! 151: STAT_TOTAL_BUFS++;
! 152: if (size == 0)
! 153: STAT_DEFAULT_BUFS++;
! 154: if (size == 1)
! 155: STAT_WEIRD_BUFS++;
! 156: #endif
1.14 espie 157: if (size == 0)
1.1 deraadt 158: size = BUF_DEF_SIZE;
1.11 espie 159: bp->inPtr = bp->endPtr = bp->buffer = emalloc(size);
160: bp->endPtr += size;
1.4 millert 161: }
1.11 espie 162:
1.4 millert 163: void
1.15 ! espie 164: Buf_KillTrailingSpaces(bp)
! 165: Buffer bp;
1.4 millert 166: {
1.15 ! espie 167: while (bp->inPtr > bp->buffer + 1 && isspace(bp->inPtr[-1])) {
! 168: if (bp->inPtr[-2] == '\\')
! 169: break;
! 170: bp->inPtr--;
! 171: }
1.1 deraadt 172: }