Home | History | Annotate | Line # | Download | only in sed
misc.c revision 1.4
      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  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #ifndef lint
     39 /* from: static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93"; */
     40 static char *rcsid = "$Id: misc.c,v 1.4 1994/02/03 23:44:54 cgd Exp $";
     41 #endif /* not lint */
     42 
     43 #include <sys/types.h>
     44 
     45 #include <errno.h>
     46 #include <regex.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 
     51 #include "defs.h"
     52 #include "extern.h"
     53 
     54 /*
     55  * malloc with result test
     56  */
     57 void *
     58 xmalloc(size)
     59 	u_int size;
     60 {
     61 	void *p;
     62 
     63 	if ((p = malloc(size)) == NULL)
     64 		err(FATAL, "%s", strerror(errno));
     65 	return (p);
     66 }
     67 
     68 /*
     69  * realloc with result test
     70  */
     71 void *
     72 xrealloc(p, size)
     73 	void *p;
     74 	u_int size;
     75 {
     76 	if (p == NULL)			/* Compatibility hack. */
     77 		return (xmalloc(size));
     78 
     79 	if ((p = realloc(p, size)) == NULL)
     80 		err(FATAL, "%s", strerror(errno));
     81 	return (p);
     82 }
     83 
     84 /*
     85  * Return a string for a regular expression error passed.  This is a overkill,
     86  * because of the silly semantics of regerror (we can never know the size of
     87  * the buffer).
     88  */
     89 char *
     90 strregerror(errcode, preg)
     91 	int errcode;
     92 	regex_t *preg;
     93 {
     94 	static char *oe;
     95 	size_t s;
     96 
     97 	if (oe != NULL)
     98 		free(oe);
     99 	s = regerror(errcode, preg, "", 0);
    100 	oe = xmalloc(s);
    101 	(void)regerror(errcode, preg, oe, s);
    102 	return (oe);
    103 }
    104 
    105 #if __STDC__
    106 #include <stdarg.h>
    107 #else
    108 #include <varargs.h>
    109 #endif
    110 /*
    111  * Error reporting function
    112  */
    113 void
    114 #if __STDC__
    115 err(int severity, const char *fmt, ...)
    116 #else
    117 err(severity, fmt, va_alist)
    118 	int severity;
    119 	char *fmt;
    120         va_dcl
    121 #endif
    122 {
    123 	va_list ap;
    124 #if __STDC__
    125 	va_start(ap, fmt);
    126 #else
    127 	va_start(ap);
    128 #endif
    129 	(void)fprintf(stderr, "sed: ");
    130 	switch (severity) {
    131 	case WARNING:
    132 	case COMPILE:
    133 		(void)fprintf(stderr, "%lu: %s: ", linenum, fname);
    134 	}
    135 	(void)vfprintf(stderr, fmt, ap);
    136 	va_end(ap);
    137 	(void)fprintf(stderr, "\n");
    138 	if (severity == WARNING)
    139 		return;
    140 	exit(1);
    141 	/* NOTREACHED */
    142 }
    143