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 /*
23355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: printer.c 151 2006-04-25 16:55:34Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs /*LINTLIBRARY*/
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdlib.h>
33355b4669Sjacobs #include <papi_impl.h>
34355b4669Sjacobs 
35355b4669Sjacobs void
papiPrinterFree(papi_printer_t printer)36355b4669Sjacobs papiPrinterFree(papi_printer_t printer)
37355b4669Sjacobs {
38355b4669Sjacobs 	printer_t *tmp = printer;
39355b4669Sjacobs 
40355b4669Sjacobs 	if (tmp != NULL) {
41355b4669Sjacobs 		void (*f)();
42355b4669Sjacobs 
43355b4669Sjacobs 		f = (void (*)())psm_sym(tmp->svc, "papiPrinterFree");
44355b4669Sjacobs 		if (f != NULL)
45355b4669Sjacobs 			f(tmp->printer);
46355b4669Sjacobs 		if (tmp->attributes != NULL)
47355b4669Sjacobs 			papiAttributeListFree(tmp->attributes);
48355b4669Sjacobs 		if (tmp->svc_is_internal != 0)
49355b4669Sjacobs 			papiServiceDestroy(tmp->svc);
50355b4669Sjacobs 		free(tmp);
51355b4669Sjacobs 	}
52355b4669Sjacobs }
53355b4669Sjacobs 
54355b4669Sjacobs void
papiPrinterListFree(papi_printer_t * printers)55355b4669Sjacobs papiPrinterListFree(papi_printer_t *printers)
56355b4669Sjacobs {
57355b4669Sjacobs 	if (printers != NULL) {
58355b4669Sjacobs 		int i;
59355b4669Sjacobs 
60355b4669Sjacobs 		for (i = 0; printers[i] != NULL; i++)
61355b4669Sjacobs 			papiPrinterFree(printers[i]);
62355b4669Sjacobs 		free(printers);
63355b4669Sjacobs 	}
64355b4669Sjacobs }
65355b4669Sjacobs 
66355b4669Sjacobs /* Enumerate a list of printers from the loaded print service. */
67355b4669Sjacobs static papi_status_t
printers_from_service(service_t * svc,char ** requested_attrs,papi_filter_t * filter,papi_printer_t ** printers)68355b4669Sjacobs printers_from_service(service_t *svc, char **requested_attrs,
69355b4669Sjacobs 		papi_filter_t *filter, papi_printer_t **printers)
70355b4669Sjacobs {
71355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
72355b4669Sjacobs 	papi_printer_t *svc_printers = NULL;
73355b4669Sjacobs 	papi_status_t (*f)();
74355b4669Sjacobs 
75355b4669Sjacobs 	if ((svc == NULL) || (printers == NULL))
76355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
77355b4669Sjacobs 
78355b4669Sjacobs 	/* connect to the service if we are not connected */
79355b4669Sjacobs 	if ((result = service_connect(svc, svc->name)) != PAPI_OK)
80355b4669Sjacobs 		return (result);
81355b4669Sjacobs 
82355b4669Sjacobs 	f = (papi_status_t (*)())psm_sym(svc, "papiPrintersList");
83355b4669Sjacobs 	if (f != NULL)
84355b4669Sjacobs 		result = f(svc->svc_handle, requested_attrs, filter,
85355b4669Sjacobs 				&svc_printers);
86355b4669Sjacobs 
87355b4669Sjacobs 	/*
88355b4669Sjacobs 	 * copy the resulting printer object pointers into our own
89355b4669Sjacobs 	 * representation of a printer object because we need the
90355b4669Sjacobs 	 * service context to operate against the individual printer
91355b4669Sjacobs 	 * objects.  We free the list now because we no longer need
92355b4669Sjacobs 	 * it and would have no way of freeing it later.
93355b4669Sjacobs 	 */
94355b4669Sjacobs 	if ((result == PAPI_OK) && (svc_printers != NULL)) {
95355b4669Sjacobs 		int i;
96355b4669Sjacobs 
97355b4669Sjacobs 		*printers = NULL;
98355b4669Sjacobs 		for (i = 0; svc_printers[i] != NULL; i++) {
99355b4669Sjacobs 			printer_t *p = NULL;
100355b4669Sjacobs 
101355b4669Sjacobs 			if ((p = calloc(1, sizeof (*p))) == NULL)
102355b4669Sjacobs 				return (PAPI_TEMPORARY_ERROR);
103355b4669Sjacobs 
104355b4669Sjacobs 			p->svc = svc;
105355b4669Sjacobs 			p->printer = svc_printers[i];
106355b4669Sjacobs 			list_append(printers, p);
107355b4669Sjacobs 		}
108355b4669Sjacobs 		free(svc_printers);
109355b4669Sjacobs 	}
110355b4669Sjacobs 
111355b4669Sjacobs 	return (result);
112355b4669Sjacobs }
113355b4669Sjacobs 
114355b4669Sjacobs /* Get printer attributes from it's print service */
115355b4669Sjacobs static papi_status_t
printer_from_service(service_t * svc,printer_t * p,char ** requested_attrs)116355b4669Sjacobs printer_from_service(service_t *svc, printer_t *p, char **requested_attrs)
117355b4669Sjacobs {
118355b4669Sjacobs 	papi_status_t result;
119355b4669Sjacobs 	papi_service_t p_svc = NULL;
120355b4669Sjacobs 	papi_printer_t printer = NULL;
121355b4669Sjacobs 	char *psm = NULL;
122355b4669Sjacobs 	char *uri = NULL;
123355b4669Sjacobs 
124355b4669Sjacobs 	/* get the psm and uri from the attributes */
125355b4669Sjacobs 	papiAttributeListGetString(p->attributes, NULL,
126355b4669Sjacobs 			"print-service-module", &psm);
127355b4669Sjacobs 	papiAttributeListGetString(p->attributes, NULL, "printer-name", &uri);
128355b4669Sjacobs 	papiAttributeListGetString(p->attributes, NULL, "printer-uri-supported",
129355b4669Sjacobs 			&uri);
130355b4669Sjacobs 
131355b4669Sjacobs 	/* contact the service for the printer */
132355b4669Sjacobs 	result = papiServiceCreate((papi_service_t *)&p_svc, psm, svc->user,
133355b4669Sjacobs 				svc->password, svc->authCB, svc->encryption,
134355b4669Sjacobs 				svc->app_data);
135355b4669Sjacobs 	if (result != PAPI_OK)
136355b4669Sjacobs 		return (result);
137355b4669Sjacobs 
138355b4669Sjacobs 	/* get the printer from the service */
139355b4669Sjacobs 	result = papiPrinterQuery(p_svc, uri, requested_attrs, NULL, &printer);
140355b4669Sjacobs 	if (result == PAPI_OK) {
141355b4669Sjacobs 		papi_attribute_t **attributes;
142355b4669Sjacobs 
143355b4669Sjacobs 		attributes = papiPrinterGetAttributeList(printer);
144355b4669Sjacobs 		copy_attributes(&p->attributes, attributes);
145355b4669Sjacobs 	}
146355b4669Sjacobs 	papiPrinterFree(printer);
147355b4669Sjacobs 	papiServiceDestroy(p_svc);
148355b4669Sjacobs 
149355b4669Sjacobs 	return (result);
150355b4669Sjacobs }
151355b4669Sjacobs 
152355b4669Sjacobs /* are the requested attributes contained in the list */
153355b4669Sjacobs static int
contained(char ** requested,papi_attribute_t ** list)154355b4669Sjacobs contained(char **requested, papi_attribute_t **list)
155355b4669Sjacobs {
156355b4669Sjacobs 	int i;
157355b4669Sjacobs 
158355b4669Sjacobs 	if (requested == NULL)	/* we want every possible attribute */
159355b4669Sjacobs 		return (0);
160355b4669Sjacobs 
161355b4669Sjacobs 	for (i = 0; requested[i] != NULL; i++)
162355b4669Sjacobs 		if (papiAttributeListFind(list, requested[i]) == NULL)
163355b4669Sjacobs 			return (0);
164355b4669Sjacobs 
165355b4669Sjacobs 	return (1);
166355b4669Sjacobs }
167355b4669Sjacobs 
168355b4669Sjacobs /* Enumerate a list of printers from the Name Service */
169355b4669Sjacobs static papi_status_t
printers_from_name_service(service_t * svc,char ** requested_attrs,papi_filter_t * filter,papi_printer_t ** printers)170355b4669Sjacobs printers_from_name_service(service_t *svc, char **requested_attrs,
171355b4669Sjacobs 		papi_filter_t *filter, papi_printer_t **printers)
172355b4669Sjacobs {
173355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
174355b4669Sjacobs 	papi_attribute_t **attrs;
175355b4669Sjacobs 
176355b4669Sjacobs 	if ((svc == NULL) || (printers == NULL))
177355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
178355b4669Sjacobs 
179355b4669Sjacobs 	/* retrieve printers from the nameservice */
180*b51e021dSjacobs 	setprinterentry(0, NULL);
181355b4669Sjacobs 	while ((attrs = getprinterentry(NULL)) != NULL) {
182355b4669Sjacobs 		printer_t *p = NULL;
183355b4669Sjacobs 
184355b4669Sjacobs 		if ((p = calloc(1, sizeof (*p))) == NULL)
185355b4669Sjacobs 			return (PAPI_TEMPORARY_ERROR);
186355b4669Sjacobs 
187355b4669Sjacobs 		p->attributes = attrs;
188355b4669Sjacobs 		list_append(printers, p);
189355b4669Sjacobs 	}
190355b4669Sjacobs 
191355b4669Sjacobs 	/* if we have printers, check if our request has been satisfied */
192355b4669Sjacobs 	if ((printers != NULL) && (*printers != NULL)) {
193355b4669Sjacobs 		int i;
194355b4669Sjacobs 
195355b4669Sjacobs 		/* walk through the list */
196355b4669Sjacobs 		for (i = 0; (*printers)[i] != NULL; i++) {
197355b4669Sjacobs 			printer_t *p = (*printers)[i];
198355b4669Sjacobs 
199355b4669Sjacobs 			/* see if the name service satisfied the request */
200355b4669Sjacobs 			if (contained(requested_attrs, p->attributes) == 0)
201355b4669Sjacobs 				printer_from_service(svc, p, requested_attrs);
202355b4669Sjacobs 		}
203355b4669Sjacobs 	}
204355b4669Sjacobs 
205355b4669Sjacobs 	return (PAPI_OK);
206355b4669Sjacobs }
207355b4669Sjacobs 
208355b4669Sjacobs papi_status_t
papiPrintersList(papi_service_t handle,char ** requested_attrs,papi_filter_t * filter,papi_printer_t ** printers)209355b4669Sjacobs papiPrintersList(papi_service_t handle, char **requested_attrs,
210355b4669Sjacobs 		papi_filter_t *filter, papi_printer_t **printers)
211355b4669Sjacobs {
212355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
213355b4669Sjacobs 	service_t *svc = handle;
214355b4669Sjacobs 	papi_printer_t *svc_printers = NULL;
215355b4669Sjacobs 	papi_status_t (*f)();
216355b4669Sjacobs 
217355b4669Sjacobs 	if ((svc == NULL) || (printers == NULL))
218355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
219355b4669Sjacobs 
220355b4669Sjacobs 	if (svc->so_handle != NULL)	/* connected, use the print svc */
221355b4669Sjacobs 		result = printers_from_service(svc, requested_attrs,
222355b4669Sjacobs 					filter, printers);
223355b4669Sjacobs 	else				/* not connected, use the name svc */
224355b4669Sjacobs 		result = printers_from_name_service(svc, requested_attrs,
225355b4669Sjacobs 					filter, printers);
226355b4669Sjacobs 
227355b4669Sjacobs 	return (result);
228355b4669Sjacobs }
229355b4669Sjacobs 
230355b4669Sjacobs papi_status_t
papiPrinterQuery(papi_service_t handle,char * name,char ** requested_attrs,papi_attribute_t ** job_attributes,papi_printer_t * printer)231355b4669Sjacobs papiPrinterQuery(papi_service_t handle, char *name, char **requested_attrs,
232355b4669Sjacobs 		papi_attribute_t **job_attributes, papi_printer_t *printer)
233355b4669Sjacobs {
234355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
235355b4669Sjacobs 	service_t *svc = handle;
236355b4669Sjacobs 	printer_t *p = NULL;
237355b4669Sjacobs 	papi_status_t (*f)();
238355b4669Sjacobs 
239355b4669Sjacobs 	if ((svc == NULL) || (name == NULL) || (printer == NULL))
240355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
241355b4669Sjacobs 
242355b4669Sjacobs 	if ((result = service_connect(svc, name)) != PAPI_OK)
243355b4669Sjacobs 		return (result);
244355b4669Sjacobs 
245355b4669Sjacobs 	if ((*printer = p = calloc(1, sizeof (*p))) == NULL)
246355b4669Sjacobs 		return (PAPI_TEMPORARY_ERROR);
247355b4669Sjacobs 
248355b4669Sjacobs 	if ((svc->name != NULL) && (svc->svc_handle != NULL) &&
249355b4669Sjacobs 	    (svc->uri != NULL)) {
250355b4669Sjacobs 		p->svc = svc;
251355b4669Sjacobs 		f = (papi_status_t (*)())psm_sym(p->svc, "papiPrinterQuery");
252355b4669Sjacobs 		if (f != NULL)
253355b4669Sjacobs 			result = f(svc->svc_handle, svc->name, requested_attrs,
254355b4669Sjacobs 					job_attributes, &p->printer);
255355b4669Sjacobs 	} else {
256355b4669Sjacobs 		setprinterentry(0, NULL);
257355b4669Sjacobs 		p->attributes = getprinterbyname(name, NULL);
258355b4669Sjacobs 		if (p->attributes == NULL)
259355b4669Sjacobs 			result = PAPI_NOT_FOUND;
260355b4669Sjacobs 		else
261355b4669Sjacobs 			result = PAPI_OK;
262355b4669Sjacobs 	}
263355b4669Sjacobs 
264355b4669Sjacobs 	return (result);
265355b4669Sjacobs }
266355b4669Sjacobs 
267355b4669Sjacobs static papi_status_t
_papi_printer_disable_or_pause(papi_service_t handle,char * name,char * message,char * function)268355b4669Sjacobs _papi_printer_disable_or_pause(papi_service_t handle, char *name, char *message,
269355b4669Sjacobs 		char *function)
270355b4669Sjacobs {
271355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
272355b4669Sjacobs 	service_t *svc = handle;
273355b4669Sjacobs 	papi_status_t (*f)();
274355b4669Sjacobs 
275355b4669Sjacobs 	if ((svc == NULL) || (name == NULL))
276355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
277355b4669Sjacobs 
278355b4669Sjacobs 	if ((result = service_connect(svc, name)) != PAPI_OK)
279355b4669Sjacobs 		return (result);
280355b4669Sjacobs 
281355b4669Sjacobs 	f = (papi_status_t (*)())psm_sym(svc, function);
282355b4669Sjacobs 	if (f != NULL)
283355b4669Sjacobs 		result = f(svc->svc_handle, svc->name, message);
284355b4669Sjacobs 
285355b4669Sjacobs 	return (result);
286355b4669Sjacobs }
287355b4669Sjacobs 
288355b4669Sjacobs static papi_status_t
_papi_printer_enable_or_resume(papi_service_t handle,char * name,char * function)289355b4669Sjacobs _papi_printer_enable_or_resume(papi_service_t handle, char *name,
290355b4669Sjacobs 		char *function)
291355b4669Sjacobs {
292355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
293355b4669Sjacobs 	service_t *svc = handle;
294355b4669Sjacobs 	papi_status_t (*f)();
295355b4669Sjacobs 
296355b4669Sjacobs 	if ((svc == NULL) || (name == NULL))
297355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
298355b4669Sjacobs 
299355b4669Sjacobs 	if ((result = service_connect(svc, name)) != PAPI_OK)
300355b4669Sjacobs 		return (result);
301355b4669Sjacobs 
302355b4669Sjacobs 	f = (papi_status_t (*)())psm_sym(svc, function);
303355b4669Sjacobs 	if (f != NULL)
304355b4669Sjacobs 		result = f(svc->svc_handle, svc->name);
305355b4669Sjacobs 
306355b4669Sjacobs 	return (result);
307355b4669Sjacobs }
308355b4669Sjacobs 
309355b4669Sjacobs papi_status_t
papiPrinterDisable(papi_service_t handle,char * name,char * message)310355b4669Sjacobs papiPrinterDisable(papi_service_t handle, char *name, char *message)
311355b4669Sjacobs {
312355b4669Sjacobs 	return (_papi_printer_disable_or_pause(handle, name, message,
313355b4669Sjacobs 						"papiPrinterDisable"));
314355b4669Sjacobs }
315355b4669Sjacobs 
316355b4669Sjacobs papi_status_t
papiPrinterPause(papi_service_t handle,char * name,char * message)317355b4669Sjacobs papiPrinterPause(papi_service_t handle, char *name, char *message)
318355b4669Sjacobs {
319355b4669Sjacobs 	return (_papi_printer_disable_or_pause(handle, name, message,
320355b4669Sjacobs 						"papiPrinterPause"));
321355b4669Sjacobs }
322355b4669Sjacobs 
323355b4669Sjacobs papi_status_t
papiPrinterEnable(papi_service_t handle,char * name)324355b4669Sjacobs papiPrinterEnable(papi_service_t handle, char *name)
325355b4669Sjacobs {
326355b4669Sjacobs 	return (_papi_printer_enable_or_resume(handle, name,
327355b4669Sjacobs 						"papiPrinterEnable"));
328355b4669Sjacobs }
329355b4669Sjacobs 
330355b4669Sjacobs papi_status_t
papiPrinterResume(papi_service_t handle,char * name)331355b4669Sjacobs papiPrinterResume(papi_service_t handle, char *name)
332355b4669Sjacobs {
333355b4669Sjacobs 	return (_papi_printer_enable_or_resume(handle, name,
334355b4669Sjacobs 						"papiPrinterResume"));
335355b4669Sjacobs }
336355b4669Sjacobs 
337355b4669Sjacobs static papi_status_t
_papi_printer_add_or_modify(papi_service_t handle,char * name,papi_attribute_t ** attributes,papi_printer_t * printer,char * function)338355b4669Sjacobs _papi_printer_add_or_modify(papi_service_t handle, char *name,
339355b4669Sjacobs 		papi_attribute_t **attributes, papi_printer_t *printer,
340355b4669Sjacobs 		char *function)
341355b4669Sjacobs {
342355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
343355b4669Sjacobs 	service_t *svc = handle;
344355b4669Sjacobs 	printer_t *p = NULL;
345355b4669Sjacobs 	papi_status_t (*f)();
346355b4669Sjacobs 
347355b4669Sjacobs 	if ((svc == NULL) || (name == NULL) || (attributes == NULL))
348355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
349355b4669Sjacobs 
350355b4669Sjacobs 	if ((result = service_connect(svc, name)) != PAPI_OK)
351355b4669Sjacobs 		return (result);
352355b4669Sjacobs 
353355b4669Sjacobs 	if ((*printer = p = calloc(1, sizeof (*p))) == NULL)
354355b4669Sjacobs 		return (PAPI_TEMPORARY_ERROR);
355355b4669Sjacobs 
356355b4669Sjacobs 	p->svc = svc;
357355b4669Sjacobs 	f = (papi_status_t (*)())psm_sym(p->svc, function);
358355b4669Sjacobs 	if (f != NULL)
359355b4669Sjacobs 		result = f(svc->svc_handle, svc->name, attributes,
360355b4669Sjacobs 				&p->printer);
361355b4669Sjacobs 
362355b4669Sjacobs 	return (result);
363355b4669Sjacobs }
364355b4669Sjacobs 
365355b4669Sjacobs papi_status_t
papiPrinterAdd(papi_service_t handle,char * name,papi_attribute_t ** attributes,papi_printer_t * printer)366355b4669Sjacobs papiPrinterAdd(papi_service_t handle, char *name,
367355b4669Sjacobs 		papi_attribute_t **attributes, papi_printer_t *printer)
368355b4669Sjacobs {
369355b4669Sjacobs 	return (_papi_printer_add_or_modify(handle, name, attributes, printer,
370355b4669Sjacobs 						"papiPrinterAdd"));
371355b4669Sjacobs }
372355b4669Sjacobs 
373355b4669Sjacobs papi_status_t
papiPrinterModify(papi_service_t handle,char * name,papi_attribute_t ** attributes,papi_printer_t * printer)374355b4669Sjacobs papiPrinterModify(papi_service_t handle, char *name,
375355b4669Sjacobs 		papi_attribute_t **attributes, papi_printer_t *printer)
376355b4669Sjacobs {
377355b4669Sjacobs 	return (_papi_printer_add_or_modify(handle, name, attributes, printer,
378355b4669Sjacobs 						"papiPrinterModify"));
379355b4669Sjacobs }
380355b4669Sjacobs 
381355b4669Sjacobs 
382355b4669Sjacobs papi_status_t
papiPrinterRemove(papi_service_t handle,char * name)383355b4669Sjacobs papiPrinterRemove(papi_service_t handle, char *name)
384355b4669Sjacobs {
385355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
386355b4669Sjacobs 	service_t *svc = handle;
387355b4669Sjacobs 	papi_status_t (*f)();
388355b4669Sjacobs 
389355b4669Sjacobs 	if ((svc == NULL) || (name == NULL))
390355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
391355b4669Sjacobs 
392355b4669Sjacobs 	if ((result = service_connect(svc, name)) != PAPI_OK)
393355b4669Sjacobs 		return (result);
394355b4669Sjacobs 
395355b4669Sjacobs 	f = (papi_status_t (*)())psm_sym(svc, "papiPrinterRemove");
396355b4669Sjacobs 	if (f != NULL)
397355b4669Sjacobs 		result = f(svc->svc_handle, svc->name);
398355b4669Sjacobs 
399355b4669Sjacobs 	return (result);
400355b4669Sjacobs }
401355b4669Sjacobs 
402355b4669Sjacobs papi_status_t
papiPrinterPurgeJobs(papi_service_t handle,char * name,papi_job_t ** jobs)403355b4669Sjacobs papiPrinterPurgeJobs(papi_service_t handle, char *name, papi_job_t **jobs)
404355b4669Sjacobs {
405355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
406355b4669Sjacobs 	service_t *svc = handle;
407355b4669Sjacobs 	papi_job_t *svc_jobs = NULL;
408355b4669Sjacobs 	papi_status_t (*f)();
409355b4669Sjacobs 
410355b4669Sjacobs 	if ((svc == NULL) || (name == NULL))
411355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
412355b4669Sjacobs 
413355b4669Sjacobs 	if ((result = service_connect(svc, name)) != PAPI_OK)
414355b4669Sjacobs 		return (result);
415355b4669Sjacobs 
416355b4669Sjacobs 	f = (papi_status_t (*)())psm_sym(svc, "papiPrinterPurgeJobs");
417355b4669Sjacobs 	if (f != NULL)
418355b4669Sjacobs 		result = f(svc->svc_handle, svc->name, &svc_jobs);
419355b4669Sjacobs 
420355b4669Sjacobs 	/*
421355b4669Sjacobs 	 * copy the resulting job object pointers into our own
422355b4669Sjacobs 	 * representation of a job object because we need the
423355b4669Sjacobs 	 * service context to operate against the individual job
424355b4669Sjacobs 	 * objects.  We free the list now because we no longer need
425355b4669Sjacobs 	 * it and would have no way of freeing it later.
426355b4669Sjacobs 	 */
427355b4669Sjacobs 	if ((result == PAPI_OK) && (svc_jobs != NULL) && (jobs != NULL)) {
428355b4669Sjacobs 		int i;
429355b4669Sjacobs 
430355b4669Sjacobs 		*jobs = NULL;
431355b4669Sjacobs 		for (i = 0; svc_jobs[i] != NULL; i++) {
432355b4669Sjacobs 			job_t *j = NULL;
433355b4669Sjacobs 
434355b4669Sjacobs 			if ((j = calloc(1, sizeof (*j))) == NULL)
435355b4669Sjacobs 				return (PAPI_TEMPORARY_ERROR);
436355b4669Sjacobs 
437355b4669Sjacobs 			j->svc = svc;
438355b4669Sjacobs 			j->job = svc_jobs[i];
439355b4669Sjacobs 			list_append(jobs, j);
440355b4669Sjacobs 		}
441355b4669Sjacobs 		free(svc_jobs);
442355b4669Sjacobs 	}
443355b4669Sjacobs 
444355b4669Sjacobs 	return (result);
445355b4669Sjacobs }
446355b4669Sjacobs 
447355b4669Sjacobs papi_status_t
papiPrinterListJobs(papi_service_t handle,char * name,char ** requested_attrs,int type_mask,int max_num_jobs,papi_job_t ** jobs)448355b4669Sjacobs papiPrinterListJobs(papi_service_t handle, char *name, char **requested_attrs,
449355b4669Sjacobs 		int type_mask, int max_num_jobs, papi_job_t **jobs)
450355b4669Sjacobs {
451355b4669Sjacobs 	papi_status_t result = PAPI_INTERNAL_ERROR;
452355b4669Sjacobs 	service_t *svc = handle;
453355b4669Sjacobs 	papi_job_t *svc_jobs = NULL;
454355b4669Sjacobs 	papi_status_t (*f)();
455355b4669Sjacobs 
456355b4669Sjacobs 	if ((svc == NULL) || (name == NULL) || (jobs == NULL))
457355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
458355b4669Sjacobs 
459355b4669Sjacobs 	if ((result = service_connect(svc, name)) != PAPI_OK)
460355b4669Sjacobs 		return (result);
461355b4669Sjacobs 
462355b4669Sjacobs 	f = (papi_status_t (*)())psm_sym(svc, "papiPrinterListJobs");
463355b4669Sjacobs 	if (f != NULL)
464355b4669Sjacobs 		result = f(svc->svc_handle, svc->name, requested_attrs,
465355b4669Sjacobs 				type_mask, max_num_jobs, &svc_jobs);
466355b4669Sjacobs 
467355b4669Sjacobs 	/*
468355b4669Sjacobs 	 * copy the resulting job object pointers into our own
469355b4669Sjacobs 	 * representation of a job object because we need the
470355b4669Sjacobs 	 * service context to operate against the individual job
471355b4669Sjacobs 	 * objects.  We free the list now because we no longer need
472355b4669Sjacobs 	 * it and would have no way of freeing it later.
473355b4669Sjacobs 	 */
474355b4669Sjacobs 	if ((result == PAPI_OK) && (svc_jobs != NULL)) {
475355b4669Sjacobs 		int i;
476355b4669Sjacobs 
477355b4669Sjacobs 		*jobs = NULL;
478355b4669Sjacobs 		for (i = 0; svc_jobs[i] != NULL; i++) {
479355b4669Sjacobs 			job_t *j = NULL;
480355b4669Sjacobs 
481355b4669Sjacobs 			if ((j = calloc(1, sizeof (*j))) == NULL)
482355b4669Sjacobs 				return (PAPI_TEMPORARY_ERROR);
483355b4669Sjacobs 
484355b4669Sjacobs 			j->svc = svc;
485355b4669Sjacobs 			j->job = svc_jobs[i];
486355b4669Sjacobs 			list_append(jobs, j);
487355b4669Sjacobs 		}
488355b4669Sjacobs 		free(svc_jobs);
489355b4669Sjacobs 	}
490355b4669Sjacobs 
491355b4669Sjacobs 	return (result);
492355b4669Sjacobs }
493355b4669Sjacobs 
494355b4669Sjacobs papi_attribute_t **
papiPrinterGetAttributeList(papi_printer_t printer)495355b4669Sjacobs papiPrinterGetAttributeList(papi_printer_t printer)
496355b4669Sjacobs {
497355b4669Sjacobs 	papi_attribute_t **result = NULL;
498355b4669Sjacobs 	printer_t *p = printer;
499355b4669Sjacobs 
500355b4669Sjacobs 	if ((p != NULL) && (p->printer != NULL)) {
501355b4669Sjacobs 		papi_attribute_t **(*f)();
502355b4669Sjacobs 
503355b4669Sjacobs 		f = (papi_attribute_t **(*)())psm_sym(p->svc,
504355b4669Sjacobs 					"papiPrinterGetAttributeList");
505355b4669Sjacobs 		if (f != NULL)
506355b4669Sjacobs 			result = f(p->printer);
507355b4669Sjacobs 	} else
508355b4669Sjacobs 		result = p->attributes;
509355b4669Sjacobs 
510355b4669Sjacobs 	return (result);
511355b4669Sjacobs }
512