xref: /openbsd-src/usr.bin/m4/misc.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
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 $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Ozan Yigit at York University.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/types.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <err.h>
45 #include "mdef.h"
46 #include "stdd.h"
47 #include "extern.h"
48 #include "pathnames.h"
49 
50 
51 char *ep;		/* first free char in strspace */
52 static char *strspace;	/* string space for evaluation */
53 char *endest;		/* end of string space	       */
54 static size_t strsize = STRSPMAX;
55 static size_t bufsize = BUFSIZE;
56 
57 unsigned char *buf;			/* push-back buffer	       */
58 unsigned char *bufbase;			/* the base for current ilevel */
59 unsigned char *bbase[MAXINP];		/* the base for each ilevel    */
60 unsigned char *bp;			/* first available character   */
61 unsigned char *endpbb;			/* end of push-back buffer     */
62 
63 
64 /*
65  * find the index of second str in the first str.
66  */
67 ptrdiff_t
68 indx(const char *s1, const char *s2)
69 {
70 	char *t;
71 
72 	t = strstr(s1, s2);
73 	if (t == NULL)
74 		return (-1);
75 	else
76 		return (t - s1);
77 }
78 /*
79  *  pushback - push character back onto input
80  */
81 void
82 pushback(int c)
83 {
84 	if (c == EOF)
85 		return;
86 	if (bp >= endpbb)
87 		enlarge_bufspace();
88 	*bp++ = c;
89 }
90 
91 /*
92  *  pbstr - push string back onto input
93  *          pushback is replicated to improve
94  *          performance.
95  */
96 void
97 pbstr(const char *s)
98 {
99 	size_t n;
100 
101 	n = strlen(s);
102 	while (endpbb - bp <= n)
103 		enlarge_bufspace();
104 	while (n > 0)
105 		*bp++ = s[--n];
106 }
107 
108 /*
109  *  pbnum - convert number to string, push back on input.
110  */
111 void
112 pbnum(int n)
113 {
114 	pbnumbase(n, 10, 0);
115 }
116 
117 void
118 pbnumbase(int n, int base, int d)
119 {
120 	static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
121 	int num;
122 	int printed = 0;
123 
124 	if (base > 36)
125 		m4errx(1, "base %d > 36: not supported.", base);
126 
127 	if (base < 2)
128 		m4errx(1, "bad base %d for conversion.", base);
129 
130 	num = (n < 0) ? -n : n;
131 	do {
132 		pushback(digits[num % base]);
133 		printed++;
134 	}
135 	while ((num /= base) > 0);
136 
137 	if (n < 0)
138 		printed++;
139 	while (printed++ < d)
140 		pushback('0');
141 
142 	if (n < 0)
143 		pushback('-');
144 }
145 
146 /*
147  *  pbunsigned - convert unsigned long to string, push back on input.
148  */
149 void
150 pbunsigned(unsigned long n)
151 {
152 	do {
153 		pushback(n % 10 + '0');
154 	}
155 	while ((n /= 10) > 0);
156 }
157 
158 void
159 initspaces()
160 {
161 	int i;
162 
163 	strspace = xalloc(strsize+1, NULL);
164 	ep = strspace;
165 	endest = strspace+strsize;
166 	buf = (unsigned char *)xalloc(bufsize, NULL);
167 	bufbase = buf;
168 	bp = buf;
169 	endpbb = buf + bufsize;
170 	for (i = 0; i < MAXINP; i++)
171 		bbase[i] = buf;
172 }
173 
174 void
175 enlarge_strspace()
176 {
177 	char *newstrspace;
178 	int i;
179 
180 	strsize *= 2;
181 	newstrspace = malloc(strsize + 1);
182 	if (!newstrspace)
183 		errx(1, "string space overflow");
184 	memcpy(newstrspace, strspace, strsize/2);
185 	for (i = 0; i <= sp; i++)
186 		if (sstack[i])
187 			mstack[i].sstr = (mstack[i].sstr - strspace)
188 			    + newstrspace;
189 	ep = (ep-strspace) + newstrspace;
190 	free(strspace);
191 	strspace = newstrspace;
192 	endest = strspace + strsize;
193 }
194 
195 void
196 enlarge_bufspace()
197 {
198 	unsigned char *newbuf;
199 	int i;
200 
201 	bufsize += bufsize/2;
202 	newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
203 	for (i = 0; i < MAXINP; i++)
204 		bbase[i] = (bbase[i]-buf)+newbuf;
205 	bp = (bp-buf)+newbuf;
206 	bufbase = (bufbase-buf)+newbuf;
207 	buf = newbuf;
208 	endpbb = buf+bufsize;
209 }
210 
211 /*
212  *  chrsave - put single char on string space
213  */
214 void
215 chrsave(int c)
216 {
217 	if (ep >= endest)
218 		enlarge_strspace();
219 	*ep++ = c;
220 }
221 
222 /*
223  * read in a diversion file, and dispose it.
224  */
225 void
226 getdiv(int n)
227 {
228 	int c;
229 
230 	if (active == outfile[n])
231 		m4errx(1, "undivert: diversion still active.");
232 	rewind(outfile[n]);
233 	while ((c = getc(outfile[n])) != EOF)
234 		putc(c, active);
235 	(void) fclose(outfile[n]);
236 	outfile[n] = NULL;
237 }
238 
239 void
240 onintr(int signo)
241 {
242 #define intrmessage	"m4: interrupted.\n"
243 	write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
244 	_exit(1);
245 }
246 
247 /*
248  * killdiv - get rid of the diversion files
249  */
250 void
251 killdiv()
252 {
253 	int n;
254 
255 	for (n = 0; n < maxout; n++)
256 		if (outfile[n] != NULL) {
257 			(void) fclose(outfile[n]);
258 		}
259 }
260 
261 extern char *__progname;
262 
263 void
264 m4errx(int eval, const char *fmt, ...)
265 {
266 	fprintf(stderr, "%s: ", __progname);
267 	fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
268 	if (fmt != NULL) {
269 		va_list ap;
270 
271 		va_start(ap, fmt);
272 		vfprintf(stderr, fmt, ap);
273 		va_end(ap);
274 	}
275 	fprintf(stderr, "\n");
276 	exit(eval);
277 }
278 
279 /*
280  * resizedivs: allocate more diversion files */
281 void
282 resizedivs(int n)
283 {
284 	int i;
285 
286 	outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n,
287 	    "too many diverts %d", n);
288 	for (i = maxout; i < n; i++)
289 		outfile[i] = NULL;
290 	maxout = n;
291 }
292 
293 void *
294 xalloc(size_t n, const char *fmt, ...)
295 {
296 	void *p = malloc(n);
297 
298 	if (p == NULL) {
299 		if (fmt == NULL)
300 			err(1, "malloc");
301 		else {
302 			va_list va;
303 
304 			va_start(va, fmt);
305 			verr(1, fmt, va);
306 			va_end(va);
307 		}
308 	}
309 	return p;
310 }
311 
312 void *
313 xrealloc(void *old, size_t n, const char *fmt, ...)
314 {
315 	char *p = realloc(old, n);
316 
317 	if (p == NULL) {
318 		free(old);
319 		if (fmt == NULL)
320 			err(1, "realloc");
321 		else {
322 			va_list va;
323 
324 			va_start(va, fmt);
325 			verr(1, fmt, va);
326 			va_end(va);
327 		}
328 	}
329 	return p;
330 }
331 
332 char *
333 xstrdup(const char *s)
334 {
335 	char *p = strdup(s);
336 	if (p == NULL)
337 		err(1, "strdup");
338 	return p;
339 }
340 
341 void
342 usage()
343 {
344 	fprintf(stderr, "usage: m4 [-gPs] [-Dname[=value]] [-d flags] "
345 			"[-I dirname] [-o filename]\n"
346 			"\t[-t macro] [-Uname] [file ...]\n");
347 	exit(1);
348 }
349 
350 int
351 obtain_char(struct input_file *f)
352 {
353 	if (f->c == EOF)
354 		return EOF;
355 
356 	f->c = fgetc(f->file);
357 	if (f->c == '\n')
358 		f->lineno++;
359 
360 	return f->c;
361 }
362 
363 void
364 set_input(struct input_file *f, FILE *real, const char *name)
365 {
366 	f->file = real;
367 	f->lineno = 1;
368 	f->c = 0;
369 	f->name = xstrdup(name);
370 	emit_synchline();
371 }
372 
373 void
374 do_emit_synchline()
375 {
376 	fprintf(active, "#line %lu \"%s\"\n",
377 	    infile[ilevel].lineno, infile[ilevel].name);
378 	infile[ilevel].synch_lineno = infile[ilevel].lineno;
379 }
380 
381 void
382 release_input(struct input_file *f)
383 {
384 	if (f->file != stdin)
385 	    fclose(f->file);
386 	f->c = EOF;
387 	/*
388 	 * XXX can't free filename, as there might still be
389 	 * error information pointing to it.
390 	 */
391 }
392 
393 void
394 doprintlineno(struct input_file *f)
395 {
396 	pbunsigned(f->lineno);
397 }
398 
399 void
400 doprintfilename(struct input_file *f)
401 {
402 	pbstr(rquote);
403 	pbstr(f->name);
404 	pbstr(lquote);
405 }
406 
407 /*
408  * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
409  * and later dump everything that was added since then to a file.
410  */
411 size_t
412 buffer_mark()
413 {
414 	return bp - buf;
415 }
416 
417 
418 void
419 dump_buffer(FILE *f, size_t m)
420 {
421 	unsigned char *s;
422 
423 	for (s = bp; s-buf > m;)
424 		fputc(*--s, f);
425 }
426