]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/error.c
39cd10391ab729320673c5104f58868260e563ce
[thirdparty/cups.git] / filter / error.c
1 /*
2 * "$Id$"
3 *
4 * Raster error handling for CUPS.
5 *
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 2007 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 */
17
18 /*
19 * Include necessary headers...
20 */
21
22 #include <cups/raster-private.h>
23
24
25 /*
26 * Local structures...
27 */
28
29 typedef struct _cups_raster_error_s /**** Error buffer structure ****/
30 {
31 char *start, /* Start of buffer */
32 *current, /* Current position in buffer */
33 *end; /* End of buffer */
34 } _cups_raster_error_t;
35
36
37 /*
38 * Local functions...
39 */
40
41 static _cups_raster_error_t *get_error_buffer(void);
42
43
44 /*
45 * '_cupsRasterAddError()' - Add an error message to the error buffer.
46 */
47
48 void
49 _cupsRasterAddError(const char *f, /* I - Printf-style error message */
50 ...) /* I - Additional arguments as needed */
51 {
52 _cups_raster_error_t *buf = get_error_buffer();
53 /* Error buffer */
54 va_list ap; /* Pointer to additional arguments */
55 char s[2048]; /* Message string */
56 ssize_t bytes; /* Bytes in message string */
57
58
59 DEBUG_printf(("_cupsRasterAddError(f=\"%s\", ...)", f));
60
61 va_start(ap, f);
62 bytes = vsnprintf(s, sizeof(s), f, ap);
63 va_end(ap);
64
65 if (bytes <= 0)
66 return;
67
68 DEBUG_printf(("1_cupsRasterAddError: %s", s));
69
70 bytes ++;
71
72 if ((size_t)bytes >= sizeof(s))
73 return;
74
75 if (bytes > (ssize_t)(buf->end - buf->current))
76 {
77 /*
78 * Allocate more memory...
79 */
80
81 char *temp; /* New buffer */
82 size_t size; /* Size of buffer */
83
84
85 size = (size_t)(buf->end - buf->start + 2 * bytes + 1024);
86
87 if (buf->start)
88 temp = realloc(buf->start, size);
89 else
90 temp = malloc(size);
91
92 if (!temp)
93 return;
94
95 /*
96 * Update pointers...
97 */
98
99 buf->end = temp + size;
100 buf->current = temp + (buf->current - buf->start);
101 buf->start = temp;
102 }
103
104 /*
105 * Append the message to the end of the current string...
106 */
107
108 memcpy(buf->current, s, (size_t)bytes);
109 buf->current += bytes - 1;
110 }
111
112
113 /*
114 * '_cupsRasterClearError()' - Clear the error buffer.
115 */
116
117 void
118 _cupsRasterClearError(void)
119 {
120 _cups_raster_error_t *buf = get_error_buffer();
121 /* Error buffer */
122
123
124 buf->current = buf->start;
125
126 if (buf->start)
127 *(buf->start) = '\0';
128 }
129
130
131 /*
132 * 'cupsRasterErrorString()' - Return the last error from a raster function.
133 *
134 * If there are no recent errors, NULL is returned.
135 *
136 * @since CUPS 1.3/OS X 10.5@
137 */
138
139 const char * /* O - Last error */
140 cupsRasterErrorString(void)
141 {
142 _cups_raster_error_t *buf = get_error_buffer();
143 /* Error buffer */
144
145
146 if (buf->current == buf->start)
147 return (NULL);
148 else
149 return (buf->start);
150 }
151
152
153 #ifdef HAVE_PTHREAD_H
154 /*
155 * Implement per-thread globals...
156 */
157
158 # include <pthread.h>
159
160
161 /*
162 * Local globals...
163 */
164
165 static pthread_key_t raster_key = 0; /* Thread local storage key */
166 static pthread_once_t raster_key_once = PTHREAD_ONCE_INIT;
167 /* One-time initialization object */
168
169
170 /*
171 * Local functions...
172 */
173
174 static void raster_init(void);
175 static void raster_destructor(void *value);
176
177
178 /*
179 * 'get_error_buffer()' - Return a pointer to thread local storage.
180 */
181
182 _cups_raster_error_t * /* O - Pointer to error buffer */
183 get_error_buffer(void)
184 {
185 _cups_raster_error_t *buf; /* Pointer to error buffer */
186
187
188 /*
189 * Initialize the global data exactly once...
190 */
191
192 DEBUG_puts("3get_error_buffer()");
193
194 pthread_once(&raster_key_once, raster_init);
195
196 /*
197 * See if we have allocated the data yet...
198 */
199
200 if ((buf = (_cups_raster_error_t *)pthread_getspecific(raster_key))
201 == NULL)
202 {
203 DEBUG_puts("4get_error_buffer: allocating memory for thread.");
204
205 /*
206 * No, allocate memory as set the pointer for the key...
207 */
208
209 buf = calloc(1, sizeof(_cups_raster_error_t));
210 pthread_setspecific(raster_key, buf);
211
212 DEBUG_printf(("4get_error_buffer: buf=%p", buf));
213 }
214
215 /*
216 * Return the pointer to the data...
217 */
218
219 return (buf);
220 }
221
222
223 /*
224 * 'raster_init()' - Initialize error buffer once.
225 */
226
227 static void
228 raster_init(void)
229 {
230 pthread_key_create(&raster_key, raster_destructor);
231
232 DEBUG_printf(("3raster_init(): raster_key=%x(%u)", (unsigned)raster_key, (unsigned)raster_key));
233 }
234
235
236 /*
237 * 'raster_destructor()' - Free memory allocated by get_error_buffer().
238 */
239
240 static void
241 raster_destructor(void *value) /* I - Data to free */
242 {
243 _cups_raster_error_t *buf = (_cups_raster_error_t *)value;
244 /* Error buffer */
245
246
247 DEBUG_printf(("3raster_destructor(value=%p)", value));
248
249 if (buf->start)
250 free(buf->start);
251
252 free(value);
253 }
254
255
256 #else
257 /*
258 * Implement static globals...
259 */
260
261 /*
262 * 'get_error_buffer()' - Return a pointer to thread local storage.
263 */
264
265 _cups_raster_error_t * /* O - Pointer to error buffer */
266 get_error_buffer(void)
267 {
268 static _cups_raster_error_t buf = { 0, 0, 0 };
269 /* Error buffer */
270
271
272 return (&buf);
273 }
274 #endif /* HAVE_PTHREAD_H */
275
276
277 /*
278 * End of "$Id$".
279 */