1 /* $OpenBSD: misc.c,v 1.44 2014/05/12 19:11:19 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. 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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
52 #include "pathnames.h"
55 char *ep; /* first free char in strspace */
56 static char *strspace; /* string space for evaluation */
57 char *endest; /* end of string space */
58 static size_t strsize = STRSPMAX;
59 static size_t bufsize = BUFSIZE;
61 unsigned char *buf; /* push-back buffer */
62 unsigned char *bufbase; /* the base for current ilevel */
63 unsigned char *bbase[MAXINP]; /* the base for each ilevel */
64 unsigned char *bp; /* first available character */
65 unsigned char *endpbb; /* end of push-back buffer */
68 * find the index of second str in the first str.
71 indx(const char *s1, const char *s2)
82 * pushback - push character back onto input
95 * pbstr - push string back onto input
96 * pushback is replicated to improve
105 while (endpbb - bp <= (long)n)
112 * pbnum - convert number to string, push back on input.
121 pbnumbase(int n, int base, int d)
123 static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
128 m4errx(1, "base %d > 36: not supported.", base);
131 m4errx(1, "bad base %d for conversion.", base);
133 num = (n < 0) ? -n : n;
135 pushback(digits[num % base]);
138 while ((num /= base) > 0);
142 while (printed++ < d)
150 * pbunsigned - convert unsigned long to string, push back on input.
153 pbunsigned(unsigned long n)
156 pushback(n % 10 + '0');
158 while ((n /= 10) > 0);
166 strspace = xalloc(strsize+1, NULL);
168 endest = strspace+strsize;
169 buf = xalloc(bufsize, NULL);
172 endpbb = buf + bufsize;
173 for (i = 0; i < MAXINP; i++)
178 enlarge_strspace(void)
184 newstrspace = malloc(strsize + 1);
186 errx(1, "string space overflow");
187 memcpy(newstrspace, strspace, strsize/2);
188 for (i = 0; i <= sp; i++)
190 mstack[i].sstr = (mstack[i].sstr - strspace)
192 ep = (ep-strspace) + newstrspace;
194 strspace = newstrspace;
195 endest = strspace + strsize;
199 enlarge_bufspace(void)
201 unsigned char *newbuf;
204 bufsize += bufsize/2;
205 newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
206 for (i = 0; i < MAXINP; i++)
207 bbase[i] = (bbase[i]-buf)+newbuf;
208 bp = (bp-buf)+newbuf;
209 bufbase = (bufbase-buf)+newbuf;
211 endpbb = buf+bufsize;
215 * chrsave - put single char on string space
226 * read in a diversion file, and dispose it.
233 if (active == outfile[n])
234 m4errx(1, "undivert: diversion still active.");
236 while ((c = getc(outfile[n])) != EOF)
238 (void) fclose(outfile[n]);
243 onintr(int signo __unused)
245 #define intrmessage "m4: interrupted.\n"
246 write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
251 * killdiv - get rid of the diversion files
258 for (n = 0; n < maxout; n++)
259 if (outfile[n] != NULL) {
260 (void) fclose(outfile[n]);
264 extern char *__progname;
267 m4errx(int exitstatus, const char *fmt, ...)
269 fprintf(stderr, "%s: ", __progname);
270 fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
275 vfprintf(stderr, fmt, ap);
278 fprintf(stderr, "\n");
283 * resizedivs: allocate more diversion files */
289 outfile = xreallocarray(outfile, n, sizeof(FILE *),
290 "too many diverts %d", n);
291 for (i = maxout; i < n; i++)
297 xalloc(size_t n, const char *fmt, ...)
316 xcalloc(size_t n, size_t s, const char *fmt, ...)
318 void *p = calloc(n, s);
335 xrealloc(void *old, size_t n, const char *fmt, ...)
337 char *p = realloc(old, n);
355 xreallocarray(void *old, size_t s1, size_t s2, const char *fmt, ...)
357 void *p = reallocarray(old, s1, s2);
362 err(1, "reallocarray");
375 xstrdup(const char *s)
386 fprintf(stderr, "usage: m4 [-gPs] [-Dname[=value]] [-d flags] "
387 "[-I dirname] [-o filename]\n"
388 "\t[-t macro] [-Uname] [file ...]\n");
393 obtain_char(struct input_file *f)
398 f->c = fgetc(f->file);
406 set_input(struct input_file *f, FILE *real, const char *name)
411 f->name = xstrdup(name);
416 do_emit_synchline(void)
418 fprintf(active, "#line %lu \"%s\"\n",
419 infile[ilevel].lineno, infile[ilevel].name);
420 infile[ilevel].synch_lineno = infile[ilevel].lineno;
424 release_input(struct input_file *f)
426 if (f->file != stdin)
430 * XXX can't free filename, as there might still be
431 * error information pointing to it.
436 doprintlineno(struct input_file *f)
438 pbunsigned(f->lineno);
442 doprintfilename(struct input_file *f)
450 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
451 * and later dump everything that was added since then to a file.
461 dump_buffer(FILE *f, size_t m)
465 for (s = bp; s-buf > (long)m;)