]>
Commit | Line | Data |
---|---|---|
224c7076 A |
1 | --- err.c.orig 2006-12-15 11:18:17.000000000 -0800 |
2 | +++ err.c 2006-12-15 11:46:52.000000000 -0800 | |
3 | @@ -44,12 +44,85 @@ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <string.h> | |
7 | +#include <vis.h> | |
8 | #include "un-namespace.h" | |
3d9156a7 A |
9 | |
10 | #include "libc_private.h" | |
11 | ||
12 | -static FILE *err_file; /* file to use for error output */ | |
13 | -static void (*err_exit)(int); | |
14 | +#ifdef BUILDING_VARIANT | |
15 | + | |
16 | +__private_extern__ FILE *_e_err_file; /* file to use for error output */ | |
17 | +__private_extern__ void (*_e_err_exit)(int); | |
224c7076 | 18 | +__private_extern__ void _e_visprintf(FILE * __restrict, const char * __restrict, va_list); |
3d9156a7 A |
19 | + |
20 | +#else /* !BUILDING_VARIANT */ | |
21 | + | |
22 | +__private_extern__ FILE *_e_err_file = NULL; /* file to use for error output */ | |
23 | +__private_extern__ void (*_e_err_exit)(int) = NULL; | |
224c7076 A |
24 | + |
25 | +/* | |
26 | + * zero means pass as is | |
27 | + * 255 means use \nnn (octal) | |
28 | + * otherwise use \x (x is value) | |
29 | + * (NUL isn't used) | |
30 | + */ | |
31 | +static unsigned char escape[256] = { | |
32 | + /* NUL SOH STX ETX EOT ENQ ACK BEL */ | |
33 | + 0 , 255, 255, 255, 255, 255, 255, 'a', | |
34 | + /* BS HT NL VT NP CR SO SI */ | |
35 | + 'b', 't', 'n', 'v', 'f', 'r', 255, 255, | |
36 | + /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */ | |
37 | + 255, 255, 255, 255, 255, 255, 255, 255, | |
38 | + /* CAN EM SUB ESC FS GS RS US */ | |
39 | + 255, 255, 255, 255, 255, 255, 255, 255, | |
40 | + /* the rest are zero */ | |
41 | +}; | |
42 | + | |
43 | +/* | |
44 | + * Make characters visible. If we can't allocate enough | |
45 | + * memory, we fall back on vfprintf(). | |
46 | + */ | |
47 | +__private_extern__ void | |
48 | +_e_visprintf(FILE * __restrict stream, const char * __restrict format, va_list ap) | |
49 | +{ | |
50 | + int failed = 0; | |
51 | + char *str, *visstr; | |
52 | + va_list backup; | |
53 | + | |
54 | + va_copy(backup, ap); | |
55 | + vasprintf(&str, format, ap); | |
56 | + if (str != NULL) { | |
57 | + if ((visstr = malloc(4 * strlen(str) + 1)) != NULL) { | |
58 | + unsigned char *fp = (unsigned char *)str; | |
59 | + unsigned char *tp = (unsigned char *)visstr; | |
60 | + while(*fp) { | |
61 | + switch(escape[*fp]) { | |
62 | + case 0: | |
63 | + *tp++ = *fp; | |
64 | + break; | |
65 | + case 255: | |
66 | + sprintf(tp, "\\%03o", *fp); | |
67 | + tp += 4; | |
68 | + break; | |
69 | + default: | |
70 | + *tp++ = '\\'; | |
71 | + *tp++ = escape[*fp]; | |
72 | + break; | |
73 | + } | |
74 | + fp++; | |
75 | + } | |
76 | + *tp = 0; | |
77 | + fputs(visstr, stream); | |
78 | + free(visstr); | |
79 | + } else | |
80 | + failed = 1; | |
81 | + free(str); | |
82 | + } else | |
83 | + failed = 1; | |
84 | + if (failed) | |
85 | + vfprintf(stream, format, backup); | |
86 | + va_end(backup); | |
87 | +} | |
3d9156a7 A |
88 | |
89 | /* | |
90 | * This is declared to take a `void *' so that the caller is not required | |
224c7076 | 91 | @@ -60,16 +133,17 @@ |
3d9156a7 A |
92 | err_set_file(void *fp) |
93 | { | |
94 | if (fp) | |
95 | - err_file = fp; | |
96 | + _e_err_file = fp; | |
97 | else | |
98 | - err_file = stderr; | |
99 | + _e_err_file = stderr; | |
100 | } | |
101 | ||
102 | void | |
103 | err_set_exit(void (*ef)(int)) | |
104 | { | |
105 | - err_exit = ef; | |
106 | + _e_err_exit = ef; | |
107 | } | |
108 | +#endif /* !BUILDING_VARIANT */ | |
109 | ||
110 | __weak_reference(_err, err); | |
111 | ||
224c7076 | 112 | @@ -107,16 +181,16 @@ |
3d9156a7 A |
113 | const char *fmt; |
114 | va_list ap; | |
115 | { | |
116 | - if (err_file == 0) | |
117 | + if (_e_err_file == 0) | |
118 | err_set_file((FILE *)0); | |
119 | - fprintf(err_file, "%s: ", _getprogname()); | |
120 | + fprintf(_e_err_file, "%s: ", _getprogname()); | |
121 | if (fmt != NULL) { | |
122 | - vfprintf(err_file, fmt, ap); | |
123 | - fprintf(err_file, ": "); | |
224c7076 | 124 | + _e_visprintf(_e_err_file, fmt, ap); |
3d9156a7 A |
125 | + fprintf(_e_err_file, ": "); |
126 | } | |
127 | - fprintf(err_file, "%s\n", strerror(code)); | |
128 | - if (err_exit) | |
129 | - err_exit(eval); | |
130 | + fprintf(_e_err_file, "%s\n", strerror(code)); | |
131 | + if (_e_err_exit) | |
132 | + _e_err_exit(eval); | |
133 | exit(eval); | |
134 | } | |
135 | ||
224c7076 | 136 | @@ -135,14 +209,14 @@ |
3d9156a7 A |
137 | const char *fmt; |
138 | va_list ap; | |
139 | { | |
140 | - if (err_file == 0) | |
141 | + if (_e_err_file == 0) | |
142 | err_set_file((FILE *)0); | |
143 | - fprintf(err_file, "%s: ", _getprogname()); | |
144 | + fprintf(_e_err_file, "%s: ", _getprogname()); | |
145 | if (fmt != NULL) | |
146 | - vfprintf(err_file, fmt, ap); | |
147 | - fprintf(err_file, "\n"); | |
148 | - if (err_exit) | |
149 | - err_exit(eval); | |
224c7076 | 150 | + _e_visprintf(_e_err_file, fmt, ap); |
3d9156a7 A |
151 | + fprintf(_e_err_file, "\n"); |
152 | + if (_e_err_exit) | |
153 | + _e_err_exit(eval); | |
154 | exit(eval); | |
155 | } | |
156 | ||
224c7076 | 157 | @@ -180,14 +254,14 @@ |
3d9156a7 A |
158 | const char *fmt; |
159 | va_list ap; | |
160 | { | |
161 | - if (err_file == 0) | |
162 | + if (_e_err_file == 0) | |
163 | err_set_file((FILE *)0); | |
164 | - fprintf(err_file, "%s: ", _getprogname()); | |
165 | + fprintf(_e_err_file, "%s: ", _getprogname()); | |
166 | if (fmt != NULL) { | |
167 | - vfprintf(err_file, fmt, ap); | |
168 | - fprintf(err_file, ": "); | |
224c7076 | 169 | + _e_visprintf(_e_err_file, fmt, ap); |
3d9156a7 A |
170 | + fprintf(_e_err_file, ": "); |
171 | } | |
172 | - fprintf(err_file, "%s\n", strerror(code)); | |
173 | + fprintf(_e_err_file, "%s\n", strerror(code)); | |
174 | } | |
175 | ||
176 | void | |
224c7076 | 177 | @@ -204,10 +278,10 @@ |
3d9156a7 A |
178 | const char *fmt; |
179 | va_list ap; | |
180 | { | |
181 | - if (err_file == 0) | |
182 | + if (_e_err_file == 0) | |
183 | err_set_file((FILE *)0); | |
184 | - fprintf(err_file, "%s: ", _getprogname()); | |
185 | + fprintf(_e_err_file, "%s: ", _getprogname()); | |
186 | if (fmt != NULL) | |
187 | - vfprintf(err_file, fmt, ap); | |
188 | - fprintf(err_file, "\n"); | |
224c7076 | 189 | + _e_visprintf(_e_err_file, fmt, ap); |
3d9156a7 A |
190 | + fprintf(_e_err_file, "\n"); |
191 | } |