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 /*
23*8569SJonathan.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,
175*8569SJonathan.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",
181*8569SJonathan.Ca@Sun.COM 		    &user);
1822264Sjacobs 	add_lpd_control_line(metadata, 'P', user);
1832264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
184*8569SJonathan.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,
192*8569SJonathan.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,
201*8569SJonathan.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,
210*8569SJonathan.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,
219*8569SJonathan.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,
228*8569SJonathan.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,
237*8569SJonathan.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,
246*8569SJonathan.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,
255*8569SJonathan.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,
264*8569SJonathan.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,
273*8569SJonathan.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,
282*8569SJonathan.Ca@Sun.COM 		    "rfc-1179-font-s", s);
2832264Sjacobs 	}
2842264Sjacobs 
2852264Sjacobs 	return (status);
2862264Sjacobs }
2872264Sjacobs 
2885825Sjacobs static char *
2895825Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used)
2905825Sjacobs {
2915825Sjacobs 	char *result = NULL;
2925825Sjacobs 	char **names = NULL;
2935825Sjacobs 	int i;
2945825Sjacobs 
2955825Sjacobs 	if ((list == NULL) || (used == NULL))
2965825Sjacobs 		return (NULL);
2975825Sjacobs 
2985825Sjacobs 	for (i = 0; used[i] != NULL; i++)
2995825Sjacobs 		list_append(&names, used[i]->name);
3005825Sjacobs 
3015825Sjacobs 	if (names != NULL) {
3025825Sjacobs 		papi_attribute_t **unused = NULL;
3035825Sjacobs 
3045825Sjacobs 		/* add these to the list of things to ignore */
3055825Sjacobs 		list_append(&names, "document-format");
3065825Sjacobs 		list_append(&names, "copies");
3075825Sjacobs 
3085825Sjacobs 		split_and_copy_attributes(names, list, NULL, &unused);
3095825Sjacobs 		if (unused != NULL) {
3105825Sjacobs 			size_t size = 0;
3115825Sjacobs 
3125825Sjacobs 			do {
3135825Sjacobs 				size += 1024;
3145825Sjacobs 				if (result != NULL)
3155825Sjacobs 					free(result);
3165825Sjacobs 				result = calloc(1, size);
3175825Sjacobs 			} while (papiAttributeListToString(unused, " ",
318*8569SJonathan.Ca@Sun.COM 			    result, size) != PAPI_OK);
3195825Sjacobs 			papiAttributeListFree(unused);
3205825Sjacobs 		}
3215825Sjacobs 		free(names);
3225825Sjacobs 	}
3235825Sjacobs 
3245825Sjacobs 	return (result);
3255825Sjacobs }
3265825Sjacobs 
3272264Sjacobs /*
3282264Sjacobs  * lpd_add_svr4_attributes
3292264Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
3302264Sjacobs  */
3312264Sjacobs static papi_status_t
3322264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
3332264Sjacobs 		char **metadata, papi_attribute_t ***used)
3342264Sjacobs {
3356725Sjacobs 	papi_attribute_t *tmp[2];
3362264Sjacobs 	char *s;
3372264Sjacobs 	int integer;
3382264Sjacobs 
3392264Sjacobs 	if (svc == NULL)
3402264Sjacobs 		return (PAPI_BAD_REQUEST);
3412264Sjacobs 
3422264Sjacobs 	/* media */
3432264Sjacobs 	s = NULL;
3442264Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
3452264Sjacobs 	if (s != NULL) {
3462264Sjacobs 		add_svr4_control_line(metadata, 'f', s);
3472264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
348*8569SJonathan.Ca@Sun.COM 		    "media", s);
3492264Sjacobs 	}
3502264Sjacobs 
3512264Sjacobs 	/* Handling */
3522264Sjacobs 	s = NULL;
3536725Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
3542264Sjacobs 	if ((s != NULL) && (strcmp(s, "indefinite"))) {
3552264Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
3562264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
357*8569SJonathan.Ca@Sun.COM 		    "job-hold-until", "indefinite");
3582264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "no-hold"))) {
3596725Sjacobs 		add_svr4_control_line(metadata, 'H', "immediate");
3602264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
361*8569SJonathan.Ca@Sun.COM 		    "job-hold-until", "no-hold");
3626725Sjacobs 	} else if (s != NULL) {
3636725Sjacobs 		add_svr4_control_line(metadata, 'H', s);
3642264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
365*8569SJonathan.Ca@Sun.COM 		    "job-hold-until", s);
3662264Sjacobs 	}
3672264Sjacobs 
3682264Sjacobs 	/* Pages */
3692264Sjacobs 	s = NULL;
3706725Sjacobs 	memset(tmp, NULL, sizeof (tmp));
3716725Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
3726725Sjacobs 	if (tmp[0] != NULL) {
3736725Sjacobs 		char buf[BUFSIZ];
3746725Sjacobs 
3756725Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
3766725Sjacobs 		if ((s = strchr(buf, '=')) != NULL) {
3776725Sjacobs 			add_svr4_control_line(metadata, 'P', ++s);
3786725Sjacobs 			papiAttributeListAddString(used, PAPI_ATTR_EXCL,
379*8569SJonathan.Ca@Sun.COM 			    "page-ranges", s);
3806725Sjacobs 		}
3812264Sjacobs 	}
3822264Sjacobs 
3832264Sjacobs 	/* Priority : lp -q */
3842264Sjacobs 	integer = -1;
3856725Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "job-priority", &integer);
3862264Sjacobs 	if (integer != -1) {
3876725Sjacobs 		integer = 40 - (integer / 2.5);
3882264Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
3892264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
390*8569SJonathan.Ca@Sun.COM 		    "job-priority", integer);
3912264Sjacobs 	}
3922264Sjacobs 
3932264Sjacobs 	/* Charset : lp -S */
3942264Sjacobs 	s = NULL;
3952264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
3962264Sjacobs 	if (s != NULL) {
3972264Sjacobs 		add_svr4_control_line(metadata, 'S', s);
3982264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
399*8569SJonathan.Ca@Sun.COM 		    "lp-charset", s);
4002264Sjacobs 	}
4012264Sjacobs 
4022264Sjacobs 	/* Type : done when adding file  */
4032264Sjacobs 
4042264Sjacobs 	/* Mode : lp -y */
4052264Sjacobs 	s = NULL;
4062264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
4072264Sjacobs 	if (s != NULL) {
4082264Sjacobs 		add_svr4_control_line(metadata, 'y', s);
4092264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
410*8569SJonathan.Ca@Sun.COM 		    "lp-modes", s);
4112264Sjacobs 	}
4122264Sjacobs 
4135825Sjacobs 	/* Options lp -o are handled elsewhere */
4145825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4155825Sjacobs 		add_lpd_control_line(metadata, 'O', s);
4165825Sjacobs 		free(s);
4172264Sjacobs 	}
4182264Sjacobs 
4192264Sjacobs 	return (PAPI_OK);
4202264Sjacobs }
4212264Sjacobs 
4222264Sjacobs papi_status_t
4232264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
4242264Sjacobs 		char **metadata, papi_attribute_t ***used)
4252264Sjacobs {
4262264Sjacobs 	char *s = NULL;
4272264Sjacobs 
4282264Sjacobs 	/* Options lp -o */
4295825Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
4302264Sjacobs 		add_hpux_control_line(metadata, s);
4315825Sjacobs 		free(s);
4322264Sjacobs 	}
4332264Sjacobs 
4342264Sjacobs 	return (PAPI_OK);
4352264Sjacobs }
4362264Sjacobs 
4372264Sjacobs papi_status_t
4382264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
4392264Sjacobs 		char **metadata, papi_attribute_t ***used)
4402264Sjacobs {
4412264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
4422264Sjacobs 		return (PAPI_BAD_REQUEST);
4432264Sjacobs 
4442264Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
4452264Sjacobs 
4465825Sjacobs 	/* add protocol extensions if applicable */
4472264Sjacobs 	if (svc->uri->fragment != NULL) {
4482264Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
4492264Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
4502264Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
451*8569SJonathan.Ca@Sun.COM 			    used);
4522264Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
4532264Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
454*8569SJonathan.Ca@Sun.COM 			    used);
4552264Sjacobs 		/*
4562264Sjacobs 		 * others could be added here:
4572264Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
4582264Sjacobs 		 */
4592264Sjacobs 	}
4602264Sjacobs 
4612264Sjacobs 	return (PAPI_OK);
4622264Sjacobs }
4632264Sjacobs 
4642264Sjacobs papi_status_t
4652264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
4662264Sjacobs 		char **files, char **metadata, papi_attribute_t ***used)
4672264Sjacobs {
4683125Sjacobs 	char *format = "text/plain";
4692264Sjacobs 	char rfc_fmt = 'l';
4702264Sjacobs 	int copies = 1;
4712264Sjacobs 	char host[BUFSIZ];
4722264Sjacobs 	int i;
4732264Sjacobs 
4742264Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
4752264Sjacobs 	    (metadata == NULL))
4762264Sjacobs 		return (PAPI_BAD_ARGUMENT);
4772264Sjacobs 
4782264Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
479*8569SJonathan.Ca@Sun.COM 	    &format);
4802264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
481*8569SJonathan.Ca@Sun.COM 	    "document-format", format);
4822264Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
4832264Sjacobs 		if ((svc->uri->fragment != NULL) &&
4842264Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
485*8569SJonathan.Ca@Sun.COM 		    (strcasecmp(svc->uri->fragment, "svr4") == 0)))
4862264Sjacobs 			add_svr4_control_line(metadata, 'T', format);
4872264Sjacobs 		rfc_fmt = 'l';
4882264Sjacobs 	}
4892264Sjacobs 
4902264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
4912264Sjacobs 	if (copies < 1)
4922264Sjacobs 		copies = 1;
4932264Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
4942264Sjacobs 
4952264Sjacobs 	gethostname(host, sizeof (host));
4962264Sjacobs 
4972264Sjacobs 	for (i = 0; files[i] != NULL; i++) {
4982264Sjacobs 		char name[BUFSIZ];
499*8569SJonathan.Ca@Sun.COM 		struct stat statbuf;
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"),
506*8569SJonathan.Ca@Sun.COM 			    files[i], strerror(errno));
5072264Sjacobs 			return (PAPI_NOT_AUTHORIZED);
5082264Sjacobs 		}
509*8569SJonathan.Ca@Sun.COM 		if (strcmp("standard input", files[i]) != 0) {
510*8569SJonathan.Ca@Sun.COM 			stat(files[i], &statbuf);
511*8569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
512*8569SJonathan.Ca@Sun.COM 				detailed_error(svc,
513*8569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
514*8569SJonathan.Ca@Sun.COM 				    files[i]);
515*8569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
516*8569SJonathan.Ca@Sun.COM 			}
517*8569SJonathan.Ca@Sun.COM 		}
5182264Sjacobs 
5192264Sjacobs 		if (i < 26)
5202264Sjacobs 			key = 'A' + i;
5212264Sjacobs 		else if (i < 52)
5222264Sjacobs 			key = 'a' + (i - 26);
5232264Sjacobs 		else if (i < 62)
5242264Sjacobs 			key = '0' + (i - 52);
5252264Sjacobs 		else {
5262264Sjacobs 			detailed_error(svc,
527*8569SJonathan.Ca@Sun.COM 			    gettext("too many files, truncated at 62"));
5282264Sjacobs 			return (PAPI_OK_SUBST);
5292264Sjacobs 		}
5302264Sjacobs 
5312264Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
5322264Sjacobs 
5332264Sjacobs 		for (j = 0; j < copies; j++)
5342264Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
5352264Sjacobs 		add_lpd_control_line(metadata, 'U', name);
5362264Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
5372264Sjacobs 	}
5382264Sjacobs 
5392264Sjacobs 	return (PAPI_OK);
5402264Sjacobs }
5412264Sjacobs 
5422264Sjacobs papi_status_t
5432264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
5442264Sjacobs 		int *ofd)
5452264Sjacobs {
5462264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
5472264Sjacobs 	int fd;
5482264Sjacobs 	char path[32];
5492264Sjacobs 	char *list[2];
5502264Sjacobs 
5512264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
5522264Sjacobs 		return (PAPI_BAD_ARGUMENT);
5532264Sjacobs 
5542264Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
5552264Sjacobs 	fd = mkstemp(path);
5562264Sjacobs 	write(fd, metadata, strlen(metadata));
5572264Sjacobs 	close(fd);
5582264Sjacobs 
5592264Sjacobs 	list[0] = path;
5602264Sjacobs 	list[1] = NULL;
5612264Sjacobs 
5623915Sceastha 	if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) {
5632264Sjacobs 		switch (errno) {
5642264Sjacobs 		case ENOSPC:
5652264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5662264Sjacobs 			break;
5672264Sjacobs 		case EIO:
5682264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
5692264Sjacobs 			break;
5702264Sjacobs 		case ECONNREFUSED:
5712264Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
5722264Sjacobs 			break;
5732264Sjacobs 		case ENOENT:
5742264Sjacobs 			status = PAPI_NOT_ACCEPTING;
5752264Sjacobs 			break;
5762264Sjacobs 		case EBADMSG:
5772264Sjacobs 		case EBADF:
5782264Sjacobs 			status = PAPI_OK;
5792264Sjacobs 			break;
5802264Sjacobs 		default:
5812264Sjacobs 			status = PAPI_TIMEOUT;
5822264Sjacobs 			break;
5832264Sjacobs 		}
5842264Sjacobs 	} else
5852264Sjacobs 		status = PAPI_OK;
5862264Sjacobs 
5872264Sjacobs 	if (ofd != NULL)
5882264Sjacobs 		*ofd = fd;
5892264Sjacobs 	else
5902264Sjacobs 		close(fd);
5912264Sjacobs 
5922264Sjacobs 	/* read the ID and add it to to the job */
5932264Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
5942264Sjacobs 		int job_id = 0;
5952264Sjacobs 		read(fd, &job_id, sizeof (job_id));
5962264Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
597*8569SJonathan.Ca@Sun.COM 		    "job-id", job_id);
5982264Sjacobs 		close(fd);
5992264Sjacobs 	}
6002264Sjacobs 
6012264Sjacobs 	unlink(path);
6022264Sjacobs 
6032264Sjacobs 	return (status);
6042264Sjacobs }
605