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 */
67 static void *reallocarray(void *, size_t, size_t);
70 * find the index of second str in the first str.
73 indx(const char *s1, const char *s2)
84 * pushback - push character back onto input
97 * pbstr - push string back onto input
98 * pushback is replicated to improve
107 while (endpbb - bp <= (long)n)
114 * pbnum - convert number to string, push back on input.
123 pbnumbase(int n, int base, int d)
125 static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
130 m4errx(1, "base %d > 36: not supported.", base);
133 m4errx(1, "bad base %d for conversion.", base);
135 num = (n < 0) ? -n : n;
137 pushback(digits[num % base]);
140 while ((num /= base) > 0);
144 while (printed++ < d)
152 * pbunsigned - convert unsigned long to string, push back on input.
155 pbunsigned(unsigned long n)
158 pushback(n % 10 + '0');
160 while ((n /= 10) > 0);
168 strspace = xalloc(strsize+1, NULL);
170 endest = strspace+strsize;
171 buf = xalloc(bufsize, NULL);
174 endpbb = buf + bufsize;
175 for (i = 0; i < MAXINP; i++)
180 enlarge_strspace(void)
186 newstrspace = malloc(strsize + 1);
188 errx(1, "string space overflow");
189 memcpy(newstrspace, strspace, strsize/2);
190 for (i = 0; i <= sp; i++)
192 mstack[i].sstr = (mstack[i].sstr - strspace)
194 ep = (ep-strspace) + newstrspace;
196 strspace = newstrspace;
197 endest = strspace + strsize;
201 enlarge_bufspace(void)
203 unsigned char *newbuf;
206 bufsize += bufsize/2;
207 newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
208 for (i = 0; i < MAXINP; i++)
209 bbase[i] = (bbase[i]-buf)+newbuf;
210 bp = (bp-buf)+newbuf;
211 bufbase = (bufbase-buf)+newbuf;
213 endpbb = buf+bufsize;
217 * chrsave - put single char on string space
228 * read in a diversion file, and dispose it.
235 if (active == outfile[n])
236 m4errx(1, "undivert: diversion still active.");
238 while ((c = getc(outfile[n])) != EOF)
240 (void) fclose(outfile[n]);
245 onintr(int signo __unused)
247 #define intrmessage "m4: interrupted.\n"
248 write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
253 * killdiv - get rid of the diversion files
260 for (n = 0; n < maxout; n++)
261 if (outfile[n] != NULL) {
262 (void) fclose(outfile[n]);
266 extern char *__progname;
269 m4errx(int exitstatus, const char *fmt, ...)
271 fprintf(stderr, "%s: ", __progname);
272 fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
277 vfprintf(stderr, fmt, ap);
280 fprintf(stderr, "\n");
285 * resizedivs: allocate more diversion files */
291 outfile = xreallocarray(outfile, n, sizeof(FILE *),
292 "too many diverts %d", n);
293 for (i = maxout; i < n; i++)
299 xalloc(size_t n, const char *fmt, ...)
318 xcalloc(size_t n, size_t s, const char *fmt, ...)
320 void *p = calloc(n, s);
337 xrealloc(void *old, size_t n, const char *fmt, ...)
339 char *p = realloc(old, n);
357 xreallocarray(void *old, size_t s1, size_t s2, const char *fmt, ...)
359 void *p = reallocarray(old, s1, s2);
364 err(1, "reallocarray");
377 xstrdup(const char *s)
388 fprintf(stderr, "usage: m4 [-gPs] [-Dname[=value]] [-d flags] "
389 "[-I dirname] [-o filename]\n"
390 "\t[-t macro] [-Uname] [file ...]\n");
395 obtain_char(struct input_file *f)
400 f->c = fgetc(f->file);
408 set_input(struct input_file *f, FILE *real, const char *name)
413 f->name = xstrdup(name);
418 do_emit_synchline(void)
420 fprintf(active, "#line %lu \"%s\"\n",
421 infile[ilevel].lineno, infile[ilevel].name);
422 infile[ilevel].synch_lineno = infile[ilevel].lineno;
426 release_input(struct input_file *f)
428 if (f->file != stdin)
432 * XXX can't free filename, as there might still be
433 * error information pointing to it.
438 doprintlineno(struct input_file *f)
440 pbunsigned(f->lineno);
444 doprintfilename(struct input_file *f)
452 * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
453 * and later dump everything that was added since then to a file.
463 dump_buffer(FILE *f, size_t m)
467 for (s = bp; s-buf > (long)m;)
471 /* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */
473 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
475 * Permission to use, copy, modify, and distribute this software for any
476 * purpose with or without fee is hereby granted, provided that the above
477 * copyright notice and this permission notice appear in all copies.
479 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
480 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
481 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
482 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
483 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
484 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
485 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
489 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
490 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
492 #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
495 reallocarray(void *optr, size_t nmemb, size_t size)
497 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
498 nmemb > 0 && SIZE_MAX / nmemb < size) {
502 return realloc(optr, size * nmemb);