1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
2395c2d302SJonathan Cowper-Andrewes  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpd-job.c 157 2006-04-26 15:07:55Z ktou $ */
29355b4669Sjacobs 
30355b4669Sjacobs 
31355b4669Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
34355b4669Sjacobs #include <unistd.h>
35355b4669Sjacobs #include <errno.h>
36355b4669Sjacobs #include <limits.h>
37355b4669Sjacobs #include <sys/types.h>
38355b4669Sjacobs #include <sys/stat.h>
39355b4669Sjacobs #include <fcntl.h>
40355b4669Sjacobs #include <string.h>
41355b4669Sjacobs #include <pwd.h>
42355b4669Sjacobs #include <libintl.h>
43355b4669Sjacobs #include <papi_impl.h>
44355b4669Sjacobs 
45355b4669Sjacobs enum { LPD_RFC, LPD_SVR4 };
46355b4669Sjacobs 
47355b4669Sjacobs static char
mime_type_to_rfc1179_type(char * mime)48355b4669Sjacobs mime_type_to_rfc1179_type(char *mime)
49355b4669Sjacobs {
50355b4669Sjacobs 	static struct { char *mime; char rfc; } cvt[] = {
510a44ef6dSjacobs 		{ "text/plain", 'f' },
52355b4669Sjacobs 		{ "application/octet-stream", 'l' },
53355b4669Sjacobs 		{ "application/postscript", 'f' }, /* rfc incorrectly has 'o' */
54355b4669Sjacobs 		{ "application/x-pr", 'p' },
55355b4669Sjacobs 		{ "application/x-cif", 'c' },
56355b4669Sjacobs 		{ "application/x-dvi", 'd' },
57355b4669Sjacobs 		{ "application/x-fortran", 'r' },
58355b4669Sjacobs 		{ "application/x-plot", 'g' },
59355b4669Sjacobs 		{ "application/x-ditroff", 'n' },
60355b4669Sjacobs 		{ "application/x-troff", 't' },
61355b4669Sjacobs 		{ "application/x-raster", 'v' },
62355b4669Sjacobs 		{ NULL, 0}
63355b4669Sjacobs 	};
64355b4669Sjacobs 	char result = '\0';
65355b4669Sjacobs 
66355b4669Sjacobs 	if (mime != NULL) {
67355b4669Sjacobs 		int i;
68355b4669Sjacobs 
69355b4669Sjacobs 		for (i = 0; cvt[i].mime != NULL; i++)
70355b4669Sjacobs 			if (strcasecmp(cvt[i].mime, mime) == 0) {
71355b4669Sjacobs 				result = cvt[i].rfc;
72355b4669Sjacobs 				break;
73355b4669Sjacobs 			}
74355b4669Sjacobs 	}
75355b4669Sjacobs 
76355b4669Sjacobs 	return (result);
77355b4669Sjacobs }
78355b4669Sjacobs 
79355b4669Sjacobs static papi_status_t
add_lpd_control_line(char ** metadata,char code,char * value)80355b4669Sjacobs add_lpd_control_line(char **metadata, char code, char *value)
81355b4669Sjacobs {
82355b4669Sjacobs 	size_t size = 0;
83355b4669Sjacobs 	char line[BUFSIZ];
84355b4669Sjacobs 
85355b4669Sjacobs 	if ((metadata == NULL) || (value == NULL))
86355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
87355b4669Sjacobs 
88355b4669Sjacobs 	if (*metadata != NULL)
89355b4669Sjacobs 		size = strlen(*metadata);
90355b4669Sjacobs 	size += strlen(value) + 3;
91355b4669Sjacobs 
92355b4669Sjacobs 	if (*metadata == NULL) {
93355b4669Sjacobs 		*metadata = (char *)calloc(1, size);
94355b4669Sjacobs 	} else {
95355b4669Sjacobs 		void *tmp;
96c1ecd8b9Sjacobs 		tmp = calloc(1, size);
97c1ecd8b9Sjacobs 		if (tmp) {
98c1ecd8b9Sjacobs 			strlcpy(tmp, *metadata, size);
99c1ecd8b9Sjacobs 			free(*metadata);
100355b4669Sjacobs 			*metadata = (char *)tmp;
101*f4593de7SToomas Soome 		} else {
102355b4669Sjacobs 			return (PAPI_TEMPORARY_ERROR);
103*f4593de7SToomas Soome 		}
104355b4669Sjacobs 	}
105355b4669Sjacobs 
106355b4669Sjacobs 	snprintf(line, sizeof (line), "%c%s\n", code, value);
107355b4669Sjacobs 	strlcat(*metadata, line, size);
108355b4669Sjacobs 
109355b4669Sjacobs 	return (PAPI_OK);
110355b4669Sjacobs }
111355b4669Sjacobs 
112355b4669Sjacobs static papi_status_t
add_svr4_control_line(char ** metadata,char code,char * value)113355b4669Sjacobs add_svr4_control_line(char **metadata, char code, char *value)
114355b4669Sjacobs {
115355b4669Sjacobs 
116355b4669Sjacobs 	char line[BUFSIZ];
117355b4669Sjacobs 
118355b4669Sjacobs 	if ((metadata == NULL) || (value == NULL))
119355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
120355b4669Sjacobs 
121355b4669Sjacobs 	snprintf(line, sizeof (line), "%c%s", code, value);
122355b4669Sjacobs 
123355b4669Sjacobs 	return (add_lpd_control_line(metadata, '5', line));
124355b4669Sjacobs }
125355b4669Sjacobs 
126355b4669Sjacobs static papi_status_t
add_hpux_control_line(char ** metadata,char * value)127355b4669Sjacobs add_hpux_control_line(char **metadata, char *value)
128355b4669Sjacobs {
129355b4669Sjacobs 
130355b4669Sjacobs 	char line[BUFSIZ];
131355b4669Sjacobs 
132355b4669Sjacobs 	if ((metadata == NULL) || (value == NULL))
133355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
134355b4669Sjacobs 
135355b4669Sjacobs 	snprintf(line, sizeof (line), " O%s", value);
136355b4669Sjacobs 
137355b4669Sjacobs 	return (add_lpd_control_line(metadata, 'N', line));
138355b4669Sjacobs }
139355b4669Sjacobs 
140355b4669Sjacobs static papi_status_t
add_int_control_line(char ** metadata,char code,int value,int flag)141355b4669Sjacobs add_int_control_line(char **metadata, char code, int value, int flag)
142355b4669Sjacobs {
143355b4669Sjacobs 	char buf[16];
144355b4669Sjacobs 
145355b4669Sjacobs 	snprintf(buf, sizeof (buf), "%d", value);
146355b4669Sjacobs 
147355b4669Sjacobs 	if (flag == LPD_SVR4)
148355b4669Sjacobs 		return (add_svr4_control_line(metadata, code, buf));
149355b4669Sjacobs 	else
150355b4669Sjacobs 		return (add_lpd_control_line(metadata, code, buf));
151355b4669Sjacobs }
152355b4669Sjacobs 
153355b4669Sjacobs static papi_status_t
lpd_add_rfc1179_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)154355b4669Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes,
155*f4593de7SToomas Soome     char **metadata, papi_attribute_t ***used)
156355b4669Sjacobs {
157355b4669Sjacobs 	papi_status_t status = PAPI_OK;
158355b4669Sjacobs 	char *s;
159355b4669Sjacobs 	int integer;
160355b4669Sjacobs 	char bool;
161355b4669Sjacobs 	char host[BUFSIZ];
162355b4669Sjacobs 	char *user = "nobody";
163355b4669Sjacobs 	uid_t uid = getuid();
164355b4669Sjacobs 	struct passwd *pw;
165286caa64SKeerthi Kondaka 	char *h1;
166355b4669Sjacobs 
167355b4669Sjacobs 	if (svc == NULL)
168355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
169355b4669Sjacobs 
170355b4669Sjacobs 	/* There is nothing to do */
171355b4669Sjacobs 	if (attributes == NULL)
172355b4669Sjacobs 		return (PAPI_OK);
173355b4669Sjacobs 
174355b4669Sjacobs 	gethostname(host, sizeof (host));
175286caa64SKeerthi Kondaka 	if (papiAttributeListGetString(attributes, NULL,
176286caa64SKeerthi Kondaka 	    "job-originating-host-name", &h1) == PAPI_OK) {
177286caa64SKeerthi Kondaka 		papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
178286caa64SKeerthi Kondaka 		    "job-host", h1);
179286caa64SKeerthi Kondaka 	}
180355b4669Sjacobs 	add_lpd_control_line(metadata, 'H', host);
181355b4669Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
18295c2d302SJonathan Cowper-Andrewes 	    "job-originating-host-name", host);
183355b4669Sjacobs 
184355b4669Sjacobs 	if ((pw = getpwuid(uid)) != NULL)
185355b4669Sjacobs 		user = pw->pw_name;
186355b4669Sjacobs 	if (uid == 0)
187355b4669Sjacobs 		papiAttributeListGetString(svc->attributes, NULL, "username",
18895c2d302SJonathan Cowper-Andrewes 		    &user);
189355b4669Sjacobs 	add_lpd_control_line(metadata, 'P', user);
190355b4669Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
19195c2d302SJonathan Cowper-Andrewes 	    "job-originating-user-name", user);
192355b4669Sjacobs 
193355b4669Sjacobs 	/* Class for Banner Page */
194355b4669Sjacobs 	s = NULL;
195355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
196355b4669Sjacobs 	if (s != NULL) {
197355b4669Sjacobs 		add_lpd_control_line(metadata, 'C', s);
198355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
19995c2d302SJonathan Cowper-Andrewes 		    "rfc-1179-class", s);
200355b4669Sjacobs 	}
201355b4669Sjacobs 
202355b4669Sjacobs 	/* Print Banner Page */
203355b4669Sjacobs 	s = NULL;
204355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
205355b4669Sjacobs 	if ((s != NULL) && (strcmp(s, "standard") == 0)) {
206355b4669Sjacobs 		add_lpd_control_line(metadata, 'L', user);
207355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
20895c2d302SJonathan Cowper-Andrewes 		    "job-sheets", s);
209355b4669Sjacobs 	}
210355b4669Sjacobs 
211355b4669Sjacobs 	/* Jobname */
212355b4669Sjacobs 	s = NULL;
213355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-name", &s);
214355b4669Sjacobs 	if (s != NULL) {
215355b4669Sjacobs 		add_lpd_control_line(metadata, 'J', s);
216355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
21795c2d302SJonathan Cowper-Andrewes 		    "job-name", s);
218355b4669Sjacobs 	}
219355b4669Sjacobs 
220355b4669Sjacobs 	/* User to mail when job is done - lpr -m */
221355b4669Sjacobs 	bool = PAPI_FALSE;
222355b4669Sjacobs 	papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
223355b4669Sjacobs 	if (bool == PAPI_TRUE) {
224355b4669Sjacobs 		add_lpd_control_line(metadata, 'M', user);
225355b4669Sjacobs 		papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
22695c2d302SJonathan Cowper-Andrewes 		    "rfc-1179-mail", bool);
227355b4669Sjacobs 	}
228355b4669Sjacobs 
229355b4669Sjacobs 	/* Title for pr */
230355b4669Sjacobs 	s = NULL;
231355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "pr-title", &s);
232355b4669Sjacobs 	if (s != NULL) {
233355b4669Sjacobs 		add_lpd_control_line(metadata, 'T', s);
234355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
23595c2d302SJonathan Cowper-Andrewes 		    "pr-title", s);
236355b4669Sjacobs 	}
237355b4669Sjacobs 
238355b4669Sjacobs 	/* Indent - used with pr filter */
239355b4669Sjacobs 	integer = 0;
240355b4669Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
241355b4669Sjacobs 	if (integer >= 1) {
242355b4669Sjacobs 		add_int_control_line(metadata, 'I', integer, LPD_RFC);
243355b4669Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
24495c2d302SJonathan Cowper-Andrewes 		    "pr-indent", integer);
245355b4669Sjacobs 	}
246355b4669Sjacobs 
247355b4669Sjacobs 	/* Width - used with pr filter */
248355b4669Sjacobs 	integer = 0;
249355b4669Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
250355b4669Sjacobs 	if (integer >= 1) {
251355b4669Sjacobs 		add_int_control_line(metadata, 'W', integer, LPD_RFC);
252355b4669Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
25395c2d302SJonathan Cowper-Andrewes 		    "pr-width", integer);
254355b4669Sjacobs 	}
255355b4669Sjacobs 
256355b4669Sjacobs 	/* file with Times Roman font lpr -1	*/
257355b4669Sjacobs 	s = NULL;
258355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
259355b4669Sjacobs 	if (s != NULL) {
260355b4669Sjacobs 		add_lpd_control_line(metadata, '1', s);
261355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
26295c2d302SJonathan Cowper-Andrewes 		    "rfc-1179-font-r", s);
263355b4669Sjacobs 	}
264355b4669Sjacobs 
265355b4669Sjacobs 	/* file with Times Roman font lpr -2	*/
266355b4669Sjacobs 	s = NULL;
267355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
268355b4669Sjacobs 	if (s != NULL) {
269355b4669Sjacobs 		add_lpd_control_line(metadata, '2', s);
270355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
27195c2d302SJonathan Cowper-Andrewes 		    "rfc-1179-font-i", s);
272355b4669Sjacobs 	}
273355b4669Sjacobs 
274355b4669Sjacobs 	/* file with Times Roman font lpr -3	*/
275355b4669Sjacobs 	s = NULL;
276355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
277355b4669Sjacobs 	if (s != NULL) {
278355b4669Sjacobs 		add_lpd_control_line(metadata, '3', s);
279355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
28095c2d302SJonathan Cowper-Andrewes 		    "rfc-1179-font-b", s);
281355b4669Sjacobs 	}
282355b4669Sjacobs 
283355b4669Sjacobs 	/* file with Times Roman font lpr -4	*/
284355b4669Sjacobs 	s = NULL;
285355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
286355b4669Sjacobs 	if (s != NULL) {
287355b4669Sjacobs 		add_lpd_control_line(metadata, '4', s);
288355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
28995c2d302SJonathan Cowper-Andrewes 		    "rfc-1179-font-s", s);
290355b4669Sjacobs 	}
291355b4669Sjacobs 
292b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	/*
293b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	 * The document format needs to be added, but the control line
294b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	 * should be added when the filenames are figured out.
295b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	 */
296b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	s = NULL;
297b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	papiAttributeListGetString(attributes, NULL, "document-format", &s);
298b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	if (s != NULL) {
299b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
300b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 		    "document-format", s);
301b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	}
302b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 
303355b4669Sjacobs 	return (status);
304355b4669Sjacobs }
305355b4669Sjacobs 
306fd06a699Sjacobs static char *
unused_attributes(papi_attribute_t ** list,papi_attribute_t ** used)307fd06a699Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used)
308fd06a699Sjacobs {
309fd06a699Sjacobs 	char *result = NULL;
310fd06a699Sjacobs 	char **names = NULL;
311fd06a699Sjacobs 	int i;
312fd06a699Sjacobs 
313fd06a699Sjacobs 	if ((list == NULL) || (used == NULL))
314fd06a699Sjacobs 		return (NULL);
315fd06a699Sjacobs 
316fd06a699Sjacobs 	for (i = 0; used[i] != NULL; i++)
317fd06a699Sjacobs 		list_append(&names, used[i]->name);
318fd06a699Sjacobs 
319fd06a699Sjacobs 	if (names != NULL) {
320fd06a699Sjacobs 		papi_attribute_t **unused = NULL;
321fd06a699Sjacobs 
322fd06a699Sjacobs 		/* add these to the list of things to ignore */
323fd06a699Sjacobs 		list_append(&names, "document-format");
324fd06a699Sjacobs 		list_append(&names, "copies");
325fd06a699Sjacobs 
326fd06a699Sjacobs 		split_and_copy_attributes(names, list, NULL, &unused);
327fd06a699Sjacobs 		if (unused != NULL) {
328fd06a699Sjacobs 			size_t size = 0;
329fd06a699Sjacobs 
330fd06a699Sjacobs 			do {
331fd06a699Sjacobs 				size += 1024;
332fd06a699Sjacobs 				if (result != NULL)
333fd06a699Sjacobs 					free(result);
334fd06a699Sjacobs 				result = calloc(1, size);
335fd06a699Sjacobs 			} while (papiAttributeListToString(unused, " ",
33695c2d302SJonathan Cowper-Andrewes 			    result, size) != PAPI_OK);
337fd06a699Sjacobs 			papiAttributeListFree(unused);
338fd06a699Sjacobs 		}
339fd06a699Sjacobs 		free(names);
340fd06a699Sjacobs 	}
341fd06a699Sjacobs 
342fd06a699Sjacobs 	return (result);
343fd06a699Sjacobs }
344fd06a699Sjacobs 
345355b4669Sjacobs /*
346355b4669Sjacobs  * lpd_add_svr4_attributes
347355b4669Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
348355b4669Sjacobs  */
349355b4669Sjacobs static papi_status_t
lpd_add_svr4_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)350355b4669Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
351*f4593de7SToomas Soome     char **metadata, papi_attribute_t ***used)
352355b4669Sjacobs {
35343b9c050Sjacobs 	papi_attribute_t *tmp[2];
354355b4669Sjacobs 	char *s;
355355b4669Sjacobs 	int integer;
356355b4669Sjacobs 
357355b4669Sjacobs 	if (svc == NULL)
358355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
359355b4669Sjacobs 
360355b4669Sjacobs 	/* media */
361355b4669Sjacobs 	s = NULL;
362355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
363355b4669Sjacobs 	if (s != NULL) {
364355b4669Sjacobs 		add_svr4_control_line(metadata, 'f', s);
365355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
36695c2d302SJonathan Cowper-Andrewes 		    "media", s);
367355b4669Sjacobs 	}
368355b4669Sjacobs 
369355b4669Sjacobs 	/* Handling */
370355b4669Sjacobs 	s = NULL;
37143b9c050Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
372fe37c54dS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	if ((s != NULL) && (strcmp(s, "indefinite") == 0)) {
373355b4669Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
374355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
37595c2d302SJonathan Cowper-Andrewes 		    "job-hold-until", "indefinite");
376fe37c54dS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	} else if ((s != NULL) && (strcmp(s, "no-hold") == 0)) {
37743b9c050Sjacobs 		add_svr4_control_line(metadata, 'H', "immediate");
378355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
37995c2d302SJonathan Cowper-Andrewes 		    "job-hold-until", "no-hold");
38043b9c050Sjacobs 	} else if (s != NULL) {
38143b9c050Sjacobs 		add_svr4_control_line(metadata, 'H', s);
382355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
38395c2d302SJonathan Cowper-Andrewes 		    "job-hold-until", s);
384355b4669Sjacobs 	}
385355b4669Sjacobs 
386355b4669Sjacobs 	/* Pages */
387355b4669Sjacobs 	s = NULL;
388*f4593de7SToomas Soome 	memset(tmp, 0, sizeof (tmp));
38943b9c050Sjacobs 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
39043b9c050Sjacobs 	if (tmp[0] != NULL) {
39143b9c050Sjacobs 		char buf[BUFSIZ];
39243b9c050Sjacobs 
39343b9c050Sjacobs 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
39443b9c050Sjacobs 		if ((s = strchr(buf, '=')) != NULL) {
39543b9c050Sjacobs 			add_svr4_control_line(metadata, 'P', ++s);
39643b9c050Sjacobs 			papiAttributeListAddString(used, PAPI_ATTR_EXCL,
39795c2d302SJonathan Cowper-Andrewes 			    "page-ranges", s);
39843b9c050Sjacobs 		}
399355b4669Sjacobs 	}
400355b4669Sjacobs 
401355b4669Sjacobs 	/* Priority : lp -q */
402355b4669Sjacobs 	integer = -1;
40343b9c050Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "job-priority", &integer);
404355b4669Sjacobs 	if (integer != -1) {
40543b9c050Sjacobs 		integer = 40 - (integer / 2.5);
406355b4669Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
407355b4669Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
40895c2d302SJonathan Cowper-Andrewes 		    "job-priority", integer);
409355b4669Sjacobs 	}
410355b4669Sjacobs 
411355b4669Sjacobs 	/* Charset : lp -S */
412355b4669Sjacobs 	s = NULL;
413355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
414355b4669Sjacobs 	if (s != NULL) {
415355b4669Sjacobs 		add_svr4_control_line(metadata, 'S', s);
416355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
41795c2d302SJonathan Cowper-Andrewes 		    "lp-charset", s);
418355b4669Sjacobs 	}
419355b4669Sjacobs 
420355b4669Sjacobs 	/* Type : done when adding file  */
421355b4669Sjacobs 
422355b4669Sjacobs 	/* Mode : lp -y */
423355b4669Sjacobs 	s = NULL;
424355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
425355b4669Sjacobs 	if (s != NULL) {
426355b4669Sjacobs 		add_svr4_control_line(metadata, 'y', s);
427355b4669Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
42895c2d302SJonathan Cowper-Andrewes 		    "lp-modes", s);
429355b4669Sjacobs 	}
430355b4669Sjacobs 
431fd06a699Sjacobs 	/* Options lp -o are handled elsewhere */
432fd06a699Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
433fd06a699Sjacobs 		add_lpd_control_line(metadata, 'O', s);
434fd06a699Sjacobs 		free(s);
435355b4669Sjacobs 	}
436355b4669Sjacobs 
437355b4669Sjacobs 	return (PAPI_OK);
438355b4669Sjacobs }
439355b4669Sjacobs 
440355b4669Sjacobs papi_status_t
lpd_add_hpux_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)441355b4669Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
442*f4593de7SToomas Soome     char **metadata, papi_attribute_t ***used)
443355b4669Sjacobs {
444355b4669Sjacobs 	char *s = NULL;
445355b4669Sjacobs 
446355b4669Sjacobs 	/* Options lp -o */
447fd06a699Sjacobs 	if ((s = unused_attributes(attributes, *used)) != NULL) {
448355b4669Sjacobs 		add_hpux_control_line(metadata, s);
449fd06a699Sjacobs 		free(s);
450355b4669Sjacobs 	}
451355b4669Sjacobs 
452355b4669Sjacobs 	return (PAPI_OK);
453355b4669Sjacobs }
454355b4669Sjacobs 
455355b4669Sjacobs papi_status_t
lpd_job_add_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)456355b4669Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
457*f4593de7SToomas Soome     char **metadata, papi_attribute_t ***used)
458355b4669Sjacobs {
459355b4669Sjacobs 	if ((svc == NULL) || (metadata == NULL))
460355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
461355b4669Sjacobs 
462355b4669Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
463355b4669Sjacobs 
464fd06a699Sjacobs 	/* add protocol extensions if applicable */
465355b4669Sjacobs 	if (svc->uri->fragment != NULL) {
466355b4669Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
467355b4669Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
468355b4669Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
46995c2d302SJonathan Cowper-Andrewes 			    used);
470355b4669Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
471355b4669Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
47295c2d302SJonathan Cowper-Andrewes 			    used);
473355b4669Sjacobs 		/*
474355b4669Sjacobs 		 * others could be added here:
475355b4669Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
476355b4669Sjacobs 		 */
477355b4669Sjacobs 	}
478355b4669Sjacobs 
479355b4669Sjacobs 	return (PAPI_OK);
480355b4669Sjacobs }
481355b4669Sjacobs 
482355b4669Sjacobs papi_status_t
lpd_job_add_files(service_t * svc,papi_attribute_t ** attributes,char ** files,char ** metadata,papi_attribute_t *** used)483355b4669Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
484*f4593de7SToomas Soome     char **files, char **metadata, papi_attribute_t ***used)
485355b4669Sjacobs {
4860a44ef6dSjacobs 	char *format = "text/plain";
487355b4669Sjacobs 	char rfc_fmt = 'l';
488355b4669Sjacobs 	int copies = 1;
489355b4669Sjacobs 	char host[BUFSIZ];
490355b4669Sjacobs 	int i;
491355b4669Sjacobs 
492355b4669Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
493355b4669Sjacobs 	    (metadata == NULL))
494355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
495355b4669Sjacobs 
496355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
49795c2d302SJonathan Cowper-Andrewes 	    &format);
498355b4669Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
49995c2d302SJonathan Cowper-Andrewes 	    "document-format", format);
500355b4669Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
501355b4669Sjacobs 		if ((svc->uri->fragment != NULL) &&
502355b4669Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
50395c2d302SJonathan Cowper-Andrewes 		    (strcasecmp(svc->uri->fragment, "svr4") == 0)))
504355b4669Sjacobs 			add_svr4_control_line(metadata, 'T', format);
505355b4669Sjacobs 		rfc_fmt = 'l';
506355b4669Sjacobs 	}
507355b4669Sjacobs 
508355b4669Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
509355b4669Sjacobs 	if (copies < 1)
510355b4669Sjacobs 		copies = 1;
511355b4669Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
512355b4669Sjacobs 
513355b4669Sjacobs 	gethostname(host, sizeof (host));
514355b4669Sjacobs 
515355b4669Sjacobs 	for (i = 0; files[i] != NULL; i++) {
516355b4669Sjacobs 		char name[BUFSIZ];
51795c2d302SJonathan Cowper-Andrewes 		struct stat statbuf;
518355b4669Sjacobs 		char key;
519355b4669Sjacobs 		int j;
520355b4669Sjacobs 
521355b4669Sjacobs 		if ((strcmp("standard input", files[i]) != 0) &&
522355b4669Sjacobs 		    (access(files[i], R_OK) < 0)) {
523355b4669Sjacobs 			detailed_error(svc, gettext("aborting request, %s: %s"),
52495c2d302SJonathan Cowper-Andrewes 			    files[i], strerror(errno));
525355b4669Sjacobs 			return (PAPI_NOT_AUTHORIZED);
526355b4669Sjacobs 		}
52795c2d302SJonathan Cowper-Andrewes 		if (strcmp("standard input", files[i]) != 0) {
528a5669307SJonathan Cowper-Andrewes 			if (stat(files[i], &statbuf) < 0) {
529a5669307SJonathan Cowper-Andrewes 				detailed_error(svc,
530a5669307SJonathan Cowper-Andrewes 				    gettext("Cannot access file: %s: %s"),
531a5669307SJonathan Cowper-Andrewes 				    files[i], strerror(errno));
532a5669307SJonathan Cowper-Andrewes 				return (PAPI_DOCUMENT_ACCESS_ERROR);
533a5669307SJonathan Cowper-Andrewes 			}
53495c2d302SJonathan Cowper-Andrewes 			if (statbuf.st_size == 0) {
53595c2d302SJonathan Cowper-Andrewes 				detailed_error(svc,
53695c2d302SJonathan Cowper-Andrewes 				    gettext("Zero byte (empty) file: %s"),
53795c2d302SJonathan Cowper-Andrewes 				    files[i]);
53895c2d302SJonathan Cowper-Andrewes 				return (PAPI_BAD_ARGUMENT);
53995c2d302SJonathan Cowper-Andrewes 			}
54095c2d302SJonathan Cowper-Andrewes 		}
541355b4669Sjacobs 
542355b4669Sjacobs 		if (i < 26)
543355b4669Sjacobs 			key = 'A' + i;
544355b4669Sjacobs 		else if (i < 52)
545355b4669Sjacobs 			key = 'a' + (i - 26);
546355b4669Sjacobs 		else if (i < 62)
547355b4669Sjacobs 			key = '0' + (i - 52);
548355b4669Sjacobs 		else {
549355b4669Sjacobs 			detailed_error(svc,
55095c2d302SJonathan Cowper-Andrewes 			    gettext("too many files, truncated at 62"));
551355b4669Sjacobs 			return (PAPI_OK_SUBST);
552355b4669Sjacobs 		}
553355b4669Sjacobs 
554355b4669Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
555355b4669Sjacobs 
556355b4669Sjacobs 		for (j = 0; j < copies; j++)
557355b4669Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
558355b4669Sjacobs 		add_lpd_control_line(metadata, 'U', name);
559355b4669Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
560355b4669Sjacobs 	}
561355b4669Sjacobs 
562355b4669Sjacobs 	return (PAPI_OK);
563355b4669Sjacobs }
564355b4669Sjacobs 
565355b4669Sjacobs papi_status_t
lpd_submit_job(service_t * svc,char * metadata,papi_attribute_t *** attributes,int * ofd)566355b4669Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
567*f4593de7SToomas Soome     int *ofd)
568355b4669Sjacobs {
569355b4669Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
570355b4669Sjacobs 	int fd;
571355b4669Sjacobs 	char path[32];
572355b4669Sjacobs 	char *list[2];
573355b4669Sjacobs 
574355b4669Sjacobs 	if ((svc == NULL) || (metadata == NULL))
575355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
576355b4669Sjacobs 
577355b4669Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
578355b4669Sjacobs 	fd = mkstemp(path);
579355b4669Sjacobs 	write(fd, metadata, strlen(metadata));
580355b4669Sjacobs 	close(fd);
581355b4669Sjacobs 
582355b4669Sjacobs 	list[0] = path;
583355b4669Sjacobs 	list[1] = NULL;
584355b4669Sjacobs 
5854bd2082fSceastha 	if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) {
586355b4669Sjacobs 		switch (errno) {
587355b4669Sjacobs 		case ENOSPC:
588355b4669Sjacobs 			status = PAPI_TEMPORARY_ERROR;
589355b4669Sjacobs 			break;
590355b4669Sjacobs 		case EIO:
591355b4669Sjacobs 			status = PAPI_TEMPORARY_ERROR;
592355b4669Sjacobs 			break;
593355b4669Sjacobs 		case ECONNREFUSED:
594355b4669Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
595355b4669Sjacobs 			break;
596355b4669Sjacobs 		case ENOENT:
597355b4669Sjacobs 			status = PAPI_NOT_ACCEPTING;
598355b4669Sjacobs 			break;
599355b4669Sjacobs 		case EBADMSG:
600355b4669Sjacobs 		case EBADF:
601355b4669Sjacobs 			status = PAPI_OK;
602355b4669Sjacobs 			break;
603355b4669Sjacobs 		default:
604355b4669Sjacobs 			status = PAPI_TIMEOUT;
605355b4669Sjacobs 			break;
606355b4669Sjacobs 		}
607*f4593de7SToomas Soome 	} else {
608355b4669Sjacobs 		status = PAPI_OK;
609*f4593de7SToomas Soome 	}
610355b4669Sjacobs 
611355b4669Sjacobs 	if (ofd != NULL)
612355b4669Sjacobs 		*ofd = fd;
613355b4669Sjacobs 	else
614355b4669Sjacobs 		close(fd);
615355b4669Sjacobs 
616355b4669Sjacobs 	/* read the ID and add it to to the job */
617355b4669Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
618355b4669Sjacobs 		int job_id = 0;
619355b4669Sjacobs 		read(fd, &job_id, sizeof (job_id));
620355b4669Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
62195c2d302SJonathan Cowper-Andrewes 		    "job-id", job_id);
622355b4669Sjacobs 		close(fd);
623355b4669Sjacobs 	}
624355b4669Sjacobs 
625355b4669Sjacobs 	unlink(path);
626355b4669Sjacobs 
627355b4669Sjacobs 	return (status);
628355b4669Sjacobs }
629