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