util.c revision 1.1 1 1.1 thorpej /* $NetBSD: util.c,v 1.1 2005/06/05 18:19:53 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1992, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This software was developed by the Computer Systems Engineering group
8 1.1 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 thorpej * contributed to Berkeley.
10 1.1 thorpej *
11 1.1 thorpej * All advertising materials mentioning features or use of this software
12 1.1 thorpej * must display the following acknowledgement:
13 1.1 thorpej * This product includes software developed by the University of
14 1.1 thorpej * California, Lawrence Berkeley Laboratories.
15 1.1 thorpej *
16 1.1 thorpej * Redistribution and use in source and binary forms, with or without
17 1.1 thorpej * modification, are permitted provided that the following conditions
18 1.1 thorpej * are met:
19 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
20 1.1 thorpej * notice, this list of conditions and the following disclaimer.
21 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
23 1.1 thorpej * documentation and/or other materials provided with the distribution.
24 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
25 1.1 thorpej * may be used to endorse or promote products derived from this software
26 1.1 thorpej * without specific prior written permission.
27 1.1 thorpej *
28 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 thorpej * SUCH DAMAGE.
39 1.1 thorpej *
40 1.1 thorpej * from: @(#)util.c 8.1 (Berkeley) 6/6/93
41 1.1 thorpej */
42 1.1 thorpej
43 1.1 thorpej #if HAVE_NBTOOL_CONFIG_H
44 1.1 thorpej #include "nbtool_config.h"
45 1.1 thorpej #endif
46 1.1 thorpej
47 1.1 thorpej #include <ctype.h>
48 1.1 thorpej #include <stdio.h>
49 1.1 thorpej #include <stdlib.h>
50 1.1 thorpej #include <string.h>
51 1.1 thorpej #include <stdarg.h>
52 1.1 thorpej #include <sys/types.h>
53 1.1 thorpej #include "defs.h"
54 1.1 thorpej
55 1.1 thorpej static void nomem(void);
56 1.1 thorpej static void vxerror(const char *, int, const char *, va_list)
57 1.1 thorpej __attribute__((__format__(__printf__, 3, 0)));
58 1.1 thorpej static void vxwarn(const char *, int, const char *, va_list)
59 1.1 thorpej __attribute__((__format__(__printf__, 3, 0)));
60 1.1 thorpej static void vxmsg(const char *fname, int line, const char *class,
61 1.1 thorpej const char *fmt, va_list)
62 1.1 thorpej __attribute__((__format__(__printf__, 4, 0)));
63 1.1 thorpej
64 1.1 thorpej /*
65 1.1 thorpej * Calloc, with abort on error.
66 1.1 thorpej */
67 1.1 thorpej void *
68 1.1 thorpej ecalloc(size_t nelem, size_t size)
69 1.1 thorpej {
70 1.1 thorpej void *p;
71 1.1 thorpej
72 1.1 thorpej if ((p = calloc(nelem, size)) == NULL)
73 1.1 thorpej nomem();
74 1.1 thorpej return (p);
75 1.1 thorpej }
76 1.1 thorpej
77 1.1 thorpej /*
78 1.1 thorpej * Malloc, with abort on error.
79 1.1 thorpej */
80 1.1 thorpej void *
81 1.1 thorpej emalloc(size_t size)
82 1.1 thorpej {
83 1.1 thorpej void *p;
84 1.1 thorpej
85 1.1 thorpej if ((p = malloc(size)) == NULL)
86 1.1 thorpej nomem();
87 1.1 thorpej return (p);
88 1.1 thorpej }
89 1.1 thorpej
90 1.1 thorpej /*
91 1.1 thorpej * Realloc, with abort on error.
92 1.1 thorpej */
93 1.1 thorpej void *
94 1.1 thorpej erealloc(void *p, size_t size)
95 1.1 thorpej {
96 1.1 thorpej void *q;
97 1.1 thorpej
98 1.1 thorpej if ((q = realloc(p, size)) == NULL)
99 1.1 thorpej nomem();
100 1.1 thorpej p = q;
101 1.1 thorpej return (p);
102 1.1 thorpej }
103 1.1 thorpej
104 1.1 thorpej /*
105 1.1 thorpej * Strdup, with abort on error.
106 1.1 thorpej */
107 1.1 thorpej char *
108 1.1 thorpej estrdup(const char *p)
109 1.1 thorpej {
110 1.1 thorpej char *cp;
111 1.1 thorpej
112 1.1 thorpej if ((cp = strdup(p)) == NULL)
113 1.1 thorpej nomem();
114 1.1 thorpej return (cp);
115 1.1 thorpej }
116 1.1 thorpej
117 1.1 thorpej static void
118 1.1 thorpej nomem(void)
119 1.1 thorpej {
120 1.1 thorpej
121 1.1 thorpej (void)fprintf(stderr, "config: out of memory\n");
122 1.1 thorpej exit(1);
123 1.1 thorpej }
124 1.1 thorpej
125 1.1 thorpej /*
126 1.1 thorpej * Push a prefix onto the prefix stack.
127 1.1 thorpej */
128 1.1 thorpej void
129 1.1 thorpej prefix_push(const char *path)
130 1.1 thorpej {
131 1.1 thorpej struct prefix *pf;
132 1.1 thorpej char *cp;
133 1.1 thorpej
134 1.1 thorpej pf = ecalloc(1, sizeof(struct prefix));
135 1.1 thorpej
136 1.1 thorpej if (! SLIST_EMPTY(&prefixes) && *path != '/') {
137 1.1 thorpej cp = emalloc(strlen(SLIST_FIRST(&prefixes)->pf_prefix) + 1 +
138 1.1 thorpej strlen(path) + 1);
139 1.1 thorpej (void) sprintf(cp, "%s/%s",
140 1.1 thorpej SLIST_FIRST(&prefixes)->pf_prefix, path);
141 1.1 thorpej pf->pf_prefix = intern(cp);
142 1.1 thorpej free(cp);
143 1.1 thorpej } else
144 1.1 thorpej pf->pf_prefix = intern(path);
145 1.1 thorpej
146 1.1 thorpej SLIST_INSERT_HEAD(&prefixes, pf, pf_next);
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej /*
150 1.1 thorpej * Pop a prefix off the prefix stack.
151 1.1 thorpej */
152 1.1 thorpej void
153 1.1 thorpej prefix_pop(void)
154 1.1 thorpej {
155 1.1 thorpej struct prefix *pf;
156 1.1 thorpej
157 1.1 thorpej if ((pf = SLIST_FIRST(&prefixes)) == NULL) {
158 1.1 thorpej error("no prefixes on the stack to pop");
159 1.1 thorpej return;
160 1.1 thorpej }
161 1.1 thorpej
162 1.1 thorpej SLIST_REMOVE_HEAD(&prefixes, pf_next);
163 1.1 thorpej /* Remember this prefix for emitting -I... directives later. */
164 1.1 thorpej SLIST_INSERT_HEAD(&allprefixes, pf, pf_next);
165 1.1 thorpej }
166 1.1 thorpej
167 1.1 thorpej /*
168 1.1 thorpej * Prepend the source path to a file name.
169 1.1 thorpej */
170 1.1 thorpej char *
171 1.1 thorpej sourcepath(const char *file)
172 1.1 thorpej {
173 1.1 thorpej size_t len;
174 1.1 thorpej char *cp;
175 1.1 thorpej struct prefix *pf;
176 1.1 thorpej
177 1.1 thorpej pf = SLIST_EMPTY(&prefixes) ? NULL : SLIST_FIRST(&prefixes);
178 1.1 thorpej if (pf != NULL && *pf->pf_prefix == '/')
179 1.1 thorpej len = strlen(pf->pf_prefix) + 1 + strlen(file) + 1;
180 1.1 thorpej else {
181 1.1 thorpej len = strlen(srcdir) + 1 + strlen(file) + 1;
182 1.1 thorpej if (pf != NULL)
183 1.1 thorpej len += strlen(pf->pf_prefix) + 1;
184 1.1 thorpej }
185 1.1 thorpej
186 1.1 thorpej cp = emalloc(len);
187 1.1 thorpej
188 1.1 thorpej if (pf != NULL) {
189 1.1 thorpej if (*pf->pf_prefix == '/')
190 1.1 thorpej (void) sprintf(cp, "%s/%s", pf->pf_prefix, file);
191 1.1 thorpej else
192 1.1 thorpej (void) sprintf(cp, "%s/%s/%s", srcdir,
193 1.1 thorpej pf->pf_prefix, file);
194 1.1 thorpej } else
195 1.1 thorpej (void) sprintf(cp, "%s/%s", srcdir, file);
196 1.1 thorpej return (cp);
197 1.1 thorpej }
198 1.1 thorpej
199 1.1 thorpej static struct nvlist *nvfreelist;
200 1.1 thorpej
201 1.1 thorpej struct nvlist *
202 1.1 thorpej newnv(const char *name, const char *str, void *ptr, int i, struct nvlist *next)
203 1.1 thorpej {
204 1.1 thorpej struct nvlist *nv;
205 1.1 thorpej
206 1.1 thorpej if ((nv = nvfreelist) == NULL)
207 1.1 thorpej nv = ecalloc(1, sizeof(*nv));
208 1.1 thorpej else
209 1.1 thorpej nvfreelist = nv->nv_next;
210 1.1 thorpej nv->nv_next = next;
211 1.1 thorpej nv->nv_name = name;
212 1.1 thorpej if (ptr == NULL)
213 1.1 thorpej nv->nv_str = str;
214 1.1 thorpej else {
215 1.1 thorpej if (str != NULL)
216 1.1 thorpej panic("newnv");
217 1.1 thorpej nv->nv_ptr = ptr;
218 1.1 thorpej }
219 1.1 thorpej nv->nv_int = i;
220 1.1 thorpej return (nv);
221 1.1 thorpej }
222 1.1 thorpej
223 1.1 thorpej /*
224 1.1 thorpej * Free an nvlist structure (just one).
225 1.1 thorpej */
226 1.1 thorpej void
227 1.1 thorpej nvfree(struct nvlist *nv)
228 1.1 thorpej {
229 1.1 thorpej
230 1.1 thorpej memset(nv, 0, sizeof(*nv));
231 1.1 thorpej nv->nv_next = nvfreelist;
232 1.1 thorpej nvfreelist = nv;
233 1.1 thorpej }
234 1.1 thorpej
235 1.1 thorpej /*
236 1.1 thorpej * Free an nvlist (the whole list).
237 1.1 thorpej */
238 1.1 thorpej void
239 1.1 thorpej nvfreel(struct nvlist *nv)
240 1.1 thorpej {
241 1.1 thorpej struct nvlist *next;
242 1.1 thorpej
243 1.1 thorpej for (; nv != NULL; nv = next) {
244 1.1 thorpej next = nv->nv_next;
245 1.1 thorpej memset(nv, 0, sizeof(*nv));
246 1.1 thorpej nv->nv_next = nvfreelist;
247 1.1 thorpej nvfreelist = nv;
248 1.1 thorpej }
249 1.1 thorpej }
250 1.1 thorpej
251 1.1 thorpej void
252 1.1 thorpej warn(const char *fmt, ...)
253 1.1 thorpej {
254 1.1 thorpej va_list ap;
255 1.1 thorpej extern const char *yyfile;
256 1.1 thorpej
257 1.1 thorpej va_start(ap, fmt);
258 1.1 thorpej vxwarn(yyfile, currentline(), fmt, ap);
259 1.1 thorpej va_end(ap);
260 1.1 thorpej }
261 1.1 thorpej
262 1.1 thorpej
263 1.1 thorpej static void
264 1.1 thorpej vxwarn(const char *file, int line, const char *fmt, va_list ap)
265 1.1 thorpej {
266 1.1 thorpej vxmsg(file, line, "warning: ", fmt, ap);
267 1.1 thorpej }
268 1.1 thorpej
269 1.1 thorpej /*
270 1.1 thorpej * External (config file) error. Complain, using current file
271 1.1 thorpej * and line number.
272 1.1 thorpej */
273 1.1 thorpej void
274 1.1 thorpej error(const char *fmt, ...)
275 1.1 thorpej {
276 1.1 thorpej va_list ap;
277 1.1 thorpej extern const char *yyfile;
278 1.1 thorpej
279 1.1 thorpej va_start(ap, fmt);
280 1.1 thorpej vxerror(yyfile, currentline(), fmt, ap);
281 1.1 thorpej va_end(ap);
282 1.1 thorpej }
283 1.1 thorpej
284 1.1 thorpej /*
285 1.1 thorpej * Delayed config file error (i.e., something was wrong but we could not
286 1.1 thorpej * find out about it until later).
287 1.1 thorpej */
288 1.1 thorpej void
289 1.1 thorpej xerror(const char *file, int line, const char *fmt, ...)
290 1.1 thorpej {
291 1.1 thorpej va_list ap;
292 1.1 thorpej
293 1.1 thorpej va_start(ap, fmt);
294 1.1 thorpej vxerror(file, line, fmt, ap);
295 1.1 thorpej va_end(ap);
296 1.1 thorpej }
297 1.1 thorpej
298 1.1 thorpej /*
299 1.1 thorpej * Internal form of error() and xerror().
300 1.1 thorpej */
301 1.1 thorpej static void
302 1.1 thorpej vxerror(const char *file, int line, const char *fmt, va_list ap)
303 1.1 thorpej {
304 1.1 thorpej vxmsg(file, line, "", fmt, ap);
305 1.1 thorpej errors++;
306 1.1 thorpej }
307 1.1 thorpej
308 1.1 thorpej
309 1.1 thorpej /*
310 1.1 thorpej * Internal error, abort.
311 1.1 thorpej */
312 1.1 thorpej __dead void
313 1.1 thorpej panic(const char *fmt, ...)
314 1.1 thorpej {
315 1.1 thorpej va_list ap;
316 1.1 thorpej
317 1.1 thorpej va_start(ap, fmt);
318 1.1 thorpej (void)fprintf(stderr, "config: panic: ");
319 1.1 thorpej (void)vfprintf(stderr, fmt, ap);
320 1.1 thorpej (void)putc('\n', stderr);
321 1.1 thorpej va_end(ap);
322 1.1 thorpej exit(2);
323 1.1 thorpej }
324 1.1 thorpej
325 1.1 thorpej /*
326 1.1 thorpej * Internal form of error() and xerror().
327 1.1 thorpej */
328 1.1 thorpej static void
329 1.1 thorpej vxmsg(const char *file, int line, const char *msgclass, const char *fmt,
330 1.1 thorpej va_list ap)
331 1.1 thorpej {
332 1.1 thorpej
333 1.1 thorpej (void)fprintf(stderr, "%s:%d: %s", file, line, msgclass);
334 1.1 thorpej (void)vfprintf(stderr, fmt, ap);
335 1.1 thorpej (void)putc('\n', stderr);
336 1.1 thorpej }
337