]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/statbuf.c
Merge changes from CUPS 1.4svn-r8454.
[thirdparty/cups.git] / scheduler / statbuf.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $"
ef416fc2 3 *
4 * Status buffer routines for the Common UNIX Printing System (CUPS)
5 * scheduler.
6 *
f0ab5bff 7 * Copyright 2007-2009 by Apple Inc.
e00b005a 8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 9 *
10 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 15 *
16 * Contents:
17 *
ef416fc2 18 * cupsdStatBufDelete() - Destroy a status buffer.
75bd9771 19 * cupsdStatBufNew() - Create a new status buffer.
ef416fc2 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
75bd9771
MS
31/*
32 * 'cupsdStatBufDelete()' - Destroy a status buffer.
33 */
34
35void
36cupsdStatBufDelete(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
ef416fc2 55/*
56 * 'cupsdStatBufNew()' - Create a new status buffer.
57 */
58
59cupsd_statbuf_t * /* O - New status buffer */
b9faaae1 60cupsdStatBufNew(int fd, /* I - File descriptor of pipe */
ef416fc2 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
ef416fc2 116/*
117 * 'cupsdStatBufUpdate()' - Update the status buffer.
118 */
119
120char * /* O - Line from buffer, "", or NULL */
f0ab5bff
MS
121cupsdStatBufUpdate(
122 cupsd_statbuf_t *sb, /* I - Status buffer */
123 int *loglevel, /* O - Log level */
124 char *line, /* I - Line buffer */
125 int linelen) /* I - Size of line buffer */
ef416fc2 126{
127 int bytes; /* Number of bytes read */
128 char *lineptr, /* Pointer to end of line in buffer */
129 *message; /* Pointer to message text */
130
131
132 /*
133 * Check if the buffer already contains a full line...
134 */
135
136 if ((lineptr = strchr(sb->buffer, '\n')) == NULL)
137 {
138 /*
139 * No, read more data...
140 */
141
142 if ((bytes = read(sb->fd, sb->buffer + sb->bufused,
143 CUPSD_SB_BUFFER_SIZE - sb->bufused - 1)) > 0)
144 {
145 sb->bufused += bytes;
146 sb->buffer[sb->bufused] = '\0';
147
148 /*
149 * Guard against a line longer than the max buffer size...
150 */
151
152 if ((lineptr = strchr(sb->buffer, '\n')) == NULL &&
153 sb->bufused == (CUPSD_SB_BUFFER_SIZE - 1))
154 lineptr = sb->buffer + sb->bufused;
155 }
156 else if (bytes < 0 && errno == EINTR)
157 {
158 /*
159 * Return an empty line if we are interrupted...
160 */
161
162 *loglevel = CUPSD_LOG_NONE;
163 line[0] = '\0';
164
165 return (line);
166 }
167 else
168 {
169 /*
170 * End-of-file, so use the whole buffer...
171 */
172
173 lineptr = sb->buffer + sb->bufused;
174 *lineptr = '\0';
175 }
176
177 /*
178 * Final check for end-of-file...
179 */
180
181 if (sb->bufused == 0 && bytes == 0)
182 lineptr = NULL;
183 }
184
d09495fa 185 if (!lineptr)
ef416fc2 186 {
187 /*
188 * End of file...
189 */
190
191 *loglevel = CUPSD_LOG_NONE;
192 line[0] = '\0';
193
194 return (NULL);
195 }
196
197 /*
198 * Terminate the line and process it...
199 */
200
201 *lineptr++ = '\0';
202
203 /*
204 * Figure out the logging level...
205 */
206
207 if (!strncmp(sb->buffer, "EMERG:", 6))
208 {
209 *loglevel = CUPSD_LOG_EMERG;
210 message = sb->buffer + 6;
211 }
212 else if (!strncmp(sb->buffer, "ALERT:", 6))
213 {
214 *loglevel = CUPSD_LOG_ALERT;
215 message = sb->buffer + 6;
216 }
217 else if (!strncmp(sb->buffer, "CRIT:", 5))
218 {
219 *loglevel = CUPSD_LOG_CRIT;
220 message = sb->buffer + 5;
221 }
222 else if (!strncmp(sb->buffer, "ERROR:", 6))
223 {
224 *loglevel = CUPSD_LOG_ERROR;
225 message = sb->buffer + 6;
226 }
227 else if (!strncmp(sb->buffer, "WARNING:", 8))
228 {
229 *loglevel = CUPSD_LOG_WARN;
230 message = sb->buffer + 8;
231 }
232 else if (!strncmp(sb->buffer, "NOTICE:", 7))
233 {
234 *loglevel = CUPSD_LOG_NOTICE;
235 message = sb->buffer + 7;
236 }
237 else if (!strncmp(sb->buffer, "INFO:", 5))
238 {
239 *loglevel = CUPSD_LOG_INFO;
240 message = sb->buffer + 5;
241 }
242 else if (!strncmp(sb->buffer, "DEBUG:", 6))
243 {
244 *loglevel = CUPSD_LOG_DEBUG;
245 message = sb->buffer + 6;
246 }
247 else if (!strncmp(sb->buffer, "DEBUG2:", 7))
248 {
249 *loglevel = CUPSD_LOG_DEBUG2;
250 message = sb->buffer + 7;
251 }
252 else if (!strncmp(sb->buffer, "PAGE:", 5))
253 {
254 *loglevel = CUPSD_LOG_PAGE;
255 message = sb->buffer + 5;
256 }
257 else if (!strncmp(sb->buffer, "STATE:", 6))
258 {
259 *loglevel = CUPSD_LOG_STATE;
260 message = sb->buffer + 6;
261 }
262 else if (!strncmp(sb->buffer, "ATTR:", 5))
263 {
264 *loglevel = CUPSD_LOG_ATTR;
265 message = sb->buffer + 5;
266 }
c9fc04c6
MS
267 else if (!strncmp(sb->buffer, "PPD:", 4))
268 {
269 *loglevel = CUPSD_LOG_PPD;
270 message = sb->buffer + 4;
271 }
ef416fc2 272 else
273 {
89d46774 274 *loglevel = CUPSD_LOG_DEBUG;
ef416fc2 275 message = sb->buffer;
276 }
277
278 /*
279 * Skip leading whitespace in the message...
280 */
281
282 while (isspace(*message & 255))
283 message ++;
284
285 /*
286 * Send it to the log file as needed...
287 */
288
75bd9771 289 if (sb->prefix[0])
ef416fc2 290 {
75bd9771
MS
291 if (*loglevel > CUPSD_LOG_NONE &&
292 (*loglevel != CUPSD_LOG_INFO || LogLevel == CUPSD_LOG_DEBUG2))
293 {
294 /*
295 * General status message; send it to the error_log file...
296 */
ef416fc2 297
75bd9771
MS
298 if (message[0] == '[')
299 cupsdLogMessage(*loglevel, "%s", message);
300 else
301 cupsdLogMessage(*loglevel, "%s %s", sb->prefix, message);
302 }
303 else if (*loglevel < CUPSD_LOG_NONE && LogLevel == CUPSD_LOG_DEBUG2)
304 cupsdLogMessage(CUPSD_LOG_DEBUG2, "%s %s", sb->prefix, sb->buffer);
ef416fc2 305 }
306
307 /*
308 * Copy the message to the line buffer...
309 */
310
311 strlcpy(line, message, linelen);
312
313 /*
314 * Copy over the buffer data we've used up...
315 */
316
e53920b9 317 if (lineptr < sb->buffer + sb->bufused)
318 _cups_strcpy(sb->buffer, lineptr);
319
ef416fc2 320 sb->bufused -= lineptr - sb->buffer;
321
322 if (sb->bufused < 0)
323 sb->bufused = 0;
324
325 return (line);
326}
327
328
329/*
75bd9771 330 * End of "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $".
ef416fc2 331 */