1 /*
2 * Raster error handling for CUPS.
3 *
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 2007 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11 /*
12 * Include necessary headers...
13 */
14
15 #include "cups-private.h"
16 #include "raster-private.h"
17 #include "debug-internal.h"
18
19
20 /*
21 * '_cupsRasterAddError()' - Add an error message to the error buffer.
22 */
23
24 void
_cupsRasterAddError(const char * f,...)25 _cupsRasterAddError(const char *f, /* I - Printf-style error message */
26 ...) /* I - Additional arguments as needed */
27 {
28 _cups_globals_t *cg = _cupsGlobals();
29 /* Thread globals */
30 _cups_raster_error_t *buf = &cg->raster_error;
31 /* Error buffer */
32 va_list ap; /* Pointer to additional arguments */
33 char s[2048]; /* Message string */
34 ssize_t bytes; /* Bytes in message string */
35
36
37 DEBUG_printf(("_cupsRasterAddError(f=\"%s\", ...)", f));
38
39 va_start(ap, f);
40 bytes = vsnprintf(s, sizeof(s), f, ap);
41 va_end(ap);
42
43 if (bytes <= 0)
44 return;
45
46 DEBUG_printf(("1_cupsRasterAddError: %s", s));
47
48 bytes ++;
49
50 if ((size_t)bytes >= sizeof(s))
51 return;
52
53 if (bytes > (ssize_t)(buf->end - buf->current))
54 {
55 /*
56 * Allocate more memory...
57 */
58
59 char *temp; /* New buffer */
60 size_t size; /* Size of buffer */
61
62
63 size = (size_t)(buf->end - buf->start + 2 * bytes + 1024);
64
65 if (buf->start)
66 temp = realloc(buf->start, size);
67 else
68 temp = malloc(size);
69
70 if (!temp)
71 return;
72
73 /*
74 * Update pointers...
75 */
76
77 buf->end = temp + size;
78 buf->current = temp + (buf->current - buf->start);
79 buf->start = temp;
80 }
81
82 /*
83 * Append the message to the end of the current string...
84 */
85
86 memcpy(buf->current, s, (size_t)bytes);
87 buf->current += bytes - 1;
88 }
89
90
91 /*
92 * '_cupsRasterClearError()' - Clear the error buffer.
93 */
94
95 void
_cupsRasterClearError(void)96 _cupsRasterClearError(void)
97 {
98 _cups_globals_t *cg = _cupsGlobals();
99 /* Thread globals */
100 _cups_raster_error_t *buf = &cg->raster_error;
101 /* Error buffer */
102
103
104 buf->current = buf->start;
105
106 if (buf->start)
107 *(buf->start) = '\0';
108 }
109
110
111 /*
112 * '_cupsRasterErrorString()' - Return the last error from a raster function.
113 *
114 * If there are no recent errors, NULL is returned.
115 *
116 * @since CUPS 1.3/macOS 10.5@
117 */
118
119 const char * /* O - Last error */
_cupsRasterErrorString(void)120 _cupsRasterErrorString(void)
121 {
122 _cups_globals_t *cg = _cupsGlobals();
123 /* Thread globals */
124 _cups_raster_error_t *buf = &cg->raster_error;
125 /* Error buffer */
126
127
128 if (buf->current == buf->start)
129 return (NULL);
130 else
131 return (buf->start);
132 }
133