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