xref: /titanic_50/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision e9a20b613b3184ca60a413a1de74b9e6bde67705)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
23f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpstat.c 173 2006-05-25 04:52:06Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs 
31355b4669Sjacobs #include <stdio.h>
32355b4669Sjacobs #include <stdlib.h>
33355b4669Sjacobs #include <unistd.h>
34355b4669Sjacobs #include <string.h>
35355b4669Sjacobs #include <locale.h>
36355b4669Sjacobs #include <libintl.h>
3791216fe4Swendyp #include <ctype.h>
38355b4669Sjacobs #include <pwd.h>
39355b4669Sjacobs #include <papi.h>
40355b4669Sjacobs #include <uri.h>
41355b4669Sjacobs #include "common.h"
42375b28ffSGowtham Thommandra #include "lp.h"
43355b4669Sjacobs 
44355b4669Sjacobs static void
45355b4669Sjacobs usage(char *program)
46355b4669Sjacobs {
47355b4669Sjacobs 	char *name;
48355b4669Sjacobs 
49355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
50355b4669Sjacobs 		name = program;
51355b4669Sjacobs 	else
52355b4669Sjacobs 		name++;
53355b4669Sjacobs 
54355b4669Sjacobs 	fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
55355b4669Sjacobs 	    "[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
56355b4669Sjacobs 	    "[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
57355b4669Sjacobs 	    "[-f [list] [-l]] [-u list]\n"),
58355b4669Sjacobs 	    name);
59355b4669Sjacobs 	exit(1);
60355b4669Sjacobs }
61355b4669Sjacobs 
62355b4669Sjacobs static char *
63355b4669Sjacobs nctime(time_t *t)
64355b4669Sjacobs {
65355b4669Sjacobs 	static char buf[64];
66355b4669Sjacobs 	struct tm *tm = localtime(t);
67355b4669Sjacobs 
68355b4669Sjacobs 	(void) strftime(buf, sizeof (buf), "%c", tm);
69355b4669Sjacobs 
70355b4669Sjacobs 	return (buf);
71355b4669Sjacobs }
72355b4669Sjacobs 
73355b4669Sjacobs static char *
74355b4669Sjacobs printer_name(papi_printer_t printer)
75355b4669Sjacobs {
76355b4669Sjacobs 	papi_attribute_t **attributes = papiPrinterGetAttributeList(printer);
77355b4669Sjacobs 	char *result = NULL;
78355b4669Sjacobs 
79355b4669Sjacobs 	if (attributes != NULL)
80355b4669Sjacobs 		papiAttributeListGetString(attributes, NULL,
81355b4669Sjacobs 		    "printer-name", &result);
82355b4669Sjacobs 
83355b4669Sjacobs 	return (result);
84355b4669Sjacobs }
85355b4669Sjacobs 
86355b4669Sjacobs static int
87355b4669Sjacobs lpstat_default_printer(papi_encryption_t encryption)
88355b4669Sjacobs {
89355b4669Sjacobs 	papi_status_t status;
90355b4669Sjacobs 	papi_service_t svc = NULL;
91355b4669Sjacobs 	papi_printer_t p = NULL;
92355b4669Sjacobs 	char *name = NULL;
93355b4669Sjacobs 
94355b4669Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback,
95355b4669Sjacobs 	    encryption, NULL);
96355b4669Sjacobs 	if (status == PAPI_OK) {
97355b4669Sjacobs 		char *req[] = { "printer-name", NULL };
98355b4669Sjacobs 
99355b4669Sjacobs 		status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p);
100355b4669Sjacobs 		if (p != NULL)
101355b4669Sjacobs 			name = printer_name(p);
102355b4669Sjacobs 	}
103355b4669Sjacobs 	if (name != NULL)
104355b4669Sjacobs 		printf(gettext("system default printer: %s\n"), name);
105355b4669Sjacobs 	else
106355b4669Sjacobs 		printf(gettext("no system default destination\n"));
107355b4669Sjacobs 	papiPrinterFree(p);
108355b4669Sjacobs 	papiServiceDestroy(svc);
109355b4669Sjacobs 
110355b4669Sjacobs 	return (0);
111355b4669Sjacobs }
112355b4669Sjacobs 
113355b4669Sjacobs static int
114355b4669Sjacobs lpstat_service_status(papi_encryption_t encryption)
115355b4669Sjacobs {
116355b4669Sjacobs 	int result = 0;
117355b4669Sjacobs 	papi_status_t status;
118355b4669Sjacobs 	papi_service_t svc = NULL;
119355b4669Sjacobs 	char *name = NULL;
120355b4669Sjacobs 
121355b4669Sjacobs 	if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
122355b4669Sjacobs 	    ((name = getenv("IPP_SERVER")) == NULL) &&
123355b4669Sjacobs 	    ((name = getenv("CUPS_SERVER")) == NULL))
124355b4669Sjacobs 		name = DEFAULT_SERVICE_URI;
125355b4669Sjacobs 
126355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
127355b4669Sjacobs 	    encryption, NULL);
128355b4669Sjacobs 	if (status != PAPI_OK) {
129355b4669Sjacobs 		printf(gettext("scheduler is not running\n"));
130355b4669Sjacobs 		result = -1;
131355b4669Sjacobs 	} else
132355b4669Sjacobs 		printf(gettext("scheduler is running\n"));
133355b4669Sjacobs 	papiServiceDestroy(svc);
134355b4669Sjacobs 
135355b4669Sjacobs 	return (result);
136355b4669Sjacobs }
137355b4669Sjacobs 
138355b4669Sjacobs static char *
139355b4669Sjacobs get_device_uri(papi_service_t svc, char *name)
140355b4669Sjacobs {
141355b4669Sjacobs 	papi_status_t status;
142355b4669Sjacobs 	papi_printer_t p = NULL;
143355b4669Sjacobs 	char *keys[] = { "device-uri", NULL };
144355b4669Sjacobs 	char *result = NULL;
145355b4669Sjacobs 
146355b4669Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
147355b4669Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
148355b4669Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
149355b4669Sjacobs 
150355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
151355b4669Sjacobs 		    "device-uri", &result);
152355b4669Sjacobs 		if (result != NULL)
153355b4669Sjacobs 			result = strdup(result);
154355b4669Sjacobs 
155355b4669Sjacobs 		papiPrinterFree(p);
156355b4669Sjacobs 	}
157355b4669Sjacobs 
158355b4669Sjacobs 	return (result);
159355b4669Sjacobs }
160355b4669Sjacobs 
161f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India static void
162f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India print_description(papi_attribute_t **list, char *printer_name)
163f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India {
164f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	char *str = "";
165f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
166f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	(void) papiAttributeListGetString(list, NULL,
167f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	    "printer-info", &str);
168f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
169f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	/*
170f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	 * If no printer-info is read then
171f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	 * by default the printer-info is <printer-name>@<server>
172f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	 */
173f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	if (str[0] == '\0') {
174f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		char *uri = NULL;
175f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		uri_t *u = NULL;
176f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
177f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		(void) papiAttributeListGetString(list, NULL,
178f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		    "printer-uri-supported", &uri);
179f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
180f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
181f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			char *nodename = localhostname();
182f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
183f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			if ((u->host == NULL) ||
184f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, "localhost") == 0) ||
185f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, nodename) == 0))
186f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 				printf(gettext("\tDescription:\n"));
187f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			else
188f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 				printf(gettext("\tDescription: %s@%s\n"),
189f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 				    printer_name, u->host);
190f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
191f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			uri_free(u);
192f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		} else
193f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			printf(gettext("\tDescription:\n"));
194f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	} else
195f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		printf(gettext("\tDescription: %s\n"), str);
196f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India }
197f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
198355b4669Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
199355b4669Sjacobs 					NULL };
200355b4669Sjacobs /* ARGSUSED2 */
201355b4669Sjacobs static int
202355b4669Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
203355b4669Sjacobs 		int verbose, int description)
204355b4669Sjacobs {
205355b4669Sjacobs 	papi_status_t status;
206355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
207355b4669Sjacobs 	char *uri = NULL;
208355b4669Sjacobs 	char *device = NULL;
209355b4669Sjacobs 	uri_t *u = NULL;
210355b4669Sjacobs 
211355b4669Sjacobs 	if (name == NULL) {
212355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
213355b4669Sjacobs 		    "printer-name", &name);
214355b4669Sjacobs 		if (status != PAPI_OK)
215355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
216355b4669Sjacobs 			    "printer-uri-supported", &name);
217355b4669Sjacobs 	}
218355b4669Sjacobs 
219355b4669Sjacobs 	if (name == NULL)
220355b4669Sjacobs 		return (-1);
221355b4669Sjacobs 
222355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
223355b4669Sjacobs 	    "printer-uri-supported", &uri);
224355b4669Sjacobs 
225355b4669Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
226355b4669Sjacobs 		char *nodename = localhostname();
227355b4669Sjacobs 
228355b4669Sjacobs 		if ((u->host == NULL) ||
229355b4669Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
230355b4669Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
231355b4669Sjacobs 			device = get_device_uri(svc, name);
232355b4669Sjacobs 
233355b4669Sjacobs 		if (device != NULL) {
234355b4669Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
235355b4669Sjacobs 			return (0);
236355b4669Sjacobs 		} else if (uri != NULL) {
237355b4669Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
238e059026eSKeerthi Kondaka 			    u->host?u->host:"localhost", uri);
239355b4669Sjacobs 			return (0);
240355b4669Sjacobs 		}
241355b4669Sjacobs 
242355b4669Sjacobs 		uri_free(u);
243355b4669Sjacobs 	}
244355b4669Sjacobs 
245355b4669Sjacobs 	return (0);
246355b4669Sjacobs }
247355b4669Sjacobs 
248355b4669Sjacobs static char *report_accepting_keys[] = { "printer-name",
249355b4669Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
250355b4669Sjacobs 			"printer-up-time", "printer-state-time",
251355b4669Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
252355b4669Sjacobs /* ARGSUSED2 */
253355b4669Sjacobs static int
254355b4669Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
255355b4669Sjacobs 		int verbose, int description)
256355b4669Sjacobs {
257355b4669Sjacobs 	papi_status_t status;
258355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
259355b4669Sjacobs 	time_t curr;
260355b4669Sjacobs 	char boolean = PAPI_FALSE;
261355b4669Sjacobs 
262355b4669Sjacobs 	if (name == NULL) {
263355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
264355b4669Sjacobs 		    "printer-name", &name);
265355b4669Sjacobs 		if (status != PAPI_OK)
266355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
267355b4669Sjacobs 			    "printer-uri-supported", &name);
268355b4669Sjacobs 	}
269355b4669Sjacobs 	if (name == NULL)
270355b4669Sjacobs 		return (-1);
271355b4669Sjacobs 
272355b4669Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
273355b4669Sjacobs 	    "printer-is-accepting-jobs", &boolean);
274355b4669Sjacobs 	(void) time(&curr);
275355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
276355b4669Sjacobs 	    "printer-up-time", &curr);
277355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
278355b4669Sjacobs 	    "printer-state-time", &curr);
279355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
280355b4669Sjacobs 	    "lpsched-reject-date", &curr);
281355b4669Sjacobs 
282355b4669Sjacobs 	if (boolean == PAPI_TRUE) {
283355b4669Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
284355b4669Sjacobs 		    name, nctime(&curr));
285355b4669Sjacobs 	} else {
286355b4669Sjacobs 		char *reason = "unknown reason";
287355b4669Sjacobs 
288355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
289355b4669Sjacobs 		    "lpsched-reject-reason", &reason);
290355b4669Sjacobs 
291355b4669Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
292355b4669Sjacobs 		    name, nctime(&curr), reason);
293355b4669Sjacobs 	}
294355b4669Sjacobs 
295355b4669Sjacobs 	return (0);
296355b4669Sjacobs }
297355b4669Sjacobs 
298355b4669Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
299355b4669Sjacobs 					"member-names", NULL };
300355b4669Sjacobs /* ARGSUSED2 */
301355b4669Sjacobs static int
302355b4669Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
303355b4669Sjacobs 		int verbose, int description)
304355b4669Sjacobs {
305355b4669Sjacobs 	papi_status_t status;
306355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
307355b4669Sjacobs 	char *member = NULL;
308355b4669Sjacobs 	void *iter = NULL;
309355b4669Sjacobs 
310355b4669Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
311355b4669Sjacobs 	    "member-names", &member);
312355b4669Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
313355b4669Sjacobs 		return (0);
314355b4669Sjacobs 
315355b4669Sjacobs 	if (name == NULL) {
316355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
317355b4669Sjacobs 		    "printer-name", &name);
318355b4669Sjacobs 		if (status != PAPI_OK)
319355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
320355b4669Sjacobs 			    "printer-uri-supported", &name);
321355b4669Sjacobs 	}
322355b4669Sjacobs 	if (name == NULL)
323355b4669Sjacobs 		return (-1);
324355b4669Sjacobs 
325355b4669Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
326355b4669Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
327355b4669Sjacobs 	    == PAPI_OK)
328355b4669Sjacobs 		printf("\t%s\n", member);
329355b4669Sjacobs 
330355b4669Sjacobs 	return (0);
331355b4669Sjacobs }
332355b4669Sjacobs 
333*e9a20b61SKeerthi Kondaka static int
334*e9a20b61SKeerthi Kondaka get_remote_hostname(papi_attribute_t **attrs, char **host)
335*e9a20b61SKeerthi Kondaka {
336*e9a20b61SKeerthi Kondaka 	char *uri = NULL;
337*e9a20b61SKeerthi Kondaka 	uri_t *u;
338*e9a20b61SKeerthi Kondaka 	char *nodename;
339*e9a20b61SKeerthi Kondaka 
340*e9a20b61SKeerthi Kondaka 	*host = NULL;
341*e9a20b61SKeerthi Kondaka 	(void) papiAttributeListGetString(attrs, NULL,
342*e9a20b61SKeerthi Kondaka 	    "job-originating-host-name", host);
343*e9a20b61SKeerthi Kondaka 	(void) papiAttributeListGetString(attrs, NULL,
344*e9a20b61SKeerthi Kondaka 	    "printer-uri-supported", &uri);
345*e9a20b61SKeerthi Kondaka 	if (*host == NULL) {
346*e9a20b61SKeerthi Kondaka 		if (uri != NULL) {
347*e9a20b61SKeerthi Kondaka 			if (uri_from_string(uri, &u) == 0) {
348*e9a20b61SKeerthi Kondaka 				if (u->host == NULL) {
349*e9a20b61SKeerthi Kondaka 					uri_free(u);
350*e9a20b61SKeerthi Kondaka 					return (0);
351*e9a20b61SKeerthi Kondaka 				}
352*e9a20b61SKeerthi Kondaka 				*host = strdup(u->host);
353*e9a20b61SKeerthi Kondaka 				uri_free(u);
354*e9a20b61SKeerthi Kondaka 			} else {
355*e9a20b61SKeerthi Kondaka 				return (0);
356*e9a20b61SKeerthi Kondaka 			}
357*e9a20b61SKeerthi Kondaka 		} else {
358*e9a20b61SKeerthi Kondaka 			return (0);
359*e9a20b61SKeerthi Kondaka 		}
360*e9a20b61SKeerthi Kondaka 	}
361*e9a20b61SKeerthi Kondaka 	nodename = localhostname();
362*e9a20b61SKeerthi Kondaka 	if ((strcasecmp(*host, "localhost") == 0) ||
363*e9a20b61SKeerthi Kondaka 	    (strcasecmp(*host, nodename) == 0)) {
364*e9a20b61SKeerthi Kondaka 		return (0);
365*e9a20b61SKeerthi Kondaka 	}
366*e9a20b61SKeerthi Kondaka 	return (1);
367*e9a20b61SKeerthi Kondaka }
368*e9a20b61SKeerthi Kondaka 
369355b4669Sjacobs static char *report_printer_keys[] = { "printer-name",
370355b4669Sjacobs 			"printer-uri-supported", "printer-state",
371355b4669Sjacobs 			"printer-up-time", "printer-state-time",
372355b4669Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
373355b4669Sjacobs 			"lpsched-disable-reason", NULL };
374355b4669Sjacobs /* ARGSUSED2 */
375355b4669Sjacobs static int
376355b4669Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
377355b4669Sjacobs 		int verbose, int description)
378355b4669Sjacobs {
379355b4669Sjacobs 	papi_status_t status;
380355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
381355b4669Sjacobs 	time_t curr;
382355b4669Sjacobs 	int32_t pstat = 0;
383355b4669Sjacobs 	char *member = NULL;
38454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 	papi_job_t *j = NULL;
385355b4669Sjacobs 
386355b4669Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
387355b4669Sjacobs 	    "member-names", &member);
388355b4669Sjacobs 	if (status == PAPI_OK)	/* it's a class */
389355b4669Sjacobs 		return (0);
390355b4669Sjacobs 
391355b4669Sjacobs 	if (name == NULL) {
392355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
393355b4669Sjacobs 		    "printer-name", &name);
394355b4669Sjacobs 		if (status != PAPI_OK)
395355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
396355b4669Sjacobs 			    "printer-uri-supported", &name);
397355b4669Sjacobs 	}
398355b4669Sjacobs 	if (name == NULL)
399355b4669Sjacobs 		return (-1);
400355b4669Sjacobs 
401355b4669Sjacobs 	printf(gettext("printer %s "), name);
402355b4669Sjacobs 
403355b4669Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
404355b4669Sjacobs 	    "printer-state", &pstat);
405355b4669Sjacobs 
406355b4669Sjacobs 	switch (pstat) {
407355b4669Sjacobs 	case 0x03:	/* idle */
408355b4669Sjacobs 		printf(gettext("idle. enabled"));
409355b4669Sjacobs 		break;
41054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 	case 0x04: /* processing */
41154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 		status = papiPrinterListJobs(svc, name, NULL,
41254d5ddccSsonam gupta - Sun Microsystems - Bangalore India 		    0, 0, &j);
41354d5ddccSsonam gupta - Sun Microsystems - Bangalore India 
41454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 		if (status == PAPI_OK) {
41554d5ddccSsonam gupta - Sun Microsystems - Bangalore India 			if (j != NULL) {
41654d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				int i = 0;
417355b4669Sjacobs 				int32_t jobid = 0;
41854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				int32_t jstate = 0;
41954d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				int flag = 0;
420355b4669Sjacobs 
42154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				for (i = 0; j[i] != NULL; ++i) {
422356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					papi_attribute_t **attr =
423356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					    papiJobGetAttributeList(j[i]);
42454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 
425356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					papiAttributeListGetInteger(attr,
42654d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    NULL, "job-state", &jstate);
427356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					papiAttributeListGetInteger(attr,
42854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    NULL, "job-id", &jobid);
42954d5ddccSsonam gupta - Sun Microsystems - Bangalore India 
43054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					/*
431c389c7f8SGowtham Thommandra 					 * If the job-state is in
432c389c7f8SGowtham Thommandra 					 * RS_PRINTING then only print.
43354d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					 */
434c389c7f8SGowtham Thommandra 					if (jstate == 0x0008) {
43554d5ddccSsonam gupta - Sun Microsystems - Bangalore India 						if (flag == 0)
43654d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							printf(gettext
43754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							    ("now printing"\
43854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							    " %s-%d. enabled"),
43954d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							    name, jobid);
44054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 						flag = 1;
44154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					}
44254d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				}
443355b4669Sjacobs 				papiJobListFree(j);
44454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 			}
445355b4669Sjacobs 		}
446355b4669Sjacobs 		break;
447355b4669Sjacobs 	case 0x05:	/* stopped */
448355b4669Sjacobs 		printf(gettext("disabled"));
449355b4669Sjacobs 		break;
450355b4669Sjacobs 	default:
451355b4669Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
452355b4669Sjacobs 		break;
453355b4669Sjacobs 	}
454355b4669Sjacobs 
455355b4669Sjacobs 	(void) time(&curr);
456355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
457355b4669Sjacobs 	    "printer-up-time", &curr);
458355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
459355b4669Sjacobs 	    "printer-state-time", &curr);
460355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
461355b4669Sjacobs 	    "lpsched-disable-date", &curr);
462355b4669Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
463355b4669Sjacobs 
464355b4669Sjacobs 	if (pstat == 0x05) {
465355b4669Sjacobs 		char *reason = "unknown reason";
466355b4669Sjacobs 
467355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
468355b4669Sjacobs 		    "printer-state-reasons", &reason);
469355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
470355b4669Sjacobs 		    "lpsched-disable-reason", &reason);
471355b4669Sjacobs 		printf(gettext("\t%s\n"), reason);
472355b4669Sjacobs 	}
473355b4669Sjacobs 
474355b4669Sjacobs 	if (verbose == 1) {
475355b4669Sjacobs 		void *iter;
476355b4669Sjacobs 		char *str;
477*e9a20b61SKeerthi Kondaka 		char *host = NULL;
478355b4669Sjacobs 
479*e9a20b61SKeerthi Kondaka 		if ((get_remote_hostname(attrs, &host)) != 0) {
480*e9a20b61SKeerthi Kondaka 			(void) printf(
481*e9a20b61SKeerthi Kondaka 			    gettext("\tRemote Name: %s\n\tRemote Server: "
482*e9a20b61SKeerthi Kondaka 			    "%s\n"), name, host);
483*e9a20b61SKeerthi Kondaka 			free(host);
484*e9a20b61SKeerthi Kondaka 			return (0);
485*e9a20b61SKeerthi Kondaka 		}
486355b4669Sjacobs 		str = "";
487355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
488355b4669Sjacobs 		    "form-ready", &str);
489355b4669Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
490355b4669Sjacobs 
491355b4669Sjacobs 		str = "";
492355b4669Sjacobs 		iter = NULL;
493355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
494355b4669Sjacobs 		    "document-format-supported", &str);
495355b4669Sjacobs 		printf(gettext("\tContent types: %s"), str);
496355b4669Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
497355b4669Sjacobs 		    == PAPI_OK)
498355b4669Sjacobs 			printf(", %s", str);
499355b4669Sjacobs 		printf("\n");
500355b4669Sjacobs 
501f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		/* Display the printer description */
502f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		print_description(attrs, name);
503355b4669Sjacobs 
504355b4669Sjacobs 		str = "";
505853389e3Swendyp 		iter = NULL;
506853389e3Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
507853389e3Swendyp 		    "lpsched-printer-type", &str);
508853389e3Swendyp 		printf(gettext("\tPrinter types: %s"), str);
509853389e3Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
510853389e3Swendyp 		    == PAPI_OK)
511853389e3Swendyp 			printf(", %s", str);
512853389e3Swendyp 		printf("\n");
513853389e3Swendyp 
514853389e3Swendyp 		str = "";
515355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
516355b4669Sjacobs 		    "lpsched-dial-info", &str);
517355b4669Sjacobs 		printf(gettext("\tConnection: %s\n"),
5185eb9a62cSJonathan Cowper-Andrewes 		    ((str[0] == '\0') ? gettext("direct") : str));
519355b4669Sjacobs 
520355b4669Sjacobs 		str = "";
521355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
522355b4669Sjacobs 		    "lpsched-interface-script", &str);
523355b4669Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
524355b4669Sjacobs 
525355b4669Sjacobs 		str = NULL;
526355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
527355b4669Sjacobs 		    "ppd-file-uri", &str);
528355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
529355b4669Sjacobs 		    "lpsched-ppd-source-path", &str);
530355b4669Sjacobs 		if (str != NULL)
531355b4669Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
532355b4669Sjacobs 
533355b4669Sjacobs 		str = NULL;
534355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
535355b4669Sjacobs 		    "lpsched-fault-alert-command", &str);
536355b4669Sjacobs 		if (str != NULL)
537355b4669Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
538355b4669Sjacobs 
539355b4669Sjacobs 		str = "";
540355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
541355b4669Sjacobs 		    "lpsched-fault-recovery", &str);
542355b4669Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
543355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("continue") : str));
544355b4669Sjacobs 
545355b4669Sjacobs 		str = "(all)";
546355b4669Sjacobs 		iter = NULL;
547355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
548355b4669Sjacobs 		    "requesting-user-name-allowed", &str);
549355b4669Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
550355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("(none)") : str));
551355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
552355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
553355b4669Sjacobs 			    &str) == PAPI_OK)
554355b4669Sjacobs 				printf("\t\t%s\n", str);
555355b4669Sjacobs 
556355b4669Sjacobs 		str = NULL;
557355b4669Sjacobs 		iter = NULL;
558355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
559355b4669Sjacobs 		    "requesting-user-name-denied", &str);
560355b4669Sjacobs 		if (str != NULL) {
561355b4669Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
562355b4669Sjacobs 			    ((str[0] == '\0') ? gettext("(none)") : str));
563355b4669Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
564355b4669Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
565355b4669Sjacobs 				    NULL, &str) == PAPI_OK)
566355b4669Sjacobs 					printf("\t\t%s\n", str);
567355b4669Sjacobs 		}
568355b4669Sjacobs 
569f06271beSsonam gupta - Sun Microsystems - Bangalore India 		str = "none";
570355b4669Sjacobs 		iter = NULL;
571355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
572355b4669Sjacobs 		    "form-supported", &str);
573f06271beSsonam gupta - Sun Microsystems - Bangalore India 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
574f06271beSsonam gupta - Sun Microsystems - Bangalore India 		    ((str[0] == '\0') ? gettext("none") : str));
575355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
576355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
577355b4669Sjacobs 			    &str) == PAPI_OK)
578f06271beSsonam gupta - Sun Microsystems - Bangalore India 				printf("\t\t(%s)\n", str);
579355b4669Sjacobs 
580355b4669Sjacobs 		str = "";
581355b4669Sjacobs 		iter = NULL;
582355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
583355b4669Sjacobs 		    "media-supported", &str);
584355b4669Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
585355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("(none)") : str));
586355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
587355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
588355b4669Sjacobs 			    &str) == PAPI_OK)
589355b4669Sjacobs 				printf("\t\t%s\n", str);
590355b4669Sjacobs 
591355b4669Sjacobs 		str = "";
592355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
593355b4669Sjacobs 		    "job-sheets-supported", &str);
594853389e3Swendyp 		if ((strcasecmp(str, "none")) == 0)
595853389e3Swendyp 			str = gettext("page never printed");
596853389e3Swendyp 		else if (strcasecmp(str, "optional") == 0)
597853389e3Swendyp 			str = gettext("not required");
598853389e3Swendyp 		else
599853389e3Swendyp 			str = gettext("required");
600853389e3Swendyp 
601853389e3Swendyp 		printf(gettext("\tBanner %s\n"), str);
602853389e3Swendyp 
603355b4669Sjacobs 
604355b4669Sjacobs 		str = "";
605355b4669Sjacobs 		iter = NULL;
606355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
607355b4669Sjacobs 		    "lpsched-print-wheels", &str);
608355b4669Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
609355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("(none)") : str));
610355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
611355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
612355b4669Sjacobs 			    &str) == PAPI_OK)
613355b4669Sjacobs 				printf("\t\t%s\n", str);
614355b4669Sjacobs 
615355b4669Sjacobs 		printf(gettext("\tDefault pitch:\n"));
616355b4669Sjacobs 		printf(gettext("\tDefault page size:\n"));
617355b4669Sjacobs 		printf(gettext("\tDefault port setting:\n"));
618355b4669Sjacobs 
619355b4669Sjacobs 		str = "";
620355b4669Sjacobs 		iter = NULL;
621355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
622355b4669Sjacobs 		    "lpsched-options", &str);
623355b4669Sjacobs 		if (str != NULL) {
624355b4669Sjacobs 			printf(gettext("\tOptions: %s"), str);
625355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
626355b4669Sjacobs 			    &str) == PAPI_OK)
627355b4669Sjacobs 				printf(", %s", str);
628355b4669Sjacobs 			printf("\n");
629355b4669Sjacobs 		}
630355b4669Sjacobs 
631f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	} else if (description == 1)
632f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		/* Display printer description */
633f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		print_description(attrs, name);
634f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	else if (verbose > 1)
635355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
636355b4669Sjacobs 
637355b4669Sjacobs 	if (verbose > 0)
638355b4669Sjacobs 		printf("\n");
639355b4669Sjacobs 
640355b4669Sjacobs 	return (0);
641355b4669Sjacobs }
642355b4669Sjacobs 
643355b4669Sjacobs static int
644355b4669Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
645355b4669Sjacobs 					int, int), papi_encryption_t encryption,
646355b4669Sjacobs 		int verbose, int description)
647355b4669Sjacobs {
648375b28ffSGowtham Thommandra 	int result = 0, i = 0;
649355b4669Sjacobs 	papi_status_t status;
650355b4669Sjacobs 	papi_service_t svc = NULL;
651375b28ffSGowtham Thommandra 	char **list = getlist(name, LP_WS, LP_SEP);
652355b4669Sjacobs 
653375b28ffSGowtham Thommandra 	if (list == NULL) {
654375b28ffSGowtham Thommandra 		list = (char **)malloc(sizeof (char *));
655375b28ffSGowtham Thommandra 		list[0] = name;
656375b28ffSGowtham Thommandra 	}
657375b28ffSGowtham Thommandra 
658375b28ffSGowtham Thommandra 	/*
659375b28ffSGowtham Thommandra 	 * The for loop executes once for every printer
660375b28ffSGowtham Thommandra 	 * entry in list. If list is NULL that implies
661375b28ffSGowtham Thommandra 	 * name is also NULL, the loop runs only one time.
662375b28ffSGowtham Thommandra 	 */
663375b28ffSGowtham Thommandra 
664375b28ffSGowtham Thommandra 	for (i = 0; name == NULL || list[i] != NULL; i++) {
665375b28ffSGowtham Thommandra 		name = list[i];
666375b28ffSGowtham Thommandra 
667375b28ffSGowtham Thommandra 		status = papiServiceCreate(&svc, name, NULL, NULL,
668375b28ffSGowtham Thommandra 		    cli_auth_callback, encryption, NULL);
669355b4669Sjacobs 		if (status != PAPI_OK) {
670022ba35cSjacobs 			if (status == PAPI_NOT_FOUND)
671375b28ffSGowtham Thommandra 				fprintf(stderr,
672375b28ffSGowtham Thommandra 				    gettext("%s: unknown printer\n"),
673022ba35cSjacobs 				    name ? name : "(NULL)");
674022ba35cSjacobs 			else
675355b4669Sjacobs 				fprintf(stderr, gettext(
676355b4669Sjacobs 				    "Failed to contact service for %s: %s\n"),
677355b4669Sjacobs 				    name ? name : "(NULL)",
678355b4669Sjacobs 				    verbose_papi_message(svc, status));
679355b4669Sjacobs 			papiServiceDestroy(svc);
680375b28ffSGowtham Thommandra 			result--;
681375b28ffSGowtham Thommandra 			continue;
682355b4669Sjacobs 		}
683355b4669Sjacobs 
684355b4669Sjacobs 		if (name == NULL) { /* all */
685355b4669Sjacobs 			char **interest = interest_list(svc);
686355b4669Sjacobs 
687355b4669Sjacobs 			if (interest != NULL) {
688355b4669Sjacobs 				int i;
689355b4669Sjacobs 
690355b4669Sjacobs 				for (i = 0; interest[i] != NULL; i++)
691375b28ffSGowtham Thommandra 					result += printer_query(interest[i],
692375b28ffSGowtham Thommandra 					    report, encryption, verbose,
693355b4669Sjacobs 					    description);
694355b4669Sjacobs 			}
695355b4669Sjacobs 		} else {
696355b4669Sjacobs 			papi_printer_t printer = NULL;
697355b4669Sjacobs 			char **keys = NULL;
698355b4669Sjacobs 
699355b4669Sjacobs 			/*
700375b28ffSGowtham Thommandra 			 * Limit the query to only required data
701375b28ffSGowtham Thommandra 			 * to reduce the need to go remote for
702375b28ffSGowtham Thommandra 			 * information.
703355b4669Sjacobs 			 */
704355b4669Sjacobs 			if (report == report_device)
705355b4669Sjacobs 				keys = report_device_keys;
706355b4669Sjacobs 			else if (report == report_class)
707355b4669Sjacobs 				keys = report_class_keys;
708355b4669Sjacobs 			else if (report == report_accepting)
709355b4669Sjacobs 				keys = report_accepting_keys;
710355b4669Sjacobs 			else if ((report == report_printer) && (verbose == 0))
711355b4669Sjacobs 				keys = report_printer_keys;
712355b4669Sjacobs 
713375b28ffSGowtham Thommandra 			status = papiPrinterQuery(svc, name, keys,
714375b28ffSGowtham Thommandra 			    NULL, &printer);
715355b4669Sjacobs 			if (status != PAPI_OK) {
716355b4669Sjacobs 				fprintf(stderr, gettext(
717355b4669Sjacobs 				    "Failed to get printer info for %s: %s\n"),
718355b4669Sjacobs 				    name, verbose_papi_message(svc, status));
719355b4669Sjacobs 				papiServiceDestroy(svc);
720375b28ffSGowtham Thommandra 				result--;
721375b28ffSGowtham Thommandra 				continue;
722355b4669Sjacobs 			}
723355b4669Sjacobs 
724355b4669Sjacobs 			if (printer != NULL)
725375b28ffSGowtham Thommandra 				result += report(svc, name, printer, verbose,
726355b4669Sjacobs 				    description);
727355b4669Sjacobs 
728355b4669Sjacobs 			papiPrinterFree(printer);
729355b4669Sjacobs 		}
730355b4669Sjacobs 
731355b4669Sjacobs 		papiServiceDestroy(svc);
732355b4669Sjacobs 
733375b28ffSGowtham Thommandra 		if (name == NULL)
734375b28ffSGowtham Thommandra 			break;
735375b28ffSGowtham Thommandra 	}
736375b28ffSGowtham Thommandra 
737375b28ffSGowtham Thommandra 	freelist(list);
738375b28ffSGowtham Thommandra 
739355b4669Sjacobs 	return (result);
740355b4669Sjacobs }
741355b4669Sjacobs 
742355b4669Sjacobs static int
743355b4669Sjacobs match_user(char *user, char **list)
744355b4669Sjacobs {
745355b4669Sjacobs 	int i;
746355b4669Sjacobs 
747355b4669Sjacobs 	for (i = 0; list[i] != NULL; i++) {
748355b4669Sjacobs 		if (strcmp(user, list[i]) == 0)
749355b4669Sjacobs 			return (0);
750355b4669Sjacobs 	}
751355b4669Sjacobs 
752355b4669Sjacobs 	return (-1);
753355b4669Sjacobs }
754355b4669Sjacobs 
755355b4669Sjacobs static char **users = NULL;
756355b4669Sjacobs 
757355b4669Sjacobs static int
75835a603adSsonam gupta - Sun Microsystems - Bangalore India report_job(char *printer, papi_job_t job, int show_rank, int verbose)
759355b4669Sjacobs {
760355b4669Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
761355b4669Sjacobs 	time_t clock = 0;
762355b4669Sjacobs 	char date[24];
763355b4669Sjacobs 	char request[26];
764355b4669Sjacobs 	char *user = "unknown";
765b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	char *host = NULL;
766355b4669Sjacobs 	int32_t size = 0;
767355b4669Sjacobs 	int32_t jstate = 0;
768b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	char User[50];
769355b4669Sjacobs 
770355b4669Sjacobs 	char *destination = "unknown";
771355b4669Sjacobs 	int32_t id = -1;
7723d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	static int check = 0;
7733d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	static char *uri = NULL;
774355b4669Sjacobs 
775355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
776355b4669Sjacobs 	    "job-originating-user-name", &user);
777355b4669Sjacobs 
778355b4669Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
779355b4669Sjacobs 		return (0);
780355b4669Sjacobs 
781b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	(void) papiAttributeListGetString(attrs, NULL,
782b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	    "job-originating-host-name", &host);
783b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 
7843d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	if (check == 0) {
7853d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		/*
7863d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * Read the attribute "job-printer-uri"
7873d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * just once
7883d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 */
7893d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		(void) papiAttributeListGetString(attrs, NULL,
7903d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-printer-uri", &uri);
7913d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		check = 1;
7923d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	}
7933d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
7943d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	if (host) {
7953d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		/* Check if it is local printer or remote printer */
7963d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		uri_t *u = NULL;
7973d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
7983d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
7993d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			char *nodename = localhostname();
8003d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
8013d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			if ((u->host == NULL) ||
8023d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, "localhost") == 0) ||
8033d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, nodename) == 0)) {
8043d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
8053d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				if (strcasecmp(host, nodename) == 0) {
8063d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					/*
8073d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 * Request submitted locally
8083d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 * for the local queue.
8093d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 * Hostname will not be displayed
8103d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 */
8113d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					snprintf(User, sizeof (User), "%s",
8123d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    user);
8133d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				}
8143d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				else
8153d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					snprintf(User, sizeof (User), "%s@%s",
8163d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    user, host);
8173d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			} else if (uri != NULL) {
8183d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				/*
8193d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 * It's a remote printer.
8203d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 * In case of remote printers hostname is
8213d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 * always displayed.
8223d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 */
8233d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				snprintf(User, sizeof (User), "%s@%s",
8243d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				    user, host);
8253d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			}
8263d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			uri_free(u);
8273d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		} else {
8283d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			/*
8293d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 * If attribute "job-printer-uri"
8303d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 * cannot be read
8313d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 * by default append the hostname
8323d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 */
833b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 			snprintf(User, sizeof (User), "%s@%s", user, host);
8343d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		}
8353d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	} else {
8363d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		/*
8373d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * When print server is s10u4 and ipp service is used
8383d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * "job-originating-hostname" attribute is not set
8393d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * So get the host information from the uri
8403d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 */
8413d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		uri_t *u = NULL;
8423d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
8433d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			if ((u != NULL) && (u->host != NULL))
8443d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				snprintf(User, sizeof (User), "%s@%s",
8453d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				    user, u->host);
846b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 			else
847b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 				snprintf(User, sizeof (User), "%s", user);
848b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 
8493d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			uri_free(u);
8503d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		} else
8513d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			snprintf(User, sizeof (User), "%s", user);
8523d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	}
853355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
854355b4669Sjacobs 	size *= 1024;	/* for the approximate byte size */
855355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
856355b4669Sjacobs 
857355b4669Sjacobs 	(void) time(&clock);
858355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
859355b4669Sjacobs 	    "time-at-creation", (int32_t *)&clock);
860355b4669Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
861355b4669Sjacobs 
862355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
863355b4669Sjacobs 	    "job-printer-uri", &destination);
864355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
865355b4669Sjacobs 	    "printer-name", &destination);
866355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
867355b4669Sjacobs 	    "job-id", &id);
86840e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 	(void) papiAttributeListGetInteger(attrs, NULL,
86940e7ce05Ssonam gupta - Sun Microsystems - Bangalore India 	    "job-id-requested", &id);
87035a603adSsonam gupta - Sun Microsystems - Bangalore India 
871f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
87235a603adSsonam gupta - Sun Microsystems - Bangalore India 	snprintf(request, sizeof (request), "%s-%d", printer, id);
873355b4669Sjacobs 
874355b4669Sjacobs 	if (show_rank != 0) {
875355b4669Sjacobs 		int32_t rank = -1;
876355b4669Sjacobs 
877355b4669Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
878355b4669Sjacobs 		    "number-of-intervening-jobs", &rank);
879355b4669Sjacobs 		rank++;
880355b4669Sjacobs 
881355b4669Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
882b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 		    rank, request, User, size, date);
883355b4669Sjacobs 	} else
884b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
885355b4669Sjacobs 
886355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
887355b4669Sjacobs 	    "job-state", &jstate);
888f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
889c389c7f8SGowtham Thommandra 	if (jstate == 0x0001)
890c389c7f8SGowtham Thommandra 		printf(gettext(" being held"));
891c389c7f8SGowtham Thommandra 	else if (jstate == 0x0800)
892c389c7f8SGowtham Thommandra 		printf(gettext(" notifying user"));
893c389c7f8SGowtham Thommandra 	else if (jstate == 0x0040)
894c389c7f8SGowtham Thommandra 		printf(gettext(" cancelled"));
895c389c7f8SGowtham Thommandra 	else if (jstate == 0x0010)
896c389c7f8SGowtham Thommandra 		printf(gettext(" finished printing"));
897c389c7f8SGowtham Thommandra 	else if (jstate == 0x0008)
898c389c7f8SGowtham Thommandra 		printf(gettext(" on %s"), destination);
899c389c7f8SGowtham Thommandra 	else if (jstate == 0x2000)
900c389c7f8SGowtham Thommandra 		printf(gettext(" held by admin"));
901c389c7f8SGowtham Thommandra 	else if (jstate == 0x0002)
902c389c7f8SGowtham Thommandra 		printf(gettext(" being filtered"));
903c389c7f8SGowtham Thommandra 	else if (jstate == 0x0004)
904c389c7f8SGowtham Thommandra 		printf(gettext(" filtered"));
905c389c7f8SGowtham Thommandra 	else if (jstate == 0x0020)
906c389c7f8SGowtham Thommandra 		printf(gettext(" held for change"));
907355b4669Sjacobs 
908355b4669Sjacobs 	if (verbose == 1) {
9090a44ef6dSjacobs 		char *form = NULL;
9100a44ef6dSjacobs 
911355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
912355b4669Sjacobs 		    "output-device-assigned", &destination);
913355b4669Sjacobs 		printf("\n\t assigned %s", destination);
9140a44ef6dSjacobs 
9150a44ef6dSjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
9160a44ef6dSjacobs 		if (form != NULL)
9170a44ef6dSjacobs 			printf(", form %s", form);
918355b4669Sjacobs 	} else if (verbose > 1) {
919355b4669Sjacobs 		printf("\n");
920355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
921355b4669Sjacobs 	}
922355b4669Sjacobs 
923355b4669Sjacobs 	printf("\n");
924355b4669Sjacobs 
925355b4669Sjacobs 	return (0);
926355b4669Sjacobs }
927355b4669Sjacobs 
928355b4669Sjacobs static int
92935a603adSsonam gupta - Sun Microsystems - Bangalore India job_query(char *request, int (*report)(char *, papi_job_t, int, int),
930355b4669Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
931355b4669Sjacobs {
932355b4669Sjacobs 	int result = 0;
933355b4669Sjacobs 	papi_status_t status;
934355b4669Sjacobs 	papi_service_t svc = NULL;
9354749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	char *printer = request;
936355b4669Sjacobs 	int32_t id = -1;
9374749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	int flag1 = 0;
9384749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	int flag = 1;
9394749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	int print_flag = 0;
940355b4669Sjacobs 
9414749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	do {
9424749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		status = papiServiceCreate(&svc, printer, NULL, NULL,
9434749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		    cli_auth_callback, encryption, NULL);
944355b4669Sjacobs 
9454749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		if ((status == PAPI_OK) && (printer != NULL))
9464749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			print_flag = 1;
9474749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
9484749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		/* <name>-# printer name does not exist */
9494749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		if (status != PAPI_OK) {
9504749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			/*
9514749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			 * Check if <name>-# is a request-id
9524749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			 * Once this check is done flag1 is set
9534749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			 */
9544749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			if (flag1 == 1)
9554749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				break;
9564749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
9574749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			get_printer_id(printer, &printer, &id);
9584749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
9594749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			status = papiServiceCreate(&svc, printer, NULL, NULL,
9604749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			    cli_auth_callback, encryption, NULL);
9614749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
962355b4669Sjacobs 			if (status != PAPI_OK) {
963355b4669Sjacobs 				fprintf(stderr, gettext(
964355b4669Sjacobs 				    "Failed to contact service for %s: %s\n"),
965355b4669Sjacobs 				    (printer ? printer : "all"),
966355b4669Sjacobs 				    verbose_papi_message(svc, status));
967355b4669Sjacobs 				return (-1);
968355b4669Sjacobs 			}
9694749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		}
970355b4669Sjacobs 
971355b4669Sjacobs 		if (printer == NULL) { /* all */
972355b4669Sjacobs 			char **interest = interest_list(svc);
973355b4669Sjacobs 
974355b4669Sjacobs 			if (interest != NULL) {
975355b4669Sjacobs 				int i;
976355b4669Sjacobs 
977355b4669Sjacobs 				for (i = 0; interest[i] != NULL; i++)
978355b4669Sjacobs 					result += job_query(interest[i], report,
979355b4669Sjacobs 					    encryption, show_rank, verbose);
980355b4669Sjacobs 			}
981355b4669Sjacobs 		} else if (id == -1) { /* a printer */
982355b4669Sjacobs 			papi_job_t *jobs = NULL;
983355b4669Sjacobs 
9844749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			status = papiPrinterListJobs(svc, printer, NULL,
9854749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			    0, 0, &jobs);
986355b4669Sjacobs 			if (status != PAPI_OK) {
987355b4669Sjacobs 				fprintf(stderr, gettext(
988355b4669Sjacobs 				    "Failed to get job list: %s\n"),
989355b4669Sjacobs 				    verbose_papi_message(svc, status));
990355b4669Sjacobs 				papiServiceDestroy(svc);
991355b4669Sjacobs 				return (-1);
992355b4669Sjacobs 			}
993355b4669Sjacobs 
994355b4669Sjacobs 			if (jobs != NULL) {
995355b4669Sjacobs 				int i;
996355b4669Sjacobs 
997355b4669Sjacobs 				for (i = 0; jobs[i] != NULL; i++)
99835a603adSsonam gupta - Sun Microsystems - Bangalore India 					result += report(printer,
99935a603adSsonam gupta - Sun Microsystems - Bangalore India 					    jobs[i], show_rank,
100035a603adSsonam gupta - Sun Microsystems - Bangalore India 					    verbose);
1001355b4669Sjacobs 			}
1002355b4669Sjacobs 
1003355b4669Sjacobs 			papiJobListFree(jobs);
1004355b4669Sjacobs 		} else {	/* a job */
1005355b4669Sjacobs 			papi_job_t job = NULL;
1006f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			int rid = id;
1007355b4669Sjacobs 
10084749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			/* Once a job has been found stop processing */
10094749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			flag = 0;
10104749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
1011f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			/*
1012f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			 * Job-id could be the job-id requested
1013f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			 * Check if it is job-id or job-id-requested
1014f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			 */
1015f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			id = job_to_be_queried(svc, printer, id);
1016f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
1017f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			if (id > 0)
1018375b28ffSGowtham Thommandra 				status = papiJobQuery(svc, printer, id,
1019375b28ffSGowtham Thommandra 				    NULL, &job);
1020f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			else
1021375b28ffSGowtham Thommandra 				status = papiJobQuery(svc, printer, rid,
1022375b28ffSGowtham Thommandra 				    NULL, &job);
1023f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
1024355b4669Sjacobs 			if (status != PAPI_OK) {
10254749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				if (!print_flag)
1026355b4669Sjacobs 					fprintf(stderr, gettext(
102754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    "Failed to get job"\
102854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    " info for %s: %s\n"),
102954d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    request,
103054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    verbose_papi_message(svc, status));
1031355b4669Sjacobs 				papiServiceDestroy(svc);
1032355b4669Sjacobs 				return (-1);
1033355b4669Sjacobs 			}
1034355b4669Sjacobs 
1035355b4669Sjacobs 			if (job != NULL)
103635a603adSsonam gupta - Sun Microsystems - Bangalore India 				result = report(printer, job,
103735a603adSsonam gupta - Sun Microsystems - Bangalore India 				    show_rank, verbose);
1038355b4669Sjacobs 
1039355b4669Sjacobs 			papiJobFree(job);
1040355b4669Sjacobs 		}
1041355b4669Sjacobs 
10424749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		if (flag) {
10434749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			id = -1;
10444749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			get_printer_id(printer, &printer, &id);
10454749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			if (id == -1)
10464749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				flag = 0;
10474749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			else
10484749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				flag1 = 1;
10494749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		}
10504749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	} while (flag);
10514749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
1052355b4669Sjacobs 	papiServiceDestroy(svc);
1053355b4669Sjacobs 
1054355b4669Sjacobs 	return (result);
1055355b4669Sjacobs }
1056355b4669Sjacobs 
1057355b4669Sjacobs static int
1058355b4669Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
1059355b4669Sjacobs {
1060355b4669Sjacobs 	papi_status_t status;
1061355b4669Sjacobs 	char *form = NULL;
1062355b4669Sjacobs 	void *iter = NULL;
1063355b4669Sjacobs 
1064355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
1065355b4669Sjacobs 	    "form-supported", &form);
1066355b4669Sjacobs 	    status == PAPI_OK;
1067355b4669Sjacobs 	    status = papiAttributeListGetString(attrs, &iter,
1068355b4669Sjacobs 	    NULL, &form)) {
1069355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
1070355b4669Sjacobs 			printf(gettext("form %s is available to you\n"), form);
1071355b4669Sjacobs 			if (verbose != 0) {
1072355b4669Sjacobs 				char *detail = NULL;
1073355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
107454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				    "form-supported-detail", &detail);
1075355b4669Sjacobs 				if (status == PAPI_OK)
1076355b4669Sjacobs 					printf("%s\n", detail);
1077355b4669Sjacobs 			}
1078355b4669Sjacobs 		}
1079355b4669Sjacobs 	}
1080355b4669Sjacobs 
1081355b4669Sjacobs 	return (0);
1082355b4669Sjacobs }
1083355b4669Sjacobs 
1084355b4669Sjacobs static int
1085355b4669Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
1086355b4669Sjacobs {
1087355b4669Sjacobs 	papi_status_t status;
1088355b4669Sjacobs 	char *pw = NULL;
1089355b4669Sjacobs 	void *iter = NULL;
1090355b4669Sjacobs 
1091355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
1092355b4669Sjacobs 	    "pw-supported", &pw);
1093355b4669Sjacobs 	    status == PAPI_OK;
1094355b4669Sjacobs 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
1095355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
1096355b4669Sjacobs 			printf(gettext("charset %s is available\n"), pw);
1097355b4669Sjacobs 			if (verbose != 0) {
1098355b4669Sjacobs 				char *info = NULL;
1099355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
1100355b4669Sjacobs 				    "pw-supported-extra", &info);
1101355b4669Sjacobs 				if (status == PAPI_OK)
1102355b4669Sjacobs 					printf("%s\n", info);
1103355b4669Sjacobs 			}
1104355b4669Sjacobs 		}
1105355b4669Sjacobs 	}
1106355b4669Sjacobs 
1107355b4669Sjacobs 	return (0);
1108355b4669Sjacobs }
1109355b4669Sjacobs 
1110355b4669Sjacobs static int
1111355b4669Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
1112355b4669Sjacobs 		papi_encryption_t encryption, int verbose)
1113355b4669Sjacobs {
1114355b4669Sjacobs 	int result = 0;
1115355b4669Sjacobs 	papi_status_t status;
1116355b4669Sjacobs 	papi_service_t svc = NULL;
1117355b4669Sjacobs 	papi_attribute_t **attrs = NULL;
1118355b4669Sjacobs 
1119355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1120355b4669Sjacobs 	    encryption, NULL);
1121355b4669Sjacobs 	if (status != PAPI_OK) {
1122355b4669Sjacobs 		papiServiceDestroy(svc);
1123355b4669Sjacobs 		return (-1);
1124355b4669Sjacobs 	}
1125355b4669Sjacobs 
1126355b4669Sjacobs 	attrs = papiServiceGetAttributeList(svc);
1127355b4669Sjacobs 	if (attrs != NULL) {
1128355b4669Sjacobs 		result = report(name, attrs, verbose);
1129355b4669Sjacobs 
1130355b4669Sjacobs 		if (verbose > 1) {
1131355b4669Sjacobs 			printf("\n");
1132355b4669Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
1133355b4669Sjacobs 			printf("\n");
1134355b4669Sjacobs 		}
1135355b4669Sjacobs 	}
1136355b4669Sjacobs 
1137355b4669Sjacobs 	papiServiceDestroy(svc);
1138355b4669Sjacobs 
1139355b4669Sjacobs 	return (result);
1140355b4669Sjacobs }
1141355b4669Sjacobs 
1142355b4669Sjacobs int
1143355b4669Sjacobs main(int ac, char *av[])
1144355b4669Sjacobs {
1145355b4669Sjacobs 	int exit_code = 0;
1146355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
1147355b4669Sjacobs 	int rank = 0;
1148355b4669Sjacobs 	int verbose = 0;
1149355b4669Sjacobs 	int description = 0;
1150355b4669Sjacobs 	int c;
1151355b4669Sjacobs 	char **argv;
1152355b4669Sjacobs 
1153355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
1154355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
1155355b4669Sjacobs 
1156355b4669Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
115791216fe4Swendyp 	for (c = 0; c < ac; c++) {
115891216fe4Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
115991216fe4Swendyp 		    (isalpha(av[c][2]) != 0)) {
116091216fe4Swendyp 			/* preserve old "-l[po...]" behavior */
116191216fe4Swendyp 			argv[c] = &av[c][1];
116291216fe4Swendyp 			argv[c][0] = '-';
116391216fe4Swendyp 			verbose = 1;
116491216fe4Swendyp 
116591216fe4Swendyp 		} else
1166355b4669Sjacobs 			argv[c] = av[c];
116791216fe4Swendyp 	}
116891216fe4Swendyp 
1169355b4669Sjacobs 	argv[c++] = "--";
1170355b4669Sjacobs 	ac = c;
1171355b4669Sjacobs 
1172355b4669Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
11735abc75a2SKeerthi Kondaka 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
11745abc75a2SKeerthi Kondaka 		switch (c) {    /* these may or may not have an option */
11755abc75a2SKeerthi Kondaka 		case 'a':
11765abc75a2SKeerthi Kondaka 		case 'c':
11775abc75a2SKeerthi Kondaka 		case 'p':
11785abc75a2SKeerthi Kondaka 		case 'o':
11795abc75a2SKeerthi Kondaka 		case 'R':
11805abc75a2SKeerthi Kondaka 		case 'u':
11815abc75a2SKeerthi Kondaka 		case 'v':
11825abc75a2SKeerthi Kondaka 		case 'l':
11835abc75a2SKeerthi Kondaka 		case 'f':
11845abc75a2SKeerthi Kondaka 		case 'S':
11855abc75a2SKeerthi Kondaka 			if (optarg[0] == '-') {
11865abc75a2SKeerthi Kondaka 				/* this check stop a possible infinite loop */
11875abc75a2SKeerthi Kondaka 				if ((optind > 1) && (argv[optind-1][1] != c))
11885abc75a2SKeerthi Kondaka 					optind--;
11895abc75a2SKeerthi Kondaka 				optarg = NULL;
11905abc75a2SKeerthi Kondaka 			} else if (strcmp(optarg, "all") == 0)
11915abc75a2SKeerthi Kondaka 				optarg = NULL;
11925abc75a2SKeerthi Kondaka 		}
11935abc75a2SKeerthi Kondaka 
1194355b4669Sjacobs 		switch (c) {
1195355b4669Sjacobs 		case 'l':
1196355b4669Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
1197355b4669Sjacobs 				optarg = "1";
1198355b4669Sjacobs 			verbose = atoi(optarg);
1199355b4669Sjacobs 			break;
1200355b4669Sjacobs 		case 'D':
1201355b4669Sjacobs 			description = 1;
1202355b4669Sjacobs 			break;
1203355b4669Sjacobs 		case 'R':
1204355b4669Sjacobs 			rank = 1;
1205355b4669Sjacobs 			break;
1206355b4669Sjacobs 		case 'E':
1207355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
1208355b4669Sjacobs 			break;
1209355b4669Sjacobs 		default:
1210355b4669Sjacobs 			break;
1211355b4669Sjacobs 		}
12125abc75a2SKeerthi Kondaka 	}
1213355b4669Sjacobs 	optind = 1;
1214355b4669Sjacobs 
1215355b4669Sjacobs 	/* process command line arguments */
1216355b4669Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
1217355b4669Sjacobs 		switch (c) {	/* these may or may not have an option */
1218355b4669Sjacobs 		case 'a':
1219355b4669Sjacobs 		case 'c':
1220355b4669Sjacobs 		case 'p':
1221355b4669Sjacobs 		case 'o':
1222355b4669Sjacobs 		case 'R':
1223355b4669Sjacobs 		case 'u':
1224355b4669Sjacobs 		case 'v':
1225355b4669Sjacobs 		case 'l':
1226355b4669Sjacobs 		case 'f':
1227355b4669Sjacobs 		case 'S':
1228355b4669Sjacobs 			if (optarg[0] == '-') {
1229355b4669Sjacobs 				/* this check stop a possible infinite loop */
1230355b4669Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
1231355b4669Sjacobs 					optind--;
1232355b4669Sjacobs 				optarg = NULL;
1233355b4669Sjacobs 			} else if (strcmp(optarg, "all") == 0)
1234355b4669Sjacobs 				optarg = NULL;
1235355b4669Sjacobs 		}
1236355b4669Sjacobs 
1237355b4669Sjacobs 		switch (c) {
1238355b4669Sjacobs 		case 'a':
1239355b4669Sjacobs 			exit_code += printer_query(optarg, report_accepting,
1240355b4669Sjacobs 			    encryption, verbose, 0);
1241355b4669Sjacobs 			break;
1242355b4669Sjacobs 		case 'c':
1243355b4669Sjacobs 			exit_code += printer_query(optarg, report_class,
1244355b4669Sjacobs 			    encryption, verbose, 0);
1245355b4669Sjacobs 			break;
1246355b4669Sjacobs 		case 'p':
1247355b4669Sjacobs 			exit_code += printer_query(optarg, report_printer,
124854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 			    encryption, verbose, description);
1249355b4669Sjacobs 			break;
1250355b4669Sjacobs 		case 'd':
1251355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
1252355b4669Sjacobs 			break;
1253355b4669Sjacobs 		case 'r':
1254355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
1255355b4669Sjacobs 			break;
1256355b4669Sjacobs 		case 'u':
1257355b4669Sjacobs 			if (optarg != NULL)
1258355b4669Sjacobs 				users = strsplit(optarg, ", \n");
1259355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
1260355b4669Sjacobs 			    encryption, rank, verbose);
1261355b4669Sjacobs 			if (users != NULL) {
1262355b4669Sjacobs 				free(users);
1263355b4669Sjacobs 				users = NULL;
1264355b4669Sjacobs 			}
1265355b4669Sjacobs 			break;
1266355b4669Sjacobs 		case 'v':
1267355b4669Sjacobs 			exit_code += printer_query(optarg, report_device,
1268355b4669Sjacobs 			    encryption, verbose, 0);
1269355b4669Sjacobs 			break;
1270c2765d20SGowtham Thommandra 		case 'R':	/* set "rank" flag in first pass */
1271355b4669Sjacobs 		case 'o':
1272355b4669Sjacobs 			exit_code += job_query(optarg, report_job,
1273355b4669Sjacobs 			    encryption, rank, verbose);
1274355b4669Sjacobs 			break;
1275355b4669Sjacobs 		case 'f':
1276355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
1277355b4669Sjacobs 			    encryption, verbose);
1278355b4669Sjacobs 			break;
1279355b4669Sjacobs 		case 'S':
1280355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
1281355b4669Sjacobs 			    encryption, verbose);
1282355b4669Sjacobs 			break;
1283355b4669Sjacobs 		case 's':
1284355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
1285355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
1286355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
1287355b4669Sjacobs 			    encryption, verbose, 0);
1288355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
1289355b4669Sjacobs 			    encryption, verbose, 0);
1290355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
1291355b4669Sjacobs 			    encryption, verbose);
1292355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
1293355b4669Sjacobs 			    encryption, verbose);
1294355b4669Sjacobs 			break;
1295355b4669Sjacobs 		case 't':
1296355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
1297355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
1298355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
1299355b4669Sjacobs 			    encryption, verbose, 0);
1300355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
1301355b4669Sjacobs 			    encryption, verbose, 0);
1302355b4669Sjacobs 			exit_code += printer_query(NULL, report_accepting,
1303355b4669Sjacobs 			    encryption, verbose, 0);
1304355b4669Sjacobs 			exit_code += printer_query(NULL, report_printer,
1305355b4669Sjacobs 			    encryption, verbose, 0);
1306355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
1307355b4669Sjacobs 			    encryption, verbose);
1308355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
1309355b4669Sjacobs 			    encryption, verbose);
1310355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
1311355b4669Sjacobs 			    encryption, rank, verbose);
1312355b4669Sjacobs 			break;
1313355b4669Sjacobs 		case 'L':	/* local-only, ignored */
1314355b4669Sjacobs 		case 'l':	/* increased verbose level in first pass */
1315355b4669Sjacobs 		case 'D':	/* set "description" flag in first pass */
1316355b4669Sjacobs 		case 'E':	/* set encryption in the first pass */
1317355b4669Sjacobs 			break;
1318355b4669Sjacobs 		default:
1319355b4669Sjacobs 			usage(av[0]);
1320355b4669Sjacobs 		}
1321355b4669Sjacobs 	}
1322355b4669Sjacobs 	ac--;
1323355b4669Sjacobs 
1324355b4669Sjacobs 	if (ac == 1) {	/* report on my jobs */
1325355b4669Sjacobs 		struct passwd *pw = getpwuid(getuid());
1326355b4669Sjacobs 
1327355b4669Sjacobs 		if (pw != NULL)
1328355b4669Sjacobs 			users = strsplit(pw->pw_name, "");
1329355b4669Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
1330355b4669Sjacobs 		    rank, verbose);
1331355b4669Sjacobs 		if (users != NULL) {
1332355b4669Sjacobs 			free(users);
1333355b4669Sjacobs 			users = NULL;
1334355b4669Sjacobs 		}
1335355b4669Sjacobs 	} else {
1336355b4669Sjacobs 		for (c = optind; c < ac; c++)
1337355b4669Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
1338355b4669Sjacobs 			    rank, verbose);
1339355b4669Sjacobs 	}
1340355b4669Sjacobs 
1341355b4669Sjacobs 
1342355b4669Sjacobs 	if (exit_code != 0)
1343355b4669Sjacobs 		exit_code = 1;
1344355b4669Sjacobs 
1345355b4669Sjacobs 	return (exit_code);
1346355b4669Sjacobs }
1347