8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / print / libpapi-lpd / common / job.c
blob86638163377e94953b01b691f46ba552c5e2d0d6
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* $Id: job.c 179 2006-07-17 18:24:07Z njacobs $ */
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #include <libintl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <papi_impl.h>
40 #include <uri.h>
43 * must copy files before leaving routine
45 papi_status_t
46 papiJobSubmit(papi_service_t handle, char *name, papi_attribute_t **attributes,
47 papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
49 papi_status_t status = PAPI_OK;
50 service_t *svc = handle;
51 job_t *j = NULL;
52 char *metadata = NULL;
54 if ((svc == NULL) || (name == NULL) || (files == NULL) ||
55 (job == NULL))
56 return (PAPI_BAD_ARGUMENT);
58 if (job_ticket != NULL) {
59 detailed_error(svc,
60 gettext("papiJobSubmit: job ticket not supported"));
61 return (PAPI_OPERATION_NOT_SUPPORTED);
64 if ((status = service_fill_in(svc, name)) != PAPI_OK)
65 return (status);
67 if ((*job = j = (job_t *)calloc(1, sizeof (*j))) == NULL) {
68 detailed_error(svc,
69 gettext("calloc() failed"));
70 return (PAPI_TEMPORARY_ERROR);
73 /* before creating a control file add the job-name */
74 if ((files != NULL) && (files[0] != NULL))
75 papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
76 "job-name", files[0]);
78 /* create a control file */
79 (void) lpd_job_add_attributes(svc, attributes, &metadata,
80 &j->attributes);
82 if ((status = lpd_job_add_files(svc, attributes, files, &metadata,
83 &j->attributes)) != PAPI_OK) {
84 return (status);
87 /* send the job to the server */
88 status = lpd_submit_job(svc, metadata, &j->attributes, NULL);
89 free(metadata);
91 return (status);
96 papi_status_t
97 papiJobSubmitByReference(papi_service_t handle, char *name,
98 papi_attribute_t **job_attributes,
99 papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
101 return (papiJobSubmit(handle, name, job_attributes,
102 job_ticket, files, job));
105 papi_status_t
106 papiJobStreamOpen(papi_service_t handle, char *name,
107 papi_attribute_t **attributes,
108 papi_job_ticket_t *job_ticket, papi_stream_t *stream)
110 papi_status_t status = PAPI_OK;
111 service_t *svc = handle;
112 char *metadata = NULL;
113 stream_t *s = NULL;
115 if ((svc == NULL) || (name == NULL) || (stream == NULL))
116 return (PAPI_BAD_ARGUMENT);
118 if (job_ticket != NULL)
119 return (PAPI_OPERATION_NOT_SUPPORTED);
121 if ((status = service_fill_in(svc, name)) != PAPI_OK)
122 return (status);
124 /* create the stream container */
125 if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
126 return (PAPI_TEMPORARY_ERROR);
128 /* create the job */
129 if ((s->job = calloc(1, sizeof (*(s->job)))) == NULL)
130 return (PAPI_TEMPORARY_ERROR);
132 papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
133 "job-name", "standard input");
135 /* process the attribute list */
136 lpd_job_add_attributes(svc, attributes, &metadata, &s->job->attributes);
138 /* if we can stream, do it */
139 if ((svc->uri->fragment != NULL) &&
140 (strcasecmp(svc->uri->fragment, "streaming") == 0)) {
141 char *files[] = { "standard input", NULL };
143 lpd_job_add_files(svc, attributes, files, &metadata,
144 &(s->job->attributes));
145 status = lpd_submit_job(svc, metadata, &(s->job->attributes),
146 &s->fd);
147 } else {
148 char dfname[18];
149 char buf[256];
151 strcpy(dfname, "/tmp/stdin-XXXXX");
153 if ((s->fd = mkstemp(dfname)) >= 0)
154 s->dfname = strdup(dfname);
155 if (s->job->attributes)
156 papiAttributeListFree(s->job->attributes);
157 s->job->attributes = NULL;
158 papiAttributeListToString(attributes, " ", buf, sizeof (buf));
159 papiAttributeListFromString(&(s->job->attributes),
160 PAPI_ATTR_APPEND, buf);
162 s->metadata = metadata;
164 return (status);
168 papi_status_t
169 papiJobStreamWrite(papi_service_t handle, papi_stream_t stream,
170 void *buffer, size_t buflen)
172 service_t *svc = handle;
173 stream_t *s = stream;
175 if ((svc == NULL) || (stream == NULL) || (buffer == NULL) ||
176 (buflen == 0))
177 return (PAPI_BAD_ARGUMENT);
179 if (write(s->fd, buffer, buflen) != buflen)
180 return (PAPI_DEVICE_ERROR);
182 return (PAPI_OK);
185 papi_status_t
186 papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job)
188 papi_status_t status = PAPI_INTERNAL_ERROR;
189 service_t *svc = handle;
190 job_t *j = NULL;
191 stream_t *s = stream;
192 int ret;
194 if ((svc == NULL) || (stream == NULL) || (job == NULL))
195 return (PAPI_BAD_ARGUMENT);
197 close(s->fd); /* close the stream */
199 if (s->dfname != NULL) { /* if it is a tmpfile, print it */
200 char *files[2];
202 files[0] = s->dfname;
203 files[1] = NULL;
205 lpd_job_add_files(svc, s->job->attributes, files, &s->metadata,
206 &(s->job->attributes));
207 status = lpd_submit_job(svc, s->metadata,
208 &(s->job->attributes), NULL);
209 unlink(s->dfname);
210 free(s->dfname);
211 } else
212 status = PAPI_OK;
214 if (s->metadata != NULL)
215 free(s->metadata);
217 *job = s->job;
219 return (status);
222 papi_status_t
223 papiJobQuery(papi_service_t handle, char *name, int32_t job_id,
224 char **job_attributes, papi_job_t *job)
226 papi_status_t status = PAPI_OK;
227 service_t *svc = handle;
229 if ((svc == NULL) || (name == NULL) || job_id < 0)
230 return (PAPI_BAD_ARGUMENT);
232 if ((status = service_fill_in(svc, name)) == PAPI_OK)
233 status = lpd_find_job_info(svc, job_id, (job_t **)job);
235 return (status);
238 papi_status_t
239 papiJobCancel(papi_service_t handle, char *name, int32_t job_id)
241 papi_status_t status;
242 service_t *svc = handle;
244 if ((svc == NULL) || (name == NULL) || (job_id < 0))
245 return (PAPI_BAD_ARGUMENT);
247 if ((status = service_fill_in(svc, name)) == PAPI_OK)
248 status = lpd_cancel_job(svc, job_id);
250 return (status);
253 papi_attribute_t **
254 papiJobGetAttributeList(papi_job_t job)
256 job_t *j = (job_t *)job;
258 if (j != NULL)
259 return ((papi_attribute_t **)j->attributes);
261 return (NULL);
264 char *
265 papiJobGetPrinterName(papi_job_t job)
267 char *result = NULL;
268 job_t *j = (job_t *)job;
270 if (j != NULL)
271 papiAttributeListGetString(j->attributes, NULL,
272 "printer-name", &result);
274 return (result);
278 papiJobGetId(papi_job_t job)
280 int result = -1;
281 job_t *j = (job_t *)job;
283 if (j != NULL)
284 papiAttributeListGetInteger(j->attributes, NULL,
285 "job-id", &result);
287 return (result);
290 void
291 papiJobFree(papi_job_t job)
293 job_t *j = (job_t *)job;
296 if (j != NULL) {
297 papiAttributeListFree(j->attributes);
298 free(j);
302 void
303 papiJobListFree(papi_job_t *jobs)
305 if (jobs != NULL) {
306 int i;
308 for (i = 0; jobs[i] != NULL; i++)
309 papiJobFree(jobs[i]);
310 free(jobs);