[BACK]Return to buf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/buf.c, Revision 1.8

1.8     ! espie       1: /*     $OpenBSD: buf.c,v 1.7 1999/10/05 21:59:00 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: #ifndef lint
                     70: #if 0
1.4       millert    71: static char sccsid[] = "@(#)buf.c      8.1 (Berkeley) 6/6/93";
1.1       deraadt    72: #else
1.8     ! espie      73: static char rcsid[] = "$OpenBSD: buf.c,v 1.7 1999/10/05 21:59:00 espie Exp $";
1.1       deraadt    74: #endif
                     75: #endif /* not lint */
                     76:
                     77: /*-
                     78:  * buf.c --
                     79:  *     Functions for automatically-expanded buffers.
                     80:  */
                     81:
                     82: #include    "sprite.h"
                     83: #include    "make.h"
                     84: #include    "buf.h"
                     85:
                     86: #ifndef max
                     87: #define max(a,b)  ((a) > (b) ? (a) : (b))
                     88: #endif
                     89:
                     90: /*
                     91:  * BufExpand --
                     92:  *     Expand the given buffer to hold the given number of additional
1.8     ! espie      93:  *     chars.
        !            94:  *     Makes sure there's room for an extra NULL char at the end of the
1.1       deraadt    95:  *     buffer in case it holds a string.
                     96:  */
1.7       espie      97: #define BufExpand(bp,nb)                                               \
                     98:        if (bp->left < (nb)+1) {                                        \
1.8     ! espie      99:            char  *newBuf;                                              \
        !           100:            size_t   newSize = (bp)->size;                              \
1.7       espie     101:                                                                        \
                    102:            do {                                                        \
                    103:                newSize *= 2 ;                                          \
                    104:                (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);    \
                    105:            } while ((bp)->left < (nb)+1+BUF_MARGIN);                   \
1.8     ! espie     106:            newBuf = erealloc((bp)->buffer, newSize);           \
1.7       espie     107:            (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer);        \
                    108:            (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);      \
                    109:            (bp)->buffer = newBuf;                                      \
                    110:            (bp)->size = newSize;                                       \
1.1       deraadt   111:        }
                    112:
                    113: #define BUF_DEF_SIZE   256     /* Default buffer size */
1.7       espie     114: #define BUF_MARGIN     256     /* Make sure we are comfortable */
1.1       deraadt   115:
                    116: /*-
                    117:  *-----------------------------------------------------------------------
1.8     ! espie     118:  * Buf_OvAddChar --
        !           119:  *     Add a single char to the buffer.
1.1       deraadt   120:  *
                    121:  * Side Effects:
                    122:  *     The buffer may be expanded.
                    123:  *
                    124:  *-----------------------------------------------------------------------
                    125:  */
                    126: void
1.8     ! espie     127: Buf_OvAddChar(bp, byte)
        !           128:     Buffer     bp;
        !           129:     char       byte;
1.1       deraadt   130: {
1.8     ! espie     131:     BufExpand(bp, 1);
1.1       deraadt   132:
                    133:     *bp->inPtr++ = byte;
                    134:     bp->left--;
                    135:
                    136:     /*
                    137:      * Null-terminate
                    138:      */
                    139:     *bp->inPtr = 0;
                    140: }
                    141: 
                    142: /*-
                    143:  *-----------------------------------------------------------------------
1.8     ! espie     144:  * Buf_AddChars --
        !           145:  *     Add a number of chars to the buffer.
1.1       deraadt   146:  *
                    147:  * Side Effects:
                    148:  *     Guess what?
                    149:  *
                    150:  *-----------------------------------------------------------------------
                    151:  */
                    152: void
1.8     ! espie     153: Buf_AddChars(bp, numBytes, bytesPtr)
        !           154:     Buffer     bp;
        !           155:     size_t     numBytes;
        !           156:     const char         *bytesPtr;
1.1       deraadt   157: {
                    158:
1.8     ! espie     159:     BufExpand(bp, numBytes);
1.1       deraadt   160:
1.8     ! espie     161:     memcpy(bp->inPtr, bytesPtr, numBytes);
1.1       deraadt   162:     bp->inPtr += numBytes;
                    163:     bp->left -= numBytes;
                    164:
                    165:     /*
                    166:      * Null-terminate
                    167:      */
                    168:     *bp->inPtr = 0;
                    169: }
                    170: 
                    171: /*-
                    172:  *-----------------------------------------------------------------------
                    173:  * Buf_GetAll --
                    174:  *     Get all the available data at once.
                    175:  *
                    176:  * Results:
1.8     ! espie     177:  *     A pointer to the data and the number of chars available.
1.1       deraadt   178:  *
                    179:  *-----------------------------------------------------------------------
                    180:  */
1.8     ! espie     181: char *
        !           182: Buf_GetAll(bp, numBytesPtr)
        !           183:     Buffer     bp;
        !           184:     size_t     *numBytesPtr;
