diff options
Diffstat (limited to 'usr.bin/make/buf.c')
-rw-r--r-- | usr.bin/make/buf.c | 257 |
1 files changed, 128 insertions, 129 deletions
diff --git a/usr.bin/make/buf.c b/usr.bin/make/buf.c index 84b62fd..3d43423 100644 --- a/usr.bin/make/buf.c +++ b/usr.bin/make/buf.c @@ -47,9 +47,9 @@ __FBSDID("$FreeBSD$"); * Functions for automatically-expanded buffers. */ -#include "sprite.h" -#include "make.h" -#include "buf.h" +#include "sprite.h" +#include "make.h" +#include "buf.h" #ifndef max #define max(a,b) ((a) > (b) ? (a) : (b)) @@ -62,17 +62,18 @@ __FBSDID("$FreeBSD$"); * Makes sure there's room for an extra NULL byte at the end of the * buffer in case it holds a string. */ -#define BufExpand(bp,nb) \ - if (bp->left < (nb)+1) {\ - int newSize = (bp)->size + max((nb) + 1, BUF_ADD_INC); \ - Byte *newBuf = erealloc((bp)->buffer, newSize); \ - \ - (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \ - (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\ - (bp)->buffer = newBuf;\ - (bp)->size = newSize;\ - (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\ - } +#define BufExpand(bp, nb) do { \ + if (bp->left < (nb) + 1) { \ + int newSize = (bp)->size + max((nb) + 1, BUF_ADD_INC); \ + Byte *newBuf = erealloc((bp)->buffer, newSize); \ + \ + (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \ + (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer); \ + (bp)->buffer = newBuf; \ + (bp)->size = newSize; \ + (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer); \ + } \ + } while (0) #define BUF_DEF_SIZE 256 /* Default buffer size */ #define BUF_ADD_INC 256 /* Expansion increment when Adding */ @@ -95,16 +96,16 @@ void Buf_OvAddByte(Buffer bp, int byte) { - bp->left = 0; - BufExpand(bp, 1); + bp->left = 0; + BufExpand(bp, 1); - *bp->inPtr++ = byte; - bp->left--; + *bp->inPtr++ = byte; + bp->left--; - /* - * Null-terminate - */ - *bp->inPtr = 0; + /* + * Null-terminate + */ + *bp->inPtr = 0; } /*- @@ -124,16 +125,16 @@ void Buf_AddBytes(Buffer bp, int numBytes, const Byte *bytesPtr) { - BufExpand(bp, numBytes); + BufExpand(bp, numBytes); - memcpy(bp->inPtr, bytesPtr, numBytes); - bp->inPtr += numBytes; - bp->left -= numBytes; + memcpy(bp->inPtr, bytesPtr, numBytes); + bp->inPtr += numBytes; + bp->left -= numBytes; - /* - * Null-terminate - */ - *bp->inPtr = 0; + /* + * Null-terminate + */ + *bp->inPtr = 0; } /*- @@ -153,35 +154,37 @@ void Buf_UngetByte(Buffer bp, int byte) { - if (bp->outPtr != bp->buffer) { - bp->outPtr--; - *bp->outPtr = byte; - } else if (bp->outPtr == bp->inPtr) { - *bp->inPtr = byte; - bp->inPtr++; - bp->left--; - *bp->inPtr = 0; - } else { - /* - * Yech. have to expand the buffer to stuff this thing in. - * We use a different expansion constant because people don't - * usually push back many bytes when they're doing it a byte at - * a time... - */ - int numBytes = bp->inPtr - bp->outPtr; - Byte *newBuf; - - newBuf = emalloc(bp->size + BUF_UNGET_INC); - memcpy(newBuf + BUF_UNGET_INC, bp->outPtr, numBytes + 1); - bp->outPtr = newBuf + BUF_UNGET_INC; - bp->inPtr = bp->outPtr + numBytes; - free(bp->buffer); - bp->buffer = newBuf; - bp->size += BUF_UNGET_INC; - bp->left = bp->size - (bp->inPtr - bp->buffer); - bp->outPtr -= 1; - *bp->outPtr = byte; - } + if (bp->outPtr != bp->buffer) { + bp->outPtr--; + *bp->outPtr = byte; + + } else if (bp->outPtr == bp->inPtr) { + *bp->inPtr = byte; + bp->inPtr++; + bp->left--; + *bp->inPtr = 0; + + } else { + /* + * Yech. have to expand the buffer to stuff this thing in. + * We use a different expansion constant because people don't + * usually push back many bytes when they're doing it a byte at + * a time... + */ + int numBytes = bp->inPtr - bp->outPtr; + Byte *newBuf; + + newBuf = emalloc(bp->size + BUF_UNGET_INC); + memcpy(newBuf + BUF_UNGET_INC, bp->outPtr, numBytes + 1); + bp->outPtr = newBuf + BUF_UNGET_INC; + bp->inPtr = bp->outPtr + numBytes; + free(bp->buffer); + bp->buffer = newBuf; + bp->size += BUF_UNGET_INC; + bp->left = bp->size - (bp->inPtr - bp->buffer); + bp->outPtr -= 1; + *bp->outPtr = byte; + } } /*- @@ -201,27 +204,27 @@ void Buf_UngetBytes(Buffer bp, int numBytes, Byte *bytesPtr) { - if (bp->outPtr - bp->buffer >= numBytes) { - bp->outPtr -= numBytes; - memcpy(bp->outPtr, bytesPtr, numBytes); - } else if (bp->outPtr == bp->inPtr) { - Buf_AddBytes(bp, numBytes, bytesPtr); - } else { - int curNumBytes = bp->inPtr - bp->outPtr; - Byte *newBuf; - int newBytes = max(numBytes,BUF_UNGET_INC); - - newBuf = emalloc(bp->size + newBytes); - memcpy(newBuf + newBytes, bp->outPtr, curNumBytes + 1); - bp->outPtr = newBuf + newBytes; - bp->inPtr = bp->outPtr + curNumBytes; - free(bp->buffer); - bp->buffer = newBuf; - bp->size += newBytes; - bp->left = bp->size - (bp->inPtr - bp->buffer); - bp->outPtr -= numBytes; - memcpy(bp->outPtr, bytesPtr, numBytes); - } + if (bp->outPtr - bp->buffer >= numBytes) { + bp->outPtr -= numBytes; + memcpy(bp->outPtr, bytesPtr, numBytes); + } else if (bp->outPtr == bp->inPtr) { + Buf_AddBytes(bp, numBytes, bytesPtr); + } else { + int curNumBytes = bp->inPtr - bp->outPtr; + Byte *newBuf; + int newBytes = max(numBytes, BUF_UNGET_INC); + + newBuf = emalloc(bp->size + newBytes); + memcpy(newBuf + newBytes, bp->outPtr, curNumBytes + 1); + bp->outPtr = newBuf + newBytes; + bp->inPtr = bp->outPtr + curNumBytes; + free(bp->buffer); + bp->buffer = newBuf; + bp->size += newBytes; + bp->left = bp->size - (bp->inPtr - bp->buffer); + bp->outPtr -= numBytes; + memcpy(bp->outPtr, bytesPtr, numBytes); + } } /*- @@ -242,20 +245,19 @@ Buf_UngetBytes(Buffer bp, int numBytes, Byte *bytesPtr) int Buf_GetByte(Buffer bp) { - int res; + int res; + + if (bp->inPtr == bp->outPtr) + return (BUF_ERROR); - if (bp->inPtr == bp->outPtr) { - return (BUF_ERROR); - } else { res = (int)*bp->outPtr; bp->outPtr += 1; if (bp->outPtr == bp->inPtr) { - bp->outPtr = bp->inPtr = bp->buffer; - bp->left = bp->size; - *bp->inPtr = 0; + bp->outPtr = bp->inPtr = bp->buffer; + bp->left = bp->size; + *bp->inPtr = 0; } return (res); - } } /*- @@ -275,18 +277,18 @@ int Buf_GetBytes(Buffer bp, int numBytes, Byte *bytesPtr) { - if (bp->inPtr - bp->outPtr < numBytes) { - numBytes = bp->inPtr - bp->outPtr; - } - memcpy(bytesPtr, bp->outPtr, numBytes); - bp->outPtr += numBytes; + if (bp->inPtr - bp->outPtr < numBytes) + numBytes = bp->inPtr - bp->outPtr; - if (bp->outPtr == bp->inPtr) { - bp->outPtr = bp->inPtr = bp->buffer; - bp->left = bp->size; - *bp->inPtr = 0; - } - return (numBytes); + memcpy(bytesPtr, bp->outPtr, numBytes); + bp->outPtr += numBytes; + + if (bp->outPtr == bp->inPtr) { + bp->outPtr = bp->inPtr = bp->buffer; + bp->left = bp->size; + *bp->inPtr = 0; + } + return (numBytes); } /*- @@ -306,11 +308,10 @@ Byte * Buf_GetAll(Buffer bp, int *numBytesPtr) { - if (numBytesPtr != NULL) { - *numBytesPtr = bp->inPtr - bp->outPtr; - } + if (numBytesPtr != NULL) + *numBytesPtr = bp->inPtr - bp->outPtr; - return (bp->outPtr); + return (bp->outPtr); } /*- @@ -330,13 +331,12 @@ void Buf_Discard(Buffer bp, int numBytes) { - if (bp->inPtr - bp->outPtr <= numBytes) { - bp->inPtr = bp->outPtr = bp->buffer; - bp->left = bp->size; - *bp->inPtr = 0; - } else { - bp->outPtr += numBytes; - } + if (bp->inPtr - bp->outPtr <= numBytes) { + bp->inPtr = bp->outPtr = bp->buffer; + bp->left = bp->size; + *bp->inPtr = 0; + } else + bp->outPtr += numBytes; } /*- @@ -357,7 +357,7 @@ int Buf_Size(Buffer buf) { - return (buf->inPtr - buf->outPtr); + return (buf->inPtr - buf->outPtr); } /*- @@ -378,19 +378,19 @@ Buf_Size(Buffer buf) Buffer Buf_Init(int size) { - Buffer bp; /* New Buffer */ + Buffer bp; /* New Buffer */ - bp = emalloc(sizeof(*bp)); + bp = emalloc(sizeof(*bp)); - if (size <= 0) { - size = BUF_DEF_SIZE; - } - bp->left = bp->size = size; - bp->buffer = emalloc(size); - bp->inPtr = bp->outPtr = bp->buffer; - *bp->inPtr = 0; + if (size <= 0) + size = BUF_DEF_SIZE; + + bp->left = bp->size = size; + bp->buffer = emalloc(size); + bp->inPtr = bp->outPtr = bp->buffer; + *bp->inPtr = 0; - return (bp); + return (bp); } /*- @@ -410,10 +410,9 @@ void Buf_Destroy(Buffer buf, Boolean freeData) { - if (freeData) { - free(buf->buffer); - } - free(buf); + if (freeData) + free(buf->buffer); + free(buf); } /*- @@ -433,8 +432,8 @@ Buf_Destroy(Buffer buf, Boolean freeData) void Buf_ReplaceLastByte(Buffer buf, int byte) { - if (buf->inPtr == buf->outPtr) - Buf_AddByte(buf, byte); - else - *(buf->inPtr - 1) = byte; + if (buf->inPtr == buf->outPtr) + Buf_AddByte(buf, byte); + else + *(buf->inPtr - 1) = byte; } |