xref: /illumos-gate/usr/src/cmd/sed/misc.c (revision 84441f85)
1*84441f85SGarrett D'Amore /*
2*84441f85SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3*84441f85SGarrett D'Amore  * Copyright (c) 1992 Diomidis Spinellis.
4*84441f85SGarrett D'Amore  * Copyright (c) 1992, 1993
5*84441f85SGarrett D'Amore  *	The Regents of the University of California.  All rights reserved.
6*84441f85SGarrett D'Amore  *
7*84441f85SGarrett D'Amore  * This code is derived from software contributed to Berkeley by
8*84441f85SGarrett D'Amore  * Diomidis Spinellis of Imperial College, University of London.
9*84441f85SGarrett D'Amore  *
10*84441f85SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
11*84441f85SGarrett D'Amore  * modification, are permitted provided that the following conditions
12*84441f85SGarrett D'Amore  * are met:
13*84441f85SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
14*84441f85SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
15*84441f85SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
16*84441f85SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
17*84441f85SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
18*84441f85SGarrett D'Amore  * 4. Neither the name of the University nor the names of its contributors
19*84441f85SGarrett D'Amore  *    may be used to endorse or promote products derived from this software
20*84441f85SGarrett D'Amore  *    without specific prior written permission.
21*84441f85SGarrett D'Amore  *
22*84441f85SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*84441f85SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*84441f85SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*84441f85SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*84441f85SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*84441f85SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*84441f85SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*84441f85SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*84441f85SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*84441f85SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*84441f85SGarrett D'Amore  * SUCH DAMAGE.
33*84441f85SGarrett D'Amore  */
34*84441f85SGarrett D'Amore 
35*84441f85SGarrett D'Amore #include <sys/types.h>
36*84441f85SGarrett D'Amore 
37*84441f85SGarrett D'Amore #include <err.h>
38*84441f85SGarrett D'Amore #include <limits.h>
39*84441f85SGarrett D'Amore #include <regex.h>
40*84441f85SGarrett D'Amore #include <stdio.h>
41*84441f85SGarrett D'Amore #include <stdlib.h>
42*84441f85SGarrett D'Amore #include <string.h>
43*84441f85SGarrett D'Amore #include <stdarg.h>
44*84441f85SGarrett D'Amore 
45*84441f85SGarrett D'Amore #include "defs.h"
46*84441f85SGarrett D'Amore #include "extern.h"
47*84441f85SGarrett D'Amore 
48*84441f85SGarrett D'Amore /*
49*84441f85SGarrett D'Amore  * Return a string for a regular expression error passed.  This is overkill,
50*84441f85SGarrett D'Amore  * because of the silly semantics of regerror (we can never know the size of
51*84441f85SGarrett D'Amore  * the buffer).
52*84441f85SGarrett D'Amore  */
53*84441f85SGarrett D'Amore char *
strregerror(int errcode,regex_t * preg)54*84441f85SGarrett D'Amore strregerror(int errcode, regex_t *preg)
55*84441f85SGarrett D'Amore {
56*84441f85SGarrett D'Amore 	static char *oe;
57*84441f85SGarrett D'Amore 	size_t s;
58*84441f85SGarrett D'Amore 
59*84441f85SGarrett D'Amore 	if (oe != NULL)
60*84441f85SGarrett D'Amore 		free(oe);
61*84441f85SGarrett D'Amore 	s = regerror(errcode, preg, NULL, 0);
62*84441f85SGarrett D'Amore 	if ((oe = malloc(s)) == NULL)
63*84441f85SGarrett D'Amore 		err(1, "malloc");
64*84441f85SGarrett D'Amore 	(void) regerror(errcode, preg, oe, s);
65*84441f85SGarrett D'Amore 	return (oe);
66*84441f85SGarrett D'Amore }
67*84441f85SGarrett D'Amore 
68*84441f85SGarrett D'Amore void
fatal(const char * fmt,...)69*84441f85SGarrett D'Amore fatal(const char *fmt, ...)
70*84441f85SGarrett D'Amore {
71*84441f85SGarrett D'Amore 	va_list ap;
72*84441f85SGarrett D'Amore 
73*84441f85SGarrett D'Amore 	(void) fprintf(stderr, "%s: %lu: ", fname, linenum);
74*84441f85SGarrett D'Amore 
75*84441f85SGarrett D'Amore 	va_start(ap, fmt);
76*84441f85SGarrett D'Amore 	(void) vfprintf(stderr, fmt, ap);
77*84441f85SGarrett D'Amore 	va_end(ap);
78*84441f85SGarrett D'Amore 
79*84441f85SGarrett D'Amore 	(void) fputc('\n', stderr);
80*84441f85SGarrett D'Amore 
81*84441f85SGarrett D'Amore 	exit(1);
82*84441f85SGarrett D'Amore }
83