1*2264Sjacobs /*
2*2264Sjacobs  * CDDL HEADER START
3*2264Sjacobs  *
4*2264Sjacobs  * The contents of this file are subject to the terms of the
5*2264Sjacobs  * Common Development and Distribution License (the "License").
6*2264Sjacobs  * You may not use this file except in compliance with the License.
7*2264Sjacobs  *
8*2264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs  * See the License for the specific language governing permissions
11*2264Sjacobs  * and limitations under the License.
12*2264Sjacobs  *
13*2264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs  *
19*2264Sjacobs  * CDDL HEADER END
20*2264Sjacobs  */
21*2264Sjacobs 
22*2264Sjacobs /*
23*2264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2264Sjacobs  * Use is subject to license terms.
25*2264Sjacobs  *
26*2264Sjacobs  */
27*2264Sjacobs 
28*2264Sjacobs /* $Id: printer.c 149 2006-04-25 16:55:01Z njacobs $ */
29*2264Sjacobs 
30*2264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*2264Sjacobs 
32*2264Sjacobs #include <stdlib.h>
33*2264Sjacobs #include <strings.h>
34*2264Sjacobs #include <papi_impl.h>
35*2264Sjacobs 
36*2264Sjacobs static int
37*2264Sjacobs contains(char *value, char **list)
38*2264Sjacobs {
39*2264Sjacobs 	int i;
40*2264Sjacobs 
41*2264Sjacobs 	if ((value == NULL) || (list == NULL))
42*2264Sjacobs 		return (1);
43*2264Sjacobs 
44*2264Sjacobs 	for (i = 0; list[i] != NULL; i++)
45*2264Sjacobs 		if (strcasecmp(value, list[i]) == 0)
46*2264Sjacobs 			return (1);
47*2264Sjacobs 
48*2264Sjacobs 	return (0);
49*2264Sjacobs }
50*2264Sjacobs 
51*2264Sjacobs papi_status_t
52*2264Sjacobs papiPrinterQuery(papi_service_t handle, char *name,
53*2264Sjacobs 		char **requested_attrs,
54*2264Sjacobs 		papi_attribute_t **job_attributes,
55*2264Sjacobs 		papi_printer_t *printer)
56*2264Sjacobs {
57*2264Sjacobs 	papi_status_t status;
58*2264Sjacobs 	service_t *svc = handle;
59*2264Sjacobs 	printer_t *p = NULL;
60*2264Sjacobs 
61*2264Sjacobs 	if ((svc == NULL) || (name == NULL) || (printer == NULL))
62*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
63*2264Sjacobs 
64*2264Sjacobs 	if ((status = service_fill_in(svc, name)) == PAPI_OK) {
65*2264Sjacobs 		*printer = NULL;
66*2264Sjacobs 
67*2264Sjacobs 		if ((contains("printer-state", requested_attrs) == 1) ||
68*2264Sjacobs 		    (contains("printer-state-reasons", requested_attrs) == 1))
69*2264Sjacobs 			status = lpd_find_printer_info(svc,
70*2264Sjacobs 					(printer_t **)printer);
71*2264Sjacobs 
72*2264Sjacobs 		if ((status == PAPI_OK) && (*printer == NULL)) {
73*2264Sjacobs 			char buf[BUFSIZ];
74*2264Sjacobs 
75*2264Sjacobs 			*printer = p = calloc(1, sizeof (*p));
76*2264Sjacobs 
77*2264Sjacobs 			papiAttributeListAddString(&(p->attributes),
78*2264Sjacobs 				PAPI_ATTR_APPEND, "printer-name",
79*2264Sjacobs 				queue_name_from_uri(svc->uri));
80*2264Sjacobs 
81*2264Sjacobs 			if (uri_to_string(svc->uri, buf, sizeof (buf)) == 0)
82*2264Sjacobs 				papiAttributeListAddString(&(p->attributes),
83*2264Sjacobs 					PAPI_ATTR_APPEND,
84*2264Sjacobs 					"printer-uri-supported", buf);
85*2264Sjacobs 		}
86*2264Sjacobs 	}
87*2264Sjacobs 
88*2264Sjacobs 	return (status);
89*2264Sjacobs }
90*2264Sjacobs 
91*2264Sjacobs papi_status_t
92*2264Sjacobs papiPrinterPurgeJobs(papi_service_t handle, char *name, papi_job_t **jobs)
93*2264Sjacobs {
94*2264Sjacobs 	papi_status_t status;
95*2264Sjacobs 	service_t *svc = handle;
96*2264Sjacobs 
97*2264Sjacobs 	if ((svc == NULL) || (name == NULL))
98*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
99*2264Sjacobs 
100*2264Sjacobs 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
101*2264Sjacobs 		status = lpd_purge_jobs(svc, (job_t ***)jobs);
102*2264Sjacobs 
103*2264Sjacobs 	return (status);
104*2264Sjacobs }
105*2264Sjacobs 
106*2264Sjacobs papi_status_t
107*2264Sjacobs papiPrinterListJobs(papi_service_t handle, char *name,
108*2264Sjacobs 		char **requested_attrs, int type_mask,
109*2264Sjacobs 		int max_num_jobs, papi_job_t **jobs)
110*2264Sjacobs {
111*2264Sjacobs 	papi_status_t status;
112*2264Sjacobs 	service_t *svc = handle;
113*2264Sjacobs 
114*2264Sjacobs 	if ((svc == NULL) || (name == NULL) || (jobs == NULL))
115*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
116*2264Sjacobs 
117*2264Sjacobs 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
118*2264Sjacobs 		status = lpd_find_jobs_info(svc, (job_t ***)jobs);
119*2264Sjacobs 
120*2264Sjacobs 	return (status);
121*2264Sjacobs }
122*2264Sjacobs 
123*2264Sjacobs papi_attribute_t **
124*2264Sjacobs papiPrinterGetAttributeList(papi_printer_t printer)
125*2264Sjacobs {
126*2264Sjacobs 	printer_t *p = printer;
127*2264Sjacobs 
128*2264Sjacobs 	if (p == NULL)
129*2264Sjacobs 		return (NULL);
130*2264Sjacobs 
131*2264Sjacobs 	return (p->attributes);
132*2264Sjacobs }
133*2264Sjacobs 
134*2264Sjacobs void
135*2264Sjacobs papiPrinterFree(papi_printer_t printer)
136*2264Sjacobs {
137*2264Sjacobs 	printer_t *p = printer;
138*2264Sjacobs 
139*2264Sjacobs 	if (p != NULL) {
140*2264Sjacobs 		if (p->attributes != NULL)
141*2264Sjacobs 			papiAttributeListFree(p->attributes);
142*2264Sjacobs 		free(p);
143*2264Sjacobs 	}
144*2264Sjacobs }
145*2264Sjacobs 
146*2264Sjacobs void
147*2264Sjacobs papiPrinterListFree(papi_printer_t *printers)
148*2264Sjacobs {
149*2264Sjacobs 	if (printers != NULL) {
150*2264Sjacobs 		int i;
151*2264Sjacobs 
152*2264Sjacobs 		for (i = 0; printers[i] != NULL; i++)
153*2264Sjacobs 			papiPrinterFree(printers[i]);
154*2264Sjacobs 		free(printers);
155*2264Sjacobs 	}
156*2264Sjacobs }
157