]>
Commit | Line | Data |
---|---|---|
294f4cee | 1 | /* |
c9d3f842 | 2 | * "$Id$" |
294f4cee | 3 | * |
4 | * Status buffer routines for the Common UNIX Printing System (CUPS) | |
5 | * scheduler. | |
6 | * | |
c9d3f842 | 7 | * Copyright 1997-2005 by Easy Software Products, all rights reserved. |
294f4cee | 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 | |
c9d3f842 | 19 | * Hollywood, Maryland 20636 USA |
294f4cee | 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 | ||
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 */ | |
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 | ||
105 | void | |
106 | cupsdStatBufDelete(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 | ||
129 | char * /* O - Line from buffer, "", or NULL */ | |
130 | cupsdStatBufUpdate(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 = L_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 | ||
196 | if (lineptr == NULL) | |
197 | { | |
198 | /* | |
199 | * End of file... | |
200 | */ | |
201 | ||
202 | *loglevel = L_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) == 0) | |
219 | { | |
220 | *loglevel = L_EMERG; | |
221 | message = sb->buffer + 6; | |
222 | } | |
223 | else if (strncmp(sb->buffer, "ALERT:", 6) == 0) | |
224 | { | |
225 | *loglevel = L_ALERT; | |
226 | message = sb->buffer + 6; | |
227 | } | |
228 | else if (strncmp(sb->buffer, "CRIT:", 5) == 0) | |
229 | { | |
230 | *loglevel = L_CRIT; | |
231 | message = sb->buffer + 5; | |
232 | } | |
233 | else if (strncmp(sb->buffer, "ERROR:", 6) == 0) | |
234 | { | |
235 | *loglevel = L_ERROR; | |
236 | message = sb->buffer + 6; | |
237 | } | |
238 | else if (strncmp(sb->buffer, "WARNING:", 8) == 0) | |
239 | { | |
240 | *loglevel = L_WARN; | |
241 | message = sb->buffer + 8; | |
242 | } | |
243 | else if (strncmp(sb->buffer, "NOTICE:", 6) == 0) | |
244 | { | |
245 | *loglevel = L_NOTICE; | |
246 | message = sb->buffer + 6; | |
247 | } | |
248 | else if (strncmp(sb->buffer, "INFO:", 5) == 0) | |
249 | { | |
250 | *loglevel = L_INFO; | |
251 | message = sb->buffer + 5; | |
252 | } | |
253 | else if (strncmp(sb->buffer, "DEBUG:", 6) == 0) | |
254 | { | |
255 | *loglevel = L_DEBUG; | |
256 | message = sb->buffer + 6; | |
257 | } | |
258 | else if (strncmp(sb->buffer, "DEBUG2:", 7) == 0) | |
259 | { | |
260 | *loglevel = L_DEBUG2; | |
261 | message = sb->buffer + 7; | |
262 | } | |
263 | else if (strncmp(sb->buffer, "PAGE:", 5) == 0) | |
264 | { | |
265 | *loglevel = L_PAGE; | |
266 | message = sb->buffer + 5; | |
267 | } | |
268 | else if (strncmp(sb->buffer, "STATE:", 6) == 0) | |
269 | { | |
270 | *loglevel = L_STATE; | |
271 | message = sb->buffer + 6; | |
272 | } | |
273 | else | |
274 | { | |
275 | *loglevel = L_DEBUG; | |
276 | message = sb->buffer; | |
277 | } | |
278 | ||
279 | /* | |
280 | * Skip leading whitespace in the message... | |
281 | */ | |
282 | ||
283 | while (isspace(*message & 255)) | |
284 | message ++; | |
285 | ||
286 | /* | |
287 | * Send it to the log file as needed... | |
288 | */ | |
289 | ||
290 | if (*loglevel > L_NONE && (*loglevel != L_INFO || LogLevel == L_DEBUG2)) | |
291 | { | |
292 | /* | |
293 | * General status message; send it to the error_log file... | |
294 | */ | |
295 | ||
296 | LogMessage(*loglevel, "%s %s", sb->prefix, message); | |
297 | } | |
298 | ||
299 | /* | |
300 | * Copy the message to the line buffer... | |
301 | */ | |
302 | ||
303 | strlcpy(line, message, linelen); | |
304 | ||
305 | /* | |
306 | * Copy over the buffer data we've used up... | |
307 | */ | |
308 | ||
309 | cups_strcpy(sb->buffer, lineptr); | |
310 | sb->bufused -= lineptr - sb->buffer; | |
311 | ||
312 | if (sb->bufused < 0) | |
313 | sb->bufused = 0; | |
314 | ||
315 | return (line); | |
316 | } | |
317 | ||
318 | ||
319 | /* | |
c9d3f842 | 320 | * End of "$Id$". |
294f4cee | 321 | */ |