xref: /illumos-gate/usr/src/lib/print/libpapi-dynamic/common/job.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: job.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 /*LINTLIBRARY*/
31 
32 #include <stdlib.h>
33 #include <papi_impl.h>
34 
35 void
36 papiJobFree(papi_job_t job)
37 {
38 	job_t *tmp = (job_t *)job;
39 
40 	if (tmp != NULL) {
41 		void (*f)();
42 
43 		f = (void (*)())psm_sym(tmp->svc, "papiJobFree");
44 		if (f != NULL)
45 			f(tmp->job);
46 		free(tmp);
47 	}
48 }
49 
50 void
51 papiJobListFree(papi_job_t *jobs)
52 {
53 	if (jobs != NULL) {
54 		int i;
55 
56 		for (i = 0; jobs[i] != NULL; i++)
57 			papiJobFree(jobs[i]);
58 		free(jobs);
59 	}
60 }
61 
62 papi_attribute_t **
63 papiJobGetAttributeList(papi_job_t job)
64 {
65 	papi_attribute_t **result = NULL;
66 	job_t *j = job;
67 
68 	if (job != NULL) {
69 		papi_attribute_t **(*f)();
70 
71 		f = (papi_attribute_t **(*)())psm_sym(j->svc,
72 						"papiJobGetAttributeList");
73 		if (f != NULL)
74 			result = f(j->job);
75 	}
76 
77 	return (result);
78 }
79 
80 char *
81 papiJobGetPrinterName(papi_job_t job)
82 {
83 	char *result = NULL;
84 	job_t *j = job;
85 
86 	if (job != NULL) {
87 		char *(*f)();
88 
89 		f = (char *(*)())psm_sym(j->svc, "papiJobGetPrinterName");
90 		if (f != NULL)
91 			result = f(j->job);
92 	}
93 
94 	return (result);
95 }
96 
97 int32_t
98 papiJobGetId(papi_job_t job)
99 {
100 	int32_t result = -1;
101 	job_t *j = job;
102 
103 	if (job != NULL) {
104 		int32_t (*f)();
105 
106 		f = (int32_t (*)())psm_sym(j->svc, "papiJobGetId");
107 		if (f != NULL)
108 			result = f(j->job);
109 	}
110 
111 	return (result);
112 }
113 
114 papi_job_ticket_t *
115 papiJobGetJobTicket(papi_job_t job)
116 {
117 	papi_job_ticket_t *result = NULL;
118 	job_t *j = job;
119 
120 	if (job != NULL) {
121 		papi_job_ticket_t *(*f)();
122 
123 		f = (papi_job_ticket_t *(*)())psm_sym(j->svc,
124 						"papiJobGetJobTicket");
125 		if (f != NULL)
126 			result = f(j->job);
127 	}
128 
129 	return (result);
130 }
131 
132 /* common support for papiJob{Submit|SubmitByReference|Validate} */
133 static papi_status_t
134 _papi_job_submit_reference_or_validate(papi_service_t handle, char *printer,
135 		papi_attribute_t **job_attributes,
136 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job,
137 		char *function)
138 {
139 	papi_status_t result = PAPI_INTERNAL_ERROR;
140 	service_t *svc = handle;
141 	job_t *j = NULL;
142 	papi_status_t (*f)();
143 
144 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
145 	    (job == NULL))
146 		return (PAPI_BAD_ARGUMENT);
147 
148 	if ((result = service_connect(svc, printer)) != PAPI_OK)
149 		return (result);
150 
151 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
152 		return (PAPI_TEMPORARY_ERROR);
153 
154 	j->svc = svc;
155 	f = (papi_status_t (*)())psm_sym(j->svc, function);
156 	if (f != NULL)
157 		result = f(svc->svc_handle, svc->name, job_attributes,
158 				job_ticket, files, &j->job);
159 
160 	return (result);
161 }
162 
163 papi_status_t
164 papiJobSubmit(papi_service_t handle, char *printer,
165 		papi_attribute_t **job_attributes,
166 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
167 {
168 	return (_papi_job_submit_reference_or_validate(handle, printer,
169 				job_attributes, job_ticket, files, job,
170 				"papiJobSubmit"));
171 }
172 
173 papi_status_t
174 papiJobSubmitByReference(papi_service_t handle, char *printer,
175 		papi_attribute_t **job_attributes,
176 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
177 {
178 	return (_papi_job_submit_reference_or_validate(handle, printer,
179 				job_attributes, job_ticket, files, job,
180 				"papiJobSubmitByReference"));
181 }
182 
183 papi_status_t
184 papiJobValidate(papi_service_t handle, char *printer,
185 		papi_attribute_t **job_attributes,
186 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
187 {
188 	return (_papi_job_submit_reference_or_validate(handle, printer,
189 				job_attributes, job_ticket, files, job,
190 				"papiJobValidate"));
191 }
192 
193 papi_status_t
194 papiJobStreamOpen(papi_service_t handle, char *printer,
195 		papi_attribute_t **job_attributes,
196 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
197 {
198 	papi_status_t result = PAPI_INTERNAL_ERROR;
199 	service_t *svc = handle;
200 	papi_status_t (*f)();
201 
202 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
203 		return (PAPI_BAD_ARGUMENT);
204 
205 	if ((result = service_connect(svc, printer)) != PAPI_OK)
206 		return (result);
207 
208 	f = (papi_status_t (*)())psm_sym(svc, "papiJobStreamOpen");
209 	if (f != NULL)
210 		result = f(svc->svc_handle, svc->name, job_attributes,
211 				job_ticket, stream);
212 
213 	return (result);
214 }
215 
216 papi_status_t
217 papiJobStreamWrite(papi_service_t handle,
218 		papi_stream_t stream, void *buffer, size_t buflen)
219 {
220 	papi_status_t result = PAPI_INTERNAL_ERROR;
221 	service_t *svc = handle;
222 	papi_status_t (*f)();
223 
224 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL) ||
225 	    (buflen == 0))
226 		return (PAPI_BAD_ARGUMENT);
227 
228 	f = (papi_status_t (*)())psm_sym(svc, "papiJobStreamWrite");
229 	if (f != NULL)
230 		result = f(svc->svc_handle, stream, buffer, buflen);
231 
232 	return (result);
233 }
234 
235 papi_status_t
236 papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job)
237 {
238 	papi_status_t result = PAPI_INTERNAL_ERROR;
239 	service_t *svc = handle;
240 	job_t *j = NULL;
241 	papi_status_t (*f)();
242 
243 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
244 		return (PAPI_BAD_ARGUMENT);
245 
246 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
247 		return (PAPI_TEMPORARY_ERROR);
248 
249 	j->svc = svc;
250 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobStreamClose");
251 	if (f != NULL)
252 		result = f(svc->svc_handle, stream, &j->job);
253 
254 	return (result);
255 }
256 
257 papi_status_t
258 papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
259 		char **requested_attrs, papi_job_t *job)
260 {
261 	papi_status_t result = PAPI_INTERNAL_ERROR;
262 	service_t *svc = handle;
263 	job_t *j = NULL;
264 	papi_status_t (*f)();
265 
266 	if ((svc == NULL) || (printer == NULL))
267 		return (PAPI_BAD_ARGUMENT);
268 
269 	if ((result = service_connect(svc, printer)) != PAPI_OK)
270 		return (result);
271 
272 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
273 		return (PAPI_TEMPORARY_ERROR);
274 
275 	j->svc = svc;
276 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobQuery");
277 	if (f != NULL)
278 		result = f(svc->svc_handle, svc->name, job_id,
279 				requested_attrs, &j->job);
280 
281 	return (result);
282 }
283 
284 papi_status_t
285 papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
286 		char *destination)
287 {
288 	papi_status_t result = PAPI_INTERNAL_ERROR;
289 	service_t *svc = handle;
290 	papi_status_t (*f)();
291 
292 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
293 		return (PAPI_BAD_ARGUMENT);
294 
295 	if ((result = service_connect(svc, printer)) != PAPI_OK)
296 		return (result);
297 
298 	f = (papi_status_t (*)())psm_sym(svc, "papiJobMove");
299 	if (f != NULL) {
300 		papi_attribute_t **attrs = getprinterbyname(destination, NULL);
301 
302 		papiAttributeListGetString(attrs, NULL,
303 				"printer-uri-supported", &destination);
304 		result = f(svc->svc_handle, svc->name, job_id, destination);
305 		papiAttributeListFree(attrs);
306 	}
307 
308 	return (result);
309 }
310 
311 /* common support for papiJob{Cancel|Release|Restart|Promote} */
312 static papi_status_t
313 _papi_job_handle_printer_id(papi_service_t handle,
314 		char *printer, int32_t job_id, char *function)
315 {
316 	papi_status_t result = PAPI_INTERNAL_ERROR;
317 	service_t *svc = handle;
318 	papi_status_t (*f)();
319 
320 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
321 		return (PAPI_BAD_ARGUMENT);
322 
323 	if ((result = service_connect(svc, printer)) != PAPI_OK)
324 		return (result);
325 
326 	f = (papi_status_t (*)())psm_sym(svc, function);
327 	if (f != NULL)
328 		result = f(svc->svc_handle, svc->name, job_id);
329 
330 	return (result);
331 }
332 
333 papi_status_t
334 papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
335 {
336 	return (_papi_job_handle_printer_id(handle, printer, job_id,
337 				"papiJobCancel"));
338 }
339 
340 papi_status_t
341 papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
342 {
343 	return (_papi_job_handle_printer_id(handle, printer, job_id,
344 				"papiJobRelease"));
345 }
346 
347 papi_status_t
348 papiJobRestart(papi_service_t handle, char *printer, int32_t job_id)
349 {
350 	return (_papi_job_handle_printer_id(handle, printer, job_id,
351 				"papiJobRestart"));
352 }
353 
354 papi_status_t
355 papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
356 {
357 	return (_papi_job_handle_printer_id(handle, printer, job_id,
358 				"papiJobPromote"));
359 }
360 
361 papi_status_t
362 papiJobCommit(papi_service_t handle, char *printer, int32_t job_id)
363 {
364 	return (_papi_job_handle_printer_id(handle, printer, job_id,
365 				"papiJobCommit"));
366 }
367 
368 papi_status_t
369 papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
370 {
371 	return (_papi_job_handle_printer_id(handle, printer, job_id,
372 				"papiJobHold"));
373 }
374 
375 papi_status_t
376 papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
377 		papi_attribute_t **attributes, papi_job_t *job)
378 {
379 	papi_status_t result = PAPI_INTERNAL_ERROR;
380 	service_t *svc = handle;
381 	job_t *j = NULL;
382 	papi_status_t (*f)();
383 
384 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
385 	    (attributes == NULL))
386 		return (PAPI_BAD_ARGUMENT);
387 
388 	if ((result = service_connect(svc, printer)) != PAPI_OK)
389 		return (result);
390 
391 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
392 		return (PAPI_TEMPORARY_ERROR);
393 
394 	j->svc = svc;
395 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobModify");
396 	if (f != NULL)
397 		result = f(svc->svc_handle, svc->name, job_id, attributes,
398 				&j->job);
399 
400 	return (result);
401 }
402 
403 /*
404  * The functions defined below are private to Solaris.  They are here
405  * temporarily, until equivalent functionality makes it's way into the PAPI
406  * spec.  This is expected in the next minor version after v1.0.
407  */
408 papi_status_t
409 papiJobCreate(papi_service_t handle, char *printer,
410 		papi_attribute_t **job_attributes,
411 		papi_job_ticket_t *job_ticket, papi_job_t *job)
412 {
413 	papi_status_t result = PAPI_INTERNAL_ERROR;
414 	service_t *svc = handle;
415 	job_t *j = NULL;
416 	papi_status_t (*f)();
417 
418 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
419 		return (PAPI_BAD_ARGUMENT);
420 
421 	if ((result = service_connect(svc, printer)) != PAPI_OK)
422 		return (result);
423 
424 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
425 		return (PAPI_TEMPORARY_ERROR);
426 
427 	j->svc = svc;
428 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobCreate");
429 	if (f != NULL)
430 		result = f(svc->svc_handle, svc->name, job_attributes,
431 				job_ticket, &j->job);
432 
433 	return (result);
434 }
435 
436 papi_status_t
437 papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
438 		papi_stream_t *stream)
439 {
440 	papi_status_t result = PAPI_INTERNAL_ERROR;
441 	service_t *svc = handle;
442 	papi_status_t (*f)();
443 
444 	if ((svc == NULL) || (printer == NULL))
445 		return (PAPI_BAD_ARGUMENT);
446 
447 	if ((result = service_connect(svc, printer)) != PAPI_OK)
448 		return (result);
449 
450 	f = (papi_status_t (*)())psm_sym(svc, "papiJobStreamAdd");
451 	if (f != NULL)
452 		result = f(svc->svc_handle, svc->name, id, stream);
453 
454 	return (result);
455 }
456