xref: /illumos-gate/usr/src/cmd/lp/lib/papi/lpsched-jobs.c (revision 45916cd2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <pwd.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* lpsched include files */
377c478bd9Sstevel@tonic-gate #include "lp.h"
387c478bd9Sstevel@tonic-gate #include "requests.h"
397c478bd9Sstevel@tonic-gate #include "printers.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <papi_impl.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate papi_status_t
447c478bd9Sstevel@tonic-gate job_attributes_to_lpsched_request(papi_service_t svc, REQUEST *r,
457c478bd9Sstevel@tonic-gate 		papi_attribute_t **attributes)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	papi_attribute_t *attr;
487c478bd9Sstevel@tonic-gate 	int i;
497c478bd9Sstevel@tonic-gate 	char *s;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	char **options = NULL;
527c478bd9Sstevel@tonic-gate 	char **modes = NULL;
537c478bd9Sstevel@tonic-gate 	char *class = NULL;
547c478bd9Sstevel@tonic-gate 	char *job_name = NULL;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	char pr_filter = 0;
577c478bd9Sstevel@tonic-gate 	char *pr_title = NULL;
587c478bd9Sstevel@tonic-gate 	int pr_width = -1;
597c478bd9Sstevel@tonic-gate 	int pr_indent = -1;
607c478bd9Sstevel@tonic-gate 	int numberUp = 0;
617c478bd9Sstevel@tonic-gate 	int orientation = 0;
627c478bd9Sstevel@tonic-gate 	int lowerPage = 0;
637c478bd9Sstevel@tonic-gate 	int upperPage = 0;
647c478bd9Sstevel@tonic-gate 	papi_status_t getResult = 0;
657c478bd9Sstevel@tonic-gate 	char buf[256];
667c478bd9Sstevel@tonic-gate 	void *iterator = NULL;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	char banner = 0;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (attributes == NULL)
717c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "job-printer",
747c478bd9Sstevel@tonic-gate 				&r->destination);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	i = r->copies;
777c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "copies", &i);
787c478bd9Sstevel@tonic-gate 	if (i <= 0)
797c478bd9Sstevel@tonic-gate 		i = 1;
807c478bd9Sstevel@tonic-gate 	r->copies = i;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (papiAttributeListGetInteger(attributes, NULL, "priority", &i)
837c478bd9Sstevel@tonic-gate 			== PAPI_OK) {
847c478bd9Sstevel@tonic-gate 		if ((i < 1) || (i > 100))
857c478bd9Sstevel@tonic-gate 			i = 50;
867c478bd9Sstevel@tonic-gate 		i = (i + 1) / 2.5;
877c478bd9Sstevel@tonic-gate 		r->priority = i;
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if ((r->priority < 0) || (r->priority > 39))
917c478bd9Sstevel@tonic-gate 		r->priority = 20;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * 'media' size should be processed both in the lpsched filter and
957c478bd9Sstevel@tonic-gate 	 * the foomatic filter (if present) so that we ensure the result of
967c478bd9Sstevel@tonic-gate 	 * other options like 'page-ranges' are consistent.
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * TODO - I thing we should really have this but I can't get it to filter
1007c478bd9Sstevel@tonic-gate  *        so its commented out for now (paulcun)
1017c478bd9Sstevel@tonic-gate  *	papiAttributeListGetString(attributes, NULL, "media", &r->form);
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #ifndef LP_USE_PAPI_ATTR
1057c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "page-ranges", &r->pages);
1067c478bd9Sstevel@tonic-gate #else
1077c478bd9Sstevel@tonic-gate 	getResult =
1087c478bd9Sstevel@tonic-gate 	    papiAttributeListGetRange(attributes, &iterator,
1097c478bd9Sstevel@tonic-gate 		"page-ranges", &lowerPage, &upperPage);
1107c478bd9Sstevel@tonic-gate 	while (getResult == PAPI_OK) {
1117c478bd9Sstevel@tonic-gate 		if (r->pages == NULL) {
1127c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf),
1137c478bd9Sstevel@tonic-gate 				"%d-%d", lowerPage, upperPage);
1147c478bd9Sstevel@tonic-gate 			r->pages = (char *)strdup(buf);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 		else
1177c478bd9Sstevel@tonic-gate 		{
1187c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "%s,%d-%d",
1197c478bd9Sstevel@tonic-gate 				r->pages, lowerPage, upperPage);
1207c478bd9Sstevel@tonic-gate 			free(r->pages);
1217c478bd9Sstevel@tonic-gate 			r->pages = (char *)strdup(buf);
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		/*
1247c478bd9Sstevel@tonic-gate 		 * get the next value; note the attribute 'name' is set to
1257c478bd9Sstevel@tonic-gate 		 * NULL to do this.
1267c478bd9Sstevel@tonic-gate 		 */
1277c478bd9Sstevel@tonic-gate 		getResult =
1287c478bd9Sstevel@tonic-gate 		    papiAttributeListGetRange(attributes, &iterator,
1297c478bd9Sstevel@tonic-gate 			"page-ranges", &lowerPage, &upperPage);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	s = NULL;
1357c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "document-format", &s);
1367c478bd9Sstevel@tonic-gate 	if (s != NULL)
1377c478bd9Sstevel@tonic-gate 		r->input_type = strdup(mime_type_to_lp_type(s));
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * If we don't have an owner, set one.
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	if (r->user == NULL) {
1437c478bd9Sstevel@tonic-gate 		uid_t uid = getuid();
1447c478bd9Sstevel@tonic-gate 		struct passwd *pw;
1457c478bd9Sstevel@tonic-gate 		char *user = "intruder";
1467c478bd9Sstevel@tonic-gate 		char *host = NULL;
1477c478bd9Sstevel@tonic-gate 		char buf[256];
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1507c478bd9Sstevel@tonic-gate 			user = pw->pw_name; /* default to the process owner */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
1537c478bd9Sstevel@tonic-gate 			papiAttributeListGetString(attributes, NULL,
1547c478bd9Sstevel@tonic-gate 					"job-host", &host);
1557c478bd9Sstevel@tonic-gate 			papiAttributeListGetString(attributes, NULL,
1567c478bd9Sstevel@tonic-gate 					"job-originating-user-name", &user);
1577c478bd9Sstevel@tonic-gate 			papiAttributeListGetString(attributes, NULL,
1587c478bd9Sstevel@tonic-gate 					"requesting-user-name", &user);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "%s%s%s", user,
1617c478bd9Sstevel@tonic-gate 					(host ? "@" : ""), (host ? host : ""));
1627c478bd9Sstevel@tonic-gate 			user = buf;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		r->user = strdup(user);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	s = NULL;
1697c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
1707c478bd9Sstevel@tonic-gate 	if (s != NULL) {
1717c478bd9Sstevel@tonic-gate 		if (strcmp(s, "immediate") == 0)
1727c478bd9Sstevel@tonic-gate 			r->actions |= ACT_IMMEDIATE;
1737c478bd9Sstevel@tonic-gate 		else if ((strcmp(s, "resume") == 0) ||
1747c478bd9Sstevel@tonic-gate 			    (strcmp(s, "no-hold") == 0))
1757c478bd9Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
1767c478bd9Sstevel@tonic-gate 		else if ((strcmp(s, "hold") == 0) ||
1777c478bd9Sstevel@tonic-gate 			    (strcmp(s, "indefinite") == 0))
1787c478bd9Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "lp-charset", &r->charset);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* legacy pr(1) filter related garbage "lpr -p" */
1847c478bd9Sstevel@tonic-gate 	papiAttributeListGetBoolean(attributes, NULL, "pr-filter", &pr_filter);
1857c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "pr-title", &pr_title);
1867c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &pr_width);
1877c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &pr_indent);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (pr_filter != 0) {
1907c478bd9Sstevel@tonic-gate 		char buf[128];
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		if (pr_title != NULL) {
1937c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "prtitle='%s'", pr_title);
1947c478bd9Sstevel@tonic-gate 			appendlist(&modes, buf);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if (pr_width > 0) {
1987c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "prwidth=%d", pr_width);
1997c478bd9Sstevel@tonic-gate 			appendlist(&modes, buf);
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		if (pr_indent > 0) {
2037c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "indent=%d", pr_indent);
2047c478bd9Sstevel@tonic-gate 			appendlist(&modes, buf);
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	} else if ((pr_title != NULL) || (pr_width >= 0) || (pr_indent >= 0))
2077c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext(
2087c478bd9Sstevel@tonic-gate 	"pr(1) filter options specified without enabling pr(1) filter"));
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* add burst page information */
2117c478bd9Sstevel@tonic-gate 	papiAttributeListGetBoolean(attributes, NULL, "job-sheets", &banner);
2127c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "job-class", &class);
2137c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "job-name", &job_name);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	{
2167c478bd9Sstevel@tonic-gate 		char buf[128];
2177c478bd9Sstevel@tonic-gate 		/* burst page is enabled by default, add the title */
2187c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "%s%s%s",
2197c478bd9Sstevel@tonic-gate 			(job_name ? job_name : ""),
2207c478bd9Sstevel@tonic-gate 			(job_name && class ? "\\n#####\\n#####\\t\\t " : ""),
2217c478bd9Sstevel@tonic-gate 			(class ? class : ""));
2227c478bd9Sstevel@tonic-gate 		if (buf[0] != '\0') {
2237c478bd9Sstevel@tonic-gate 			if (r->title != NULL)
2247c478bd9Sstevel@tonic-gate 				free(r->title);
2257c478bd9Sstevel@tonic-gate 			r->title = strdup(buf);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	if (banner == 0) /* burst page is disabled via lp "option" */
2297c478bd9Sstevel@tonic-gate 		appendlist(&options, "nobanner");
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/* add "lp -o" options */
2327c478bd9Sstevel@tonic-gate 	attr = papiAttributeListFind(attributes, "lp-options");
2337c478bd9Sstevel@tonic-gate 	if ((attr != NULL) && (attr->type == PAPI_STRING) &&
2347c478bd9Sstevel@tonic-gate 	    (attr->values != NULL)) {
2357c478bd9Sstevel@tonic-gate 		int i;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		for (i = 0; attr->values[i] != NULL; i++)
2387c478bd9Sstevel@tonic-gate 			appendlist(&options, attr->values[i]->string);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (options != NULL) {
2427c478bd9Sstevel@tonic-gate 		if (r->options != NULL)
2437c478bd9Sstevel@tonic-gate 			free(r->options);
2447c478bd9Sstevel@tonic-gate 		r->options = sprintlist(options);
2457c478bd9Sstevel@tonic-gate 		freelist(options);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* Convert attribute "number-up" to mode group=n */
2497c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "number-up", &numberUp);
2507c478bd9Sstevel@tonic-gate 	if ((numberUp >= 2) && ((numberUp % 2) == 0)) {
2517c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "group=%d", numberUp);
2527c478bd9Sstevel@tonic-gate 		appendlist(&modes, buf);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Convert attribute "orientation-requested" to modes
2577c478bd9Sstevel@tonic-gate 	 * 'landscape', 'portrait', etc.
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL,
2607c478bd9Sstevel@tonic-gate 				    "orientation-requested", &orientation);
2617c478bd9Sstevel@tonic-gate 	if ((orientation >= 3) && (orientation <= 6)) {
2627c478bd9Sstevel@tonic-gate 		switch (orientation) {
2637c478bd9Sstevel@tonic-gate 			case 3:
2647c478bd9Sstevel@tonic-gate 			{
2657c478bd9Sstevel@tonic-gate 				/* 3 = portrait */
2667c478bd9Sstevel@tonic-gate 				appendlist(&modes, "portrait");
2677c478bd9Sstevel@tonic-gate 				break;
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 			case 4:
2717c478bd9Sstevel@tonic-gate 			{
2727c478bd9Sstevel@tonic-gate 				/* 4 = landscape */
2737c478bd9Sstevel@tonic-gate 				appendlist(&modes, "landscape");
2747c478bd9Sstevel@tonic-gate 				break;
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 			case 5:
2787c478bd9Sstevel@tonic-gate 			{
2797c478bd9Sstevel@tonic-gate 				/*
2807c478bd9Sstevel@tonic-gate 				 * 5 = reverse-landscape - not supported in
2817c478bd9Sstevel@tonic-gate 				 *    lpsched so just use 'landscape' for now
2827c478bd9Sstevel@tonic-gate 				 */
2837c478bd9Sstevel@tonic-gate 				appendlist(&modes, "landscape");
2847c478bd9Sstevel@tonic-gate 				break;
2857c478bd9Sstevel@tonic-gate 			}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			case 6:
2887c478bd9Sstevel@tonic-gate 			{
2897c478bd9Sstevel@tonic-gate 				/*
2907c478bd9Sstevel@tonic-gate 				 * 6 = reverse-portrait not supported in
2917c478bd9Sstevel@tonic-gate 				 *    lpsched so just use 'portrait' for now
2927c478bd9Sstevel@tonic-gate 				 */
2937c478bd9Sstevel@tonic-gate 				appendlist(&modes, "portrait");
2947c478bd9Sstevel@tonic-gate 				break;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 			default:
2987c478bd9Sstevel@tonic-gate 			{
2997c478bd9Sstevel@tonic-gate 				appendlist(&modes, "portrait");
3007c478bd9Sstevel@tonic-gate 				break;
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/* add "lp -y" modes */
3067c478bd9Sstevel@tonic-gate 	attr = papiAttributeListFind(attributes, "lp-modes");
3077c478bd9Sstevel@tonic-gate 	if ((attr != NULL) && (attr->type == PAPI_STRING) &&
3087c478bd9Sstevel@tonic-gate 	    (attr->values != NULL)) {
3097c478bd9Sstevel@tonic-gate 		int i;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		for (i = 0; attr->values[i] != NULL; i++)
3127c478bd9Sstevel@tonic-gate 			appendlist(&modes, attr->values[i]->string);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (modes != NULL) {
3167c478bd9Sstevel@tonic-gate 		if (r->modes == NULL)
3177c478bd9Sstevel@tonic-gate 			free(r->modes);
3187c478bd9Sstevel@tonic-gate 		r->modes = sprintlist(modes);
3197c478bd9Sstevel@tonic-gate 		freelist(modes);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Convert REQUEST->outcome (or R_REQUEST_* state) to the equivalent
3277c478bd9Sstevel@tonic-gate  * PAPI attribute representation.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate static void
3307c478bd9Sstevel@tonic-gate lpsched_request_outcome_to_attributes(papi_attribute_t ***attributes,
3317c478bd9Sstevel@tonic-gate 		unsigned short state)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	if (attributes == NULL)
3347c478bd9Sstevel@tonic-gate 		return;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (state & (RS_HELD|RS_ADMINHELD)) {
3377c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
3387c478bd9Sstevel@tonic-gate 			"job-state", 0x04);	/* held */
3397c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3407c478bd9Sstevel@tonic-gate 			"job-state-reasons", "job-hold-until-specified");
3417c478bd9Sstevel@tonic-gate 	} else if (state & RS_ACTIVE) {
3427c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
3437c478bd9Sstevel@tonic-gate 			"job-state", 0x05);
3447c478bd9Sstevel@tonic-gate 		if (state & RS_FILTERING)
3457c478bd9Sstevel@tonic-gate 			papiAttributeListAddString(attributes,
3467c478bd9Sstevel@tonic-gate 				PAPI_ATTR_REPLACE,
3477c478bd9Sstevel@tonic-gate 				"job-state-reasons", "job-transforming");
3487c478bd9Sstevel@tonic-gate 		else if (state & RS_PRINTING)
3497c478bd9Sstevel@tonic-gate 			papiAttributeListAddString(attributes,
3507c478bd9Sstevel@tonic-gate 				PAPI_ATTR_REPLACE,
3517c478bd9Sstevel@tonic-gate 				"job-state-reasons", "job-printing");
3527c478bd9Sstevel@tonic-gate 		else
3537c478bd9Sstevel@tonic-gate 			papiAttributeListAddString(attributes,
3547c478bd9Sstevel@tonic-gate 				PAPI_ATTR_REPLACE,
3557c478bd9Sstevel@tonic-gate 				"job-state-reasons", "job-processing");
3567c478bd9Sstevel@tonic-gate 	} else if (state & RS_CANCELLED) {
3577c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
3587c478bd9Sstevel@tonic-gate 			"job-state", 0x07);
3597c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3607c478bd9Sstevel@tonic-gate 			"job-state-reasons", "job-canceled-by-user");
3617c478bd9Sstevel@tonic-gate 	} else if (state & RS_PRINTED) {
3627c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
3637c478bd9Sstevel@tonic-gate 			"job-state", 0x09);
3647c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3657c478bd9Sstevel@tonic-gate 			"job-state-reasons", "job-complete");
3667c478bd9Sstevel@tonic-gate 	} else {
3677c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
3687c478bd9Sstevel@tonic-gate 			"job-state", 0x03);
3697c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3707c478bd9Sstevel@tonic-gate 			"job-state-reasons", "job-queued");
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3737c478bd9Sstevel@tonic-gate 				"job-hold-until",
3747c478bd9Sstevel@tonic-gate 		((state & RS_HELD) ? "indefinite" : "no-hold"));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Convert REQUEST structure to the equivalent PAPI attribute representation.
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate void
3817c478bd9Sstevel@tonic-gate lpsched_request_to_job_attributes(REQUEST *r, job_t *j)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	char *tmp;
3847c478bd9Sstevel@tonic-gate 	int i;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/* copies */
3877c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
3887c478bd9Sstevel@tonic-gate 				"copies", r->copies);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/* destination */
3917c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE, "printer-name",
3927c478bd9Sstevel@tonic-gate 				r->destination);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* file_list */
3957c478bd9Sstevel@tonic-gate 	addLPStrings(&j->attributes, PAPI_ATTR_REPLACE,
3967c478bd9Sstevel@tonic-gate 				"lpsched-files", r->file_list);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/* form */
3997c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE, "media", r->form);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* actions */
4027c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4037c478bd9Sstevel@tonic-gate 					"lpsched-actions", r->actions);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/* alert */
4067c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE, "lp-alert", r->alert);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/* options */
4097c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE,
4107c478bd9Sstevel@tonic-gate 					"lp-options", r->options);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	tmp = (((r->options != NULL) && (strstr(r->options, "nobanner")
4137c478bd9Sstevel@tonic-gate 		!= NULL)) ? "none" : "standard");
4147c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4157c478bd9Sstevel@tonic-gate 				"job-sheets", tmp);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	tmp = (((r->options != NULL) && (strstr(r->options, "duplex")
4187c478bd9Sstevel@tonic-gate 		!= NULL)) ? "two-sized" : "one-sided");
4197c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4207c478bd9Sstevel@tonic-gate 				"sides", tmp);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	i = (((r->options != NULL) && (strstr(r->options, "landscape")
4237c478bd9Sstevel@tonic-gate 		!= NULL)) ? 4 : 3);
4247c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4257c478bd9Sstevel@tonic-gate 				"orientation-requested", i);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/* priority (map 0-39 to 1-100) */
4287c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4297c478bd9Sstevel@tonic-gate 				"job-priority", (int)((r->priority + 1) * 2.5));
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/* pages */
4327c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE, "page-ranges", r->pages);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/* charset */
4357c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE, "lp-charset",
4367c478bd9Sstevel@tonic-gate 				r->charset);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/* modes */
4397c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE, "lp-modes", r->modes);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/* title */
4427c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE, "job-name", r->title);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* input_type */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/* user */
4477c478bd9Sstevel@tonic-gate 	addLPString(&j->attributes, PAPI_ATTR_REPLACE,
4487c478bd9Sstevel@tonic-gate 				"job-originating-user-name", r->user);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* outcome */
4517c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4527c478bd9Sstevel@tonic-gate 				"lpsched-outcome", r->outcome);
4537c478bd9Sstevel@tonic-gate 	lpsched_request_outcome_to_attributes(&j->attributes, r->outcome);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* version */
4567c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4577c478bd9Sstevel@tonic-gate 				"lpsched-version", r->version);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/* constants, (should be derived from options) */
4607c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4617c478bd9Sstevel@tonic-gate 				"number-up", 1);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4647c478bd9Sstevel@tonic-gate 				"multiple-document-handling",
4657c478bd9Sstevel@tonic-gate 				"seperate-documents-collated-copies");
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * Convert R_REQUEST_* results to the equivalent PAPI attribute representation.
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate void
472*45916cd2Sjpk job_status_to_attributes(job_t *job, char *req_id, char *user, char *slabel,
473*45916cd2Sjpk 		size_t size, time_t date, short state, char *destination,
474*45916cd2Sjpk 		char *form, char *charset, short rank, char *file)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
4777c478bd9Sstevel@tonic-gate 	char *p;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	addLPString(&job->attributes, PAPI_ATTR_REPLACE,
4807c478bd9Sstevel@tonic-gate 				"job-originating-user-name", user);
4817c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4827c478bd9Sstevel@tonic-gate 				"job-k-octets", size/1024);
4837c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4847c478bd9Sstevel@tonic-gate 				"job-octets", size);
4857c478bd9Sstevel@tonic-gate 	if ((p = strrchr(req_id, '-')) != NULL) {
4867c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4877c478bd9Sstevel@tonic-gate 				"job-id", atoi(++p));
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), "lpsched://%s/%d", destination, atoi(p));
4907c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4917c478bd9Sstevel@tonic-gate 				"job-uri", buf);
4927c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), "lpsched://%s", destination);
4937c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4947c478bd9Sstevel@tonic-gate 				"job-printer-uri", buf);
4957c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4967c478bd9Sstevel@tonic-gate 				"job-printer-up-time", time(NULL));
4977c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4987c478bd9Sstevel@tonic-gate 				"output-device-assigned", destination);
4997c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
5007c478bd9Sstevel@tonic-gate 				"printer-name", destination);
5017c478bd9Sstevel@tonic-gate 	addLPString(&job->attributes, PAPI_ATTR_REPLACE, "media", form);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	lpsched_request_outcome_to_attributes(&job->attributes, state);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5067c478bd9Sstevel@tonic-gate 				"time-at-creation", date);
5077c478bd9Sstevel@tonic-gate 	addLPString(&job->attributes, PAPI_ATTR_REPLACE,
5087c478bd9Sstevel@tonic-gate 				"lpsched-request-id", req_id);
5097c478bd9Sstevel@tonic-gate 	addLPString(&job->attributes, PAPI_ATTR_REPLACE,
5107c478bd9Sstevel@tonic-gate 				"lp-charset", charset);
5117c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5127c478bd9Sstevel@tonic-gate 				"lpsched-job-state", state);
5137c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5147c478bd9Sstevel@tonic-gate 				"number-of-intervening-jobs", rank - 1);
5157c478bd9Sstevel@tonic-gate 	addLPString(&job->attributes, PAPI_ATTR_REPLACE,
5167c478bd9Sstevel@tonic-gate 				"lpsched-file", file);
5177c478bd9Sstevel@tonic-gate 	addLPString(&job->attributes, PAPI_ATTR_EXCL,
5187c478bd9Sstevel@tonic-gate 				"job-name", file);
519*45916cd2Sjpk 	addLPString(&job->attributes, PAPI_ATTR_EXCL,
520*45916cd2Sjpk 				"tsol-sensitivity-label", slabel);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate void
5247c478bd9Sstevel@tonic-gate lpsched_read_job_configuration(service_t *svc, job_t *j, char *file)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	REQUEST *r;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if ((r = getrequest(file)) == NULL) {
5297c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to read job data: %s"),
5307c478bd9Sstevel@tonic-gate 			file);
5317c478bd9Sstevel@tonic-gate 		return;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
5357c478bd9Sstevel@tonic-gate }
536