xref: /titanic_50/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 35a603ad9532c431e0923d22b1e5151c73aca44e)
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"
42355b4669Sjacobs 
43355b4669Sjacobs static void
44355b4669Sjacobs usage(char *program)
45355b4669Sjacobs {
46355b4669Sjacobs 	char *name;
47355b4669Sjacobs 
48355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
49355b4669Sjacobs 		name = program;
50355b4669Sjacobs 	else
51355b4669Sjacobs 		name++;
52355b4669Sjacobs 
53355b4669Sjacobs 	fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
54355b4669Sjacobs 	    "[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
55355b4669Sjacobs 	    "[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
56355b4669Sjacobs 	    "[-f [list] [-l]] [-u list]\n"),
57355b4669Sjacobs 	    name);
58355b4669Sjacobs 	exit(1);
59355b4669Sjacobs }
60355b4669Sjacobs 
61355b4669Sjacobs static char *
62355b4669Sjacobs nctime(time_t *t)
63355b4669Sjacobs {
64355b4669Sjacobs 	static char buf[64];
65355b4669Sjacobs 	struct tm *tm = localtime(t);
66355b4669Sjacobs 
67355b4669Sjacobs 	(void) strftime(buf, sizeof (buf), "%c", tm);
68355b4669Sjacobs 
69355b4669Sjacobs 	return (buf);
70355b4669Sjacobs }
71355b4669Sjacobs 
72355b4669Sjacobs static char *
73355b4669Sjacobs printer_name(papi_printer_t printer)
74355b4669Sjacobs {
75355b4669Sjacobs 	papi_attribute_t **attributes = papiPrinterGetAttributeList(printer);
76355b4669Sjacobs 	char *result = NULL;
77355b4669Sjacobs 
78355b4669Sjacobs 	if (attributes != NULL)
79355b4669Sjacobs 		papiAttributeListGetString(attributes, NULL,
80355b4669Sjacobs 		    "printer-name", &result);
81355b4669Sjacobs 
82355b4669Sjacobs 	return (result);
83355b4669Sjacobs }
84355b4669Sjacobs 
85355b4669Sjacobs static int
86355b4669Sjacobs lpstat_default_printer(papi_encryption_t encryption)
87355b4669Sjacobs {
88355b4669Sjacobs 	papi_status_t status;
89355b4669Sjacobs 	papi_service_t svc = NULL;
90355b4669Sjacobs 	papi_printer_t p = NULL;
91355b4669Sjacobs 	char *name = NULL;
92355b4669Sjacobs 
93355b4669Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback,
94355b4669Sjacobs 	    encryption, NULL);
95355b4669Sjacobs 	if (status == PAPI_OK) {
96355b4669Sjacobs 		char *req[] = { "printer-name", NULL };
97355b4669Sjacobs 
98355b4669Sjacobs 		status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p);
99355b4669Sjacobs 		if (p != NULL)
100355b4669Sjacobs 			name = printer_name(p);
101355b4669Sjacobs 	}
102355b4669Sjacobs 	if (name != NULL)
103355b4669Sjacobs 		printf(gettext("system default printer: %s\n"), name);
104355b4669Sjacobs 	else
105355b4669Sjacobs 		printf(gettext("no system default destination\n"));
106355b4669Sjacobs 	papiPrinterFree(p);
107355b4669Sjacobs 	papiServiceDestroy(svc);
108355b4669Sjacobs 
109355b4669Sjacobs 	return (0);
110355b4669Sjacobs }
111355b4669Sjacobs 
112355b4669Sjacobs static int
113355b4669Sjacobs lpstat_service_status(papi_encryption_t encryption)
114355b4669Sjacobs {
115355b4669Sjacobs 	int result = 0;
116355b4669Sjacobs 	papi_status_t status;
117355b4669Sjacobs 	papi_service_t svc = NULL;
118355b4669Sjacobs 	char *name = NULL;
119355b4669Sjacobs 
120355b4669Sjacobs 	if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
121355b4669Sjacobs 	    ((name = getenv("IPP_SERVER")) == NULL) &&
122355b4669Sjacobs 	    ((name = getenv("CUPS_SERVER")) == NULL))
123355b4669Sjacobs 		name = DEFAULT_SERVICE_URI;
124355b4669Sjacobs 
125355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
126355b4669Sjacobs 	    encryption, NULL);
127355b4669Sjacobs 	if (status != PAPI_OK) {
128355b4669Sjacobs 		printf(gettext("scheduler is not running\n"));
129355b4669Sjacobs 		result = -1;
130355b4669Sjacobs 	} else
131355b4669Sjacobs 		printf(gettext("scheduler is running\n"));
132355b4669Sjacobs 	papiServiceDestroy(svc);
133355b4669Sjacobs 
134355b4669Sjacobs 	return (result);
135355b4669Sjacobs }
136355b4669Sjacobs 
137355b4669Sjacobs static char *
138355b4669Sjacobs get_device_uri(papi_service_t svc, char *name)
139355b4669Sjacobs {
140355b4669Sjacobs 	papi_status_t status;
141355b4669Sjacobs 	papi_printer_t p = NULL;
142355b4669Sjacobs 	char *keys[] = { "device-uri", NULL };
143355b4669Sjacobs 	char *result = NULL;
144355b4669Sjacobs 
145355b4669Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
146355b4669Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
147355b4669Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
148355b4669Sjacobs 
149355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
150355b4669Sjacobs 		    "device-uri", &result);
151355b4669Sjacobs 		if (result != NULL)
152355b4669Sjacobs 			result = strdup(result);
153355b4669Sjacobs 
154355b4669Sjacobs 		papiPrinterFree(p);
155355b4669Sjacobs 	}
156355b4669Sjacobs 
157355b4669Sjacobs 	return (result);
158355b4669Sjacobs }
159355b4669Sjacobs 
160f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India static void
161f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India print_description(papi_attribute_t **list, char *printer_name)
162f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India {
163f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	char *str = "";
164f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
165f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	(void) papiAttributeListGetString(list, NULL,
166f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	    "printer-info", &str);
167f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
168f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	/*
169f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	 * If no printer-info is read then
170f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	 * by default the printer-info is <printer-name>@<server>
171f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	 */
172f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	if (str[0] == '\0') {
173f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		char *uri = NULL;
174f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		uri_t *u = NULL;
175f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
176f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		(void) papiAttributeListGetString(list, NULL,
177f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		    "printer-uri-supported", &uri);
178f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
179f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
180f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			char *nodename = localhostname();
181f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
182f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			if ((u->host == NULL) ||
183f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, "localhost") == 0) ||
184f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, nodename) == 0))
185f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 				printf(gettext("\tDescription:\n"));
186f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			else
187f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 				printf(gettext("\tDescription: %s@%s\n"),
188f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 				    printer_name, u->host);
189f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
190f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			uri_free(u);
191f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		} else
192f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 			printf(gettext("\tDescription:\n"));
193f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	} else
194f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		printf(gettext("\tDescription: %s\n"), str);
195f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India }
196f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 
197355b4669Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
198355b4669Sjacobs 					NULL };
199355b4669Sjacobs /* ARGSUSED2 */
200355b4669Sjacobs static int
201355b4669Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
202355b4669Sjacobs 		int verbose, int description)
203355b4669Sjacobs {
204355b4669Sjacobs 	papi_status_t status;
205355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
206355b4669Sjacobs 	char *uri = NULL;
207355b4669Sjacobs 	char *device = NULL;
208355b4669Sjacobs 	uri_t *u = NULL;
209355b4669Sjacobs 
210355b4669Sjacobs 	if (name == NULL) {
211355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
212355b4669Sjacobs 		    "printer-name", &name);
213355b4669Sjacobs 		if (status != PAPI_OK)
214355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
215355b4669Sjacobs 			    "printer-uri-supported", &name);
216355b4669Sjacobs 	}
217355b4669Sjacobs 
218355b4669Sjacobs 	if (name == NULL)
219355b4669Sjacobs 		return (-1);
220355b4669Sjacobs 
221355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
222355b4669Sjacobs 	    "printer-uri-supported", &uri);
223355b4669Sjacobs 
224355b4669Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
225355b4669Sjacobs 		char *nodename = localhostname();
226355b4669Sjacobs 
227355b4669Sjacobs 		if ((u->host == NULL) ||
228355b4669Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
229355b4669Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
230355b4669Sjacobs 			device = get_device_uri(svc, name);
231355b4669Sjacobs 
232355b4669Sjacobs 		if (device != NULL) {
233355b4669Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
234355b4669Sjacobs 			return (0);
235355b4669Sjacobs 		} else if (uri != NULL) {
236355b4669Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
237355b4669Sjacobs 			    u->host, uri);
238355b4669Sjacobs 			return (0);
239355b4669Sjacobs 		}
240355b4669Sjacobs 
241355b4669Sjacobs 		uri_free(u);
242355b4669Sjacobs 	}
243355b4669Sjacobs 
244355b4669Sjacobs 	return (0);
245355b4669Sjacobs }
246355b4669Sjacobs 
247355b4669Sjacobs static char *report_accepting_keys[] = { "printer-name",
248355b4669Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
249355b4669Sjacobs 			"printer-up-time", "printer-state-time",
250355b4669Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
251355b4669Sjacobs /* ARGSUSED2 */
252355b4669Sjacobs static int
253355b4669Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
254355b4669Sjacobs 		int verbose, int description)
255355b4669Sjacobs {
256355b4669Sjacobs 	papi_status_t status;
257355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
258355b4669Sjacobs 	time_t curr;
259355b4669Sjacobs 	char boolean = PAPI_FALSE;
260355b4669Sjacobs 
261355b4669Sjacobs 	if (name == NULL) {
262355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
263355b4669Sjacobs 		    "printer-name", &name);
264355b4669Sjacobs 		if (status != PAPI_OK)
265355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
266355b4669Sjacobs 			    "printer-uri-supported", &name);
267355b4669Sjacobs 	}
268355b4669Sjacobs 	if (name == NULL)
269355b4669Sjacobs 		return (-1);
270355b4669Sjacobs 
271355b4669Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
272355b4669Sjacobs 	    "printer-is-accepting-jobs", &boolean);
273355b4669Sjacobs 	(void) time(&curr);
274355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
275355b4669Sjacobs 	    "printer-up-time", &curr);
276355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
277355b4669Sjacobs 	    "printer-state-time", &curr);
278355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
279355b4669Sjacobs 	    "lpsched-reject-date", &curr);
280355b4669Sjacobs 
281355b4669Sjacobs 	if (boolean == PAPI_TRUE) {
282355b4669Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
283355b4669Sjacobs 		    name, nctime(&curr));
284355b4669Sjacobs 	} else {
285355b4669Sjacobs 		char *reason = "unknown reason";
286355b4669Sjacobs 
287355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
288355b4669Sjacobs 		    "lpsched-reject-reason", &reason);
289355b4669Sjacobs 
290355b4669Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
291355b4669Sjacobs 		    name, nctime(&curr), reason);
292355b4669Sjacobs 	}
293355b4669Sjacobs 
294355b4669Sjacobs 	return (0);
295355b4669Sjacobs }
296355b4669Sjacobs 
297355b4669Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
298355b4669Sjacobs 					"member-names", NULL };
299355b4669Sjacobs /* ARGSUSED2 */
300355b4669Sjacobs static int
301355b4669Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
302355b4669Sjacobs 		int verbose, int description)
303355b4669Sjacobs {
304355b4669Sjacobs 	papi_status_t status;
305355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
306355b4669Sjacobs 	char *member = NULL;
307355b4669Sjacobs 	void *iter = NULL;
308355b4669Sjacobs 
309355b4669Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
310355b4669Sjacobs 	    "member-names", &member);
311355b4669Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
312355b4669Sjacobs 		return (0);
313355b4669Sjacobs 
314355b4669Sjacobs 	if (name == NULL) {
315355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
316355b4669Sjacobs 		    "printer-name", &name);
317355b4669Sjacobs 		if (status != PAPI_OK)
318355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
319355b4669Sjacobs 			    "printer-uri-supported", &name);
320355b4669Sjacobs 	}
321355b4669Sjacobs 	if (name == NULL)
322355b4669Sjacobs 		return (-1);
323355b4669Sjacobs 
324355b4669Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
325355b4669Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
326355b4669Sjacobs 	    == PAPI_OK)
327355b4669Sjacobs 		printf("\t%s\n", member);
328355b4669Sjacobs 
329355b4669Sjacobs 	return (0);
330355b4669Sjacobs }
331355b4669Sjacobs 
332355b4669Sjacobs static char *report_printer_keys[] = { "printer-name",
333355b4669Sjacobs 			"printer-uri-supported", "printer-state",
334355b4669Sjacobs 			"printer-up-time", "printer-state-time",
335355b4669Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
336355b4669Sjacobs 			"lpsched-disable-reason", NULL };
337355b4669Sjacobs /* ARGSUSED2 */
338355b4669Sjacobs static int
339355b4669Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
340355b4669Sjacobs 		int verbose, int description)
341355b4669Sjacobs {
342355b4669Sjacobs 	papi_status_t status;
343355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
344355b4669Sjacobs 	time_t curr;
345355b4669Sjacobs 	int32_t pstat = 0;
346355b4669Sjacobs 	char *member = NULL;
34754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 	papi_job_t *j = NULL;
348355b4669Sjacobs 
349355b4669Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
350355b4669Sjacobs 	    "member-names", &member);
351355b4669Sjacobs 	if (status == PAPI_OK)	/* it's a class */
352355b4669Sjacobs 		return (0);
353355b4669Sjacobs 
354355b4669Sjacobs 	if (name == NULL) {
355355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
356355b4669Sjacobs 		    "printer-name", &name);
357355b4669Sjacobs 		if (status != PAPI_OK)
358355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
359355b4669Sjacobs 			    "printer-uri-supported", &name);
360355b4669Sjacobs 	}
361355b4669Sjacobs 	if (name == NULL)
362355b4669Sjacobs 		return (-1);
363355b4669Sjacobs 
364355b4669Sjacobs 	printf(gettext("printer %s "), name);
365355b4669Sjacobs 
366355b4669Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
367355b4669Sjacobs 	    "printer-state", &pstat);
368355b4669Sjacobs 
369355b4669Sjacobs 	switch (pstat) {
370355b4669Sjacobs 	case 0x03:	/* idle */
371355b4669Sjacobs 		printf(gettext("idle. enabled"));
372355b4669Sjacobs 		break;
37354d5ddccSsonam gupta - Sun Microsystems - Bangalore India 	case 0x04: /* processing */
37454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 		status = papiPrinterListJobs(svc, name, NULL,
37554d5ddccSsonam gupta - Sun Microsystems - Bangalore India 		    0, 0, &j);
37654d5ddccSsonam gupta - Sun Microsystems - Bangalore India 
37754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 		if (status == PAPI_OK) {
37854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 			if (j != NULL) {
37954d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				int i = 0;
380355b4669Sjacobs 				int32_t jobid = 0;
38154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				int32_t jstate = 0;
38254d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				int flag = 0;
383355b4669Sjacobs 
38454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				for (i = 0; j[i] != NULL; ++i) {
385356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					papi_attribute_t **attr =
386356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					    papiJobGetAttributeList(j[i]);
38754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 
388356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					papiAttributeListGetInteger(attr,
38954d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    NULL, "job-state", &jstate);
390356a8421Ssonam gupta - Sun Microsystems - Bangalore India 					papiAttributeListGetInteger(attr,
39154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    NULL, "job-id", &jobid);
39254d5ddccSsonam gupta - Sun Microsystems - Bangalore India 
39354d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					/*
39454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					 * If the job-state is not
39554d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					 * "held", "cancelled" or
39654d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					 * "completed" then only print.
39754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					 */
39854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					if ((jstate != 0x04) &&
39954d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    (jstate != 0x07) &&
40054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    (jstate != 0x09)) {
40154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 						if (flag == 0)
40254d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							printf(gettext
40354d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							    ("now printing"\
40454d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							    " %s-%d. enabled"),
40554d5ddccSsonam gupta - Sun Microsystems - Bangalore India 							    name, jobid);
40654d5ddccSsonam gupta - Sun Microsystems - Bangalore India 						flag = 1;
40754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					}
40854d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				}
409355b4669Sjacobs 				papiJobListFree(j);
41054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 			}
411355b4669Sjacobs 		}
412355b4669Sjacobs 		break;
413355b4669Sjacobs 	case 0x05:	/* stopped */
414355b4669Sjacobs 		printf(gettext("disabled"));
415355b4669Sjacobs 		break;
416355b4669Sjacobs 	default:
417355b4669Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
418355b4669Sjacobs 		break;
419355b4669Sjacobs 	}
420355b4669Sjacobs 
421355b4669Sjacobs 	(void) time(&curr);
422355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
423355b4669Sjacobs 	    "printer-up-time", &curr);
424355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
425355b4669Sjacobs 	    "printer-state-time", &curr);
426355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
427355b4669Sjacobs 	    "lpsched-disable-date", &curr);
428355b4669Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
429355b4669Sjacobs 
430355b4669Sjacobs 	if (pstat == 0x05) {
431355b4669Sjacobs 		char *reason = "unknown reason";
432355b4669Sjacobs 
433355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
434355b4669Sjacobs 		    "printer-state-reasons", &reason);
435355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
436355b4669Sjacobs 		    "lpsched-disable-reason", &reason);
437355b4669Sjacobs 		printf(gettext("\t%s\n"), reason);
438355b4669Sjacobs 	}
439355b4669Sjacobs 
440355b4669Sjacobs 	if (verbose == 1) {
441355b4669Sjacobs 		void *iter;
442355b4669Sjacobs 		char *str;
443355b4669Sjacobs 
444355b4669Sjacobs 		str = "";
445355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
446355b4669Sjacobs 		    "form-ready", &str);
447355b4669Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
448355b4669Sjacobs 
449355b4669Sjacobs 		str = "";
450355b4669Sjacobs 		iter = NULL;
451355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
452355b4669Sjacobs 		    "document-format-supported", &str);
453355b4669Sjacobs 		printf(gettext("\tContent types: %s"), str);
454355b4669Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
455355b4669Sjacobs 		    == PAPI_OK)
456355b4669Sjacobs 			printf(", %s", str);
457355b4669Sjacobs 		printf("\n");
458355b4669Sjacobs 
459f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		/* Display the printer description */
460f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		print_description(attrs, name);
461355b4669Sjacobs 
462355b4669Sjacobs 		str = "";
463853389e3Swendyp 		iter = NULL;
464853389e3Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
465853389e3Swendyp 		    "lpsched-printer-type", &str);
466853389e3Swendyp 		printf(gettext("\tPrinter types: %s"), str);
467853389e3Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
468853389e3Swendyp 		    == PAPI_OK)
469853389e3Swendyp 			printf(", %s", str);
470853389e3Swendyp 		printf("\n");
471853389e3Swendyp 
472853389e3Swendyp 		str = "";
473355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
474355b4669Sjacobs 		    "lpsched-dial-info", &str);
475355b4669Sjacobs 		printf(gettext("\tConnection: %s\n"),
4765eb9a62cSJonathan Cowper-Andrewes 		    ((str[0] == '\0') ? gettext("direct") : str));
477355b4669Sjacobs 
478355b4669Sjacobs 		str = "";
479355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
480355b4669Sjacobs 		    "lpsched-interface-script", &str);
481355b4669Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
482355b4669Sjacobs 
483355b4669Sjacobs 		str = NULL;
484355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
485355b4669Sjacobs 		    "ppd-file-uri", &str);
486355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
487355b4669Sjacobs 		    "lpsched-ppd-source-path", &str);
488355b4669Sjacobs 		if (str != NULL)
489355b4669Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
490355b4669Sjacobs 
491355b4669Sjacobs 		str = NULL;
492355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
493355b4669Sjacobs 		    "lpsched-fault-alert-command", &str);
494355b4669Sjacobs 		if (str != NULL)
495355b4669Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
496355b4669Sjacobs 
497355b4669Sjacobs 		str = "";
498355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
499355b4669Sjacobs 		    "lpsched-fault-recovery", &str);
500355b4669Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
501355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("continue") : str));
502355b4669Sjacobs 
503355b4669Sjacobs 		str = "(all)";
504355b4669Sjacobs 		iter = NULL;
505355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
506355b4669Sjacobs 		    "requesting-user-name-allowed", &str);
507355b4669Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
508355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("(none)") : str));
509355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
510355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
511355b4669Sjacobs 			    &str) == PAPI_OK)
512355b4669Sjacobs 				printf("\t\t%s\n", str);
513355b4669Sjacobs 
514355b4669Sjacobs 		str = NULL;
515355b4669Sjacobs 		iter = NULL;
516355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
517355b4669Sjacobs 		    "requesting-user-name-denied", &str);
518355b4669Sjacobs 		if (str != NULL) {
519355b4669Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
520355b4669Sjacobs 			    ((str[0] == '\0') ? gettext("(none)") : str));
521355b4669Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
522355b4669Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
523355b4669Sjacobs 				    NULL, &str) == PAPI_OK)
524355b4669Sjacobs 					printf("\t\t%s\n", str);
525355b4669Sjacobs 		}
526355b4669Sjacobs 
527f06271beSsonam gupta - Sun Microsystems - Bangalore India 		str = "none";
528355b4669Sjacobs 		iter = NULL;
529355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
530355b4669Sjacobs 		    "form-supported", &str);
531f06271beSsonam gupta - Sun Microsystems - Bangalore India 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
532f06271beSsonam gupta - Sun Microsystems - Bangalore India 		    ((str[0] == '\0') ? gettext("none") : str));
533355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
534355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
535355b4669Sjacobs 			    &str) == PAPI_OK)
536f06271beSsonam gupta - Sun Microsystems - Bangalore India 				printf("\t\t(%s)\n", str);
537355b4669Sjacobs 
538355b4669Sjacobs 		str = "";
539355b4669Sjacobs 		iter = NULL;
540355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
541355b4669Sjacobs 		    "media-supported", &str);
542355b4669Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
543355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("(none)") : str));
544355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
545355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
546355b4669Sjacobs 			    &str) == PAPI_OK)
547355b4669Sjacobs 				printf("\t\t%s\n", str);
548355b4669Sjacobs 
549355b4669Sjacobs 		str = "";
550355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
551355b4669Sjacobs 		    "job-sheets-supported", &str);
552853389e3Swendyp 		if ((strcasecmp(str, "none")) == 0)
553853389e3Swendyp 			str = gettext("page never printed");
554853389e3Swendyp 		else if (strcasecmp(str, "optional") == 0)
555853389e3Swendyp 			str = gettext("not required");
556853389e3Swendyp 		else
557853389e3Swendyp 			str = gettext("required");
558853389e3Swendyp 
559853389e3Swendyp 		printf(gettext("\tBanner %s\n"), str);
560853389e3Swendyp 
561355b4669Sjacobs 
562355b4669Sjacobs 		str = "";
563355b4669Sjacobs 		iter = NULL;
564355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
565355b4669Sjacobs 		    "lpsched-print-wheels", &str);
566355b4669Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
567355b4669Sjacobs 		    ((str[0] == '\0') ? gettext("(none)") : str));
568355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
569355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
570355b4669Sjacobs 			    &str) == PAPI_OK)
571355b4669Sjacobs 				printf("\t\t%s\n", str);
572355b4669Sjacobs 
573355b4669Sjacobs 		printf(gettext("\tDefault pitch:\n"));
574355b4669Sjacobs 		printf(gettext("\tDefault page size:\n"));
575355b4669Sjacobs 		printf(gettext("\tDefault port setting:\n"));
576355b4669Sjacobs 
577355b4669Sjacobs 		str = "";
578355b4669Sjacobs 		iter = NULL;
579355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
580355b4669Sjacobs 		    "lpsched-options", &str);
581355b4669Sjacobs 		if (str != NULL) {
582355b4669Sjacobs 			printf(gettext("\tOptions: %s"), str);
583355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
584355b4669Sjacobs 			    &str) == PAPI_OK)
585355b4669Sjacobs 				printf(", %s", str);
586355b4669Sjacobs 			printf("\n");
587355b4669Sjacobs 		}
588355b4669Sjacobs 
589f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	} else if (description == 1)
590f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		/* Display printer description */
591f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 		print_description(attrs, name);
592f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India 	else if (verbose > 1)
593355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
594355b4669Sjacobs 
595355b4669Sjacobs 	if (verbose > 0)
596355b4669Sjacobs 		printf("\n");
597355b4669Sjacobs 
598355b4669Sjacobs 	return (0);
599355b4669Sjacobs }
600355b4669Sjacobs 
601355b4669Sjacobs static int
602355b4669Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
603355b4669Sjacobs 					int, int), papi_encryption_t encryption,
604355b4669Sjacobs 		int verbose, int description)
605355b4669Sjacobs {
606355b4669Sjacobs 	int result = 0;
607355b4669Sjacobs 	papi_status_t status;
608355b4669Sjacobs 	papi_service_t svc = NULL;
609355b4669Sjacobs 
610355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
611355b4669Sjacobs 	    encryption, NULL);
612355b4669Sjacobs 	if (status != PAPI_OK) {
613022ba35cSjacobs 		if (status == PAPI_NOT_FOUND)
614022ba35cSjacobs 			fprintf(stderr, gettext("%s: unknown printer\n"),
615022ba35cSjacobs 			    name ? name : "(NULL)");
616022ba35cSjacobs 		else
617355b4669Sjacobs 			fprintf(stderr, gettext(
618355b4669Sjacobs 			    "Failed to contact service for %s: %s\n"),
619355b4669Sjacobs 			    name ? name : "(NULL)",
620355b4669Sjacobs 			    verbose_papi_message(svc, status));
621355b4669Sjacobs 		papiServiceDestroy(svc);
622355b4669Sjacobs 		return (-1);
623355b4669Sjacobs 	}
624355b4669Sjacobs 
625355b4669Sjacobs 	if (name == NULL) { /* all */
626355b4669Sjacobs 		char **interest = interest_list(svc);
627355b4669Sjacobs 
628355b4669Sjacobs 		if (interest != NULL) {
629355b4669Sjacobs 			int i;
630355b4669Sjacobs 
631355b4669Sjacobs 			for (i = 0; interest[i] != NULL; i++)
632355b4669Sjacobs 				result += printer_query(interest[i], report,
633355b4669Sjacobs 				    encryption, verbose,
634355b4669Sjacobs 				    description);
635355b4669Sjacobs 		}
636355b4669Sjacobs 	} else {
637355b4669Sjacobs 		papi_printer_t printer = NULL;
638355b4669Sjacobs 		char **keys = NULL;
639355b4669Sjacobs 
640355b4669Sjacobs 		/*
641355b4669Sjacobs 		 * Limit the query to only required data to reduce the need
642355b4669Sjacobs 		 * to go remote for information.
643355b4669Sjacobs 		 */
644355b4669Sjacobs 		if (report == report_device)
645355b4669Sjacobs 			keys = report_device_keys;
646355b4669Sjacobs 		else if (report == report_class)
647355b4669Sjacobs 			keys = report_class_keys;
648355b4669Sjacobs 		else if (report == report_accepting)
649355b4669Sjacobs 			keys = report_accepting_keys;
650355b4669Sjacobs 		else if ((report == report_printer) && (verbose == 0))
651355b4669Sjacobs 			keys = report_printer_keys;
652355b4669Sjacobs 
653355b4669Sjacobs 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
654355b4669Sjacobs 		if (status != PAPI_OK) {
655355b4669Sjacobs 			fprintf(stderr, gettext(
656355b4669Sjacobs 			    "Failed to get printer info for %s: %s\n"),
657355b4669Sjacobs 			    name, verbose_papi_message(svc, status));
658355b4669Sjacobs 			papiServiceDestroy(svc);
659355b4669Sjacobs 			return (-1);
660355b4669Sjacobs 		}
661355b4669Sjacobs 
662355b4669Sjacobs 		if (printer != NULL)
663355b4669Sjacobs 			result = report(svc, name, printer, verbose,
664355b4669Sjacobs 			    description);
665355b4669Sjacobs 
666355b4669Sjacobs 		papiPrinterFree(printer);
667355b4669Sjacobs 	}
668355b4669Sjacobs 
669355b4669Sjacobs 	papiServiceDestroy(svc);
670355b4669Sjacobs 
671355b4669Sjacobs 	return (result);
672355b4669Sjacobs }
673355b4669Sjacobs 
674355b4669Sjacobs static int
675355b4669Sjacobs match_user(char *user, char **list)
676355b4669Sjacobs {
677355b4669Sjacobs 	int i;
678355b4669Sjacobs 
679355b4669Sjacobs 	for (i = 0; list[i] != NULL; i++) {
680355b4669Sjacobs 		if (strcmp(user, list[i]) == 0)
681355b4669Sjacobs 			return (0);
682355b4669Sjacobs 	}
683355b4669Sjacobs 
684355b4669Sjacobs 	return (-1);
685355b4669Sjacobs }
686355b4669Sjacobs 
687355b4669Sjacobs static char **users = NULL;
688355b4669Sjacobs 
689355b4669Sjacobs static int
690*35a603adSsonam gupta - Sun Microsystems - Bangalore India report_job(char *printer, papi_job_t job, int show_rank, int verbose)
691355b4669Sjacobs {
692355b4669Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
693355b4669Sjacobs 	time_t clock = 0;
694355b4669Sjacobs 	char date[24];
695355b4669Sjacobs 	char request[26];
696355b4669Sjacobs 	char *user = "unknown";
697b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	char *host = NULL;
698355b4669Sjacobs 	int32_t size = 0;
699355b4669Sjacobs 	int32_t jstate = 0;
700b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	char User[50];
701355b4669Sjacobs 
702355b4669Sjacobs 	char *destination = "unknown";
703355b4669Sjacobs 	int32_t id = -1;
7043d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	static int check = 0;
7053d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	static char *uri = NULL;
706355b4669Sjacobs 
707355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
708355b4669Sjacobs 	    "job-originating-user-name", &user);
709355b4669Sjacobs 
710355b4669Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
711355b4669Sjacobs 		return (0);
712355b4669Sjacobs 
713b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	(void) papiAttributeListGetString(attrs, NULL,
714b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 	    "job-originating-host-name", &host);
715b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 
7163d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	if (check == 0) {
7173d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		/*
7183d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * Read the attribute "job-printer-uri"
7193d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * just once
7203d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 */
7213d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		(void) papiAttributeListGetString(attrs, NULL,
7223d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-printer-uri", &uri);
7233d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		check = 1;
7243d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	}
7253d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
7263d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	if (host) {
7273d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		/* Check if it is local printer or remote printer */
7283d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		uri_t *u = NULL;
7293d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
7303d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
7313d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			char *nodename = localhostname();
7323d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
7333d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			if ((u->host == NULL) ||
7343d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, "localhost") == 0) ||
7353d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			    (strcasecmp(u->host, nodename) == 0)) {
7363d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
7373d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				if (strcasecmp(host, nodename) == 0) {
7383d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					/*
7393d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 * Request submitted locally
7403d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 * for the local queue.
7413d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 * Hostname will not be displayed
7423d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					 */
7433d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					snprintf(User, sizeof (User), "%s",
7443d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    user);
7453d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				}
7463d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				else
7473d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					snprintf(User, sizeof (User), "%s@%s",
7483d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    user, host);
7493d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			} else if (uri != NULL) {
7503d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				/*
7513d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 * It's a remote printer.
7523d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 * In case of remote printers hostname is
7533d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 * always displayed.
7543d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				 */
7553d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				snprintf(User, sizeof (User), "%s@%s",
7563d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				    user, host);
7573d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			}
7583d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			uri_free(u);
7593d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		} else {
7603d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			/*
7613d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 * If attribute "job-printer-uri"
7623d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 * cannot be read
7633d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 * by default append the hostname
7643d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			 */
765b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 			snprintf(User, sizeof (User), "%s@%s", user, host);
7663d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		}
7673d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	} else {
7683d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		/*
7693d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * When print server is s10u4 and ipp service is used
7703d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * "job-originating-hostname" attribute is not set
7713d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 * So get the host information from the uri
7723d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		 */
7733d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		uri_t *u = NULL;
7743d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
7753d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			if ((u != NULL) && (u->host != NULL))
7763d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				snprintf(User, sizeof (User), "%s@%s",
7773d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				    user, u->host);
778b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 			else
779b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 				snprintf(User, sizeof (User), "%s", user);
780b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 
7813d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			uri_free(u);
7823d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		} else
7833d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			snprintf(User, sizeof (User), "%s", user);
7843d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	}
785355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
786355b4669Sjacobs 	size *= 1024;	/* for the approximate byte size */
787355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
788355b4669Sjacobs 
789355b4669Sjacobs 	(void) time(&clock);
790355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
791355b4669Sjacobs 	    "time-at-creation", (int32_t *)&clock);
792355b4669Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
793355b4669Sjacobs 
794355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
795355b4669Sjacobs 	    "job-printer-uri", &destination);
796355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
797355b4669Sjacobs 	    "printer-name", &destination);
798355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
799355b4669Sjacobs 	    "job-id", &id);
800*35a603adSsonam gupta - Sun Microsystems - Bangalore India 
801*35a603adSsonam gupta - Sun Microsystems - Bangalore India 	snprintf(request, sizeof (request), "%s-%d", printer, id);
802355b4669Sjacobs 
803355b4669Sjacobs 	if (show_rank != 0) {
804355b4669Sjacobs 		int32_t rank = -1;
805355b4669Sjacobs 
806355b4669Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
807355b4669Sjacobs 		    "number-of-intervening-jobs", &rank);
808355b4669Sjacobs 		rank++;
809355b4669Sjacobs 
810355b4669Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
811b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 		    rank, request, User, size, date);
812355b4669Sjacobs 	} else
813b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
814355b4669Sjacobs 
815355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
816355b4669Sjacobs 	    "job-state", &jstate);
817355b4669Sjacobs 	if (jstate == 0x04)
818355b4669Sjacobs 		printf(gettext(", being held"));
819355b4669Sjacobs 	else if (jstate == 0x07)
820355b4669Sjacobs 		printf(gettext(", cancelled"));
821355b4669Sjacobs 	else if (jstate == 0x09)
822355b4669Sjacobs 		printf(gettext(", complete"));
823355b4669Sjacobs 
824355b4669Sjacobs 	if (verbose == 1) {
8250a44ef6dSjacobs 		char *form = NULL;
8260a44ef6dSjacobs 
827355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
828355b4669Sjacobs 		    "output-device-assigned", &destination);
829355b4669Sjacobs 		printf("\n\t assigned %s", destination);
8300a44ef6dSjacobs 
8310a44ef6dSjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
8320a44ef6dSjacobs 		if (form != NULL)
8330a44ef6dSjacobs 			printf(", form %s", form);
834355b4669Sjacobs 	} else if (verbose > 1) {
835355b4669Sjacobs 		printf("\n");
836355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
837355b4669Sjacobs 	}
838355b4669Sjacobs 
839355b4669Sjacobs 	printf("\n");
840355b4669Sjacobs 
841355b4669Sjacobs 	return (0);
842355b4669Sjacobs }
843355b4669Sjacobs 
844355b4669Sjacobs static int
845*35a603adSsonam gupta - Sun Microsystems - Bangalore India job_query(char *request, int (*report)(char *, papi_job_t, int, int),
846355b4669Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
847355b4669Sjacobs {
848355b4669Sjacobs 	int result = 0;
849355b4669Sjacobs 	papi_status_t status;
850355b4669Sjacobs 	papi_service_t svc = NULL;
8514749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	char *printer = request;
852355b4669Sjacobs 	int32_t id = -1;
8534749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	int flag1 = 0;
8544749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	int flag = 1;
8554749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	int print_flag = 0;
856355b4669Sjacobs 
8574749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	do {
8584749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		status = papiServiceCreate(&svc, printer, NULL, NULL,
8594749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		    cli_auth_callback, encryption, NULL);
860355b4669Sjacobs 
8614749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		if ((status == PAPI_OK) && (printer != NULL))
8624749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			print_flag = 1;
8634749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
8644749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		/* <name>-# printer name does not exist */
8654749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		if (status != PAPI_OK) {
8664749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			/*
8674749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			 * Check if <name>-# is a request-id
8684749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			 * Once this check is done flag1 is set
8694749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			 */
8704749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			if (flag1 == 1)
8714749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				break;
8724749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
8734749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			get_printer_id(printer, &printer, &id);
8744749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
8754749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			status = papiServiceCreate(&svc, printer, NULL, NULL,
8764749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			    cli_auth_callback, encryption, NULL);
8774749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
878355b4669Sjacobs 			if (status != PAPI_OK) {
879355b4669Sjacobs 				fprintf(stderr, gettext(
880355b4669Sjacobs 				    "Failed to contact service for %s: %s\n"),
881355b4669Sjacobs 				    (printer ? printer : "all"),
882355b4669Sjacobs 				    verbose_papi_message(svc, status));
883355b4669Sjacobs 				return (-1);
884355b4669Sjacobs 			}
8854749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		}
886355b4669Sjacobs 
887355b4669Sjacobs 		if (printer == NULL) { /* all */
888355b4669Sjacobs 			char **interest = interest_list(svc);
889355b4669Sjacobs 
890355b4669Sjacobs 			if (interest != NULL) {
891355b4669Sjacobs 				int i;
892355b4669Sjacobs 
893355b4669Sjacobs 				for (i = 0; interest[i] != NULL; i++)
894355b4669Sjacobs 					result += job_query(interest[i], report,
895355b4669Sjacobs 					    encryption, show_rank, verbose);
896355b4669Sjacobs 			}
897355b4669Sjacobs 		} else if (id == -1) { /* a printer */
898355b4669Sjacobs 			papi_job_t *jobs = NULL;
899355b4669Sjacobs 
9004749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			status = papiPrinterListJobs(svc, printer, NULL,
9014749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			    0, 0, &jobs);
902355b4669Sjacobs 			if (status != PAPI_OK) {
903355b4669Sjacobs 				fprintf(stderr, gettext(
904355b4669Sjacobs 				    "Failed to get job list: %s\n"),
905355b4669Sjacobs 				    verbose_papi_message(svc, status));
906355b4669Sjacobs 				papiServiceDestroy(svc);
907355b4669Sjacobs 				return (-1);
908355b4669Sjacobs 			}
909355b4669Sjacobs 
910355b4669Sjacobs 			if (jobs != NULL) {
911355b4669Sjacobs 				int i;
912355b4669Sjacobs 
913355b4669Sjacobs 				for (i = 0; jobs[i] != NULL; i++)
914*35a603adSsonam gupta - Sun Microsystems - Bangalore India 					result += report(printer,
915*35a603adSsonam gupta - Sun Microsystems - Bangalore India 					    jobs[i], show_rank,
916*35a603adSsonam gupta - Sun Microsystems - Bangalore India 					    verbose);
917355b4669Sjacobs 			}
918355b4669Sjacobs 
919355b4669Sjacobs 			papiJobListFree(jobs);
920355b4669Sjacobs 		} else {	/* a job */
921355b4669Sjacobs 			papi_job_t job = NULL;
922355b4669Sjacobs 
9234749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			/* Once a job has been found stop processing */
9244749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			flag = 0;
9254749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
926355b4669Sjacobs 			status = papiJobQuery(svc, printer, id, NULL, &job);
927355b4669Sjacobs 			if (status != PAPI_OK) {
9284749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				if (!print_flag)
929355b4669Sjacobs 					fprintf(stderr, gettext(
93054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    "Failed to get job"\
93154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    " info for %s: %s\n"),
93254d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    request,
93354d5ddccSsonam gupta - Sun Microsystems - Bangalore India 					    verbose_papi_message(svc, status));
934355b4669Sjacobs 				papiServiceDestroy(svc);
935355b4669Sjacobs 				return (-1);
936355b4669Sjacobs 			}
937355b4669Sjacobs 
938355b4669Sjacobs 			if (job != NULL)
939*35a603adSsonam gupta - Sun Microsystems - Bangalore India 				result = report(printer, job,
940*35a603adSsonam gupta - Sun Microsystems - Bangalore India 				    show_rank, verbose);
941355b4669Sjacobs 
942355b4669Sjacobs 			papiJobFree(job);
943355b4669Sjacobs 		}
944355b4669Sjacobs 
9454749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		if (flag) {
9464749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			id = -1;
9474749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			get_printer_id(printer, &printer, &id);
9484749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			if (id == -1)
9494749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				flag = 0;
9504749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 			else
9514749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 				flag1 = 1;
9524749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 		}
9534749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 	} while (flag);
9544749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 
955355b4669Sjacobs 	papiServiceDestroy(svc);
956355b4669Sjacobs 
957355b4669Sjacobs 	return (result);
958355b4669Sjacobs }
959355b4669Sjacobs 
960355b4669Sjacobs static int
961355b4669Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
962355b4669Sjacobs {
963355b4669Sjacobs 	papi_status_t status;
964355b4669Sjacobs 	char *form = NULL;
965355b4669Sjacobs 	void *iter = NULL;
966355b4669Sjacobs 
967355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
968355b4669Sjacobs 	    "form-supported", &form);
969355b4669Sjacobs 	    status == PAPI_OK;
970355b4669Sjacobs 	    status = papiAttributeListGetString(attrs, &iter,
971355b4669Sjacobs 	    NULL, &form)) {
972355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
973355b4669Sjacobs 			printf(gettext("form %s is available to you\n"), form);
974355b4669Sjacobs 			if (verbose != 0) {
975355b4669Sjacobs 				char *detail = NULL;
976355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
97754d5ddccSsonam gupta - Sun Microsystems - Bangalore India 				    "form-supported-detail", &detail);
978355b4669Sjacobs 				if (status == PAPI_OK)
979355b4669Sjacobs 					printf("%s\n", detail);
980355b4669Sjacobs 			}
981355b4669Sjacobs 		}
982355b4669Sjacobs 	}
983355b4669Sjacobs 
984355b4669Sjacobs 	return (0);
985355b4669Sjacobs }
986355b4669Sjacobs 
987355b4669Sjacobs static int
988355b4669Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
989355b4669Sjacobs {
990355b4669Sjacobs 	papi_status_t status;
991355b4669Sjacobs 	char *pw = NULL;
992355b4669Sjacobs 	void *iter = NULL;
993355b4669Sjacobs 
994355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
995355b4669Sjacobs 	    "pw-supported", &pw);
996355b4669Sjacobs 	    status == PAPI_OK;
997355b4669Sjacobs 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
998355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
999355b4669Sjacobs 			printf(gettext("charset %s is available\n"), pw);
1000355b4669Sjacobs 			if (verbose != 0) {
1001355b4669Sjacobs 				char *info = NULL;
1002355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
1003355b4669Sjacobs 				    "pw-supported-extra", &info);
1004355b4669Sjacobs 				if (status == PAPI_OK)
1005355b4669Sjacobs 					printf("%s\n", info);
1006355b4669Sjacobs 			}
1007355b4669Sjacobs 		}
1008355b4669Sjacobs 	}
1009355b4669Sjacobs 
1010355b4669Sjacobs 	return (0);
1011355b4669Sjacobs }
1012355b4669Sjacobs 
1013355b4669Sjacobs static int
1014355b4669Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
1015355b4669Sjacobs 		papi_encryption_t encryption, int verbose)
1016355b4669Sjacobs {
1017355b4669Sjacobs 	int result = 0;
1018355b4669Sjacobs 	papi_status_t status;
1019355b4669Sjacobs 	papi_service_t svc = NULL;
1020355b4669Sjacobs 	papi_attribute_t **attrs = NULL;
1021355b4669Sjacobs 
1022355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1023355b4669Sjacobs 	    encryption, NULL);
1024355b4669Sjacobs 	if (status != PAPI_OK) {
1025355b4669Sjacobs 		papiServiceDestroy(svc);
1026355b4669Sjacobs 		return (-1);
1027355b4669Sjacobs 	}
1028355b4669Sjacobs 
1029355b4669Sjacobs 	attrs = papiServiceGetAttributeList(svc);
1030355b4669Sjacobs 	if (attrs != NULL) {
1031355b4669Sjacobs 		result = report(name, attrs, verbose);
1032355b4669Sjacobs 
1033355b4669Sjacobs 		if (verbose > 1) {
1034355b4669Sjacobs 			printf("\n");
1035355b4669Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
1036355b4669Sjacobs 			printf("\n");
1037355b4669Sjacobs 		}
1038355b4669Sjacobs 	}
1039355b4669Sjacobs 
1040355b4669Sjacobs 	papiServiceDestroy(svc);
1041355b4669Sjacobs 
1042355b4669Sjacobs 	return (result);
1043355b4669Sjacobs }
1044355b4669Sjacobs 
1045355b4669Sjacobs int
1046355b4669Sjacobs main(int ac, char *av[])
1047355b4669Sjacobs {
1048355b4669Sjacobs 	int exit_code = 0;
1049355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
1050355b4669Sjacobs 	int rank = 0;
1051355b4669Sjacobs 	int verbose = 0;
1052355b4669Sjacobs 	int description = 0;
1053355b4669Sjacobs 	int c;
1054355b4669Sjacobs 	char **argv;
1055355b4669Sjacobs 
1056355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
1057355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
1058355b4669Sjacobs 
1059355b4669Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
106091216fe4Swendyp 	for (c = 0; c < ac; c++) {
106191216fe4Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
106291216fe4Swendyp 		    (isalpha(av[c][2]) != 0)) {
106391216fe4Swendyp 			/* preserve old "-l[po...]" behavior */
106491216fe4Swendyp 			argv[c] = &av[c][1];
106591216fe4Swendyp 			argv[c][0] = '-';
106691216fe4Swendyp 			verbose = 1;
106791216fe4Swendyp 
106891216fe4Swendyp 		} else
1069355b4669Sjacobs 			argv[c] = av[c];
107091216fe4Swendyp 	}
107191216fe4Swendyp 
1072355b4669Sjacobs 	argv[c++] = "--";
1073355b4669Sjacobs 	ac = c;
1074355b4669Sjacobs 
1075355b4669Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
1076355b4669Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
1077355b4669Sjacobs 		switch (c) {
1078355b4669Sjacobs 		case 'l':
1079355b4669Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
1080355b4669Sjacobs 				optarg = "1";
1081355b4669Sjacobs 			verbose = atoi(optarg);
1082355b4669Sjacobs 			break;
1083355b4669Sjacobs 		case 'D':
1084355b4669Sjacobs 			description = 1;
1085355b4669Sjacobs 			break;
1086355b4669Sjacobs 		case 'R':
1087355b4669Sjacobs 			rank = 1;
1088355b4669Sjacobs 			break;
1089355b4669Sjacobs 		case 'E':
1090355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
1091355b4669Sjacobs 			break;
1092355b4669Sjacobs 		default:
1093355b4669Sjacobs 			break;
1094355b4669Sjacobs 		}
1095355b4669Sjacobs 	optind = 1;
1096355b4669Sjacobs 
1097355b4669Sjacobs 	/* process command line arguments */
1098355b4669Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
1099355b4669Sjacobs 		switch (c) {	/* these may or may not have an option */
1100355b4669Sjacobs 		case 'a':
1101355b4669Sjacobs 		case 'c':
1102355b4669Sjacobs 		case 'p':
1103355b4669Sjacobs 		case 'o':
1104355b4669Sjacobs 		case 'R':
1105355b4669Sjacobs 		case 'u':
1106355b4669Sjacobs 		case 'v':
1107355b4669Sjacobs 		case 'l':
1108355b4669Sjacobs 		case 'f':
1109355b4669Sjacobs 		case 'S':
1110355b4669Sjacobs 			if (optarg[0] == '-') {
1111355b4669Sjacobs 				/* this check stop a possible infinite loop */
1112355b4669Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
1113355b4669Sjacobs 					optind--;
1114355b4669Sjacobs 				optarg = NULL;
1115355b4669Sjacobs 			} else if (strcmp(optarg, "all") == 0)
1116355b4669Sjacobs 				optarg = NULL;
1117355b4669Sjacobs 		}
1118355b4669Sjacobs 
1119355b4669Sjacobs 		switch (c) {
1120355b4669Sjacobs 		case 'a':
1121355b4669Sjacobs 			exit_code += printer_query(optarg, report_accepting,
1122355b4669Sjacobs 			    encryption, verbose, 0);
1123355b4669Sjacobs 			break;
1124355b4669Sjacobs 		case 'c':
1125355b4669Sjacobs 			exit_code += printer_query(optarg, report_class,
1126355b4669Sjacobs 			    encryption, verbose, 0);
1127355b4669Sjacobs 			break;
1128355b4669Sjacobs 		case 'p':
1129355b4669Sjacobs 			exit_code += printer_query(optarg, report_printer,
113054d5ddccSsonam gupta - Sun Microsystems - Bangalore India 			    encryption, verbose, description);
1131355b4669Sjacobs 			break;
1132355b4669Sjacobs 		case 'd':
1133355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
1134355b4669Sjacobs 			break;
1135355b4669Sjacobs 		case 'r':
1136355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
1137355b4669Sjacobs 			break;
1138355b4669Sjacobs 		case 'u':
1139355b4669Sjacobs 			if (optarg != NULL)
1140355b4669Sjacobs 				users = strsplit(optarg, ", \n");
1141355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
1142355b4669Sjacobs 			    encryption, rank, verbose);
1143355b4669Sjacobs 			if (users != NULL) {
1144355b4669Sjacobs 				free(users);
1145355b4669Sjacobs 				users = NULL;
1146355b4669Sjacobs 			}
1147355b4669Sjacobs 			break;
1148355b4669Sjacobs 		case 'v':
1149355b4669Sjacobs 			exit_code += printer_query(optarg, report_device,
1150355b4669Sjacobs 			    encryption, verbose, 0);
1151355b4669Sjacobs 			break;
1152c2765d20SGowtham Thommandra 		case 'R':	/* set "rank" flag in first pass */
1153355b4669Sjacobs 		case 'o':
1154355b4669Sjacobs 			exit_code += job_query(optarg, report_job,
1155355b4669Sjacobs 			    encryption, rank, verbose);
1156355b4669Sjacobs 			break;
1157355b4669Sjacobs 		case 'f':
1158355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
1159355b4669Sjacobs 			    encryption, verbose);
1160355b4669Sjacobs 			break;
1161355b4669Sjacobs 		case 'S':
1162355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
1163355b4669Sjacobs 			    encryption, verbose);
1164355b4669Sjacobs 			break;
1165355b4669Sjacobs 		case 's':
1166355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
1167355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
1168355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
1169355b4669Sjacobs 			    encryption, verbose, 0);
1170355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
1171355b4669Sjacobs 			    encryption, verbose, 0);
1172355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
1173355b4669Sjacobs 			    encryption, verbose);
1174355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
1175355b4669Sjacobs 			    encryption, verbose);
1176355b4669Sjacobs 			break;
1177355b4669Sjacobs 		case 't':
1178355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
1179355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
1180355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
1181355b4669Sjacobs 			    encryption, verbose, 0);
1182355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
1183355b4669Sjacobs 			    encryption, verbose, 0);
1184355b4669Sjacobs 			exit_code += printer_query(NULL, report_accepting,
1185355b4669Sjacobs 			    encryption, verbose, 0);
1186355b4669Sjacobs 			exit_code += printer_query(NULL, report_printer,
1187355b4669Sjacobs 			    encryption, verbose, 0);
1188355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
1189355b4669Sjacobs 			    encryption, verbose);
1190355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
1191355b4669Sjacobs 			    encryption, verbose);
1192355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
1193355b4669Sjacobs 			    encryption, rank, verbose);
1194355b4669Sjacobs 			break;
1195355b4669Sjacobs 		case 'L':	/* local-only, ignored */
1196355b4669Sjacobs 		case 'l':	/* increased verbose level in first pass */
1197355b4669Sjacobs 		case 'D':	/* set "description" flag in first pass */
1198355b4669Sjacobs 		case 'E':	/* set encryption in the first pass */
1199355b4669Sjacobs 			break;
1200355b4669Sjacobs 		default:
1201355b4669Sjacobs 			usage(av[0]);
1202355b4669Sjacobs 		}
1203355b4669Sjacobs 	}
1204355b4669Sjacobs 	ac--;
1205355b4669Sjacobs 
1206355b4669Sjacobs 	if (ac == 1) {	/* report on my jobs */
1207355b4669Sjacobs 		struct passwd *pw = getpwuid(getuid());
1208355b4669Sjacobs 
1209355b4669Sjacobs 		if (pw != NULL)
1210355b4669Sjacobs 			users = strsplit(pw->pw_name, "");
1211355b4669Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
1212355b4669Sjacobs 		    rank, verbose);
1213355b4669Sjacobs 		if (users != NULL) {
1214355b4669Sjacobs 			free(users);
1215355b4669Sjacobs 			users = NULL;
1216355b4669Sjacobs 		}
1217355b4669Sjacobs 	} else {
1218355b4669Sjacobs 		for (c = optind; c < ac; c++)
1219355b4669Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
1220355b4669Sjacobs 			    rank, verbose);
1221355b4669Sjacobs 	}
1222355b4669Sjacobs 
1223355b4669Sjacobs 
1224355b4669Sjacobs 	if (exit_code != 0)
1225355b4669Sjacobs 		exit_code = 1;
1226355b4669Sjacobs 
1227355b4669Sjacobs 	return (exit_code);
1228355b4669Sjacobs }
1229