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