xref: /titanic_51/usr/src/lib/print/libpapi-lpd/common/printer.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: printer.c 149 2006-04-25 16:55:01Z njacobs $ */
29 
30 #include <stdlib.h>
31 #include <strings.h>
32 #include <papi_impl.h>
33 #include <libintl.h>
34 
35 static int
36 contains(char *value, char **list)
37 {
38 	int i;
39 
40 	if ((value == NULL) || (list == NULL))
41 		return (1);
42 
43 	for (i = 0; list[i] != NULL; i++)
44 		if (strcasecmp(value, list[i]) == 0)
45 			return (1);
46 
47 	return (0);
48 }
49 
50 papi_status_t
51 papiPrinterQuery(papi_service_t handle, char *name,
52 		char **requested_attrs,
53 		papi_attribute_t **job_attributes,
54 		papi_printer_t *printer)
55 {
56 	papi_status_t status;
57 	service_t *svc = handle;
58 	printer_t *p = NULL;
59 
60 	if ((svc == NULL) || (name == NULL) || (printer == NULL))
61 		return (PAPI_BAD_ARGUMENT);
62 
63 	if ((status = service_fill_in(svc, name)) == PAPI_OK) {
64 		*printer = NULL;
65 
66 		if ((contains("printer-state", requested_attrs) == 1) ||
67 		    (contains("printer-state-reasons", requested_attrs) == 1))
68 			status = lpd_find_printer_info(svc,
69 			    (printer_t **)printer);
70 
71 		if ((status == PAPI_OK) && (*printer == NULL)) {
72 			char buf[BUFSIZ];
73 
74 			*printer = p = calloc(1, sizeof (*p));
75 
76 			papiAttributeListAddString(&(p->attributes),
77 			    PAPI_ATTR_APPEND, "printer-name",
78 			    queue_name_from_uri(svc->uri));
79 
80 			if (uri_to_string(svc->uri, buf, sizeof (buf)) == 0)
81 				papiAttributeListAddString(&(p->attributes),
82 				    PAPI_ATTR_APPEND,
83 				    "printer-uri-supported", buf);
84 		}
85 		/* Set printer accepting: mimic prepapi behavior */
86 		if ((p = *printer) != NULL)
87 			papiAttributeListAddBoolean(&(p->attributes),
88 			    PAPI_ATTR_REPLACE,
89 			    "printer-is-accepting-jobs", PAPI_TRUE);
90 
91 	}
92 
93 	return (status);
94 }
95 
96 papi_status_t
97 papiPrinterPurgeJobs(papi_service_t handle, char *name, papi_job_t **jobs)
98 {
99 	papi_status_t status;
100 	service_t *svc = handle;
101 
102 	if ((svc == NULL) || (name == NULL))
103 		return (PAPI_BAD_ARGUMENT);
104 
105 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
106 		status = lpd_purge_jobs(svc, (job_t ***)jobs);
107 
108 	return (status);
109 }
110 
111 papi_status_t
112 papiPrinterListJobs(papi_service_t handle, char *name,
113 		char **requested_attrs, int type_mask,
114 		int max_num_jobs, papi_job_t **jobs)
115 {
116 	papi_status_t status;
117 	service_t *svc = handle;
118 
119 	if ((svc == NULL) || (name == NULL) || (jobs == NULL))
120 		return (PAPI_BAD_ARGUMENT);
121 
122 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
123 		status = lpd_find_jobs_info(svc, (job_t ***)jobs);
124 
125 	return (status);
126 }
127 
128 papi_attribute_t **
129 papiPrinterGetAttributeList(papi_printer_t printer)
130 {
131 	printer_t *p = printer;
132 
133 	if (p == NULL)
134 		return (NULL);
135 
136 	return (p->attributes);
137 }
138 
139 void
140 papiPrinterFree(papi_printer_t printer)
141 {
142 	printer_t *p = printer;
143 
144 	if (p != NULL) {
145 		if (p->attributes != NULL)
146 			papiAttributeListFree(p->attributes);
147 		free(p);
148 	}
149 }
150 
151 void
152 papiPrinterListFree(papi_printer_t *printers)
153 {
154 	if (printers != NULL) {
155 		int i;
156 
157 		for (i = 0; printers[i] != NULL; i++)
158 			papiPrinterFree(printers[i]);
159 		free(printers);
160 	}
161 }
162 
163 
164 papi_status_t
165 papiPrinterDisable(papi_service_t handle, char *name, char *message)
166 {
167 	service_t *svc = handle;
168 	papi_status_t status;
169 
170 	if ((status = service_fill_in(svc, name)) == PAPI_OK) {
171 		detailed_error(svc,
172 		    gettext("Warning: %s is remote, disable has no meaning."),
173 		    queue_name_from_uri(svc->uri));
174 	}
175 	return (PAPI_OPERATION_NOT_SUPPORTED);
176 }
177 
178 papi_status_t
179 papiPrinterEnable(papi_service_t handle, char *name)
180 {
181 	service_t *svc = handle;
182 	papi_status_t status;
183 
184 	if ((status = service_fill_in(svc, name)) == PAPI_OK) {
185 		detailed_error(svc,
186 		    gettext("Warning: %s is remote, enable has no meaning."),
187 		    queue_name_from_uri(svc->uri));
188 	}
189 	return (PAPI_OPERATION_NOT_SUPPORTED);
190 }
191 
192 
193 papi_status_t
194 papiPrinterResume(papi_service_t handle, char *name)
195 {
196 	service_t *svc = handle;
197 	papi_status_t status;
198 
199 	if ((status = service_fill_in(svc, name)) == PAPI_OK) {
200 		detailed_error(svc,
201 		    gettext("Warning: %s is remote, accept has no meaning."),
202 		    queue_name_from_uri(svc->uri));
203 	}
204 	return (PAPI_OPERATION_NOT_SUPPORTED);
205 }
206 
207 
208 papi_status_t
209 papiPrinterPause(papi_service_t handle, char *name, char *message)
210 {
211 	service_t *svc = handle;
212 	papi_status_t status;
213 
214 	if ((status = service_fill_in(svc, name)) == PAPI_OK) {
215 		detailed_error(svc,
216 		    gettext("Warning: %s is remote, reject has no meaning."),
217 		    queue_name_from_uri(svc->uri));
218 	}
219 	return (PAPI_OPERATION_NOT_SUPPORTED);
220 }
221