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]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: printer.c 151 2006-04-25 16:55:34Z njacobs $ */
30 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <papi_impl.h>
38 papiPrinterFree(papi_printer_t printer
)
40 printer_t
*tmp
= printer
;
45 f
= (void (*)())psm_sym(tmp
->svc
, "papiPrinterFree");
48 if (tmp
->attributes
!= NULL
)
49 papiAttributeListFree(tmp
->attributes
);
50 if (tmp
->svc_is_internal
!= 0)
51 papiServiceDestroy(tmp
->svc
);
57 papiPrinterListFree(papi_printer_t
*printers
)
59 if (printers
!= NULL
) {
62 for (i
= 0; printers
[i
] != NULL
; i
++)
63 papiPrinterFree(printers
[i
]);
68 /* Enumerate a list of printers from the loaded print service. */
70 printers_from_service(service_t
*svc
, char **requested_attrs
,
71 papi_filter_t
*filter
, papi_printer_t
**printers
)
73 papi_status_t result
= PAPI_INTERNAL_ERROR
;
74 papi_printer_t
*svc_printers
= NULL
;
77 if ((svc
== NULL
) || (printers
== NULL
))
78 return (PAPI_BAD_ARGUMENT
);
80 /* connect to the service if we are not connected */
81 if ((result
= service_connect(svc
, svc
->name
)) != PAPI_OK
)
84 f
= (papi_status_t (*)())psm_sym(svc
, "papiPrintersList");
86 result
= f(svc
->svc_handle
, requested_attrs
, filter
,
90 * copy the resulting printer object pointers into our own
91 * representation of a printer object because we need the
92 * service context to operate against the individual printer
93 * objects. We free the list now because we no longer need
94 * it and would have no way of freeing it later.
96 if ((result
== PAPI_OK
) && (svc_printers
!= NULL
)) {
100 for (i
= 0; svc_printers
[i
] != NULL
; i
++) {
103 if ((p
= calloc(1, sizeof (*p
))) == NULL
)
104 return (PAPI_TEMPORARY_ERROR
);
107 p
->printer
= svc_printers
[i
];
108 list_append(printers
, p
);
116 /* Get printer attributes from it's print service */
118 printer_from_service(service_t
*svc
, printer_t
*p
, char **requested_attrs
)
120 papi_status_t result
;
121 papi_service_t p_svc
= NULL
;
122 papi_printer_t printer
= NULL
;
126 /* get the psm and uri from the attributes */
127 papiAttributeListGetString(p
->attributes
, NULL
,
128 "print-service-module", &psm
);
129 papiAttributeListGetString(p
->attributes
, NULL
, "printer-name", &uri
);
130 papiAttributeListGetString(p
->attributes
, NULL
, "printer-uri-supported",
133 /* contact the service for the printer */
134 result
= papiServiceCreate((papi_service_t
*)&p_svc
, psm
, svc
->user
,
135 svc
->password
, svc
->authCB
, svc
->encryption
,
137 if (result
!= PAPI_OK
)
140 /* get the printer from the service */
141 result
= papiPrinterQuery(p_svc
, uri
, requested_attrs
, NULL
, &printer
);
142 if (result
== PAPI_OK
) {
143 papi_attribute_t
**attributes
;
145 attributes
= papiPrinterGetAttributeList(printer
);
146 copy_attributes(&p
->attributes
, attributes
);
148 papiPrinterFree(printer
);
149 papiServiceDestroy(p_svc
);
154 /* are the requested attributes contained in the list */
156 contained(char **requested
, papi_attribute_t
**list
)
160 if (requested
== NULL
) /* we want every possible attribute */
163 for (i
= 0; requested
[i
] != NULL
; i
++)
164 if (papiAttributeListFind(list
, requested
[i
]) == NULL
)
170 /* Enumerate a list of printers from the Name Service */
172 printers_from_name_service(service_t
*svc
, char **requested_attrs
,
173 papi_filter_t
*filter
, papi_printer_t
**printers
)
175 papi_status_t result
= PAPI_INTERNAL_ERROR
;
176 papi_attribute_t
**attrs
;
178 if ((svc
== NULL
) || (printers
== NULL
))
179 return (PAPI_BAD_ARGUMENT
);
181 /* retrieve printers from the nameservice */
182 setprinterentry(0, NULL
);
183 while ((attrs
= getprinterentry(NULL
)) != NULL
) {
186 if ((p
= calloc(1, sizeof (*p
))) == NULL
)
187 return (PAPI_TEMPORARY_ERROR
);
189 p
->attributes
= attrs
;
190 list_append(printers
, p
);
193 /* if we have printers, check if our request has been satisfied */
194 if ((printers
!= NULL
) && (*printers
!= NULL
)) {
197 /* walk through the list */
198 for (i
= 0; (*printers
)[i
] != NULL
; i
++) {
199 printer_t
*p
= (*printers
)[i
];
201 /* see if the name service satisfied the request */
202 if (contained(requested_attrs
, p
->attributes
) == 0)
203 printer_from_service(svc
, p
, requested_attrs
);
211 papiPrintersList(papi_service_t handle
, char **requested_attrs
,
212 papi_filter_t
*filter
, papi_printer_t
**printers
)
214 papi_status_t result
= PAPI_INTERNAL_ERROR
;
215 service_t
*svc
= handle
;
216 papi_printer_t
*svc_printers
= NULL
;
217 papi_status_t (*f
)();
219 if ((svc
== NULL
) || (printers
== NULL
))
220 return (PAPI_BAD_ARGUMENT
);
222 if (svc
->so_handle
!= NULL
) /* connected, use the print svc */
223 result
= printers_from_service(svc
, requested_attrs
,
225 else /* not connected, use the name svc */
226 result
= printers_from_name_service(svc
, requested_attrs
,
233 papiPrinterQuery(papi_service_t handle
, char *name
, char **requested_attrs
,
234 papi_attribute_t
**job_attributes
, papi_printer_t
*printer
)
236 papi_status_t result
= PAPI_INTERNAL_ERROR
;
237 service_t
*svc
= handle
;
239 papi_status_t (*f
)();
241 if ((svc
== NULL
) || (name
== NULL
) || (printer
== NULL
))
242 return (PAPI_BAD_ARGUMENT
);
244 if ((result
= service_connect(svc
, name
)) != PAPI_OK
)
247 if ((*printer
= p
= calloc(1, sizeof (*p
))) == NULL
)
248 return (PAPI_TEMPORARY_ERROR
);
250 if ((svc
->name
!= NULL
) && (svc
->svc_handle
!= NULL
) &&
251 (svc
->uri
!= NULL
)) {
253 f
= (papi_status_t (*)())psm_sym(p
->svc
, "papiPrinterQuery");
255 result
= f(svc
->svc_handle
, svc
->name
, requested_attrs
,
256 job_attributes
, &p
->printer
);
258 setprinterentry(0, NULL
);
259 p
->attributes
= getprinterbyname(name
, NULL
);
260 if (p
->attributes
== NULL
)
261 result
= PAPI_NOT_FOUND
;
270 _papi_printer_disable_or_pause(papi_service_t handle
, char *name
, char *message
,
273 papi_status_t result
= PAPI_INTERNAL_ERROR
;
274 service_t
*svc
= handle
;
275 papi_status_t (*f
)();
277 if ((svc
== NULL
) || (name
== NULL
))
278 return (PAPI_BAD_ARGUMENT
);
280 if ((result
= service_connect(svc
, name
)) != PAPI_OK
)
283 f
= (papi_status_t (*)())psm_sym(svc
, function
);
285 result
= f(svc
->svc_handle
, svc
->name
, message
);
291 _papi_printer_enable_or_resume(papi_service_t handle
, char *name
,
294 papi_status_t result
= PAPI_INTERNAL_ERROR
;
295 service_t
*svc
= handle
;
296 papi_status_t (*f
)();
298 if ((svc
== NULL
) || (name
== NULL
))
299 return (PAPI_BAD_ARGUMENT
);
301 if ((result
= service_connect(svc
, name
)) != PAPI_OK
)
304 f
= (papi_status_t (*)())psm_sym(svc
, function
);
306 result
= f(svc
->svc_handle
, svc
->name
);
312 papiPrinterDisable(papi_service_t handle
, char *name
, char *message
)
314 return (_papi_printer_disable_or_pause(handle
, name
, message
,
315 "papiPrinterDisable"));
319 papiPrinterPause(papi_service_t handle
, char *name
, char *message
)
321 return (_papi_printer_disable_or_pause(handle
, name
, message
,
322 "papiPrinterPause"));
326 papiPrinterEnable(papi_service_t handle
, char *name
)
328 return (_papi_printer_enable_or_resume(handle
, name
,
329 "papiPrinterEnable"));
333 papiPrinterResume(papi_service_t handle
, char *name
)
335 return (_papi_printer_enable_or_resume(handle
, name
,
336 "papiPrinterResume"));
340 _papi_printer_add_or_modify(papi_service_t handle
, char *name
,
341 papi_attribute_t
**attributes
, papi_printer_t
*printer
,
344 papi_status_t result
= PAPI_INTERNAL_ERROR
;
345 service_t
*svc
= handle
;
347 papi_status_t (*f
)();
349 if ((svc
== NULL
) || (name
== NULL
) || (attributes
== NULL
))
350 return (PAPI_BAD_ARGUMENT
);
352 if ((result
= service_connect(svc
, name
)) != PAPI_OK
)
355 if ((*printer
= p
= calloc(1, sizeof (*p
))) == NULL
)
356 return (PAPI_TEMPORARY_ERROR
);
359 f
= (papi_status_t (*)())psm_sym(p
->svc
, function
);
361 result
= f(svc
->svc_handle
, svc
->name
, attributes
,
368 papiPrinterAdd(papi_service_t handle
, char *name
,
369 papi_attribute_t
**attributes
, papi_printer_t
*printer
)
371 return (_papi_printer_add_or_modify(handle
, name
, attributes
, printer
,
376 papiPrinterModify(papi_service_t handle
, char *name
,
377 papi_attribute_t
**attributes
, papi_printer_t
*printer
)
379 return (_papi_printer_add_or_modify(handle
, name
, attributes
, printer
,
380 "papiPrinterModify"));
385 papiPrinterRemove(papi_service_t handle
, char *name
)
387 papi_status_t result
= PAPI_INTERNAL_ERROR
;
388 service_t
*svc
= handle
;
389 papi_status_t (*f
)();
391 if ((svc
== NULL
) || (name
== NULL
))
392 return (PAPI_BAD_ARGUMENT
);
394 if ((result
= service_connect(svc
, name
)) != PAPI_OK
)
397 f
= (papi_status_t (*)())psm_sym(svc
, "papiPrinterRemove");
399 result
= f(svc
->svc_handle
, svc
->name
);
405 papiPrinterPurgeJobs(papi_service_t handle
, char *name
, papi_job_t
**jobs
)
407 papi_status_t result
= PAPI_INTERNAL_ERROR
;
408 service_t
*svc
= handle
;
409 papi_job_t
*svc_jobs
= NULL
;
410 papi_status_t (*f
)();
412 if ((svc
== NULL
) || (name
== NULL
))
413 return (PAPI_BAD_ARGUMENT
);
415 if ((result
= service_connect(svc
, name
)) != PAPI_OK
)
418 f
= (papi_status_t (*)())psm_sym(svc
, "papiPrinterPurgeJobs");
420 result
= f(svc
->svc_handle
, svc
->name
, &svc_jobs
);
423 * copy the resulting job object pointers into our own
424 * representation of a job object because we need the
425 * service context to operate against the individual job
426 * objects. We free the list now because we no longer need
427 * it and would have no way of freeing it later.
429 if ((result
== PAPI_OK
) && (svc_jobs
!= NULL
) && (jobs
!= NULL
)) {
433 for (i
= 0; svc_jobs
[i
] != NULL
; i
++) {
436 if ((j
= calloc(1, sizeof (*j
))) == NULL
)
437 return (PAPI_TEMPORARY_ERROR
);
440 j
->job
= svc_jobs
[i
];
441 list_append(jobs
, j
);
450 papiPrinterListJobs(papi_service_t handle
, char *name
, char **requested_attrs
,
451 int type_mask
, int max_num_jobs
, papi_job_t
**jobs
)
453 papi_status_t result
= PAPI_INTERNAL_ERROR
;
454 service_t
*svc
= handle
;
455 papi_job_t
*svc_jobs
= NULL
;
456 papi_status_t (*f
)();
458 if ((svc
== NULL
) || (name
== NULL
) || (jobs
== NULL
))
459 return (PAPI_BAD_ARGUMENT
);
461 if ((result
= service_connect(svc
, name
)) != PAPI_OK
)
464 f
= (papi_status_t (*)())psm_sym(svc
, "papiPrinterListJobs");
466 result
= f(svc
->svc_handle
, svc
->name
, requested_attrs
,
467 type_mask
, max_num_jobs
, &svc_jobs
);
470 * copy the resulting job object pointers into our own
471 * representation of a job object because we need the
472 * service context to operate against the individual job
473 * objects. We free the list now because we no longer need
474 * it and would have no way of freeing it later.
476 if ((result
== PAPI_OK
) && (svc_jobs
!= NULL
)) {
480 for (i
= 0; svc_jobs
[i
] != NULL
; i
++) {
483 if ((j
= calloc(1, sizeof (*j
))) == NULL
)
484 return (PAPI_TEMPORARY_ERROR
);
487 j
->job
= svc_jobs
[i
];
488 list_append(jobs
, j
);
497 papiPrinterGetAttributeList(papi_printer_t printer
)
499 papi_attribute_t
**result
= NULL
;
500 printer_t
*p
= printer
;
502 if ((p
!= NULL
) && (p
->printer
!= NULL
)) {
503 papi_attribute_t
**(*f
)();
505 f
= (papi_attribute_t
**(*)())psm_sym(p
->svc
,
506 "papiPrinterGetAttributeList");
508 result
= f(p
->printer
);
510 result
= p
->attributes
;