1 /* $OpenBSD: util.c,v 1.17 2016/09/07 18:36:52 akfaew 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. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)util.c 8.1 (Berkeley) 6/6/93 42 */ 43 44 #include <sys/types.h> 45 46 #include <ctype.h> 47 #include <err.h> 48 #include <stdarg.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 53 #include "config.h" 54 55 static void vxerror(const char *, int, const char *, va_list); 56 57 /* 58 * Malloc, with abort on error. 59 */ 60 void * 61 emalloc(size_t size) 62 { 63 void *p; 64 65 if ((p = calloc(1, size)) == NULL) 66 err(1, NULL); 67 return p; 68 } 69 70 /* 71 * Reallocarray, with abort on error. 72 */ 73 void * 74 ereallocarray(void *p, size_t sz1, size_t sz2) 75 { 76 77 if ((p = reallocarray(p, sz1, sz2)) == NULL) 78 err(1, NULL); 79 return p; 80 } 81 82 /* 83 * Calloc, with abort on error. 84 */ 85 void * 86 ecalloc(size_t sz1, size_t sz2) 87 { 88 void *p; 89 90 if ((p = calloc(sz1, sz2)) == NULL) 91 err(1, NULL); 92 return p; 93 } 94 95 /* 96 * Prepend the source path to a file name. 97 */ 98 char * 99 sourcepath(const char *file) 100 { 101 char *cp; 102 103 if (asprintf(&cp, "%s/%s", srcdir, file) == -1) 104 err(1, NULL); 105 106 return cp; 107 } 108 109 static struct nvlist *nvhead; 110 111 struct nvlist * 112 newnv(const char *name, const char *str, void *ptr, int i, struct nvlist *next) 113 { 114 struct nvlist *nv; 115 116 if ((nv = nvhead) == NULL) 117 nv = emalloc(sizeof(*nv)); 118 else 119 nvhead = nv->nv_next; 120 nv->nv_next = next; 121 nv->nv_name = (char *)name; 122 if (ptr == NULL) 123 nv->nv_str = str; 124 else { 125 if (str != NULL) 126 panic("newnv"); 127 nv->nv_ptr = ptr; 128 } 129 nv->nv_int = i; 130 return nv; 131 } 132 133 /* 134 * Free an nvlist structure (just one). 135 */ 136 void 137 nvfree(struct nvlist *nv) 138 { 139 140 nv->nv_next = nvhead; 141 nvhead = nv; 142 } 143 144 /* 145 * Free an nvlist (the whole list). 146 */ 147 void 148 nvfreel(struct nvlist *nv) 149 { 150 struct nvlist *next; 151 152 for (; nv != NULL; nv = next) { 153 next = nv->nv_next; 154 nv->nv_next = nvhead; 155 nvhead = nv; 156 } 157 } 158 159 /* 160 * External (config file) error. Complain, using current file 161 * and line number. 162 */ 163 void 164 error(const char *fmt, ...) 165 { 166 va_list ap; 167 extern const char *yyfile; 168 169 va_start(ap, fmt); 170 vxerror(yyfile, currentline(), fmt, ap); 171 va_end(ap); 172 } 173 174 /* 175 * Delayed config file error (i.e., something was wrong but we could not 176 * find out about it until later). 177 */ 178 void 179 xerror(const char *file, int line, const char *fmt, ...) 180 { 181 va_list ap; 182 183 va_start(ap, fmt); 184 vxerror(file, line, fmt, ap); 185 va_end(ap); 186 } 187 188 /* 189 * Internal form of error() and xerror(). 190 */ 191 static void 192 vxerror(const char *file, int line, const char *fmt, va_list ap) 193 { 194 195 (void)fprintf(stderr, "%s:%d: ", file, line); 196 (void)vfprintf(stderr, fmt, ap); 197 (void)putc('\n', stderr); 198 errors++; 199 } 200 201 /* 202 * Internal error, abort. 203 */ 204 __dead void 205 panic(const char *fmt, ...) 206 { 207 va_list ap; 208 209 va_start(ap, fmt); 210 (void)fprintf(stderr, "config: panic: "); 211 (void)vfprintf(stderr, fmt, ap); 212 (void)putc('\n', stderr); 213 va_end(ap); 214 exit(2); 215 } 216