1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*LINTLIBRARY*/
30 
31 #include <stdlib.h>
32 #include <libintl.h>
33 #include <unistd.h>
34 #include <sys/utsname.h>
35 #include <papi_impl.h>
36 
37 #include "class.h"
38 
39 
40 void
41 lpsched_printer_status_to_attributes(papi_attribute_t ***attrs,
42 	unsigned short status)
43 {
44 	if (attrs == NULL)
45 		return;
46 
47 	if (status & (PS_DISABLED|PS_LATER|PS_FAULTED|PS_FORM_FAULT)) {
48 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
49 				"printer-state", 0x05); /* stopped */
50 		if (status & PS_LATER)
51 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
52 				"printer-state-reasons", "moving-to-paused");
53 		else if (status & PS_FAULTED)
54 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
55 				"printer-state-reasons", "none");
56 		else if (status & PS_FORM_FAULT)
57 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
58 				"printer-state-reasons",
59 				"interpreter-resource-unavailable");
60 		else
61 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
62 				"printer-state-reasons", "paused");
63 	} else if (status & PS_BUSY) {
64 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
65 				"printer-state", 0x04); /* processing */
66 		papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
67 				"printer-state-reasons", "moving-to-paused");
68 	} else {
69 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
70 				"printer-state", 0x03); /* idle */
71 	}
72 
73 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
74 			"printer-is-accepting-jobs",
75 			((status & PS_REJECTED) != PS_REJECTED));
76 	papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
77 			"lpsched-status", status);
78 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
79 			"printer-is-processing-jobs",
80 			((status & PS_DISABLED) != PS_DISABLED));
81 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
82 			"lpsched-faulted", (status & PS_FAULTED));
83 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
84 			"lpsched-busy", (status & PS_BUSY));
85 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
86 			"lpsched-later", (status & PS_LATER));
87 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
88 			"lpsched-remote", (status & PS_REMOTE));
89 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
90 			"lpsched-show-fault", (status & PS_SHOW_FAULT));
91 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
92 			"lpsched-use-as-key", (status & PS_USE_AS_KEY));
93 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
94 			"lpsched-form-fault", (status & PS_FORM_FAULT));
95 }
96 
97 void
98 lpsched_printer_defaults(papi_attribute_t ***attributes)
99 {
100 	if (attributes == NULL)
101 		return;
102 
103 	papiAttributeListAddBoolean(attributes, PAPI_ATTR_REPLACE,
104 			"multiple-document-jobs-supported", PAPI_TRUE);
105 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
106 			"multiple-document-handling-supported",
107 			"seperate-documents-colated-copies");
108 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
109 			"pdl-override-supported", "not-attempted");
110 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
111 			"job-priority-supported", 40);
112 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
113 			"job-priority-default", 20);
114 	papiAttributeListAddRange(attributes, PAPI_ATTR_REPLACE,
115 			"copies-supported", 1, 65535);
116 	papiAttributeListAddBoolean(attributes, PAPI_ATTR_REPLACE,
117 			"page-ranges-supported", PAPI_TRUE);
118 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
119 			"number-up-supported", 1);
120 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
121 			"number-up-default", 1);
122 }
123 
124 papi_status_t
125 lpsched_printer_configuration_to_attributes(service_t *svc, printer_t *p,
126 	char *dest)
127 {
128 	PRINTER *tmp;
129 	char buf[BUFSIZ+1];
130 	struct utsname sysname;
131 
132 	if ((svc == NULL) || (p == NULL) || (dest == NULL))
133 		return (PAPI_BAD_ARGUMENT);
134 
135 	/* get the configuration DB data */
136 	if ((tmp = getprinter(dest)) == NULL) {
137 		detailed_error(svc,
138 			gettext("unable to read configuration data"));
139 		return (PAPI_DEVICE_ERROR);
140 	}
141 
142 	/* name */
143 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
144 				"printer-name", tmp->name);
145 	if (tmp->name != NULL) {
146 		char uri[BUFSIZ];
147 
148 		snprintf(uri, sizeof (uri), "lpsched://%s", tmp->name);
149 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
150 				"printer-uri-supported", uri);
151 	}
152 
153 	/* banner */
154 	if ((tmp->banner & BAN_OPTIONAL) == BAN_OPTIONAL)
155 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
156 				"job-sheets-supported", "optional");
157 	else if (tmp->banner & BAN_NEVER)
158 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
159 				"job-sheets-supported", "none");
160 	else if (tmp->banner & BAN_ALWAYS)
161 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
162 				"job-sheets-supported", "standard");
163 
164 	/* input_types */
165 	if (tmp->input_types != NULL) {
166 		int i;
167 
168 		for (i = 0; tmp->input_types[i] != NULL; i++)
169 			addLPString(&p->attributes,
170 				PAPI_ATTR_APPEND, "document-format-supported",
171 				lp_type_to_mime_type(tmp->input_types[i]));
172 	}
173 
174 	/* description */
175 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
176 			"printer-info", tmp->description);
177 
178 	/* add lpsched specific attributes */
179 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
180 				"device-uri", tmp->device);
181 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
182 				"lpsched-dial-info", tmp->dial_info);
183 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
184 				"lpsched-fault-recovery", tmp->fault_rec);
185 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
186 				"lpsched-interface-script", tmp->interface);
187 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
188 				"lpsched-data-rate", tmp->speed);
189 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
190 				"lpsched-stty", tmp->stty);
191 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
192 				"lpsched-remote", tmp->remote);
193 	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
194 				"lpsched-login-term", tmp->login);
195 	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
196 				"lpsched-daisy", tmp->daisy);
197 	addLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
198 				"lpsched-charsets", tmp->char_sets);
199 #ifdef CAN_DO_MODULES
200 	addLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
201 				"lpsched-modules", tmp->modules);
202 #endif /* CAN_DO_MODULES */
203 	addLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
204 				"lpsched-options", tmp->options);
205 	addLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
206 				"lpsched-printer-type", tmp->printer_types);
207 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
208 				"lpsched-fault-alert-command",
209 				tmp->fault_alert.shcmd);
210 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
211 				"lpsched-fault-alert-threshold",
212 				tmp->fault_alert.Q);
213 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
214 				"lpsched-fault-alert-interval",
215 				tmp->fault_alert.W);
216 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
217 				"lpsched-cpi-value", tmp->cpi.val);
218 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
219 				"lpsched-cpi-unit", tmp->cpi.sc);
220 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
221 				"lpsched-lpi-value", tmp->lpi.val);
222 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
223 				"lpsched-lpi-unit", tmp->lpi.sc);
224 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
225 				"lpsched-plen-value", tmp->plen.val);
226 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
227 				"lpsched-plen-unit", tmp->plen.sc);
228 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
229 				"lpsched-pwid-value", tmp->pwid.val);
230 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
231 				"lpsched-pwid-unit", tmp->pwid.sc);
232 #ifdef LP_USE_PAPI_ATTR
233 	if (tmp->ppd != NULL) {
234 		int fd;
235 		struct stat sbuf;
236 
237 		/* construct the two URIs for the printer's PPD file */
238 		if (uname(&sysname) < 0) {
239 			/* failed to get systen name */
240 			sysname.nodename[0] = 0;
241 		}
242 		snprintf(buf, sizeof (buf), "file://%s%s/ppd/%s.ppd",
243 			sysname.nodename, ETCDIR, tmp->name);
244 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
245 			"lpsched-printer-ppd-uri", buf);
246 
247 		snprintf(buf, sizeof (buf), "file://%s%s",
248 			sysname.nodename, tmp->ppd);
249 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
250 			"lpsched-printer-configure-ppd-uri", buf);
251 
252 		snprintf(buf, sizeof (buf), "%s/ppd/%s.ppd", ETCDIR, tmp->name);
253 
254 		/*
255 		 * We don't return error on any of the error conditions, we just
256 		 * silently return without adding the attribute.
257 		 */
258 		if (((fd = open(buf, O_RDONLY)) >= 0) &&
259 		    (fstat(fd, &sbuf) == 0)) {
260 			char *contents;
261 
262 			if ((contents = malloc(sbuf.st_size + 1)) != NULL) {
263 				int pos = 0, rd, rdsize;
264 
265 				rdsize = sbuf.st_blksize;
266 
267 				while (rd = read(fd, contents + pos, rdsize)) {
268 					if (rd < 0) {
269 						if (errno == EINTR) {
270 							continue;
271 						} else {
272 							break;
273 						}
274 					}
275 					pos += rd;
276 
277 					/*
278 					 * Don't write past the end of our
279 					 * buffer.  This is paranoid, in case
280 					 * the file increased size while we were
281 					 * reading it.
282 					 */
283 					if (pos + rdsize > sbuf.st_size) {
284 						rdsize = sbuf.st_size - pos;
285 					}
286 				}
287 
288 				/* File didn't change size while reading. */
289 				if (pos + rd == sbuf.st_size) {
290 					/*
291 					 * Terminate the buffer and set
292 					 * attribute. This assume that there
293 					 * are no null bytes in the ppd file.
294 					 */
295 					contents[pos + rd] = '\0';
296 
297 					papiAttributeListAddString(
298 						&p->attributes,
299 						PAPI_ATTR_REPLACE,
300 						"lpsched-printer-ppd-contents",
301 						contents);
302 				}
303 
304 				free(contents);
305 			}
306 		}
307 		close(fd);
308 	}
309 #endif
310 
311 	freeprinter(tmp);
312 
313 	return (PAPI_OK);
314 }
315 
316 papi_status_t
317 printer_status_to_attributes(printer_t *p, char *printer, char *form,
318 		char *character_set, char *reject_reason, char *disable_reason,
319 		short status, char *request_id,
320 		long enable_date, long reject_date)
321 {
322 	if (p == NULL)
323 		return (PAPI_BAD_ARGUMENT);
324 
325 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
326 			"media-ready", form);
327 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
328 			"lpsched-active-job", request_id);
329 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
330 			"lpsched-mounted-char-set", character_set);
331 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
332 			"printer-reject-reason", reject_reason);
333 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
334 			"printer-disable-reason", disable_reason);
335 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
336 			"lpsched-enable-date", enable_date);
337 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
338 			"lpsched-reject-date", reject_date);
339 
340 	/* add the current system time */
341 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
342 			"printer-current-time", time(NULL));
343 
344 	/* add the time since last enabled */
345 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
346 			"printer-up-time", time(NULL));
347 
348 	/* add the status information */
349 	lpsched_printer_status_to_attributes(&p->attributes, status);
350 
351 	papiAttributeListAddString(&p->attributes, PAPI_ATTR_EXCL,
352 			"printer-state-reasons", "none");
353 
354 	lpsched_printer_defaults(&p->attributes);
355 
356 	return (PAPI_OK);
357 }
358 
359 
360 /*
361  * This puts the class information in only.  It could create a hybrid
362  * printer object to return, but that is problematic at best.
363  */
364 papi_status_t
365 lpsched_class_configuration_to_attributes(service_t *svc, printer_t *p,
366 	char *dest)
367 {
368 	CLASS *tmp;
369 
370 	if ((svc == NULL) || (p == NULL))
371 		return (PAPI_BAD_ARGUMENT);
372 
373 	/* get the configuration DB data */
374 	if ((tmp = getclass(dest)) == NULL) {
375 		detailed_error(svc,
376 			gettext("unable to read configuration data"));
377 		return (PAPI_DEVICE_ERROR);
378 	}
379 
380 	/* name */
381 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
382 				"printer-name", tmp->name);
383 	if (tmp->name != NULL) {
384 		char uri[BUFSIZ];
385 
386 		snprintf(uri, sizeof (uri), "lpsched://%s", tmp->name);
387 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
388 				"printer-uri-supported", uri);
389 	}
390 
391 	if (tmp->members != NULL) {
392 		char **members = tmp->members;
393 		int i;
394 
395 		for (i = 0; members[i] != NULL; i++)
396 			papiAttributeListAddString(&p->attributes,
397 					PAPI_ATTR_APPEND,
398 					"member-names", members[i]);
399 	}
400 
401 	freeclass(tmp);
402 
403 	return (PAPI_OK);
404 }
405 
406 papi_status_t
407 class_status_to_attributes(printer_t *p, char *printer, short status,
408 		char *reject_reason, long reject_date)
409 {
410 	if (p == NULL)
411 		return (PAPI_BAD_ARGUMENT);
412 
413 	addLPString(&p->attributes, PAPI_ATTR_REPLACE,
414 			"printer-reject-reason", reject_reason);
415 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
416 			"lpsched-reject-date", reject_date);
417 
418 	/* add the current system time */
419 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
420 			"printer-current-time", time(NULL));
421 
422 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
423 			"printer-up-time", time(NULL));
424 
425 	/* add the status information */
426 	lpsched_printer_status_to_attributes(&p->attributes, status);
427 
428 	papiAttributeListAddString(&p->attributes, PAPI_ATTR_EXCL,
429 			"printer-state-reasons", "none");
430 
431 	lpsched_printer_defaults(&p->attributes);
432 
433 	return (PAPI_OK);
434 }
435