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 	}
285