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