xref: /onnv-gate/usr/src/cmd/lp/lib/papi/lpsched-jobs.c (revision 9257:c09c7b116816)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229152SSonam.Gupta@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*LINTLIBRARY*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <pwd.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /* lpsched include files */
350Sstevel@tonic-gate #include "lp.h"
360Sstevel@tonic-gate #include "requests.h"
370Sstevel@tonic-gate #include "printers.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <papi_impl.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate papi_status_t
job_attributes_to_lpsched_request(papi_service_t svc,REQUEST * r,papi_attribute_t ** attributes)420Sstevel@tonic-gate job_attributes_to_lpsched_request(papi_service_t svc, REQUEST *r,
430Sstevel@tonic-gate 		papi_attribute_t **attributes)
440Sstevel@tonic-gate {
452264Sjacobs 	papi_status_t status = PAPI_OK;
460Sstevel@tonic-gate 	papi_attribute_t *attr;
472264Sjacobs 	papi_attribute_t **unmapped = NULL;
486725Sjacobs 	papi_attribute_t *tmp[2];
490Sstevel@tonic-gate 	int i;
500Sstevel@tonic-gate 	char *s;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	char **options = NULL;
530Sstevel@tonic-gate 	char **modes = NULL;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	char pr_filter = 0;
560Sstevel@tonic-gate 	char *pr_title = NULL;
570Sstevel@tonic-gate 	int pr_width = -1;
580Sstevel@tonic-gate 	int pr_indent = -1;
590Sstevel@tonic-gate 	int numberUp = 0;
600Sstevel@tonic-gate 	int orientation = 0;
612264Sjacobs 	int lower = 0;
622264Sjacobs 	int upper = 0;
630Sstevel@tonic-gate 	char buf[256];
640Sstevel@tonic-gate 	void *iterator = NULL;
652264Sjacobs 	char *mapped_keys[] = { "copies", "document-format", "form",
662264Sjacobs 			"job-class", "job-hold-until", "job-host", "job-name",
672264Sjacobs 			"job-originating-user-name", "job-printer",
682264Sjacobs 			"job-sheets", "lp-charset", "lp-modes", "number-up",
692264Sjacobs 			"orienttation-requested", "page-ranges", "pr-filter",
706725Sjacobs 			"pr-indent", "pr-title", "pr-width", "job-priority",
713125Sjacobs 			"requesting-user-name", "job-originating-host-name",
723125Sjacobs 			NULL };
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (attributes == NULL)
750Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
760Sstevel@tonic-gate 
772264Sjacobs 	/* replace the current destination */
782264Sjacobs 	papiAttributeListGetLPString(attributes,
799152SSonam.Gupta@Sun.COM 	    "job-printer", &r->destination);
800Sstevel@tonic-gate 
812264Sjacobs 	/* set the copies.  We need at least 1 */
820Sstevel@tonic-gate 	i = r->copies;
830Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "copies", &i);
840Sstevel@tonic-gate 	if (i <= 0)
850Sstevel@tonic-gate 		i = 1;
860Sstevel@tonic-gate 	r->copies = i;
870Sstevel@tonic-gate 
882264Sjacobs 	/*
892264Sjacobs 	 * set the priority.  PAPI/IPP uses 1-100, lpsched use 0-39, so we
902264Sjacobs 	 * have to convert it.
912264Sjacobs 	 */
926725Sjacobs 	if (papiAttributeListGetInteger(attributes, NULL, "job-priority", &i)
939152SSonam.Gupta@Sun.COM 	    == PAPI_OK) {
940Sstevel@tonic-gate 		if ((i < 1) || (i > 100))
950Sstevel@tonic-gate 			i = 50;
966725Sjacobs 		i = 40 - (i / 2.5);
970Sstevel@tonic-gate 		r->priority = i;
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	if ((r->priority < 0) || (r->priority > 39))
1000Sstevel@tonic-gate 		r->priority = 20;
1010Sstevel@tonic-gate 
1022264Sjacobs 	/* set the requested form to print on */
1032264Sjacobs 	papiAttributeListGetLPString(attributes, "form", &r->form);
1040Sstevel@tonic-gate 
1052264Sjacobs 	/* set the page range */
1066728Sjacobs 	memset(tmp, NULL, sizeof (tmp));
1076728Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
1086728Sjacobs 	if (tmp[0] != NULL) {
1096728Sjacobs 		char buf[BUFSIZ];
1106725Sjacobs 
1116728Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
1126728Sjacobs 		if ((s = strchr(buf, '=')) != NULL)
1136725Sjacobs 			r->pages = (char *)strdup(++s);
1146728Sjacobs 	}
1150Sstevel@tonic-gate 
1162264Sjacobs 	/*
1172264Sjacobs 	 * set the document format, converting to old format names as
1182264Sjacobs 	 * as needed.
1192264Sjacobs 	 */
1200Sstevel@tonic-gate 	s = NULL;
1210Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "document-format", &s);
1220Sstevel@tonic-gate 	if (s != NULL)
1230Sstevel@tonic-gate 		r->input_type = strdup(mime_type_to_lp_type(s));
1240Sstevel@tonic-gate 
1252264Sjacobs 
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * If we don't have an owner, set one.
1280Sstevel@tonic-gate 	 */
1290Sstevel@tonic-gate 	if (r->user == NULL) {
1300Sstevel@tonic-gate 		uid_t uid = getuid();
1310Sstevel@tonic-gate 		struct passwd *pw;
1320Sstevel@tonic-gate 		char *user = "intruder";
1330Sstevel@tonic-gate 		char *host = NULL;
1340Sstevel@tonic-gate 		char buf[256];
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1370Sstevel@tonic-gate 			user = pw->pw_name; /* default to the process owner */
1380Sstevel@tonic-gate 
1399152SSonam.Gupta@Sun.COM 		papiAttributeListGetString(attributes, NULL,
1409152SSonam.Gupta@Sun.COM 		    "job-originating-host-name", &host);
1419152SSonam.Gupta@Sun.COM 		papiAttributeListGetString(attributes, NULL,
1429152SSonam.Gupta@Sun.COM 		    "job-host", &host);
1439152SSonam.Gupta@Sun.COM 		papiAttributeListGetString(attributes, NULL,
1449152SSonam.Gupta@Sun.COM 		    "job-originating-user-name", &user);
1459152SSonam.Gupta@Sun.COM 		papiAttributeListGetString(attributes, NULL,
1469152SSonam.Gupta@Sun.COM 		    "requesting-user-name", &user);
1470Sstevel@tonic-gate 
1489152SSonam.Gupta@Sun.COM 		snprintf(buf, sizeof (buf), "%s%s%s", user,
1499152SSonam.Gupta@Sun.COM 		    (host ? "@" : ""), (host ? host : ""));
1509152SSonam.Gupta@Sun.COM 		user = buf;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		r->user = strdup(user);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1552264Sjacobs 	/* set any held state */
1560Sstevel@tonic-gate 	s = NULL;
1570Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
1580Sstevel@tonic-gate 	if (s != NULL) {
1596725Sjacobs 		r->actions &= ~(ACT_SPECIAL); /* strip immediate/hold/resume */
1606725Sjacobs 		if (strcmp(s, "resume") == 0)
1616725Sjacobs 			r->actions |= ACT_RESUME;
1626725Sjacobs 		else if ((strcmp(s, "immediate") == 0) ||
1639152SSonam.Gupta@Sun.COM 		    (strcmp(s, "no-hold") == 0))
1646725Sjacobs 			r->actions |= ACT_IMMEDIATE;
1656725Sjacobs 		else if ((strcmp(s, "indefinite") == 0) ||
1669152SSonam.Gupta@Sun.COM 		    (strcmp(s, "hold") == 0))
1670Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1702264Sjacobs 	/* set lp charset/printwheel */
1712264Sjacobs 	papiAttributeListGetLPString(attributes, "lp-charset", &r->charset);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/* legacy pr(1) filter related garbage "lpr -p" */
1740Sstevel@tonic-gate 	papiAttributeListGetBoolean(attributes, NULL, "pr-filter", &pr_filter);
1750Sstevel@tonic-gate 	papiAttributeListGetString(attributes, NULL, "pr-title", &pr_title);
1760Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &pr_width);
1770Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &pr_indent);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (pr_filter != 0) {
1800Sstevel@tonic-gate 		char buf[128];
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		if (pr_title != NULL) {
1830Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "prtitle='%s'", pr_title);
1840Sstevel@tonic-gate 			appendlist(&modes, buf);
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		if (pr_width > 0) {
1880Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "prwidth=%d", pr_width);
1890Sstevel@tonic-gate 			appendlist(&modes, buf);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		if (pr_indent > 0) {
1930Sstevel@tonic-gate 			snprintf(buf, sizeof (buf), "indent=%d", pr_indent);
1940Sstevel@tonic-gate 			appendlist(&modes, buf);
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 	} else if ((pr_title != NULL) || (pr_width >= 0) || (pr_indent >= 0))
1970Sstevel@tonic-gate 		detailed_error(svc, gettext(
1980Sstevel@tonic-gate 	"pr(1) filter options specified without enabling pr(1) filter"));
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/* add burst page information */
2012264Sjacobs 	s = NULL;
2022264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
2033125Sjacobs 	if ((s != NULL) && (strcasecmp(s, "none") != 0)) {
2042264Sjacobs 		char buf[128];
2052264Sjacobs 		char *class = NULL;
2062264Sjacobs 		char *job_name = NULL;
2070Sstevel@tonic-gate 
2082264Sjacobs 		papiAttributeListGetLPString(attributes, "job-class", &class);
2092264Sjacobs 		papiAttributeListGetLPString(attributes, "job-name", &job_name);
2102264Sjacobs 
2110Sstevel@tonic-gate 		/* burst page is enabled by default, add the title */
2120Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "%s%s%s",
2139152SSonam.Gupta@Sun.COM 		    (job_name ? job_name : ""),
2149152SSonam.Gupta@Sun.COM 		    (job_name && class ? "\\n#####\\n#####\\t\\t " : ""),
2159152SSonam.Gupta@Sun.COM 		    (class ? class : ""));
2160Sstevel@tonic-gate 		if (buf[0] != '\0') {
2170Sstevel@tonic-gate 			if (r->title != NULL)
2180Sstevel@tonic-gate 				free(r->title);
2190Sstevel@tonic-gate 			r->title = strdup(buf);
2200Sstevel@tonic-gate 		}
2218114SSonam.Gupta@Sun.COM 	} else if ((s != NULL) && (strcasecmp(s, "none") == 0)) {
2228114SSonam.Gupta@Sun.COM 		/* burst page is disabled via lp "option" */
2230Sstevel@tonic-gate 		appendlist(&options, "nobanner");
2248114SSonam.Gupta@Sun.COM 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/* Convert attribute "number-up" to mode group=n */
2270Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL, "number-up", &numberUp);
2280Sstevel@tonic-gate 	if ((numberUp >= 2) && ((numberUp % 2) == 0)) {
2290Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "group=%d", numberUp);
2300Sstevel@tonic-gate 		appendlist(&modes, buf);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * Convert attribute "orientation-requested" to modes
2350Sstevel@tonic-gate 	 * 'landscape', 'portrait', etc.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	papiAttributeListGetInteger(attributes, NULL,
2389152SSonam.Gupta@Sun.COM 	    "orientation-requested", &orientation);
2390Sstevel@tonic-gate 	if ((orientation >= 3) && (orientation <= 6)) {
2400Sstevel@tonic-gate 		switch (orientation) {
2412264Sjacobs 		case 4:	/* landscape */
2422264Sjacobs 		case 5:	/* reverse-landscape, use landscape instead */
2432264Sjacobs 			appendlist(&modes, "landscape");
2442264Sjacobs 			break;
2452264Sjacobs 		case 3:	/* portrait */
2462264Sjacobs 		case 6: /* reverse-portrait, use portrait instead */
2472264Sjacobs 		default:
2482264Sjacobs 			appendlist(&modes, "portrait");
2492264Sjacobs 			break;
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/* add "lp -y" modes */
2540Sstevel@tonic-gate 	attr = papiAttributeListFind(attributes, "lp-modes");
2550Sstevel@tonic-gate 	if ((attr != NULL) && (attr->type == PAPI_STRING) &&
2560Sstevel@tonic-gate 	    (attr->values != NULL)) {
2570Sstevel@tonic-gate 		int i;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		for (i = 0; attr->values[i] != NULL; i++)
2600Sstevel@tonic-gate 			appendlist(&modes, attr->values[i]->string);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if (modes != NULL) {
2640Sstevel@tonic-gate 		if (r->modes == NULL)
2650Sstevel@tonic-gate 			free(r->modes);
2660Sstevel@tonic-gate 		r->modes = sprintlist(modes);
2670Sstevel@tonic-gate 		freelist(modes);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2702264Sjacobs 	/* add any unconsumed attributes to the "options" list */
2712264Sjacobs 	split_and_copy_attributes(mapped_keys, attributes, NULL, &unmapped);
2722264Sjacobs 	if (unmapped != NULL) {	/* convert them to lp options */
2732264Sjacobs 		char *buf = malloc(1024);
2742264Sjacobs 		ssize_t size = 1024;
2752264Sjacobs 
2766725Sjacobs 		while (papiAttributeListToString(unmapped, " ", buf, size)
2779152SSonam.Gupta@Sun.COM 		    != PAPI_OK) {
2782264Sjacobs 			size += 1024;
2792264Sjacobs 			buf = realloc(buf, size);
2802264Sjacobs 		}
2812264Sjacobs 		appendlist(&options, buf);
2822264Sjacobs 		free(buf);
2832264Sjacobs 		papiAttributeListFree(unmapped);
2842264Sjacobs 	}
2852264Sjacobs 
2862264Sjacobs 	if (options != NULL) {
2872264Sjacobs 		if (r->options != NULL)
2882264Sjacobs 			free(r->options);
2892264Sjacobs 		r->options = sprintlist(options);
2902264Sjacobs 		freelist(options);
2912264Sjacobs 	}
2922264Sjacobs 
2930Sstevel@tonic-gate 	return (PAPI_OK);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * Convert REQUEST->outcome (or R_REQUEST_* state) to the equivalent
2980Sstevel@tonic-gate  * PAPI attribute representation.
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate static void
lpsched_request_outcome_to_attributes(papi_attribute_t *** attributes,unsigned short state)3010Sstevel@tonic-gate lpsched_request_outcome_to_attributes(papi_attribute_t ***attributes,
3020Sstevel@tonic-gate 		unsigned short state)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	if (attributes == NULL)
3050Sstevel@tonic-gate 		return;
3060Sstevel@tonic-gate 
307*9257SGowtham.Thommandra@Sun.COM 	if (state & RS_NOTIFYING) {
3080Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
309*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0800);   /* notifying user */
310*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
311*9257SGowtham.Thommandra@Sun.COM 		    "job-state-reasons", "job-notifying");
312*9257SGowtham.Thommandra@Sun.COM 	} else if (state & RS_HELD) {
313*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
314*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0001);   /* held */
3150Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3169152SSonam.Gupta@Sun.COM 		    "job-state-reasons", "job-hold-until-specified");
3170Sstevel@tonic-gate 	} else if (state & RS_CANCELLED) {
3180Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
319*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0040);   /* job cancelled */
3200Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3219152SSonam.Gupta@Sun.COM 		    "job-state-reasons", "job-canceled-by-user");
3220Sstevel@tonic-gate 	} else if (state & RS_PRINTED) {
3230Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
324*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0010);   /* finished printing job */
3250Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3269152SSonam.Gupta@Sun.COM 		    "job-state-reasons", "job-complete");
327*9257SGowtham.Thommandra@Sun.COM 	} else if (state & RS_PRINTING) {
328*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
329*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0008);   /* printing job */
330*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
331*9257SGowtham.Thommandra@Sun.COM 		    "job-state-reasons", "job-printing");
332*9257SGowtham.Thommandra@Sun.COM 	} else if (state & RS_ADMINHELD) {
333*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
334*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x2000);   /* held by admin */
335*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
336*9257SGowtham.Thommandra@Sun.COM 		    "job-state-reasons", "job-hold-until-specified");
337*9257SGowtham.Thommandra@Sun.COM 	} else if (state & RS_FILTERED) {
338*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
339*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0004);   /* filtered */
340*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
341*9257SGowtham.Thommandra@Sun.COM 		    "job-state-reasons", "job-filtered");
342*9257SGowtham.Thommandra@Sun.COM 	} else if (state & RS_CHANGING) {
343*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
344*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0020);   /* job held for changing */
345*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
346*9257SGowtham.Thommandra@Sun.COM 		    "job-state-reasons", "job-held-for-change");
347*9257SGowtham.Thommandra@Sun.COM 	} else if (state & RS_FILTERING) {
348*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
349*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x0002);   /* being filtered */
350*9257SGowtham.Thommandra@Sun.COM 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
351*9257SGowtham.Thommandra@Sun.COM 		    "job-state-reasons", "job-being-filtered");
3520Sstevel@tonic-gate 	} else {
3530Sstevel@tonic-gate 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
354*9257SGowtham.Thommandra@Sun.COM 		    "job-state", 0x4000);   /* else */
3550Sstevel@tonic-gate 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3569152SSonam.Gupta@Sun.COM 		    "job-state-reasons", "job-queued");
3570Sstevel@tonic-gate 	}
358*9257SGowtham.Thommandra@Sun.COM 
359*9257SGowtham.Thommandra@Sun.COM 
360*9257SGowtham.Thommandra@Sun.COM 
3610Sstevel@tonic-gate 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
3629152SSonam.Gupta@Sun.COM 	    "job-hold-until",
3639152SSonam.Gupta@Sun.COM 	    ((state & RS_HELD) ? "indefinite" : "no-hold"));
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3673125Sjacobs  * convert user[@host] to papi attributes
3683125Sjacobs  */
3693125Sjacobs static void
lpsched_user_to_job_attributes(papi_attribute_t *** list,char * user)3703125Sjacobs lpsched_user_to_job_attributes(papi_attribute_t ***list, char *user)
3713125Sjacobs {
3723125Sjacobs 	if ((list != NULL) && (user != NULL) && (user[0] != NULL)) {
3733125Sjacobs 		char *host = strrchr(user, '@');
3743125Sjacobs 
3753125Sjacobs 		if (host != NULL) {
3763125Sjacobs 			*host = NULL;
3773125Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
3789152SSonam.Gupta@Sun.COM 			    "job-originating-user-name", user);
3793125Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
3809152SSonam.Gupta@Sun.COM 			    "job-originating-host-name", host + 1);
3813125Sjacobs 			*host = '@';
3823125Sjacobs 		} else
3833125Sjacobs 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
3849152SSonam.Gupta@Sun.COM 			    "job-originating-user-name", user);
3853125Sjacobs 	}
3863125Sjacobs }
3873125Sjacobs 
3883125Sjacobs /*
3890Sstevel@tonic-gate  * Convert REQUEST structure to the equivalent PAPI attribute representation.
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate void
lpsched_request_to_job_attributes(REQUEST * r,job_t * j)3920Sstevel@tonic-gate lpsched_request_to_job_attributes(REQUEST *r, job_t *j)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	char *tmp;
3950Sstevel@tonic-gate 	int i;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/* copies */
3980Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
3999152SSonam.Gupta@Sun.COM 	    "copies", r->copies);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	/* destination */
4022264Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4039152SSonam.Gupta@Sun.COM 	    "printer-name", r->destination);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/* form */
4062264Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4079152SSonam.Gupta@Sun.COM 	    "form", r->form);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/* options */
4102264Sjacobs 	papiAttributeListFromString(&j->attributes, PAPI_ATTR_APPEND,
4119152SSonam.Gupta@Sun.COM 	    r->options);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	tmp = (((r->options != NULL) && (strstr(r->options, "nobanner")
4149152SSonam.Gupta@Sun.COM 	    != NULL)) ? "none" : "standard");
4150Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4169152SSonam.Gupta@Sun.COM 	    "job-sheets", tmp);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	tmp = (((r->options != NULL) && (strstr(r->options, "duplex")
4199152SSonam.Gupta@Sun.COM 	    != NULL)) ? "two-sized" : "one-sided");
4200Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4219152SSonam.Gupta@Sun.COM 	    "sides", tmp);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	i = (((r->options != NULL) && (strstr(r->options, "landscape")
4249152SSonam.Gupta@Sun.COM 	    != NULL)) ? 4 : 3);
4250Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4269152SSonam.Gupta@Sun.COM 	    "orientation-requested", i);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/* priority (map 0-39 to 1-100) */
4290Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4309152SSonam.Gupta@Sun.COM 	    "job-priority",
4319152SSonam.Gupta@Sun.COM 	    (int)(100 - (r->priority * 2.5)));
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/* pages */
4342264Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4359152SSonam.Gupta@Sun.COM 	    "page-ranges", r->pages);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/* charset */
4382264Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4399152SSonam.Gupta@Sun.COM 	    "lp-charset", r->charset);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/* modes */
4422264Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4439152SSonam.Gupta@Sun.COM 	    "lp-modes", r->modes);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/* title */
4462264Sjacobs 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
4479152SSonam.Gupta@Sun.COM 	    "job-name", r->title);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/* input_type */
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	/* user */
4523125Sjacobs 	lpsched_user_to_job_attributes(&j->attributes, r->user);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/* outcome */
4550Sstevel@tonic-gate 	lpsched_request_outcome_to_attributes(&j->attributes, r->outcome);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/* constants, (should be derived from options) */
4580Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4599152SSonam.Gupta@Sun.COM 	    "number-up", 1);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4629152SSonam.Gupta@Sun.COM 	    "multiple-document-handling",
4639152SSonam.Gupta@Sun.COM 	    "seperate-documents-collated-copies");
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate  * Convert R_REQUEST_* results to the equivalent PAPI attribute representation.
4680Sstevel@tonic-gate  */
4690Sstevel@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)4701676Sjpk job_status_to_attributes(job_t *job, char *req_id, char *user, char *slabel,
4711676Sjpk 		size_t size, time_t date, short state, char *destination,
4721676Sjpk 		char *form, char *charset, short rank, char *file)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	char buf[BUFSIZ];
4750Sstevel@tonic-gate 	char *p;
4760Sstevel@tonic-gate 
4773125Sjacobs 	lpsched_user_to_job_attributes(&job->attributes, user);
4780Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4799152SSonam.Gupta@Sun.COM 	    "job-k-octets", size/1024);
4800Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4819152SSonam.Gupta@Sun.COM 	    "job-octets", size);
4820Sstevel@tonic-gate 	if ((p = strrchr(req_id, '-')) != NULL) {
4830Sstevel@tonic-gate 		papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4849152SSonam.Gupta@Sun.COM 		    "job-id", atoi(++p));
4850Sstevel@tonic-gate 	}
4862264Sjacobs 	snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s/%d",
4879152SSonam.Gupta@Sun.COM 	    destination, atoi(p));
4880Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4899152SSonam.Gupta@Sun.COM 	    "job-uri", buf);
4902264Sjacobs 	snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s",
4919152SSonam.Gupta@Sun.COM 	    destination);
4920Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4939152SSonam.Gupta@Sun.COM 	    "job-printer-uri", buf);
4940Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
4959152SSonam.Gupta@Sun.COM 	    "job-printer-up-time", time(NULL));
4960Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4979152SSonam.Gupta@Sun.COM 	    "output-device-assigned", destination);
4980Sstevel@tonic-gate 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
4999152SSonam.Gupta@Sun.COM 	    "printer-name", destination);
5002264Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5019152SSonam.Gupta@Sun.COM 	    "form", form);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	lpsched_request_outcome_to_attributes(&job->attributes, state);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5069152SSonam.Gupta@Sun.COM 	    "time-at-creation", date);
5072264Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5089152SSonam.Gupta@Sun.COM 	    "lpsched-request-id", req_id);
5092264Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5109152SSonam.Gupta@Sun.COM 	    "lp-charset", charset);
5110Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5129152SSonam.Gupta@Sun.COM 	    "lpsched-job-state", state);
5130Sstevel@tonic-gate 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
5149152SSonam.Gupta@Sun.COM 	    "number-of-intervening-jobs", rank - 1);
5152264Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
5169152SSonam.Gupta@Sun.COM 	    "lpsched-file", file);
5172264Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL,
5189152SSonam.Gupta@Sun.COM 	    "job-name", file);
5192264Sjacobs 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL,
5209152SSonam.Gupta@Sun.COM 	    "tsol-sensitivity-label", slabel);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate void
lpsched_read_job_configuration(service_t * svc,job_t * j,char * file)5240Sstevel@tonic-gate lpsched_read_job_configuration(service_t *svc, job_t *j, char *file)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	REQUEST *r;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if ((r = getrequest(file)) == NULL) {
5290Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to read job data: %s"),
5309152SSonam.Gupta@Sun.COM 		    file);
5310Sstevel@tonic-gate 		return;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
5350Sstevel@tonic-gate }
536