xref: /illumos-gate/usr/src/lib/print/libpapi-lpd/common/printer.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
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 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <papi_impl.h>
35 
36 static int
37 contains(char *value, char **list)
38 {
39 	int i;
40 
41 	if ((value == NULL) || (list == NULL))
42 		return (1);
43 
44 	for (i = 0; list[i] != NULL; i++)
45 		if (strcasecmp(value, list[i]) == 0)
46 			return (1);
47 
48 	return (0);
49 }
50 
51 papi_status_t
52 papiPrinterQuery(papi_service_t handle, char *name,
53 		char **requested_attrs,
54 		papi_attribute_t **job_attributes,
55 		papi_printer_t *printer)
56 {
57 	papi_status_t status;
58 	service_t *svc = handle;
59 	printer_t *p = NULL;
60 
61 	if ((svc == NULL) || (name == NULL) || (printer == NULL))
62 		return (PAPI_BAD_ARGUMENT);
63 
64 	if ((status = service_fill_in(svc, name)) == PAPI_OK) {
65 		*printer = NULL;
66 
67 		if ((contains("printer-state", requested_attrs) == 1) ||
68 		    (contains("printer-state-reasons", requested_attrs) == 1))
69 			status = lpd_find_printer_info(svc,
70 					(printer_t **)printer);
71 
72 		if ((status == PAPI_OK) && (*printer == NULL)) {
73 			char buf[BUFSIZ];
74 
75 			*printer = p = calloc(1, sizeof (*p));
76 
77 			papiAttributeListAddString(&(p->attributes),
78 				PAPI_ATTR_APPEND, "printer-name",
79 				queue_name_from_uri(svc->uri));
80 
81 			if (uri_to_string(svc->uri, buf, sizeof (buf)) == 0)
82 				papiAttributeListAddString(&(p->attributes),
83 					PAPI_ATTR_APPEND,
84 					"printer-uri-supported", buf);
85 		}
86 	}
87 
88 	return (status);
89 }
90 
91 papi_status_t
92 papiPrinterPurgeJobs(papi_service_t handle, char *name, papi_job_t **jobs)
93 {
94 	papi_status_t status;
95 	service_t *svc = handle;
96 
97 	if ((svc == NULL) || (name == NULL))
98 		return (PAPI_BAD_ARGUMENT);
99 
100 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
101 		status = lpd_purge_jobs(svc, (job_t ***)jobs);
102 
103 	return (status);
104 }
105 
106 papi_status_t
107 papiPrinterListJobs(papi_service_t handle, char *name,
108 		char **requested_attrs, int type_mask,
109 		int max_num_jobs, papi_job_t **jobs)
110 {
111 	papi_status_t status;
112 	service_t *svc = handle;
113 
114 	if ((svc == NULL) || (name == NULL) || (jobs == NULL))
115 		return (PAPI_BAD_ARGUMENT);
116 
117 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
118 		status = lpd_find_jobs_info(svc, (job_t ***)jobs);
119 
120 	return (status);
121 }
122 
123 papi_attribute_t **
124 papiPrinterGetAttributeList(papi_printer_t printer)
125 {
126 	printer_t *p = printer;
127 
128 	if (p == NULL)
129 		return (NULL);
130 
131 	return (p->attributes);
132 }
133 
134 void
135 papiPrinterFree(papi_printer_t printer)
136 {
137 	printer_t *p = printer;
138 
139 	if (p != NULL) {
140 		if (p->attributes != NULL)
141 			papiAttributeListFree(p->attributes);
142 		free(p);
143 	}
144 }
145 
146 void
147 papiPrinterListFree(papi_printer_t *printers)
148 {
149 	if (printers != NULL) {
150 		int i;
151 
152 		for (i = 0; printers[i] != NULL; i++)
153 			papiPrinterFree(printers[i]);
154 		free(printers);
155 	}
156 }
157