12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
238534SSonam.Gupta@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: lpstat.c 173 2006-05-25 04:52:06Z njacobs $ */
292264Sjacobs 
302264Sjacobs 
312264Sjacobs #include <stdio.h>
322264Sjacobs #include <stdlib.h>
332264Sjacobs #include <unistd.h>
342264Sjacobs #include <string.h>
352264Sjacobs #include <locale.h>
362264Sjacobs #include <libintl.h>
374286Swendyp #include <ctype.h>
382264Sjacobs #include <pwd.h>
392264Sjacobs #include <papi.h>
402264Sjacobs #include <uri.h>
412264Sjacobs #include "common.h"
42*9669SGowtham.Thommandra@Sun.COM #include "lp.h"
432264Sjacobs 
442264Sjacobs static void
452264Sjacobs usage(char *program)
462264Sjacobs {
472264Sjacobs 	char *name;
482264Sjacobs 
492264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
502264Sjacobs 		name = program;
512264Sjacobs 	else
522264Sjacobs 		name++;
532264Sjacobs 
542264Sjacobs 	fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
558962SSonam.Gupta@Sun.COM 	    "[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
568962SSonam.Gupta@Sun.COM 	    "[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
578962SSonam.Gupta@Sun.COM 	    "[-f [list] [-l]] [-u list]\n"),
588962SSonam.Gupta@Sun.COM 	    name);
592264Sjacobs 	exit(1);
602264Sjacobs }
612264Sjacobs 
622264Sjacobs static char *
632264Sjacobs nctime(time_t *t)
642264Sjacobs {
652264Sjacobs 	static char buf[64];
662264Sjacobs 	struct tm *tm = localtime(t);
672264Sjacobs 
682264Sjacobs 	(void) strftime(buf, sizeof (buf), "%c", tm);
692264Sjacobs 
702264Sjacobs 	return (buf);
712264Sjacobs }
722264Sjacobs 
732264Sjacobs static char *
742264Sjacobs printer_name(papi_printer_t printer)
752264Sjacobs {
762264Sjacobs 	papi_attribute_t **attributes = papiPrinterGetAttributeList(printer);
772264Sjacobs 	char *result = NULL;
782264Sjacobs 
792264Sjacobs 	if (attributes != NULL)
802264Sjacobs 		papiAttributeListGetString(attributes, NULL,
818962SSonam.Gupta@Sun.COM 		    "printer-name", &result);
822264Sjacobs 
832264Sjacobs 	return (result);
842264Sjacobs }
852264Sjacobs 
862264Sjacobs static int
872264Sjacobs lpstat_default_printer(papi_encryption_t encryption)
882264Sjacobs {
892264Sjacobs 	papi_status_t status;
902264Sjacobs 	papi_service_t svc = NULL;
912264Sjacobs 	papi_printer_t p = NULL;
922264Sjacobs 	char *name = NULL;
932264Sjacobs 
942264Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback,
958962SSonam.Gupta@Sun.COM 	    encryption, NULL);
962264Sjacobs 	if (status == PAPI_OK) {
972264Sjacobs 		char *req[] = { "printer-name", NULL };
982264Sjacobs 
992264Sjacobs 		status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p);
1002264Sjacobs 		if (p != NULL)
1012264Sjacobs 			name = printer_name(p);
1022264Sjacobs 	}
1032264Sjacobs 	if (name != NULL)
1042264Sjacobs 		printf(gettext("system default printer: %s\n"), name);
1052264Sjacobs 	else
1062264Sjacobs 		printf(gettext("no system default destination\n"));
1072264Sjacobs 	papiPrinterFree(p);
1082264Sjacobs 	papiServiceDestroy(svc);
1092264Sjacobs 
1102264Sjacobs 	return (0);
1112264Sjacobs }
1122264Sjacobs 
1132264Sjacobs static int
1142264Sjacobs lpstat_service_status(papi_encryption_t encryption)
1152264Sjacobs {
1162264Sjacobs 	int result = 0;
1172264Sjacobs 	papi_status_t status;
1182264Sjacobs 	papi_service_t svc = NULL;
1192264Sjacobs 	char *name = NULL;
1202264Sjacobs 
1212264Sjacobs 	if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
1222264Sjacobs 	    ((name = getenv("IPP_SERVER")) == NULL) &&
1232264Sjacobs 	    ((name = getenv("CUPS_SERVER")) == NULL))
1242264Sjacobs 		name = DEFAULT_SERVICE_URI;
1252264Sjacobs 
1262264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1278962SSonam.Gupta@Sun.COM 	    encryption, NULL);
1282264Sjacobs 	if (status != PAPI_OK) {
1292264Sjacobs 		printf(gettext("scheduler is not running\n"));
1302264Sjacobs 		result = -1;
1312264Sjacobs 	} else
1322264Sjacobs 		printf(gettext("scheduler is running\n"));
1332264Sjacobs 	papiServiceDestroy(svc);
1342264Sjacobs 
1352264Sjacobs 	return (result);
1362264Sjacobs }
1372264Sjacobs 
1382264Sjacobs static char *
1392264Sjacobs get_device_uri(papi_service_t svc, char *name)
1402264Sjacobs {
1412264Sjacobs 	papi_status_t status;
1422264Sjacobs 	papi_printer_t p = NULL;
1432264Sjacobs 	char *keys[] = { "device-uri", NULL };
1442264Sjacobs 	char *result = NULL;
1452264Sjacobs 
1462264Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
1472264Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
1482264Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
1492264Sjacobs 
1502264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
1518962SSonam.Gupta@Sun.COM 		    "device-uri", &result);
1522264Sjacobs 		if (result != NULL)
1532264Sjacobs 			result = strdup(result);
1542264Sjacobs 
1552264Sjacobs 		papiPrinterFree(p);
1562264Sjacobs 	}
1572264Sjacobs 
1582264Sjacobs 	return (result);
1592264Sjacobs }
1602264Sjacobs 
1618534SSonam.Gupta@Sun.COM static void
1628534SSonam.Gupta@Sun.COM print_description(papi_attribute_t **list, char *printer_name)
1638534SSonam.Gupta@Sun.COM {
1648534SSonam.Gupta@Sun.COM 	char *str = "";
1658534SSonam.Gupta@Sun.COM 
1668534SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(list, NULL,
1678534SSonam.Gupta@Sun.COM 	    "printer-info", &str);
1688534SSonam.Gupta@Sun.COM 
1698534SSonam.Gupta@Sun.COM 	/*
1708534SSonam.Gupta@Sun.COM 	 * If no printer-info is read then
1718534SSonam.Gupta@Sun.COM 	 * by default the printer-info is <printer-name>@<server>
1728534SSonam.Gupta@Sun.COM 	 */
1738534SSonam.Gupta@Sun.COM 	if (str[0] == '\0') {
1748534SSonam.Gupta@Sun.COM 		char *uri = NULL;
1758534SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
1768534SSonam.Gupta@Sun.COM 
1778534SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(list, NULL,
1788534SSonam.Gupta@Sun.COM 		    "printer-uri-supported", &uri);
1798534SSonam.Gupta@Sun.COM 
1808534SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
1818534SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
1828534SSonam.Gupta@Sun.COM 
1838534SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
1848534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
1858534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0))
1868534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription:\n"));
1878534SSonam.Gupta@Sun.COM 			else
1888534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription: %s@%s\n"),
1898534SSonam.Gupta@Sun.COM 				    printer_name, u->host);
1908534SSonam.Gupta@Sun.COM 
1918534SSonam.Gupta@Sun.COM 			uri_free(u);
1928534SSonam.Gupta@Sun.COM 		} else
1938534SSonam.Gupta@Sun.COM 			printf(gettext("\tDescription:\n"));
1948534SSonam.Gupta@Sun.COM 	} else
1958534SSonam.Gupta@Sun.COM 		printf(gettext("\tDescription: %s\n"), str);
1968534SSonam.Gupta@Sun.COM }
1978534SSonam.Gupta@Sun.COM 
1982264Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
1992264Sjacobs 					NULL };
2002264Sjacobs /* ARGSUSED2 */
2012264Sjacobs static int
2022264Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
2032264Sjacobs 		int verbose, int description)
2042264Sjacobs {
2052264Sjacobs 	papi_status_t status;
2062264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2072264Sjacobs 	char *uri = NULL;
2082264Sjacobs 	char *device = NULL;
2092264Sjacobs 	uri_t *u = NULL;
2102264Sjacobs 
2112264Sjacobs 	if (name == NULL) {
2122264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2138962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
2142264Sjacobs 		if (status != PAPI_OK)
2152264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2168962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
2172264Sjacobs 	}
2182264Sjacobs 
2192264Sjacobs 	if (name == NULL)
2202264Sjacobs 		return (-1);
2212264Sjacobs 
2222264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
2238962SSonam.Gupta@Sun.COM 	    "printer-uri-supported", &uri);
2242264Sjacobs 
2252264Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
2262264Sjacobs 		char *nodename = localhostname();
2272264Sjacobs 
2282264Sjacobs 		if ((u->host == NULL) ||
2292264Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
2302264Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
2312264Sjacobs 			device = get_device_uri(svc, name);
2322264Sjacobs 
2332264Sjacobs 		if (device != NULL) {
2342264Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
2352264Sjacobs 			return (0);
2362264Sjacobs 		} else if (uri != NULL) {
2372264Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
2389411SKeerthi.Kondaka@Sun.COM 			    u->host?u->host:"localhost", uri);
2392264Sjacobs 			return (0);
2402264Sjacobs 		}
2412264Sjacobs 
2422264Sjacobs 		uri_free(u);
2432264Sjacobs 	}
2442264Sjacobs 
2452264Sjacobs 	return (0);
2462264Sjacobs }
2472264Sjacobs 
2482264Sjacobs static char *report_accepting_keys[] = { "printer-name",
2492264Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
2502264Sjacobs 			"printer-up-time", "printer-state-time",
2512264Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
2522264Sjacobs /* ARGSUSED2 */
2532264Sjacobs static int
2542264Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
2552264Sjacobs 		int verbose, int description)
2562264Sjacobs {
2572264Sjacobs 	papi_status_t status;
2582264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2592264Sjacobs 	time_t curr;
2602264Sjacobs 	char boolean = PAPI_FALSE;
2612264Sjacobs 
2622264Sjacobs 	if (name == NULL) {
2632264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2648962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
2652264Sjacobs 		if (status != PAPI_OK)
2662264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2678962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
2682264Sjacobs 	}
2692264Sjacobs 	if (name == NULL)
2702264Sjacobs 		return (-1);
2712264Sjacobs 
2722264Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
2738962SSonam.Gupta@Sun.COM 	    "printer-is-accepting-jobs", &boolean);
2742264Sjacobs 	(void) time(&curr);
2752264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2768962SSonam.Gupta@Sun.COM 	    "printer-up-time", &curr);
2772264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2788962SSonam.Gupta@Sun.COM 	    "printer-state-time", &curr);
2792264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2808962SSonam.Gupta@Sun.COM 	    "lpsched-reject-date", &curr);
2812264Sjacobs 
2822264Sjacobs 	if (boolean == PAPI_TRUE) {
2832264Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
2848962SSonam.Gupta@Sun.COM 		    name, nctime(&curr));
2852264Sjacobs 	} else {
2862264Sjacobs 		char *reason = "unknown reason";
2872264Sjacobs 
2882264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
2898962SSonam.Gupta@Sun.COM 		    "lpsched-reject-reason", &reason);
2902264Sjacobs 
2912264Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
2928962SSonam.Gupta@Sun.COM 		    name, nctime(&curr), reason);
2932264Sjacobs 	}
2942264Sjacobs 
2952264Sjacobs 	return (0);
2962264Sjacobs }
2972264Sjacobs 
2982264Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
2992264Sjacobs 					"member-names", NULL };
3002264Sjacobs /* ARGSUSED2 */
3012264Sjacobs static int
3022264Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
3032264Sjacobs 		int verbose, int description)
3042264Sjacobs {
3052264Sjacobs 	papi_status_t status;
3062264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3072264Sjacobs 	char *member = NULL;
3082264Sjacobs 	void *iter = NULL;
3092264Sjacobs 
3102264Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
3118962SSonam.Gupta@Sun.COM 	    "member-names", &member);
3122264Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
3132264Sjacobs 		return (0);
3142264Sjacobs 
3152264Sjacobs 	if (name == NULL) {
3162264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3178962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
3182264Sjacobs 		if (status != PAPI_OK)
3192264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3208962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
3212264Sjacobs 	}
3222264Sjacobs 	if (name == NULL)
3232264Sjacobs 		return (-1);
3242264Sjacobs 
3252264Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
3262264Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
3278962SSonam.Gupta@Sun.COM 	    == PAPI_OK)
3282264Sjacobs 		printf("\t%s\n", member);
3292264Sjacobs 
3302264Sjacobs 	return (0);
3312264Sjacobs }
3322264Sjacobs 
3332264Sjacobs static char *report_printer_keys[] = { "printer-name",
3342264Sjacobs 			"printer-uri-supported", "printer-state",
3352264Sjacobs 			"printer-up-time", "printer-state-time",
3362264Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
3372264Sjacobs 			"lpsched-disable-reason", NULL };
3382264Sjacobs /* ARGSUSED2 */
3392264Sjacobs static int
3402264Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
3412264Sjacobs 		int verbose, int description)
3422264Sjacobs {
3432264Sjacobs 	papi_status_t status;
3442264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3452264Sjacobs 	time_t curr;
3462264Sjacobs 	int32_t pstat = 0;
3472264Sjacobs 	char *member = NULL;
3488962SSonam.Gupta@Sun.COM 	papi_job_t *j = NULL;
3492264Sjacobs 
3502264Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
3518962SSonam.Gupta@Sun.COM 	    "member-names", &member);
3522264Sjacobs 	if (status == PAPI_OK)	/* it's a class */
3532264Sjacobs 		return (0);
3542264Sjacobs 
3552264Sjacobs 	if (name == NULL) {
3562264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3578962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
3582264Sjacobs 		if (status != PAPI_OK)
3592264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3608962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
3612264Sjacobs 	}
3622264Sjacobs 	if (name == NULL)
3632264Sjacobs 		return (-1);
3642264Sjacobs 
3652264Sjacobs 	printf(gettext("printer %s "), name);
3662264Sjacobs 
3672264Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
3688962SSonam.Gupta@Sun.COM 	    "printer-state", &pstat);
3692264Sjacobs 
3702264Sjacobs 	switch (pstat) {
3712264Sjacobs 	case 0x03:	/* idle */
3722264Sjacobs 		printf(gettext("idle. enabled"));
3732264Sjacobs 		break;
3748962SSonam.Gupta@Sun.COM 	case 0x04: /* processing */
3758962SSonam.Gupta@Sun.COM 		status = papiPrinterListJobs(svc, name, NULL,
3768962SSonam.Gupta@Sun.COM 		    0, 0, &j);
3778962SSonam.Gupta@Sun.COM 
3788962SSonam.Gupta@Sun.COM 		if (status == PAPI_OK) {
3798962SSonam.Gupta@Sun.COM 			if (j != NULL) {
3808962SSonam.Gupta@Sun.COM 				int i = 0;
3818962SSonam.Gupta@Sun.COM 				int32_t jobid = 0;
3828962SSonam.Gupta@Sun.COM 				int32_t jstate = 0;
3838962SSonam.Gupta@Sun.COM 				int flag = 0;
3848962SSonam.Gupta@Sun.COM 
3858962SSonam.Gupta@Sun.COM 				for (i = 0; j[i] != NULL; ++i) {
3869206SSonam.Gupta@Sun.COM 					papi_attribute_t **attr =
3879206SSonam.Gupta@Sun.COM 					    papiJobGetAttributeList(j[i]);
3888962SSonam.Gupta@Sun.COM 
3899206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
3908962SSonam.Gupta@Sun.COM 					    NULL, "job-state", &jstate);
3919206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
3928962SSonam.Gupta@Sun.COM 					    NULL, "job-id", &jobid);
3932264Sjacobs 
3948962SSonam.Gupta@Sun.COM 					/*
3959257SGowtham.Thommandra@Sun.COM 					 * If the job-state is in
3969257SGowtham.Thommandra@Sun.COM 					 * RS_PRINTING then only print.
3978962SSonam.Gupta@Sun.COM 					 */
3989257SGowtham.Thommandra@Sun.COM 					if (jstate == 0x0008) {
3998962SSonam.Gupta@Sun.COM 						if (flag == 0)
4008962SSonam.Gupta@Sun.COM 							printf(gettext
4018962SSonam.Gupta@Sun.COM 							    ("now printing"\
4028962SSonam.Gupta@Sun.COM 							    " %s-%d. enabled"),
4038962SSonam.Gupta@Sun.COM 							    name, jobid);
4048962SSonam.Gupta@Sun.COM 						flag = 1;
4058962SSonam.Gupta@Sun.COM 					}
4068962SSonam.Gupta@Sun.COM 				}
4078962SSonam.Gupta@Sun.COM 				papiJobListFree(j);
4088962SSonam.Gupta@Sun.COM 			}
4092264Sjacobs 		}
4102264Sjacobs 		break;
4112264Sjacobs 	case 0x05:	/* stopped */
4122264Sjacobs 		printf(gettext("disabled"));
4132264Sjacobs 		break;
4142264Sjacobs 	default:
4152264Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
4162264Sjacobs 		break;
4172264Sjacobs 	}
4182264Sjacobs 
4192264Sjacobs 	(void) time(&curr);
4202264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4218962SSonam.Gupta@Sun.COM 	    "printer-up-time", &curr);
4222264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4238962SSonam.Gupta@Sun.COM 	    "printer-state-time", &curr);
4242264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4258962SSonam.Gupta@Sun.COM 	    "lpsched-disable-date", &curr);
4262264Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
4272264Sjacobs 
4282264Sjacobs 	if (pstat == 0x05) {
4292264Sjacobs 		char *reason = "unknown reason";
4302264Sjacobs 
4312264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4328962SSonam.Gupta@Sun.COM 		    "printer-state-reasons", &reason);
4332264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4348962SSonam.Gupta@Sun.COM 		    "lpsched-disable-reason", &reason);
4352264Sjacobs 		printf(gettext("\t%s\n"), reason);
4362264Sjacobs 	}
4372264Sjacobs 
4382264Sjacobs 	if (verbose == 1) {
4392264Sjacobs 		void *iter;
4402264Sjacobs 		char *str;
4412264Sjacobs 
4422264Sjacobs 		str = "";
4432264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4448962SSonam.Gupta@Sun.COM 		    "form-ready", &str);
4452264Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
4462264Sjacobs 
4472264Sjacobs 		str = "";
4482264Sjacobs 		iter = NULL;
4492264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4508962SSonam.Gupta@Sun.COM 		    "document-format-supported", &str);
4512264Sjacobs 		printf(gettext("\tContent types: %s"), str);
4522264Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
4538962SSonam.Gupta@Sun.COM 		    == PAPI_OK)
4542264Sjacobs 			printf(", %s", str);
4552264Sjacobs 		printf("\n");
4562264Sjacobs 
4578534SSonam.Gupta@Sun.COM 		/* Display the printer description */
4588534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
4592264Sjacobs 
4602264Sjacobs 		str = "";
4616676Swendyp 		iter = NULL;
4626676Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
4636676Swendyp 		    "lpsched-printer-type", &str);
4646676Swendyp 		printf(gettext("\tPrinter types: %s"), str);
4656676Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
4666676Swendyp 		    == PAPI_OK)
4676676Swendyp 			printf(", %s", str);
4686676Swendyp 		printf("\n");
4696676Swendyp 
4706676Swendyp 		str = "";
4712264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4728962SSonam.Gupta@Sun.COM 		    "lpsched-dial-info", &str);
4732264Sjacobs 		printf(gettext("\tConnection: %s\n"),
4747653SJonathan.Ca@Sun.COM 		    ((str[0] == '\0') ? gettext("direct") : str));
4752264Sjacobs 
4762264Sjacobs 		str = "";
4772264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4788962SSonam.Gupta@Sun.COM 		    "lpsched-interface-script", &str);
4792264Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
4802264Sjacobs 
4812264Sjacobs 		str = NULL;
4822264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4838962SSonam.Gupta@Sun.COM 		    "ppd-file-uri", &str);
4842264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4858962SSonam.Gupta@Sun.COM 		    "lpsched-ppd-source-path", &str);
4862264Sjacobs 		if (str != NULL)
4872264Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
4882264Sjacobs 
4892264Sjacobs 		str = NULL;
4902264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4918962SSonam.Gupta@Sun.COM 		    "lpsched-fault-alert-command", &str);
4922264Sjacobs 		if (str != NULL)
4932264Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
4942264Sjacobs 
4952264Sjacobs 		str = "";
4962264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4978962SSonam.Gupta@Sun.COM 		    "lpsched-fault-recovery", &str);
4982264Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
4998962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("continue") : str));
5002264Sjacobs 
5012264Sjacobs 		str = "(all)";
5022264Sjacobs 		iter = NULL;
5032264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5048962SSonam.Gupta@Sun.COM 		    "requesting-user-name-allowed", &str);
5052264Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
5068962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5072264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5082264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5098962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5102264Sjacobs 				printf("\t\t%s\n", str);
5112264Sjacobs 
5122264Sjacobs 		str = NULL;
5132264Sjacobs 		iter = NULL;
5142264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5158962SSonam.Gupta@Sun.COM 		    "requesting-user-name-denied", &str);
5162264Sjacobs 		if (str != NULL) {
5172264Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
5188962SSonam.Gupta@Sun.COM 			    ((str[0] == '\0') ? gettext("(none)") : str));
5192264Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
5202264Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
5218962SSonam.Gupta@Sun.COM 				    NULL, &str) == PAPI_OK)
5222264Sjacobs 					printf("\t\t%s\n", str);
5232264Sjacobs 		}
5242264Sjacobs 
5258238SSonam.Gupta@Sun.COM 		str = "none";
5262264Sjacobs 		iter = NULL;
5272264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5288962SSonam.Gupta@Sun.COM 		    "form-supported", &str);
5298238SSonam.Gupta@Sun.COM 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
5308962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("none") : str));
5312264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5322264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5338962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5348238SSonam.Gupta@Sun.COM 				printf("\t\t(%s)\n", str);
5352264Sjacobs 
5362264Sjacobs 		str = "";
5372264Sjacobs 		iter = NULL;
5382264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5398962SSonam.Gupta@Sun.COM 		    "media-supported", &str);
5402264Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
5418962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5422264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5432264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5448962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5452264Sjacobs 				printf("\t\t%s\n", str);
5462264Sjacobs 
5472264Sjacobs 		str = "";
5482264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5498962SSonam.Gupta@Sun.COM 		    "job-sheets-supported", &str);
5506676Swendyp 		if ((strcasecmp(str, "none")) == 0)
5516676Swendyp 			str = gettext("page never printed");
5526676Swendyp 		else if (strcasecmp(str, "optional") == 0)
5536676Swendyp 			str = gettext("not required");
5546676Swendyp 		else
5556676Swendyp 			str = gettext("required");
5566676Swendyp 
5576676Swendyp 		printf(gettext("\tBanner %s\n"), str);
5586676Swendyp 
5592264Sjacobs 
5602264Sjacobs 		str = "";
5612264Sjacobs 		iter = NULL;
5622264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5638962SSonam.Gupta@Sun.COM 		    "lpsched-print-wheels", &str);
5642264Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
5658962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5662264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5672264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5688962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5692264Sjacobs 				printf("\t\t%s\n", str);
5702264Sjacobs 
5712264Sjacobs 		printf(gettext("\tDefault pitch:\n"));
5722264Sjacobs 		printf(gettext("\tDefault page size:\n"));
5732264Sjacobs 		printf(gettext("\tDefault port setting:\n"));
5742264Sjacobs 
5752264Sjacobs 		str = "";
5762264Sjacobs 		iter = NULL;
5772264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5788962SSonam.Gupta@Sun.COM 		    "lpsched-options", &str);
5792264Sjacobs 		if (str != NULL) {
5802264Sjacobs 			printf(gettext("\tOptions: %s"), str);
5812264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5828962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5832264Sjacobs 				printf(", %s", str);
5842264Sjacobs 			printf("\n");
5852264Sjacobs 		}
5862264Sjacobs 
5878534SSonam.Gupta@Sun.COM 	} else if (description == 1)
5888534SSonam.Gupta@Sun.COM 		/* Display printer description */
5898534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
5908534SSonam.Gupta@Sun.COM 	else if (verbose > 1)
5912264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
5922264Sjacobs 
5932264Sjacobs 	if (verbose > 0)
5942264Sjacobs 		printf("\n");
5952264Sjacobs 
5962264Sjacobs 	return (0);
5972264Sjacobs }
5982264Sjacobs 
5992264Sjacobs static int
6002264Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
6012264Sjacobs 					int, int), papi_encryption_t encryption,
6022264Sjacobs 		int verbose, int description)
6032264Sjacobs {
604*9669SGowtham.Thommandra@Sun.COM 	int result = 0, i = 0;
6052264Sjacobs 	papi_status_t status;
6062264Sjacobs 	papi_service_t svc = NULL;
607*9669SGowtham.Thommandra@Sun.COM 	char **list = getlist(name, LP_WS, LP_SEP);
6082264Sjacobs 
609*9669SGowtham.Thommandra@Sun.COM 	if (list == NULL) {
610*9669SGowtham.Thommandra@Sun.COM 		list = (char **)malloc(sizeof (char *));
611*9669SGowtham.Thommandra@Sun.COM 		list[0] = name;
6122264Sjacobs 	}
6132264Sjacobs 
614*9669SGowtham.Thommandra@Sun.COM 	/*
615*9669SGowtham.Thommandra@Sun.COM 	 * The for loop executes once for every printer
616*9669SGowtham.Thommandra@Sun.COM 	 * entry in list. If list is NULL that implies
617*9669SGowtham.Thommandra@Sun.COM 	 * name is also NULL, the loop runs only one time.
618*9669SGowtham.Thommandra@Sun.COM 	 */
6192264Sjacobs 
620*9669SGowtham.Thommandra@Sun.COM 	for (i = 0; name == NULL || list[i] != NULL; i++) {
621*9669SGowtham.Thommandra@Sun.COM 		name = list[i];
6222264Sjacobs 
623*9669SGowtham.Thommandra@Sun.COM 		status = papiServiceCreate(&svc, name, NULL, NULL,
624*9669SGowtham.Thommandra@Sun.COM 		    cli_auth_callback, encryption, NULL);
6252264Sjacobs 		if (status != PAPI_OK) {
626*9669SGowtham.Thommandra@Sun.COM 			if (status == PAPI_NOT_FOUND)
627*9669SGowtham.Thommandra@Sun.COM 				fprintf(stderr,
628*9669SGowtham.Thommandra@Sun.COM 				    gettext("%s: unknown printer\n"),
629*9669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)");
630*9669SGowtham.Thommandra@Sun.COM 			else
631*9669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
632*9669SGowtham.Thommandra@Sun.COM 				    "Failed to contact service for %s: %s\n"),
633*9669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)",
634*9669SGowtham.Thommandra@Sun.COM 				    verbose_papi_message(svc, status));
6352264Sjacobs 			papiServiceDestroy(svc);
636*9669SGowtham.Thommandra@Sun.COM 			result--;
637*9669SGowtham.Thommandra@Sun.COM 			continue;
6382264Sjacobs 		}
6392264Sjacobs 
640*9669SGowtham.Thommandra@Sun.COM 		if (name == NULL) { /* all */
641*9669SGowtham.Thommandra@Sun.COM 			char **interest = interest_list(svc);
642*9669SGowtham.Thommandra@Sun.COM 
643*9669SGowtham.Thommandra@Sun.COM 			if (interest != NULL) {
644*9669SGowtham.Thommandra@Sun.COM 				int i;
645*9669SGowtham.Thommandra@Sun.COM 
646*9669SGowtham.Thommandra@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
647*9669SGowtham.Thommandra@Sun.COM 					result += printer_query(interest[i],
648*9669SGowtham.Thommandra@Sun.COM 					    report, encryption, verbose,
649*9669SGowtham.Thommandra@Sun.COM 					    description);
650*9669SGowtham.Thommandra@Sun.COM 			}
651*9669SGowtham.Thommandra@Sun.COM 		} else {
652*9669SGowtham.Thommandra@Sun.COM 			papi_printer_t printer = NULL;
653*9669SGowtham.Thommandra@Sun.COM 			char **keys = NULL;
6542264Sjacobs 
655*9669SGowtham.Thommandra@Sun.COM 			/*
656*9669SGowtham.Thommandra@Sun.COM 			 * Limit the query to only required data
657*9669SGowtham.Thommandra@Sun.COM 			 * to reduce the need to go remote for
658*9669SGowtham.Thommandra@Sun.COM 			 * information.
659*9669SGowtham.Thommandra@Sun.COM 			 */
660*9669SGowtham.Thommandra@Sun.COM 			if (report == report_device)
661*9669SGowtham.Thommandra@Sun.COM 				keys = report_device_keys;
662*9669SGowtham.Thommandra@Sun.COM 			else if (report == report_class)
663*9669SGowtham.Thommandra@Sun.COM 				keys = report_class_keys;
664*9669SGowtham.Thommandra@Sun.COM 			else if (report == report_accepting)
665*9669SGowtham.Thommandra@Sun.COM 				keys = report_accepting_keys;
666*9669SGowtham.Thommandra@Sun.COM 			else if ((report == report_printer) && (verbose == 0))
667*9669SGowtham.Thommandra@Sun.COM 				keys = report_printer_keys;
668*9669SGowtham.Thommandra@Sun.COM 
669*9669SGowtham.Thommandra@Sun.COM 			status = papiPrinterQuery(svc, name, keys,
670*9669SGowtham.Thommandra@Sun.COM 			    NULL, &printer);
671*9669SGowtham.Thommandra@Sun.COM 			if (status != PAPI_OK) {
672*9669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
673*9669SGowtham.Thommandra@Sun.COM 				    "Failed to get printer info for %s: %s\n"),
674*9669SGowtham.Thommandra@Sun.COM 				    name, verbose_papi_message(svc, status));
675*9669SGowtham.Thommandra@Sun.COM 				papiServiceDestroy(svc);
676*9669SGowtham.Thommandra@Sun.COM 				result--;
677*9669SGowtham.Thommandra@Sun.COM 				continue;
678*9669SGowtham.Thommandra@Sun.COM 			}
679*9669SGowtham.Thommandra@Sun.COM 
680*9669SGowtham.Thommandra@Sun.COM 			if (printer != NULL)
681*9669SGowtham.Thommandra@Sun.COM 				result += report(svc, name, printer, verbose,
682*9669SGowtham.Thommandra@Sun.COM 				    description);
683*9669SGowtham.Thommandra@Sun.COM 
684*9669SGowtham.Thommandra@Sun.COM 			papiPrinterFree(printer);
685*9669SGowtham.Thommandra@Sun.COM 		}
686*9669SGowtham.Thommandra@Sun.COM 
687*9669SGowtham.Thommandra@Sun.COM 		papiServiceDestroy(svc);
688*9669SGowtham.Thommandra@Sun.COM 
689*9669SGowtham.Thommandra@Sun.COM 		if (name == NULL)
690*9669SGowtham.Thommandra@Sun.COM 			break;
6912264Sjacobs 	}
6922264Sjacobs 
693*9669SGowtham.Thommandra@Sun.COM 	freelist(list);
6942264Sjacobs 
6952264Sjacobs 	return (result);
6962264Sjacobs }
6972264Sjacobs 
6982264Sjacobs static int
6992264Sjacobs match_user(char *user, char **list)
7002264Sjacobs {
7012264Sjacobs 	int i;
7022264Sjacobs 
7032264Sjacobs 	for (i = 0; list[i] != NULL; i++) {
7042264Sjacobs 		if (strcmp(user, list[i]) == 0)
7052264Sjacobs 			return (0);
7062264Sjacobs 	}
7072264Sjacobs 
7082264Sjacobs 	return (-1);
7092264Sjacobs }
7102264Sjacobs 
7112264Sjacobs static char **users = NULL;
7122264Sjacobs 
7132264Sjacobs static int
7149256SSonam.Gupta@Sun.COM report_job(char *printer, papi_job_t job, int show_rank, int verbose)
7152264Sjacobs {
7162264Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
7172264Sjacobs 	time_t clock = 0;
7182264Sjacobs 	char date[24];
7192264Sjacobs 	char request[26];
7202264Sjacobs 	char *user = "unknown";
7218321SSonam.Gupta@Sun.COM 	char *host = NULL;
7222264Sjacobs 	int32_t size = 0;
7232264Sjacobs 	int32_t jstate = 0;
7248321SSonam.Gupta@Sun.COM 	char User[50];
7252264Sjacobs 
7262264Sjacobs 	char *destination = "unknown";
7272264Sjacobs 	int32_t id = -1;
7289152SSonam.Gupta@Sun.COM 	static int check = 0;
7299152SSonam.Gupta@Sun.COM 	static char *uri = NULL;
7302264Sjacobs 
7312264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
7328962SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
7332264Sjacobs 
7342264Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
7352264Sjacobs 		return (0);
7362264Sjacobs 
7378321SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
7388321SSonam.Gupta@Sun.COM 	    "job-originating-host-name", &host);
7398321SSonam.Gupta@Sun.COM 
7409152SSonam.Gupta@Sun.COM 	if (check == 0) {
7419152SSonam.Gupta@Sun.COM 		/*
7429152SSonam.Gupta@Sun.COM 		 * Read the attribute "job-printer-uri"
7439152SSonam.Gupta@Sun.COM 		 * just once
7449152SSonam.Gupta@Sun.COM 		 */
7459152SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(attrs, NULL,
7469152SSonam.Gupta@Sun.COM 		    "job-printer-uri", &uri);
7479152SSonam.Gupta@Sun.COM 		check = 1;
7489152SSonam.Gupta@Sun.COM 	}
7499152SSonam.Gupta@Sun.COM 
7509152SSonam.Gupta@Sun.COM 	if (host) {
7519152SSonam.Gupta@Sun.COM 		/* Check if it is local printer or remote printer */
7529152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
7539152SSonam.Gupta@Sun.COM 
7549152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
7559152SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
7569152SSonam.Gupta@Sun.COM 
7579152SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
7589152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
7599152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0)) {
7608321SSonam.Gupta@Sun.COM 
7619152SSonam.Gupta@Sun.COM 				if (strcasecmp(host, nodename) == 0) {
7629152SSonam.Gupta@Sun.COM 					/*
7639152SSonam.Gupta@Sun.COM 					 * Request submitted locally
7649152SSonam.Gupta@Sun.COM 					 * for the local queue.
7659152SSonam.Gupta@Sun.COM 					 * Hostname will not be displayed
7669152SSonam.Gupta@Sun.COM 					 */
7679152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s",
7689152SSonam.Gupta@Sun.COM 					    user);
7699152SSonam.Gupta@Sun.COM 				}
7709152SSonam.Gupta@Sun.COM 				else
7719152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s@%s",
7729152SSonam.Gupta@Sun.COM 					    user, host);
7739152SSonam.Gupta@Sun.COM 			} else if (uri != NULL) {
7749152SSonam.Gupta@Sun.COM 				/*
7759152SSonam.Gupta@Sun.COM 				 * It's a remote printer.
7769152SSonam.Gupta@Sun.COM 				 * In case of remote printers hostname is
7779152SSonam.Gupta@Sun.COM 				 * always displayed.
7789152SSonam.Gupta@Sun.COM 				 */
7799152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
7809152SSonam.Gupta@Sun.COM 				    user, host);
7819152SSonam.Gupta@Sun.COM 			}
7829152SSonam.Gupta@Sun.COM 			uri_free(u);
7839152SSonam.Gupta@Sun.COM 		} else {
7849152SSonam.Gupta@Sun.COM 			/*
7859152SSonam.Gupta@Sun.COM 			 * If attribute "job-printer-uri"
7869152SSonam.Gupta@Sun.COM 			 * cannot be read
7879152SSonam.Gupta@Sun.COM 			 * by default append the hostname
7889152SSonam.Gupta@Sun.COM 			 */
7899152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s@%s", user, host);
7909152SSonam.Gupta@Sun.COM 		}
7919152SSonam.Gupta@Sun.COM 	} else {
7929152SSonam.Gupta@Sun.COM 		/*
7939152SSonam.Gupta@Sun.COM 		 * When print server is s10u4 and ipp service is used
7949152SSonam.Gupta@Sun.COM 		 * "job-originating-hostname" attribute is not set
7959152SSonam.Gupta@Sun.COM 		 * So get the host information from the uri
7969152SSonam.Gupta@Sun.COM 		 */
7979152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
7989152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
7999152SSonam.Gupta@Sun.COM 			if ((u != NULL) && (u->host != NULL))
8009152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
8019152SSonam.Gupta@Sun.COM 				    user, u->host);
8029152SSonam.Gupta@Sun.COM 			else
8039152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s", user);
8049152SSonam.Gupta@Sun.COM 
8059152SSonam.Gupta@Sun.COM 			uri_free(u);
8069152SSonam.Gupta@Sun.COM 		} else
8079152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s", user);
8089152SSonam.Gupta@Sun.COM 	}
8092264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
8102264Sjacobs 	size *= 1024;	/* for the approximate byte size */
8112264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
8122264Sjacobs 
8132264Sjacobs 	(void) time(&clock);
8142264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8158962SSonam.Gupta@Sun.COM 	    "time-at-creation", (int32_t *)&clock);
8162264Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
8172264Sjacobs 
8182264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
8198962SSonam.Gupta@Sun.COM 	    "job-printer-uri", &destination);
8202264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
8218962SSonam.Gupta@Sun.COM 	    "printer-name", &destination);
8222264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8238962SSonam.Gupta@Sun.COM 	    "job-id", &id);
8249330SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetInteger(attrs, NULL,
8259330SSonam.Gupta@Sun.COM 	    "job-id-requested", &id);
8269256SSonam.Gupta@Sun.COM 
8279331SSonam.Gupta@Sun.COM 
8289256SSonam.Gupta@Sun.COM 	snprintf(request, sizeof (request), "%s-%d", printer, id);
8292264Sjacobs 
8302264Sjacobs 	if (show_rank != 0) {
8312264Sjacobs 		int32_t rank = -1;
8322264Sjacobs 
8332264Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
8348962SSonam.Gupta@Sun.COM 		    "number-of-intervening-jobs", &rank);
8352264Sjacobs 		rank++;
8362264Sjacobs 
8372264Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
8388321SSonam.Gupta@Sun.COM 		    rank, request, User, size, date);
8392264Sjacobs 	} else
8408321SSonam.Gupta@Sun.COM 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
8412264Sjacobs 
8422264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8438962SSonam.Gupta@Sun.COM 	    "job-state", &jstate);
8449331SSonam.Gupta@Sun.COM 
8459257SGowtham.Thommandra@Sun.COM 	if (jstate == 0x0001)
8469257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being held"));
8479257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0800)
8489257SGowtham.Thommandra@Sun.COM 		printf(gettext(" notifying user"));
8499257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0040)
8509257SGowtham.Thommandra@Sun.COM 		printf(gettext(" cancelled"));
8519257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0010)
8529257SGowtham.Thommandra@Sun.COM 		printf(gettext(" finished printing"));
8539257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0008)
8549257SGowtham.Thommandra@Sun.COM 		printf(gettext(" on %s"), destination);
8559257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x2000)
8569257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held by admin"));
8579257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0002)
8589257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being filtered"));
8599257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0004)
8609257SGowtham.Thommandra@Sun.COM 		printf(gettext(" filtered"));
8619257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0020)
8629257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held for change"));
8632264Sjacobs 
8642264Sjacobs 	if (verbose == 1) {
8653125Sjacobs 		char *form = NULL;
8663125Sjacobs 
8672264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
8688962SSonam.Gupta@Sun.COM 		    "output-device-assigned", &destination);
8692264Sjacobs 		printf("\n\t assigned %s", destination);
8703127Sjacobs 
8713125Sjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
8723125Sjacobs 		if (form != NULL)
8733125Sjacobs 			printf(", form %s", form);
8742264Sjacobs 	} else if (verbose > 1) {
8752264Sjacobs 		printf("\n");
8762264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
8772264Sjacobs 	}
8782264Sjacobs 
8792264Sjacobs 	printf("\n");
8802264Sjacobs 
8812264Sjacobs 	return (0);
8822264Sjacobs }
8832264Sjacobs 
8842264Sjacobs static int
8859256SSonam.Gupta@Sun.COM job_query(char *request, int (*report)(char *, papi_job_t, int, int),
8862264Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
8872264Sjacobs {
8882264Sjacobs 	int result = 0;
8892264Sjacobs 	papi_status_t status;
8902264Sjacobs 	papi_service_t svc = NULL;
8918295SSonam.Gupta@Sun.COM 	char *printer = request;
8922264Sjacobs 	int32_t id = -1;
8938295SSonam.Gupta@Sun.COM 	int flag1 = 0;
8948295SSonam.Gupta@Sun.COM 	int flag = 1;
8958295SSonam.Gupta@Sun.COM 	int print_flag = 0;
8962264Sjacobs 
8978295SSonam.Gupta@Sun.COM 	do {
8988295SSonam.Gupta@Sun.COM 		status = papiServiceCreate(&svc, printer, NULL, NULL,
8998962SSonam.Gupta@Sun.COM 		    cli_auth_callback, encryption, NULL);
9002264Sjacobs 
9018295SSonam.Gupta@Sun.COM 		if ((status == PAPI_OK) && (printer != NULL))
9028295SSonam.Gupta@Sun.COM 			print_flag = 1;
9032264Sjacobs 
9048295SSonam.Gupta@Sun.COM 		/* <name>-# printer name does not exist */
9058295SSonam.Gupta@Sun.COM 		if (status != PAPI_OK) {
9068295SSonam.Gupta@Sun.COM 			/*
9078295SSonam.Gupta@Sun.COM 			 * Check if <name>-# is a request-id
9088295SSonam.Gupta@Sun.COM 			 * Once this check is done flag1 is set
9098295SSonam.Gupta@Sun.COM 			 */
9108295SSonam.Gupta@Sun.COM 			if (flag1 == 1)
9118295SSonam.Gupta@Sun.COM 				break;
9128295SSonam.Gupta@Sun.COM 
9138295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
9142264Sjacobs 
9158295SSonam.Gupta@Sun.COM 			status = papiServiceCreate(&svc, printer, NULL, NULL,
9168962SSonam.Gupta@Sun.COM 			    cli_auth_callback, encryption, NULL);
9172264Sjacobs 
9188295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
9198295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
9208295SSonam.Gupta@Sun.COM 				    "Failed to contact service for %s: %s\n"),
9218295SSonam.Gupta@Sun.COM 				    (printer ? printer : "all"),
9228295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
9238295SSonam.Gupta@Sun.COM 				return (-1);
9248295SSonam.Gupta@Sun.COM 			}
9252264Sjacobs 		}
9262264Sjacobs 
9278295SSonam.Gupta@Sun.COM 		if (printer == NULL) { /* all */
9288295SSonam.Gupta@Sun.COM 			char **interest = interest_list(svc);
9298295SSonam.Gupta@Sun.COM 
9308295SSonam.Gupta@Sun.COM 			if (interest != NULL) {
9318295SSonam.Gupta@Sun.COM 				int i;
9328295SSonam.Gupta@Sun.COM 
9338295SSonam.Gupta@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
9348295SSonam.Gupta@Sun.COM 					result += job_query(interest[i], report,
9358962SSonam.Gupta@Sun.COM 					    encryption, show_rank, verbose);
9368295SSonam.Gupta@Sun.COM 			}
9378295SSonam.Gupta@Sun.COM 		} else if (id == -1) { /* a printer */
9388295SSonam.Gupta@Sun.COM 			papi_job_t *jobs = NULL;
9398295SSonam.Gupta@Sun.COM 
9408295SSonam.Gupta@Sun.COM 			status = papiPrinterListJobs(svc, printer, NULL,
9418962SSonam.Gupta@Sun.COM 			    0, 0, &jobs);
9428295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
9438295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
9448295SSonam.Gupta@Sun.COM 				    "Failed to get job list: %s\n"),
9458295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
9468295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
9478295SSonam.Gupta@Sun.COM 				return (-1);
9488295SSonam.Gupta@Sun.COM 			}
9498295SSonam.Gupta@Sun.COM 
9508295SSonam.Gupta@Sun.COM 			if (jobs != NULL) {
9518295SSonam.Gupta@Sun.COM 				int i;
9522264Sjacobs 
9538295SSonam.Gupta@Sun.COM 				for (i = 0; jobs[i] != NULL; i++)
9549256SSonam.Gupta@Sun.COM 					result += report(printer,
9559256SSonam.Gupta@Sun.COM 					    jobs[i], show_rank,
9569256SSonam.Gupta@Sun.COM 					    verbose);
9578295SSonam.Gupta@Sun.COM 			}
9588295SSonam.Gupta@Sun.COM 
9598295SSonam.Gupta@Sun.COM 			papiJobListFree(jobs);
9608295SSonam.Gupta@Sun.COM 		} else {	/* a job */
9618295SSonam.Gupta@Sun.COM 			papi_job_t job = NULL;
9629331SSonam.Gupta@Sun.COM 			int rid = id;
9638295SSonam.Gupta@Sun.COM 
9648295SSonam.Gupta@Sun.COM 			/* Once a job has been found stop processing */
9658295SSonam.Gupta@Sun.COM 			flag = 0;
9668295SSonam.Gupta@Sun.COM 
9679331SSonam.Gupta@Sun.COM 			/*
9689331SSonam.Gupta@Sun.COM 			 * Job-id could be the job-id requested
9699331SSonam.Gupta@Sun.COM 			 * Check if it is job-id or job-id-requested
9709331SSonam.Gupta@Sun.COM 			 */
9719331SSonam.Gupta@Sun.COM 			id = job_to_be_queried(svc, printer, id);
9729331SSonam.Gupta@Sun.COM 
9739331SSonam.Gupta@Sun.COM 			if (id > 0)
974*9669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, id,
975*9669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
9769331SSonam.Gupta@Sun.COM 			else
977*9669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, rid,
978*9669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
9799331SSonam.Gupta@Sun.COM 
9808295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
9818295SSonam.Gupta@Sun.COM 				if (!print_flag)
9828295SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
9838962SSonam.Gupta@Sun.COM 					    "Failed to get job"\
9848962SSonam.Gupta@Sun.COM 					    " info for %s: %s\n"),
9858962SSonam.Gupta@Sun.COM 					    request,
9868962SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
9878295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
9888295SSonam.Gupta@Sun.COM 				return (-1);
9898295SSonam.Gupta@Sun.COM 			}
9908295SSonam.Gupta@Sun.COM 
9918295SSonam.Gupta@Sun.COM 			if (job != NULL)
9929256SSonam.Gupta@Sun.COM 				result = report(printer, job,
9939256SSonam.Gupta@Sun.COM 				    show_rank, verbose);
9948295SSonam.Gupta@Sun.COM 
9958295SSonam.Gupta@Sun.COM 			papiJobFree(job);
9962264Sjacobs 		}
9972264Sjacobs 
9988295SSonam.Gupta@Sun.COM 		if (flag) {
9998295SSonam.Gupta@Sun.COM 			id = -1;
10008295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
10018295SSonam.Gupta@Sun.COM 			if (id == -1)
10028295SSonam.Gupta@Sun.COM 				flag = 0;
10038295SSonam.Gupta@Sun.COM 			else
10048295SSonam.Gupta@Sun.COM 				flag1 = 1;
10052264Sjacobs 		}
10068295SSonam.Gupta@Sun.COM 	} while (flag);
10072264Sjacobs 
10082264Sjacobs 	papiServiceDestroy(svc);
10092264Sjacobs 
10102264Sjacobs 	return (result);
10112264Sjacobs }
10122264Sjacobs 
10132264Sjacobs static int
10142264Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
10152264Sjacobs {
10162264Sjacobs 	papi_status_t status;
10172264Sjacobs 	char *form = NULL;
10182264Sjacobs 	void *iter = NULL;
10192264Sjacobs 
10202264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
10218962SSonam.Gupta@Sun.COM 	    "form-supported", &form);
10228962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
10238962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter,
10248962SSonam.Gupta@Sun.COM 	    NULL, &form)) {
10252264Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
10262264Sjacobs 			printf(gettext("form %s is available to you\n"), form);
10272264Sjacobs 			if (verbose != 0) {
10282264Sjacobs 				char *detail = NULL;
10292264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
10308962SSonam.Gupta@Sun.COM 				    "form-supported-detail", &detail);
10312264Sjacobs 				if (status == PAPI_OK)
10322264Sjacobs 					printf("%s\n", detail);
10332264Sjacobs 			}
10342264Sjacobs 		}
10352264Sjacobs 	}
10362264Sjacobs 
10372264Sjacobs 	return (0);
10382264Sjacobs }
10392264Sjacobs 
10402264Sjacobs static int
10412264Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
10422264Sjacobs {
10432264Sjacobs 	papi_status_t status;
10442264Sjacobs 	char *pw = NULL;
10452264Sjacobs 	void *iter = NULL;
10462264Sjacobs 
10472264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
10488962SSonam.Gupta@Sun.COM 	    "pw-supported", &pw);
10498962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
10508962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
10512264Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
10522264Sjacobs 			printf(gettext("charset %s is available\n"), pw);
10532264Sjacobs 			if (verbose != 0) {
10542264Sjacobs 				char *info = NULL;
10552264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
10568962SSonam.Gupta@Sun.COM 				    "pw-supported-extra", &info);
10572264Sjacobs 				if (status == PAPI_OK)
10582264Sjacobs 					printf("%s\n", info);
10592264Sjacobs 			}
10602264Sjacobs 		}
10612264Sjacobs 	}
10622264Sjacobs 
10632264Sjacobs 	return (0);
10642264Sjacobs }
10652264Sjacobs 
10662264Sjacobs static int
10672264Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
10682264Sjacobs 		papi_encryption_t encryption, int verbose)
10692264Sjacobs {
10702264Sjacobs 	int result = 0;
10712264Sjacobs 	papi_status_t status;
10722264Sjacobs 	papi_service_t svc = NULL;
10732264Sjacobs 	papi_attribute_t **attrs = NULL;
10742264Sjacobs 
10752264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
10768962SSonam.Gupta@Sun.COM 	    encryption, NULL);
10772264Sjacobs 	if (status != PAPI_OK) {
10782264Sjacobs 		papiServiceDestroy(svc);
10792264Sjacobs 		return (-1);
10802264Sjacobs 	}
10812264Sjacobs 
10822264Sjacobs 	attrs = papiServiceGetAttributeList(svc);
10832264Sjacobs 	if (attrs != NULL) {
10842264Sjacobs 		result = report(name, attrs, verbose);
10852264Sjacobs 
10862264Sjacobs 		if (verbose > 1) {
10872264Sjacobs 			printf("\n");
10882264Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
10892264Sjacobs 			printf("\n");
10902264Sjacobs 		}
10912264Sjacobs 	}
10922264Sjacobs 
10932264Sjacobs 	papiServiceDestroy(svc);
10942264Sjacobs 
10952264Sjacobs 	return (result);
10962264Sjacobs }
10972264Sjacobs 
10982264Sjacobs int
10992264Sjacobs main(int ac, char *av[])
11002264Sjacobs {
11012264Sjacobs 	int exit_code = 0;
11022264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
11032264Sjacobs 	int rank = 0;
11042264Sjacobs 	int verbose = 0;
11052264Sjacobs 	int description = 0;
11062264Sjacobs 	int c;
11072264Sjacobs 	char **argv;
11082264Sjacobs 
11092264Sjacobs 	(void) setlocale(LC_ALL, "");
11102264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
11112264Sjacobs 
11122264Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
11134286Swendyp 	for (c = 0; c < ac; c++) {
11144286Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
11158962SSonam.Gupta@Sun.COM 		    (isalpha(av[c][2]) != 0)) {
11164286Swendyp 			/* preserve old "-l[po...]" behavior */
11174286Swendyp 			argv[c] = &av[c][1];
11184286Swendyp 			argv[c][0] = '-';
11194286Swendyp 			verbose = 1;
11204286Swendyp 
11214286Swendyp 		} else
11224286Swendyp 			argv[c] = av[c];
11234286Swendyp 	}
11244286Swendyp 
11252264Sjacobs 	argv[c++] = "--";
11262264Sjacobs 	ac = c;
11272264Sjacobs 
11282264Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
11292264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
11302264Sjacobs 		switch (c) {
11312264Sjacobs 		case 'l':
11322264Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
11332264Sjacobs 				optarg = "1";
11342264Sjacobs 			verbose = atoi(optarg);
11352264Sjacobs 			break;
11362264Sjacobs 		case 'D':
11372264Sjacobs 			description = 1;
11382264Sjacobs 			break;
11392264Sjacobs 		case 'R':
11402264Sjacobs 			rank = 1;
11412264Sjacobs 			break;
11422264Sjacobs 		case 'E':
11432264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
11442264Sjacobs 			break;
11452264Sjacobs 		default:
11462264Sjacobs 			break;
11472264Sjacobs 		}
11482264Sjacobs 	optind = 1;
11492264Sjacobs 
11502264Sjacobs 	/* process command line arguments */
11512264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
11522264Sjacobs 		switch (c) {	/* these may or may not have an option */
11532264Sjacobs 		case 'a':
11542264Sjacobs 		case 'c':
11552264Sjacobs 		case 'p':
11562264Sjacobs 		case 'o':
11572264Sjacobs 		case 'R':
11582264Sjacobs 		case 'u':
11592264Sjacobs 		case 'v':
11602264Sjacobs 		case 'l':
11612264Sjacobs 		case 'f':
11622264Sjacobs 		case 'S':
11632264Sjacobs 			if (optarg[0] == '-') {
11642264Sjacobs 				/* this check stop a possible infinite loop */
11652264Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
11662264Sjacobs 					optind--;
11672264Sjacobs 				optarg = NULL;
11682264Sjacobs 			} else if (strcmp(optarg, "all") == 0)
11692264Sjacobs 				optarg = NULL;
11702264Sjacobs 		}
11712264Sjacobs 
11722264Sjacobs 		switch (c) {
11732264Sjacobs 		case 'a':
11742264Sjacobs 			exit_code += printer_query(optarg, report_accepting,
11758962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
11762264Sjacobs 			break;
11772264Sjacobs 		case 'c':
11782264Sjacobs 			exit_code += printer_query(optarg, report_class,
11798962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
11802264Sjacobs 			break;
11812264Sjacobs 		case 'p':
11822264Sjacobs 			exit_code += printer_query(optarg, report_printer,
11838962SSonam.Gupta@Sun.COM 			    encryption, verbose, description);
11842264Sjacobs 			break;
11852264Sjacobs 		case 'd':
11862264Sjacobs 			exit_code += lpstat_default_printer(encryption);
11872264Sjacobs 			break;
11882264Sjacobs 		case 'r':
11892264Sjacobs 			exit_code += lpstat_service_status(encryption);
11902264Sjacobs 			break;
11912264Sjacobs 		case 'u':
11922264Sjacobs 			if (optarg != NULL)
11932264Sjacobs 				users = strsplit(optarg, ", \n");
11942264Sjacobs 			exit_code += job_query(NULL, report_job,
11958962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
11962264Sjacobs 			if (users != NULL) {
11972264Sjacobs 				free(users);
11982264Sjacobs 				users = NULL;
11992264Sjacobs 			}
12002264Sjacobs 			break;
12012264Sjacobs 		case 'v':
12022264Sjacobs 			exit_code += printer_query(optarg, report_device,
12038962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12042264Sjacobs 			break;
12058604SGowtham.Thommandra@Sun.COM 		case 'R':	/* set "rank" flag in first pass */
12062264Sjacobs 		case 'o':
12072264Sjacobs 			exit_code += job_query(optarg, report_job,
12088962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
12092264Sjacobs 			break;
12102264Sjacobs 		case 'f':
12112264Sjacobs 			exit_code += service_query(optarg, report_form,
12128962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12132264Sjacobs 			break;
12142264Sjacobs 		case 'S':
12152264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
12168962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12172264Sjacobs 			break;
12182264Sjacobs 		case 's':
12192264Sjacobs 			exit_code += lpstat_service_status(encryption);
12202264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12212264Sjacobs 			exit_code += printer_query(NULL, report_class,
12228962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12232264Sjacobs 			exit_code += printer_query(NULL, report_device,
12248962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12252264Sjacobs 			exit_code += service_query(optarg, report_form,
12268962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12272264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
12288962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12292264Sjacobs 			break;
12302264Sjacobs 		case 't':
12312264Sjacobs 			exit_code += lpstat_service_status(encryption);
12322264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12332264Sjacobs 			exit_code += printer_query(NULL, report_class,
12348962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12352264Sjacobs 			exit_code += printer_query(NULL, report_device,
12368962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12372264Sjacobs 			exit_code += printer_query(NULL, report_accepting,
12388962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12392264Sjacobs 			exit_code += printer_query(NULL, report_printer,
12408962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12412264Sjacobs 			exit_code += service_query(optarg, report_form,
12428962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12432264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
12448962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12452264Sjacobs 			exit_code += job_query(NULL, report_job,
12468962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
12472264Sjacobs 			break;
12482264Sjacobs 		case 'L':	/* local-only, ignored */
12492264Sjacobs 		case 'l':	/* increased verbose level in first pass */
12502264Sjacobs 		case 'D':	/* set "description" flag in first pass */
12512264Sjacobs 		case 'E':	/* set encryption in the first pass */
12522264Sjacobs 			break;
12532264Sjacobs 		default:
12542264Sjacobs 			usage(av[0]);
12552264Sjacobs 		}
12562264Sjacobs 	}
12572264Sjacobs 	ac--;
12582264Sjacobs 
12592264Sjacobs 	if (ac == 1) {	/* report on my jobs */
12602264Sjacobs 		struct passwd *pw = getpwuid(getuid());
12612264Sjacobs 
12622264Sjacobs 		if (pw != NULL)
12632264Sjacobs 			users = strsplit(pw->pw_name, "");
12642264Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
12658962SSonam.Gupta@Sun.COM 		    rank, verbose);
12662264Sjacobs 		if (users != NULL) {
12672264Sjacobs 			free(users);
12682264Sjacobs 			users = NULL;
12692264Sjacobs 		}
12702264Sjacobs 	} else {
12712264Sjacobs 		for (c = optind; c < ac; c++)
12722264Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
12738962SSonam.Gupta@Sun.COM 			    rank, verbose);
12742264Sjacobs 	}
12752264Sjacobs 
12762264Sjacobs 
12772264Sjacobs 	if (exit_code != 0)
12782264Sjacobs 		exit_code = 1;
12792264Sjacobs 
12802264Sjacobs 	return (exit_code);
12812264Sjacobs }
1282