]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/statbuf.c
4ea37be9efd53e9a19e92068940b51ad0c19ccc7
2 * Status buffer routines for the CUPS scheduler.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
23 * 'cupsdStatBufDelete()' - Destroy a status buffer.
27 cupsdStatBufDelete(cupsd_statbuf_t
*sb
) /* I - Status buffer */
30 * Range check input...
37 * Close the status pipe and free memory used...
47 * 'cupsdStatBufNew()' - Create a new status buffer.
50 cupsd_statbuf_t
* /* O - New status buffer */
51 cupsdStatBufNew(int fd
, /* I - File descriptor of pipe */
52 const char *prefix
, /* I - Printf-style prefix string */
53 ...) /* I - Additional args as needed */
55 cupsd_statbuf_t
*sb
; /* New status buffer */
56 va_list ap
; /* Argument list */
60 * Range check input...
67 * Allocate the status buffer...
70 if ((sb
= calloc(1, sizeof(cupsd_statbuf_t
))) != NULL
)
73 * Assign the file descriptor...
79 * Format the prefix string, if any. This is usually "[Job 123]"
80 * or "[Sub 123]", and so forth.
86 * Printf-style prefix string...
90 vsnprintf(sb
->prefix
, sizeof(sb
->prefix
), prefix
, ap
);
108 * 'cupsdStatBufUpdate()' - Update the status buffer.
111 char * /* O - Line from buffer, "", or NULL */
113 cupsd_statbuf_t
*sb
, /* I - Status buffer */
114 int *loglevel
, /* O - Log level */
115 char *line
, /* I - Line buffer */
116 int linelen
) /* I - Size of line buffer */
118 int bytes
; /* Number of bytes read */
119 char *lineptr
, /* Pointer to end of line in buffer */
120 *message
; /* Pointer to message text */
124 * Check if the buffer already contains a full line...
127 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
)
130 * No, read more data...
133 if ((bytes
= read(sb
->fd
, sb
->buffer
+ sb
->bufused
, (size_t)(CUPSD_SB_BUFFER_SIZE
- sb
->bufused
- 1))) > 0)
135 sb
->bufused
+= bytes
;
136 sb
->buffer
[sb
->bufused
] = '\0';
139 * Guard against a line longer than the max buffer size...
142 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
&&
143 sb
->bufused
== (CUPSD_SB_BUFFER_SIZE
- 1))
144 lineptr
= sb
->buffer
+ sb
->bufused
;
146 else if (bytes
< 0 && errno
== EINTR
)
149 * Return an empty line if we are interrupted...
152 *loglevel
= CUPSD_LOG_NONE
;
160 * End-of-file, so use the whole buffer...
163 lineptr
= sb
->buffer
+ sb
->bufused
;
168 * Final check for end-of-file...
171 if (sb
->bufused
== 0 && bytes
== 0)
181 *loglevel
= CUPSD_LOG_NONE
;
188 * Terminate the line and process it...
194 * Figure out the logging level...
197 if (!strncmp(sb
->buffer
, "EMERG:", 6))
199 *loglevel
= CUPSD_LOG_EMERG
;
200 message
= sb
->buffer
+ 6;
202 else if (!strncmp(sb
->buffer
, "ALERT:", 6))
204 *loglevel
= CUPSD_LOG_ALERT
;
205 message
= sb
->buffer
+ 6;
207 else if (!strncmp(sb
->buffer
, "CRIT:", 5))
209 *loglevel
= CUPSD_LOG_CRIT
;
210 message
= sb
->buffer
+ 5;
212 else if (!strncmp(sb
->buffer
, "ERROR:", 6))
214 *loglevel
= CUPSD_LOG_ERROR
;
215 message
= sb
->buffer
+ 6;
217 else if (!strncmp(sb
->buffer
, "WARNING:", 8))
219 *loglevel
= CUPSD_LOG_WARN
;
220 message
= sb
->buffer
+ 8;
222 else if (!strncmp(sb
->buffer
, "NOTICE:", 7))
224 *loglevel
= CUPSD_LOG_NOTICE
;
225 message
= sb
->buffer
+ 7;
227 else if (!strncmp(sb
->buffer
, "INFO:", 5))
229 *loglevel
= CUPSD_LOG_INFO
;
230 message
= sb
->buffer
+ 5;
232 else if (!strncmp(sb
->buffer
, "DEBUG:", 6))
234 *loglevel
= CUPSD_LOG_DEBUG
;
235 message
= sb
->buffer
+ 6;
237 else if (!strncmp(sb
->buffer
, "DEBUG2:", 7))
239 *loglevel
= CUPSD_LOG_DEBUG2
;
240 message
= sb
->buffer
+ 7;
242 else if (!strncmp(sb
->buffer
, "PAGE:", 5))
244 *loglevel
= CUPSD_LOG_PAGE
;
245 message
= sb
->buffer
+ 5;
247 else if (!strncmp(sb
->buffer
, "STATE:", 6))
249 *loglevel
= CUPSD_LOG_STATE
;
250 message
= sb
->buffer
+ 6;
252 else if (!strncmp(sb
->buffer
, "JOBSTATE:", 9))
254 *loglevel
= CUPSD_LOG_JOBSTATE
;
255 message
= sb
->buffer
+ 9;
257 else if (!strncmp(sb
->buffer
, "ATTR:", 5))
259 *loglevel
= CUPSD_LOG_ATTR
;
260 message
= sb
->buffer
+ 5;
262 else if (!strncmp(sb
->buffer
, "PPD:", 4))
264 *loglevel
= CUPSD_LOG_PPD
;
265 message
= sb
->buffer
+ 4;
269 *loglevel
= CUPSD_LOG_DEBUG
;
270 message
= sb
->buffer
;
274 * Skip leading whitespace in the message...
277 while (isspace(*message
& 255))
281 * Send it to the log file as needed...
286 if (*loglevel
> CUPSD_LOG_NONE
&&
287 (*loglevel
!= CUPSD_LOG_INFO
|| LogLevel
>= CUPSD_LOG_DEBUG
))
290 * General status message; send it to the error_log file...
293 if (message
[0] == '[')
294 cupsdLogMessage(*loglevel
, "%s", message
);
296 cupsdLogMessage(*loglevel
, "%s %s", sb
->prefix
, message
);
298 else if (*loglevel
< CUPSD_LOG_NONE
&& LogLevel
>= CUPSD_LOG_DEBUG
)
299 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "%s %s", sb
->prefix
, sb
->buffer
);
303 * Copy the message to the line buffer...
306 strlcpy(line
, message
, (size_t)linelen
);
309 * Copy over the buffer data we've used up...
312 if (lineptr
< sb
->buffer
+ sb
->bufused
)
313 _cups_strcpy(sb
->buffer
, lineptr
);
315 sb
->bufused
-= lineptr
- sb
->buffer
;