xref: /titanic_52/usr/src/cmd/lp/lib/papi/printer.c (revision 40e7ce05bde825a43b0db32ded62db8cc5b6ec9c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22c389c7f8SGowtham Thommandra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <papi_impl.h>
327c478bd9Sstevel@tonic-gate #include <lp.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate extern int isclass(char *);
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate void
377c478bd9Sstevel@tonic-gate papiPrinterFree(papi_printer_t printer)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	printer_t *tmp = printer;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	if (tmp != NULL) {
427c478bd9Sstevel@tonic-gate 		papiAttributeListFree(tmp->attributes);
437c478bd9Sstevel@tonic-gate 		free(tmp);
447c478bd9Sstevel@tonic-gate 	}
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate void
487c478bd9Sstevel@tonic-gate papiPrinterListFree(papi_printer_t *printers)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	if (printers != NULL) {
517c478bd9Sstevel@tonic-gate 		int i;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 		for (i = 0; printers[i] != NULL; i++)
547c478bd9Sstevel@tonic-gate 			papiPrinterFree(printers[i]);
557c478bd9Sstevel@tonic-gate 		free(printers);
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate papi_status_t
60355b4669Sjacobs papiPrintersList(papi_service_t handle, char **requested_attrs,
61355b4669Sjacobs 		papi_filter_t *filter, papi_printer_t **printers)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
647c478bd9Sstevel@tonic-gate 	printer_t *p = NULL;
657c478bd9Sstevel@tonic-gate 	short status = MOK;
667c478bd9Sstevel@tonic-gate 	char *printer = NULL,
677c478bd9Sstevel@tonic-gate 	    *form = NULL,
687c478bd9Sstevel@tonic-gate 	    *request_id = NULL,
697c478bd9Sstevel@tonic-gate 	    *character_set = NULL,
707c478bd9Sstevel@tonic-gate 	    *reject_reason = NULL,
717c478bd9Sstevel@tonic-gate 	    *disable_reason = NULL;
727c478bd9Sstevel@tonic-gate 	short printer_status = 0;
737c478bd9Sstevel@tonic-gate 	long enable_date = 0, reject_date = 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if ((handle == NULL) || (printers == NULL))
767c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if ((filter == NULL) ||
797c478bd9Sstevel@tonic-gate 	    ((filter->filter.bitmask.mask & PAPI_PRINTER_LOCAL) ==
807c478bd9Sstevel@tonic-gate 	    (filter->filter.bitmask.value & PAPI_PRINTER_LOCAL))) {
817c478bd9Sstevel@tonic-gate 		/* ask the spooler for the printer(s) and state */
827c478bd9Sstevel@tonic-gate 		if (snd_msg(svc, S_INQUIRE_PRINTER_STATUS, NAME_ALL) < 0)
837c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		do {
867c478bd9Sstevel@tonic-gate 			if (rcv_msg(svc, R_INQUIRE_PRINTER_STATUS, &status,
877c478bd9Sstevel@tonic-gate 			    &printer, &form, &character_set,
887c478bd9Sstevel@tonic-gate 			    &disable_reason, &reject_reason,
897c478bd9Sstevel@tonic-gate 			    &printer_status, &request_id,
907c478bd9Sstevel@tonic-gate 			    &enable_date, &reject_date) < 0)
917c478bd9Sstevel@tonic-gate 				return (PAPI_SERVICE_UNAVAILABLE);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 			if ((p = calloc(1, sizeof (*p))) == NULL)
947c478bd9Sstevel@tonic-gate 				return (PAPI_TEMPORARY_ERROR);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 			lpsched_printer_configuration_to_attributes(svc, p,
977c478bd9Sstevel@tonic-gate 			    printer);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 			printer_status_to_attributes(p, printer, form,
1007c478bd9Sstevel@tonic-gate 			    character_set, disable_reason,
1017c478bd9Sstevel@tonic-gate 			    reject_reason, printer_status,
1027c478bd9Sstevel@tonic-gate 			    request_id, enable_date, reject_date);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 			list_append(printers, p);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		} while (status == MOKMORE);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if ((filter == NULL) ||
1107c478bd9Sstevel@tonic-gate 	    ((filter->filter.bitmask.mask & PAPI_PRINTER_CLASS) ==
1117c478bd9Sstevel@tonic-gate 	    (filter->filter.bitmask.value & PAPI_PRINTER_CLASS))) {
1127c478bd9Sstevel@tonic-gate 		/* ask the spooler for the class(es) and state */
1137c478bd9Sstevel@tonic-gate 		if (snd_msg(svc, S_INQUIRE_CLASS, NAME_ALL) < 0)
1147c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		do {
1177c478bd9Sstevel@tonic-gate 			if (rcv_msg(svc, R_INQUIRE_CLASS, &status, &printer,
1187c478bd9Sstevel@tonic-gate 			    &printer_status, &reject_reason,
1197c478bd9Sstevel@tonic-gate 			    &reject_date) < 0)
1207c478bd9Sstevel@tonic-gate 				return (PAPI_SERVICE_UNAVAILABLE);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 			if ((p = calloc(1, sizeof (*p))) == NULL)
1237c478bd9Sstevel@tonic-gate 				return (PAPI_TEMPORARY_ERROR);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 			lpsched_class_configuration_to_attributes(svc, p,
1267c478bd9Sstevel@tonic-gate 			    printer);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 			class_status_to_attributes(p, printer, printer_status,
1297c478bd9Sstevel@tonic-gate 			    reject_reason, reject_date);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 			list_append(printers, p);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		} while (status == MOKMORE);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate papi_status_t
140355b4669Sjacobs papiPrinterQuery(papi_service_t handle, char *name,
141355b4669Sjacobs 		char **requested_attrs,
142355b4669Sjacobs 		papi_attribute_t **job_attrs,
1437c478bd9Sstevel@tonic-gate 		papi_printer_t *printer)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	papi_status_t pst;
1467c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
1477c478bd9Sstevel@tonic-gate 	printer_t *p = NULL;
1487c478bd9Sstevel@tonic-gate 	char *dest;
1497c478bd9Sstevel@tonic-gate 	short status = MOK;
1507c478bd9Sstevel@tonic-gate 	char *pname = NULL,
1517c478bd9Sstevel@tonic-gate 	    *form = NULL,
1527c478bd9Sstevel@tonic-gate 	    *request_id = NULL,
1537c478bd9Sstevel@tonic-gate 	    *character_set = NULL,
1547c478bd9Sstevel@tonic-gate 	    *reject_reason = NULL,
1557c478bd9Sstevel@tonic-gate 	    *disable_reason = NULL;
1567c478bd9Sstevel@tonic-gate 	short printer_status = 0;
1577c478bd9Sstevel@tonic-gate 	long enable_date = 0, reject_date = 0;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if ((handle == NULL) || (name == NULL) || (printer == NULL))
1607c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if ((*printer = p = calloc(1, sizeof (*p))) == NULL)
1637c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(name, -1);
1667c478bd9Sstevel@tonic-gate 
167355b4669Sjacobs 	if (strcmp(dest, "_default") == 0) {
168355b4669Sjacobs 		static char *_default;
169355b4669Sjacobs 
170355b4669Sjacobs 		if (_default == NULL) {
171355b4669Sjacobs 			int fd;
172355b4669Sjacobs 			static char buf[128];
173355b4669Sjacobs 
174355b4669Sjacobs 			if ((fd = open("/etc/lp/default", O_RDONLY)) >= 0) {
175355b4669Sjacobs 				read(fd, buf, sizeof (buf));
176355b4669Sjacobs 				close(fd);
177355b4669Sjacobs 				_default = strtok(buf, " \t\n");
178355b4669Sjacobs 			}
179355b4669Sjacobs 		}
180355b4669Sjacobs 		dest = _default;
181355b4669Sjacobs 	}
182355b4669Sjacobs 
1837c478bd9Sstevel@tonic-gate 	if (isprinter(dest) != 0) {
1847c478bd9Sstevel@tonic-gate 		pst = lpsched_printer_configuration_to_attributes(svc, p, dest);
1857c478bd9Sstevel@tonic-gate 		if (pst != PAPI_OK)
1867c478bd9Sstevel@tonic-gate 			return (pst);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		/* get the spooler status data now */
1897c478bd9Sstevel@tonic-gate 		if (snd_msg(svc, S_INQUIRE_PRINTER_STATUS, dest) < 0)
1907c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		if (rcv_msg(svc, R_INQUIRE_PRINTER_STATUS, &status, &pname,
1937c478bd9Sstevel@tonic-gate 		    &form, &character_set, &disable_reason,
1947c478bd9Sstevel@tonic-gate 		    &reject_reason, &printer_status, &request_id,
1957c478bd9Sstevel@tonic-gate 		    &enable_date, &reject_date) < 0)
1967c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		printer_status_to_attributes(p, pname, form, character_set,
1997c478bd9Sstevel@tonic-gate 		    disable_reason, reject_reason, printer_status,
2007c478bd9Sstevel@tonic-gate 		    request_id, enable_date, reject_date);
2017c478bd9Sstevel@tonic-gate 	} else if (isclass(dest) != 0) {
2027c478bd9Sstevel@tonic-gate 		pst = lpsched_class_configuration_to_attributes(svc, p, dest);
2037c478bd9Sstevel@tonic-gate 		if (pst != PAPI_OK)
2047c478bd9Sstevel@tonic-gate 			return (pst);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		/* get the spooler status data now */
2077c478bd9Sstevel@tonic-gate 		if (snd_msg(svc, S_INQUIRE_CLASS, dest) < 0)
2087c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (rcv_msg(svc, R_INQUIRE_CLASS, &status, &pname,
2117c478bd9Sstevel@tonic-gate 		    &printer_status, &reject_reason,
2127c478bd9Sstevel@tonic-gate 		    &reject_date) < 0)
2137c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		class_status_to_attributes(p, pname, printer_status,
2167c478bd9Sstevel@tonic-gate 		    reject_reason, reject_date);
2177c478bd9Sstevel@tonic-gate 	} else if (strcmp(dest, "PrintService") == 0) {
2187c478bd9Sstevel@tonic-gate 		/* fill the printer object with service information */
219355b4669Sjacobs 		lpsched_service_information(&p->attributes);
2207c478bd9Sstevel@tonic-gate 	} else
2217c478bd9Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	free(dest);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate papi_status_t
229355b4669Sjacobs papiPrinterAdd(papi_service_t handle, char *name,
230355b4669Sjacobs 		papi_attribute_t **attributes, papi_printer_t *result)
2317c478bd9Sstevel@tonic-gate {
232355b4669Sjacobs 	papi_status_t status;
233355b4669Sjacobs 	printer_t *p = NULL;
234355b4669Sjacobs 	char *dest;
2357c478bd9Sstevel@tonic-gate 
236355b4669Sjacobs 	if ((handle == NULL) || (name == NULL) || (attributes == NULL))
2377c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
2387c478bd9Sstevel@tonic-gate 
239355b4669Sjacobs 	dest = printer_name_from_uri_id(name, -1);
240355b4669Sjacobs 
241355b4669Sjacobs 	if (isprinter(dest) != 0) {
242355b4669Sjacobs 		status = lpsched_add_modify_printer(handle, dest,
243355b4669Sjacobs 		    attributes, 0);
244355b4669Sjacobs 
245355b4669Sjacobs 		if ((*result = p = calloc(1, sizeof (*p))) != NULL)
246355b4669Sjacobs 			lpsched_printer_configuration_to_attributes(handle, p,
247355b4669Sjacobs 			    dest);
248355b4669Sjacobs 		else
249355b4669Sjacobs 			status = PAPI_TEMPORARY_ERROR;
250355b4669Sjacobs 
251355b4669Sjacobs 	} else if (isclass(dest) != 0) {
252355b4669Sjacobs 		status = lpsched_add_modify_class(handle, dest, attributes);
253355b4669Sjacobs 
254355b4669Sjacobs 		if ((*result = p = calloc(1, sizeof (*p))) != NULL)
255355b4669Sjacobs 			lpsched_class_configuration_to_attributes(handle, p,
256355b4669Sjacobs 			    dest);
257355b4669Sjacobs 		else
258355b4669Sjacobs 			status = PAPI_TEMPORARY_ERROR;
259355b4669Sjacobs 
260355b4669Sjacobs 	} else
261355b4669Sjacobs 		status = PAPI_NOT_FOUND;
262355b4669Sjacobs 
263355b4669Sjacobs 	free(dest);
264355b4669Sjacobs 
265355b4669Sjacobs 	return (status);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate papi_status_t
269355b4669Sjacobs papiPrinterModify(papi_service_t handle, char *name,
270355b4669Sjacobs 		papi_attribute_t **attributes, papi_printer_t *result)
271355b4669Sjacobs {
272355b4669Sjacobs 	papi_status_t status;
273355b4669Sjacobs 	printer_t *p = NULL;
274355b4669Sjacobs 	char *dest;
275355b4669Sjacobs 
276355b4669Sjacobs 	if ((handle == NULL) || (name == NULL) || (attributes == NULL))
277355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
278355b4669Sjacobs 
279355b4669Sjacobs 	dest = printer_name_from_uri_id(name, -1);
280355b4669Sjacobs 
281355b4669Sjacobs 	if (isprinter(dest) != 0) {
282355b4669Sjacobs 		status = lpsched_add_modify_printer(handle, dest,
283355b4669Sjacobs 		    attributes, 1);
284355b4669Sjacobs 
285355b4669Sjacobs 		if ((*result = p = calloc(1, sizeof (*p))) != NULL)
286355b4669Sjacobs 			lpsched_printer_configuration_to_attributes(handle, p,
287355b4669Sjacobs 			    dest);
288355b4669Sjacobs 		else
289355b4669Sjacobs 			status = PAPI_TEMPORARY_ERROR;
290355b4669Sjacobs 	} else if (isclass(dest) != 0) {
291355b4669Sjacobs 		status = lpsched_add_modify_class(handle, dest, attributes);
292355b4669Sjacobs 
293355b4669Sjacobs 		if ((*result = p = calloc(1, sizeof (*p))) != NULL)
294355b4669Sjacobs 			lpsched_class_configuration_to_attributes(handle, p,
295355b4669Sjacobs 			    dest);
296355b4669Sjacobs 		else
297355b4669Sjacobs 			status = PAPI_TEMPORARY_ERROR;
298355b4669Sjacobs 	} else
299355b4669Sjacobs 		status = PAPI_NOT_FOUND;
300355b4669Sjacobs 
301355b4669Sjacobs 	free(dest);
302355b4669Sjacobs 
303355b4669Sjacobs 	return (status);
304355b4669Sjacobs }
305355b4669Sjacobs 
306355b4669Sjacobs papi_status_t
307355b4669Sjacobs papiPrinterRemove(papi_service_t handle, char *name)
308355b4669Sjacobs {
309355b4669Sjacobs 	papi_status_t result;
310355b4669Sjacobs 	char *dest;
311355b4669Sjacobs 
312355b4669Sjacobs 	if ((handle == NULL) || (name == NULL))
313355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
314355b4669Sjacobs 
315355b4669Sjacobs 	dest = printer_name_from_uri_id(name, -1);
316355b4669Sjacobs 
317355b4669Sjacobs 	if (isprinter(dest) != 0) {
318355b4669Sjacobs 		result = lpsched_remove_printer(handle, dest);
319355b4669Sjacobs 	} else if (isclass(dest) != 0) {
320355b4669Sjacobs 		result = lpsched_remove_class(handle, dest);
321355b4669Sjacobs 	} else
322355b4669Sjacobs 		result = PAPI_NOT_FOUND;
323355b4669Sjacobs 
324355b4669Sjacobs 	free(dest);
325355b4669Sjacobs 
326355b4669Sjacobs 	return (result);
327355b4669Sjacobs }
328355b4669Sjacobs 
329355b4669Sjacobs papi_status_t
330355b4669Sjacobs papiPrinterDisable(papi_service_t handle, char *name, char *message)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	papi_status_t result;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if ((handle == NULL) || (name == NULL))
3357c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	result = lpsched_disable_printer(handle, name, message);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	return (result);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate papi_status_t
343355b4669Sjacobs papiPrinterEnable(papi_service_t handle, char *name)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	papi_status_t result;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if ((handle == NULL) || (name == NULL))
3487c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	result = lpsched_enable_printer(handle, name);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	return (result);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate papi_status_t
356355b4669Sjacobs papiPrinterPause(papi_service_t handle, char *name, char *message)
357355b4669Sjacobs {
358355b4669Sjacobs 	papi_status_t result;
359355b4669Sjacobs 
360355b4669Sjacobs 	if ((handle == NULL) || (name == NULL))
361355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
362355b4669Sjacobs 
363355b4669Sjacobs 	result = lpsched_reject_printer(handle, name, message);
364355b4669Sjacobs 
365355b4669Sjacobs 	return (result);
366355b4669Sjacobs }
367355b4669Sjacobs 
368355b4669Sjacobs papi_status_t
369355b4669Sjacobs papiPrinterResume(papi_service_t handle, char *name)
370355b4669Sjacobs {
371355b4669Sjacobs 	papi_status_t result;
372355b4669Sjacobs 
373355b4669Sjacobs 	if ((handle == NULL) || (name == NULL))
374355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
375355b4669Sjacobs 
376355b4669Sjacobs 	result = lpsched_accept_printer(handle, name);
377355b4669Sjacobs 
378355b4669Sjacobs 	return (result);
379355b4669Sjacobs }
380355b4669Sjacobs 
381355b4669Sjacobs papi_status_t
382355b4669Sjacobs papiPrinterPurgeJobs(papi_service_t handle, char *name, papi_job_t **jobs)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
3857c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_OK_SUBST;
3867c478bd9Sstevel@tonic-gate 	short more;
3877c478bd9Sstevel@tonic-gate 	long status;
3887c478bd9Sstevel@tonic-gate 	char *dest;
3897c478bd9Sstevel@tonic-gate 	char *req_id;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if ((handle == NULL) || (name == NULL))
3927c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(name, -1);
395355b4669Sjacobs 	more = snd_msg(svc, S_CANCEL, dest, "", "");
3967c478bd9Sstevel@tonic-gate 	free(dest);
397355b4669Sjacobs 	if (more < 0)
398355b4669Sjacobs 		return (PAPI_SERVICE_UNAVAILABLE);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	do {
4017c478bd9Sstevel@tonic-gate 		if (rcv_msg(svc, R_CANCEL, &more, &status, &req_id) < 0)
4027c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	switch (status) {
4057c478bd9Sstevel@tonic-gate 	case MOK:
4067c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
4077c478bd9Sstevel@tonic-gate 		    "canceled-jobs", req_id);
4087c478bd9Sstevel@tonic-gate 		break;
4097c478bd9Sstevel@tonic-gate 	case M2LATE:
4107c478bd9Sstevel@tonic-gate 	case MUNKNOWN:
4117c478bd9Sstevel@tonic-gate 	case MNOINFO:
4127c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
4137c478bd9Sstevel@tonic-gate 		    "cancel-failed", req_id);
4147c478bd9Sstevel@tonic-gate 		result = PAPI_DEVICE_ERROR;
4157c478bd9Sstevel@tonic-gate 		break;
4167c478bd9Sstevel@tonic-gate 	case MNOPERM:
4177c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
4187c478bd9Sstevel@tonic-gate 		    "cancel-failed", req_id);
4197c478bd9Sstevel@tonic-gate 		result = PAPI_NOT_AUTHORIZED;
4207c478bd9Sstevel@tonic-gate 		break;
4217c478bd9Sstevel@tonic-gate 	default:
4227c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("cancel failed, bad status (%d)\n"),
4237c478bd9Sstevel@tonic-gate 		    status);
4247c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	} while (more == MOKMORE);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	return (result);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate papi_status_t
432355b4669Sjacobs papiPrinterListJobs(papi_service_t handle, char *name,
433355b4669Sjacobs 		char **requested_attrs, int type_mask,
434355b4669Sjacobs 		int max_num_jobs, papi_job_t **jobs)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
4377c478bd9Sstevel@tonic-gate 	char *dest;
4387c478bd9Sstevel@tonic-gate 	short rc;
4397c478bd9Sstevel@tonic-gate 	int count = 1;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if ((handle == NULL) || (name == NULL) || (jobs == NULL))
4427c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(name, -1);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", dest, "", "", "");
4477c478bd9Sstevel@tonic-gate 	free(dest);
4487c478bd9Sstevel@tonic-gate 	if (rc < 0)
4497c478bd9Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	do {
4527c478bd9Sstevel@tonic-gate 		job_t *job = NULL;
4537c478bd9Sstevel@tonic-gate 		char *dest = NULL,
4547c478bd9Sstevel@tonic-gate 		    *ptr,
4557c478bd9Sstevel@tonic-gate 		    *form = NULL,
4567c478bd9Sstevel@tonic-gate 		    *req_id = NULL,
4577c478bd9Sstevel@tonic-gate 		    *charset = NULL,
4587c478bd9Sstevel@tonic-gate 		    *owner = NULL,
45945916cd2Sjpk 		    *slabel = NULL,
4607c478bd9Sstevel@tonic-gate 		    *file = NULL;
461*40e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 		char request_file[128];
4627c478bd9Sstevel@tonic-gate 		time_t date = 0;
4637c478bd9Sstevel@tonic-gate 		size_t size = 0;
4647c478bd9Sstevel@tonic-gate 		short  rank = 0, state = 0;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &req_id,
46745916cd2Sjpk 		    &owner, &slabel, &size, &date, &state, &dest,
46845916cd2Sjpk 		    &form, &charset, &rank, &file) < 0)
4697c478bd9Sstevel@tonic-gate 			return (PAPI_SERVICE_UNAVAILABLE);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		if ((rc != MOK) && (rc != MOKMORE))
4727c478bd9Sstevel@tonic-gate 			continue;
4737c478bd9Sstevel@tonic-gate 		/*
4747c478bd9Sstevel@tonic-gate 		 * at this point, we should check to see if the job matches the
4757c478bd9Sstevel@tonic-gate 		 * selection criterion defined in "type_mask".
4767c478bd9Sstevel@tonic-gate 		 */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		/* too many yet? */
4797c478bd9Sstevel@tonic-gate 		if ((max_num_jobs != 0) && (count++ > max_num_jobs))
4807c478bd9Sstevel@tonic-gate 			continue;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		if ((job = calloc(1, sizeof (*job))) == NULL)
4837c478bd9Sstevel@tonic-gate 			continue;
4847c478bd9Sstevel@tonic-gate 
485*40e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 		/* Request file is <req_id>-0 */
486*40e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 		if ((ptr = strrchr(req_id, '-')) != NULL) {
487*40e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 			++ptr;
488*40e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 			snprintf(request_file, sizeof (request_file),
489*40e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 			    "%s-0", ptr);
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 
492*40e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 		lpsched_read_job_configuration(svc, job, request_file);
4937c478bd9Sstevel@tonic-gate 
494c389c7f8SGowtham Thommandra 		job_status_to_attributes(job, req_id, owner, slabel, size,
495c389c7f8SGowtham Thommandra 		    date, state, dest, form, charset, rank, file);
496c389c7f8SGowtham Thommandra 
4977c478bd9Sstevel@tonic-gate 		list_append(jobs, job);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	} while (rc == MOKMORE);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (rc == MNOINFO)	/* If no jobs are found, it's still ok */
5027c478bd9Sstevel@tonic-gate 		rc = MOK;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (lpsched_status_to_papi_status(rc));
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate papi_attribute_t **
5087c478bd9Sstevel@tonic-gate papiPrinterGetAttributeList(papi_printer_t printer)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	printer_t *tmp = printer;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (tmp == NULL)
5137c478bd9Sstevel@tonic-gate 		return (NULL);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	return (tmp->attributes);
5167c478bd9Sstevel@tonic-gate }
517