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