xref: /onnv-gate/usr/src/lib/print/libpapi-lpd/common/lpd-job.c (revision 9606:5a3eed119f5b)
12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
238569SJonathan.Ca@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: lpd-job.c 157 2006-04-26 15:07:55Z ktou $ */
292264Sjacobs 
302264Sjacobs 
312264Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
322264Sjacobs #include <stdio.h>
332264Sjacobs #include <stdlib.h>
342264Sjacobs #include <unistd.h>
352264Sjacobs #include <errno.h>
362264Sjacobs #include <limits.h>
372264Sjacobs #include <sys/types.h>
382264Sjacobs #include <sys/stat.h>
392264Sjacobs #include <fcntl.h>
402264Sjacobs #include <string.h>
412264Sjacobs #include <pwd.h>
422264Sjacobs #include <libintl.h>
432264Sjacobs #include <papi_impl.h>
442264Sjacobs 
452264Sjacobs enum { LPD_RFC, LPD_SVR4 };
462264Sjacobs 
472264Sjacobs static char
mime_type_to_rfc1179_type(char * mime)482264Sjacobs mime_type_to_rfc1179_type(char *mime)
492264Sjacobs {
502264Sjacobs 	static struct { char *mime; char rfc; } cvt[] = {
513125Sjacobs 		{ "text/plain", 'f' },
522264Sjacobs 		{ "application/octet-stream", 'l' },
532264Sjacobs 		{ "application/postscript", 'f' }, /* rfc incorrectly has 'o' */
542264Sjacobs 		{ "application/x-pr", 'p' },
552264Sjacobs 		{ "application/x-cif", 'c' },
562264Sjacobs 		{ "application/x-dvi", 'd' },
572264Sjacobs 		{ "application/x-fortran", 'r' },
582264Sjacobs 		{ "application/x-plot", 'g' },
592264Sjacobs 		{ "application/x-ditroff", 'n' },
602264Sjacobs 		{ "application/x-troff", 't' },
612264Sjacobs 		{ "application/x-raster", 'v' },
622264Sjacobs 		{ NULL, 0}
632264Sjacobs 	};
642264Sjacobs 	char result = '\0';
652264Sjacobs 
662264Sjacobs 	if (mime != NULL) {
672264Sjacobs 		int i;
682264Sjacobs 
692264Sjacobs 		for (i = 0; cvt[i].mime != NULL; i++)
702264Sjacobs 			if (strcasecmp(cvt[i].mime, mime) == 0) {
712264Sjacobs 				result = cvt[i].rfc;
722264Sjacobs 				break;
732264Sjacobs 			}
742264Sjacobs 	}
752264Sjacobs 
762264Sjacobs 	return (result);
772264Sjacobs }
782264Sjacobs 
792264Sjacobs static papi_status_t
add_lpd_control_line(char ** metadata,char code,char * value)802264Sjacobs add_lpd_control_line(char **metadata, char code, char *value)
812264Sjacobs {
822264Sjacobs 	size_t size = 0;
832264Sjacobs 	char line[BUFSIZ];
842264Sjacobs 
852264Sjacobs 	if ((metadata == NULL) || (value == NULL))
862264Sjacobs 		return (PAPI_BAD_REQUEST);
872264Sjacobs 
882264Sjacobs 	if (*metadata != NULL)
892264Sjacobs 		size = strlen(*metadata);
902264Sjacobs 	size += strlen(value) + 3;
912264Sjacobs 
922264Sjacobs 	if (*metadata == NULL) {
932264Sjacobs 		*metadata = (char *)calloc(1, size);
942264Sjacobs 	} else {
952264Sjacobs 		void *tmp;
967253Sjacobs 		tmp = calloc(1, size);
977253Sjacobs 		if (tmp) {
987253Sjacobs 			strlcpy(tmp, *metadata, size);
997253Sjacobs 			free(*metadata);
1002264Sjacobs 			*metadata = (char *)tmp;
1017253Sjacobs 		} else
1022264Sjacobs 			return (PAPI_TEMPORARY_ERROR);
1032264Sjacobs 	}
1042264Sjacobs 
1052264Sjacobs 	snprintf(line, sizeof (line), "%c%s\n", code, value);
1062264Sjacobs 	strlcat(*metadata, line, size);
1072264Sjacobs 
1082264Sjacobs 	return (PAPI_OK);
1092264Sjacobs }
1102264Sjacobs 
1112264Sjacobs static papi_status_t
add_svr4_control_line(char ** metadata,char code,char * value)1122264Sjacobs add_svr4_control_line(char **metadata, char code, char *value)
1132264Sjacobs {
1142264Sjacobs 
1152264Sjacobs 	char line[BUFSIZ];
1162264Sjacobs 
1172264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1182264Sjacobs 		return (PAPI_BAD_REQUEST);
1192264Sjacobs 
1202264Sjacobs 	snprintf(line, sizeof (line), "%c%s", code, value);
1212264Sjacobs 
1222264Sjacobs 	return (add_lpd_control_line(metadata, '5', line));
1232264Sjacobs }
1242264Sjacobs 
1252264Sjacobs static papi_status_t
add_hpux_control_line(char ** metadata,char * value)1262264Sjacobs add_hpux_control_line(char **metadata, char *value)
1272264Sjacobs {
1282264Sjacobs 
1292264Sjacobs 	char line[BUFSIZ];
1302264Sjacobs 
1312264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1322264Sjacobs 		return (PAPI_BAD_REQUEST);
1332264Sjacobs 
1342264Sjacobs 	snprintf(line, sizeof (line), " O%s", value);
1352264Sjacobs 
1362264Sjacobs 	return (add_lpd_control_line(metadata, 'N', line));
1372264Sjacobs }
1382264Sjacobs 
1392264Sjacobs static papi_status_t
add_int_control_line(char ** metadata,char code,int value,int flag)1402264Sjacobs add_int_control_line(char **metadata, char code, int value, int flag)
1412264Sjacobs {
1422264Sjacobs 	char buf[16];
1432264Sjacobs 
1442264Sjacobs 	snprintf(buf, sizeof (buf), "%d", value);
1452264Sjacobs 
1462264Sjacobs 	if (flag == LPD_SVR4)
1472264Sjacobs 		return (add_svr4_control_line(metadata, code, buf));
1482264Sjacobs 	else
1492264Sjacobs 		return (add_lpd_control_line(metadata, code, buf));
1502264Sjacobs }
1512264Sjacobs 
1522264Sjacobs static papi_status_t
lpd_add_rfc1179_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)1532264Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes,
1542264Sjacobs 		char **metadata, papi_attribute_t ***used)
1552264Sjacobs {
1562264Sjacobs 	papi_status_t status = PAPI_OK;
1572264Sjacobs 	char *s;
1582264Sjacobs 	int integer;
1592264Sjacobs 	char bool;
1602264Sjacobs 	char host[BUFSIZ];
1612264Sjacobs 	char *user = "nobody";
1622264Sjacobs 	uid_t uid = getuid();
1632264Sjacobs 	struct passwd *pw;
164*9606SKeerthi.Kondaka@Sun.COM 	char *h1;
1652264Sjacobs 
1662264Sjacobs 	if (svc == NULL)
1672264Sjacobs 		return (PAPI_BAD_REQUEST);
1682264Sjacobs 
1692264Sjacobs 	/* There is nothing to do */
1702264Sjacobs 	if (attributes == NULL)
1712264Sjacobs 		return (PAPI_OK);
1722264Sjacobs 
1732264Sjacobs 	gethostname(host, sizeof (host));
174*9606SKeerthi.Kondaka@Sun.COM 	if (papiAttributeListGetString(attributes, NULL,
175*9606SKeerthi.Kondaka@Sun.COM 	    "job-originating-host-name", &h1) == PAPI_OK) {
176*9606SKeerthi.Kondaka@Sun.COM 		papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
177*9606SKeerthi.Kondaka@Sun.COM 		    "job-host", h1);
178*9606SKeerthi.Kondaka@Sun.COM 	}
1792264Sjacobs 	add_lpd_control_line(metadata, 'H', host);
1802264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1818569SJonathan.Ca@Sun.COM 	    "job-originating-host-name", host);
1822264Sjacobs 
1832264Sjacobs 	if ((pw = getpwuid(uid)) != NULL)
1842264Sjacobs 		user = pw->pw_name;
1852264Sjacobs 	if (uid == 0)
1862264Sjacobs 		papiAttributeListGetString(svc->attributes, NULL, "username",
1878569SJonathan.Ca@Sun.COM 		    &user);
1882264Sjacobs 	add_lpd_control_line(metadata, 'P', user);
1892264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1908569SJonathan.Ca@Sun.COM 	    "job-originating-user-name", user);
1912264Sjacobs 
1922264Sjacobs 	/* Class for Banner Page */
1932264Sjacobs 	s = NULL;
1942264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
1952264Sjacobs 	if (s != NULL) {
1962264Sjacobs 		add_lpd_control_line(metadata, 'C', s);
1972264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1988569SJonathan.Ca@Sun.COM 		    "rfc-1179-class", s);
1992264Sjacobs 	}
2002264Sjacobs 
2012264Sjacobs 	/* Print Banner Page */
2022264Sjacobs 	s = NULL;
2032264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
2042264Sjacobs 	if ((s != NULL) && (strcmp(s, "standard") == 0)) {
2052264Sjacobs 		add_lpd_control_line(metadata, 'L', user);
2062264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2078569SJonathan.Ca@Sun.COM 		    "job-sheets", s);
2082264Sjacobs 	}
2092264Sjacobs 
2102264Sjacobs 	/* Jobname */
2112264Sjacobs 	s = NULL;
2122264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-name", &s);
2132264Sjacobs 	if (s != NULL) {
2142264Sjacobs 		add_lpd_control_line(metadata, 'J', s);
2152264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2168569SJonathan.Ca@Sun.COM 		    "job-name", s);
2172264Sjacobs 	}
2182264Sjacobs 
2192264Sjacobs 	/* User to mail when job is done - lpr -m */
2202264Sjacobs 	bool = PAPI_FALSE;
2212264Sjacobs 	papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
2222264Sjacobs 	if (bool == PAPI_TRUE) {
2232264Sjacobs 		add_lpd_control_line(metadata, 'M', user);
2242264Sjacobs 		papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
2258569SJonathan.Ca@Sun.COM 		    "rfc-1179-mail", bool);
2262264Sjacobs 	}
2272264Sjacobs 
2282264Sjacobs 	/* Title for pr */
2292264Sjacobs 	s = NULL;
2302264Sjacobs 	papiAttributeListGetString(attributes, NULL, "pr-title", &s);
2312264Sjacobs 	if (s != NULL) {
2322264Sjacobs 		add_lpd_control_line(metadata, 'T', s);
2332264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2348569SJonathan.Ca@Sun.COM 		    "pr-title", s);
2352264Sjacobs 	}
2362264Sjacobs 
2372264Sjacobs 	/* Indent - used with pr filter */
2382264Sjacobs 	integer = 0;
2392264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
2402264Sjacobs 	if (integer >= 1) {
2412264Sjacobs 		add_int_control_line(metadata, 'I', integer, LPD_RFC);
2422264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2438569SJonathan.Ca@Sun.COM 		    "pr-indent", integer);
2442264Sjacobs 	}
2452264Sjacobs 
2462264Sjacobs 	/* Width - used with pr filter */
2472264Sjacobs 	integer = 0;
2482264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
2492264Sjacobs 	if (integer >= 1) {
2502264Sjacobs 		add_int_control_line(metadata, 'W', integer, LPD_RFC);
2512264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2528569SJonathan.Ca@Sun.COM 		    "pr-width", integer);
2532264Sjacobs 	}
2542264Sjacobs 
2552264Sjacobs 	/* file with Times Roman font lpr -1	*/
2562264Sjacobs 	s = NULL;
2572264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
2582264Sjacobs 	if (s != NULL) {
2592264Sjacobs 		add_lpd_control_line(metadata, '1', s);
2602264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2618569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-r", s);
2622264Sjacobs 	}
2632264Sjacobs 
2642264Sjacobs 	/* file with Times Roman font lpr -2	*/
2652264Sjacobs 	s = NULL;
2662264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
2672264Sjacobs 	if (s != NULL) {
2682264Sjacobs 		add_lpd_control_line(metadata, '2', s);
2692264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2708569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-i", s);
2712264Sjacobs 	}
2722264Sjacobs 
2732264Sjacobs 	/* file with Times Roman font lpr -3	*/
2742264Sjacobs 	s = NULL;
2752264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
2762264Sjacobs 	if (s != NULL) {
2772264Sjacobs 		add_lpd_control_line(metadata, '3', s);
2782264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2798569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-b", s);
2802264Sjacobs 	}
2812264Sjacobs 
2822264Sjacobs 	/* file with Times Roman font lpr -4	*/
2832264Sjacobs 	s = NULL;
2842264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
2852264Sjacobs 	if (s != NULL) {
2862264Sjacobs 		add_lpd_control_line(metadata, '4', s);
2872264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2888569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-s", s);
2892264Sjacobs 	}
2902264Sjacobs 
2919077SNagaraj.Yedathore@Sun.COM 	/*
2929077SNagaraj.Yedathore@Sun.COM 	 * The document format needs to be added, but the control line
2939077SNagaraj.Yedathore@Sun.COM 	 * should be added when the filenames are figured out.
2949077SNagaraj.Yedathore@Sun.COM 	 */
2959077SNagaraj.Yedathore@Sun.COM 	s = NULL;
2969077SNagaraj.Yedathore@Sun.COM 	papiAttributeListGetString(attributes, NULL, "document-format", &s);
2979077SNagaraj.Yedathore@Sun.COM 	if (s != NULL) {
2989077SNagaraj.Yedathore@Sun.COM 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2999077SNagaraj.Yedathore@Sun.COM 		    "document-format", s);
3009077SNagaraj.Yedathore@Sun.COM 	}
3019077SNagaraj.Yedathore@Sun.COM 
3022264Sjacobs 	return (status);
3032264Sjacobs }
3042264Sjacobs 
3055825Sjacobs static char *
unused_attributes(papi_attribute_t ** list,papi_attribute_t ** used)3065825Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used)
3075825Sjacobs {
3085825Sjacobs 	char *result = NULL;
3095825Sjacobs 	char **names = NULL;
3105825Sjacobs 	int i;
3115825Sjacobs 
3125825Sjacobs 	if ((list == NULL) || (used == NULL))
3135825Sjacobs 		return (NULL);
3145825Sjacobs 
3155825Sjacobs 	for (i = 0; used[i] != NULL; i++)
3165825Sjacobs 		list_append(&names, used[i]->name);
3175825Sjacobs 
3185825Sjacobs 	if (names != NULL) {
3195825Sjacobs 		papi_attribute_t **unused = NULL;
3205825Sjacobs 
3215825Sjacobs 		/* add these to the list of things to ignore */
3225825Sjacobs 		list_append(&names, "document-format");
3235825Sjacobs 		list_append(&names, "copies");
3245825Sjacobs 
3255825Sjacobs 		split_and_copy_attributes(names, list, NULL, &unused);
3265825Sjacobs 		if (unused != NULL) {
3275825Sjacobs 			size_t size = 0;
3285825Sjacobs 
3295825Sjacobs 			do {
3305825Sjacobs 				size += 1024;
3315825Sjacobs 				if (result != NULL)
3325825Sjacobs 					free(result);
3335825Sjacobs 				result = calloc(1, size);
3345825Sjacobs 			} while (papiAttributeListToString(unused, " ",
3358569SJonathan.Ca@Sun.COM 			    result, size) != PAPI_OK);
3365825Sjacobs 			papiAttributeListFree(unused);
3375825Sjacobs 		}
3385825Sjacobs 		free(names);
3395825Sjacobs 	}
3405825Sjacobs 
3415825Sjacobs 	return (result);
3425825Sjacobs }
3435825Sjacobs 
3442264Sjacobs /*
3452264Sjacobs  * lpd_add_svr4_attributes
3462264Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
3472264Sjacobs  */
3482264Sjacobs static papi_status_t
lpd_add_svr4_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)3492264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
3502264Sjacobs 		char **metadata, papi_attribute_t ***used)
3512264Sjacobs {
3526725Sjacobs 	papi_attribute_t *tmp[2];
3532264Sjacobs 	char *s;
3542264Sjacobs 	int integer;
3552264Sjacobs 
3562264Sjacobs 	if (svc == NULL)
3572264Sjacobs 		return (PAPI_BAD_REQUEST);
3582264Sjacobs 
3592264Sjacobs 	/* media */
3602264Sjacobs 	s = NULL;
3612264Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
3622264Sjacobs 	if (s != NULL) {
3632264Sjacobs 		add_svr4_control_line(metadata, 'f', s);
3642264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3658569SJonathan.Ca@Sun.COM 		    "media", s);
3662264Sjacobs 	}
3672264Sjacobs 
3682264Sjacobs 	/* Handling */
3692264Sjacobs 	s = NULL;
3706725Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
3719153SNagaraj.Yedathore@Sun.COM 	if ((s != NULL) && (strcmp(s, "indefinite") == 0)) {
3722264Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
3732264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3748569SJonathan.Ca@Sun.COM 		    "job-hold-until", "indefinite");
3759153SNagaraj.Yedathore@Sun.COM 	} else if ((s != NULL) && (strcmp(s, "no-hold") == 0)) {
3766725Sjacobs 		add_svr4_control_line(metadata, 'H', "immediate");
3772264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3788569SJonathan.Ca@Sun.COM 		    "job-hold-until", "no-hold");
3796725Sjacobs 	} else if (s != NULL) {
3806725Sjacobs 		add_svr4_control_line(metadata, 'H', s);
3812264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3828569SJonathan.Ca@Sun.COM 		    "job-hold-until", s);
3832264Sjacobs 	}
3842264Sjacobs 
3852264Sjacobs 	/* Pages */
3862264Sjacobs 	s = NULL;
3876725Sjacobs 	memset(tmp, NULL, sizeof (tmp));
3886725Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
3896725Sjacobs 	if (tmp[0] != NULL) {
3906725Sjacobs 		char buf[BUFSIZ];
3916725Sjacobs 
3926725Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
3936725Sjacobs 		if ((s = strchr(buf, '=')) != NULL) {
3946725Sjacobs 			add_svr4_control_line(metadata, 'P', ++s);
3956725Sjacobs 			papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3968569SJonathan.Ca@Sun.COM 			    "page-ranges", s);
3976725Sjacobs 		}
3982264Sjacobs 	}
3992264Sjacobs 
4002264Sjacobs 	/* Priority : lp -q */
4012264Sjacobs 	integer = -1;
4026725Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "job-priority", &integer);
4032264Sjacobs 	if (integer != -1) {
4046725Sjacobs 		integer = 40 - (integer / 2.5);
4052264Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
4062264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
4078569SJonathan.Ca@Sun.COM 		    "job-priority", integer);
4082264Sjacobs 	}
4092264Sjacobs 
4102264Sjacobs 	/* Charset : lp -S */
4112264Sjacobs 	s = NULL;
4122264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
4132264Sjacobs 	if (s != NULL) {
4142264Sjacobs 		add_svr4_control_line(metadata, 'S', s);
4152264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4168569SJonathan.Ca@Sun.COM 		    "lp-charset", s);
4172264Sjacobs 	}
4182264Sjacobs 
4192264Sjacobs 	/* Type : done when adding file  */
4202264Sjacobs 
4212264Sjacobs 	/* Mode : lp -y */
4222264Sjacobs 	s = NULL;
4232264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
4242264Sjacobs 	if (s != NULL) {
4252264Sjacobs 		add_svr4_control_line(metadata, 'y', s);
4262264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4278569SJonathan.Ca@Sun.COM 		    "lp-modes", s);
4282264Sjacobs 	}
4292264Sjacobs 
4305825Sjacobs 	/* Options lp -o are handled elsewhere */
4315825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4325825Sjacobs 		add_lpd_control_line(metadata, 'O', s);
4335825Sjacobs 		free(s);
4342264Sjacobs 	}
4352264Sjacobs 
4362264Sjacobs 	return (PAPI_OK);
4372264Sjacobs }
4382264Sjacobs 
4392264Sjacobs papi_status_t
lpd_add_hpux_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)4402264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
4412264Sjacobs 		char **metadata, papi_attribute_t ***used)
4422264Sjacobs {
4432264Sjacobs 	char *s = NULL;
4442264Sjacobs 
4452264Sjacobs 	/* Options lp -o */
4465825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4472264Sjacobs 		add_hpux_control_line(metadata, s);
4485825Sjacobs 		free(s);
4492264Sjacobs 	}
4502264Sjacobs 
4512264Sjacobs 	return (PAPI_OK);
4522264Sjacobs }
4532264Sjacobs 
4542264Sjacobs papi_status_t
lpd_job_add_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)4552264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
4562264Sjacobs 		char **metadata, papi_attribute_t ***used)
4572264Sjacobs {
4582264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
4592264Sjacobs 		return (PAPI_BAD_REQUEST);
4602264Sjacobs 
4612264Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
4622264Sjacobs 
4635825Sjacobs 	/* add protocol extensions if applicable */
4642264Sjacobs 	if (svc->uri->fragment != NULL) {
4652264Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4662264Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
4672264Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
4688569SJonathan.Ca@Sun.COM 			    used);
4692264Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
4702264Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
4718569SJonathan.Ca@Sun.COM 			    used);
4722264Sjacobs 		/*
4732264Sjacobs 		 * others could be added here:
4742264Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
4752264Sjacobs 		 */
4762264Sjacobs 	}
4772264Sjacobs 
4782264Sjacobs 	return (PAPI_OK);
4792264Sjacobs }
4802264Sjacobs 
4812264Sjacobs papi_status_t
lpd_job_add_files(service_t * svc,papi_attribute_t ** attributes,char ** files,char ** metadata,papi_attribute_t *** used)4822264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
4832264Sjacobs 		char **files, char **metadata, papi_attribute_t ***used)
4842264Sjacobs {
4853125Sjacobs 	char *format = "text/plain";
4862264Sjacobs 	char rfc_fmt = 'l';
4872264Sjacobs 	int copies = 1;
4882264Sjacobs 	char host[BUFSIZ];
4892264Sjacobs 	int i;
4902264Sjacobs 
4912264Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
4922264Sjacobs 	    (metadata == NULL))
4932264Sjacobs 		return (PAPI_BAD_ARGUMENT);
4942264Sjacobs 
4952264Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
4968569SJonathan.Ca@Sun.COM 	    &format);
4972264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4988569SJonathan.Ca@Sun.COM 	    "document-format", format);
4992264Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
5002264Sjacobs 		if ((svc->uri->fragment != NULL) &&
5012264Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
5028569SJonathan.Ca@Sun.COM 		    (strcasecmp(svc->uri->fragment, "svr4") == 0)))
5032264Sjacobs 			add_svr4_control_line(metadata, 'T', format);
5042264Sjacobs 		rfc_fmt = 'l';
5052264Sjacobs 	}
5062264Sjacobs 
5072264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
5082264Sjacobs 	if (copies < 1)
5092264Sjacobs 		copies = 1;
5102264Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
5112264Sjacobs 
5122264Sjacobs 	gethostname(host, sizeof (host));
5132264Sjacobs 
5142264Sjacobs 	for (i = 0; files[i] != NULL; i++) {
5152264Sjacobs 		char name[BUFSIZ];
5168569SJonathan.Ca@Sun.COM 		struct stat statbuf;
5172264Sjacobs 		char key;
5182264Sjacobs 		int j;
5192264Sjacobs 
5202264Sjacobs 		if ((strcmp("standard input", files[i]) != 0) &&
5212264Sjacobs 		    (access(files[i], R_OK) < 0)) {
5222264Sjacobs 			detailed_error(svc, gettext("aborting request, %s: %s"),
5238569SJonathan.Ca@Sun.COM 			    files[i], strerror(errno));
5242264Sjacobs 			return (PAPI_NOT_AUTHORIZED);
5252264Sjacobs 		}
5268569SJonathan.Ca@Sun.COM 		if (strcmp("standard input", files[i]) != 0) {
5279224SJonathan.Ca@Sun.COM 			if (stat(files[i], &statbuf) < 0) {
5289224SJonathan.Ca@Sun.COM 				detailed_error(svc,
5299224SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
5309224SJonathan.Ca@Sun.COM 				    files[i], strerror(errno));
5319224SJonathan.Ca@Sun.COM 				return (PAPI_DOCUMENT_ACCESS_ERROR);
5329224SJonathan.Ca@Sun.COM 			}
5338569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
5348569SJonathan.Ca@Sun.COM 				detailed_error(svc,
5358569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
5368569SJonathan.Ca@Sun.COM 				    files[i]);
5378569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
5388569SJonathan.Ca@Sun.COM 			}
5398569SJonathan.Ca@Sun.COM 		}
5402264Sjacobs 
5412264Sjacobs 		if (i < 26)
5422264Sjacobs 			key = 'A' + i;
5432264Sjacobs 		else if (i < 52)
5442264Sjacobs 			key = 'a' + (i - 26);
5452264Sjacobs 		else if (i < 62)
5462264Sjacobs 			key = '0' + (i - 52);
5472264Sjacobs 		else {
5482264Sjacobs 			detailed_error(svc,
5498569SJonathan.Ca@Sun.COM 			    gettext("too many files, truncated at 62"));
5502264Sjacobs 			return (PAPI_OK_SUBST);
5512264Sjacobs 		}
5522264Sjacobs 
5532264Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
5542264Sjacobs 
5552264Sjacobs 		for (j = 0; j < copies; j++)
5562264Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
5572264Sjacobs 		add_lpd_control_line(metadata, 'U', name);
5582264Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
5592264Sjacobs 	}
5602264Sjacobs 
5612264Sjacobs 	return (PAPI_OK);
5622264Sjacobs }
5632264Sjacobs 
5642264Sjacobs papi_status_t
lpd_submit_job(service_t * svc,char * metadata,papi_attribute_t *** attributes,int * ofd)5652264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
5662264Sjacobs 		int *ofd)
5672264Sjacobs {
5682264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
5692264Sjacobs 	int fd;
5702264Sjacobs 	char path[32];
5712264Sjacobs 	char *list[2];
5722264Sjacobs 
5732264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
5742264Sjacobs 		return (PAPI_BAD_ARGUMENT);
5752264Sjacobs 
5762264Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
5772264Sjacobs 	fd = mkstemp(path);
5782264Sjacobs 	write(fd, metadata, strlen(metadata));
5792264Sjacobs 	close(fd);
5802264Sjacobs 
5812264Sjacobs 	list[0] = path;
5822264Sjacobs 	list[1] = NULL;
5832264Sjacobs 
5843915Sceastha 	if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) {
5852264Sjacobs 		switch (errno) {
5862264Sjacobs 		case ENOSPC:
5872264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5882264Sjacobs 			break;
5892264Sjacobs 		case EIO:
5902264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5912264Sjacobs 			break;
5922264Sjacobs 		case ECONNREFUSED:
5932264Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
5942264Sjacobs 			break;
5952264Sjacobs 		case ENOENT:
5962264Sjacobs 			status = PAPI_NOT_ACCEPTING;
5972264Sjacobs 			break;
5982264Sjacobs 		case EBADMSG:
5992264Sjacobs 		case EBADF:
6002264Sjacobs 			status = PAPI_OK;
6012264Sjacobs 			break;
6022264Sjacobs 		default:
6032264Sjacobs 			status = PAPI_TIMEOUT;
6042264Sjacobs 			break;
6052264Sjacobs 		}
6062264Sjacobs 	} else
6072264Sjacobs 		status = PAPI_OK;
6082264Sjacobs 
6092264Sjacobs 	if (ofd != NULL)
6102264Sjacobs 		*ofd = fd;
6112264Sjacobs 	else
6122264Sjacobs 		close(fd);
6132264Sjacobs 
6142264Sjacobs 	/* read the ID and add it to to the job */
6152264Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
6162264Sjacobs 		int job_id = 0;
6172264Sjacobs 		read(fd, &job_id, sizeof (job_id));
6182264Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
6198569SJonathan.Ca@Sun.COM 		    "job-id", job_id);
6202264Sjacobs 		close(fd);
6212264Sjacobs 	}
6222264Sjacobs 
6232264Sjacobs 	unlink(path);
6242264Sjacobs 
6252264Sjacobs 	return (status);
6262264Sjacobs }
627