1 /* $OpenBSD: util.c,v 1.12 2003/06/28 04:55:07 deraadt Exp $ */
2 /* $NetBSD: util.c,v 1.5 1996/08/31 20:58:29 mycroft Exp $ */
3
4 /*
5 * Copyright © 2013
6 * Thorsten “mirabilos” Glaser <tg@mirbsd.org>
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This software was developed by the Computer Systems Engineering group
11 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
12 * contributed to Berkeley.
13 *
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Lawrence Berkeley Laboratories.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 *
43 * from: @(#)util.c 8.1 (Berkeley) 6/6/93
44 */
45
46 #include <sys/types.h>
47 #include <ctype.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <stdarg.h>
52 #include "config.h"
53
54 __RCSID("$MirOS: src/usr.sbin/config/util.c,v 1.5 2013/10/31 20:07:18 tg Exp $");
55
56 static void nomem(void) __dead;
57 #ifndef IN_MODLOAD
58 static void vxerror(const char *, int, const char *, va_list)
59 __attribute__((__format__(__printf__, 3, 0)));
60 #endif
61
62 /*
63 * Malloc, with abort on error.
64 */
65 void *
emalloc(size_t size)66 emalloc(size_t size)
67 {
68 void *p;
69
70 if ((p = malloc(size)) == NULL)
71 nomem();
72 memset(p, 0, size);
73 return (p);
74 }
75
76 #ifndef IN_MODLOAD
77 /*
78 * Realloc, with abort on error.
79 */
80 void *
erealloc(void * p,size_t size)81 erealloc(void *p, size_t size)
82 {
83
84 if ((p = realloc(p, size)) == NULL)
85 nomem();
86 return (p);
87 }
88 #endif
89
90 static void
nomem(void)91 nomem(void)
92 {
93 (void)fprintf(stderr, "config: out of memory\n");
94 exit(1);
95 }
96
97 #ifndef IN_MODLOAD
98 /*
99 * Prepend the source path to a file name.
100 */
101 char *
sourcepath(const char * file)102 sourcepath(const char *file)
103 {
104 char *cp;
105 int len = strlen(srcdir) + 1 + strlen(file) + 1;
106
107 cp = emalloc(len);
108 (void)snprintf(cp, len, "%s/%s", srcdir, file);
109 return (cp);
110 }
111
112 static struct nvlist *nvhead;
113
114 struct nvlist *
newnv(const char * name,const char * str,void * ptr,int i,struct nvlist * next)115 newnv(const char *name, const char *str, void *ptr, int i, struct nvlist *next)
116 {
117 struct nvlist *nv;
118
119 if ((nv = nvhead) == NULL)
120 nv = emalloc(sizeof(*nv));
121 else
122 nvhead = nv->nv_next;
123 nv->nv_next = next;
124 nv->nv_name = name;
125 if (ptr == NULL)
126 nv->nv_str = str;
127 else {
128 if (str != NULL)
129 panic("newnv");
130 nv->nv_ptr = ptr;
131 }
132 nv->nv_int = i;
133 return (nv);
134 }
135
136 /*
137 * Free an nvlist structure (just one).
138 */
139 void
nvfree(struct nvlist * nv)140 nvfree(struct nvlist *nv)
141 {
142
143 nv->nv_next = nvhead;
144 nvhead = nv;
145 }
146
147 /*
148 * Free an nvlist (the whole list).
149 */
150 void
nvfreel(struct nvlist * nv)151 nvfreel(struct nvlist *nv)
152 {
153 struct nvlist *next;
154
155 for (; nv != NULL; nv = next) {
156 next = nv->nv_next;
157 nv->nv_next = nvhead;
158 nvhead = nv;
159 }
160 }
161
162 /*
163 * External (config file) error. Complain, using current file
164 * and line number.
165 */
166 void
error(const char * fmt,...)167 error(const char *fmt, ...)
168 {
169 va_list ap;
170 extern const char *yyfile;
171
172 va_start(ap, fmt);
173 vxerror(yyfile, currentline(), fmt, ap);
174 va_end(ap);
175 }
176
177 /*
178 * Delayed config file error (i.e., something was wrong but we could not
179 * find out about it until later).
180 */
181 void
xerror(const char * file,int line,const char * fmt,...)182 xerror(const char *file, int line, const char *fmt, ...)
183 {
184 va_list ap;
185
186 va_start(ap, fmt);
187 vxerror(file, line, fmt, ap);
188 va_end(ap);
189 }
190
191 /*
192 * Internal form of error() and xerror().
193 */
194 static void
vxerror(const char * file,int line,const char * fmt,va_list ap)195 vxerror(const char *file, int line, const char *fmt, va_list ap)
196 {
197
198 (void)fprintf(stderr, "%s:%d: ", file, line);
199 (void)vfprintf(stderr, fmt, ap);
200 (void)putc('\n', stderr);
201 errors++;
202 }
203
204 /*
205 * Internal error, abort.
206 */
207 void
panic(const char * fmt,...)208 panic(const char *fmt, ...)
209 {
210 va_list ap;
211
212 va_start(ap, fmt);
213 (void)fprintf(stderr, "config: panic: ");
214 (void)vfprintf(stderr, fmt, ap);
215 (void)putc('\n', stderr);
216 va_end(ap);
217 exit(2);
218 }
219 #endif
220