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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
31 #include <papi_impl.h>
34 extern int isclass(char *);
37 papiPrinterFree(papi_printer_t printer
)
39 printer_t
*tmp
= printer
;
42 papiAttributeListFree(tmp
->attributes
);
48 papiPrinterListFree(papi_printer_t
*printers
)
50 if (printers
!= NULL
) {
53 for (i
= 0; printers
[i
] != NULL
; i
++)
54 papiPrinterFree(printers
[i
]);
60 papiPrintersList(papi_service_t handle
, char **requested_attrs
,
61 papi_filter_t
*filter
, papi_printer_t
**printers
)
63 service_t
*svc
= handle
;
69 *character_set
= NULL
,
70 *reject_reason
= NULL
,
71 *disable_reason
= NULL
;
72 short printer_status
= 0;
73 long enable_date
= 0, reject_date
= 0;
75 if ((handle
== NULL
) || (printers
== NULL
))
76 return (PAPI_BAD_ARGUMENT
);
78 if ((filter
== NULL
) ||
79 ((filter
->filter
.bitmask
.mask
& PAPI_PRINTER_LOCAL
) ==
80 (filter
->filter
.bitmask
.value
& PAPI_PRINTER_LOCAL
))) {
81 /* ask the spooler for the printer(s) and state */
82 if (snd_msg(svc
, S_INQUIRE_PRINTER_STATUS
, NAME_ALL
) < 0)
83 return (PAPI_SERVICE_UNAVAILABLE
);
86 if (rcv_msg(svc
, R_INQUIRE_PRINTER_STATUS
, &status
,
87 &printer
, &form
, &character_set
,
88 &disable_reason
, &reject_reason
,
89 &printer_status
, &request_id
,
90 &enable_date
, &reject_date
) < 0)
91 return (PAPI_SERVICE_UNAVAILABLE
);
93 if ((p
= calloc(1, sizeof (*p
))) == NULL
)
94 return (PAPI_TEMPORARY_ERROR
);
96 lpsched_printer_configuration_to_attributes(svc
, p
,
99 printer_status_to_attributes(p
, printer
, form
,
100 character_set
, disable_reason
,
101 reject_reason
, printer_status
,
102 request_id
, enable_date
, reject_date
);
104 list_append(printers
, p
);
106 } while (status
== MOKMORE
);
109 if ((filter
== NULL
) ||
110 ((filter
->filter
.bitmask
.mask
& PAPI_PRINTER_CLASS
) ==
111 (filter
->filter
.bitmask
.value
& PAPI_PRINTER_CLASS
))) {
112 /* ask the spooler for the class(es) and state */
113 if (snd_msg(svc
, S_INQUIRE_CLASS
, NAME_ALL
) < 0)
114 return (PAPI_SERVICE_UNAVAILABLE
);
117 if (rcv_msg(svc
, R_INQUIRE_CLASS
, &status
, &printer
,
118 &printer_status
, &reject_reason
,
120 return (PAPI_SERVICE_UNAVAILABLE
);
122 if ((p
= calloc(1, sizeof (*p
))) == NULL
)
123 return (PAPI_TEMPORARY_ERROR
);
125 lpsched_class_configuration_to_attributes(svc
, p
,
128 class_status_to_attributes(p
, printer
, printer_status
,
129 reject_reason
, reject_date
);
131 list_append(printers
, p
);
133 } while (status
== MOKMORE
);
140 papiPrinterQuery(papi_service_t handle
, char *name
,
141 char **requested_attrs
,
142 papi_attribute_t
**job_attrs
,
143 papi_printer_t
*printer
)
146 service_t
*svc
= handle
;
153 *character_set
= NULL
,
154 *reject_reason
= NULL
,
155 *disable_reason
= NULL
;
156 short printer_status
= 0;
157 long enable_date
= 0, reject_date
= 0;
159 if ((handle
== NULL
) || (name
== NULL
) || (printer
== NULL
))
160 return (PAPI_BAD_ARGUMENT
);
162 if ((*printer
= p
= calloc(1, sizeof (*p
))) == NULL
)
163 return (PAPI_TEMPORARY_ERROR
);
165 dest
= printer_name_from_uri_id(name
, -1);
167 if (strcmp(dest
, "_default") == 0) {
168 static char *_default
;
170 if (_default
== NULL
) {
172 static char buf
[128];
174 if ((fd
= open("/etc/lp/default", O_RDONLY
)) >= 0) {
175 read(fd
, buf
, sizeof (buf
));
177 _default
= strtok(buf
, " \t\n");
183 if (isprinter(dest
) != 0) {
184 pst
= lpsched_printer_configuration_to_attributes(svc
, p
, dest
);
188 /* get the spooler status data now */
189 if (snd_msg(svc
, S_INQUIRE_PRINTER_STATUS
, dest
) < 0)
190 return (PAPI_SERVICE_UNAVAILABLE
);
192 if (rcv_msg(svc
, R_INQUIRE_PRINTER_STATUS
, &status
, &pname
,
193 &form
, &character_set
, &disable_reason
,
194 &reject_reason
, &printer_status
, &request_id
,
195 &enable_date
, &reject_date
) < 0)
196 return (PAPI_SERVICE_UNAVAILABLE
);
198 printer_status_to_attributes(p
, pname
, form
, character_set
,
199 disable_reason
, reject_reason
, printer_status
,
200 request_id
, enable_date
, reject_date
);
201 } else if (isclass(dest
) != 0) {
202 pst
= lpsched_class_configuration_to_attributes(svc
, p
, dest
);
206 /* get the spooler status data now */
207 if (snd_msg(svc
, S_INQUIRE_CLASS
, dest
) < 0)
208 return (PAPI_SERVICE_UNAVAILABLE
);
210 if (rcv_msg(svc
, R_INQUIRE_CLASS
, &status
, &pname
,
211 &printer_status
, &reject_reason
,
213 return (PAPI_SERVICE_UNAVAILABLE
);
215 class_status_to_attributes(p
, pname
, printer_status
,
216 reject_reason
, reject_date
);
217 } else if (strcmp(dest
, "PrintService") == 0) {
218 /* fill the printer object with service information */
219 lpsched_service_information(&p
->attributes
);
221 return (PAPI_NOT_FOUND
);
229 papiPrinterAdd(papi_service_t handle
, char *name
,
230 papi_attribute_t
**attributes
, papi_printer_t
*result
)
232 papi_status_t status
;
236 if ((handle
== NULL
) || (name
== NULL
) || (attributes
== NULL
))
237 return (PAPI_BAD_ARGUMENT
);
239 dest
= printer_name_from_uri_id(name
, -1);
241 if (isprinter(dest
) != 0) {
242 status
= lpsched_add_modify_printer(handle
, dest
,
245 if ((*result
= p
= calloc(1, sizeof (*p
))) != NULL
)
246 lpsched_printer_configuration_to_attributes(handle
, p
,
249 status
= PAPI_TEMPORARY_ERROR
;
251 } else if (isclass(dest
) != 0) {
252 status
= lpsched_add_modify_class(handle
, dest
, attributes
);
254 if ((*result
= p
= calloc(1, sizeof (*p
))) != NULL
)
255 lpsched_class_configuration_to_attributes(handle
, p
,
258 status
= PAPI_TEMPORARY_ERROR
;
261 status
= PAPI_NOT_FOUND
;
269 papiPrinterModify(papi_service_t handle
, char *name
,
270 papi_attribute_t
**attributes
, papi_printer_t
*result
)
272 papi_status_t status
;
276 if ((handle
== NULL
) || (name
== NULL
) || (attributes
== NULL
))
277 return (PAPI_BAD_ARGUMENT
);
279 dest
= printer_name_from_uri_id(name
, -1);
281 if (isprinter(dest
) != 0) {
282 status
= lpsched_add_modify_printer(handle
, dest
,
285 if ((*result
= p
= calloc(1, sizeof (*p
))) != NULL
)
286 lpsched_printer_configuration_to_attributes(handle
, p
,
289 status
= PAPI_TEMPORARY_ERROR
;
290 } else if (isclass(dest
) != 0) {
291 status
= lpsched_add_modify_class(handle
, dest
, attributes
);
293 if ((*result
= p
= calloc(1, sizeof (*p
))) != NULL
)
294 lpsched_class_configuration_to_attributes(handle
, p
,
297 status
= PAPI_TEMPORARY_ERROR
;
299 status
= PAPI_NOT_FOUND
;
307 papiPrinterRemove(papi_service_t handle
, char *name
)
309 papi_status_t result
;
312 if ((handle
== NULL
) || (name
== NULL
))
313 return (PAPI_BAD_ARGUMENT
);
315 dest
= printer_name_from_uri_id(name
, -1);
317 if (isprinter(dest
) != 0) {
318 result
= lpsched_remove_printer(handle
, dest
);
319 } else if (isclass(dest
) != 0) {
320 result
= lpsched_remove_class(handle
, dest
);
322 result
= PAPI_NOT_FOUND
;
330 papiPrinterDisable(papi_service_t handle
, char *name
, char *message
)
332 papi_status_t result
;
334 if ((handle
== NULL
) || (name
== NULL
))
335 return (PAPI_BAD_ARGUMENT
);
337 result
= lpsched_disable_printer(handle
, name
, message
);
343 papiPrinterEnable(papi_service_t handle
, char *name
)
345 papi_status_t result
;
347 if ((handle
== NULL
) || (name
== NULL
))
348 return (PAPI_BAD_ARGUMENT
);
350 result
= lpsched_enable_printer(handle
, name
);
356 papiPrinterPause(papi_service_t handle
, char *name
, char *message
)
358 papi_status_t result
;
360 if ((handle
== NULL
) || (name
== NULL
))
361 return (PAPI_BAD_ARGUMENT
);
363 result
= lpsched_reject_printer(handle
, name
, message
);
369 papiPrinterResume(papi_service_t handle
, char *name
)
371 papi_status_t result
;
373 if ((handle
== NULL
) || (name
== NULL
))
374 return (PAPI_BAD_ARGUMENT
);
376 result
= lpsched_accept_printer(handle
, name
);
382 papiPrinterPurgeJobs(papi_service_t handle
, char *name
, papi_job_t
**jobs
)
384 service_t
*svc
= handle
;
385 papi_status_t result
= PAPI_OK_SUBST
;
391 if ((handle
== NULL
) || (name
== NULL
))
392 return (PAPI_BAD_ARGUMENT
);
394 dest
= printer_name_from_uri_id(name
, -1);
395 more
= snd_msg(svc
, S_CANCEL
, dest
, "", "");
398 return (PAPI_SERVICE_UNAVAILABLE
);
401 if (rcv_msg(svc
, R_CANCEL
, &more
, &status
, &req_id
) < 0)
402 return (PAPI_SERVICE_UNAVAILABLE
);
406 papiAttributeListAddString(&svc
->attributes
, PAPI_ATTR_APPEND
,
407 "canceled-jobs", req_id
);
412 papiAttributeListAddString(&svc
->attributes
, PAPI_ATTR_APPEND
,
413 "cancel-failed", req_id
);
414 result
= PAPI_DEVICE_ERROR
;
417 papiAttributeListAddString(&svc
->attributes
, PAPI_ATTR_APPEND
,
418 "cancel-failed", req_id
);
419 result
= PAPI_NOT_AUTHORIZED
;
422 detailed_error(svc
, gettext("cancel failed, bad status (%d)\n"),
424 return (PAPI_DEVICE_ERROR
);
426 } while (more
== MOKMORE
);
432 papiPrinterListJobs(papi_service_t handle
, char *name
,
433 char **requested_attrs
, int type_mask
,
434 int max_num_jobs
, papi_job_t
**jobs
)
436 service_t
*svc
= handle
;
441 if ((handle
== NULL
) || (name
== NULL
) || (jobs
== NULL
))
442 return (PAPI_BAD_ARGUMENT
);
444 dest
= printer_name_from_uri_id(name
, -1);
446 rc
= snd_msg(svc
, S_INQUIRE_REQUEST_RANK
, 0, "", dest
, "", "", "");
449 return (PAPI_SERVICE_UNAVAILABLE
);
461 char request_file
[128];
464 short rank
= 0, state
= 0;
466 if (rcv_msg(svc
, R_INQUIRE_REQUEST_RANK
, &rc
, &req_id
,
467 &owner
, &slabel
, &size
, &date
, &state
, &dest
,
468 &form
, &charset
, &rank
, &file
) < 0)
469 return (PAPI_SERVICE_UNAVAILABLE
);
471 if ((rc
!= MOK
) && (rc
!= MOKMORE
))
474 * at this point, we should check to see if the job matches the
475 * selection criterion defined in "type_mask".
479 if ((max_num_jobs
!= 0) && (count
++ > max_num_jobs
))
482 if ((job
= calloc(1, sizeof (*job
))) == NULL
)
485 /* Request file is <req_id>-0 */
486 if ((ptr
= strrchr(req_id
, '-')) != NULL
) {
488 snprintf(request_file
, sizeof (request_file
),
492 lpsched_read_job_configuration(svc
, job
, request_file
);
494 job_status_to_attributes(job
, req_id
, owner
, slabel
, size
,
495 date
, state
, dest
, form
, charset
, rank
, file
);
497 list_append(jobs
, job
);
499 } while (rc
== MOKMORE
);
501 if (rc
== MNOINFO
) /* If no jobs are found, it's still ok */
504 return (lpsched_status_to_papi_status(rc
));
508 papiPrinterGetAttributeList(papi_printer_t printer
)
510 printer_t
*tmp
= printer
;
515 return (tmp
->attributes
);