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;
101c1ecd8b9Sjacobs } else
102355b4669Sjacobs return (PAPI_TEMPORARY_ERROR);
103355b4669Sjacobs }
104355b4669Sjacobs
105355b4669Sjacobs snprintf(line, sizeof (line), "%c%s\n", code, value);
106355b4669Sjacobs strlcat(*metadata, line, size);
107355b4669Sjacobs
108355b4669Sjacobs return (PAPI_OK);
109355b4669Sjacobs }
110355b4669Sjacobs
111355b4669Sjacobs static papi_status_t
add_svr4_control_line(char ** metadata,char code,char * value)112355b4669Sjacobs add_svr4_control_line(char **metadata, char code, char *value)
113355b4669Sjacobs {
114355b4669Sjacobs
115355b4669Sjacobs char line[BUFSIZ];
116355b4669Sjacobs
117355b4669Sjacobs if ((metadata == NULL) || (value == NULL))
118355b4669Sjacobs return (PAPI_BAD_REQUEST);
119355b4669Sjacobs
120355b4669Sjacobs snprintf(line, sizeof (line), "%c%s", code, value);
121355b4669Sjacobs
122355b4669Sjacobs return (add_lpd_control_line(metadata, '5', line));
123355b4669Sjacobs }
124355b4669Sjacobs
125355b4669Sjacobs static papi_status_t
add_hpux_control_line(char ** metadata,char * value)126355b4669Sjacobs add_hpux_control_line(char **metadata, char *value)
127355b4669Sjacobs {
128355b4669Sjacobs
129355b4669Sjacobs char line[BUFSIZ];
130355b4669Sjacobs
131355b4669Sjacobs if ((metadata == NULL) || (value == NULL))
132355b4669Sjacobs return (PAPI_BAD_REQUEST);
133355b4669Sjacobs
134355b4669Sjacobs snprintf(line, sizeof (line), " O%s", value);
135355b4669Sjacobs
136355b4669Sjacobs return (add_lpd_control_line(metadata, 'N', line));
137355b4669Sjacobs }
138355b4669Sjacobs
139355b4669Sjacobs static papi_status_t
add_int_control_line(char ** metadata,char code,int value,int flag)140355b4669Sjacobs add_int_control_line(char **metadata, char code, int value, int flag)
141355b4669Sjacobs {
142355b4669Sjacobs char buf[16];
143355b4669Sjacobs
144355b4669Sjacobs snprintf(buf, sizeof (buf), "%d", value);
145355b4669Sjacobs
146355b4669Sjacobs if (flag == LPD_SVR4)
147355b4669Sjacobs return (add_svr4_control_line(metadata, code, buf));
148355b4669Sjacobs else
149355b4669Sjacobs return (add_lpd_control_line(metadata, code, buf));
150355b4669Sjacobs }
151355b4669Sjacobs
152355b4669Sjacobs static papi_status_t
lpd_add_rfc1179_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)153355b4669Sjacobs lpd_add_rfc1179_attributes(service_t *svc, papi_attribute_t **attributes,
154355b4669Sjacobs char **metadata, papi_attribute_t ***used)
155355b4669Sjacobs {
156355b4669Sjacobs papi_status_t status = PAPI_OK;
157355b4669Sjacobs char *s;
158355b4669Sjacobs int integer;
159355b4669Sjacobs char bool;
160355b4669Sjacobs char host[BUFSIZ];
161355b4669Sjacobs char *user = "nobody";
162355b4669Sjacobs uid_t uid = getuid();
163355b4669Sjacobs struct passwd *pw;
164*286caa64SKeerthi Kondaka char *h1;
165355b4669Sjacobs
166355b4669Sjacobs if (svc == NULL)
167355b4669Sjacobs return (PAPI_BAD_REQUEST);
168355b4669Sjacobs
169355b4669Sjacobs /* There is nothing to do */
170355b4669Sjacobs if (attributes == NULL)
171355b4669Sjacobs return (PAPI_OK);
172355b4669Sjacobs
173355b4669Sjacobs gethostname(host, sizeof (host));
174*286caa64SKeerthi Kondaka if (papiAttributeListGetString(attributes, NULL,
175*286caa64SKeerthi Kondaka "job-originating-host-name", &h1) == PAPI_OK) {
176*286caa64SKeerthi Kondaka papiAttributeListAddString(&attributes, PAPI_ATTR_APPEND,
177*286caa64SKeerthi Kondaka "job-host", h1);
178*286caa64SKeerthi Kondaka }
179355b4669Sjacobs add_lpd_control_line(metadata, 'H', host);
180355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
181355b4669Sjacobs "job-originating-host-name", host);
182355b4669Sjacobs
183355b4669Sjacobs if ((pw = getpwuid(uid)) != NULL)
184355b4669Sjacobs user = pw->pw_name;
185355b4669Sjacobs if (uid == 0)
186355b4669Sjacobs papiAttributeListGetString(svc->attributes, NULL, "username",
187355b4669Sjacobs &user);
188355b4669Sjacobs add_lpd_control_line(metadata, 'P', user);
189355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
190355b4669Sjacobs "job-originating-user-name", user);
191355b4669Sjacobs
192355b4669Sjacobs /* Class for Banner Page */
193355b4669Sjacobs s = NULL;
194355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-class", &s);
195355b4669Sjacobs if (s != NULL) {
196355b4669Sjacobs add_lpd_control_line(metadata, 'C', s);
197355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
198355b4669Sjacobs "rfc-1179-class", s);
199355b4669Sjacobs }
200355b4669Sjacobs
201355b4669Sjacobs /* Print Banner Page */
202355b4669Sjacobs s = NULL;
203355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
204355b4669Sjacobs if ((s != NULL) && (strcmp(s, "standard") == 0)) {
205355b4669Sjacobs add_lpd_control_line(metadata, 'L', user);
206355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
207355b4669Sjacobs "job-sheets", s);
208355b4669Sjacobs }
209355b4669Sjacobs
210355b4669Sjacobs /* Jobname */
211355b4669Sjacobs s = NULL;
212355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "job-name", &s);
213355b4669Sjacobs if (s != NULL) {
214355b4669Sjacobs add_lpd_control_line(metadata, 'J', s);
215355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
216355b4669Sjacobs "job-name", s);
217355b4669Sjacobs }
218355b4669Sjacobs
219355b4669Sjacobs /* User to mail when job is done - lpr -m */
220355b4669Sjacobs bool = PAPI_FALSE;
221355b4669Sjacobs papiAttributeListGetBoolean(attributes, NULL, "rfc-1179-mail", &bool);
222355b4669Sjacobs if (bool == PAPI_TRUE) {
223355b4669Sjacobs add_lpd_control_line(metadata, 'M', user);
224355b4669Sjacobs papiAttributeListAddBoolean(used, PAPI_ATTR_EXCL,
225355b4669Sjacobs "rfc-1179-mail", bool);
226355b4669Sjacobs }
227355b4669Sjacobs
228355b4669Sjacobs /* Title for pr */
229355b4669Sjacobs s = NULL;
230355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "pr-title", &s);
231355b4669Sjacobs if (s != NULL) {
232355b4669Sjacobs add_lpd_control_line(metadata, 'T', s);
233355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
234355b4669Sjacobs "pr-title", s);
235355b4669Sjacobs }
236355b4669Sjacobs
237355b4669Sjacobs /* Indent - used with pr filter */
238355b4669Sjacobs integer = 0;
239355b4669Sjacobs papiAttributeListGetInteger(attributes, NULL, "pr-indent", &integer);
240355b4669Sjacobs if (integer >= 1) {
241355b4669Sjacobs add_int_control_line(metadata, 'I', integer, LPD_RFC);
242355b4669Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
243355b4669Sjacobs "pr-indent", integer);
244355b4669Sjacobs }
245355b4669Sjacobs
246355b4669Sjacobs /* Width - used with pr filter */
247355b4669Sjacobs integer = 0;
248355b4669Sjacobs papiAttributeListGetInteger(attributes, NULL, "pr-width", &integer);
249355b4669Sjacobs if (integer >= 1) {
250355b4669Sjacobs add_int_control_line(metadata, 'W', integer, LPD_RFC);
251355b4669Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
252355b4669Sjacobs "pr-width", integer);
253355b4669Sjacobs }
254355b4669Sjacobs
255355b4669Sjacobs /* file with Times Roman font lpr -1 */
256355b4669Sjacobs s = NULL;
257355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-r", &s);
258355b4669Sjacobs if (s != NULL) {
259355b4669Sjacobs add_lpd_control_line(metadata, '1', s);
260355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
261355b4669Sjacobs "rfc-1179-font-r", s);
262355b4669Sjacobs }
263355b4669Sjacobs
264355b4669Sjacobs /* file with Times Roman font lpr -2 */
265355b4669Sjacobs s = NULL;
266355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-i", &s);
267355b4669Sjacobs if (s != NULL) {
268355b4669Sjacobs add_lpd_control_line(metadata, '2', s);
269355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
270355b4669Sjacobs "rfc-1179-font-i", s);
271355b4669Sjacobs }
272355b4669Sjacobs
273355b4669Sjacobs /* file with Times Roman font lpr -3 */
274355b4669Sjacobs s = NULL;
275355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-b", &s);
276355b4669Sjacobs if (s != NULL) {
277355b4669Sjacobs add_lpd_control_line(metadata, '3', s);
278355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
279355b4669Sjacobs "rfc-1179-font-b", s);
280355b4669Sjacobs }
281355b4669Sjacobs
282355b4669Sjacobs /* file with Times Roman font lpr -4 */
283355b4669Sjacobs s = NULL;
284355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "rfc-1179-font-s", &s);
285355b4669Sjacobs if (s != NULL) {
286355b4669Sjacobs add_lpd_control_line(metadata, '4', s);
287355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
288355b4669Sjacobs "rfc-1179-font-s", s);
289355b4669Sjacobs }
290355b4669Sjacobs
291b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" /*
292b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" * The document format needs to be added, but the control line
293b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" * should be added when the filenames are figured out.
294b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" */
295b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" s = NULL;
296b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" papiAttributeListGetString(attributes, NULL, "document-format", &s);
297b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" if (s != NULL) {
298b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" papiAttributeListAddString(used, PAPI_ATTR_EXCL,
299b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" "document-format", s);
300b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" }
301b657fff7S"Nagaraj Yedathore - Sun Microsystems - Bangalore India"
302355b4669Sjacobs return (status);
303355b4669Sjacobs }
304355b4669Sjacobs
305fd06a699Sjacobs static char *
unused_attributes(papi_attribute_t ** list,papi_attribute_t ** used)306fd06a699Sjacobs unused_attributes(papi_attribute_t **list, papi_attribute_t **used)
307fd06a699Sjacobs {
308fd06a699Sjacobs char *result = NULL;
309fd06a699Sjacobs char **names = NULL;
310fd06a699Sjacobs int i;
311fd06a699Sjacobs
312fd06a699Sjacobs if ((list == NULL) || (used == NULL))
313fd06a699Sjacobs return (NULL);
314fd06a699Sjacobs
315fd06a699Sjacobs for (i = 0; used[i] != NULL; i++)
316fd06a699Sjacobs list_append(&names, used[i]->name);
317fd06a699Sjacobs
318fd06a699Sjacobs if (names != NULL) {
319fd06a699Sjacobs papi_attribute_t **unused = NULL;
320fd06a699Sjacobs
321fd06a699Sjacobs /* add these to the list of things to ignore */
322fd06a699Sjacobs list_append(&names, "document-format");
323fd06a699Sjacobs list_append(&names, "copies");
324fd06a699Sjacobs
325fd06a699Sjacobs split_and_copy_attributes(names, list, NULL, &unused);
326fd06a699Sjacobs if (unused != NULL) {
327fd06a699Sjacobs size_t size = 0;
328fd06a699Sjacobs
329fd06a699Sjacobs do {
330fd06a699Sjacobs size += 1024;
331fd06a699Sjacobs if (result != NULL)
332fd06a699Sjacobs free(result);
333fd06a699Sjacobs result = calloc(1, size);
334fd06a699Sjacobs } while (papiAttributeListToString(unused, " ",
335fd06a699Sjacobs result, size) != PAPI_OK);
336fd06a699Sjacobs papiAttributeListFree(unused);
337fd06a699Sjacobs }
338fd06a699Sjacobs free(names);
339fd06a699Sjacobs }
340fd06a699Sjacobs
341fd06a699Sjacobs return (result);
342fd06a699Sjacobs }
343fd06a699Sjacobs
344355b4669Sjacobs /*
345355b4669Sjacobs * lpd_add_svr4_attributes
346355b4669Sjacobs * Solaris 2.x LP - BSD protocol extensions
347355b4669Sjacobs */
348355b4669Sjacobs static papi_status_t
lpd_add_svr4_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)349355b4669Sjacobs lpd_add_svr4_attributes(service_t *svc, papi_attribute_t **attributes,
350355b4669Sjacobs char **metadata, papi_attribute_t ***used)
351355b4669Sjacobs {
35243b9c050Sjacobs papi_attribute_t *tmp[2];
353355b4669Sjacobs char *s;
354355b4669Sjacobs int integer;
355355b4669Sjacobs
356355b4669Sjacobs if (svc == NULL)
357355b4669Sjacobs return (PAPI_BAD_REQUEST);
358355b4669Sjacobs
359355b4669Sjacobs /* media */
360355b4669Sjacobs s = NULL;
361355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "media", &s);
362355b4669Sjacobs if (s != NULL) {
363355b4669Sjacobs add_svr4_control_line(metadata, 'f', s);
364355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
365355b4669Sjacobs "media", s);
366355b4669Sjacobs }
367355b4669Sjacobs
368355b4669Sjacobs /* Handling */
369355b4669Sjacobs s = NULL;
37043b9c050Sjacobs papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
371fe37c54dS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" if ((s != NULL) && (strcmp(s, "indefinite") == 0)) {
372355b4669Sjacobs add_svr4_control_line(metadata, 'H', "hold");
373355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
37443b9c050Sjacobs "job-hold-until", "indefinite");
375fe37c54dS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" } else if ((s != NULL) && (strcmp(s, "no-hold") == 0)) {
37643b9c050Sjacobs add_svr4_control_line(metadata, 'H', "immediate");
377355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
37843b9c050Sjacobs "job-hold-until", "no-hold");
37943b9c050Sjacobs } else if (s != NULL) {
38043b9c050Sjacobs add_svr4_control_line(metadata, 'H', s);
381355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
38243b9c050Sjacobs "job-hold-until", s);
383355b4669Sjacobs }
384355b4669Sjacobs
385355b4669Sjacobs /* Pages */
386355b4669Sjacobs s = NULL;
38743b9c050Sjacobs memset(tmp, NULL, sizeof (tmp));
38843b9c050Sjacobs tmp[0] = papiAttributeListFind(attributes, "page-ranges");
38943b9c050Sjacobs if (tmp[0] != NULL) {
39043b9c050Sjacobs char buf[BUFSIZ];
39143b9c050Sjacobs
39243b9c050Sjacobs papiAttributeListToString(tmp, " ", buf, sizeof (buf));
39343b9c050Sjacobs if ((s = strchr(buf, '=')) != NULL) {
39443b9c050Sjacobs add_svr4_control_line(metadata, 'P', ++s);
395355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
396355b4669Sjacobs "page-ranges", s);
397355b4669Sjacobs }
39843b9c050Sjacobs }
399355b4669Sjacobs
400355b4669Sjacobs /* Priority : lp -q */
401355b4669Sjacobs integer = -1;
40243b9c050Sjacobs papiAttributeListGetInteger(attributes, NULL, "job-priority", &integer);
403355b4669Sjacobs if (integer != -1) {
40443b9c050Sjacobs integer = 40 - (integer / 2.5);
405355b4669Sjacobs add_int_control_line(metadata, 'q', integer, LPD_SVR4);
406355b4669Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL,
40743b9c050Sjacobs "job-priority", integer);
408355b4669Sjacobs }
409355b4669Sjacobs
410355b4669Sjacobs /* Charset : lp -S */
411355b4669Sjacobs s = NULL;
412355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "lp-charset", &s);
413355b4669Sjacobs if (s != NULL) {
414355b4669Sjacobs add_svr4_control_line(metadata, 'S', s);
415355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
416355b4669Sjacobs "lp-charset", s);
417355b4669Sjacobs }
418355b4669Sjacobs
419355b4669Sjacobs /* Type : done when adding file */
420355b4669Sjacobs
421355b4669Sjacobs /* Mode : lp -y */
422355b4669Sjacobs s = NULL;
423355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "lp-modes", &s);
424355b4669Sjacobs if (s != NULL) {
425355b4669Sjacobs add_svr4_control_line(metadata, 'y', s);
426355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
427355b4669Sjacobs "lp-modes", s);
428355b4669Sjacobs }
429355b4669Sjacobs
430fd06a699Sjacobs /* Options lp -o are handled elsewhere */
431fd06a699Sjacobs if ((s = unused_attributes(attributes, *used)) != NULL) {
432fd06a699Sjacobs add_lpd_control_line(metadata, 'O', s);
433fd06a699Sjacobs free(s);
434355b4669Sjacobs }
435355b4669Sjacobs
436355b4669Sjacobs return (PAPI_OK);
437355b4669Sjacobs }
438355b4669Sjacobs
439355b4669Sjacobs papi_status_t
lpd_add_hpux_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)440355b4669Sjacobs lpd_add_hpux_attributes(service_t *svc, papi_attribute_t **attributes,
441355b4669Sjacobs char **metadata, papi_attribute_t ***used)
442355b4669Sjacobs {
443355b4669Sjacobs char *s = NULL;
444355b4669Sjacobs
445355b4669Sjacobs /* Options lp -o */
446fd06a699Sjacobs if ((s = unused_attributes(attributes, *used)) != NULL) {
447355b4669Sjacobs add_hpux_control_line(metadata, s);
448fd06a699Sjacobs free(s);
449355b4669Sjacobs }
450355b4669Sjacobs
451355b4669Sjacobs return (PAPI_OK);
452355b4669Sjacobs }
453355b4669Sjacobs
454355b4669Sjacobs papi_status_t
lpd_job_add_attributes(service_t * svc,papi_attribute_t ** attributes,char ** metadata,papi_attribute_t *** used)455355b4669Sjacobs lpd_job_add_attributes(service_t *svc, papi_attribute_t **attributes,
456355b4669Sjacobs char **metadata, papi_attribute_t ***used)
457355b4669Sjacobs {
458355b4669Sjacobs if ((svc == NULL) || (metadata == NULL))
459355b4669Sjacobs return (PAPI_BAD_REQUEST);
460355b4669Sjacobs
461355b4669Sjacobs lpd_add_rfc1179_attributes(svc, attributes, metadata, used);
462355b4669Sjacobs
463fd06a699Sjacobs /* add protocol extensions if applicable */
464355b4669Sjacobs if (svc->uri->fragment != NULL) {
465355b4669Sjacobs if ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
466355b4669Sjacobs (strcasecmp(svc->uri->fragment, "svr4") == 0))
467355b4669Sjacobs lpd_add_svr4_attributes(svc, attributes, metadata,
468355b4669Sjacobs used);
469355b4669Sjacobs else if (strcasecmp(svc->uri->fragment, "hpux") == 0)
470355b4669Sjacobs lpd_add_hpux_attributes(svc, attributes, metadata,
471355b4669Sjacobs used);
472355b4669Sjacobs /*
473355b4669Sjacobs * others could be added here:
474355b4669Sjacobs * lprng, sco, aix, digital unix, xerox, ...
475355b4669Sjacobs */
476355b4669Sjacobs }
477355b4669Sjacobs
478355b4669Sjacobs return (PAPI_OK);
479355b4669Sjacobs }
480355b4669Sjacobs
481355b4669Sjacobs papi_status_t
lpd_job_add_files(service_t * svc,papi_attribute_t ** attributes,char ** files,char ** metadata,papi_attribute_t *** used)482355b4669Sjacobs lpd_job_add_files(service_t *svc, papi_attribute_t **attributes,
483355b4669Sjacobs char **files, char **metadata, papi_attribute_t ***used)
484355b4669Sjacobs {
4850a44ef6dSjacobs char *format = "text/plain";
486355b4669Sjacobs char rfc_fmt = 'l';
487355b4669Sjacobs int copies = 1;
488355b4669Sjacobs char host[BUFSIZ];
489355b4669Sjacobs int i;
490355b4669Sjacobs
491355b4669Sjacobs if ((svc == NULL) || (attributes == NULL) || (files == NULL) ||
492355b4669Sjacobs (metadata == NULL))
493355b4669Sjacobs return (PAPI_BAD_ARGUMENT);
494355b4669Sjacobs
495355b4669Sjacobs papiAttributeListGetString(attributes, NULL, "document-format",
496355b4669Sjacobs &format);
497355b4669Sjacobs papiAttributeListAddString(used, PAPI_ATTR_EXCL,
498355b4669Sjacobs "document-format", format);
499355b4669Sjacobs if ((rfc_fmt = mime_type_to_rfc1179_type(format)) == '\0') {
500355b4669Sjacobs if ((svc->uri->fragment != NULL) &&
501355b4669Sjacobs ((strcasecmp(svc->uri->fragment, "solaris") == 0) ||
502355b4669Sjacobs (strcasecmp(svc->uri->fragment, "svr4") == 0)))
503355b4669Sjacobs add_svr4_control_line(metadata, 'T', format);
504355b4669Sjacobs rfc_fmt = 'l';
505355b4669Sjacobs }
506355b4669Sjacobs
507355b4669Sjacobs papiAttributeListGetInteger(attributes, NULL, "copies", &copies);
508355b4669Sjacobs if (copies < 1)
509355b4669Sjacobs copies = 1;
510355b4669Sjacobs papiAttributeListAddInteger(used, PAPI_ATTR_EXCL, "copies", copies);
511355b4669Sjacobs
512355b4669Sjacobs gethostname(host, sizeof (host));
513355b4669Sjacobs
514355b4669Sjacobs for (i = 0; files[i] != NULL; i++) {
515355b4669Sjacobs char name[BUFSIZ];
51695c2d302SJonathan Cowper-Andrewes struct stat statbuf;
517355b4669Sjacobs char key;
518355b4669Sjacobs int j;
519355b4669Sjacobs
520355b4669Sjacobs if ((strcmp("standard input", files[i]) != 0) &&
521355b4669Sjacobs (access(files[i], R_OK) < 0)) {
522355b4669Sjacobs detailed_error(svc, gettext("aborting request, %s: %s"),
523355b4669Sjacobs files[i], strerror(errno));
524355b4669Sjacobs return (PAPI_NOT_AUTHORIZED);
525355b4669Sjacobs }
52695c2d302SJonathan Cowper-Andrewes if (strcmp("standard input", files[i]) != 0) {
527a5669307SJonathan Cowper-Andrewes if (stat(files[i], &statbuf) < 0) {
528a5669307SJonathan Cowper-Andrewes detailed_error(svc,
529a5669307SJonathan Cowper-Andrewes gettext("Cannot access file: %s: %s"),
530a5669307SJonathan Cowper-Andrewes files[i], strerror(errno));
531a5669307SJonathan Cowper-Andrewes return (PAPI_DOCUMENT_ACCESS_ERROR);
532a5669307SJonathan Cowper-Andrewes }
53395c2d302SJonathan Cowper-Andrewes if (statbuf.st_size == 0) {
53495c2d302SJonathan Cowper-Andrewes detailed_error(svc,
53595c2d302SJonathan Cowper-Andrewes gettext("Zero byte (empty) file: %s"),
53695c2d302SJonathan Cowper-Andrewes files[i]);
53795c2d302SJonathan Cowper-Andrewes return (PAPI_BAD_ARGUMENT);
53895c2d302SJonathan Cowper-Andrewes }
53995c2d302SJonathan Cowper-Andrewes }
540355b4669Sjacobs
541355b4669Sjacobs if (i < 26)
542355b4669Sjacobs key = 'A' + i;
543355b4669Sjacobs else if (i < 52)
544355b4669Sjacobs key = 'a' + (i - 26);
545355b4669Sjacobs else if (i < 62)
546355b4669Sjacobs key = '0' + (i - 52);
547355b4669Sjacobs else {
548355b4669Sjacobs detailed_error(svc,
549355b4669Sjacobs gettext("too many files, truncated at 62"));
550355b4669Sjacobs return (PAPI_OK_SUBST);
551355b4669Sjacobs }
552355b4669Sjacobs
553355b4669Sjacobs snprintf(name, sizeof (name), "df%cXXX%s", key, host);
554355b4669Sjacobs
555355b4669Sjacobs for (j = 0; j < copies; j++)
556355b4669Sjacobs add_lpd_control_line(metadata, rfc_fmt, name);
557355b4669Sjacobs add_lpd_control_line(metadata, 'U', name);
558355b4669Sjacobs add_lpd_control_line(metadata, 'N', (char *)files[i]);
559355b4669Sjacobs }
560355b4669Sjacobs
561355b4669Sjacobs return (PAPI_OK);
562355b4669Sjacobs }
563355b4669Sjacobs
564355b4669Sjacobs papi_status_t
lpd_submit_job(service_t * svc,char * metadata,papi_attribute_t *** attributes,int * ofd)565355b4669Sjacobs lpd_submit_job(service_t *svc, char *metadata, papi_attribute_t ***attributes,
566355b4669Sjacobs int *ofd)
567355b4669Sjacobs {
568355b4669Sjacobs papi_status_t status = PAPI_INTERNAL_ERROR;
569355b4669Sjacobs int fd;
570355b4669Sjacobs char path[32];
571355b4669Sjacobs char *list[2];
572355b4669Sjacobs
573355b4669Sjacobs if ((svc == NULL) || (metadata == NULL))
574355b4669Sjacobs return (PAPI_BAD_ARGUMENT);
575355b4669Sjacobs
576355b4669Sjacobs strcpy(path, "/tmp/lpd-job-XXXXXX");
577355b4669Sjacobs fd = mkstemp(path);
578355b4669Sjacobs write(fd, metadata, strlen(metadata));
579355b4669Sjacobs close(fd);
580355b4669Sjacobs
581355b4669Sjacobs list[0] = path;
582355b4669Sjacobs list[1] = NULL;
583355b4669Sjacobs
5844bd2082fSceastha if (((fd = lpd_open(svc, 's', list, 15)) < 0) && (errno != EBADMSG)) {
585355b4669Sjacobs switch (errno) {
586355b4669Sjacobs case ENOSPC:
587355b4669Sjacobs status = PAPI_TEMPORARY_ERROR;
588355b4669Sjacobs break;
589355b4669Sjacobs case EIO:
590355b4669Sjacobs status = PAPI_TEMPORARY_ERROR;
591355b4669Sjacobs break;
592355b4669Sjacobs case ECONNREFUSED:
593355b4669Sjacobs status = PAPI_SERVICE_UNAVAILABLE;
594355b4669Sjacobs break;
595355b4669Sjacobs case ENOENT:
596355b4669Sjacobs status = PAPI_NOT_ACCEPTING;
597355b4669Sjacobs break;
598355b4669Sjacobs case EBADMSG:
599355b4669Sjacobs case EBADF:
600355b4669Sjacobs status = PAPI_OK;
601355b4669Sjacobs break;
602355b4669Sjacobs default:
603355b4669Sjacobs status = PAPI_TIMEOUT;
604355b4669Sjacobs break;
605355b4669Sjacobs }
606355b4669Sjacobs } else
607355b4669Sjacobs status = PAPI_OK;
608355b4669Sjacobs
609355b4669Sjacobs if (ofd != NULL)
610355b4669Sjacobs *ofd = fd;
611355b4669Sjacobs else
612355b4669Sjacobs close(fd);
613355b4669Sjacobs
614355b4669Sjacobs /* read the ID and add it to to the job */
615355b4669Sjacobs if ((fd = open(path, O_RDONLY)) >= 0) {
616355b4669Sjacobs int job_id = 0;
617355b4669Sjacobs read(fd, &job_id, sizeof (job_id));
618355b4669Sjacobs papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
619355b4669Sjacobs "job-id", job_id);
620355b4669Sjacobs close(fd);
621355b4669Sjacobs }
622355b4669Sjacobs
623355b4669Sjacobs unlink(path);
624355b4669Sjacobs
625355b4669Sjacobs return (status);
626355b4669Sjacobs }
627