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