1 /* $OpenBSD: misc.c,v 1.27 2002/04/26 16:15:16 espie Exp $ */
2 /* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
42 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
45 static char rcsid[] = "$OpenBSD: misc.c,v 1.27 2002/04/26 16:15:16 espie Exp $";
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/types.h>
64 #include "pathnames.h"
67 char *ep; /* first free char in strspace */
68 static char *strspace; /* string space for evaluation */
69 char *endest; /* end of string space */
70 static size_t strsize = STRSPMAX;
71 static size_t bufsize = BUFSIZE;
73 char *buf; /* push-back buffer */
74 char *bufbase; /* the base for current ilevel */
75 char *bbase[MAXINP]; /* the base for each ilevel */
76 char *bp; /* first available character */
77 char *endpbb; /* end of push-back buffer */
81 * find the index of second str in the first str.
84 indx(const char *s1, const char *s2)
95 * putback - push character back onto input
108 * pbstr - push string back onto input
109 * putback is replicated to improve
118 while ((size_t)(endpbb - bp) <= n)
125 * pbnum - convert number to string, push back on input.
132 num = (n < 0) ? -n : n;
134 putback(num % 10 + '0');
136 while ((num /= 10) > 0);
143 * pbunsigned - convert unsigned long to string, push back on input.
146 pbunsigned(unsigned long n)
149 putback(n % 10 + '0');
151 while ((n /= 10) > 0);
159 strspace = xalloc(strsize+1);
161 endest = strspace+strsize;
162 buf = (char *)xalloc(bufsize);
165 endpbb = buf + bufsize;
166 for (i = 0; i < MAXINP; i++)
171 enlarge_strspace(void)
177 newstrspace = malloc(strsize + 1);
179 errx(1, "string space overflow");
180 memcpy(newstrspace, strspace, strsize/2);
181 for (i = 0; i <= sp; i++)
183 mstack[i].sstr = (mstack[i].sstr - strspace)
185 ep = (ep-strspace) + newstrspace;
187 strspace = newstrspace;
188 endest = strspace + strsize;
192 enlarge_bufspace(void)
198 newbuf = realloc(buf, bufsize);
200 errx(1, "too many characters pushed back");
201 for (i = 0; i < MAXINP; i++)
202 bbase[i] = (bbase[i]-buf)+newbuf;
203 bp = (bp-buf)+newbuf;
204 bufbase = (bufbase-buf)+newbuf;
206 endpbb = buf+bufsize;
210 * chrsave - put single char on string space
221 * read in a diversion file, and dispose it.
228 if (active == outfile[n])
229 errx(1, "undivert: diversion still active");
231 while ((c = getc(outfile[n])) != EOF)
233 (void) fclose(outfile[n]);
238 onintr(int signo __unused)
240 #define intrmessage "m4: interrupted.\n"
241 write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
246 * killdiv - get rid of the diversion files
253 for (n = 0; n < maxout; n++)
254 if (outfile[n] != NULL) {
255 (void) fclose(outfile[n]);
260 * resizedivs: allocate more diversion files */
266 outfile = (FILE **)realloc(outfile, sizeof(FILE *) * n);
268 errx(1, "too many diverts %d", n);
269 for (i = maxout; i < n; i++)
285 xstrdup(const char *s)
297 "usage: m4 [-d flags] [-t name] [-gs] [-D name[=value]]...\n"
298 " [-U name]... [-I dirname]... file...\n");
303 obtain_char(struct input_file *f)
307 else if (f->c == '\n')
310 f->c = fgetc(f->file);
315 set_input(struct input_file *f, FILE *real, const char *name)
320 f->name = xstrdup(name);
324 release_input(struct input_file *f)
326 if (f->file != stdin)
330 * XXX can't free filename, as there might still be
331 * error information pointing to it.
336 doprintlineno(struct input_file *f)
338 pbunsigned(f->lineno);
342 doprintfilename(struct input_file *f)
350 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
351 * and later dump everything that was added since then to a file.
361 dump_buffer(FILE *f, size_t m)
365 for (s = bp; s - buf > (int)m;)