]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/statbuf.c
2 * "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $"
4 * Status buffer routines for the Common UNIX Printing System (CUPS)
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
18 * cupsdStatBufDelete() - Destroy a status buffer.
19 * cupsdStatBufNew() - Create a new status buffer.
20 * cupsdStatBufUpdate() - Update the status buffer.
24 * Include necessary headers...
32 * 'cupsdStatBufDelete()' - Destroy a status buffer.
36 cupsdStatBufDelete(cupsd_statbuf_t
*sb
) /* I - Status buffer */
39 * Range check input...
46 * Close the status pipe and free memory used...
56 * 'cupsdStatBufNew()' - Create a new status buffer.
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 */
64 cupsd_statbuf_t
*sb
; /* New status buffer */
65 va_list ap
; /* Argument list */
69 * Range check input...
76 * Allocate the status buffer...
79 if ((sb
= calloc(1, sizeof(cupsd_statbuf_t
))) != NULL
)
82 * Assign the file descriptor...
88 * Format the prefix string, if any. This is usually "[Job 123]"
89 * or "[Sub 123]", and so forth.
95 * Printf-style prefix string...
99 vsnprintf(sb
->prefix
, sizeof(sb
->prefix
), prefix
, ap
);
105 * No prefix string...
108 sb
->prefix
[0] = '\0';
117 * 'cupsdStatBufUpdate()' - Update the status buffer.
120 char * /* O - Line from buffer, "", or NULL */
121 cupsdStatBufUpdate(cupsd_statbuf_t
*sb
, /* I - Status buffer */
125 /* I - Line buffer */
127 /* I - Size of line buffer */
129 int bytes
; /* Number of bytes read */
130 char *lineptr
, /* Pointer to end of line in buffer */
131 *message
; /* Pointer to message text */
135 * Check if the buffer already contains a full line...
138 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
)
141 * No, read more data...
144 if ((bytes
= read(sb
->fd
, sb
->buffer
+ sb
->bufused
,
145 CUPSD_SB_BUFFER_SIZE
- sb
->bufused
- 1)) > 0)
147 sb
->bufused
+= bytes
;
148 sb
->buffer
[sb
->bufused
] = '\0';
151 * Guard against a line longer than the max buffer size...
154 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
&&
155 sb
->bufused
== (CUPSD_SB_BUFFER_SIZE
- 1))
156 lineptr
= sb
->buffer
+ sb
->bufused
;
158 else if (bytes
< 0 && errno
== EINTR
)
161 * Return an empty line if we are interrupted...
164 *loglevel
= CUPSD_LOG_NONE
;
172 * End-of-file, so use the whole buffer...
175 lineptr
= sb
->buffer
+ sb
->bufused
;
180 * Final check for end-of-file...
183 if (sb
->bufused
== 0 && bytes
== 0)
193 *loglevel
= CUPSD_LOG_NONE
;
200 * Terminate the line and process it...
206 * Figure out the logging level...
209 if (!strncmp(sb
->buffer
, "EMERG:", 6))
211 *loglevel
= CUPSD_LOG_EMERG
;
212 message
= sb
->buffer
+ 6;
214 else if (!strncmp(sb
->buffer
, "ALERT:", 6))
216 *loglevel
= CUPSD_LOG_ALERT
;
217 message
= sb
->buffer
+ 6;
219 else if (!strncmp(sb
->buffer
, "CRIT:", 5))
221 *loglevel
= CUPSD_LOG_CRIT
;
222 message
= sb
->buffer
+ 5;
224 else if (!strncmp(sb
->buffer
, "ERROR:", 6))
226 *loglevel
= CUPSD_LOG_ERROR
;
227 message
= sb
->buffer
+ 6;
229 else if (!strncmp(sb
->buffer
, "WARNING:", 8))
231 *loglevel
= CUPSD_LOG_WARN
;
232 message
= sb
->buffer
+ 8;
234 else if (!strncmp(sb
->buffer
, "NOTICE:", 7))
236 *loglevel
= CUPSD_LOG_NOTICE
;
237 message
= sb
->buffer
+ 7;
239 else if (!strncmp(sb
->buffer
, "INFO:", 5))
241 *loglevel
= CUPSD_LOG_INFO
;
242 message
= sb
->buffer
+ 5;
244 else if (!strncmp(sb
->buffer
, "DEBUG:", 6))
246 *loglevel
= CUPSD_LOG_DEBUG
;
247 message
= sb
->buffer
+ 6;
249 else if (!strncmp(sb
->buffer
, "DEBUG2:", 7))
251 *loglevel
= CUPSD_LOG_DEBUG2
;
252 message
= sb
->buffer
+ 7;
254 else if (!strncmp(sb
->buffer
, "PAGE:", 5))
256 *loglevel
= CUPSD_LOG_PAGE
;
257 message
= sb
->buffer
+ 5;
259 else if (!strncmp(sb
->buffer
, "STATE:", 6))
261 *loglevel
= CUPSD_LOG_STATE
;
262 message
= sb
->buffer
+ 6;
264 else if (!strncmp(sb
->buffer
, "ATTR:", 5))
266 *loglevel
= CUPSD_LOG_ATTR
;
267 message
= sb
->buffer
+ 5;
269 else if (!strncmp(sb
->buffer
, "PPD:", 4))
271 *loglevel
= CUPSD_LOG_PPD
;
272 message
= sb
->buffer
+ 4;
276 *loglevel
= CUPSD_LOG_DEBUG
;
277 message
= sb
->buffer
;
281 * Skip leading whitespace in the message...
284 while (isspace(*message
& 255))
288 * Send it to the log file as needed...
293 if (*loglevel
> CUPSD_LOG_NONE
&&
294 (*loglevel
!= CUPSD_LOG_INFO
|| LogLevel
== CUPSD_LOG_DEBUG2
))
297 * General status message; send it to the error_log file...
300 if (message
[0] == '[')
301 cupsdLogMessage(*loglevel
, "%s", message
);
303 cupsdLogMessage(*loglevel
, "%s %s", sb
->prefix
, message
);
305 else if (*loglevel
< CUPSD_LOG_NONE
&& LogLevel
== CUPSD_LOG_DEBUG2
)
306 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "%s %s", sb
->prefix
, sb
->buffer
);
310 * Copy the message to the line buffer...
313 strlcpy(line
, message
, linelen
);
316 * Copy over the buffer data we've used up...
319 if (lineptr
< sb
->buffer
+ sb
->bufused
)
320 _cups_strcpy(sb
->buffer
, lineptr
);
322 sb
->bufused
-= lineptr
- sb
->buffer
;
332 * End of "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $".