1*2264Sjacobs /*
2*2264Sjacobs  * CDDL HEADER START
3*2264Sjacobs  *
4*2264Sjacobs  * The contents of this file are subject to the terms of the
5*2264Sjacobs  * Common Development and Distribution License (the "License").
6*2264Sjacobs  * You may not use this file except in compliance with the License.
7*2264Sjacobs  *
8*2264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs  * See the License for the specific language governing permissions
11*2264Sjacobs  * and limitations under the License.
12*2264Sjacobs  *
13*2264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs  *
19*2264Sjacobs  * CDDL HEADER END
20*2264Sjacobs  */
21*2264Sjacobs 
22*2264Sjacobs /*
23*2264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2264Sjacobs  * Use is subject to license terms.
25*2264Sjacobs  *
26*2264Sjacobs  */
27*2264Sjacobs 
28*2264Sjacobs /* $Id: lpd-job.c 157 2006-04-26 15:07:55Z ktou $ */
29*2264Sjacobs 
30*2264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*2264Sjacobs 
32*2264Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
33*2264Sjacobs #include <stdio.h>
34*2264Sjacobs #include <stdlib.h>
35*2264Sjacobs #include <unistd.h>
36*2264Sjacobs #include <errno.h>
37*2264Sjacobs #include <limits.h>
38*2264Sjacobs #include <sys/types.h>
39*2264Sjacobs #include <sys/stat.h>
40*2264Sjacobs #include <fcntl.h>
41*2264Sjacobs #include <string.h>
42*2264Sjacobs #include <pwd.h>
43*2264Sjacobs #include <libintl.h>
44*2264Sjacobs #include <papi_impl.h>
45*2264Sjacobs 
46*2264Sjacobs enum { LPD_RFC, LPD_SVR4 };
47*2264Sjacobs 
48*2264Sjacobs static char
49*2264Sjacobs mime_type_to_rfc1179_type(char *mime)
50*2264Sjacobs {
51*2264Sjacobs 	static struct { char *mime; char rfc; } cvt[] = {
52*2264Sjacobs 		{ "plain/text", 'f' },
53*2264Sjacobs 		{ "application/octet-stream", 'l' },
54*2264Sjacobs 		{ "application/postscript", 'f' }, /* rfc incorrectly has 'o' */
55*2264Sjacobs 		{ "application/x-pr", 'p' },
56*2264Sjacobs 		{ "application/x-cif", 'c' },
57*2264Sjacobs 		{ "application/x-dvi", 'd' },
58*2264Sjacobs 		{ "application/x-fortran", 'r' },
59*2264Sjacobs 		{ "application/x-plot", 'g' },
60*2264Sjacobs 		{ "application/x-ditroff", 'n' },
61*2264Sjacobs 		{ "application/x-troff", 't' },
62*2264Sjacobs 		{ "application/x-raster", 'v' },
63*2264Sjacobs 		{ NULL, 0}
64*2264Sjacobs 	};
65*2264Sjacobs 	char result = '\0';
66*2264Sjacobs 
67*2264Sjacobs 	if (mime != NULL) {
68*2264Sjacobs 		int i;
69*2264Sjacobs 
70*2264Sjacobs 		for (i = 0; cvt[i].mime != NULL; i++)
71*2264Sjacobs 			if (strcasecmp(cvt[i].mime, mime) == 0) {
72*2264Sjacobs 				result = cvt[i].rfc;
73*2264Sjacobs 				break;
74*2264Sjacobs 			}
75*2264Sjacobs 	}
76*2264Sjacobs 
77*2264Sjacobs 	return (result);
78*2264Sjacobs }
79*2264Sjacobs 
80*2264Sjacobs static papi_status_t
81*2264Sjacobs add_lpd_control_line(char **metadata, char code, char *value)
82*2264Sjacobs {
83*2264Sjacobs 	size_t size = 0;
84*2264Sjacobs 	char line[BUFSIZ];
85*2264Sjacobs 
86*2264Sjacobs 	if ((metadata == NULL) || (value == NULL))
87*2264Sjacobs 		return (PAPI_BAD_REQUEST);
88*2264Sjacobs 
89*2264Sjacobs 	if (*metadata != NULL)
90*2264Sjacobs 		size = strlen(*metadata);
91*2264Sjacobs 	size += strlen(value) + 3;
92*2264Sjacobs 
93*2264Sjacobs 	if (*metadata == NULL) {
94*2264Sjacobs 		*metadata = (char *)calloc(1, size);
95*2264Sjacobs 	} else {
96*2264Sjacobs 		void *tmp;
97*2264Sjacobs 		tmp = realloc(*metadata, size);
98*2264Sjacobs 		if (tmp)
99*2264Sjacobs 			*metadata = (char *)tmp;
100*2264Sjacobs 		else
101*2264Sjacobs 			return (PAPI_TEMPORARY_ERROR);
102*2264Sjacobs 	}
103*2264Sjacobs 
104*2264Sjacobs 	snprintf(line, sizeof (line), "%c%s\n", code, value);
105*2264Sjacobs 	strlcat(*metadata, line, size);
106*2264Sjacobs 
107*2264Sjacobs 	return (PAPI_OK);
108*2264Sjacobs }
109*2264Sjacobs 
110*2264Sjacobs static papi_status_t
111*2264Sjacobs add_svr4_control_line(char **metadata, char code, char *value)
112*2264Sjacobs {
113*2264Sjacobs 
114*2264Sjacobs 	char line[BUFSIZ];
115*2264Sjacobs 
116*2264Sjacobs 	if ((metadata == NULL) || (value == NULL))
117*2264Sjacobs 		return (PAPI_BAD_REQUEST);
118*2264Sjacobs 
119*2264Sjacobs 	snprintf(line, sizeof (line), "%c%s", code, value);
120*2264Sjacobs 
121*2264Sjacobs 	return (add_lpd_control_line(metadata, '5', line));
122*2264Sjacobs }
123*2264Sjacobs 
124*2264Sjacobs static papi_status_t
125*2264Sjacobs add_hpux_control_line(char **metadata, char *value)
126*2264Sjacobs {
127*2264Sjacobs 
128*2264Sjacobs 	char line[BUFSIZ];
129*2264Sjacobs 
130*2264Sjacobs 	if ((metadata == NULL) || (value == NULL))
131*2264Sjacobs 		return (PAPI_BAD_REQUEST);
132*2264Sjacobs 
133*2264Sjacobs 	snprintf(line, sizeof (line), " O%s", value);
134*2264Sjacobs 
135*2264Sjacobs 	return (add_lpd_control_line(metadata, 'N', line));
136*2264Sjacobs }
137*2264Sjacobs 
138*2264Sjacobs static papi_status_t
139*2264Sjacobs add_int_control_line(char **metadata, char code, int value, int flag)
140*2264Sjacobs {
141*2264Sjacobs 	char buf[16];
142*2264Sjacobs 
143*2264Sjacobs 	snprintf(buf, sizeof (buf), "%d", value);
144*2264Sjacobs 
145*2264Sjacobs 	if (flag == LPD_SVR4)
146*2264Sjacobs 		return (add_svr4_control_line(metadata, code, buf));
147*2264Sjacobs 	else
148*2264Sjacobs 		return (add_lpd_control_line(metadata, code, buf));
149*2264Sjacobs }
150*2264Sjacobs 
151*2264Sjacobs static papi_status_t
152*2264Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes,
153*2264Sjacobs 		char **metadata, papi_attribute_t ***used)
154*2264Sjacobs {
155*2264Sjacobs 	papi_status_t status = PAPI_OK;
156*2264Sjacobs 	char *s;
157*2264Sjacobs 	int integer;
158*2264Sjacobs 	char bool;
159*2264Sjacobs 	char host[BUFSIZ];
160*2264Sjacobs 	char *user = "nobody";
161*2264Sjacobs 	uid_t uid = getuid();
162*2264Sjacobs 	struct passwd *pw;
163*2264Sjacobs 
164*2264Sjacobs 	if (svc == NULL)
165*2264Sjacobs 		return (PAPI_BAD_REQUEST);
166*2264Sjacobs 
167*2264Sjacobs 	/* There is nothing to do */
168*2264Sjacobs 	if (attributes == NULL)
169*2264Sjacobs 		return (PAPI_OK);
170*2264Sjacobs 
171*2264Sjacobs 	gethostname(host, sizeof (host));
172*2264Sjacobs 	add_lpd_control_line(metadata, 'H', host);
173*2264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
174*2264Sjacobs 			"job-originating-host-name", host);
175*2264Sjacobs 
176*2264Sjacobs 	if ((pw = getpwuid(uid)) != NULL)
177*2264Sjacobs 		user = pw->pw_name;
178*2264Sjacobs 	if (uid == 0)
179*2264Sjacobs 		papiAttributeListGetString(svc->attributes, NULL, "username",
180*2264Sjacobs 			&user);
181*2264Sjacobs 	add_lpd_control_line(metadata, 'P', user);
182*2264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
183*2264Sjacobs 			"job-originating-user-name", user);
184*2264Sjacobs 
185*2264Sjacobs 	/* Class for Banner Page */
186*2264Sjacobs 	s = NULL;
187*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
188*2264Sjacobs 	if (s != NULL) {
189*2264Sjacobs 		add_lpd_control_line(metadata, 'C', s);
190*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
191*2264Sjacobs 				"rfc-1179-class", s);
192*2264Sjacobs 	}
193*2264Sjacobs 
194*2264Sjacobs 	/* Print Banner Page */
195*2264Sjacobs 	s = NULL;
196*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
197*2264Sjacobs 	if ((s != NULL) && (strcmp(s, "standard") == 0)) {
198*2264Sjacobs 		add_lpd_control_line(metadata, 'L', user);
199*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
200*2264Sjacobs 				"job-sheets", s);
201*2264Sjacobs 	}
202*2264Sjacobs 
203*2264Sjacobs 	/* Jobname */
204*2264Sjacobs 	s = NULL;
205*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job-name", &s);
206*2264Sjacobs 	if (s != NULL) {
207*2264Sjacobs 		add_lpd_control_line(metadata, 'J', s);
208*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
209*2264Sjacobs 				"job-name", s);
210*2264Sjacobs 	}
211*2264Sjacobs 
212*2264Sjacobs 	/* User to mail when job is done - lpr -m */
213*2264Sjacobs 	bool = PAPI_FALSE;
214*2264Sjacobs 	papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
215*2264Sjacobs 	if (bool == PAPI_TRUE) {
216*2264Sjacobs 		add_lpd_control_line(metadata, 'M', user);
217*2264Sjacobs 		papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
218*2264Sjacobs 				"rfc-1179-mail", bool);
219*2264Sjacobs 	}
220*2264Sjacobs 
221*2264Sjacobs 	/* Title for pr */
222*2264Sjacobs 	s = NULL;
223*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "pr-title", &s);
224*2264Sjacobs 	if (s != NULL) {
225*2264Sjacobs 		add_lpd_control_line(metadata, 'T', s);
226*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
227*2264Sjacobs 				"pr-title", s);
228*2264Sjacobs 	}
229*2264Sjacobs 
230*2264Sjacobs 	/* Indent - used with pr filter */
231*2264Sjacobs 	integer = 0;
232*2264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
233*2264Sjacobs 	if (integer >= 1) {
234*2264Sjacobs 		add_int_control_line(metadata, 'I', integer, LPD_RFC);
235*2264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
236*2264Sjacobs 				"pr-indent", integer);
237*2264Sjacobs 	}
238*2264Sjacobs 
239*2264Sjacobs 	/* Width - used with pr filter */
240*2264Sjacobs 	integer = 0;
241*2264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
242*2264Sjacobs 	if (integer >= 1) {
243*2264Sjacobs 		add_int_control_line(metadata, 'W', integer, LPD_RFC);
244*2264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
245*2264Sjacobs 				"pr-width", integer);
246*2264Sjacobs 	}
247*2264Sjacobs 
248*2264Sjacobs 	/* file with Times Roman font lpr -1	*/
249*2264Sjacobs 	s = NULL;
250*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
251*2264Sjacobs 	if (s != NULL) {
252*2264Sjacobs 		add_lpd_control_line(metadata, '1', s);
253*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
254*2264Sjacobs 				"rfc-1179-font-r", s);
255*2264Sjacobs 	}
256*2264Sjacobs 
257*2264Sjacobs 	/* file with Times Roman font lpr -2	*/
258*2264Sjacobs 	s = NULL;
259*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
260*2264Sjacobs 	if (s != NULL) {
261*2264Sjacobs 		add_lpd_control_line(metadata, '2', s);
262*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
263*2264Sjacobs 				"rfc-1179-font-i", s);
264*2264Sjacobs 	}
265*2264Sjacobs 
266*2264Sjacobs 	/* file with Times Roman font lpr -3	*/
267*2264Sjacobs 	s = NULL;
268*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
269*2264Sjacobs 	if (s != NULL) {
270*2264Sjacobs 		add_lpd_control_line(metadata, '3', s);
271*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
272*2264Sjacobs 				"rfc-1179-font-b", s);
273*2264Sjacobs 	}
274*2264Sjacobs 
275*2264Sjacobs 	/* file with Times Roman font lpr -4	*/
276*2264Sjacobs 	s = NULL;
277*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
278*2264Sjacobs 	if (s != NULL) {
279*2264Sjacobs 		add_lpd_control_line(metadata, '4', s);
280*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
281*2264Sjacobs 				"rfc-1179-font-s", s);
282*2264Sjacobs 	}
283*2264Sjacobs 
284*2264Sjacobs 	return (status);
285*2264Sjacobs }
286*2264Sjacobs 
287*2264Sjacobs /*
288*2264Sjacobs  * lpd_add_svr4_attributes
289*2264Sjacobs  *	Solaris 2.x LP - BSD protocol extensions
290*2264Sjacobs  */
291*2264Sjacobs static papi_status_t
292*2264Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
293*2264Sjacobs 		char **metadata, papi_attribute_t ***used)
294*2264Sjacobs {
295*2264Sjacobs 	char *s;
296*2264Sjacobs 	int integer;
297*2264Sjacobs 
298*2264Sjacobs 	if (svc == NULL)
299*2264Sjacobs 		return (PAPI_BAD_REQUEST);
300*2264Sjacobs 
301*2264Sjacobs 	/* media */
302*2264Sjacobs 	s = NULL;
303*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "media", &s);
304*2264Sjacobs 	if (s != NULL) {
305*2264Sjacobs 		add_svr4_control_line(metadata, 'f', s);
306*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
307*2264Sjacobs 				"media", s);
308*2264Sjacobs 	}
309*2264Sjacobs 
310*2264Sjacobs 	/* Handling */
311*2264Sjacobs 	s = NULL;
312*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "job_hold_until", &s);
313*2264Sjacobs 	if ((s != NULL) && (strcmp(s, "indefinite"))) {
314*2264Sjacobs 		add_svr4_control_line(metadata, 'H', "hold");
315*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
316*2264Sjacobs 				"media", "hold");
317*2264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "no-hold"))) {
318*2264Sjacobs 		add_svr4_control_line(metadata, 'H', "release");
319*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
320*2264Sjacobs 				"media", "release");
321*2264Sjacobs 	} else if ((s != NULL) && (strcmp(s, "immediate"))) {
322*2264Sjacobs 		add_int_control_line(metadata, 'q', 0, LPD_SVR4);
323*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
324*2264Sjacobs 				"media", "immediate");
325*2264Sjacobs 	}
326*2264Sjacobs 
327*2264Sjacobs 	/* Pages */
328*2264Sjacobs 	s = NULL;
329*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "page-ranges", &s);
330*2264Sjacobs 	if (s != NULL) {
331*2264Sjacobs 		add_svr4_control_line(metadata, 'P', s);
332*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
333*2264Sjacobs 				"page-ranges", s);
334*2264Sjacobs 	}
335*2264Sjacobs 
336*2264Sjacobs 	/* Priority : lp -q */
337*2264Sjacobs 	integer = -1;
338*2264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "priority", &integer);
339*2264Sjacobs 	if (integer != -1) {
340*2264Sjacobs 		add_int_control_line(metadata, 'q', integer, LPD_SVR4);
341*2264Sjacobs 		papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
342*2264Sjacobs 				"priority", integer);
343*2264Sjacobs 	}
344*2264Sjacobs 
345*2264Sjacobs 	/* Charset : lp -S */
346*2264Sjacobs 	s = NULL;
347*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
348*2264Sjacobs 	if (s != NULL) {
349*2264Sjacobs 		add_svr4_control_line(metadata, 'S', s);
350*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
351*2264Sjacobs 				"lp-charset", s);
352*2264Sjacobs 	}
353*2264Sjacobs 
354*2264Sjacobs 	/* Type : done when adding file  */
355*2264Sjacobs 
356*2264Sjacobs 	/* Mode : lp -y */
357*2264Sjacobs 	s = NULL;
358*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
359*2264Sjacobs 	if (s != NULL) {
360*2264Sjacobs 		add_svr4_control_line(metadata, 'y', s);
361*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
362*2264Sjacobs 				"lp-modes", s);
363*2264Sjacobs 	}
364*2264Sjacobs 
365*2264Sjacobs 	/* Options lp -o */
366*2264Sjacobs 	s = NULL;
367*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-options", &s);
368*2264Sjacobs 	if (s != NULL) {
369*2264Sjacobs 		add_svr4_control_line(metadata, 'o', s);
370*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
371*2264Sjacobs 				"lp-options", s);
372*2264Sjacobs 	}
373*2264Sjacobs 
374*2264Sjacobs 	return (PAPI_OK);
375*2264Sjacobs }
376*2264Sjacobs 
377*2264Sjacobs papi_status_t
378*2264Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
379*2264Sjacobs 		char **metadata, papi_attribute_t ***used)
380*2264Sjacobs {
381*2264Sjacobs 	char *s = NULL;
382*2264Sjacobs 
383*2264Sjacobs 	/* Options lp -o */
384*2264Sjacobs 	s = NULL;
385*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "lp-options", &s);
386*2264Sjacobs 	if (s != NULL) {
387*2264Sjacobs 		add_hpux_control_line(metadata, s);
388*2264Sjacobs 		papiAttributeListAddString(used, PAPI_ATTR_EXCL,
389*2264Sjacobs 				"lp-options", s);
390*2264Sjacobs 	}
391*2264Sjacobs 
392*2264Sjacobs 	return (PAPI_OK);
393*2264Sjacobs }
394*2264Sjacobs 
395*2264Sjacobs papi_status_t
396*2264Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
397*2264Sjacobs 		char **metadata, papi_attribute_t ***used)
398*2264Sjacobs {
399*2264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
400*2264Sjacobs 		return (PAPI_BAD_REQUEST);
401*2264Sjacobs 
402*2264Sjacobs 	lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
403*2264Sjacobs 
404*2264Sjacobs 	if (svc->uri->fragment != NULL) {
405*2264Sjacobs 		if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
406*2264Sjacobs 		    (strcasecmp(svc->uri->fragment, "svr4") == 0))
407*2264Sjacobs 			lpd_add_svr4_attributes(svc, attributes, metadata,
408*2264Sjacobs 					used);
409*2264Sjacobs 		else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
410*2264Sjacobs 			lpd_add_hpux_attributes(svc, attributes, metadata,
411*2264Sjacobs 						used);
412*2264Sjacobs 		/*
413*2264Sjacobs 		 * others could be added here:
414*2264Sjacobs 		 *	lprng, sco, aix, digital unix, xerox, ...
415*2264Sjacobs 		 */
416*2264Sjacobs 	}
417*2264Sjacobs 
418*2264Sjacobs 	return (PAPI_OK);
419*2264Sjacobs }
420*2264Sjacobs 
421*2264Sjacobs papi_status_t
422*2264Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
423*2264Sjacobs 		char **files, char **metadata, papi_attribute_t ***used)
424*2264Sjacobs {
425*2264Sjacobs 	char *format = "plain/text";
426*2264Sjacobs 	char rfc_fmt = 'l';
427*2264Sjacobs 	int copies = 1;
428*2264Sjacobs 	char host[BUFSIZ];
429*2264Sjacobs 	int i;
430*2264Sjacobs 
431*2264Sjacobs 	if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
432*2264Sjacobs 	    (metadata == NULL))
433*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
434*2264Sjacobs 
435*2264Sjacobs 	papiAttributeListGetString(attributes, NULL, "document-format",
436*2264Sjacobs 			&format);
437*2264Sjacobs 	papiAttributeListAddString(used, PAPI_ATTR_EXCL,
438*2264Sjacobs 			"document-format", format);
439*2264Sjacobs 	if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
440*2264Sjacobs 		if ((svc->uri->fragment != NULL) &&
441*2264Sjacobs 		    ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
442*2264Sjacobs 		     (strcasecmp(svc->uri->fragment, "svr4") == 0)))
443*2264Sjacobs 			add_svr4_control_line(metadata, 'T', format);
444*2264Sjacobs 		rfc_fmt = 'l';
445*2264Sjacobs 	}
446*2264Sjacobs 
447*2264Sjacobs 	papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
448*2264Sjacobs 	if (copies < 1)
449*2264Sjacobs 		copies = 1;
450*2264Sjacobs 	papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
451*2264Sjacobs 
452*2264Sjacobs 	gethostname(host, sizeof (host));
453*2264Sjacobs 
454*2264Sjacobs 	for (i = 0; files[i] != NULL; i++) {
455*2264Sjacobs 		char name[BUFSIZ];
456*2264Sjacobs 		char key;
457*2264Sjacobs 		int j;
458*2264Sjacobs 
459*2264Sjacobs 		if ((strcmp("standard input", files[i]) != 0) &&
460*2264Sjacobs 		    (access(files[i], R_OK) < 0)) {
461*2264Sjacobs 			detailed_error(svc, gettext("aborting request, %s: %s"),
462*2264Sjacobs 				files[i], strerror(errno));
463*2264Sjacobs 			return (PAPI_NOT_AUTHORIZED);
464*2264Sjacobs 		}
465*2264Sjacobs 
466*2264Sjacobs 		if (i < 26)
467*2264Sjacobs 			key = 'A' + i;
468*2264Sjacobs 		else if (i < 52)
469*2264Sjacobs 			key = 'a' + (i - 26);
470*2264Sjacobs 		else if (i < 62)
471*2264Sjacobs 			key = '0' + (i - 52);
472*2264Sjacobs 		else {
473*2264Sjacobs 			detailed_error(svc,
474*2264Sjacobs 				gettext("too many files, truncated at 62"));
475*2264Sjacobs 			return (PAPI_OK_SUBST);
476*2264Sjacobs 		}
477*2264Sjacobs 
478*2264Sjacobs 		snprintf(name, sizeof (name), "df%cXXX%s", key, host);
479*2264Sjacobs 
480*2264Sjacobs 		for (j = 0; j < copies; j++)
481*2264Sjacobs 			add_lpd_control_line(metadata, rfc_fmt, name);
482*2264Sjacobs 		add_lpd_control_line(metadata, 'U', name);
483*2264Sjacobs 		add_lpd_control_line(metadata, 'N', (char *)files[i]);
484*2264Sjacobs 	}
485*2264Sjacobs 
486*2264Sjacobs 	return (PAPI_OK);
487*2264Sjacobs }
488*2264Sjacobs 
489*2264Sjacobs papi_status_t
490*2264Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
491*2264Sjacobs 		int *ofd)
492*2264Sjacobs {
493*2264Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
494*2264Sjacobs 	int fd;
495*2264Sjacobs 	char path[32];
496*2264Sjacobs 	char *list[2];
497*2264Sjacobs 
498*2264Sjacobs 	if ((svc == NULL) || (metadata == NULL))
499*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
500*2264Sjacobs 
501*2264Sjacobs 	strcpy(path, "/tmp/lpd-job-XXXXXX");
502*2264Sjacobs 	fd = mkstemp(path);
503*2264Sjacobs 	write(fd, metadata, strlen(metadata));
504*2264Sjacobs 	close(fd);
505*2264Sjacobs 
506*2264Sjacobs 	list[0] = path;
507*2264Sjacobs 	list[1] = NULL;
508*2264Sjacobs 
509*2264Sjacobs 	if (((fd = lpd_open(svc, 's', list, 3)) < 0) && (errno != EBADMSG)) {
510*2264Sjacobs 		switch (errno) {
511*2264Sjacobs 		case ENOSPC:
512*2264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
513*2264Sjacobs 			break;
514*2264Sjacobs 		case EIO:
515*2264Sjacobs 			status = PAPI_TEMPORARY_ERROR;
516*2264Sjacobs 			break;
517*2264Sjacobs 		case ECONNREFUSED:
518*2264Sjacobs 			status = PAPI_SERVICE_UNAVAILABLE;
519*2264Sjacobs 			break;
520*2264Sjacobs 		case ENOENT:
521*2264Sjacobs 			status = PAPI_NOT_ACCEPTING;
522*2264Sjacobs 			break;
523*2264Sjacobs 		case EBADMSG:
524*2264Sjacobs 		case EBADF:
525*2264Sjacobs 			status = PAPI_OK;
526*2264Sjacobs 			break;
527*2264Sjacobs 		default:
528*2264Sjacobs 			status = PAPI_TIMEOUT;
529*2264Sjacobs 			break;
530*2264Sjacobs 		}
531*2264Sjacobs 	} else
532*2264Sjacobs 		status = PAPI_OK;
533*2264Sjacobs 
534*2264Sjacobs 	if (ofd != NULL)
535*2264Sjacobs 		*ofd = fd;
536*2264Sjacobs 	else
537*2264Sjacobs 		close(fd);
538*2264Sjacobs 
539*2264Sjacobs 	/* read the ID and add it to to the job */
540*2264Sjacobs 	if ((fd = open(path, O_RDONLY)) >= 0) {
541*2264Sjacobs 		int job_id = 0;
542*2264Sjacobs 		read(fd, &job_id, sizeof (job_id));
543*2264Sjacobs 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
544*2264Sjacobs 				"job-id", job_id);
545*2264Sjacobs 		close(fd);
546*2264Sjacobs 	}
547*2264Sjacobs 
548*2264Sjacobs 	unlink(path);
549*2264Sjacobs 
550*2264Sjacobs 	return (status);
551*2264Sjacobs }
552