| 1 | /*
|
|---|
| 2 | * Copyright (c) 1988, 1989, 1990, 1993
|
|---|
| 3 | * The Regents of the University of California. All rights reserved.
|
|---|
| 4 | * Copyright (c) 1988, 1989 by Adam de Boor
|
|---|
| 5 | * Copyright (c) 1989 by Berkeley Softworks
|
|---|
| 6 | * All rights reserved.
|
|---|
| 7 | *
|
|---|
| 8 | * This code is derived from software contributed to Berkeley by
|
|---|
| 9 | * Adam de Boor.
|
|---|
| 10 | *
|
|---|
| 11 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 12 | * modification, are permitted provided that the following conditions
|
|---|
| 13 | * are met:
|
|---|
| 14 | * 1. Redistributions of source code must retain the above copyright
|
|---|
| 15 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 16 | * 2. Redistributions in binary form must reproduce the above copyright
|
|---|
| 17 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 18 | * documentation and/or other materials provided with the distribution.
|
|---|
| 19 | * 3. All advertising materials mentioning features or use of this software
|
|---|
| 20 | * must display the following acknowledgement:
|
|---|
| 21 | * This product includes software developed by the University of
|
|---|
| 22 | * California, Berkeley and its contributors.
|
|---|
| 23 | * 4. Neither the name of the University nor the names of its contributors
|
|---|
| 24 | * may be used to endorse or promote products derived from this software
|
|---|
| 25 | * without specific prior written permission.
|
|---|
| 26 | *
|
|---|
| 27 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|---|
| 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|---|
| 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|---|
| 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|---|
| 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|---|
| 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|---|
| 37 | * SUCH DAMAGE.
|
|---|
| 38 | *
|
|---|
| 39 | * @(#)buf.c 8.1 (Berkeley) 6/6/93
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | #include <sys/cdefs.h>
|
|---|
| 43 | __FBSDID("$FreeBSD: src/usr.bin/make/buf.c,v 1.18 2002/10/09 03:42:09 jmallett Exp $");
|
|---|
| 44 |
|
|---|
| 45 | /*-
|
|---|
| 46 | * buf.c --
|
|---|
| 47 | * Functions for automatically-expanded buffers.
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | #include "sprite.h"
|
|---|
| 51 | #include "make.h"
|
|---|
| 52 | #include "buf.h"
|
|---|
| 53 |
|
|---|
| 54 | #ifndef max
|
|---|
| 55 | #define max(a,b) ((a) > (b) ? (a) : (b))
|
|---|
| 56 | #endif
|
|---|
| 57 |
|
|---|
| 58 | /*
|
|---|
| 59 | * BufExpand --
|
|---|
| 60 | * Expand the given buffer to hold the given number of additional
|
|---|
| 61 | * bytes.
|
|---|
| 62 | * Makes sure there's room for an extra NULL byte at the end of the
|
|---|
| 63 | * buffer in case it holds a string.
|
|---|
| 64 | */
|
|---|
| 65 | #define BufExpand(bp,nb) \
|
|---|
| 66 | if (bp->left < (nb)+1) {\
|
|---|
| 67 | int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
|
|---|
| 68 | Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
|
|---|
| 69 | \
|
|---|
| 70 | (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
|
|---|
| 71 | (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
|
|---|
| 72 | (bp)->buffer = newBuf;\
|
|---|
| 73 | (bp)->size = newSize;\
|
|---|
| 74 | (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | #define BUF_DEF_SIZE 256 /* Default buffer size */
|
|---|
| 78 | #define BUF_ADD_INC 256 /* Expansion increment when Adding */
|
|---|
| 79 | #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
|
|---|
| 80 |
|
|---|
| 81 | /*-
|
|---|
| 82 | *-----------------------------------------------------------------------
|
|---|
| 83 | * Buf_OvAddByte --
|
|---|
| 84 | * Add a single byte to the buffer. left is zero or negative.
|
|---|
| 85 | *
|
|---|
| 86 | * Results:
|
|---|
| 87 | * None.
|
|---|
| 88 | *
|
|---|
| 89 | * Side Effects:
|
|---|
| 90 | * The buffer may be expanded.
|
|---|
| 91 | *
|
|---|
| 92 | *-----------------------------------------------------------------------
|
|---|
| 93 | */
|
|---|
| 94 | void
|
|---|
| 95 | Buf_OvAddByte (Buffer bp, int byte)
|
|---|
| 96 | {
|
|---|
| 97 | bp->left = 0;
|
|---|
| 98 | BufExpand (bp, 1);
|
|---|
| 99 |
|
|---|
| 100 | *bp->inPtr++ = byte;
|
|---|
| 101 | bp->left--;
|
|---|
| 102 |
|
|---|
| 103 | /*
|
|---|
| 104 | * Null-terminate
|
|---|
| 105 | */
|
|---|
| 106 | *bp->inPtr = 0;
|
|---|
| 107 | }
|
|---|
| 108 | |
|---|
| 109 |
|
|---|
| 110 | /*-
|
|---|
| 111 | *-----------------------------------------------------------------------
|
|---|
| 112 | * Buf_AddBytes --
|
|---|
| 113 | * Add a number of bytes to the buffer.
|
|---|
| 114 | *
|
|---|
| 115 | * Results:
|
|---|
| 116 | * None.
|
|---|
| 117 | *
|
|---|
| 118 | * Side Effects:
|
|---|
| 119 | * Guess what?
|
|---|
| 120 | *
|
|---|
| 121 | *-----------------------------------------------------------------------
|
|---|
| 122 | */
|
|---|
| 123 | void
|
|---|
| 124 | Buf_AddBytes (Buffer bp, int numBytes, const Byte *bytesPtr)
|
|---|
| 125 | {
|
|---|
| 126 |
|
|---|
| 127 | BufExpand (bp, numBytes);
|
|---|
| 128 |
|
|---|
| 129 | memcpy (bp->inPtr, bytesPtr, numBytes);
|
|---|
| 130 | bp->inPtr += numBytes;
|
|---|
| 131 | bp->left -= numBytes;
|
|---|
| 132 |
|
|---|
| 133 | /*
|
|---|
| 134 | * Null-terminate
|
|---|
| 135 | */
|
|---|
| 136 | *bp->inPtr = 0;
|
|---|
| 137 | }
|
|---|
| 138 | |
|---|
| 139 |
|
|---|
| 140 | /*-
|
|---|
| 141 | *-----------------------------------------------------------------------
|
|---|
| 142 | * Buf_UngetByte --
|
|---|
| 143 | * Place the byte back at the beginning of the buffer.
|
|---|
| 144 | *
|
|---|
| 145 | * Results:
|
|---|
| 146 | * SUCCESS if the byte was added ok. FAILURE if not.
|
|---|
| 147 | *
|
|---|
| 148 | * Side Effects:
|
|---|
| 149 | * The byte is stuffed in the buffer and outPtr is decremented.
|
|---|
| 150 | *
|
|---|
| 151 | *-----------------------------------------------------------------------
|
|---|
| 152 | */
|
|---|
| 153 | void
|
|---|
| 154 | Buf_UngetByte (Buffer bp, int byte)
|
|---|
| 155 | {
|
|---|
| 156 |
|
|---|
| 157 | if (bp->outPtr != bp->buffer) {
|
|---|
| 158 | bp->outPtr--;
|
|---|
| 159 | *bp->outPtr = byte;
|
|---|
| 160 | } else if (bp->outPtr == bp->inPtr) {
|
|---|
| 161 | *bp->inPtr = byte;
|
|---|
| 162 | bp->inPtr++;
|
|---|
| 163 | bp->left--;
|
|---|
| 164 | *bp->inPtr = 0;
|
|---|
| 165 | } else {
|
|---|
| 166 | /*
|
|---|
| 167 | * Yech. have to expand the buffer to stuff this thing in.
|
|---|
| 168 | * We use a different expansion constant because people don't
|
|---|
| 169 | * usually push back many bytes when they're doing it a byte at
|
|---|
| 170 | * a time...
|
|---|
| 171 | */
|
|---|
| 172 | int numBytes = bp->inPtr - bp->outPtr;
|
|---|
| 173 | Byte *newBuf;
|
|---|
| 174 |
|
|---|
| 175 | newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
|
|---|
| 176 | memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
|
|---|
| 177 | bp->outPtr = newBuf + BUF_UNGET_INC;
|
|---|
| 178 | bp->inPtr = bp->outPtr + numBytes;
|
|---|
| 179 | free ((char *)bp->buffer);
|
|---|
| 180 | bp->buffer = newBuf;
|
|---|
| 181 | bp->size += BUF_UNGET_INC;
|
|---|
| 182 | bp->left = bp->size - (bp->inPtr - bp->buffer);
|
|---|
| 183 | bp->outPtr -= 1;
|
|---|
| 184 | *bp->outPtr = byte;
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | |
|---|
| 188 |
|
|---|
| 189 | /*-
|
|---|
| 190 | *-----------------------------------------------------------------------
|
|---|
| 191 | * Buf_UngetBytes --
|
|---|
| 192 | * Push back a series of bytes at the beginning of the buffer.
|
|---|
| 193 | *
|
|---|
| 194 | * Results:
|
|---|
| 195 | * None.
|
|---|
| 196 | *
|
|---|
| 197 | * Side Effects:
|
|---|
| 198 | * outPtr is decremented and the bytes copied into the buffer.
|
|---|
| 199 | *
|
|---|
| 200 | *-----------------------------------------------------------------------
|
|---|
| 201 | */
|
|---|
| 202 | void
|
|---|
| 203 | Buf_UngetBytes (Buffer bp, int numBytes, Byte *bytesPtr)
|
|---|
| 204 | {
|
|---|
| 205 |
|
|---|
| 206 | if (bp->outPtr - bp->buffer >= numBytes) {
|
|---|
| 207 | bp->outPtr -= numBytes;
|
|---|
| 208 | memcpy (bp->outPtr, bytesPtr, numBytes);
|
|---|
| 209 | } else if (bp->outPtr == bp->inPtr) {
|
|---|
| 210 | Buf_AddBytes (bp, numBytes, bytesPtr);
|
|---|
| 211 | } else {
|
|---|
| 212 | int curNumBytes = bp->inPtr - bp->outPtr;
|
|---|
| 213 | Byte *newBuf;
|
|---|
| 214 | int newBytes = max(numBytes,BUF_UNGET_INC);
|
|---|
| 215 |
|
|---|
| 216 | newBuf = (Byte *)emalloc (bp->size + newBytes);
|
|---|
| 217 | memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
|
|---|
| 218 | bp->outPtr = newBuf + newBytes;
|
|---|
| 219 | bp->inPtr = bp->outPtr + curNumBytes;
|
|---|
| 220 | free ((char *)bp->buffer);
|
|---|
| 221 | bp->buffer = newBuf;
|
|---|
| 222 | bp->size += newBytes;
|
|---|
| 223 | bp->left = bp->size - (bp->inPtr - bp->buffer);
|
|---|
| 224 | bp->outPtr -= numBytes;
|
|---|
| 225 | memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 | |
|---|
| 229 |
|
|---|
| 230 | /*-
|
|---|
| 231 | *-----------------------------------------------------------------------
|
|---|
| 232 | * Buf_GetByte --
|
|---|
| 233 | * Return the next byte from the buffer. Actually returns an integer.
|
|---|
| 234 | *
|
|---|
| 235 | * Results:
|
|---|
| 236 | * Returns BUF_ERROR if there's no byte in the buffer, or the byte
|
|---|
| 237 | * itself if there is one.
|
|---|
| 238 | *
|
|---|
| 239 | * Side Effects:
|
|---|
| 240 | * outPtr is incremented and both outPtr and inPtr will be reset if
|
|---|
| 241 | * the buffer is emptied.
|
|---|
| 242 | *
|
|---|
| 243 | *-----------------------------------------------------------------------
|
|---|
| 244 | */
|
|---|
| 245 | int
|
|---|
| 246 | Buf_GetByte (Buffer bp)
|
|---|
| 247 | {
|
|---|
| 248 | int res;
|
|---|
| 249 |
|
|---|
| 250 | if (bp->inPtr == bp->outPtr) {
|
|---|
| 251 | return (BUF_ERROR);
|
|---|
| 252 | } else {
|
|---|
| 253 | res = (int) *bp->outPtr;
|
|---|
| 254 | bp->outPtr += 1;
|
|---|
| 255 | if (bp->outPtr == bp->inPtr) {
|
|---|
| 256 | bp->outPtr = bp->inPtr = bp->buffer;
|
|---|
| 257 | bp->left = bp->size;
|
|---|
| 258 | *bp->inPtr = 0;
|
|---|
| 259 | }
|
|---|
| 260 | return (res);
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 | |
|---|
| 264 |
|
|---|
| 265 | /*-
|
|---|
| 266 | *-----------------------------------------------------------------------
|
|---|
| 267 | * Buf_GetBytes --
|
|---|
| 268 | * Extract a number of bytes from the buffer.
|
|---|
| 269 | *
|
|---|
| 270 | * Results:
|
|---|
| 271 | * The number of bytes gotten.
|
|---|
| 272 | *
|
|---|
| 273 | * Side Effects:
|
|---|
| 274 | * The passed array is overwritten.
|
|---|
| 275 | *
|
|---|
| 276 | *-----------------------------------------------------------------------
|
|---|
| 277 | */
|
|---|
| 278 | int
|
|---|
| 279 | Buf_GetBytes (Buffer bp, int numBytes, Byte *bytesPtr)
|
|---|
| 280 | {
|
|---|
| 281 |
|
|---|
| 282 | if (bp->inPtr - bp->outPtr < numBytes) {
|
|---|
| 283 | numBytes = bp->inPtr - bp->outPtr;
|
|---|
| 284 | }
|
|---|
| 285 | memcpy (bytesPtr, bp->outPtr, numBytes);
|
|---|
| 286 | bp->outPtr += numBytes;
|
|---|
| 287 |
|
|---|
| 288 | if (bp->outPtr == bp->inPtr) {
|
|---|
| 289 | bp->outPtr = bp->inPtr = bp->buffer;
|
|---|
| 290 | bp->left = bp->size;
|
|---|
| 291 | *bp->inPtr = 0;
|
|---|
| 292 | }
|
|---|
| 293 | return (numBytes);
|
|---|
| 294 | }
|
|---|
| 295 | |
|---|
| 296 |
|
|---|
| 297 | /*-
|
|---|
| 298 | *-----------------------------------------------------------------------
|
|---|
| 299 | * Buf_GetAll --
|
|---|
| 300 | * Get all the available data at once.
|
|---|
| 301 | *
|
|---|
| 302 | * Results:
|
|---|
| 303 | * A pointer to the data and the number of bytes available.
|
|---|
| 304 | *
|
|---|
| 305 | * Side Effects:
|
|---|
| 306 | * None.
|
|---|
| 307 | *
|
|---|
| 308 | *-----------------------------------------------------------------------
|
|---|
| 309 | */
|
|---|
| 310 | Byte *
|
|---|
| 311 | Buf_GetAll (Buffer bp, int *numBytesPtr)
|
|---|
| 312 | {
|
|---|
| 313 |
|
|---|
| 314 | if (numBytesPtr != (int *)NULL) {
|
|---|
| 315 | *numBytesPtr = bp->inPtr - bp->outPtr;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | return (bp->outPtr);
|
|---|
| 319 | }
|
|---|
| 320 | |
|---|
| 321 |
|
|---|
| 322 | /*-
|
|---|
| 323 | *-----------------------------------------------------------------------
|
|---|
| 324 | * Buf_Discard --
|
|---|
| 325 | * Throw away bytes in a buffer.
|
|---|
| 326 | *
|
|---|
| 327 | * Results:
|
|---|
| 328 | * None.
|
|---|
| 329 | *
|
|---|
| 330 | * Side Effects:
|
|---|
| 331 | * The bytes are discarded.
|
|---|
| 332 | *
|
|---|
| 333 | *-----------------------------------------------------------------------
|
|---|
| 334 | */
|
|---|
| 335 | void
|
|---|
| 336 | Buf_Discard (Buffer bp, int numBytes)
|
|---|
| 337 | {
|
|---|
| 338 |
|
|---|
| 339 | if (bp->inPtr - bp->outPtr <= numBytes) {
|
|---|
| 340 | bp->inPtr = bp->outPtr = bp->buffer;
|
|---|
| 341 | bp->left = bp->size;
|
|---|
| 342 | *bp->inPtr = 0;
|
|---|
| 343 | } else {
|
|---|
| 344 | bp->outPtr += numBytes;
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 | |
|---|
| 348 |
|
|---|
| 349 | /*-
|
|---|
| 350 | *-----------------------------------------------------------------------
|
|---|
| 351 | * Buf_Size --
|
|---|
| 352 | * Returns the number of bytes in the given buffer. Doesn't include
|
|---|
| 353 | * the null-terminating byte.
|
|---|
| 354 | *
|
|---|
| 355 | * Results:
|
|---|
| 356 | * The number of bytes.
|
|---|
| 357 | *
|
|---|
| 358 | * Side Effects:
|
|---|
| 359 | * None.
|
|---|
| 360 | *
|
|---|
| 361 | *-----------------------------------------------------------------------
|
|---|
| 362 | */
|
|---|
| 363 | int
|
|---|
| 364 | Buf_Size (Buffer buf)
|
|---|
| 365 | {
|
|---|
| 366 | return (buf->inPtr - buf->outPtr);
|
|---|
| 367 | }
|
|---|
| 368 | |
|---|
| 369 |
|
|---|
| 370 | /*-
|
|---|
| 371 | *-----------------------------------------------------------------------
|
|---|
| 372 | * Buf_Init --
|
|---|
| 373 | * Initialize a buffer. If no initial size is given, a reasonable
|
|---|
| 374 | * default is used.
|
|---|
| 375 | *
|
|---|
| 376 | * Results:
|
|---|
| 377 | * A buffer to be given to other functions in this library.
|
|---|
| 378 | *
|
|---|
| 379 | * Side Effects:
|
|---|
| 380 | * The buffer is created, the space allocated and pointers
|
|---|
| 381 | * initialized.
|
|---|
| 382 | *
|
|---|
| 383 | *-----------------------------------------------------------------------
|
|---|
| 384 | */
|
|---|
| 385 | Buffer
|
|---|
| 386 | Buf_Init (int size)
|
|---|
| 387 | {
|
|---|
| 388 | Buffer bp; /* New Buffer */
|
|---|
| 389 |
|
|---|
| 390 | bp = (Buffer)emalloc(sizeof(*bp));
|
|---|
| 391 |
|
|---|
| 392 | if (size <= 0) {
|
|---|
| 393 | size = BUF_DEF_SIZE;
|
|---|
| 394 | }
|
|---|
| 395 | bp->left = bp->size = size;
|
|---|
| 396 | bp->buffer = (Byte *)emalloc(size);
|
|---|
| 397 | bp->inPtr = bp->outPtr = bp->buffer;
|
|---|
| 398 | *bp->inPtr = 0;
|
|---|
| 399 |
|
|---|
| 400 | return (bp);
|
|---|
| 401 | }
|
|---|
| 402 | |
|---|
| 403 |
|
|---|
| 404 | /*-
|
|---|
| 405 | *-----------------------------------------------------------------------
|
|---|
| 406 | * Buf_Destroy --
|
|---|
| 407 | * Destroy a buffer, and optionally free its data, too.
|
|---|
| 408 | *
|
|---|
| 409 | * Results:
|
|---|
| 410 | * None.
|
|---|
| 411 | *
|
|---|
| 412 | * Side Effects:
|
|---|
| 413 | * The buffer is freed.
|
|---|
| 414 | *
|
|---|
| 415 | *-----------------------------------------------------------------------
|
|---|
| 416 | */
|
|---|
| 417 | void
|
|---|
| 418 | Buf_Destroy (Buffer buf, Boolean freeData)
|
|---|
| 419 | {
|
|---|
| 420 |
|
|---|
| 421 | if (freeData) {
|
|---|
| 422 | free ((char *)buf->buffer);
|
|---|
| 423 | }
|
|---|
| 424 | free ((char *)buf);
|
|---|
| 425 | }
|
|---|
| 426 | |
|---|
| 427 |
|
|---|
| 428 | /*-
|
|---|
| 429 | *-----------------------------------------------------------------------
|
|---|
| 430 | * Buf_ReplaceLastByte --
|
|---|
| 431 | * Replace the last byte in a buffer.
|
|---|
| 432 | *
|
|---|
| 433 | * Results:
|
|---|
| 434 | * None.
|
|---|
| 435 | *
|
|---|
| 436 | * Side Effects:
|
|---|
| 437 | * If the buffer was empty intially, then a new byte will be added.
|
|---|
| 438 | * Otherwise, the last byte is overwritten.
|
|---|
| 439 | *
|
|---|
| 440 | *-----------------------------------------------------------------------
|
|---|
| 441 | */
|
|---|
| 442 | void
|
|---|
| 443 | Buf_ReplaceLastByte (Buffer buf, int byte)
|
|---|
| 444 | {
|
|---|
| 445 | if (buf->inPtr == buf->outPtr)
|
|---|
| 446 | Buf_AddByte(buf, byte);
|
|---|
| 447 | else
|
|---|
| 448 | *(buf->inPtr - 1) = byte;
|
|---|
| 449 | }
|
|---|