1 /* $OpenBSD: misc.c,v 1.46 2015/12/07 14:12:46 espie Exp $ */
2 /* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1989, 1993
8 * The Regents of the University of California. All rights reserved.
10 * This code is derived from software contributed to Berkeley by
11 * Ozan Yigit at York University.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
54 #include "pathnames.h"
57 char *ep; /* first free char in strspace */
58 static char *strspace; /* string space for evaluation */
59 char *endest; /* end of string space */
60 static size_t strsize = STRSPMAX;
61 static size_t bufsize = BUFSIZE;
63 unsigned char *buf; /* push-back buffer */
64 unsigned char *bufbase; /* the base for current ilevel */
65 unsigned char *bbase[MAXINP]; /* the base for each ilevel */
66 unsigned char *bp; /* first available character */
67 unsigned char *endpbb; /* end of push-back buffer */
71 * find the index of second str in the first str.
74 indx(const char *s1, const char *s2)
85 * pushback - push character back onto input
98 * pbstr - push string back onto input
99 * pushback is replicated to improve
108 while (endpbb - bp <= (long)n)
115 * pbnum - convert number to string, push back on input.
124 pbnumbase(int n, int base, int d)
126 static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
131 m4errx(1, "base %d > 36: not supported.", base);
134 m4errx(1, "bad base %d for conversion.", base);
136 num = (n < 0) ? -n : n;
138 pushback(digits[num % base]);
141 while ((num /= base) > 0);
145 while (printed++ < d)
153 * pbunsigned - convert unsigned long to string, push back on input.
156 pbunsigned(unsigned long n)
159 pushback(n % 10 + '0');
161 while ((n /= 10) > 0);
169 strspace = xalloc(strsize+1, NULL);
171 endest = strspace+strsize;
172 buf = xalloc(bufsize, NULL);
175 endpbb = buf + bufsize;
176 for (i = 0; i < MAXINP; i++)
181 enlarge_strspace(void)
187 newstrspace = malloc(strsize + 1);
189 errx(1, "string space overflow");
190 memcpy(newstrspace, strspace, strsize/2);
191 for (i = 0; i <= sp; i++)
192 if (sstack[i] == STORAGE_STRSPACE)
193 mstack[i].sstr = (mstack[i].sstr - strspace)
195 ep = (ep-strspace) + newstrspace;
197 strspace = newstrspace;
198 endest = strspace + strsize;
202 enlarge_bufspace(void)
204 unsigned char *newbuf;
207 bufsize += bufsize/2;
208 newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
209 for (i = 0; i < MAXINP; i++)
210 bbase[i] = (bbase[i]-buf)+newbuf;
211 bp = (bp-buf)+newbuf;
212 bufbase = (bufbase-buf)+newbuf;
214 endpbb = buf+bufsize;
218 * chrsave - put single char on string space
229 * read in a diversion file, and dispose it.
236 if (active == outfile[n])
237 m4errx(1, "undivert: diversion still active.");
239 while ((c = getc(outfile[n])) != EOF)
241 (void) fclose(outfile[n]);
246 onintr(int signo __unused)
248 #define intrmessage "m4: interrupted.\n"
249 write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
254 * killdiv - get rid of the diversion files
261 for (n = 0; n < maxout; n++)
262 if (outfile[n] != NULL) {
263 (void) fclose(outfile[n]);
267 extern char *__progname;
270 m4errx(int eval, const char *fmt, ...)
272 fprintf(stderr, "%s: ", __progname);
273 fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
278 vfprintf(stderr, fmt, ap);
281 fprintf(stderr, "\n");
286 * resizedivs: allocate more diversion files */
292 outfile = xreallocarray(outfile, n, sizeof(FILE *),
293 "too many diverts %d", n);
294 for (i = maxout; i < n; i++)
300 xalloc(size_t n, const char *fmt, ...)
319 xcalloc(size_t n, size_t s, const char *fmt, ...)
321 void *p = calloc(n, s);
338 xrealloc(void *old, size_t n, const char *fmt, ...)
340 char *p = realloc(old, n);
358 xreallocarray(void *old, size_t s1, size_t s2, const char *fmt, ...)
360 void *p = reallocarray(old, s1, s2);
365 err(1, "reallocarray");
378 xstrdup(const char *s)
389 fprintf(stderr, "usage: m4 [-gPs] [-Dname[=value]] [-d flags] "
390 "[-I dirname] [-o filename]\n"
391 "\t[-t macro] [-Uname] [file ...]\n");
396 obtain_char(struct input_file *f)
401 f->c = fgetc(f->file);
409 set_input(struct input_file *f, FILE *real, const char *name)
414 f->name = xstrdup(name);
419 do_emit_synchline(void)
421 fprintf(active, "#line %lu \"%s\"\n",
422 infile[ilevel].lineno, infile[ilevel].name);
423 infile[ilevel].synch_lineno = infile[ilevel].lineno;
427 release_input(struct input_file *f)
430 errx(1, "Fatal error reading from %s\n", f->name);
431 if (f->file != stdin)
435 * XXX can't free filename, as there might still be
436 * error information pointing to it.
441 doprintlineno(struct input_file *f)
443 pbunsigned(f->lineno);
447 doprintfilename(struct input_file *f)
455 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
456 * and later dump everything that was added since then to a file.
466 dump_buffer(FILE *f, size_t m)
470 for (s = bp; s-buf > (long)m;)