/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1991, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Kenneth Almquist.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <string.h>3536struct stackmark {37struct stack_block *stackp;38char *stacknxt;39int stacknleft;40};414243extern char *stacknxt;44extern int stacknleft;45extern char *sstrend;4647pointer ckmalloc(size_t);48pointer ckrealloc(pointer, int);49void ckfree(pointer);50char *savestr(const char *);51pointer stalloc(int);52void stunalloc(pointer);53char *stsavestr(const char *);54void setstackmark(struct stackmark *);55void popstackmark(struct stackmark *);56char *growstackstr(void);57char *makestrspace(int, char *);58char *stputbin(const char *data, size_t len, char *p);59char *stputs(const char *data, char *p);60616263#define stackblock() stacknxt64#define stackblocksize() stacknleft65#define grabstackblock(n) stalloc(n)66#define STARTSTACKSTR(p) p = stackblock()67#define STPUTC(c, p) do { if (p == sstrend) p = growstackstr(); *p++ = (c); } while(0)68#define CHECKSTRSPACE(n, p) { if ((size_t)(sstrend - p) < n) p = makestrspace(n, p); }69#define USTPUTC(c, p) (*p++ = (c))70/*71* STACKSTRNUL's use is where we want to be able to turn a stack72* (non-sentinel, character counting string) into a C string,73* and later pretend the NUL is not there.74* Note: Because of STACKSTRNUL's semantics, STACKSTRNUL cannot be used75* on a stack that will grabstackstr()ed.76*/77#define STACKSTRNUL(p) (p == sstrend ? (p = growstackstr(), *p = '\0') : (*p = '\0'))78#define STUNPUTC(p) (--p)79#define STTOPC(p) p[-1]80#define STADJUST(amount, p) (p += (amount))81#define grabstackstr(p) stalloc((char *)p - stackblock())82#define ungrabstackstr(s, p) stunalloc((s))83#define STPUTBIN(s, len, p) p = stputbin((s), (len), p)84#define STPUTS(s, p) p = stputs((s), p)858687