BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / make / buf.c
index ce60380..fd0b707 100644 (file)
@@ -7,23 +7,37 @@
  * This code is derived from software contributed to Berkeley by
  * Adam de Boor.
  *
  * This code is derived from software contributed to Berkeley by
  * Adam de Boor.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that: (1) source distributions retain this entire copyright
- * notice and comment, and (2) distributions including binaries display
- * the following acknowledgement:  ``This product includes software
- * developed by the University of California, Berkeley and its contributors''
- * in the documentation or other materials provided with the distribution
- * and in all advertising materials mentioning features or use of this
- * software. Neither the name of the University nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)buf.c      5.4 (Berkeley) 6/1/90";
+static char sccsid[] = "@(#)buf.c      5.5 (Berkeley) 12/28/90";
 #endif /* not lint */
 
 /*-
 #endif /* not lint */
 
 /*-
@@ -34,13 +48,6 @@ static char sccsid[] = "@(#)buf.c    5.4 (Berkeley) 6/1/90";
 #include    "sprite.h"
 #include    "buf.h"
 
 #include    "sprite.h"
 #include    "buf.h"
 
-typedef struct {
-    int            size;       /* Current size of the buffer */
-    Byte    *buffer;   /* The buffer itself */
-    Byte    *inPtr;    /* Place to write to */
-    Byte    *outPtr;   /* Place to read from */
-} Buf, *BufPtr;
-
 #ifndef max
 #define max(a,b)  ((a) > (b) ? (a) : (b))
 #endif
 #ifndef max
 #define max(a,b)  ((a) > (b) ? (a) : (b))
 #endif
