]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/statbuf.c
2 * "$Id: statbuf.c 5373 2006-04-06 20:03:32Z mike $"
4 * Status buffer routines for the Common UNIX Printing System (CUPS)
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
27 * cupsdStatBufNew() - Create a new status buffer.
28 * cupsdStatBufDelete() - Destroy a status buffer.
29 * cupsdStatBufUpdate() - Update the status buffer.
33 * Include necessary headers...
41 * 'cupsdStatBufNew()' - Create a new status buffer.
44 cupsd_statbuf_t
* /* O - New status buffer */
45 cupsdStatBufNew(int fd
, /* I - File descriptor of pipe */
46 const char *prefix
, /* I - Printf-style prefix string */
47 ...) /* I - Additional args as needed */
49 cupsd_statbuf_t
*sb
; /* New status buffer */
50 va_list ap
; /* Argument list */
54 * Range check input...
61 * Allocate the status buffer...
64 if ((sb
= calloc(1, sizeof(cupsd_statbuf_t
))) != NULL
)
67 * Assign the file descriptor...
73 * Format the prefix string, if any. This is usually "[Job 123]"
74 * or "[Sub 123]", and so forth.
80 * Printf-style prefix string...
84 vsnprintf(sb
->prefix
, sizeof(sb
->prefix
), prefix
, ap
);
102 * 'cupsdStatBufDelete()' - Destroy a status buffer.
106 cupsdStatBufDelete(cupsd_statbuf_t
*sb
) /* I - Status buffer */
109 * Range check input...
116 * Close the status pipe and free memory used...
126 * 'cupsdStatBufUpdate()' - Update the status buffer.
129 char * /* O - Line from buffer, "", or NULL */
130 cupsdStatBufUpdate(cupsd_statbuf_t
*sb
, /* I - Status buffer */
134 /* I - Line buffer */
136 /* I - Size of line buffer */
138 int bytes
; /* Number of bytes read */
139 char *lineptr
, /* Pointer to end of line in buffer */
140 *message
; /* Pointer to message text */
144 * Check if the buffer already contains a full line...
147 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
)
150 * No, read more data...
153 if ((bytes
= read(sb
->fd
, sb
->buffer
+ sb
->bufused
,
154 CUPSD_SB_BUFFER_SIZE
- sb
->bufused
- 1)) > 0)
156 sb
->bufused
+= bytes
;
157 sb
->buffer
[sb
->bufused
] = '\0';
160 * Guard against a line longer than the max buffer size...
163 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
&&
164 sb
->bufused
== (CUPSD_SB_BUFFER_SIZE
- 1))
165 lineptr
= sb
->buffer
+ sb
->bufused
;
167 else if (bytes
< 0 && errno
== EINTR
)
170 * Return an empty line if we are interrupted...
173 *loglevel
= CUPSD_LOG_NONE
;
181 * End-of-file, so use the whole buffer...
184 lineptr
= sb
->buffer
+ sb
->bufused
;
189 * Final check for end-of-file...
192 if (sb
->bufused
== 0 && bytes
== 0)
202 *loglevel
= CUPSD_LOG_NONE
;
209 * Terminate the line and process it...
215 * Figure out the logging level...
218 if (!strncmp(sb
->buffer
, "EMERG:", 6))
220 *loglevel
= CUPSD_LOG_EMERG
;
221 message
= sb
->buffer
+ 6;
223 else if (!strncmp(sb
->buffer
, "ALERT:", 6))
225 *loglevel
= CUPSD_LOG_ALERT
;
226 message
= sb
->buffer
+ 6;
228 else if (!strncmp(sb
->buffer
, "CRIT:", 5))
230 *loglevel
= CUPSD_LOG_CRIT
;
231 message
= sb
->buffer
+ 5;
233 else if (!strncmp(sb
->buffer
, "ERROR:", 6))
235 *loglevel
= CUPSD_LOG_ERROR
;
236 message
= sb
->buffer
+ 6;
238 else if (!strncmp(sb
->buffer
, "WARNING:", 8))
240 *loglevel
= CUPSD_LOG_WARN
;
241 message
= sb
->buffer
+ 8;
243 else if (!strncmp(sb
->buffer
, "NOTICE:", 7))
245 *loglevel
= CUPSD_LOG_NOTICE
;
246 message
= sb
->buffer
+ 7;
248 else if (!strncmp(sb
->buffer
, "INFO:", 5))
250 *loglevel
= CUPSD_LOG_INFO
;
251 message
= sb
->buffer
+ 5;
253 else if (!strncmp(sb
->buffer
, "DEBUG:", 6))
255 *loglevel
= CUPSD_LOG_DEBUG
;
256 message
= sb
->buffer
+ 6;
258 else if (!strncmp(sb
->buffer
, "DEBUG2:", 7))
260 *loglevel
= CUPSD_LOG_DEBUG2
;
261 message
= sb
->buffer
+ 7;
263 else if (!strncmp(sb
->buffer
, "PAGE:", 5))
265 *loglevel
= CUPSD_LOG_PAGE
;
266 message
= sb
->buffer
+ 5;
268 else if (!strncmp(sb
->buffer
, "STATE:", 6))
270 *loglevel
= CUPSD_LOG_STATE
;
271 message
= sb
->buffer
+ 6;
273 else if (!strncmp(sb
->buffer
, "ATTR:", 5))
275 *loglevel
= CUPSD_LOG_ATTR
;
276 message
= sb
->buffer
+ 5;
280 *loglevel
= CUPSD_LOG_ERROR
;
281 message
= sb
->buffer
;
285 * Skip leading whitespace in the message...
288 while (isspace(*message
& 255))
292 * Send it to the log file as needed...
295 if (*loglevel
> CUPSD_LOG_NONE
&&
296 (*loglevel
!= CUPSD_LOG_INFO
|| LogLevel
== CUPSD_LOG_DEBUG2
))
299 * General status message; send it to the error_log file...
302 if (message
[0] == '[')
303 cupsdLogMessage(*loglevel
, "%s", message
);
305 cupsdLogMessage(*loglevel
, "%s %s", sb
->prefix
, message
);
307 else if (*loglevel
< CUPSD_LOG_NONE
&& LogLevel
== CUPSD_LOG_DEBUG2
)
308 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "%s %s", sb
->prefix
, sb
->buffer
);
311 * Copy the message to the line buffer...
314 strlcpy(line
, message
, linelen
);
317 * Copy over the buffer data we've used up...
320 if (lineptr
< sb
->buffer
+ sb
->bufused
)
321 _cups_strcpy(sb
->buffer
, lineptr
);
323 sb
->bufused
-= lineptr
- sb
->buffer
;
333 * End of "$Id: statbuf.c 5373 2006-04-06 20:03:32Z mike $".