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