xref: /titanic_52/usr/src/lib/print/libpapi-lpd/common/printer.c (revision 940d71d237794874e18a0eb72f6564821a823517)
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 2007 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 		/* Set printer accepting: mimic prepapi behavior */
87 		if ((p = *printer) != NULL)
88 			papiAttributeListAddBoolean(&(p->attributes),
89 				PAPI_ATTR_REPLACE,
90 				"printer-is-accepting-jobs", PAPI_TRUE);
91 
92 	}
93 
94 	return (status);
95 }
96 
97 papi_status_t
98 papiPrinterPurgeJobs(papi_service_t handle, char *name, papi_job_t **jobs)
99 {
100 	papi_status_t status;
101 	service_t *svc = handle;
102 
103 	if ((svc == NULL) || (name == NULL))
104 		return (PAPI_BAD_ARGUMENT);
105 
106 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
107 		status = lpd_purge_jobs(svc, (job_t ***)jobs);
108 
109 	return (status);
110 }
111 
112 papi_status_t
113 papiPrinterListJobs(papi_service_t handle, char *name,
114 		char **requested_attrs, int type_mask,
115 		int max_num_jobs, papi_job_t **jobs)
116 {
117 	papi_status_t status;
118 	service_t *svc = handle;
119 
120 	if ((svc == NULL) || (name == NULL) || (jobs == NULL))
121 		return (PAPI_BAD_ARGUMENT);
122 
123 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
124 		status = lpd_find_jobs_info(svc, (job_t ***)jobs);
125 
126 	return (status);
127 }
128 
129 papi_attribute_t **
130 papiPrinterGetAttributeList(papi_printer_t printer)
131 {
132 	printer_t *p = printer;
133 
134 	if (p == NULL)
135 		return (NULL);
136 
137 	return (p->attributes);
138 }
139 
140 void
141 papiPrinterFree(papi_printer_t printer)
142 {
143 	printer_t *p = printer;
144 
145 	if (p != NULL) {
146 		if (p->attributes != NULL)
147 			papiAttributeListFree(p->attributes);
148 		free(p);
149 	}
150 }
151 
152 void
153 papiPrinterListFree(papi_printer_t *printers)
154 {
155 	if (printers != NULL) {
156 		int i;
157 
158 		for (i = 0; printers[i] != NULL; i++)
159 			papiPrinterFree(printers[i]);
160 		free(printers);
161 	}
162 }
163