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 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
41 static char rcsid[] = "$OpenBSD: misc.c,v 1.27 2002/04/26 16:15:16 espie Exp $";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
60 #include "pathnames.h"
63 char *ep; /* first free char in strspace */
64 static char *strspace; /* string space for evaluation */
65 char *endest; /* end of string space */
66 static size_t strsize = STRSPMAX;
67 static size_t bufsize = BUFSIZE;
69 char *buf; /* push-back buffer */
70 char *bufbase; /* the base for current ilevel */
71 char *bbase[MAXINP]; /* the base for each ilevel */
72 char *bp; /* first available character */
73 char *endpbb; /* end of push-back buffer */
77 * find the index of second str in the first str.
80 indx(const char *s1, const char *s2)
91 * putback - push character back onto input
104 * pbstr - push string back onto input
105 * putback is replicated to improve
114 while ((size_t)(endpbb - bp) <= n)
121 * pbnum - convert number to string, push back on input.
128 num = (n < 0) ? -n : n;
130 putback(num % 10 + '0');
132 while ((num /= 10) > 0);
139 * pbunsigned - convert unsigned long to string, push back on input.
142 pbunsigned(unsigned long n)
145 putback(n % 10 + '0');
147 while ((n /= 10) > 0);
155 strspace = xalloc(strsize+1);
157 endest = strspace+strsize;
158 buf = (char *)xalloc(bufsize);
161 endpbb = buf + bufsize;
162 for (i = 0; i < MAXINP; i++)
167 enlarge_strspace(void)
173 newstrspace = malloc(strsize + 1);
175 errx(1, "string space overflow");
176 memcpy(newstrspace, strspace, strsize/2);
177 for (i = 0; i <= sp; i++)
179 mstack[i].sstr = (mstack[i].sstr - strspace)
181 ep = (ep-strspace) + newstrspace;
183 strspace = newstrspace;
184 endest = strspace + strsize;
188 enlarge_bufspace(void)
194 newbuf = realloc(buf, bufsize);
196 errx(1, "too many characters pushed back");
197 for (i = 0; i < MAXINP; i++)
198 bbase[i] = (bbase[i]-buf)+newbuf;
199 bp = (bp-buf)+newbuf;
200 bufbase = (bufbase-buf)+newbuf;
202 endpbb = buf+bufsize;
206 * chrsave - put single char on string space
217 * read in a diversion file, and dispose it.
224 if (active == outfile[n])
225 errx(1, "undivert: diversion still active");
227 while ((c = getc(outfile[n])) != EOF)
229 (void) fclose(outfile[n]);
234 onintr(int signo __unused)
236 #define intrmessage "m4: interrupted.\n"
237 write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
242 * killdiv - get rid of the diversion files
249 for (n = 0; n < maxout; n++)
250 if (outfile[n] != NULL) {
251 (void) fclose(outfile[n]);
256 * resizedivs: allocate more diversion files */
262 outfile = (FILE **)realloc(outfile, sizeof(FILE *) * n);
264 errx(1, "too many diverts %d", n);
265 for (i = maxout; i < n; i++)
281 xstrdup(const char *s)
293 "usage: m4 [-d flags] [-t name] [-gs] [-D name[=value]]...\n"
294 " [-U name]... [-I dirname]... file...\n");
299 obtain_char(struct input_file *f)
303 else if (f->c == '\n')
306 f->c = fgetc(f->file);
311 set_input(struct input_file *f, FILE *real, const char *name)
316 f->name = xstrdup(name);
320 release_input(struct input_file *f)
322 if (f->file != stdin)
326 * XXX can't free filename, as there might still be
327 * error information pointing to it.
332 doprintlineno(struct input_file *f)
334 pbunsigned(f->lineno);
338 doprintfilename(struct input_file *f)
346 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
347 * and later dump everything that was added since then to a file.
357 dump_buffer(FILE *f, size_t m)
361 for (s = bp; s - buf > (int)m;)