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