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