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 /*
235825Sjacobs  * Copyright 2008 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
312264Sjacobs 
322264Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
332264Sjacobs #include <stdio.h>
342264Sjacobs #include <stdlib.h>
352264Sjacobs #include <unistd.h>
362264Sjacobs #include <errno.h>
372264Sjacobs #include <limits.h>
382264Sjacobs #include <sys/types.h>
392264Sjacobs #include <sys/stat.h>
402264Sjacobs #include <fcntl.h>
412264Sjacobs #include <string.h>
422264Sjacobs #include <pwd.h>
432264Sjacobs #include <libintl.h>
442264Sjacobs #include <papi_impl.h>
452264Sjacobs 
462264Sjacobs enum { LPD_RFC, LPD_SVR4 };
472264Sjacobs 
482264Sjacobs static char
492264Sjacobs mime_type_to_rfc1179_type(char *mime)
502264Sjacobs {
512264Sjacobs 	static struct { char *mime; char rfc; } cvt[] = {
523125Sjacobs 		{ "text/plain", 'f' },
532264Sjacobs 		{ "application/octet-stream", 'l' },
542264Sjacobs 		{ "application/postscript", 'f' }, /* rfc incorrectly has 'o' */
552264Sjacobs 		{ "application/x-pr", 'p' },
562264Sjacobs 		{ "application/x-cif", 'c' },
572264Sjacobs 		{ "application/x-dvi", 'd' },
582264Sjacobs 		{ "application/x-fortran", 'r' },
592264Sjacobs 		{ "application/x-plot", 'g' },
602264Sjacobs 		{ "application/x-ditroff", 'n' },
612264Sjacobs 		{ "application/x-troff", 't' },
622264Sjacobs 		{ "application/x-raster", 'v' },
632264Sjacobs 		{ NULL, 0}
642264Sjacobs 	};
652264Sjacobs 	char result = '\0';
662264Sjacobs 
672264Sjacobs 	if (mime != NULL) {
682264Sjacobs 		int i;
692264Sjacobs 
702264Sjacobs 		for (i = 0; cvt[i].mime != NULL; i++)
712264Sjacobs 			if (strcasecmp(cvt[i].mime, mime) == 0) {
722264Sjacobs 				result = cvt[i].rfc;
732264Sjacobs 				break;
742264Sjacobs 			}
752264Sjacobs 	}
762264Sjacobs 
772264Sjacobs 	return (result);
782264Sjacobs }
792264Sjacobs 
802264Sjacobs static papi_status_t
812264Sjacobs add_lpd_control_line(char **metadata, char code, char *value)
822264Sjacobs {
832264Sjacobs 	size_t size = 0;
842264Sjacobs 	char line[BUFSIZ];
852264Sjacobs 
862264Sjacobs 	if ((metadata == NULL) || (value == NULL))
872264Sjacobs 		return (PAPI_BAD_REQUEST);
882264Sjacobs 
892264Sjacobs 	if (*metadata != NULL)
902264Sjacobs 		size = strlen(*metadata);
912264Sjacobs 	size += strlen(value) + 3;
922264Sjacobs 
932264Sjacobs 	if (*metadata == NULL) {
942264Sjacobs 		*metadata = (char *)calloc(1, size);
952264Sjacobs 	} else {
962264Sjacobs 		void *tmp;
97*7253Sjacobs 		tmp = calloc(1, size);
98*7253Sjacobs 		if (tmp) {
99*7253Sjacobs 			strlcpy(tmp, *metadata, size);
100*7253Sjacobs 			free(*metadata);
1012264Sjacobs 			*metadata = (char *)tmp;
102*7253Sjacobs 		} else
1032264Sjacobs 			return (PAPI_TEMPORARY_ERROR);
1042264Sjacobs 	}
1052264Sjacobs 
1062264Sjacobs 	snprintf(line, sizeof (line), "%c%s\n", code, value);
1072264Sjacobs 	strlcat(*metadata, line, size);
1082264Sjacobs 
1092264Sjacobs 	return (PAPI_OK);
1102264Sjacobs }
1112264Sjacobs 
1122264Sjacobs static papi_status_t
1132264Sjacobs add_svr4_control_line(char **metadata, char code, char *value)
1142264Sjacobs {
1152264Sjacobs 
1162264Sjacobs 	char line[BUFSIZ];
1172264Sjacobs 
1182264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1192264Sjacobs 		return (PAPI_BAD_REQUEST);
1202264Sjacobs 
1212264Sjacobs 	snprintf(line, sizeof (line), "%c%s", code, value);
1222264Sjacobs 
1232264Sjacobs 	return (add_lpd_control_line(metadata, '5', line));
1242264Sjacobs }
1252264Sjacobs 
1262264Sjacobs static papi_status_t
1272264Sjacobs add_hpux_control_line(char **metadata, char *value)
1282264Sjacobs {
1292264Sjacobs 
1302264Sjacobs 	char line[BUFSIZ];
1312264Sjacobs 
1322264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1332264Sjacobs 		return (PAPI_BAD_REQUEST);
1342264Sjacobs 
1352264Sjacobs 	snprintf(line, sizeof (line), " O%s", value);
1362264Sjacobs 
1372264Sjacobs 	return (add_lpd_control_line(metadata, 'N', line));
1382264Sjacobs }
1392264Sjacobs 
1402264Sjacobs static papi_status_t
1412264Sjacobs add_int_control_line(char **metadata, char code, int value, int flag)
1422264Sjacobs {
1432264Sjacobs 	char buf[16];
1442264Sjacobs 
1452264Sjacobs 	snprintf(buf, sizeof (buf), "%d", value);
1462264Sjacobs 
1472264Sjacobs 	if (flag == LPD_SVR4)
1482264Sjacobs 		return (add_svr4_control_line(metadata, code, buf));
1492264Sjacobs 	else
1502264Sjacobs 		return (add_lpd_control_line(metadata, code, buf));
1512264Sjacobs }
1522264Sjacobs 
1532264Sjacobs static papi_status_t
1542264Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes,
1552264Sjacobs 		char **metadata, papi_attribute_t ***used)
1562264Sjacobs {
1572264Sjacobs 	papi_status_t status = PAPI_OK;
1582264Sjacobs 	char *s;
1592264Sjacobs 	int integer;
1602264Sjacobs 	char bool;
1612264Sjacobs 	char host[BUFSIZ];
1622264Sjacobs 	char *user = "nobody";
1632264Sjacobs 	uid_t uid = getuid();
1642264Sjacobs 	struct passwd *pw;
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));
1742264Sjacobs 	add_lpd_control_line(metadata, 'H', host);
1752264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1762264Sjacobs 			"job-originating-host-name", host);
1772264Sjacobs 
1782264Sjacobs 	if ((pw = getpwuid(uid)) != NULL)
1792264Sjacobs 		user = pw->pw_name;
1802264Sjacobs 	if (uid == 0)
1812264Sjacobs 		papiAttributeListGetString(svc->attributes, NULL, "username",
1822264Sjacobs 			&user);
1832264Sjacobs 	add_lpd_control_line(metadata, 'P', user);
1842264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1852264Sjacobs 			"job-originating-user-name", user);
1862264Sjacobs 
1872264Sjacobs 	/* Class for Banner Page */
1882264Sjacobs 	s = NULL;
1892264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
1902264Sjacobs 	if (s != NULL) {
1912264Sjacobs 		add_lpd_control_line(metadata, 'C', s);
1922264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1932264Sjacobs 				"rfc-1179-class", s);
1942264Sjacobs 	}
1952264Sjacobs 
1962264Sjacobs 	/* Print Banner Page */
1972264Sjacobs 	s = NULL;
1982264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
1992264Sjacobs 	if ((s != NULL) && (strcmp(s, "standard") == 0)) {
2002264Sjacobs 		add_lpd_control_line(metadata, 'L', user);
2012264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2022264Sjacobs 				"job-sheets", s);
2032264Sjacobs 	}
2042264Sjacobs 
2052264Sjacobs 	/* Jobname */
2062264Sjacobs 	s = NULL;
2072264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-name", &s);
2082264Sjacobs 	if (s != NULL) {
2092264Sjacobs 		add_lpd_control_line(metadata, 'J', s);
2102264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2112264Sjacobs 				"job-name", s);
2122264Sjacobs 	}
2132264Sjacobs 
2142264Sjacobs 	/* User to mail when job is done - lpr -m */
2152264Sjacobs 	bool = PAPI_FALSE;
2162264Sjacobs 	papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
2172264Sjacobs 	if (bool == PAPI_TRUE) {
2182264Sjacobs 		add_lpd_control_line(metadata, 'M', user);
2192264Sjacobs 		papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
2202264Sjacobs 				"rfc-1179-mail", bool);
2212264Sjacobs 	}
2222264Sjacobs 
2232264Sjacobs 	/* Title for pr */
2242264Sjacobs 	s = NULL;
2252264Sjacobs 	papiAttributeListGetString(attributes, NULL, "pr-title", &s);
2262264Sjacobs 	if (s != NULL) {
2272264Sjacobs 		add_lpd_control_line(metadata, 'T', s);
2282264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2292264Sjacobs 				"pr-title", s);
2302264Sjacobs 	}
2312264Sjacobs 
2322264Sjacobs 	/* Indent - used with pr filter */
2332264Sjacobs 	integer = 0;
2342264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
2352264Sjacobs 	if (integer >= 1) {
2362264Sjacobs 		add_int_control_line(metadata, 'I', integer, LPD_RFC);
2372264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2382264Sjacobs 				"pr-indent", integer);
2392264Sjacobs 	}
2402264Sjacobs 
2412264Sjacobs 	/* Width - used with pr filter */
2422264Sjacobs 	integer = 0;
2432264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
2442264Sjacobs 	if (integer >= 1) {
2452264Sjacobs 		add_int_control_line(metadata, 'W', integer, LPD_RFC);
2462264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2472264Sjacobs 				"pr-width", integer);
2482264Sjacobs 	}
2492264Sjacobs 
2502264Sjacobs 	/* file with Times Roman font lpr -1	*/
2512264Sjacobs 	s = NULL;
2522264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
2532264Sjacobs 	if (s != NULL) {
2542264Sjacobs 		add_lpd_control_line(metadata, '1', s);
2552264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2562264Sjacobs 				"rfc-1179-font-r", s);
2572264Sjacobs 	}
2582264Sjacobs 
2592264Sjacobs 	/* file with Times Roman font lpr -2	*/
2602264Sjacobs 	s = NULL;
2612264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
2622264Sjacobs 	if (s != NULL) {
2632264Sjacobs 		add_lpd_control_line(metadata, '2', s);
2642264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2652264Sjacobs 				"rfc-1179-font-i", s);
2662264Sjacobs 	}
2672264Sjacobs 
2682264Sjacobs 	/* file with Times Roman font lpr -3	*/
2692264Sjacobs 	s = NULL;
2702264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
2712264Sjacobs 	if (s != NULL) {
2722264Sjacobs 		add_lpd_control_line(metadata, '3', s);
2732264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2742264Sjacobs 				"rfc-1179-font-b", s);
2752264Sjacobs 	}
2762264Sjacobs 
2772264Sjacobs 	/* file with Times Roman font lpr -4	*/
2782264Sjacobs 	s = NULL;
2792264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
2802264Sjacobs 	if (s != NULL) {
2812264Sjacobs 		add_lpd_control_line(metadata, '4', s);
2822264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2832264Sjacobs 				"rfc-1179-font-s", s);
2842264Sjacobs 	}
2852264Sjacobs 
2862264Sjacobs 	return (status);
2872264Sjacobs }
2882264Sjacobs 
2895825Sjacobs static char *
2905825Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used)
2915825Sjacobs {
2925825Sjacobs 	char *result = NULL;
2935825Sjacobs 	char **names = NULL;
2945825Sjacobs 	int i;
2955825Sjacobs 
2965825Sjacobs 	if ((list == NULL) || (used == NULL))
2975825Sjacobs 		return (NULL);
2985825Sjacobs 
2995825Sjacobs 	for (i = 0; used[i] != NULL; i++)
3005825Sjacobs 		list_append(&names, used[i]->name);
3015825Sjacobs 
3025825Sjacobs 	if (names != NULL) {
3035825Sjacobs 		papi_attribute_t **unused = NULL;
3045825Sjacobs 
3055825Sjacobs 		/* add these to the list of things to ignore */
3065825Sjacobs 		list_append(&names, "document-format");
3075825Sjacobs 		list_append(&names, "copies");
3085825Sjacobs 
3095825Sjacobs 		split_and_copy_attributes(names, list, NULL, &unused);
3105825Sjacobs 		if (unused != NULL) {
3115825Sjacobs 			size_t size = 0;
3125825Sjacobs 
3135825Sjacobs 			do {
3145825Sjacobs 				size += 1024;
3155825Sjacobs 				if (result != NULL)
3165825Sjacobs 					free(result);
3175825Sjacobs 				result = calloc(1, size);
3185825Sjacobs 			} while (papiAttributeListToString(unused, " ",
3195825Sjacobs 					result, size) != PAPI_OK);
3205825Sjacobs 			papiAttributeListFree(unused);
3215825Sjacobs 		}
3225825Sjacobs 		free(names);
3235825Sjacobs 	}
3245825Sjacobs 
3255825Sjacobs 	return (result);
3265825Sjacobs }
3275825Sjacobs 
3282264Sjacobs /*
3292264Sjacobs  * lpd_add_svr4_attributes
3302264Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
3312264Sjacobs  */
3322264Sjacobs static papi_status_t
3332264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
3342264Sjacobs 		char **metadata, papi_attribute_t ***used)
3352264Sjacobs {
3366725Sjacobs 	papi_attribute_t *tmp[2];
3372264Sjacobs 	char *s;
3382264Sjacobs 	int integer;
3392264Sjacobs 
3402264Sjacobs 	if (svc == NULL)
3412264Sjacobs 		return (PAPI_BAD_REQUEST);
3422264Sjacobs 
3432264Sjacobs 	/* media */
3442264Sjacobs 	s = NULL;
3452264Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
3462264Sjacobs 	if (s != NULL) {
3472264Sjacobs 		add_svr4_control_line(metadata, 'f', s);
3482264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3492264Sjacobs 				"media", s);
3502264Sjacobs 	}
3512264Sjacobs 
3522264Sjacobs 	/* Handling */
3532264Sjacobs 	s = NULL;
3546725Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
3552264Sjacobs 	if ((s != NULL) && (strcmp(s, "indefinite"))) {
3562264Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
3572264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3586725Sjacobs 				"job-hold-until", "indefinite");
3592264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "no-hold"))) {
3606725Sjacobs 		add_svr4_control_line(metadata, 'H', "immediate");
3612264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3626725Sjacobs 				"job-hold-until", "no-hold");
3636725Sjacobs 	} else if (s != NULL) {
3646725Sjacobs 		add_svr4_control_line(metadata, 'H', s);
3652264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3666725Sjacobs 				"job-hold-until", s);
3672264Sjacobs 	}
3682264Sjacobs 
3692264Sjacobs 	/* Pages */
3702264Sjacobs 	s = NULL;
3716725Sjacobs 	memset(tmp, NULL, sizeof (tmp));
3726725Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
3736725Sjacobs 	if (tmp[0] != NULL) {
3746725Sjacobs 		char buf[BUFSIZ];
3756725Sjacobs 
3766725Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
3776725Sjacobs 		if ((s = strchr(buf, '=')) != NULL) {
3786725Sjacobs 			add_svr4_control_line(metadata, 'P', ++s);
3796725Sjacobs 			papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3806725Sjacobs 					"page-ranges", s);
3816725Sjacobs 		}
3822264Sjacobs 	}
3832264Sjacobs 
3842264Sjacobs 	/* Priority : lp -q */
3852264Sjacobs 	integer = -1;
3866725Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "job-priority", &integer);
3872264Sjacobs 	if (integer != -1) {
3886725Sjacobs 		integer = 40 - (integer / 2.5);
3892264Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
3902264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
3916725Sjacobs 				"job-priority", integer);
3922264Sjacobs 	}
3932264Sjacobs 
3942264Sjacobs 	/* Charset : lp -S */
3952264Sjacobs 	s = NULL;
3962264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
3972264Sjacobs 	if (s != NULL) {
3982264Sjacobs 		add_svr4_control_line(metadata, 'S', s);
3992264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4002264Sjacobs 				"lp-charset", s);
4012264Sjacobs 	}
4022264Sjacobs 
4032264Sjacobs 	/* Type : done when adding file  */
4042264Sjacobs 
4052264Sjacobs 	/* Mode : lp -y */
4062264Sjacobs 	s = NULL;
4072264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
4082264Sjacobs 	if (s != NULL) {
4092264Sjacobs 		add_svr4_control_line(metadata, 'y', s);
4102264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4112264Sjacobs 				"lp-modes", s);
4122264Sjacobs 	}
4132264Sjacobs 
4145825Sjacobs 	/* Options lp -o are handled elsewhere */
4155825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4165825Sjacobs 		add_lpd_control_line(metadata, 'O', s);
4175825Sjacobs 		free(s);
4182264Sjacobs 	}
4192264Sjacobs 
4202264Sjacobs 	return (PAPI_OK);
4212264Sjacobs }
4222264Sjacobs 
4232264Sjacobs papi_status_t
4242264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
4252264Sjacobs 		char **metadata, papi_attribute_t ***used)
4262264Sjacobs {
4272264Sjacobs 	char *s = NULL;
4282264Sjacobs 
4292264Sjacobs 	/* Options lp -o */
4305825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4312264Sjacobs 		add_hpux_control_line(metadata, s);
4325825Sjacobs 		free(s);
4332264Sjacobs 	}
4342264Sjacobs 
4352264Sjacobs 	return (PAPI_OK);
4362264Sjacobs }
4372264Sjacobs 
4382264Sjacobs papi_status_t
4392264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
4402264Sjacobs 		char **metadata, papi_attribute_t ***used)
4412264Sjacobs {
4422264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
4432264Sjacobs 		return (PAPI_BAD_REQUEST);
4442264Sjacobs 
4452264Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
4462264Sjacobs 
4475825Sjacobs 	/* add protocol extensions if applicable */
4482264Sjacobs 	if (svc->uri->fragment != NULL) {
4492264Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4502264Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
4512264Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
4522264Sjacobs 					used);
4532264Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
4542264Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
4552264Sjacobs 						used);
4562264Sjacobs 		/*
4572264Sjacobs 		 * others could be added here:
4582264Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
4592264Sjacobs 		 */
4602264Sjacobs 	}
4612264Sjacobs 
4622264Sjacobs 	return (PAPI_OK);
4632264Sjacobs }
4642264Sjacobs 
4652264Sjacobs papi_status_t
4662264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
4672264Sjacobs 		char **files, char **metadata, papi_attribute_t ***used)
4682264Sjacobs {
4693125Sjacobs 	char *format = "text/plain";
4702264Sjacobs 	char rfc_fmt = 'l';
4712264Sjacobs 	int copies = 1;
4722264Sjacobs 	char host[BUFSIZ];
4732264Sjacobs 	int i;
4742264Sjacobs 
4752264Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
4762264Sjacobs 	    (metadata == NULL))
4772264Sjacobs 		return (PAPI_BAD_ARGUMENT);
4782264Sjacobs 
4792264Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
4802264Sjacobs 			&format);
4812264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4822264Sjacobs 			"document-format", format);
4832264Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
4842264Sjacobs 		if ((svc->uri->fragment != NULL) &&
4852264Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4862264Sjacobs 		     (strcasecmp(svc->uri->fragment, "svr4") == 0)))
4872264Sjacobs 			add_svr4_control_line(metadata, 'T', format);
4882264Sjacobs 		rfc_fmt = 'l';
4892264Sjacobs 	}
4902264Sjacobs 
4912264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
4922264Sjacobs 	if (copies < 1)
4932264Sjacobs 		copies = 1;
4942264Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
4952264Sjacobs 
4962264Sjacobs 	gethostname(host, sizeof (host));
4972264Sjacobs 
4982264Sjacobs 	for (i = 0; files[i] != NULL; i++) {
4992264Sjacobs 		char name[BUFSIZ];
5002264Sjacobs 		char key;
5012264Sjacobs 		int j;
5022264Sjacobs 
5032264Sjacobs 		if ((strcmp("standard input", files[i]) != 0) &&
5042264Sjacobs 		    (access(files[i], R_OK) < 0)) {
5052264Sjacobs 			detailed_error(svc, gettext("aborting request, %s: %s"),
5062264Sjacobs 				files[i], strerror(errno));
5072264Sjacobs 			return (PAPI_NOT_AUTHORIZED);
5082264Sjacobs 		}
5092264Sjacobs 
5102264Sjacobs 		if (i < 26)
5112264Sjacobs 			key = 'A' + i;
5122264Sjacobs 		else if (i < 52)
5132264Sjacobs 			key = 'a' + (i - 26);
5142264Sjacobs 		else if (i < 62)
5152264Sjacobs 			key = '0' + (i - 52);
5162264Sjacobs 		else {
5172264Sjacobs 			detailed_error(svc,
5182264Sjacobs 				gettext("too many files, truncated at 62"));
5192264Sjacobs 			return (PAPI_OK_SUBST);
5202264Sjacobs 		}
5212264Sjacobs 
5222264Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
5232264Sjacobs 
5242264Sjacobs 		for (j = 0; j < copies; j++)
5252264Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
5262264Sjacobs 		add_lpd_control_line(metadata, 'U', name);
5272264Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
5282264Sjacobs 	}
5292264Sjacobs 
5302264Sjacobs 	return (PAPI_OK);
5312264Sjacobs }
5322264Sjacobs 
5332264Sjacobs papi_status_t
5342264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
5352264Sjacobs 		int *ofd)
5362264Sjacobs {
5372264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
5382264Sjacobs 	int fd;
5392264Sjacobs 	char path[32];
5402264Sjacobs 	char *list[2];
5412264Sjacobs 
5422264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
5432264Sjacobs 		return (PAPI_BAD_ARGUMENT);
5442264Sjacobs 
5452264Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
5462264Sjacobs 	fd = mkstemp(path);
5472264Sjacobs 	write(fd, metadata, strlen(metadata));
5482264Sjacobs 	close(fd);
5492264Sjacobs 
5502264Sjacobs 	list[0] = path;
5512264Sjacobs 	list[1] = NULL;
5522264Sjacobs 
5533915Sceastha 	if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) {
5542264Sjacobs 		switch (errno) {
5552264Sjacobs 		case ENOSPC:
5562264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5572264Sjacobs 			break;
5582264Sjacobs 		case EIO:
5592264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5602264Sjacobs 			break;
5612264Sjacobs 		case ECONNREFUSED:
5622264Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
5632264Sjacobs 			break;
5642264Sjacobs 		case ENOENT:
5652264Sjacobs 			status = PAPI_NOT_ACCEPTING;
5662264Sjacobs 			break;
5672264Sjacobs 		case EBADMSG:
5682264Sjacobs 		case EBADF:
5692264Sjacobs 			status = PAPI_OK;
5702264Sjacobs 			break;
5712264Sjacobs 		default:
5722264Sjacobs 			status = PAPI_TIMEOUT;
5732264Sjacobs 			break;
5742264Sjacobs 		}
5752264Sjacobs 	} else
5762264Sjacobs 		status = PAPI_OK;
5772264Sjacobs 
5782264Sjacobs 	if (ofd != NULL)
5792264Sjacobs 		*ofd = fd;
5802264Sjacobs 	else
5812264Sjacobs 		close(fd);
5822264Sjacobs 
5832264Sjacobs 	/* read the ID and add it to to the job */
5842264Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
5852264Sjacobs 		int job_id = 0;
5862264Sjacobs 		read(fd, &job_id, sizeof (job_id));
5872264Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
5882264Sjacobs 				"job-id", job_id);
5892264Sjacobs 		close(fd);
5902264Sjacobs 	}
5912264Sjacobs 
5922264Sjacobs 	unlink(path);
5932264Sjacobs 
5942264Sjacobs 	return (status);
5952264Sjacobs }
596