1 /*- 2 * Copyright (c) 1992 Diomidis Spinellis. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Diomidis Spinellis of Imperial College, University of London. 8 * 9 * %sccs.include.redist.c% 10 */ 11 12 #ifndef lint 13 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 06/06/93"; 14 #endif /* not lint */ 15 16 #include <sys/types.h> 17 18 #include <errno.h> 19 #include <regex.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 24 #include "defs.h" 25 #include "extern.h" 26 27 /* 28 * malloc with result test 29 */ 30 void * 31 xmalloc(size) 32 u_int size; 33 { 34 void *p; 35 36 if ((p = malloc(size)) == NULL) 37 err(FATAL, "%s", strerror(errno)); 38 return (p); 39 } 40 41 /* 42 * realloc with result test 43 */ 44 void * 45 xrealloc(p, size) 46 void *p; 47 u_int size; 48 { 49 if (p == NULL) /* Compatibility hack. */ 50 return (xmalloc(size)); 51 52 if ((p = realloc(p, size)) == NULL) 53 err(FATAL, "%s", strerror(errno)); 54 return (p); 55 } 56 57 /* 58 * Return a string for a regular expression error passed. This is a overkill, 59 * because of the silly semantics of regerror (we can never know the size of 60 * the buffer). 61 */ 62 char * 63 strregerror(errcode, preg) 64 int errcode; 65 regex_t *preg; 66 { 67 static char *oe; 68 size_t s; 69 70 if (oe != NULL) 71 free(oe); 72 s = regerror(errcode, preg, "", 0); 73 oe = xmalloc(s); 74 (void)regerror(errcode, preg, oe, s); 75 return (oe); 76 } 77 78 #if __STDC__ 79 #include <stdarg.h> 80 #else 81 #include <varargs.h> 82 #endif 83 /* 84 * Error reporting function 85 */ 86 void 87 #if __STDC__ 88 err(int severity, const char *fmt, ...) 89 #else 90 err(severity, fmt, va_alist) 91 int severity; 92 char *fmt; 93 va_dcl 94 #endif 95 { 96 va_list ap; 97 #if __STDC__ 98 va_start(ap, fmt); 99 #else 100 va_start(ap); 101 #endif 102 (void)fprintf(stderr, "sed: "); 103 switch (severity) { 104 case WARNING: 105 case COMPILE: 106 (void)fprintf(stderr, "%lu: %s: ", linenum, fname); 107 } 108 (void)vfprintf(stderr, fmt, ap); 109 va_end(ap); 110 (void)fprintf(stderr, "\n"); 111 if (severity == WARNING) 112 return; 113 exit(1); 114 /* NOTREACHED */ 115 } 116