]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/error.c
Changes to eliminate warnings from new Clang.
[thirdparty/cups.git] / filter / error.c
CommitLineData
f7deaa1a 1/*
f2d18633 2 * "$Id$"
f7deaa1a 3 *
7e86f2f6 4 * Raster error handling for CUPS.
f7deaa1a 5 *
d9564ec7 6 * Copyright 2007-2015 by Apple Inc.
7e86f2f6 7 * Copyright 2007 by Easy Software Products.
f7deaa1a 8 *
7e86f2f6
MS
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/".
f7deaa1a 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
f7deaa1a 16 */
17
18/*
19 * Include necessary headers...
20 */
21
a4845881 22#include <cups/raster-private.h>
f7deaa1a 23
24
25/*
26 * Local structures...
27 */
28
29typedef 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
41static _cups_raster_error_t *get_error_buffer(void);
42
43
44/*
45 * '_cupsRasterAddError()' - Add an error message to the error buffer.
46 */
47
48void
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 */
7e86f2f6 56 ssize_t bytes; /* Bytes in message string */
f7deaa1a 57
58
a5425f93
MS
59 DEBUG_printf(("_cupsRasterAddError(f=\"%s\", ...)", f));
60
f7deaa1a 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
a5425f93
MS
68 DEBUG_printf(("1_cupsRasterAddError: %s", s));
69
f7deaa1a 70 bytes ++;
71
7e86f2f6 72 if ((size_t)bytes >= sizeof(s))
f7deaa1a 73 return;
74
7e86f2f6 75 if (bytes > (ssize_t)(buf->end - buf->current))
f7deaa1a 76 {
77 /*
78 * Allocate more memory...
79 */
80
81 char *temp; /* New buffer */
82 size_t size; /* Size of buffer */
83
84
7e86f2f6 85 size = (size_t)(buf->end - buf->start + 2 * bytes + 1024);
f7deaa1a 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
7e86f2f6 108 memcpy(buf->current, s, (size_t)bytes);
f7deaa1a 109 buf->current += bytes - 1;
110}
111
112
113/*
114 * '_cupsRasterClearError()' - Clear the error buffer.
115 */
116
117void
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 *
f3c17241 136 * @since CUPS 1.3/OS X 10.5@
f7deaa1a 137 */
138
139const char * /* O - Last error */
140cupsRasterErrorString(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
7e86f2f6 165static pthread_key_t raster_key = 0; /* Thread local storage key */
f7deaa1a 166static pthread_once_t raster_key_once = PTHREAD_ONCE_INIT;
167 /* One-time initialization object */
168
169
170/*
171 * Local functions...
172 */
173
174static void raster_init(void);
175static 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 */
183get_error_buffer(void)
184{
185 _cups_raster_error_t *buf; /* Pointer to error buffer */
186
187
321d8d57 188 /*
f7deaa1a 189 * Initialize the global data exactly once...
190 */
191
d9564ec7 192 DEBUG_puts("3get_error_buffer()");
f7deaa1a 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 {
d9564ec7 203 DEBUG_puts("4get_error_buffer: allocating memory for thread.");
f7deaa1a 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
807315e6 212 DEBUG_printf(("4get_error_buffer: buf=%p", (void *)buf));
f7deaa1a 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
227static void
228raster_init(void)
229{
230 pthread_key_create(&raster_key, raster_destructor);
231
d9564ec7 232 DEBUG_printf(("3raster_init(): raster_key=%x(%u)", (unsigned)raster_key, (unsigned)raster_key));
f7deaa1a 233}
234
235
236/*
237 * 'raster_destructor()' - Free memory allocated by get_error_buffer().
238 */
239
240static void
241raster_destructor(void *value) /* I - Data to free */
242{
243 _cups_raster_error_t *buf = (_cups_raster_error_t *)value;
244 /* Error buffer */
245
246
d9564ec7 247 DEBUG_printf(("3raster_destructor(value=%p)", value));
f7deaa1a 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 */
266get_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/*
f2d18633 278 * End of "$Id$".
f7deaa1a 279 */