]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/statbuf.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / scheduler / statbuf.c
1 /*
2 * "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $"
3 *
4 * Status buffer routines for the Common UNIX Printing System (CUPS)
5 * scheduler.
6 *
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 *
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
15 *
16 * Contents:
17 *
18 * cupsdStatBufDelete() - Destroy a status buffer.
19 * cupsdStatBufNew() - Create a new status buffer.
20 * cupsdStatBufUpdate() - Update the status buffer.
21 */
22
23 /*
24 * Include necessary headers...
25 */
26
27 #include "cupsd.h"
28 #include <stdarg.h>
29
30
31 /*
32 * 'cupsdStatBufDelete()' - Destroy a status buffer.
33 */
34
35 void
36 cupsdStatBufDelete(cupsd_statbuf_t *sb) /* I - Status buffer */
37 {
38 /*
39 * Range check input...
40 */
41
42 if (!sb)
43 return;
44
45 /*
46 * Close the status pipe and free memory used...
47 */
48
49 close(sb->fd);
50
51 free(sb);
52 }
53
54
55 /*
56 * 'cupsdStatBufNew()' - Create a new status buffer.
57 */
58
59 cupsd_statbuf_t * /* O - New status buffer */
60 cupsdStatBufNew(int fd, /* I - File descriptor of pipe */
61 const char *prefix, /* I - Printf-style prefix string */
62 ...) /* I - Additional args as needed */
63 {
64 cupsd_statbuf_t *sb; /* New status buffer */
65 va_list ap; /* Argument list */
66
67
68 /*
69 * Range check input...
70 */
71
72 if (fd < 0)
73 return (NULL);
74
75 /*
76 * Allocate the status buffer...
77 */
78
79 if ((sb = calloc(1, sizeof(cupsd_statbuf_t))) != NULL)
80 {
81 /*
82 * Assign the file descriptor...
83 */
84
85 sb->fd = fd;
86
87 /*
88 * Format the prefix string, if any. This is usually "[Job 123]"
89 * or "[Sub 123]", and so forth.
90 */
91
92 if (prefix)
93 {
94 /*
95 * Printf-style prefix string...
96 */
97
98 va_start(ap, prefix);
99 vsnprintf(sb->prefix, sizeof(sb->prefix), prefix, ap);
100 va_end(ap);
101 }
102 else
103 {
104 /*
105 * No prefix string...
106 */
107
108 sb->prefix[0] = '\0';
109 }
110 }
111
112 return (sb);
113 }
114
115
116 /*
117 * 'cupsdStatBufUpdate()' - Update the status buffer.
118 */
119
120 char * /* O - Line from buffer, "", or NULL */
121 cupsdStatBufUpdate(cupsd_statbuf_t *sb, /* I - Status buffer */
122 int *loglevel,
123 /* O - Log level */
124 char *line,
125 /* I - Line buffer */
126 int linelen)
127 /* I - Size of line buffer */
128 {
129 int bytes; /* Number of bytes read */
130 char *lineptr, /* Pointer to end of line in buffer */
131 *message; /* Pointer to message text */
132
133
134 /*
135 * Check if the buffer already contains a full line...
136 */
137
138 if ((lineptr = strchr(sb->buffer, '\n')) == NULL)
139 {
140 /*
141 * No, read more data...
142 */
143
144 if ((bytes = read(sb->fd, sb->buffer + sb->bufused,
145 CUPSD_SB_BUFFER_SIZE - sb->bufused - 1)) > 0)
146 {
147 sb->bufused += bytes;
148 sb->buffer[sb->bufused] = '\0';
149
150 /*
151 * Guard against a line longer than the max buffer size...
152 */
153
154 if ((lineptr = strchr(sb->buffer, '\n')) == NULL &&
155 sb->bufused == (CUPSD_SB_BUFFER_SIZE - 1))
156 lineptr = sb->buffer + sb->bufused;
157 }
158 else if (bytes < 0 && errno == EINTR)
159 {
160 /*
161 * Return an empty line if we are interrupted...
162 */
163
164 *loglevel = CUPSD_LOG_NONE;
165 line[0] = '\0';
166
167 return (line);
168 }
169 else
170 {
171 /*
172 * End-of-file, so use the whole buffer...
173 */
174
175 lineptr = sb->buffer + sb->bufused;
176 *lineptr = '\0';
177 }
178
179 /*
180 * Final check for end-of-file...
181 */
182
183 if (sb->bufused == 0 && bytes == 0)
184 lineptr = NULL;
185 }
186
187 if (!lineptr)
188 {
189 /*
190 * End of file...
191 */
192
193 *loglevel = CUPSD_LOG_NONE;
194 line[0] = '\0';
195
196 return (NULL);
197 }
198
199 /*
200 * Terminate the line and process it...
201 */
202
203 *lineptr++ = '\0';
204
205 /*
206 * Figure out the logging level...
207 */
208
209 if (!strncmp(sb->buffer, "EMERG:", 6))
210 {
211 *loglevel = CUPSD_LOG_EMERG;
212 message = sb->buffer + 6;
213 }
214 else if (!strncmp(sb->buffer, "ALERT:", 6))
215 {
216 *loglevel = CUPSD_LOG_ALERT;
217 message = sb->buffer + 6;
218 }
219 else if (!strncmp(sb->buffer, "CRIT:", 5))
220 {
221 *loglevel = CUPSD_LOG_CRIT;
222 message = sb->buffer + 5;
223 }
224 else if (!strncmp(sb->buffer, "ERROR:", 6))
225 {
226 *loglevel = CUPSD_LOG_ERROR;
227 message = sb->buffer + 6;
228 }
229 else if (!strncmp(sb->buffer, "WARNING:", 8))
230 {
231 *loglevel = CUPSD_LOG_WARN;
232 message = sb->buffer + 8;
233 }
234 else if (!strncmp(sb->buffer, "NOTICE:", 7))
235 {
236 *loglevel = CUPSD_LOG_NOTICE;
237 message = sb->buffer + 7;
238 }
239 else if (!strncmp(sb->buffer, "INFO:", 5))
240 {
241 *loglevel = CUPSD_LOG_INFO;
242 message = sb->buffer + 5;
243 }
244 else if (!strncmp(sb->buffer, "DEBUG:", 6))
245 {
246 *loglevel = CUPSD_LOG_DEBUG;
247 message = sb->buffer + 6;
248 }
249 else if (!strncmp(sb->buffer, "DEBUG2:", 7))
250 {
251 *loglevel = CUPSD_LOG_DEBUG2;
252 message = sb->buffer + 7;
253 }
254 else if (!strncmp(sb->buffer, "PAGE:", 5))
255 {
256 *loglevel = CUPSD_LOG_PAGE;
257 message = sb->buffer + 5;
258 }
259 else if (!strncmp(sb->buffer, "STATE:", 6))
260 {
261 *loglevel = CUPSD_LOG_STATE;
262 message = sb->buffer + 6;
263 }
264 else if (!strncmp(sb->buffer, "ATTR:", 5))
265 {
266 *loglevel = CUPSD_LOG_ATTR;
267 message = sb->buffer + 5;
268 }
269 else if (!strncmp(sb->buffer, "PPD:", 4))
270 {
271 *loglevel = CUPSD_LOG_PPD;
272 message = sb->buffer + 4;
273 }
274 else
275 {
276 *loglevel = CUPSD_LOG_DEBUG;
277 message = sb->buffer;
278 }
279
280 /*
281 * Skip leading whitespace in the message...
282 */
283
284 while (isspace(*message & 255))
285 message ++;
286
287 /*
288 * Send it to the log file as needed...
289 */
290
291 if (sb->prefix[0])
292 {
293 if (*loglevel > CUPSD_LOG_NONE &&
294 (*loglevel != CUPSD_LOG_INFO || LogLevel == CUPSD_LOG_DEBUG2))
295 {
296 /*
297 * General status message; send it to the error_log file...
298 */
299
300 if (message[0] == '[')
301 cupsdLogMessage(*loglevel, "%s", message);
302 else
303 cupsdLogMessage(*loglevel, "%s %s", sb->prefix, message);
304 }
305 else if (*loglevel < CUPSD_LOG_NONE && LogLevel == CUPSD_LOG_DEBUG2)
306 cupsdLogMessage(CUPSD_LOG_DEBUG2, "%s %s", sb->prefix, sb->buffer);
307 }
308
309 /*
310 * Copy the message to the line buffer...
311 */
312
313 strlcpy(line, message, linelen);
314
315 /*
316 * Copy over the buffer data we've used up...
317 */
318
319 if (lineptr < sb->buffer + sb->bufused)
320 _cups_strcpy(sb->buffer, lineptr);
321
322 sb->bufused -= lineptr - sb->buffer;
323
324 if (sb->bufused < 0)
325 sb->bufused = 0;
326
327 return (line);
328 }
329
330
331 /*
332 * End of "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $".
333 */