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
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
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
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
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
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
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;
1642264Sjacobs 
1652264Sjacobs 	if (svc == NULL)
1662264Sjacobs 		return (PAPI_BAD_REQUEST);
1672264Sjacobs 
1682264Sjacobs 	/* There is nothing to do */
1692264Sjacobs 	if (attributes == NULL)
1702264Sjacobs 		return (PAPI_OK);
1712264Sjacobs 
1722264Sjacobs 	gethostname(host, sizeof (host));
1732264Sjacobs 	add_lpd_control_line(metadata, 'H', host);
1742264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1758569SJonathan.Ca@Sun.COM 	    "job-originating-host-name", host);
1762264Sjacobs 
1772264Sjacobs 	if ((pw = getpwuid(uid)) != NULL)
1782264Sjacobs 		user = pw->pw_name;
1792264Sjacobs 	if (uid == 0)
1802264Sjacobs 		papiAttributeListGetString(svc->attributes, NULL, "username",
1818569SJonathan.Ca@Sun.COM 		    &user);
1822264Sjacobs 	add_lpd_control_line(metadata, 'P', user);
1832264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1848569SJonathan.Ca@Sun.COM 	    "job-originating-user-name", user);
1852264Sjacobs 
1862264Sjacobs 	/* Class for Banner Page */
1872264Sjacobs 	s = NULL;
1882264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
1892264Sjacobs 	if (s != NULL) {
1902264Sjacobs 		add_lpd_control_line(metadata, 'C', s);
1912264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
1928569SJonathan.Ca@Sun.COM 		    "rfc-1179-class", s);
1932264Sjacobs 	}
1942264Sjacobs 
1952264Sjacobs 	/* Print Banner Page */
1962264Sjacobs 	s = NULL;
1972264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
1982264Sjacobs 	if ((s != NULL) && (strcmp(s, "standard") == 0)) {
1992264Sjacobs 		add_lpd_control_line(metadata, 'L', user);
2002264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2018569SJonathan.Ca@Sun.COM 		    "job-sheets", s);
2022264Sjacobs 	}
2032264Sjacobs 
2042264Sjacobs 	/* Jobname */
2052264Sjacobs 	s = NULL;
2062264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-name", &s);
2072264Sjacobs 	if (s != NULL) {
2082264Sjacobs 		add_lpd_control_line(metadata, 'J', s);
2092264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2108569SJonathan.Ca@Sun.COM 		    "job-name", s);
2112264Sjacobs 	}
2122264Sjacobs 
2132264Sjacobs 	/* User to mail when job is done - lpr -m */
2142264Sjacobs 	bool = PAPI_FALSE;
2152264Sjacobs 	papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
2162264Sjacobs 	if (bool == PAPI_TRUE) {
2172264Sjacobs 		add_lpd_control_line(metadata, 'M', user);
2182264Sjacobs 		papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
2198569SJonathan.Ca@Sun.COM 		    "rfc-1179-mail", bool);
2202264Sjacobs 	}
2212264Sjacobs 
2222264Sjacobs 	/* Title for pr */
2232264Sjacobs 	s = NULL;
2242264Sjacobs 	papiAttributeListGetString(attributes, NULL, "pr-title", &s);
2252264Sjacobs 	if (s != NULL) {
2262264Sjacobs 		add_lpd_control_line(metadata, 'T', s);
2272264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2288569SJonathan.Ca@Sun.COM 		    "pr-title", s);
2292264Sjacobs 	}
2302264Sjacobs 
2312264Sjacobs 	/* Indent - used with pr filter */
2322264Sjacobs 	integer = 0;
2332264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
2342264Sjacobs 	if (integer >= 1) {
2352264Sjacobs 		add_int_control_line(metadata, 'I', integer, LPD_RFC);
2362264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2378569SJonathan.Ca@Sun.COM 		    "pr-indent", integer);
2382264Sjacobs 	}
2392264Sjacobs 
2402264Sjacobs 	/* Width - used with pr filter */
2412264Sjacobs 	integer = 0;
2422264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
2432264Sjacobs 	if (integer >= 1) {
2442264Sjacobs 		add_int_control_line(metadata, 'W', integer, LPD_RFC);
2452264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
2468569SJonathan.Ca@Sun.COM 		    "pr-width", integer);
2472264Sjacobs 	}
2482264Sjacobs 
2492264Sjacobs 	/* file with Times Roman font lpr -1	*/
2502264Sjacobs 	s = NULL;
2512264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
2522264Sjacobs 	if (s != NULL) {
2532264Sjacobs 		add_lpd_control_line(metadata, '1', s);
2542264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2558569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-r", s);
2562264Sjacobs 	}
2572264Sjacobs 
2582264Sjacobs 	/* file with Times Roman font lpr -2	*/
2592264Sjacobs 	s = NULL;
2602264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
2612264Sjacobs 	if (s != NULL) {
2622264Sjacobs 		add_lpd_control_line(metadata, '2', s);
2632264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2648569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-i", s);
2652264Sjacobs 	}
2662264Sjacobs 
2672264Sjacobs 	/* file with Times Roman font lpr -3	*/
2682264Sjacobs 	s = NULL;
2692264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
2702264Sjacobs 	if (s != NULL) {
2712264Sjacobs 		add_lpd_control_line(metadata, '3', s);
2722264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2738569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-b", s);
2742264Sjacobs 	}
2752264Sjacobs 
2762264Sjacobs 	/* file with Times Roman font lpr -4	*/
2772264Sjacobs 	s = NULL;
2782264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
2792264Sjacobs 	if (s != NULL) {
2802264Sjacobs 		add_lpd_control_line(metadata, '4', s);
2812264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
2828569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-s", s);
2832264Sjacobs 	}
2842264Sjacobs 
285*9077SNagaraj.Yedathore@Sun.COM 	/*
286*9077SNagaraj.Yedathore@Sun.COM 	 * The document format needs to be added, but the control line
287*9077SNagaraj.Yedathore@Sun.COM 	 * should be added when the filenames are figured out.
288*9077SNagaraj.Yedathore@Sun.COM 	 */
289*9077SNagaraj.Yedathore@Sun.COM 	s = NULL;
290*9077SNagaraj.Yedathore@Sun.COM 	papiAttributeListGetString(attributes, NULL, "document-format", &s);
291*9077SNagaraj.Yedathore@Sun.COM 	if (s != NULL) {
292*9077SNagaraj.Yedathore@Sun.COM 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
293*9077SNagaraj.Yedathore@Sun.COM 		    "document-format", s);
294*9077SNagaraj.Yedathore@Sun.COM 	}
295*9077SNagaraj.Yedathore@Sun.COM 
2962264Sjacobs 	return (status);
2972264Sjacobs }
2982264Sjacobs 
2995825Sjacobs static char *
3005825Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used)
3015825Sjacobs {
3025825Sjacobs 	char *result = NULL;
3035825Sjacobs 	char **names = NULL;
3045825Sjacobs 	int i;
3055825Sjacobs 
3065825Sjacobs 	if ((list == NULL) || (used == NULL))
3075825Sjacobs 		return (NULL);
3085825Sjacobs 
3095825Sjacobs 	for (i = 0; used[i] != NULL; i++)
3105825Sjacobs 		list_append(&names, used[i]->name);
3115825Sjacobs 
3125825Sjacobs 	if (names != NULL) {
3135825Sjacobs 		papi_attribute_t **unused = NULL;
3145825Sjacobs 
3155825Sjacobs 		/* add these to the list of things to ignore */
3165825Sjacobs 		list_append(&names, "document-format");
3175825Sjacobs 		list_append(&names, "copies");
3185825Sjacobs 
3195825Sjacobs 		split_and_copy_attributes(names, list, NULL, &unused);
3205825Sjacobs 		if (unused != NULL) {
3215825Sjacobs 			size_t size = 0;
3225825Sjacobs 
3235825Sjacobs 			do {
3245825Sjacobs 				size += 1024;
3255825Sjacobs 				if (result != NULL)
3265825Sjacobs 					free(result);
3275825Sjacobs 				result = calloc(1, size);
3285825Sjacobs 			} while (papiAttributeListToString(unused, " ",
3298569SJonathan.Ca@Sun.COM 			    result, size) != PAPI_OK);
3305825Sjacobs 			papiAttributeListFree(unused);
3315825Sjacobs 		}
3325825Sjacobs 		free(names);
3335825Sjacobs 	}
3345825Sjacobs 
3355825Sjacobs 	return (result);
3365825Sjacobs }
3375825Sjacobs 
3382264Sjacobs /*
3392264Sjacobs  * lpd_add_svr4_attributes
3402264Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
3412264Sjacobs  */
3422264Sjacobs static papi_status_t
3432264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
3442264Sjacobs 		char **metadata, papi_attribute_t ***used)
3452264Sjacobs {
3466725Sjacobs 	papi_attribute_t *tmp[2];
3472264Sjacobs 	char *s;
3482264Sjacobs 	int integer;
3492264Sjacobs 
3502264Sjacobs 	if (svc == NULL)
3512264Sjacobs 		return (PAPI_BAD_REQUEST);
3522264Sjacobs 
3532264Sjacobs 	/* media */
3542264Sjacobs 	s = NULL;
3552264Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
3562264Sjacobs 	if (s != NULL) {
3572264Sjacobs 		add_svr4_control_line(metadata, 'f', s);
3582264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3598569SJonathan.Ca@Sun.COM 		    "media", s);
3602264Sjacobs 	}
3612264Sjacobs 
3622264Sjacobs 	/* Handling */
3632264Sjacobs 	s = NULL;
3646725Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
3652264Sjacobs 	if ((s != NULL) && (strcmp(s, "indefinite"))) {
3662264Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
3672264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3688569SJonathan.Ca@Sun.COM 		    "job-hold-until", "indefinite");
3692264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "no-hold"))) {
3706725Sjacobs 		add_svr4_control_line(metadata, 'H', "immediate");
3712264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3728569SJonathan.Ca@Sun.COM 		    "job-hold-until", "no-hold");
3736725Sjacobs 	} else if (s != NULL) {
3746725Sjacobs 		add_svr4_control_line(metadata, 'H', s);
3752264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3768569SJonathan.Ca@Sun.COM 		    "job-hold-until", s);
3772264Sjacobs 	}
3782264Sjacobs 
3792264Sjacobs 	/* Pages */
3802264Sjacobs 	s = NULL;
3816725Sjacobs 	memset(tmp, NULL, sizeof (tmp));
3826725Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
3836725Sjacobs 	if (tmp[0] != NULL) {
3846725Sjacobs 		char buf[BUFSIZ];
3856725Sjacobs 
3866725Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
3876725Sjacobs 		if ((s = strchr(buf, '=')) != NULL) {
3886725Sjacobs 			add_svr4_control_line(metadata, 'P', ++s);
3896725Sjacobs 			papiAttributeListAddString(used, PAPI_ATTR_EXCL,
3908569SJonathan.Ca@Sun.COM 			    "page-ranges", s);
3916725Sjacobs 		}
3922264Sjacobs 	}
3932264Sjacobs 
3942264Sjacobs 	/* Priority : lp -q */
3952264Sjacobs 	integer = -1;
3966725Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "job-priority", &integer);
3972264Sjacobs 	if (integer != -1) {
3986725Sjacobs 		integer = 40 - (integer / 2.5);
3992264Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
4002264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
4018569SJonathan.Ca@Sun.COM 		    "job-priority", integer);
4022264Sjacobs 	}
4032264Sjacobs 
4042264Sjacobs 	/* Charset : lp -S */
4052264Sjacobs 	s = NULL;
4062264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
4072264Sjacobs 	if (s != NULL) {
4082264Sjacobs 		add_svr4_control_line(metadata, 'S', s);
4092264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4108569SJonathan.Ca@Sun.COM 		    "lp-charset", s);
4112264Sjacobs 	}
4122264Sjacobs 
4132264Sjacobs 	/* Type : done when adding file  */
4142264Sjacobs 
4152264Sjacobs 	/* Mode : lp -y */
4162264Sjacobs 	s = NULL;
4172264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
4182264Sjacobs 	if (s != NULL) {
4192264Sjacobs 		add_svr4_control_line(metadata, 'y', s);
4202264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4218569SJonathan.Ca@Sun.COM 		    "lp-modes", s);
4222264Sjacobs 	}
4232264Sjacobs 
4245825Sjacobs 	/* Options lp -o are handled elsewhere */
4255825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4265825Sjacobs 		add_lpd_control_line(metadata, 'O', s);
4275825Sjacobs 		free(s);
4282264Sjacobs 	}
4292264Sjacobs 
4302264Sjacobs 	return (PAPI_OK);
4312264Sjacobs }
4322264Sjacobs 
4332264Sjacobs papi_status_t
4342264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
4352264Sjacobs 		char **metadata, papi_attribute_t ***used)
4362264Sjacobs {
4372264Sjacobs 	char *s = NULL;
4382264Sjacobs 
4392264Sjacobs 	/* Options lp -o */
4405825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4412264Sjacobs 		add_hpux_control_line(metadata, s);
4425825Sjacobs 		free(s);
4432264Sjacobs 	}
4442264Sjacobs 
4452264Sjacobs 	return (PAPI_OK);
4462264Sjacobs }
4472264Sjacobs 
4482264Sjacobs papi_status_t
4492264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
4502264Sjacobs 		char **metadata, papi_attribute_t ***used)
4512264Sjacobs {
4522264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
4532264Sjacobs 		return (PAPI_BAD_REQUEST);
4542264Sjacobs 
4552264Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
4562264Sjacobs 
4575825Sjacobs 	/* add protocol extensions if applicable */
4582264Sjacobs 	if (svc->uri->fragment != NULL) {
4592264Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4602264Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
4612264Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
4628569SJonathan.Ca@Sun.COM 			    used);
4632264Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
4642264Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
4658569SJonathan.Ca@Sun.COM 			    used);
4662264Sjacobs 		/*
4672264Sjacobs 		 * others could be added here:
4682264Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
4692264Sjacobs 		 */
4702264Sjacobs 	}
4712264Sjacobs 
4722264Sjacobs 	return (PAPI_OK);
4732264Sjacobs }
4742264Sjacobs 
4752264Sjacobs papi_status_t
4762264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
4772264Sjacobs 		char **files, char **metadata, papi_attribute_t ***used)
4782264Sjacobs {
4793125Sjacobs 	char *format = "text/plain";
4802264Sjacobs 	char rfc_fmt = 'l';
4812264Sjacobs 	int copies = 1;
4822264Sjacobs 	char host[BUFSIZ];
4832264Sjacobs 	int i;
4842264Sjacobs 
4852264Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
4862264Sjacobs 	    (metadata == NULL))
4872264Sjacobs 		return (PAPI_BAD_ARGUMENT);
4882264Sjacobs 
4892264Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
4908569SJonathan.Ca@Sun.COM 	    &format);
4912264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
4928569SJonathan.Ca@Sun.COM 	    "document-format", format);
4932264Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
4942264Sjacobs 		if ((svc->uri->fragment != NULL) &&
4952264Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4968569SJonathan.Ca@Sun.COM 		    (strcasecmp(svc->uri->fragment, "svr4") == 0)))
4972264Sjacobs 			add_svr4_control_line(metadata, 'T', format);
4982264Sjacobs 		rfc_fmt = 'l';
4992264Sjacobs 	}
5002264Sjacobs 
5012264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
5022264Sjacobs 	if (copies < 1)
5032264Sjacobs 		copies = 1;
5042264Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
5052264Sjacobs 
5062264Sjacobs 	gethostname(host, sizeof (host));
5072264Sjacobs 
5082264Sjacobs 	for (i = 0; files[i] != NULL; i++) {
5092264Sjacobs 		char name[BUFSIZ];
5108569SJonathan.Ca@Sun.COM 		struct stat statbuf;
5112264Sjacobs 		char key;
5122264Sjacobs 		int j;
5132264Sjacobs 
5142264Sjacobs 		if ((strcmp("standard input", files[i]) != 0) &&
5152264Sjacobs 		    (access(files[i], R_OK) < 0)) {
5162264Sjacobs 			detailed_error(svc, gettext("aborting request, %s: %s"),
5178569SJonathan.Ca@Sun.COM 			    files[i], strerror(errno));
5182264Sjacobs 			return (PAPI_NOT_AUTHORIZED);
5192264Sjacobs 		}
5208569SJonathan.Ca@Sun.COM 		if (strcmp("standard input", files[i]) != 0) {
5218569SJonathan.Ca@Sun.COM 			stat(files[i], &statbuf);
5228569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
5238569SJonathan.Ca@Sun.COM 				detailed_error(svc,
5248569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
5258569SJonathan.Ca@Sun.COM 				    files[i]);
5268569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
5278569SJonathan.Ca@Sun.COM 			}
5288569SJonathan.Ca@Sun.COM 		}
5292264Sjacobs 
5302264Sjacobs 		if (i < 26)
5312264Sjacobs 			key = 'A' + i;
5322264Sjacobs 		else if (i < 52)
5332264Sjacobs 			key = 'a' + (i - 26);
5342264Sjacobs 		else if (i < 62)
5352264Sjacobs 			key = '0' + (i - 52);
5362264Sjacobs 		else {
5372264Sjacobs 			detailed_error(svc,
5388569SJonathan.Ca@Sun.COM 			    gettext("too many files, truncated at 62"));
5392264Sjacobs 			return (PAPI_OK_SUBST);
5402264Sjacobs 		}
5412264Sjacobs 
5422264Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
5432264Sjacobs 
5442264Sjacobs 		for (j = 0; j < copies; j++)
5452264Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
5462264Sjacobs 		add_lpd_control_line(metadata, 'U', name);
5472264Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
5482264Sjacobs 	}
5492264Sjacobs 
5502264Sjacobs 	return (PAPI_OK);
5512264Sjacobs }
5522264Sjacobs 
5532264Sjacobs papi_status_t
5542264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
5552264Sjacobs 		int *ofd)
5562264Sjacobs {
5572264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
5582264Sjacobs 	int fd;
5592264Sjacobs 	char path[32];
5602264Sjacobs 	char *list[2];
5612264Sjacobs 
5622264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
5632264Sjacobs 		return (PAPI_BAD_ARGUMENT);
5642264Sjacobs 
5652264Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
5662264Sjacobs 	fd = mkstemp(path);
5672264Sjacobs 	write(fd, metadata, strlen(metadata));
5682264Sjacobs 	close(fd);
5692264Sjacobs 
5702264Sjacobs 	list[0] = path;
5712264Sjacobs 	list[1] = NULL;
5722264Sjacobs 
5733915Sceastha 	if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) {
5742264Sjacobs 		switch (errno) {
5752264Sjacobs 		case ENOSPC:
5762264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5772264Sjacobs 			break;
5782264Sjacobs 		case EIO:
5792264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5802264Sjacobs 			break;
5812264Sjacobs 		case ECONNREFUSED:
5822264Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
5832264Sjacobs 			break;
5842264Sjacobs 		case ENOENT:
5852264Sjacobs 			status = PAPI_NOT_ACCEPTING;
5862264Sjacobs 			break;
5872264Sjacobs 		case EBADMSG:
5882264Sjacobs 		case EBADF:
5892264Sjacobs 			status = PAPI_OK;
5902264Sjacobs 			break;
5912264Sjacobs 		default:
5922264Sjacobs 			status = PAPI_TIMEOUT;
5932264Sjacobs 			break;
5942264Sjacobs 		}
5952264Sjacobs 	} else
5962264Sjacobs 		status = PAPI_OK;
5972264Sjacobs 
5982264Sjacobs 	if (ofd != NULL)
5992264Sjacobs 		*ofd = fd;
6002264Sjacobs 	else
6012264Sjacobs 		close(fd);
6022264Sjacobs 
6032264Sjacobs 	/* read the ID and add it to to the job */
6042264Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
6052264Sjacobs 		int job_id = 0;
6062264Sjacobs 		read(fd, &job_id, sizeof (job_id));
6072264Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
6088569SJonathan.Ca@Sun.COM 		    "job-id", job_id);
6092264Sjacobs 		close(fd);
6102264Sjacobs 	}
6112264Sjacobs 
6122264Sjacobs 	unlink(path);
6132264Sjacobs 
6142264Sjacobs 	return (status);
6152264Sjacobs }
616