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