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