xref: /csrg-svn/usr.bin/sed/misc.c (revision 62229)
155996Sbostic /*-
255996Sbostic  * Copyright (c) 1992 Diomidis Spinellis.
3*62229Sbostic  * Copyright (c) 1992, 1993
4*62229Sbostic  *	The Regents of the University of California.  All rights reserved.
555996Sbostic  *
655996Sbostic  * This code is derived from software contributed to Berkeley by
755996Sbostic  * Diomidis Spinellis of Imperial College, University of London.
855996Sbostic  *
955996Sbostic  * %sccs.include.redist.c%
1055996Sbostic  */
1155996Sbostic 
1255996Sbostic #ifndef lint
13*62229Sbostic static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 06/06/93";
1455996Sbostic #endif /* not lint */
1555996Sbostic 
1655996Sbostic #include <sys/types.h>
1755996Sbostic 
1855996Sbostic #include <errno.h>
1955996Sbostic #include <regex.h>
2055996Sbostic #include <stdio.h>
2155996Sbostic #include <stdlib.h>
2255996Sbostic #include <string.h>
2355996Sbostic 
2455996Sbostic #include "defs.h"
2555996Sbostic #include "extern.h"
2655996Sbostic 
2755996Sbostic /*
2855996Sbostic  * malloc with result test
2955996Sbostic  */
3055996Sbostic void *
xmalloc(size)3155996Sbostic xmalloc(size)
3255996Sbostic 	u_int size;
3355996Sbostic {
3455996Sbostic 	void *p;
3555996Sbostic 
3655996Sbostic 	if ((p = malloc(size)) == NULL)
3755996Sbostic 		err(FATAL, "%s", strerror(errno));
3855996Sbostic 	return (p);
3955996Sbostic }
4055996Sbostic 
4155996Sbostic /*
4255996Sbostic  * realloc with result test
4355996Sbostic  */
4455996Sbostic void *
xrealloc(p,size)4555996Sbostic xrealloc(p, size)
4655996Sbostic 	void *p;
4755996Sbostic 	u_int size;
4855996Sbostic {
4955996Sbostic 	if (p == NULL)			/* Compatibility hack. */
5055996Sbostic 		return (xmalloc(size));
5155996Sbostic 
5255996Sbostic 	if ((p = realloc(p, size)) == NULL)
5355996Sbostic 		err(FATAL, "%s", strerror(errno));
5455996Sbostic 	return (p);
5555996Sbostic }
5655996Sbostic 
5755996Sbostic /*
5855996Sbostic  * Return a string for a regular expression error passed.  This is a overkill,
5955996Sbostic  * because of the silly semantics of regerror (we can never know the size of
6055996Sbostic  * the buffer).
6155996Sbostic  */
6255996Sbostic char *
strregerror(errcode,preg)6355996Sbostic strregerror(errcode, preg)
6455996Sbostic 	int errcode;
6555996Sbostic 	regex_t *preg;
6655996Sbostic {
6755996Sbostic 	static char *oe;
6855996Sbostic 	size_t s;
6955996Sbostic 
7055996Sbostic 	if (oe != NULL)
7155996Sbostic 		free(oe);
7255996Sbostic 	s = regerror(errcode, preg, "", 0);
7355996Sbostic 	oe = xmalloc(s);
7455996Sbostic 	(void)regerror(errcode, preg, oe, s);
7555996Sbostic 	return (oe);
7655996Sbostic }
7755996Sbostic 
7855996Sbostic #if __STDC__
7955996Sbostic #include <stdarg.h>
8055996Sbostic #else
8155996Sbostic #include <varargs.h>
8255996Sbostic #endif
8355996Sbostic /*
8455996Sbostic  * Error reporting function
8555996Sbostic  */
8655996Sbostic void
8755996Sbostic #if __STDC__
err(int severity,const char * fmt,...)8855996Sbostic err(int severity, const char *fmt, ...)
8955996Sbostic #else
9055996Sbostic err(severity, fmt, va_alist)
9155996Sbostic 	int severity;
9255996Sbostic 	char *fmt;
9355996Sbostic         va_dcl
9455996Sbostic #endif
9555996Sbostic {
9655996Sbostic 	va_list ap;
9755996Sbostic #if __STDC__
9855996Sbostic 	va_start(ap, fmt);
9955996Sbostic #else
10055996Sbostic 	va_start(ap);
10155996Sbostic #endif
10255996Sbostic 	(void)fprintf(stderr, "sed: ");
10355996Sbostic 	switch (severity) {
10455996Sbostic 	case WARNING:
10555996Sbostic 	case COMPILE:
10656045Sbostic 		(void)fprintf(stderr, "%lu: %s: ", linenum, fname);
10755996Sbostic 	}
10855996Sbostic 	(void)vfprintf(stderr, fmt, ap);
10955996Sbostic 	va_end(ap);
11055996Sbostic 	(void)fprintf(stderr, "\n");
11156014Sbostic 	if (severity == WARNING)
11256014Sbostic 		return;
11356014Sbostic 	exit(1);
11455996Sbostic 	/* NOTREACHED */
11555996Sbostic }
116