xref: /original-bsd/usr.sbin/config.new/util.c (revision 44178c51)
1 /*
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, Lawrence Berkeley Laboratories.
13  *
14  * %sccs.include.redist.c%
15  *
16  *	@(#)util.c	5.1 (Berkeley) 01/12/93
17  *
18  * from: $Header: util.c,v 1.3 93/01/12 03:59:55 torek Exp $
19  */
20 
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #if __STDC__
25 #include <stdarg.h>
26 #else
27 #include <varargs.h>
28 #endif
29 #include "config.h"
30 
31 static void nomem __P((void));
32 static void vxerror __P((const char *, int, const char *, va_list));
33 
34 /*
35  * Malloc, with abort on error.
36  */
37 void *
38 emalloc(size)
39 	size_t size;
40 {
41 	void *p;
42 
43 	if ((p = malloc(size)) == NULL)
44 		nomem();
45 	return (p);
46 }
47 
48 /*
49  * Realloc, with abort on error.
50  */
51 void *
52 erealloc(p, size)
53 	void *p;
54 	size_t size;
55 {
56 
57 	if ((p = realloc(p, size)) == NULL)
58 		nomem();
59 	return (p);
60 }
61 
62 static void
63 nomem()
64 {
65 
66 	(void)fprintf(stderr, "config: out of memory\n");
67 	exit(1);
68 }
69 
70 /*
71  * Prepend the compilation directory to a file name.
72  */
73 char *
74 path(file)
75 	const char *file;
76 {
77 	register char *cp;
78 #define	CDIR "../../compile/"
79 
80 	if (file == NULL) {
81 		cp = emalloc(sizeof(CDIR) + strlen(confdirbase));
82 		(void)sprintf(cp, "%s%s", CDIR, confdirbase);
83 	} else {
84 		cp = emalloc(sizeof(CDIR) + strlen(confdirbase) + 1 +
85 		    strlen(file));
86 		(void)sprintf(cp, "%s%s/%s", CDIR, confdirbase, file);
87 	}
88 	return (cp);
89 }
90 
91 static struct nvlist *nvhead;
92 
93 struct nvlist *
94 newnv(name, str, ptr, i)
95 	const char *name, *str;
96 	void *ptr;
97 	int i;
98 {
99 	register struct nvlist *nv;
100 
101 	if ((nv = nvhead) == NULL)
102 		nv = emalloc(sizeof(*nv));
103 	else
104 		nvhead = nv->nv_next;
105 	nv->nv_next = NULL;
106 	nv->nv_name = name;
107 	if (ptr == NULL)
108 		nv->nv_str = str;
109 	else {
110 		if (str != NULL)
111 			panic("newnv");
112 		nv->nv_ptr = ptr;
113 	}
114 	nv->nv_int = i;
115 	return (nv);
116 }
117 
118 /*
119  * Free an nvlist structure (just one).
120  */
121 void
122 nvfree(nv)
123 	register struct nvlist *nv;
124 {
125 
126 	nv->nv_next = nvhead;
127 	nvhead = nv;
128 }
129 
130 /*
131  * Free an nvlist (the whole list).
132  */
133 void
134 nvfreel(nv)
135 	register struct nvlist *nv;
136 {
137 	register struct nvlist *next;
138 
139 	for (; nv != NULL; nv = next) {
140 		next = nv->nv_next;
141 		nv->nv_next = nvhead;
142 		nvhead = nv;
143 	}
144 }
145 
146 /*
147  * External (config file) error.  Complain, using current file
148  * and line number.
149  */
150 void
151 #if __STDC__
152 error(const char *fmt, ...)
153 #else
154 error(fmt, va_alist)
155 	const char *fmt;
156 	va_dcl
157 #endif
158 {
159 	va_list ap;
160 	extern const char *yyfile;
161 
162 #if __STDC__
163 	va_start(ap, fmt);
164 #else
165 	va_start(ap);
166 #endif
167 	vxerror(yyfile, currentline(), fmt, ap);
168 	va_end(ap);
169 }
170 
171 /*
172  * Delayed config file error (i.e., something was wrong but we could not
173  * find out about it until later).
174  */
175 void
176 #if __STDC__
177 xerror(const char *file, int line, const char *fmt, ...)
178 #else
179 xerror(file, line, fmt, va_alist)
180 	const char *file;
181 	int line;
182 	const char *fmt;
183 	va_dcl
184 #endif
185 {
186 	va_list ap;
187 
188 #if __STDC__
189 	va_start(ap, fmt);
190 #else
191 	va_start(ap);
192 #endif
193 	vxerror(file, line, fmt, ap);
194 	va_end(ap);
195 }
196 
197 /*
198  * Internal form of error() and xerror().
199  */
200 static void
201 vxerror(file, line, fmt, ap)
202 	const char *file;
203 	int line;
204 	const char *fmt;
205 	va_list ap;
206 {
207 
208 	(void)fprintf(stderr, "%s:%d: ", file, line);
209 	(void)vfprintf(stderr, fmt, ap);
210 	(void)putc('\n', stderr);
211 	errors++;
212 }
213 
214 /*
215  * Internal error, abort.
216  */
217 __dead void
218 #if __STDC__
219 panic(const char *fmt, ...)
220 #else
221 panic(fmt, va_alist)
222 	const char *fmt;
223 	va_dcl
224 #endif
225 {
226 	va_list ap;
227 
228 #if __STDC__
229 	va_start(ap, fmt);
230 #else
231 	va_start(ap);
232 #endif
233 	(void)fprintf(stderr, "config: panic: ");
234 	(void)vfprintf(stderr, fmt, ap);
235 	(void)putc('\n', stderr);
236 	va_end(ap);
237 	exit(2);
238 }
239