misc.c revision 1.17 1 1.17 jmc /* $NetBSD: misc.c,v 1.17 2004/06/20 22:20:16 jmc Exp $ */
2 1.12 tv /* $OpenBSD: misc.c,v 1.25 2001/10/10 11:17:37 espie Exp $ */
3 1.6 tls
4 1.5 glass /*
5 1.5 glass * Copyright (c) 1989, 1993
6 1.5 glass * The Regents of the University of California. All rights reserved.
7 1.5 glass *
8 1.5 glass * This code is derived from software contributed to Berkeley by
9 1.5 glass * Ozan Yigit at York University.
10 1.5 glass *
11 1.5 glass * Redistribution and use in source and binary forms, with or without
12 1.5 glass * modification, are permitted provided that the following conditions
13 1.5 glass * are met:
14 1.5 glass * 1. Redistributions of source code must retain the above copyright
15 1.5 glass * notice, this list of conditions and the following disclaimer.
16 1.5 glass * 2. Redistributions in binary form must reproduce the above copyright
17 1.5 glass * notice, this list of conditions and the following disclaimer in the
18 1.5 glass * documentation and/or other materials provided with the distribution.
19 1.16 agc * 3. Neither the name of the University nor the names of its contributors
20 1.5 glass * may be used to endorse or promote products derived from this software
21 1.5 glass * without specific prior written permission.
22 1.5 glass *
23 1.5 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.5 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.5 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.5 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.5 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.5 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.5 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.5 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.5 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.5 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.5 glass * SUCH DAMAGE.
34 1.5 glass */
35 1.5 glass
36 1.17 jmc #if HAVE_NBTOOL_CONFIG_H
37 1.17 jmc #include "nbtool_config.h"
38 1.17 jmc #endif
39 1.17 jmc
40 1.7 lukem #include <sys/cdefs.h>
41 1.15 tv #if defined(__RCSID) && !defined(lint)
42 1.6 tls #if 0
43 1.5 glass static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
44 1.6 tls #else
45 1.17 jmc __RCSID("$NetBSD: misc.c,v 1.17 2004/06/20 22:20:16 jmc Exp $");
46 1.6 tls #endif
47 1.5 glass #endif /* not lint */
48 1.5 glass
49 1.5 glass #include <sys/types.h>
50 1.5 glass #include <errno.h>
51 1.5 glass #include <stdio.h>
52 1.5 glass #include <stdlib.h>
53 1.12 tv #include <stddef.h>
54 1.5 glass #include <string.h>
55 1.1 cgd #include "mdef.h"
56 1.5 glass #include "stdd.h"
57 1.5 glass #include "extern.h"
58 1.5 glass #include "pathnames.h"
59 1.12 tv
60 1.12 tv char *ep; /* first free char in strspace */
61 1.12 tv static char *strspace; /* string space for evaluation */
62 1.12 tv char *endest; /* end of string space */
63 1.12 tv static size_t strsize = STRSPMAX;
64 1.12 tv static size_t bufsize = BUFSIZE;
65 1.12 tv
66 1.12 tv char *buf; /* push-back buffer */
67 1.12 tv char *bufbase; /* the base for current ilevel */
68 1.12 tv char *bbase[MAXINP]; /* the base for each ilevel */
69 1.12 tv char *bp; /* first available character */
70 1.12 tv char *endpbb; /* end of push-back buffer */
71 1.12 tv
72 1.12 tv
73 1.5 glass /*
74 1.5 glass * find the index of second str in the first str.
75 1.5 glass */
76 1.12 tv ptrdiff_t
77 1.5 glass indx(s1, s2)
78 1.12 tv const char *s1;
79 1.12 tv const char *s2;
80 1.5 glass {
81 1.7 lukem char *t;
82 1.7 lukem
83 1.7 lukem t = strstr(s1, s2);
84 1.7 lukem if (t == NULL)
85 1.7 lukem return (-1);
86 1.7 lukem return (t - s1);
87 1.5 glass }
88 1.7 lukem
89 1.5 glass /*
90 1.5 glass * putback - push character back onto input
91 1.5 glass */
92 1.5 glass void
93 1.5 glass putback(c)
94 1.11 wiz int c;
95 1.5 glass {
96 1.12 tv if (c == EOF)
97 1.12 tv return;
98 1.12 tv if (bp >= endpbb)
99 1.12 tv enlarge_bufspace();
100 1.12 tv *bp++ = c;
101 1.9 cgd }
102 1.9 cgd
103 1.9 cgd /*
104 1.5 glass * pbstr - push string back onto input
105 1.5 glass * putback is replicated to improve
106 1.5 glass * performance.
107 1.5 glass */
108 1.5 glass void
109 1.5 glass pbstr(s)
110 1.12 tv const char *s;
111 1.5 glass {
112 1.12 tv size_t n;
113 1.2 glass
114 1.12 tv n = strlen(s);
115 1.12 tv while (endpbb - bp <= n)
116 1.12 tv enlarge_bufspace();
117 1.12 tv while (n > 0)
118 1.12 tv *bp++ = s[--n];
119 1.5 glass }
120 1.5 glass
121 1.5 glass /*
122 1.5 glass * pbnum - convert number to string, push back on input.
123 1.5 glass */
124 1.5 glass void
125 1.5 glass pbnum(n)
126 1.7 lukem int n;
127 1.5 glass {
128 1.7 lukem int num;
129 1.2 glass
130 1.5 glass num = (n < 0) ? -n : n;
131 1.2 glass do {
132 1.5 glass putback(num % 10 + '0');
133 1.2 glass }
134 1.5 glass while ((num /= 10) > 0);
135 1.2 glass
136 1.5 glass if (n < 0)
137 1.5 glass putback('-');
138 1.5 glass }
139 1.5 glass
140 1.5 glass /*
141 1.12 tv * pbunsigned - convert unsigned long to string, push back on input.
142 1.12 tv */
143 1.12 tv void
144 1.12 tv pbunsigned(n)
145 1.12 tv unsigned long n;
146 1.12 tv {
147 1.12 tv do {
148 1.12 tv putback(n % 10 + '0');
149 1.12 tv }
150 1.12 tv while ((n /= 10) > 0);
151 1.12 tv }
152 1.12 tv
153 1.12 tv void
154 1.12 tv initspaces()
155 1.12 tv {
156 1.12 tv int i;
157 1.12 tv
158 1.12 tv strspace = xalloc(strsize+1);
159 1.12 tv ep = strspace;
160 1.12 tv endest = strspace+strsize;
161 1.12 tv buf = (char *)xalloc(bufsize);
162 1.12 tv bufbase = buf;
163 1.12 tv bp = buf;
164 1.12 tv endpbb = buf + bufsize;
165 1.12 tv for (i = 0; i < MAXINP; i++)
166 1.12 tv bbase[i] = buf;
167 1.12 tv }
168 1.12 tv
169 1.12 tv void
170 1.12 tv enlarge_strspace()
171 1.12 tv {
172 1.12 tv char *newstrspace;
173 1.12 tv int i;
174 1.12 tv
175 1.12 tv strsize *= 2;
176 1.12 tv newstrspace = malloc(strsize + 1);
177 1.12 tv if (!newstrspace)
178 1.12 tv errx(1, "string space overflow");
179 1.12 tv memcpy(newstrspace, strspace, strsize/2);
180 1.12 tv for (i = 0; i <= sp; i++)
181 1.12 tv if (sstack[i])
182 1.12 tv mstack[i].sstr = (mstack[i].sstr - strspace)
183 1.12 tv + newstrspace;
184 1.12 tv ep = (ep-strspace) + newstrspace;
185 1.12 tv free(strspace);
186 1.12 tv strspace = newstrspace;
187 1.12 tv endest = strspace + strsize;
188 1.12 tv }
189 1.12 tv
190 1.12 tv void
191 1.12 tv enlarge_bufspace()
192 1.12 tv {
193 1.12 tv char *newbuf;
194 1.12 tv int i;
195 1.12 tv
196 1.12 tv bufsize *= 2;
197 1.12 tv newbuf = realloc(buf, bufsize);
198 1.12 tv if (!newbuf)
199 1.12 tv errx(1, "too many characters pushed back");
200 1.12 tv for (i = 0; i < MAXINP; i++)
201 1.12 tv bbase[i] = (bbase[i]-buf)+newbuf;
202 1.12 tv bp = (bp-buf)+newbuf;
203 1.12 tv bufbase = (bufbase-buf)+newbuf;
204 1.12 tv buf = newbuf;
205 1.12 tv endpbb = buf+bufsize;
206 1.12 tv }
207 1.12 tv
208 1.12 tv /*
209 1.5 glass * chrsave - put single char on string space
210 1.5 glass */
211 1.5 glass void
212 1.5 glass chrsave(c)
213 1.12 tv int c;
214 1.5 glass {
215 1.12 tv if (ep >= endest)
216 1.12 tv enlarge_strspace();
217 1.12 tv *ep++ = c;
218 1.5 glass }
219 1.5 glass
220 1.5 glass /*
221 1.5 glass * read in a diversion file, and dispose it.
222 1.5 glass */
223 1.5 glass void
224 1.5 glass getdiv(n)
225 1.7 lukem int n;
226 1.5 glass {
227 1.7 lukem int c;
228 1.2 glass
229 1.5 glass if (active == outfile[n])
230 1.7 lukem errx(1, "undivert: diversion still active");
231 1.12 tv rewind(outfile[n]);
232 1.12 tv while ((c = getc(outfile[n])) != EOF)
233 1.12 tv putc(c, active);
234 1.5 glass (void) fclose(outfile[n]);
235 1.5 glass outfile[n] = NULL;
236 1.5 glass }
237 1.2 glass
238 1.5 glass void
239 1.5 glass onintr(signo)
240 1.5 glass int signo;
241 1.5 glass {
242 1.12 tv #define intrmessage "m4: interrupted.\n"
243 1.12 tv write(STDERR_FILENO, intrmessage, sizeof(intrmessage));
244 1.12 tv _exit(1);
245 1.5 glass }
246 1.5 glass
247 1.5 glass /*
248 1.5 glass * killdiv - get rid of the diversion files
249 1.5 glass */
250 1.5 glass void
251 1.5 glass killdiv()
252 1.5 glass {
253 1.7 lukem int n;
254 1.2 glass
255 1.12 tv for (n = 0; n < maxout; n++)
256 1.5 glass if (outfile[n] != NULL) {
257 1.5 glass (void) fclose(outfile[n]);
258 1.5 glass }
259 1.5 glass }
260 1.2 glass
261 1.12 tv /*
262 1.12 tv * resizedivs: allocate more diversion files */
263 1.12 tv void
264 1.12 tv resizedivs(n)
265 1.12 tv int n;
266 1.12 tv {
267 1.12 tv int i;
268 1.12 tv
269 1.12 tv outfile = (FILE **)realloc(outfile, sizeof(FILE *) * n);
270 1.12 tv if (outfile == NULL)
271 1.12 tv errx(1, "too many diverts %d", n);
272 1.12 tv for (i = maxout; i < n; i++)
273 1.12 tv outfile[i] = NULL;
274 1.12 tv maxout = n;
275 1.12 tv }
276 1.12 tv
277 1.12 tv void *
278 1.5 glass xalloc(n)
279 1.12 tv size_t n;
280 1.5 glass {
281 1.7 lukem char *p = malloc(n);
282 1.2 glass
283 1.5 glass if (p == NULL)
284 1.7 lukem err(1, "malloc");
285 1.5 glass return p;
286 1.5 glass }
287 1.2 glass
288 1.5 glass char *
289 1.5 glass xstrdup(s)
290 1.7 lukem const char *s;
291 1.5 glass {
292 1.7 lukem char *p = strdup(s);
293 1.5 glass if (p == NULL)
294 1.7 lukem err(1, "strdup");
295 1.5 glass return p;
296 1.5 glass }
297 1.2 glass
298 1.12 tv void
299 1.13 tv usage(progname)
300 1.13 tv const char *progname;
301 1.12 tv {
302 1.13 tv fprintf(stderr, "usage: %s [-Pg] [-Dname[=val]] [-I dirname] [-Uname]\n", progname);
303 1.13 tv fprintf(stderr, "\t[-d flags] [-o trfile] [-t macro]\n");
304 1.12 tv exit(1);
305 1.12 tv }
306 1.12 tv
307 1.12 tv int
308 1.12 tv obtain_char(f)
309 1.12 tv struct input_file *f;
310 1.12 tv {
311 1.12 tv if (f->c == EOF)
312 1.12 tv return EOF;
313 1.12 tv else if (f->c == '\n')
314 1.12 tv f->lineno++;
315 1.12 tv
316 1.12 tv f->c = fgetc(f->file);
317 1.12 tv return f->c;
318 1.12 tv }
319 1.12 tv
320 1.12 tv void
321 1.12 tv set_input(f, real, name)
322 1.12 tv struct input_file *f;
323 1.12 tv FILE *real;
324 1.12 tv const char *name;
325 1.12 tv {
326 1.12 tv f->file = real;
327 1.12 tv f->lineno = 1;
328 1.12 tv f->c = 0;
329 1.12 tv f->name = xstrdup(name);
330 1.12 tv }
331 1.12 tv
332 1.12 tv void
333 1.12 tv release_input(f)
334 1.12 tv struct input_file *f;
335 1.12 tv {
336 1.12 tv if (f->file != stdin)
337 1.12 tv fclose(f->file);
338 1.12 tv f->c = EOF;
339 1.12 tv /*
340 1.12 tv * XXX can't free filename, as there might still be
341 1.12 tv * error information pointing to it.
342 1.12 tv */
343 1.12 tv }
344 1.12 tv
345 1.12 tv void
346 1.12 tv doprintlineno(f)
347 1.12 tv struct input_file *f;
348 1.5 glass {
349 1.12 tv pbunsigned(f->lineno);
350 1.12 tv }
351 1.2 glass
352 1.12 tv void
353 1.12 tv doprintfilename(f)
354 1.12 tv struct input_file *f;
355 1.12 tv {
356 1.12 tv pbstr(rquote);
357 1.12 tv pbstr(f->name);
358 1.12 tv pbstr(lquote);
359 1.12 tv }
360 1.2 glass
361 1.12 tv /*
362 1.12 tv * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
363 1.12 tv * and later dump everything that was added since then to a file.
364 1.12 tv */
365 1.12 tv size_t
366 1.12 tv buffer_mark()
367 1.12 tv {
368 1.12 tv return bp - buf;
369 1.5 glass }
370 1.2 glass
371 1.12 tv
372 1.5 glass void
373 1.12 tv dump_buffer(f, m)
374 1.12 tv FILE *f;
375 1.12 tv size_t m;
376 1.5 glass {
377 1.12 tv char *s;
378 1.12 tv
379 1.12 tv for (s = bp; s-buf > m;)
380 1.12 tv fputc(*--s, f);
381 1.5 glass }
382