xref: /illumos-gate/usr/src/cmd/lp/lib/papi/lpsched-jobs.c (revision e4fb8a5f)
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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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 /*
223d09a4feSsonam gupta - Sun Microsystems - Bangalore India  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <pwd.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /* lpsched include files */
357c478bd9Sstevel@tonic-gate #include "lp.h"
367c478bd9Sstevel@tonic-gate #include "requests.h"
377c478bd9Sstevel@tonic-gate #include "printers.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <papi_impl.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate papi_status_t
job_attributes_to_lpsched_request(papi_service_t svc,REQUEST * r,papi_attribute_t ** attributes)427c478bd9Sstevel@tonic-gate job_attributes_to_lpsched_request(papi_service_t svc, REQUEST *r,
437c478bd9Sstevel@tonic-gate 		papi_attribute_t **attributes)
447c478bd9Sstevel@tonic-gate {
45355b4669Sjacobs 	papi_status_t status = PAPI_OK;
467c478bd9Sstevel@tonic-gate 	papi_attribute_t *attr;
47355b4669Sjacobs 	papi_attribute_t **unmapped = NULL;
4843b9c050Sjacobs 	papi_attribute_t *tmp[2];
497c478bd9Sstevel@tonic-gate 	int i;
507c478bd9Sstevel@tonic-gate 	char *s;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	char **options = NULL;
537c478bd9Sstevel@tonic-gate 	char **modes = NULL;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	char pr_filter = 0;
567c478bd9Sstevel@tonic-gate 	char *pr_title = NULL;
577c478bd9Sstevel@tonic-gate 	int pr_width = -1;
587c478bd9Sstevel@tonic-gate 	int pr_indent = -1;
597c478bd9Sstevel@tonic-gate 	int numberUp = 0;
607c478bd9Sstevel@tonic-gate 	int orientation = 0;
61355b4669Sjacobs 	int lower = 0;
62355b4669Sjacobs 	int upper = 0;
637c478bd9Sstevel@tonic-gate 	char buf[256];
647c478bd9Sstevel@tonic-gate 	void *iterator = NULL;
65355b4669Sjacobs 	char *mapped_keys[] = { "copies", "document-format", "form",
66355b4669Sjacobs 			"job-class", "job-hold-until", "job-host", "job-name",
67355b4669Sjacobs 			"job-originating-user-name", "job-printer",
68355b4669Sjacobs 			"job-sheets", "lp-charset", "lp-modes", "number-up",
69355b4669Sjacobs 			"orienttation-requested", "page-ranges", "pr-filter",
7043b9c050Sjacobs 			"pr-indent", "pr-title", "pr-width", "job-priority",
710a44ef6dSjacobs 			"requesting-user-name", "job-originating-host-name",
720a44ef6dSjacobs 			NULL };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (attributes == NULL)
757c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
767c478bd9Sstevel@tonic-gate 
77355b4669Sjacobs 	/* replace the current destination */
78355b4669Sjacobs 	papiAttributeListGetLPString(attributes,
793d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-printer", &r->destination);
807c478bd9Sstevel@tonic-gate 
81355b4669Sjacobs 	/* set the copies.  We need at least 1 */
827c478bd9Sstevel@tonic-gate 	i = r->copies;
837c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "copies", &i);
847c478bd9Sstevel@tonic-gate 	if (i <= 0)
857c478bd9Sstevel@tonic-gate 		i = 1;
867c478bd9Sstevel@tonic-gate 	r->copies = i;
877c478bd9Sstevel@tonic-gate 
88355b4669Sjacobs 	/*
89355b4669Sjacobs 	 * set the priority.  PAPI/IPP uses 1-100, lpsched use 0-39, so we
90355b4669Sjacobs 	 * have to convert it.
91355b4669Sjacobs 	 */
9243b9c050Sjacobs 	if (papiAttributeListGetInteger(attributes, NULL, "job-priority", &i)
933d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    == PAPI_OK) {
947c478bd9Sstevel@tonic-gate 		if ((i < 1) || (i > 100))
957c478bd9Sstevel@tonic-gate 			i = 50;
9643b9c050Sjacobs 		i = 40 - (i / 2.5);
977c478bd9Sstevel@tonic-gate 		r->priority = i;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	if ((r->priority < 0) || (r->priority > 39))
1007c478bd9Sstevel@tonic-gate 		r->priority = 20;
1017c478bd9Sstevel@tonic-gate 
102355b4669Sjacobs 	/* set the requested form to print on */
103355b4669Sjacobs 	papiAttributeListGetLPString(attributes, "form", &r->form);
1047c478bd9Sstevel@tonic-gate 
105355b4669Sjacobs 	/* set the page range */
106*e4fb8a5fSToomas Soome 	memset(tmp, 0, sizeof (tmp));
107574e15a0Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
108574e15a0Sjacobs 	if (tmp[0] != NULL) {
109574e15a0Sjacobs 		char buf[BUFSIZ];
11043b9c050Sjacobs 
111574e15a0Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
112574e15a0Sjacobs 		if ((s = strchr(buf, '=')) != NULL)
11343b9c050Sjacobs 			r->pages = (char *)strdup(++s);
114574e15a0Sjacobs 	}
1157c478bd9Sstevel@tonic-gate 
116355b4669Sjacobs 	/*
117355b4669Sjacobs 	 * set the document format, converting to old format names as
118355b4669Sjacobs 	 * as needed.
119355b4669Sjacobs 	 */
1207c478bd9Sstevel@tonic-gate 	s = NULL;
1217c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "document-format", &s);
1227c478bd9Sstevel@tonic-gate 	if (s != NULL)
1237c478bd9Sstevel@tonic-gate 		r->input_type = strdup(mime_type_to_lp_type(s));
1247c478bd9Sstevel@tonic-gate 
125355b4669Sjacobs 
1267c478bd9Sstevel@tonic-gate 	/*
1277c478bd9Sstevel@tonic-gate 	 * If we don't have an owner, set one.
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	if (r->user == NULL) {
1307c478bd9Sstevel@tonic-gate 		uid_t uid = getuid();
1317c478bd9Sstevel@tonic-gate 		struct passwd *pw;
1327c478bd9Sstevel@tonic-gate 		char *user = "intruder";
1337c478bd9Sstevel@tonic-gate 		char *host = NULL;
1347c478bd9Sstevel@tonic-gate 		char buf[256];
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1377c478bd9Sstevel@tonic-gate 			user = pw->pw_name; /* default to the process owner */
1387c478bd9Sstevel@tonic-gate 
1393d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		papiAttributeListGetString(attributes, NULL,
1403d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-originating-host-name", &host);
1413d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		papiAttributeListGetString(attributes, NULL,
1423d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-host", &host);
1433d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		papiAttributeListGetString(attributes, NULL,
1443d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-originating-user-name", &user);
1453d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		papiAttributeListGetString(attributes, NULL,
1463d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "requesting-user-name", &user);
1473d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
1483d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		snprintf(buf, sizeof (buf), "%s%s%s", user,
1493d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    (host ? "@" : ""), (host ? host : ""));
1503d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		user = buf;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		r->user = strdup(user);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
155355b4669Sjacobs 	/* set any held state */
1567c478bd9Sstevel@tonic-gate 	s = NULL;
1577c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
1587c478bd9Sstevel@tonic-gate 	if (s != NULL) {
15943b9c050Sjacobs 		r->actions &= ~(ACT_SPECIAL); /* strip immediate/hold/resume */
16043b9c050Sjacobs 		if (strcmp(s, "resume") == 0)
1617c478bd9Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
16243b9c050Sjacobs 		else if ((strcmp(s, "immediate") == 0) ||
1633d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    (strcmp(s, "no-hold") == 0))
16443b9c050Sjacobs 			r->actions |= ACT_IMMEDIATE;
16543b9c050Sjacobs 		else if ((strcmp(s, "indefinite") == 0) ||
1663d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    (strcmp(s, "hold") == 0))
1677c478bd9Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
170355b4669Sjacobs 	/* set lp charset/printwheel */
171355b4669Sjacobs 	papiAttributeListGetLPString(attributes, "lp-charset", &r->charset);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* legacy pr(1) filter related garbage "lpr -p" */
1747c478bd9Sstevel@tonic-gate 	papiAttributeListGetBoolean(attributes, NULL, "pr-filter", &pr_filter);
1757c478bd9Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "pr-title", &pr_title);
1767c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &pr_width);
1777c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &pr_indent);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (pr_filter != 0) {
1807c478bd9Sstevel@tonic-gate 		char buf[128];
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		if (pr_title != NULL) {
1837c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "prtitle='%s'", pr_title);
1847c478bd9Sstevel@tonic-gate 			appendlist(&modes, buf);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		if (pr_width > 0) {
1887c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "prwidth=%d", pr_width);
1897c478bd9Sstevel@tonic-gate 			appendlist(&modes, buf);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		if (pr_indent > 0) {
1937c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "indent=%d", pr_indent);
1947c478bd9Sstevel@tonic-gate 			appendlist(&modes, buf);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 	} else if ((pr_title != NULL) || (pr_width >= 0) || (pr_indent >= 0))
1977c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext(
1987c478bd9Sstevel@tonic-gate 	"pr(1) filter options specified without enabling pr(1) filter"));
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* add burst page information */
201355b4669Sjacobs 	s = NULL;
202355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
2030a44ef6dSjacobs 	if ((s != NULL) && (strcasecmp(s, "none") != 0)) {
2047c478bd9Sstevel@tonic-gate 		char buf[128];
205355b4669Sjacobs 		char *class = NULL;
206355b4669Sjacobs 		char *job_name = NULL;
207355b4669Sjacobs 
208355b4669Sjacobs 		papiAttributeListGetLPString(attributes, "job-class", &class);
209355b4669Sjacobs 		papiAttributeListGetLPString(attributes, "job-name", &job_name);
210355b4669Sjacobs 
2117c478bd9Sstevel@tonic-gate 		/* burst page is enabled by default, add the title */
2127c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "%s%s%s",
2133d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    (job_name ? job_name : ""),
2143d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    (job_name && class ? "\\n#####\\n#####\\t\\t " : ""),
2153d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    (class ? class : ""));
2167c478bd9Sstevel@tonic-gate 		if (buf[0] != '\0') {
2177c478bd9Sstevel@tonic-gate 			if (r->title != NULL)
2187c478bd9Sstevel@tonic-gate 				free(r->title);
2197c478bd9Sstevel@tonic-gate 			r->title = strdup(buf);
2207c478bd9Sstevel@tonic-gate 		}
2217f79af0bSsonam gupta - Sun Microsystems - Bangalore India 	} else if ((s != NULL) && (strcasecmp(s, "none") == 0)) {
2227f79af0bSsonam gupta - Sun Microsystems - Bangalore India 		/* burst page is disabled via lp "option" */
2237c478bd9Sstevel@tonic-gate 		appendlist(&options, "nobanner");
2247f79af0bSsonam gupta - Sun Microsystems - Bangalore India 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/* Convert attribute "number-up" to mode group=n */
2277c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "number-up", &numberUp);
2287c478bd9Sstevel@tonic-gate 	if ((numberUp >= 2) && ((numberUp % 2) == 0)) {
2297c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "group=%d", numberUp);
2307c478bd9Sstevel@tonic-gate 		appendlist(&modes, buf);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * Convert attribute "orientation-requested" to modes
2357c478bd9Sstevel@tonic-gate 	 * 'landscape', 'portrait', etc.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL,
2383d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "orientation-requested", &orientation);
2397c478bd9Sstevel@tonic-gate 	if ((orientation >= 3) && (orientation <= 6)) {
2407c478bd9Sstevel@tonic-gate 		switch (orientation) {
241355b4669Sjacobs 		case 4:	/* landscape */
242355b4669Sjacobs 		case 5:	/* reverse-landscape, use landscape instead */
243355b4669Sjacobs 			appendlist(&modes, "landscape");
244355b4669Sjacobs 			break;
245355b4669Sjacobs 		case 3:	/* portrait */
246355b4669Sjacobs 		case 6: /* reverse-portrait, use portrait instead */
247355b4669Sjacobs 		default:
248355b4669Sjacobs 			appendlist(&modes, "portrait");
249355b4669Sjacobs 			break;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/* add "lp -y" modes */
2547c478bd9Sstevel@tonic-gate 	attr = papiAttributeListFind(attributes, "lp-modes");
2557c478bd9Sstevel@tonic-gate 	if ((attr != NULL) && (attr->type == PAPI_STRING) &&
2567c478bd9Sstevel@tonic-gate 	    (attr->values != NULL)) {
2577c478bd9Sstevel@tonic-gate 		int i;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		for (i = 0; attr->values[i] != NULL; i++)
2607c478bd9Sstevel@tonic-gate 			appendlist(&modes, attr->values[i]->string);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (modes != NULL) {
2647c478bd9Sstevel@tonic-gate 		if (r->modes == NULL)
2657c478bd9Sstevel@tonic-gate 			free(r->modes);
2667c478bd9Sstevel@tonic-gate 		r->modes = sprintlist(modes);
2677c478bd9Sstevel@tonic-gate 		freelist(modes);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
270355b4669Sjacobs 	/* add any unconsumed attributes to the "options" list */
271355b4669Sjacobs 	split_and_copy_attributes(mapped_keys, attributes, NULL, &unmapped);
272355b4669Sjacobs 	if (unmapped != NULL) {	/* convert them to lp options */
273355b4669Sjacobs 		char *buf = malloc(1024);
274355b4669Sjacobs 		ssize_t size = 1024;
275355b4669Sjacobs 
27643b9c050Sjacobs 		while (papiAttributeListToString(unmapped, " ", buf, size)
2773d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    != PAPI_OK) {
278355b4669Sjacobs 			size += 1024;
279355b4669Sjacobs 			buf = realloc(buf, size);
280355b4669Sjacobs 		}
281355b4669Sjacobs 		appendlist(&options, buf);
282355b4669Sjacobs 		free(buf);
283355b4669Sjacobs 		papiAttributeListFree(unmapped);
284355b4669Sjacobs 	}
285355b4669Sjacobs 
286355b4669Sjacobs 	if (options != NULL) {
287355b4669Sjacobs 		if (r->options != NULL)
288355b4669Sjacobs 			free(r->options);
289355b4669Sjacobs 		r->options = sprintlist(options);
290355b4669Sjacobs 		freelist(options);
291355b4669Sjacobs 	}
292355b4669Sjacobs 
2937c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * Convert REQUEST->outcome (or R_REQUEST_* state) to the equivalent
2987c478bd9Sstevel@tonic-gate  * PAPI attribute representation.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate static void
lpsched_request_outcome_to_attributes(papi_attribute_t *** attributes,unsigned short state)3017c478bd9Sstevel@tonic-gate lpsched_request_outcome_to_attributes(papi_attribute_t ***attributes,
3027c478bd9Sstevel@tonic-gate 		unsigned short state)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	if (attributes == NULL)
3057c478bd9Sstevel@tonic-gate 		return;
3067c478bd9Sstevel@tonic-gate 
307c389c7f8SGowtham Thommandra 	if (state & RS_NOTIFYING) {
3087c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
309c389c7f8SGowtham Thommandra 		    "job-state", 0x0800);   /* notifying user */
3107c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
311c389c7f8SGowtham Thommandra 		    "job-state-reasons", "job-notifying");
312c389c7f8SGowtham Thommandra 	} else if (state & RS_HELD) {
3137c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
314c389c7f8SGowtham Thommandra 		    "job-state", 0x0001);   /* held */
315c389c7f8SGowtham Thommandra 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
316c389c7f8SGowtham Thommandra 		    "job-state-reasons", "job-hold-until-specified");
3177c478bd9Sstevel@tonic-gate 	} else if (state & RS_CANCELLED) {
3187c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
319c389c7f8SGowtham Thommandra 		    "job-state", 0x0040);   /* job cancelled */
3207c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3213d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-state-reasons", "job-canceled-by-user");
3227c478bd9Sstevel@tonic-gate 	} else if (state & RS_PRINTED) {
3237c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
324c389c7f8SGowtham Thommandra 		    "job-state", 0x0010);   /* finished printing job */
3257c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3263d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-state-reasons", "job-complete");
327c389c7f8SGowtham Thommandra 	} else if (state & RS_PRINTING) {
328c389c7f8SGowtham Thommandra 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
329c389c7f8SGowtham Thommandra 		    "job-state", 0x0008);   /* printing job */
330c389c7f8SGowtham Thommandra 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
331c389c7f8SGowtham Thommandra 		    "job-state-reasons", "job-printing");
332c389c7f8SGowtham Thommandra 	} else if (state & RS_ADMINHELD) {
333c389c7f8SGowtham Thommandra 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
334c389c7f8SGowtham Thommandra 		    "job-state", 0x2000);   /* held by admin */
335c389c7f8SGowtham Thommandra 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
336c389c7f8SGowtham Thommandra 		    "job-state-reasons", "job-hold-until-specified");
337c389c7f8SGowtham Thommandra 	} else if (state & RS_FILTERED) {
338c389c7f8SGowtham Thommandra 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
339c389c7f8SGowtham Thommandra 		    "job-state", 0x0004);   /* filtered */
340c389c7f8SGowtham Thommandra 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
341c389c7f8SGowtham Thommandra 		    "job-state-reasons", "job-filtered");
342c389c7f8SGowtham Thommandra 	} else if (state & RS_CHANGING) {
343c389c7f8SGowtham Thommandra 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
344c389c7f8SGowtham Thommandra 		    "job-state", 0x0020);   /* job held for changing */
345c389c7f8SGowtham Thommandra 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
346c389c7f8SGowtham Thommandra 		    "job-state-reasons", "job-held-for-change");
347c389c7f8SGowtham Thommandra 	} else if (state & RS_FILTERING) {
348c389c7f8SGowtham Thommandra 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
349c389c7f8SGowtham Thommandra 		    "job-state", 0x0002);   /* being filtered */
350c389c7f8SGowtham Thommandra 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
351c389c7f8SGowtham Thommandra 		    "job-state-reasons", "job-being-filtered");
3527c478bd9Sstevel@tonic-gate 	} else {
3537c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
354c389c7f8SGowtham Thommandra 		    "job-state", 0x4000);   /* else */
3557c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3563d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-state-reasons", "job-queued");
3577c478bd9Sstevel@tonic-gate 	}
358c389c7f8SGowtham Thommandra 
359c389c7f8SGowtham Thommandra 
360c389c7f8SGowtham Thommandra 
3617c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3623d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-hold-until",
3633d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    ((state & RS_HELD) ? "indefinite" : "no-hold"));
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3660a44ef6dSjacobs /*
3670a44ef6dSjacobs  * convert user[@host] to papi attributes
3680a44ef6dSjacobs  */
3690a44ef6dSjacobs static void
lpsched_user_to_job_attributes(papi_attribute_t *** list,char * user)3700a44ef6dSjacobs lpsched_user_to_job_attributes(papi_attribute_t ***list, char *user)
3710a44ef6dSjacobs {
372*e4fb8a5fSToomas Soome 	if ((list != NULL) && (user != NULL) && (user[0] != '\0')) {
3730a44ef6dSjacobs 		char *host = strrchr(user, '@');
3740a44ef6dSjacobs 
3750a44ef6dSjacobs 		if (host != NULL) {
376*e4fb8a5fSToomas Soome 			*host = '\0';
3770a44ef6dSjacobs 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
3783d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			    "job-originating-user-name", user);
3790a44ef6dSjacobs 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
3803d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			    "job-originating-host-name", host + 1);
3810a44ef6dSjacobs 			*host = '@';
3820a44ef6dSjacobs 		} else
3830a44ef6dSjacobs 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
3843d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			    "job-originating-user-name", user);
3850a44ef6dSjacobs 	}
3860a44ef6dSjacobs }
3870a44ef6dSjacobs 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Convert REQUEST structure to the equivalent PAPI attribute representation.
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate void
lpsched_request_to_job_attributes(REQUEST * r,job_t * j)3927c478bd9Sstevel@tonic-gate lpsched_request_to_job_attributes(REQUEST *r, job_t *j)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	char *tmp;
3957c478bd9Sstevel@tonic-gate 	int i;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* copies */
3987c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
3993d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "copies", r->copies);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* destination */
402355b4669Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4033d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "printer-name", r->destination);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/* form */
406355b4669Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4073d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "form", r->form);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/* options */
410355b4669Sjacobs 	papiAttributeListFromString(&j->attributes, PAPI_ATTR_APPEND,
4113d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    r->options);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	tmp = (((r->options != NULL) && (strstr(r->options, "nobanner")
4143d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    != NULL)) ? "none" : "standard");
4157c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4163d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-sheets", tmp);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	tmp = (((r->options != NULL) && (strstr(r->options, "duplex")
4193d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    != NULL)) ? "two-sized" : "one-sided");
4207c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4213d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "sides", tmp);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	i = (((r->options != NULL) && (strstr(r->options, "landscape")
4243d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    != NULL)) ? 4 : 3);
4257c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4263d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "orientation-requested", i);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* priority (map 0-39 to 1-100) */
4297c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4303d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-priority",
4313d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    (int)(100 - (r->priority * 2.5)));
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/* pages */
434355b4669Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4353d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "page-ranges", r->pages);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/* charset */
438355b4669Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4393d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "lp-charset", r->charset);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/* modes */
442355b4669Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4433d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "lp-modes", r->modes);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/* title */
446355b4669Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4473d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-name", r->title);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/* input_type */
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/* user */
4520a44ef6dSjacobs 	lpsched_user_to_job_attributes(&j->attributes, r->user);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/* outcome */
4557c478bd9Sstevel@tonic-gate 	lpsched_request_outcome_to_attributes(&j->attributes, r->outcome);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* constants, (should be derived from options) */
4587c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4593d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "number-up", 1);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4623d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "multiple-document-handling",
4633d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "seperate-documents-collated-copies");
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * Convert R_REQUEST_* results to the equivalent PAPI attribute representation.
4687c478bd9Sstevel@tonic-gate  */
4697c478bd9Sstevel@tonic-gate void
job_status_to_attributes(job_t * job,char * req_id,char * user,char * slabel,size_t size,time_t date,short state,char * destination,char * form,char * charset,short rank,char * file)47045916cd2Sjpk job_status_to_attributes(job_t *job, char *req_id, char *user, char *slabel,
47145916cd2Sjpk 		size_t size, time_t date, short state, char *destination,
47245916cd2Sjpk 		char *form, char *charset, short rank, char *file)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
4757c478bd9Sstevel@tonic-gate 	char *p;
4767c478bd9Sstevel@tonic-gate 
4770a44ef6dSjacobs 	lpsched_user_to_job_attributes(&job->attributes, user);
4787c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4793d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-k-octets", size/1024);
4807c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4813d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-octets", size);
4827c478bd9Sstevel@tonic-gate 	if ((p = strrchr(req_id, '-')) != NULL) {
4837c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4843d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-id", atoi(++p));
4857c478bd9Sstevel@tonic-gate 	}
486355b4669Sjacobs 	snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s/%d",
4873d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    destination, atoi(p));
4887c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4893d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-uri", buf);
490355b4669Sjacobs 	snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s",
4913d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    destination);
4927c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4933d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-printer-uri", buf);
4947c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4953d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-printer-up-time", time(NULL));
4967c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4973d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "output-device-assigned", destination);
4987c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4993d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "printer-name", destination);
500355b4669Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5013d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "form", 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,
5063d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "time-at-creation", date);
507355b4669Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5083d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "lpsched-request-id", req_id);
509355b4669Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5103d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "lp-charset", charset);
5117c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5123d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "lpsched-job-state", state);
5137c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5143d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "number-of-intervening-jobs", rank - 1);
515355b4669Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5163d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "lpsched-file", file);
517355b4669Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL,
5183d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "job-name", file);
519355b4669Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL,
5203d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "tsol-sensitivity-label", slabel);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate void
lpsched_read_job_configuration(service_t * svc,job_t * j,char * file)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"),
5303d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    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