1.1       deraadt   185: {
                    186:
1.8     ! espie     187:     if (numBytesPtr != NULL) {
1.1       deraadt   188:        *numBytesPtr = bp->inPtr - bp->outPtr;
                    189:     }
1.4       millert   190:
1.1       deraadt   191:     return (bp->outPtr);
                    192: }
                    193: 
                    194: /*-
                    195:  *-----------------------------------------------------------------------
                    196:  * Buf_Discard --
1.8     ! espie     197:  *     Throw away chars in a buffer.
1.1       deraadt   198:  *
                    199:  * Side Effects:
1.8     ! espie     200:  *     The chars are discarded.
1.1       deraadt   201:  *
                    202:  *-----------------------------------------------------------------------
                    203:  */
                    204: void
1.8     ! espie     205: Buf_Discard(bp, numBytes)
        !           206:     Buffer     bp;
        !           207:     size_t     numBytes;
1.1       deraadt   208: {
                    209:
                    210:     if (bp->inPtr - bp->outPtr <= numBytes) {
                    211:        bp->inPtr = bp->outPtr = bp->buffer;
                    212:        bp->left = bp->size;
                    213:        *bp->inPtr = 0;
                    214:     } else {
                    215:        bp->outPtr += numBytes;
                    216:     }
                    217: }
                    218: 
                    219: /*-
                    220:  *-----------------------------------------------------------------------
                    221:  * Buf_Size --
1.8     ! espie     222:  *     Returns the number of chars in the given buffer. Doesn't include
        !           223:  *     the null-terminating char.
1.1       deraadt   224:  *
                    225:  * Results:
1.8     ! espie     226:  *     The number of chars.
1.1       deraadt   227:  *
                    228:  *-----------------------------------------------------------------------
                    229:  */
                    230: int
                    231: Buf_Size (buf)
                    232:     Buffer  buf;
                    233: {
                    234:     return (buf->inPtr - buf->outPtr);
                    235: }
                    236: 
                    237: /*-
                    238:  *-----------------------------------------------------------------------
                    239:  * Buf_Init --
                    240:  *     Initialize a buffer. If no initial size is given, a reasonable
                    241:  *     default is used.
                    242:  *
                    243:  * Results:
                    244:  *     A buffer to be given to other functions in this library.
                    245:  *
                    246:  * Side Effects:
                    247:  *     The buffer is created, the space allocated and pointers
                    248:  *     initialized.
                    249:  *
                    250:  *-----------------------------------------------------------------------
                    251:  */
                    252: Buffer
1.8     ! espie     253: Buf_Init(size)
        !           254:     size_t     size;   /* Initial size for the buffer */
1.1       deraadt   255: {
1.8     ! espie     256:     Buffer     bp;     /* New Buffer */
1.1       deraadt   257:
                    258:     bp = (Buffer)emalloc(sizeof(*bp));
                    259:
1.8     ! espie     260:     if (size == 0) {
1.1       deraadt   261:        size = BUF_DEF_SIZE;
                    262:     }
                    263:     bp->left = bp->size = size;
1.8     ! espie     264:     bp->buffer = emalloc(size);
1.1       deraadt   265:     bp->inPtr = bp->outPtr = bp->buffer;
                    266:     *bp->inPtr = 0;
                    267:
                    268:     return (bp);
                    269: }
                    270: 
                    271: /*-
                    272:  *-----------------------------------------------------------------------
                    273:  * Buf_Destroy --
                    274:  *     Nuke a buffer and all its resources.
                    275:  *
                    276:  * Side Effects:
                    277:  *     The buffer is freed.
                    278:  *
                    279:  *-----------------------------------------------------------------------
                    280:  */
                    281: void
1.8     ! espie     282: Buf_Destroy(buf, freeData)
1.1       deraadt   283:     Buffer  buf;       /* Buffer to destroy */
                    284:     Boolean freeData;  /* TRUE if the data should be destroyed as well */
                    285: {
1.4       millert   286:
1.1       deraadt   287:     if (freeData) {
1.8     ! espie     288:        free(buf->buffer);
1.1       deraadt   289:     }
                    290:     free ((char *)buf);
1.4       millert   291: }
                    292: 
                    293: /*-
                    294:  *-----------------------------------------------------------------------
1.8     ! espie     295:  * Buf_ReplaceLastChar --
        !           296:  *     Replace the last char in a buffer.
1.4       millert   297:  *
                    298:  * Side Effects:
                    299:  *     If the buffer was empty intially, then a new byte will be added.
                    300:  *     Otherwise, the last byte is overwritten.
                    301:  *
                    302:  *-----------------------------------------------------------------------
                    303:  */
                    304: void
1.8     ! espie     305: Buf_ReplaceLastChar(buf, byte)
        !           306:     Buffer     buf;    /* buffer to augment */
        !           307:     char       byte;   /* byte to be written */
1.4       millert   308: {
                    309:     if (buf->inPtr == buf->outPtr)
1.8     ! espie     310:         Buf_AddChar(buf, byte);
1.4       millert   311:     else
                    312:         *(buf->inPtr - 1) = byte;
1.1       deraadt   313: }