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;
972264Sjacobs 		tmp = realloc(*metadata, size);
982264Sjacobs 		if (tmp)
992264Sjacobs 			*metadata = (char *)tmp;
1002264Sjacobs 		else
1012264Sjacobs 			return (PAPI_TEMPORARY_ERROR);
1022264Sjacobs 	}
1032264Sjacobs 
1042264Sjacobs 	snprintf(line, sizeof (line), "%c%s\n", code, value);
1052264Sjacobs 	strlcat(*metadata, line, size);
1062264Sjacobs 
1072264Sjacobs 	return (PAPI_OK);
1082264Sjacobs }
1092264Sjacobs 
1102264Sjacobs static papi_status_t
1112264Sjacobs add_svr4_control_line(char **metadata, char code, char *value)
1122264Sjacobs {
1132264Sjacobs 
1142264Sjacobs 	char line[BUFSIZ];
1152264Sjacobs 
1162264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1172264Sjacobs 		return (PAPI_BAD_REQUEST);
1182264Sjacobs 
1192264Sjacobs 	snprintf(line, sizeof (line), "%c%s", code, value);
1202264Sjacobs 
1212264Sjacobs 	return (add_lpd_control_line(metadata, '5', line));
1222264Sjacobs }
1232264Sjacobs 
1242264Sjacobs static papi_status_t
1252264Sjacobs add_hpux_control_line(char **metadata, char *value)
1262264Sjacobs {
1272264Sjacobs 
1282264Sjacobs 	char line[BUFSIZ];
1292264Sjacobs 
1302264Sjacobs 	if ((metadata == NULL) || (value == NULL))
1312264Sjacobs 		return (PAPI_BAD_REQUEST);
1322264Sjacobs 
1332264Sjacobs 	snprintf(line, sizeof (line), " O%s", value);
1342264Sjacobs 
1352264Sjacobs 	return (add_lpd_control_line(metadata, 'N', line));
1362264Sjacobs }
1372264Sjacobs 
1382264Sjacobs static papi_status_t
1392264Sjacobs add_int_control_line(char **metadata, char code, int value, int flag)
1402264Sjacobs {
1412264Sjacobs 	char buf[16];
1422264Sjacobs 
1432264Sjacobs 	snprintf(buf, sizeof (buf), "%d", value);
1442264Sjacobs 
1452264Sjacobs 	if (flag == LPD_SVR4)
1462264Sjacobs 		return (add_svr4_control_line(metadata, code, buf));
1472264Sjacobs 	else
1482264Sjacobs 		return (add_lpd_control_line(metadata, code, buf));
1492264Sjacobs }
1502264Sjacobs 
1512264Sjacobs static papi_status_t
1522264Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes,
1532264Sjacobs 		char **metadata, papi_attribute_t ***used)
1542264Sjacobs {
1552264Sjacobs 	papi_status_t status = PAPI_OK;
1562264Sjacobs 	char *s;
1572264Sjacobs 	int integer;
1582264Sjacobs 	char bool;
1592264Sjacobs 	char host[BUFSIZ];
1602264Sjacobs 	char *user = "nobody";
1612264Sjacobs 	uid_t uid = getuid();
1622264Sjacobs 	struct passwd *pw;
1632264Sjacobs 
1642264Sjacobs 	if (svc == NULL)
1652264Sjacobs 		return (PAPI_BAD_REQUEST);
1662264Sjacobs 
1672264Sjacobs 	/* There is nothing to do */
1682264Sjacobs 	if (attributes == NULL)
1692264Sjacobs 		return (PAPI_OK);
1702264Sjacobs 
1712264Sjacobs 	gethostname(host, sizeof (host));
1722264Sjacobs 	add_lpd_control_line(metadata, 'H', host);
1732264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1742264Sjacobs 			"job-originating-host-name", host);
1752264Sjacobs 
1762264Sjacobs 	if ((pw = getpwuid(uid)) != NULL)
1772264Sjacobs 		user = pw->pw_name;
1782264Sjacobs 	if (uid == 0)
1792264Sjacobs 		papiAttributeListGetString(svc->attributes, NULL, "username",
1802264Sjacobs 			&user);
1812264Sjacobs 	add_lpd_control_line(metadata, 'P', user);
1822264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1832264Sjacobs 			"job-originating-user-name", user);
1842264Sjacobs 
1852264Sjacobs 	/* Class for Banner Page */
1862264Sjacobs 	s = NULL;
1872264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
1882264Sjacobs 	if (s != NULL) {
1892264Sjacobs 		add_lpd_control_line(metadata, 'C', s);
1902264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1912264Sjacobs 				"rfc-1179-class", s);
1922264Sjacobs 	}
1932264Sjacobs 
1942264Sjacobs 	/* Print Banner Page */
1952264Sjacobs 	s = NULL;
1962264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
1972264Sjacobs 	if ((s != NULL) && (strcmp(s, "standard") == 0)) {
1982264Sjacobs 		add_lpd_control_line(metadata, 'L', user);
1992264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2002264Sjacobs 				"job-sheets", s);
2012264Sjacobs 	}
2022264Sjacobs 
2032264Sjacobs 	/* Jobname */
2042264Sjacobs 	s = NULL;
2052264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-name", &s);
2062264Sjacobs 	if (s != NULL) {
2072264Sjacobs 		add_lpd_control_line(metadata, 'J', s);
2082264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2092264Sjacobs 				"job-name", s);
2102264Sjacobs 	}
2112264Sjacobs 
2122264Sjacobs 	/* User to mail when job is done - lpr -m */
2132264Sjacobs 	bool = PAPI_FALSE;
2142264Sjacobs 	papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
2152264Sjacobs 	if (bool == PAPI_TRUE) {
2162264Sjacobs 		add_lpd_control_line(metadata, 'M', user);
2172264Sjacobs 		papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
2182264Sjacobs 				"rfc-1179-mail", bool);
2192264Sjacobs 	}
2202264Sjacobs 
2212264Sjacobs 	/* Title for pr */
2222264Sjacobs 	s = NULL;
2232264Sjacobs 	papiAttributeListGetString(attributes, NULL, "pr-title", &s);
2242264Sjacobs 	if (s != NULL) {
2252264Sjacobs 		add_lpd_control_line(metadata, 'T', s);
2262264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2272264Sjacobs 				"pr-title", s);
2282264Sjacobs 	}
2292264Sjacobs 
2302264Sjacobs 	/* Indent - used with pr filter */
2312264Sjacobs 	integer = 0;
2322264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
2332264Sjacobs 	if (integer >= 1) {
2342264Sjacobs 		add_int_control_line(metadata, 'I', integer, LPD_RFC);
2352264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2362264Sjacobs 				"pr-indent", integer);
2372264Sjacobs 	}
2382264Sjacobs 
2392264Sjacobs 	/* Width - used with pr filter */
2402264Sjacobs 	integer = 0;
2412264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
2422264Sjacobs 	if (integer >= 1) {
2432264Sjacobs 		add_int_control_line(metadata, 'W', integer, LPD_RFC);
2442264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2452264Sjacobs 				"pr-width", integer);
2462264Sjacobs 	}
2472264Sjacobs 
2482264Sjacobs 	/* file with Times Roman font lpr -1	*/
2492264Sjacobs 	s = NULL;
2502264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
2512264Sjacobs 	if (s != NULL) {
2522264Sjacobs 		add_lpd_control_line(metadata, '1', s);
2532264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2542264Sjacobs 				"rfc-1179-font-r", s);
2552264Sjacobs 	}
2562264Sjacobs 
2572264Sjacobs 	/* file with Times Roman font lpr -2	*/
2582264Sjacobs 	s = NULL;
2592264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
2602264Sjacobs 	if (s != NULL) {
2612264Sjacobs 		add_lpd_control_line(metadata, '2', s);
2622264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2632264Sjacobs 				"rfc-1179-font-i", s);
2642264Sjacobs 	}
2652264Sjacobs 
2662264Sjacobs 	/* file with Times Roman font lpr -3	*/
2672264Sjacobs 	s = NULL;
2682264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
2692264Sjacobs 	if (s != NULL) {
2702264Sjacobs 		add_lpd_control_line(metadata, '3', s);
2712264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2722264Sjacobs 				"rfc-1179-font-b", s);
2732264Sjacobs 	}
2742264Sjacobs 
2752264Sjacobs 	/* file with Times Roman font lpr -4	*/
2762264Sjacobs 	s = NULL;
2772264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
2782264Sjacobs 	if (s != NULL) {
2792264Sjacobs 		add_lpd_control_line(metadata, '4', s);
2802264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2812264Sjacobs 				"rfc-1179-font-s", s);
2822264Sjacobs 	}
2832264Sjacobs 
2842264Sjacobs 	return (status);
2852264Sjacobs }
2862264Sjacobs 
2875825Sjacobs static char *
2885825Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used)
2895825Sjacobs {
2905825Sjacobs 	char *result = NULL;
2915825Sjacobs 	char **names = NULL;
2925825Sjacobs 	int i;
2935825Sjacobs 
2945825Sjacobs 	if ((list == NULL) || (used == NULL))
2955825Sjacobs 		return (NULL);
2965825Sjacobs 
2975825Sjacobs 	for (i = 0; used[i] != NULL; i++)
2985825Sjacobs 		list_append(&names, used[i]->name);
2995825Sjacobs 
3005825Sjacobs 	if (names != NULL) {
3015825Sjacobs 		papi_attribute_t **unused = NULL;
3025825Sjacobs 
3035825Sjacobs 		/* add these to the list of things to ignore */
3045825Sjacobs 		list_append(&names, "document-format");
3055825Sjacobs 		list_append(&names, "copies");
3065825Sjacobs 
3075825Sjacobs 		split_and_copy_attributes(names, list, NULL, &unused);
3085825Sjacobs 		if (unused != NULL) {
3095825Sjacobs 			size_t size = 0;
3105825Sjacobs 
3115825Sjacobs 			do {
3125825Sjacobs 				size += 1024;
3135825Sjacobs 				if (result != NULL)
3145825Sjacobs 					free(result);
3155825Sjacobs 				result = calloc(1, size);
3165825Sjacobs 			} while (papiAttributeListToString(unused, " ",
3175825Sjacobs 					result, size) != PAPI_OK);
3185825Sjacobs 			papiAttributeListFree(unused);
3195825Sjacobs 		}
3205825Sjacobs 		free(names);
3215825Sjacobs 	}
3225825Sjacobs 
3235825Sjacobs 	return (result);
3245825Sjacobs }
3255825Sjacobs 
3262264Sjacobs /*
3272264Sjacobs  * lpd_add_svr4_attributes
3282264Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
3292264Sjacobs  */
3302264Sjacobs static papi_status_t
3312264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
3322264Sjacobs 		char **metadata, papi_attribute_t ***used)
3332264Sjacobs {
334*6725Sjacobs 	papi_attribute_t *tmp[2];
3352264Sjacobs 	char *s;
3362264Sjacobs 	int integer;
3372264Sjacobs 
3382264Sjacobs 	if (svc == NULL)
3392264Sjacobs 		return (PAPI_BAD_REQUEST);
3402264Sjacobs 
3412264Sjacobs 	/* media */
3422264Sjacobs 	s = NULL;
3432264Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
3442264Sjacobs 	if (s != NULL) {
3452264Sjacobs 		add_svr4_control_line(metadata, 'f', s);
3462264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3472264Sjacobs 				"media", s);
3482264Sjacobs 	}
3492264Sjacobs 
3502264Sjacobs 	/* Handling */
3512264Sjacobs 	s = NULL;
352*6725Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
3532264Sjacobs 	if ((s != NULL) && (strcmp(s, "indefinite"))) {
3542264Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
3552264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
356*6725Sjacobs 				"job-hold-until", "indefinite");
3572264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "no-hold"))) {
358*6725Sjacobs 		add_svr4_control_line(metadata, 'H', "immediate");
3592264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
360*6725Sjacobs 				"job-hold-until", "no-hold");
361*6725Sjacobs 	} else if (s != NULL) {
362*6725Sjacobs 		add_svr4_control_line(metadata, 'H', s);
3632264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
364*6725Sjacobs 				"job-hold-until", s);
3652264Sjacobs 	}
3662264Sjacobs 
3672264Sjacobs 	/* Pages */
3682264Sjacobs 	s = NULL;
369*6725Sjacobs 	memset(tmp, NULL, sizeof (tmp));
370*6725Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
371*6725Sjacobs 	if (tmp[0] != NULL) {
372*6725Sjacobs 		char buf[BUFSIZ];
373*6725Sjacobs 
374*6725Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
375*6725Sjacobs 		if ((s = strchr(buf, '=')) != NULL) {
376*6725Sjacobs 			add_svr4_control_line(metadata, 'P', ++s);
377*6725Sjacobs 			papiAttributeListAddString(used, PAPI_ATTR_EXCL,
378*6725Sjacobs 					"page-ranges", s);
379*6725Sjacobs 		}
3802264Sjacobs 	}
3812264Sjacobs 
3822264Sjacobs 	/* Priority : lp -q */
3832264Sjacobs 	integer = -1;
384*6725Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "job-priority", &integer);
3852264Sjacobs 	if (integer != -1) {
386*6725Sjacobs 		integer = 40 - (integer / 2.5);
3872264Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
3882264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
389*6725Sjacobs 				"job-priority", integer);
3902264Sjacobs 	}
3912264Sjacobs 
3922264Sjacobs 	/* Charset : lp -S */
3932264Sjacobs 	s = NULL;
3942264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
3952264Sjacobs 	if (s != NULL) {
3962264Sjacobs 		add_svr4_control_line(metadata, 'S', s);
3972264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3982264Sjacobs 				"lp-charset", s);
3992264Sjacobs 	}
4002264Sjacobs 
4012264Sjacobs 	/* Type : done when adding file  */
4022264Sjacobs 
4032264Sjacobs 	/* Mode : lp -y */
4042264Sjacobs 	s = NULL;
4052264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
4062264Sjacobs 	if (s != NULL) {
4072264Sjacobs 		add_svr4_control_line(metadata, 'y', s);
4082264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4092264Sjacobs 				"lp-modes", s);
4102264Sjacobs 	}
4112264Sjacobs 
4125825Sjacobs 	/* Options lp -o are handled elsewhere */
4135825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4145825Sjacobs 		add_lpd_control_line(metadata, 'O', s);
4155825Sjacobs 		free(s);
4162264Sjacobs 	}
4172264Sjacobs 
4182264Sjacobs 	return (PAPI_OK);
4192264Sjacobs }
4202264Sjacobs 
4212264Sjacobs papi_status_t
4222264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
4232264Sjacobs 		char **metadata, papi_attribute_t ***used)
4242264Sjacobs {
4252264Sjacobs 	char *s = NULL;
4262264Sjacobs 
4272264Sjacobs 	/* Options lp -o */
4285825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4292264Sjacobs 		add_hpux_control_line(metadata, s);
4305825Sjacobs 		free(s);
4312264Sjacobs 	}
4322264Sjacobs 
4332264Sjacobs 	return (PAPI_OK);
4342264Sjacobs }
4352264Sjacobs 
4362264Sjacobs papi_status_t
4372264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
4382264Sjacobs 		char **metadata, papi_attribute_t ***used)
4392264Sjacobs {
4402264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
4412264Sjacobs 		return (PAPI_BAD_REQUEST);
4422264Sjacobs 
4432264Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
4442264Sjacobs 
4455825Sjacobs 	/* add protocol extensions if applicable */
4462264Sjacobs 	if (svc->uri->fragment != NULL) {
4472264Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4482264Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
4492264Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
4502264Sjacobs 					used);
4512264Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
4522264Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
4532264Sjacobs 						used);
4542264Sjacobs 		/*
4552264Sjacobs 		 * others could be added here:
4562264Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
4572264Sjacobs 		 */
4582264Sjacobs 	}
4592264Sjacobs 
4602264Sjacobs 	return (PAPI_OK);
4612264Sjacobs }
4622264Sjacobs 
4632264Sjacobs papi_status_t
4642264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
4652264Sjacobs 		char **files, char **metadata, papi_attribute_t ***used)
4662264Sjacobs {
4673125Sjacobs 	char *format = "text/plain";
4682264Sjacobs 	char rfc_fmt = 'l';
4692264Sjacobs 	int copies = 1;
4702264Sjacobs 	char host[BUFSIZ];
4712264Sjacobs 	int i;
4722264Sjacobs 
4732264Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
4742264Sjacobs 	    (metadata == NULL))
4752264Sjacobs 		return (PAPI_BAD_ARGUMENT);
4762264Sjacobs 
4772264Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
4782264Sjacobs 			&format);
4792264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4802264Sjacobs 			"document-format", format);
4812264Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
4822264Sjacobs 		if ((svc->uri->fragment != NULL) &&
4832264Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4842264Sjacobs 		     (strcasecmp(svc->uri->fragment, "svr4") == 0)))
4852264Sjacobs 			add_svr4_control_line(metadata, 'T', format);
4862264Sjacobs 		rfc_fmt = 'l';
4872264Sjacobs 	}
4882264Sjacobs 
4892264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
4902264Sjacobs 	if (copies < 1)
4912264Sjacobs 		copies = 1;
4922264Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
4932264Sjacobs 
4942264Sjacobs 	gethostname(host, sizeof (host));
4952264Sjacobs 
4962264Sjacobs 	for (i = 0; files[i] != NULL; i++) {
4972264Sjacobs 		char name[BUFSIZ];
4982264Sjacobs 		char key;
4992264Sjacobs 		int j;
5002264Sjacobs 
5012264Sjacobs 		if ((strcmp("standard input", files[i]) != 0) &&
5022264Sjacobs 		    (access(files[i], R_OK) < 0)) {
5032264Sjacobs 			detailed_error(svc, gettext("aborting request, %s: %s"),
5042264Sjacobs 				files[i], strerror(errno));
5052264Sjacobs 			return (PAPI_NOT_AUTHORIZED);
5062264Sjacobs 		}
5072264Sjacobs 
5082264Sjacobs 		if (i < 26)
5092264Sjacobs 			key = 'A' + i;
5102264Sjacobs 		else if (i < 52)
5112264Sjacobs 			key = 'a' + (i - 26);
5122264Sjacobs 		else if (i < 62)
5132264Sjacobs 			key = '0' + (i - 52);
5142264Sjacobs 		else {
5152264Sjacobs 			detailed_error(svc,
5162264Sjacobs 				gettext("too many files, truncated at 62"));
5172264Sjacobs 			return (PAPI_OK_SUBST);
5182264Sjacobs 		}
5192264Sjacobs 
5202264Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
5212264Sjacobs 
5222264Sjacobs 		for (j = 0; j < copies; j++)
5232264Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
5242264Sjacobs 		add_lpd_control_line(metadata, 'U', name);
5252264Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
5262264Sjacobs 	}
5272264Sjacobs 
5282264Sjacobs 	return (PAPI_OK);
5292264Sjacobs }
5302264Sjacobs 
5312264Sjacobs papi_status_t
5322264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
5332264Sjacobs 		int *ofd)
5342264Sjacobs {
5352264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
5362264Sjacobs 	int fd;
5372264Sjacobs 	char path[32];
5382264Sjacobs 	char *list[2];
5392264Sjacobs 
5402264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
5412264Sjacobs 		return (PAPI_BAD_ARGUMENT);
5422264Sjacobs 
5432264Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
5442264Sjacobs 	fd = mkstemp(path);
5452264Sjacobs 	write(fd, metadata, strlen(metadata));
5462264Sjacobs 	close(fd);
5472264Sjacobs 
5482264Sjacobs 	list[0] = path;
5492264Sjacobs 	list[1] = NULL;
5502264Sjacobs 
5513915Sceastha 	if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) {
5522264Sjacobs 		switch (errno) {
5532264Sjacobs 		case ENOSPC:
5542264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5552264Sjacobs 			break;
5562264Sjacobs 		case EIO:
5572264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5582264Sjacobs 			break;
5592264Sjacobs 		case ECONNREFUSED:
5602264Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
5612264Sjacobs 			break;
5622264Sjacobs 		case ENOENT:
5632264Sjacobs 			status = PAPI_NOT_ACCEPTING;
5642264Sjacobs 			break;
5652264Sjacobs 		case EBADMSG:
5662264Sjacobs 		case EBADF:
5672264Sjacobs 			status = PAPI_OK;
5682264Sjacobs 			break;
5692264Sjacobs 		default:
5702264Sjacobs 			status = PAPI_TIMEOUT;
5712264Sjacobs 			break;
5722264Sjacobs 		}
5732264Sjacobs 	} else
5742264Sjacobs 		status = PAPI_OK;
5752264Sjacobs 
5762264Sjacobs 	if (ofd != NULL)
5772264Sjacobs 		*ofd = fd;
5782264Sjacobs 	else
5792264Sjacobs 		close(fd);
5802264Sjacobs 
5812264Sjacobs 	/* read the ID and add it to to the job */
5822264Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
5832264Sjacobs 		int job_id = 0;
5842264Sjacobs 		read(fd, &job_id, sizeof (job_id));
5852264Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
5862264Sjacobs 				"job-id", job_id);
5872264Sjacobs 		close(fd);
5882264Sjacobs 	}
5892264Sjacobs 
5902264Sjacobs 	unlink(path);
5912264Sjacobs 
5922264Sjacobs 	return (status);
5932264Sjacobs }
594