Commit | Line | Data |
---|---|---|
4a67a271 KB |
1 | /*- |
2 | * Copyright (c) 1992 Diomidis Spinellis. | |
3 | * Copyright (c) 1992 The Regents of the University of California. | |
4 | * All rights reserved. | |
5 | * | |
6 | * This code is derived from software contributed to Berkeley by | |
7 | * Diomidis Spinellis of Imperial College, University of London. | |
8 | * | |
9 | * %sccs.include.redist.c% | |
10 | */ | |
11 | ||
12 | #ifndef lint | |
7341d2d0 | 13 | static char sccsid[] = "@(#)misc.c 5.2 (Berkeley) %G%"; |
4a67a271 KB |
14 | #endif /* not lint */ |
15 | ||
16 | #include <sys/types.h> | |
17 | ||
18 | #include <errno.h> | |
19 | #include <regex.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | ||
24 | #include "defs.h" | |
25 | #include "extern.h" | |
26 | ||
27 | /* | |
28 | * malloc with result test | |
29 | */ | |
30 | void * | |
31 | xmalloc(size) | |
32 | u_int size; | |
33 | { | |
34 | void *p; | |
35 | ||
36 | if ((p = malloc(size)) == NULL) | |
37 | err(FATAL, "%s", strerror(errno)); | |
38 | return (p); | |
39 | } | |
40 | ||
41 | /* | |
42 | * realloc with result test | |
43 | */ | |
44 | void * | |
45 | xrealloc(p, size) | |
46 | void *p; | |
47 | u_int size; | |
48 | { | |
49 | if (p == NULL) /* Compatibility hack. */ | |
50 | return (xmalloc(size)); | |
51 | ||
52 | if ((p = realloc(p, size)) == NULL) | |
53 | err(FATAL, "%s", strerror(errno)); | |
54 | return (p); | |
55 | } | |
56 | ||
57 | /* | |
58 | * Return a string for a regular expression error passed. This is a overkill, | |
59 | * because of the silly semantics of regerror (we can never know the size of | |
60 | * the buffer). | |
61 | */ | |
62 | char * | |
63 | strregerror(errcode, preg) | |
64 | int errcode; | |
65 | regex_t *preg; | |
66 | { | |
67 | static char *oe; | |
68 | size_t s; | |
69 | ||
70 | if (oe != NULL) | |
71 | free(oe); | |
72 | s = regerror(errcode, preg, "", 0); | |
73 | oe = xmalloc(s); | |
74 | (void)regerror(errcode, preg, oe, s); | |
75 | return (oe); | |
76 | } | |
77 | ||
78 | #if __STDC__ | |
79 | #include <stdarg.h> | |
80 | #else | |
81 | #include <varargs.h> | |
82 | #endif | |
83 | /* | |
84 | * Error reporting function | |
85 | */ | |
86 | void | |
87 | #if __STDC__ | |
88 | err(int severity, const char *fmt, ...) | |
89 | #else | |
90 | err(severity, fmt, va_alist) | |
91 | int severity; | |
92 | char *fmt; | |
93 | va_dcl | |
94 | #endif | |
95 | { | |
96 | va_list ap; | |
97 | #if __STDC__ | |
98 | va_start(ap, fmt); | |
99 | #else | |
100 | va_start(ap); | |
101 | #endif | |
102 | (void)fprintf(stderr, "sed: "); | |
103 | switch (severity) { | |
104 | case WARNING: | |
105 | case COMPILE: | |
106 | (void)fprintf(stderr, "%s(%lu): ", fname, linenum); | |
107 | } | |
108 | (void)vfprintf(stderr, fmt, ap); | |
109 | va_end(ap); | |
110 | (void)fprintf(stderr, "\n"); | |
111 | switch (severity) { | |
112 | case COMPILE: | |
113 | compile_errors++; | |
114 | #define MAX_COMPILE_ERRS 20 | |
115 | if (compile_errors > MAX_COMPILE_ERRS) | |
116 | err(FATAL, "too many compilation errors; exiting"); | |
117 | case FATAL: | |
118 | exit(1); | |
119 | } | |
120 | /* NOTREACHED */ | |
121 | } |