@@ -53,7 +60,7 @@ typedef struct {
  *     buffer in case it holds a string.
  */
 #define BufExpand(bp,nb) \
  *     buffer in case it holds a string.
  */
 #define BufExpand(bp,nb) \
-       if (((bp)->size - ((bp)->inPtr - (bp)->buffer)) < (nb)+1) {\
+       if (bp->left < (nb)+1) {\
            int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
            Byte  *newBuf = (Byte *) realloc((bp)->buffer, newSize); \
            \
            int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
            Byte  *newBuf = (Byte *) realloc((bp)->buffer, newSize); \
            \
@@ -61,6 +68,7 @@ typedef struct {
            (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
            (bp)->buffer = newBuf;\
            (bp)->size = newSize;\
            (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
            (bp)->buffer = newBuf;\
            (bp)->size = newSize;\
+           (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
        }
 
 #define BUF_DEF_SIZE   256     /* Default buffer size */
        }
 
 #define BUF_DEF_SIZE   256     /* Default buffer size */
@@ -69,8 +77,8 @@ typedef struct {
 
 /*-
  *-----------------------------------------------------------------------
 
 /*-
  *-----------------------------------------------------------------------
- * Buf_AddByte --
- *     Add a single byte to the buffer.
+ * Buf_OvAddByte --
+ *     Add a single byte to the buffer.  left is zero or negative.
  *
  * Results:
  *     None.
  *
  * Results:
  *     None.
@@ -81,16 +89,16 @@ typedef struct {
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_AddByte (buf, byte)
-    Buffer  buf;
+Buf_OvAddByte (bp, byte)
+    register Buffer bp;
     Byte    byte;
 {
     Byte    byte;
 {
-    register BufPtr  bp = (BufPtr) buf;
 
 
+    bp->left = 0;
     BufExpand (bp, 1);
 
     BufExpand (bp, 1);
 
-    *bp->inPtr = byte;
-    bp->inPtr += 1;
+    *bp->inPtr++ = byte;
+    bp->left--;
 
     /*
      * Null-terminate
 
     /*
      * Null-terminate
@@ -112,17 +120,17 @@ Buf_AddByte (buf, byte)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_AddBytes (buf, numBytes, bytesPtr)
-    Buffer  buf;
+Buf_AddBytes (bp, numBytes, bytesPtr)
+    register Buffer bp;
     int            numBytes;
     Byte    *bytesPtr;
 {
     int            numBytes;
     Byte    *bytesPtr;
 {
-    register BufPtr  bp = (BufPtr) buf;
 
     BufExpand (bp, numBytes);
 
     bcopy (bytesPtr, bp->inPtr, numBytes);
     bp->inPtr += numBytes;
 
     BufExpand (bp, numBytes);
 
     bcopy (bytesPtr, bp->inPtr, numBytes);
     bp->inPtr += numBytes;
+    bp->left -= numBytes;
 
     /*
      * Null-terminate
 
     /*
      * Null-terminate
@@ -144,18 +152,18 @@ Buf_AddBytes (buf, numBytes, bytesPtr)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_UngetByte (buf, byte)
-    Buffer  buf;
+Buf_UngetByte (bp, byte)
+    register Buffer bp;
     Byte    byte;
 {
     Byte    byte;
 {
-    register BufPtr    bp = (BufPtr) buf;
 
     if (bp->outPtr != bp->buffer) {
 
     if (bp->outPtr != bp->buffer) {
-       bp->outPtr -= 1;
+       bp->outPtr--;
        *bp->outPtr = byte;
     } else if (bp->outPtr == bp->inPtr) {
        *bp->inPtr = byte;
        *bp->outPtr = byte;
     } else if (bp->outPtr == bp->inPtr) {
        *bp->inPtr = byte;
-       bp->inPtr += 1;
+       bp->inPtr++;
+       bp->left--;
        *bp->inPtr = 0;
     } else {
        /*
        *bp->inPtr = 0;
     } else {
        /*
@@ -168,13 +176,14 @@ Buf_UngetByte (buf, byte)
        Byte      *newBuf;
 
        newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
        Byte      *newBuf;
 
        newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
-       bcopy ((Address)bp->outPtr,
-                       (Address)(newBuf+BUF_UNGET_INC), numBytes+1);
+       bcopy ((char *)bp->outPtr,
+                       (char *)(newBuf+BUF_UNGET_INC), numBytes+1);
        bp->outPtr = newBuf + BUF_UNGET_INC;
        bp->inPtr = bp->outPtr + numBytes;
        bp->outPtr = newBuf + BUF_UNGET_INC;
        bp->inPtr = bp->outPtr + numBytes;
-       free ((Address)bp->buffer);
+       free ((char *)bp->buffer);
        bp->buffer = newBuf;
        bp->size += BUF_UNGET_INC;
        bp->buffer = newBuf;
        bp->size += BUF_UNGET_INC;
+       bp->left = bp->size - (bp->inPtr - bp->buffer);
        bp->outPtr -= 1;
        *bp->outPtr = byte;
     }
        bp->outPtr -= 1;
        *bp->outPtr = byte;
     }
@@ -194,32 +203,32 @@ Buf_UngetByte (buf, byte)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_UngetBytes (buf, numBytes, bytesPtr)
-    Buffer  buf;
+Buf_UngetBytes (bp, numBytes, bytesPtr)
+    register Buffer bp;
     int            numBytes;
     Byte    *bytesPtr;
 {
     int            numBytes;
     Byte    *bytesPtr;
 {
-    register BufPtr    bp = (BufPtr) buf;
 
     if (bp->outPtr - bp->buffer >= numBytes) {
        bp->outPtr -= numBytes;
        bcopy (bytesPtr, bp->outPtr, numBytes);
     } else if (bp->outPtr == bp->inPtr) {
 
     if (bp->outPtr - bp->buffer >= numBytes) {
        bp->outPtr -= numBytes;
        bcopy (bytesPtr, bp->outPtr, numBytes);
     } else if (bp->outPtr == bp->inPtr) {
-       Buf_AddBytes (buf, numBytes, bytesPtr);
+       Buf_AddBytes (bp, numBytes, bytesPtr);
     } else {
        int       curNumBytes = bp->inPtr - bp->outPtr;
        Byte      *newBuf;
        int       newBytes = max(numBytes,BUF_UNGET_INC);
 
        newBuf = (Byte *)emalloc (bp->size + newBytes);
     } else {
        int       curNumBytes = bp->inPtr - bp->outPtr;
        Byte      *newBuf;
        int       newBytes = max(numBytes,BUF_UNGET_INC);
 
        newBuf = (Byte *)emalloc (bp->size + newBytes);
-       bcopy((Address)bp->outPtr, (Address)(newBuf+newBytes), curNumBytes+1);
+       bcopy((char *)bp->outPtr, (char *)(newBuf+newBytes), curNumBytes+1);
        bp->outPtr = newBuf + newBytes;
        bp->inPtr = bp->outPtr + curNumBytes;
        bp->outPtr = newBuf + newBytes;
        bp->inPtr = bp->outPtr + curNumBytes;
-       free ((Address)bp->buffer);
+       free ((char *)bp->buffer);
        bp->buffer = newBuf;
        bp->size += newBytes;
        bp->buffer = newBuf;
        bp->size += newBytes;
+       bp->left = bp->size - (bp->inPtr - bp->buffer);
        bp->outPtr -= numBytes;
        bp->outPtr -= numBytes;
-       bcopy ((Address)bytesPtr, (Address)bp->outPtr, numBytes);
+       bcopy ((char *)bytesPtr, (char *)bp->outPtr, numBytes);
     }
 }
 \f
     }
 }
 \f
@@ -239,10 +248,9 @@ Buf_UngetBytes (buf, numBytes, bytesPtr)
  *-----------------------------------------------------------------------
  */
 int
  *-----------------------------------------------------------------------
  */
 int
-Buf_GetByte (buf)
-    Buffer  buf;
+Buf_GetByte (bp)
+    register Buffer bp;
 {
 {
-    BufPtr  bp = (BufPtr) buf;
     int            res;
 
     if (bp->inPtr == bp->outPtr) {
     int            res;
 
     if (bp->inPtr == bp->outPtr) {
@@ -252,7 +260,8 @@ Buf_GetByte (buf)
        bp->outPtr += 1;
        if (bp->outPtr == bp->inPtr) {
            bp->outPtr = bp->inPtr = bp->buffer;
        bp->outPtr += 1;
        if (bp->outPtr == bp->inPtr) {
            bp->outPtr = bp->inPtr = bp->buffer;
-           bp->inPtr = 0;
+           bp->left = bp->size;
+           *bp->inPtr = 0;
        }
        return (res);
     }
        }
        return (res);
     }
@@ -272,12 +281,11 @@ Buf_GetByte (buf)
  *-----------------------------------------------------------------------
  */
 int
  *-----------------------------------------------------------------------
  */
 int
-Buf_GetBytes (buf, numBytes, bytesPtr)
-    Buffer  buf;
+Buf_GetBytes (bp, numBytes, bytesPtr)
+    register Buffer bp;
     int            numBytes;
     Byte    *bytesPtr;
 {
     int            numBytes;
     Byte    *bytesPtr;
 {
-    BufPtr  bp = (BufPtr) buf;
     
     if (bp->inPtr - bp->outPtr < numBytes) {
        numBytes = bp->inPtr - bp->outPtr;
     
     if (bp->inPtr - bp->outPtr < numBytes) {
        numBytes = bp->inPtr - bp->outPtr;
@@ -287,6 +295,7 @@ Buf_GetBytes (buf, numBytes, bytesPtr)
 
     if (bp->outPtr == bp->inPtr) {
        bp->outPtr = bp->inPtr = bp->buffer;
 
     if (bp->outPtr == bp->inPtr) {
        bp->outPtr = bp->inPtr = bp->buffer;
+       bp->left = bp->size;
        *bp->inPtr = 0;
     }
     return (numBytes);
        *bp->inPtr = 0;
     }
     return (numBytes);
@@ -306,11 +315,10 @@ Buf_GetBytes (buf, numBytes, bytesPtr)
  *-----------------------------------------------------------------------
  */
 Byte *
  *-----------------------------------------------------------------------
  */
 Byte *
-Buf_GetAll (buf, numBytesPtr)
-    Buffer  buf;
+Buf_GetAll (bp, numBytesPtr)
+    register Buffer bp;
     int            *numBytesPtr;
 {
     int            *numBytesPtr;
 {
-    BufPtr  bp = (BufPtr)buf;
 
     if (numBytesPtr != (int *)NULL) {
        *numBytesPtr = bp->inPtr - bp->outPtr;
 
     if (numBytesPtr != (int *)NULL) {
        *numBytesPtr = bp->inPtr - bp->outPtr;
@@ -333,14 +341,14 @@ Buf_GetAll (buf, numBytesPtr)
  *-----------------------------------------------------------------------
  */
 void
  *-----------------------------------------------------------------------
  */
 void
-Buf_Discard (buf, numBytes)
-    Buffer  buf;
+Buf_Discard (bp, numBytes)
+    register Buffer bp;
     int            numBytes;
 {
     int            numBytes;
 {
-    register BufPtr    bp = (BufPtr) buf;
 
     if (bp->inPtr - bp->outPtr <= numBytes) {
        bp->inPtr = bp->outPtr = bp->buffer;
 
     if (bp->inPtr - bp->outPtr <= numBytes) {
        bp->inPtr = bp->outPtr = bp->buffer;
+       bp->left = bp->size;
        *bp->inPtr = 0;
     } else {
        bp->outPtr += numBytes;
        *bp->inPtr = 0;
     } else {
        bp->outPtr += numBytes;
@@ -365,7 +373,7 @@ int
 Buf_Size (buf)
     Buffer  buf;
 {
 Buf_Size (buf)
     Buffer  buf;
 {
-    return (((BufPtr)buf)->inPtr - ((BufPtr)buf)->outPtr);
+    return (buf->inPtr - buf->outPtr);
 }
 \f
 /*-
 }
 \f
 /*-
@@ -387,19 +395,19 @@ Buffer
 Buf_Init (size)
     int            size;       /* Initial size for the buffer */
 {
 Buf_Init (size)
     int            size;       /* Initial size for the buffer */
 {
-    BufPtr  bp;                /* New Buffer */
+    Buffer bp;         /* New Buffer */
 
 
-    bp = (Buf *)emalloc(sizeof(Buf));
+    bp = (Buffer)emalloc(sizeof(*bp));
 
     if (size <= 0) {
        size = BUF_DEF_SIZE;
     }
 
     if (size <= 0) {
        size = BUF_DEF_SIZE;
     }
-    bp->size = size;
-    bp->buffer = (Byte *)emalloc (size);
+    bp->left = bp->size = size;
+    bp->buffer = (Byte *)emalloc(size);
     bp->inPtr = bp->outPtr = bp->buffer;
     *bp->inPtr = 0;
 
     bp->inPtr = bp->outPtr = bp->buffer;
     *bp->inPtr = 0;
 
-    return ((Buffer) bp);
+    return (bp);
 }
 \f
 /*-
 }
 \f
 /*-
@@ -420,10 +428,9 @@ Buf_Destroy (buf, freeData)
     Buffer  buf;       /* Buffer to destroy */
     Boolean freeData;  /* TRUE if the data should be destroyed as well */
 {
     Buffer  buf;       /* Buffer to destroy */
     Boolean freeData;  /* TRUE if the data should be destroyed as well */
 {
-    BufPtr  bp = (BufPtr) buf;
     
     if (freeData) {
     
     if (freeData) {
-       free ((Address)bp->buffer);
+       free ((char *)buf->buffer);
     }
     }
-    free ((Address)bp);
+    free ((char *)buf);
 }
 }