util.c revision 1.6 1 1.6 christos /* $NetBSD: util.c,v 1.6 2007/01/13 23:47:36 christos 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.3 christos #include <sys/types.h>
48 1.1 thorpej #include <ctype.h>
49 1.1 thorpej #include <stdio.h>
50 1.1 thorpej #include <stdlib.h>
51 1.1 thorpej #include <string.h>
52 1.1 thorpej #include <stdarg.h>
53 1.3 christos #include <util.h>
54 1.6 christos #include <err.h>
55 1.1 thorpej #include "defs.h"
56 1.1 thorpej
57 1.6 christos static void cfgvxerror(const char *, int, const char *, va_list)
58 1.1 thorpej __attribute__((__format__(__printf__, 3, 0)));
59 1.6 christos static void cfgvxwarn(const char *, int, const char *, va_list)
60 1.1 thorpej __attribute__((__format__(__printf__, 3, 0)));
61 1.6 christos static void cfgvxmsg(const char *, int, const char *, const char *, va_list)
62 1.1 thorpej __attribute__((__format__(__printf__, 4, 0)));
63 1.1 thorpej
64 1.1 thorpej /*
65 1.1 thorpej * Push a prefix onto the prefix stack.
66 1.1 thorpej */
67 1.1 thorpej void
68 1.1 thorpej prefix_push(const char *path)
69 1.1 thorpej {
70 1.1 thorpej struct prefix *pf;
71 1.1 thorpej char *cp;
72 1.1 thorpej
73 1.1 thorpej pf = ecalloc(1, sizeof(struct prefix));
74 1.1 thorpej
75 1.1 thorpej if (! SLIST_EMPTY(&prefixes) && *path != '/') {
76 1.1 thorpej cp = emalloc(strlen(SLIST_FIRST(&prefixes)->pf_prefix) + 1 +
77 1.1 thorpej strlen(path) + 1);
78 1.1 thorpej (void) sprintf(cp, "%s/%s",
79 1.1 thorpej SLIST_FIRST(&prefixes)->pf_prefix, path);
80 1.1 thorpej pf->pf_prefix = intern(cp);
81 1.1 thorpej free(cp);
82 1.1 thorpej } else
83 1.1 thorpej pf->pf_prefix = intern(path);
84 1.1 thorpej
85 1.1 thorpej SLIST_INSERT_HEAD(&prefixes, pf, pf_next);
86 1.1 thorpej }
87 1.1 thorpej
88 1.1 thorpej /*
89 1.1 thorpej * Pop a prefix off the prefix stack.
90 1.1 thorpej */
91 1.1 thorpej void
92 1.1 thorpej prefix_pop(void)
93 1.1 thorpej {
94 1.1 thorpej struct prefix *pf;
95 1.1 thorpej
96 1.1 thorpej if ((pf = SLIST_FIRST(&prefixes)) == NULL) {
97 1.6 christos cfgerror("no prefixes on the stack to pop");
98 1.1 thorpej return;
99 1.1 thorpej }
100 1.1 thorpej
101 1.1 thorpej SLIST_REMOVE_HEAD(&prefixes, pf_next);
102 1.1 thorpej /* Remember this prefix for emitting -I... directives later. */
103 1.1 thorpej SLIST_INSERT_HEAD(&allprefixes, pf, pf_next);
104 1.1 thorpej }
105 1.1 thorpej
106 1.1 thorpej /*
107 1.1 thorpej * Prepend the source path to a file name.
108 1.1 thorpej */
109 1.1 thorpej char *
110 1.1 thorpej sourcepath(const char *file)
111 1.1 thorpej {
112 1.1 thorpej size_t len;
113 1.1 thorpej char *cp;
114 1.1 thorpej struct prefix *pf;
115 1.1 thorpej
116 1.1 thorpej pf = SLIST_EMPTY(&prefixes) ? NULL : SLIST_FIRST(&prefixes);
117 1.1 thorpej if (pf != NULL && *pf->pf_prefix == '/')
118 1.1 thorpej len = strlen(pf->pf_prefix) + 1 + strlen(file) + 1;
119 1.1 thorpej else {
120 1.1 thorpej len = strlen(srcdir) + 1 + strlen(file) + 1;
121 1.1 thorpej if (pf != NULL)
122 1.1 thorpej len += strlen(pf->pf_prefix) + 1;
123 1.1 thorpej }
124 1.1 thorpej
125 1.1 thorpej cp = emalloc(len);
126 1.1 thorpej
127 1.1 thorpej if (pf != NULL) {
128 1.1 thorpej if (*pf->pf_prefix == '/')
129 1.1 thorpej (void) sprintf(cp, "%s/%s", pf->pf_prefix, file);
130 1.1 thorpej else
131 1.1 thorpej (void) sprintf(cp, "%s/%s/%s", srcdir,
132 1.1 thorpej pf->pf_prefix, file);
133 1.1 thorpej } else
134 1.1 thorpej (void) sprintf(cp, "%s/%s", srcdir, file);
135 1.1 thorpej return (cp);
136 1.1 thorpej }
137 1.1 thorpej
138 1.1 thorpej struct nvlist *
139 1.1 thorpej newnv(const char *name, const char *str, void *ptr, int i, struct nvlist *next)
140 1.1 thorpej {
141 1.1 thorpej struct nvlist *nv;
142 1.1 thorpej
143 1.4 dsl nv = ecalloc(1, sizeof(*nv));
144 1.1 thorpej nv->nv_next = next;
145 1.1 thorpej nv->nv_name = name;
146 1.4 dsl nv->nv_str = str;
147 1.4 dsl nv->nv_ptr = ptr;
148 1.1 thorpej nv->nv_int = i;
149 1.4 dsl return nv;
150 1.1 thorpej }
151 1.1 thorpej
152 1.1 thorpej /*
153 1.1 thorpej * Free an nvlist structure (just one).
154 1.1 thorpej */
155 1.1 thorpej void
156 1.1 thorpej nvfree(struct nvlist *nv)
157 1.1 thorpej {
158 1.1 thorpej
159 1.4 dsl free(nv);
160 1.1 thorpej }
161 1.1 thorpej
162 1.1 thorpej /*
163 1.1 thorpej * Free an nvlist (the whole list).
164 1.1 thorpej */
165 1.1 thorpej void
166 1.1 thorpej nvfreel(struct nvlist *nv)
167 1.1 thorpej {
168 1.1 thorpej struct nvlist *next;
169 1.1 thorpej
170 1.1 thorpej for (; nv != NULL; nv = next) {
171 1.1 thorpej next = nv->nv_next;
172 1.4 dsl free(nv);
173 1.1 thorpej }
174 1.1 thorpej }
175 1.1 thorpej
176 1.5 cube struct nvlist *
177 1.5 cube nvcat(struct nvlist *nv1, struct nvlist *nv2)
178 1.5 cube {
179 1.5 cube struct nvlist *nv;
180 1.5 cube
181 1.5 cube if (nv1 == NULL)
182 1.5 cube return nv2;
183 1.5 cube
184 1.5 cube for (nv = nv1; nv->nv_next != NULL; nv = nv->nv_next);
185 1.5 cube
186 1.5 cube nv->nv_next = nv2;
187 1.5 cube return nv1;
188 1.5 cube }
189 1.5 cube
190 1.1 thorpej void
191 1.6 christos cfgwarn(const char *fmt, ...)
192 1.1 thorpej {
193 1.1 thorpej va_list ap;
194 1.1 thorpej extern const char *yyfile;
195 1.1 thorpej
196 1.1 thorpej va_start(ap, fmt);
197 1.6 christos cfgvxwarn(yyfile, currentline(), fmt, ap);
198 1.1 thorpej va_end(ap);
199 1.1 thorpej }
200 1.1 thorpej
201 1.2 cube void
202 1.6 christos cfgxwarn(const char *file, int line, const char *fmt, ...)
203 1.2 cube {
204 1.2 cube va_list ap;
205 1.2 cube
206 1.2 cube va_start(ap, fmt);
207 1.6 christos cfgvxwarn(file, line, fmt, ap);
208 1.2 cube va_end(ap);
209 1.2 cube }
210 1.1 thorpej
211 1.1 thorpej static void
212 1.6 christos cfgvxwarn(const char *file, int line, const char *fmt, va_list ap)
213 1.1 thorpej {
214 1.6 christos cfgvxmsg(file, line, "warning: ", fmt, ap);
215 1.1 thorpej }
216 1.1 thorpej
217 1.1 thorpej /*
218 1.1 thorpej * External (config file) error. Complain, using current file
219 1.1 thorpej * and line number.
220 1.1 thorpej */
221 1.1 thorpej void
222 1.6 christos cfgerror(const char *fmt, ...)
223 1.1 thorpej {
224 1.1 thorpej va_list ap;
225 1.1 thorpej extern const char *yyfile;
226 1.1 thorpej
227 1.1 thorpej va_start(ap, fmt);
228 1.6 christos cfgvxerror(yyfile, currentline(), fmt, ap);
229 1.1 thorpej va_end(ap);
230 1.1 thorpej }
231 1.1 thorpej
232 1.1 thorpej /*
233 1.1 thorpej * Delayed config file error (i.e., something was wrong but we could not
234 1.1 thorpej * find out about it until later).
235 1.1 thorpej */
236 1.1 thorpej void
237 1.6 christos cfgxerror(const char *file, int line, const char *fmt, ...)
238 1.1 thorpej {
239 1.1 thorpej va_list ap;
240 1.1 thorpej
241 1.1 thorpej va_start(ap, fmt);
242 1.6 christos cfgvxerror(file, line, fmt, ap);
243 1.1 thorpej va_end(ap);
244 1.1 thorpej }
245 1.1 thorpej
246 1.1 thorpej /*
247 1.1 thorpej * Internal form of error() and xerror().
248 1.1 thorpej */
249 1.1 thorpej static void
250 1.6 christos cfgvxerror(const char *file, int line, const char *fmt, va_list ap)
251 1.1 thorpej {
252 1.6 christos cfgvxmsg(file, line, "", fmt, ap);
253 1.1 thorpej errors++;
254 1.1 thorpej }
255 1.1 thorpej
256 1.1 thorpej
257 1.1 thorpej /*
258 1.1 thorpej * Internal error, abort.
259 1.1 thorpej */
260 1.1 thorpej __dead void
261 1.1 thorpej panic(const char *fmt, ...)
262 1.1 thorpej {
263 1.1 thorpej va_list ap;
264 1.1 thorpej
265 1.1 thorpej va_start(ap, fmt);
266 1.6 christos (void)fprintf(stderr, "%s: panic: ", getprogname());
267 1.1 thorpej (void)vfprintf(stderr, fmt, ap);
268 1.1 thorpej (void)putc('\n', stderr);
269 1.1 thorpej va_end(ap);
270 1.1 thorpej exit(2);
271 1.1 thorpej }
272 1.1 thorpej
273 1.1 thorpej /*
274 1.1 thorpej * Internal form of error() and xerror().
275 1.1 thorpej */
276 1.1 thorpej static void
277 1.6 christos cfgvxmsg(const char *file, int line, const char *msgclass, const char *fmt,
278 1.1 thorpej va_list ap)
279 1.1 thorpej {
280 1.1 thorpej
281 1.1 thorpej (void)fprintf(stderr, "%s:%d: %s", file, line, msgclass);
282 1.1 thorpej (void)vfprintf(stderr, fmt, ap);
283 1.1 thorpej (void)putc('\n', stderr);
284 1.1 thorpej }
285