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