1 /* $OpenBSD: misc.c,v 1.42 2010/09/07 19:58:09 marco 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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
50 #include "pathnames.h"
53 char *ep; /* first free char in strspace */
54 static char *strspace; /* string space for evaluation */
55 char *endest; /* end of string space */
56 static size_t strsize = STRSPMAX;
57 static size_t bufsize = BUFSIZE;
59 unsigned char *buf; /* push-back buffer */
60 unsigned char *bufbase; /* the base for current ilevel */
61 unsigned char *bbase[MAXINP]; /* the base for each ilevel */
62 unsigned char *bp; /* first available character */
63 unsigned char *endpbb; /* end of push-back buffer */
67 * find the index of second str in the first str.
70 indx(const char *s1, const char *s2)
81 * pushback - push character back onto input
94 * pbstr - push string back onto input
95 * pushback is replicated to improve
104 while (endpbb - bp <= (long)n)
111 * pbnum - convert number to string, push back on input.
120 pbnumbase(int n, int base, int d)
122 static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
127 m4errx(1, "base %d > 36: not supported.", base);
130 m4errx(1, "bad base %d for conversion.", base);
132 num = (n < 0) ? -n : n;
134 pushback(digits[num % base]);
137 while ((num /= base) > 0);
141 while (printed++ < d)
149 * pbunsigned - convert unsigned long to string, push back on input.
152 pbunsigned(unsigned long n)
155 pushback(n % 10 + '0');
157 while ((n /= 10) > 0);
165 strspace = xalloc(strsize+1, NULL);
167 endest = strspace+strsize;
168 buf = (unsigned char *)xalloc(bufsize, NULL);
171 endpbb = buf + bufsize;
172 for (i = 0; i < MAXINP; i++)
177 enlarge_strspace(void)
183 newstrspace = malloc(strsize + 1);
185 errx(1, "string space overflow");
186 memcpy(newstrspace, strspace, strsize/2);
187 for (i = 0; i <= sp; i++)
189 mstack[i].sstr = (mstack[i].sstr - strspace)
191 ep = (ep-strspace) + newstrspace;
193 strspace = newstrspace;
194 endest = strspace + strsize;
198 enlarge_bufspace(void)
200 unsigned char *newbuf;
203 bufsize += bufsize/2;
204 newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
205 for (i = 0; i < MAXINP; i++)
206 bbase[i] = (bbase[i]-buf)+newbuf;
207 bp = (bp-buf)+newbuf;
208 bufbase = (bufbase-buf)+newbuf;
210 endpbb = buf+bufsize;
214 * chrsave - put single char on string space
225 * read in a diversion file, and dispose it.
232 if (active == outfile[n])
233 m4errx(1, "undivert: diversion still active.");
235 while ((c = getc(outfile[n])) != EOF)
237 (void) fclose(outfile[n]);
242 onintr(__unused int signo)
244 #define intrmessage "m4: interrupted.\n"
245 write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
250 * killdiv - get rid of the diversion files
257 for (n = 0; n < maxout; n++)
258 if (outfile[n] != NULL) {
259 (void) fclose(outfile[n]);
263 extern char *__progname;
266 m4errx(int evaluation, const char *fmt, ...)
268 fprintf(stderr, "%s: ", __progname);
269 fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
274 vfprintf(stderr, fmt, ap);
277 fprintf(stderr, "\n");
282 * resizedivs: allocate more diversion files */
288 outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n,
289 "too many diverts %d", n);
290 for (i = maxout; i < n; i++)
296 xalloc(size_t n, const char *fmt, ...)
315 xrealloc(void *old, size_t n, const char *fmt, ...)
317 char *p = realloc(old, n);
335 xstrdup(const char *s)
346 fprintf(stderr, "usage: m4 [-gPs] [-Dname[=value]] [-d flags] "
347 "[-I dirname] [-o filename]\n"
348 "\t[-t macro] [-Uname] [file ...]\n");
353 obtain_char(struct input_file *f)
358 f->c = fgetc(f->file);
366 set_input(struct input_file *f, FILE *real, const char *name)
371 f->name = xstrdup(name);
376 do_emit_synchline(void)
378 fprintf(active, "#line %lu \"%s\"\n",
379 infile[ilevel].lineno, infile[ilevel].name);
380 infile[ilevel].synch_lineno = infile[ilevel].lineno;
384 release_input(struct input_file *f)
386 if (f->file != stdin)
390 * XXX can't free filename, as there might still be
391 * error information pointing to it.
396 doprintlineno(struct input_file *f)
398 pbunsigned(f->lineno);
402 doprintfilename(struct input_file *f)
410 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
411 * and later dump everything that was added since then to a file.
421 dump_buffer(FILE *f, size_t m)
425 for (s = bp; s-buf > (long)m;)