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"
429669SGowtham.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 	papi_status_t status;
1172264Sjacobs 	papi_service_t svc = NULL;
1182264Sjacobs 	char *name = NULL;
1192264Sjacobs 
1202264Sjacobs 	if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
1212264Sjacobs 	    ((name = getenv("IPP_SERVER")) == NULL) &&
1222264Sjacobs 	    ((name = getenv("CUPS_SERVER")) == NULL))
1232264Sjacobs 		name = DEFAULT_SERVICE_URI;
1242264Sjacobs 
1252264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1268962SSonam.Gupta@Sun.COM 	    encryption, NULL);
1272264Sjacobs 	if (status != PAPI_OK) {
1282264Sjacobs 		printf(gettext("scheduler is not running\n"));
1292264Sjacobs 	} else
1302264Sjacobs 		printf(gettext("scheduler is running\n"));
1312264Sjacobs 	papiServiceDestroy(svc);
1322264Sjacobs 
133*9852SGowtham.Thommandra@Sun.COM 	return (0);
1342264Sjacobs }
1352264Sjacobs 
1362264Sjacobs static char *
1372264Sjacobs get_device_uri(papi_service_t svc, char *name)
1382264Sjacobs {
1392264Sjacobs 	papi_status_t status;
1402264Sjacobs 	papi_printer_t p = NULL;
1412264Sjacobs 	char *keys[] = { "device-uri", NULL };
1422264Sjacobs 	char *result = NULL;
1432264Sjacobs 
1442264Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
1452264Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
1462264Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
1472264Sjacobs 
1482264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
1498962SSonam.Gupta@Sun.COM 		    "device-uri", &result);
1502264Sjacobs 		if (result != NULL)
1512264Sjacobs 			result = strdup(result);
1522264Sjacobs 
1532264Sjacobs 		papiPrinterFree(p);
1542264Sjacobs 	}
1552264Sjacobs 
1562264Sjacobs 	return (result);
1572264Sjacobs }
1582264Sjacobs 
1598534SSonam.Gupta@Sun.COM static void
1608534SSonam.Gupta@Sun.COM print_description(papi_attribute_t **list, char *printer_name)
1618534SSonam.Gupta@Sun.COM {
1628534SSonam.Gupta@Sun.COM 	char *str = "";
1638534SSonam.Gupta@Sun.COM 
1648534SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(list, NULL,
1658534SSonam.Gupta@Sun.COM 	    "printer-info", &str);
1668534SSonam.Gupta@Sun.COM 
1678534SSonam.Gupta@Sun.COM 	/*
1688534SSonam.Gupta@Sun.COM 	 * If no printer-info is read then
1698534SSonam.Gupta@Sun.COM 	 * by default the printer-info is <printer-name>@<server>
1708534SSonam.Gupta@Sun.COM 	 */
1718534SSonam.Gupta@Sun.COM 	if (str[0] == '\0') {
1728534SSonam.Gupta@Sun.COM 		char *uri = NULL;
1738534SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
1748534SSonam.Gupta@Sun.COM 
1758534SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(list, NULL,
1768534SSonam.Gupta@Sun.COM 		    "printer-uri-supported", &uri);
1778534SSonam.Gupta@Sun.COM 
1788534SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
1798534SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
1808534SSonam.Gupta@Sun.COM 
1818534SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
1828534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
1838534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0))
1848534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription:\n"));
1858534SSonam.Gupta@Sun.COM 			else
1868534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription: %s@%s\n"),
1878534SSonam.Gupta@Sun.COM 				    printer_name, u->host);
1888534SSonam.Gupta@Sun.COM 
1898534SSonam.Gupta@Sun.COM 			uri_free(u);
1908534SSonam.Gupta@Sun.COM 		} else
1918534SSonam.Gupta@Sun.COM 			printf(gettext("\tDescription:\n"));
1928534SSonam.Gupta@Sun.COM 	} else
1938534SSonam.Gupta@Sun.COM 		printf(gettext("\tDescription: %s\n"), str);
1948534SSonam.Gupta@Sun.COM }
1958534SSonam.Gupta@Sun.COM 
1962264Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
1972264Sjacobs 					NULL };
1982264Sjacobs /* ARGSUSED2 */
1992264Sjacobs static int
2002264Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
2012264Sjacobs 		int verbose, int description)
2022264Sjacobs {
2032264Sjacobs 	papi_status_t status;
2042264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2052264Sjacobs 	char *uri = NULL;
2062264Sjacobs 	char *device = NULL;
2072264Sjacobs 	uri_t *u = NULL;
2082264Sjacobs 
2092264Sjacobs 	if (name == NULL) {
2102264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2118962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
2122264Sjacobs 		if (status != PAPI_OK)
2132264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2148962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
2152264Sjacobs 	}
2162264Sjacobs 
2172264Sjacobs 	if (name == NULL)
2182264Sjacobs 		return (-1);
2192264Sjacobs 
2202264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
2218962SSonam.Gupta@Sun.COM 	    "printer-uri-supported", &uri);
2222264Sjacobs 
2232264Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
2242264Sjacobs 		char *nodename = localhostname();
2252264Sjacobs 
2262264Sjacobs 		if ((u->host == NULL) ||
2272264Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
2282264Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
2292264Sjacobs 			device = get_device_uri(svc, name);
2302264Sjacobs 
2312264Sjacobs 		if (device != NULL) {
2322264Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
2332264Sjacobs 			return (0);
2342264Sjacobs 		} else if (uri != NULL) {
2352264Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
2369411SKeerthi.Kondaka@Sun.COM 			    u->host?u->host:"localhost", uri);
2372264Sjacobs 			return (0);
2382264Sjacobs 		}
2392264Sjacobs 
2402264Sjacobs 		uri_free(u);
2412264Sjacobs 	}
2422264Sjacobs 
2432264Sjacobs 	return (0);
2442264Sjacobs }
2452264Sjacobs 
2462264Sjacobs static char *report_accepting_keys[] = { "printer-name",
2472264Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
2482264Sjacobs 			"printer-up-time", "printer-state-time",
2492264Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
2502264Sjacobs /* ARGSUSED2 */
2512264Sjacobs static int
2522264Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
2532264Sjacobs 		int verbose, int description)
2542264Sjacobs {
2552264Sjacobs 	papi_status_t status;
2562264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2572264Sjacobs 	time_t curr;
2582264Sjacobs 	char boolean = PAPI_FALSE;
2592264Sjacobs 
2602264Sjacobs 	if (name == NULL) {
2612264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2628962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
2632264Sjacobs 		if (status != PAPI_OK)
2642264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2658962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
2662264Sjacobs 	}
2672264Sjacobs 	if (name == NULL)
2682264Sjacobs 		return (-1);
2692264Sjacobs 
2702264Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
2718962SSonam.Gupta@Sun.COM 	    "printer-is-accepting-jobs", &boolean);
2722264Sjacobs 	(void) time(&curr);
2732264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2748962SSonam.Gupta@Sun.COM 	    "printer-up-time", &curr);
2752264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2768962SSonam.Gupta@Sun.COM 	    "printer-state-time", &curr);
2772264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2788962SSonam.Gupta@Sun.COM 	    "lpsched-reject-date", &curr);
2792264Sjacobs 
2802264Sjacobs 	if (boolean == PAPI_TRUE) {
2812264Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
2828962SSonam.Gupta@Sun.COM 		    name, nctime(&curr));
2832264Sjacobs 	} else {
2842264Sjacobs 		char *reason = "unknown reason";
2852264Sjacobs 
2862264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
2878962SSonam.Gupta@Sun.COM 		    "lpsched-reject-reason", &reason);
2882264Sjacobs 
2892264Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
2908962SSonam.Gupta@Sun.COM 		    name, nctime(&curr), reason);
2912264Sjacobs 	}
2922264Sjacobs 
2932264Sjacobs 	return (0);
2942264Sjacobs }
2952264Sjacobs 
2962264Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
2972264Sjacobs 					"member-names", NULL };
2982264Sjacobs /* ARGSUSED2 */
2992264Sjacobs static int
3002264Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
3012264Sjacobs 		int verbose, int description)
3022264Sjacobs {
3032264Sjacobs 	papi_status_t status;
3042264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3052264Sjacobs 	char *member = NULL;
3062264Sjacobs 	void *iter = NULL;
3072264Sjacobs 
3082264Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
3098962SSonam.Gupta@Sun.COM 	    "member-names", &member);
3102264Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
3112264Sjacobs 		return (0);
3122264Sjacobs 
3132264Sjacobs 	if (name == NULL) {
3142264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3158962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
3162264Sjacobs 		if (status != PAPI_OK)
3172264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3188962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
3192264Sjacobs 	}
3202264Sjacobs 	if (name == NULL)
3212264Sjacobs 		return (-1);
3222264Sjacobs 
3232264Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
3242264Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
3258962SSonam.Gupta@Sun.COM 	    == PAPI_OK)
3262264Sjacobs 		printf("\t%s\n", member);
3272264Sjacobs 
3282264Sjacobs 	return (0);
3292264Sjacobs }
3302264Sjacobs 
3319810SKeerthi.Kondaka@Sun.COM static int
3329810SKeerthi.Kondaka@Sun.COM get_remote_hostname(papi_attribute_t **attrs, char **host)
3339810SKeerthi.Kondaka@Sun.COM {
3349810SKeerthi.Kondaka@Sun.COM 	char *uri = NULL;
3359810SKeerthi.Kondaka@Sun.COM 	uri_t *u;
3369810SKeerthi.Kondaka@Sun.COM 	char *nodename;
3379810SKeerthi.Kondaka@Sun.COM 
3389810SKeerthi.Kondaka@Sun.COM 	*host = NULL;
3399810SKeerthi.Kondaka@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
3409810SKeerthi.Kondaka@Sun.COM 	    "job-originating-host-name", host);
3419810SKeerthi.Kondaka@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
3429810SKeerthi.Kondaka@Sun.COM 	    "printer-uri-supported", &uri);
3439810SKeerthi.Kondaka@Sun.COM 	if (*host == NULL) {
3449810SKeerthi.Kondaka@Sun.COM 		if (uri != NULL) {
3459810SKeerthi.Kondaka@Sun.COM 			if (uri_from_string(uri, &u) == 0) {
3469810SKeerthi.Kondaka@Sun.COM 				if (u->host == NULL) {
3479810SKeerthi.Kondaka@Sun.COM 					uri_free(u);
3489810SKeerthi.Kondaka@Sun.COM 					return (0);
3499810SKeerthi.Kondaka@Sun.COM 				}
3509810SKeerthi.Kondaka@Sun.COM 				*host = strdup(u->host);
3519810SKeerthi.Kondaka@Sun.COM 				uri_free(u);
3529810SKeerthi.Kondaka@Sun.COM 			} else {
3539810SKeerthi.Kondaka@Sun.COM 				return (0);
3549810SKeerthi.Kondaka@Sun.COM 			}
3559810SKeerthi.Kondaka@Sun.COM 		} else {
3569810SKeerthi.Kondaka@Sun.COM 			return (0);
3579810SKeerthi.Kondaka@Sun.COM 		}
3589810SKeerthi.Kondaka@Sun.COM 	}
3599810SKeerthi.Kondaka@Sun.COM 	nodename = localhostname();
3609810SKeerthi.Kondaka@Sun.COM 	if ((strcasecmp(*host, "localhost") == 0) ||
3619810SKeerthi.Kondaka@Sun.COM 	    (strcasecmp(*host, nodename) == 0)) {
3629810SKeerthi.Kondaka@Sun.COM 		return (0);
3639810SKeerthi.Kondaka@Sun.COM 	}
3649810SKeerthi.Kondaka@Sun.COM 	return (1);
3659810SKeerthi.Kondaka@Sun.COM }
3669810SKeerthi.Kondaka@Sun.COM 
3672264Sjacobs static char *report_printer_keys[] = { "printer-name",
3682264Sjacobs 			"printer-uri-supported", "printer-state",
3692264Sjacobs 			"printer-up-time", "printer-state-time",
3702264Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
3712264Sjacobs 			"lpsched-disable-reason", NULL };
3722264Sjacobs /* ARGSUSED2 */
3732264Sjacobs static int
3742264Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
3752264Sjacobs 		int verbose, int description)
3762264Sjacobs {
3772264Sjacobs 	papi_status_t status;
3782264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3792264Sjacobs 	time_t curr;
3802264Sjacobs 	int32_t pstat = 0;
3812264Sjacobs 	char *member = NULL;
3828962SSonam.Gupta@Sun.COM 	papi_job_t *j = NULL;
3832264Sjacobs 
3842264Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
3858962SSonam.Gupta@Sun.COM 	    "member-names", &member);
3862264Sjacobs 	if (status == PAPI_OK)	/* it's a class */
3872264Sjacobs 		return (0);
3882264Sjacobs 
3892264Sjacobs 	if (name == NULL) {
3902264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3918962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
3922264Sjacobs 		if (status != PAPI_OK)
3932264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3948962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
3952264Sjacobs 	}
3962264Sjacobs 	if (name == NULL)
3972264Sjacobs 		return (-1);
3982264Sjacobs 
3992264Sjacobs 	printf(gettext("printer %s "), name);
4002264Sjacobs 
4012264Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
4028962SSonam.Gupta@Sun.COM 	    "printer-state", &pstat);
4032264Sjacobs 
4042264Sjacobs 	switch (pstat) {
4052264Sjacobs 	case 0x03:	/* idle */
4062264Sjacobs 		printf(gettext("idle. enabled"));
4072264Sjacobs 		break;
4088962SSonam.Gupta@Sun.COM 	case 0x04: /* processing */
4098962SSonam.Gupta@Sun.COM 		status = papiPrinterListJobs(svc, name, NULL,
4108962SSonam.Gupta@Sun.COM 		    0, 0, &j);
4118962SSonam.Gupta@Sun.COM 
4128962SSonam.Gupta@Sun.COM 		if (status == PAPI_OK) {
4138962SSonam.Gupta@Sun.COM 			if (j != NULL) {
4148962SSonam.Gupta@Sun.COM 				int i = 0;
4158962SSonam.Gupta@Sun.COM 				int32_t jobid = 0;
4168962SSonam.Gupta@Sun.COM 				int32_t jstate = 0;
4178962SSonam.Gupta@Sun.COM 				int flag = 0;
4188962SSonam.Gupta@Sun.COM 
4198962SSonam.Gupta@Sun.COM 				for (i = 0; j[i] != NULL; ++i) {
4209206SSonam.Gupta@Sun.COM 					papi_attribute_t **attr =
4219206SSonam.Gupta@Sun.COM 					    papiJobGetAttributeList(j[i]);
4228962SSonam.Gupta@Sun.COM 
4239206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
4248962SSonam.Gupta@Sun.COM 					    NULL, "job-state", &jstate);
4259206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
4268962SSonam.Gupta@Sun.COM 					    NULL, "job-id", &jobid);
4272264Sjacobs 
4288962SSonam.Gupta@Sun.COM 					/*
4299257SGowtham.Thommandra@Sun.COM 					 * If the job-state is in
4309257SGowtham.Thommandra@Sun.COM 					 * RS_PRINTING then only print.
4318962SSonam.Gupta@Sun.COM 					 */
4329257SGowtham.Thommandra@Sun.COM 					if (jstate == 0x0008) {
4338962SSonam.Gupta@Sun.COM 						if (flag == 0)
4348962SSonam.Gupta@Sun.COM 							printf(gettext
4358962SSonam.Gupta@Sun.COM 							    ("now printing"\
4368962SSonam.Gupta@Sun.COM 							    " %s-%d. enabled"),
4378962SSonam.Gupta@Sun.COM 							    name, jobid);
4388962SSonam.Gupta@Sun.COM 						flag = 1;
4398962SSonam.Gupta@Sun.COM 					}
4408962SSonam.Gupta@Sun.COM 				}
4418962SSonam.Gupta@Sun.COM 				papiJobListFree(j);
4428962SSonam.Gupta@Sun.COM 			}
4432264Sjacobs 		}
4442264Sjacobs 		break;
4452264Sjacobs 	case 0x05:	/* stopped */
4462264Sjacobs 		printf(gettext("disabled"));
4472264Sjacobs 		break;
4482264Sjacobs 	default:
4492264Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
4502264Sjacobs 		break;
4512264Sjacobs 	}
4522264Sjacobs 
4532264Sjacobs 	(void) time(&curr);
4542264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4558962SSonam.Gupta@Sun.COM 	    "printer-up-time", &curr);
4562264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4578962SSonam.Gupta@Sun.COM 	    "printer-state-time", &curr);
4582264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4598962SSonam.Gupta@Sun.COM 	    "lpsched-disable-date", &curr);
4602264Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
4612264Sjacobs 
4622264Sjacobs 	if (pstat == 0x05) {
4632264Sjacobs 		char *reason = "unknown reason";
4642264Sjacobs 
4652264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4668962SSonam.Gupta@Sun.COM 		    "printer-state-reasons", &reason);
4672264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4688962SSonam.Gupta@Sun.COM 		    "lpsched-disable-reason", &reason);
4692264Sjacobs 		printf(gettext("\t%s\n"), reason);
4702264Sjacobs 	}
4712264Sjacobs 
4722264Sjacobs 	if (verbose == 1) {
4732264Sjacobs 		void *iter;
4742264Sjacobs 		char *str;
4759810SKeerthi.Kondaka@Sun.COM 		char *host = NULL;
4762264Sjacobs 
4779810SKeerthi.Kondaka@Sun.COM 		if ((get_remote_hostname(attrs, &host)) != 0) {
4789810SKeerthi.Kondaka@Sun.COM 			(void) printf(
4799810SKeerthi.Kondaka@Sun.COM 			    gettext("\tRemote Name: %s\n\tRemote Server: "
4809810SKeerthi.Kondaka@Sun.COM 			    "%s\n"), name, host);
4819810SKeerthi.Kondaka@Sun.COM 			free(host);
4829810SKeerthi.Kondaka@Sun.COM 			return (0);
4839810SKeerthi.Kondaka@Sun.COM 		}
4842264Sjacobs 		str = "";
4852264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4868962SSonam.Gupta@Sun.COM 		    "form-ready", &str);
4872264Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
4882264Sjacobs 
4892264Sjacobs 		str = "";
4902264Sjacobs 		iter = NULL;
4912264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4928962SSonam.Gupta@Sun.COM 		    "document-format-supported", &str);
4932264Sjacobs 		printf(gettext("\tContent types: %s"), str);
4942264Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
4958962SSonam.Gupta@Sun.COM 		    == PAPI_OK)
4962264Sjacobs 			printf(", %s", str);
4972264Sjacobs 		printf("\n");
4982264Sjacobs 
4998534SSonam.Gupta@Sun.COM 		/* Display the printer description */
5008534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
5012264Sjacobs 
5022264Sjacobs 		str = "";
5036676Swendyp 		iter = NULL;
5046676Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
5056676Swendyp 		    "lpsched-printer-type", &str);
5066676Swendyp 		printf(gettext("\tPrinter types: %s"), str);
5076676Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
5086676Swendyp 		    == PAPI_OK)
5096676Swendyp 			printf(", %s", str);
5106676Swendyp 		printf("\n");
5116676Swendyp 
5126676Swendyp 		str = "";
5132264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5148962SSonam.Gupta@Sun.COM 		    "lpsched-dial-info", &str);
5152264Sjacobs 		printf(gettext("\tConnection: %s\n"),
5167653SJonathan.Ca@Sun.COM 		    ((str[0] == '\0') ? gettext("direct") : str));
5172264Sjacobs 
5182264Sjacobs 		str = "";
5192264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5208962SSonam.Gupta@Sun.COM 		    "lpsched-interface-script", &str);
5212264Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
5222264Sjacobs 
5232264Sjacobs 		str = NULL;
5242264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5258962SSonam.Gupta@Sun.COM 		    "ppd-file-uri", &str);
5262264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5278962SSonam.Gupta@Sun.COM 		    "lpsched-ppd-source-path", &str);
5282264Sjacobs 		if (str != NULL)
5292264Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
5302264Sjacobs 
5312264Sjacobs 		str = NULL;
5322264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5338962SSonam.Gupta@Sun.COM 		    "lpsched-fault-alert-command", &str);
5342264Sjacobs 		if (str != NULL)
5352264Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
5362264Sjacobs 
5372264Sjacobs 		str = "";
5382264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5398962SSonam.Gupta@Sun.COM 		    "lpsched-fault-recovery", &str);
5402264Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
5418962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("continue") : str));
5422264Sjacobs 
5432264Sjacobs 		str = "(all)";
5442264Sjacobs 		iter = NULL;
5452264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5468962SSonam.Gupta@Sun.COM 		    "requesting-user-name-allowed", &str);
5472264Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
5488962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5492264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5502264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5518962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5522264Sjacobs 				printf("\t\t%s\n", str);
5532264Sjacobs 
5542264Sjacobs 		str = NULL;
5552264Sjacobs 		iter = NULL;
5562264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5578962SSonam.Gupta@Sun.COM 		    "requesting-user-name-denied", &str);
5582264Sjacobs 		if (str != NULL) {
5592264Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
5608962SSonam.Gupta@Sun.COM 			    ((str[0] == '\0') ? gettext("(none)") : str));
5612264Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
5622264Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
5638962SSonam.Gupta@Sun.COM 				    NULL, &str) == PAPI_OK)
5642264Sjacobs 					printf("\t\t%s\n", str);
5652264Sjacobs 		}
5662264Sjacobs 
5678238SSonam.Gupta@Sun.COM 		str = "none";
5682264Sjacobs 		iter = NULL;
5692264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5708962SSonam.Gupta@Sun.COM 		    "form-supported", &str);
5718238SSonam.Gupta@Sun.COM 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
5728962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("none") : str));
5732264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5742264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5758962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5768238SSonam.Gupta@Sun.COM 				printf("\t\t(%s)\n", str);
5772264Sjacobs 
5782264Sjacobs 		str = "";
5792264Sjacobs 		iter = NULL;
5802264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5818962SSonam.Gupta@Sun.COM 		    "media-supported", &str);
5822264Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
5838962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5842264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5852264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5868962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5872264Sjacobs 				printf("\t\t%s\n", str);
5882264Sjacobs 
5892264Sjacobs 		str = "";
5902264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5918962SSonam.Gupta@Sun.COM 		    "job-sheets-supported", &str);
5926676Swendyp 		if ((strcasecmp(str, "none")) == 0)
5936676Swendyp 			str = gettext("page never printed");
5946676Swendyp 		else if (strcasecmp(str, "optional") == 0)
5956676Swendyp 			str = gettext("not required");
5966676Swendyp 		else
5976676Swendyp 			str = gettext("required");
5986676Swendyp 
5996676Swendyp 		printf(gettext("\tBanner %s\n"), str);
6006676Swendyp 
6012264Sjacobs 
6022264Sjacobs 		str = "";
6032264Sjacobs 		iter = NULL;
6042264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6058962SSonam.Gupta@Sun.COM 		    "lpsched-print-wheels", &str);
6062264Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
6078962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
6082264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
6092264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6108962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6112264Sjacobs 				printf("\t\t%s\n", str);
6122264Sjacobs 
6132264Sjacobs 		printf(gettext("\tDefault pitch:\n"));
6142264Sjacobs 		printf(gettext("\tDefault page size:\n"));
6152264Sjacobs 		printf(gettext("\tDefault port setting:\n"));
6162264Sjacobs 
6172264Sjacobs 		str = "";
6182264Sjacobs 		iter = NULL;
6192264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6208962SSonam.Gupta@Sun.COM 		    "lpsched-options", &str);
6212264Sjacobs 		if (str != NULL) {
6222264Sjacobs 			printf(gettext("\tOptions: %s"), str);
6232264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6248962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6252264Sjacobs 				printf(", %s", str);
6262264Sjacobs 			printf("\n");
6272264Sjacobs 		}
6282264Sjacobs 
6298534SSonam.Gupta@Sun.COM 	} else if (description == 1)
6308534SSonam.Gupta@Sun.COM 		/* Display printer description */
6318534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
6328534SSonam.Gupta@Sun.COM 	else if (verbose > 1)
6332264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
6342264Sjacobs 
6352264Sjacobs 	if (verbose > 0)
6362264Sjacobs 		printf("\n");
6372264Sjacobs 
6382264Sjacobs 	return (0);
6392264Sjacobs }
6402264Sjacobs 
6412264Sjacobs static int
6422264Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
6432264Sjacobs 					int, int), papi_encryption_t encryption,
6442264Sjacobs 		int verbose, int description)
6452264Sjacobs {
6469669SGowtham.Thommandra@Sun.COM 	int result = 0, i = 0;
6472264Sjacobs 	papi_status_t status;
6482264Sjacobs 	papi_service_t svc = NULL;
6499669SGowtham.Thommandra@Sun.COM 	char **list = getlist(name, LP_WS, LP_SEP);
6502264Sjacobs 
6519669SGowtham.Thommandra@Sun.COM 	if (list == NULL) {
6529669SGowtham.Thommandra@Sun.COM 		list = (char **)malloc(sizeof (char *));
6539669SGowtham.Thommandra@Sun.COM 		list[0] = name;
6542264Sjacobs 	}
6552264Sjacobs 
6569669SGowtham.Thommandra@Sun.COM 	/*
6579669SGowtham.Thommandra@Sun.COM 	 * The for loop executes once for every printer
6589669SGowtham.Thommandra@Sun.COM 	 * entry in list. If list is NULL that implies
6599669SGowtham.Thommandra@Sun.COM 	 * name is also NULL, the loop runs only one time.
6609669SGowtham.Thommandra@Sun.COM 	 */
6612264Sjacobs 
6629669SGowtham.Thommandra@Sun.COM 	for (i = 0; name == NULL || list[i] != NULL; i++) {
6639669SGowtham.Thommandra@Sun.COM 		name = list[i];
6642264Sjacobs 
6659669SGowtham.Thommandra@Sun.COM 		status = papiServiceCreate(&svc, name, NULL, NULL,
6669669SGowtham.Thommandra@Sun.COM 		    cli_auth_callback, encryption, NULL);
6672264Sjacobs 		if (status != PAPI_OK) {
6689669SGowtham.Thommandra@Sun.COM 			if (status == PAPI_NOT_FOUND)
6699669SGowtham.Thommandra@Sun.COM 				fprintf(stderr,
6709669SGowtham.Thommandra@Sun.COM 				    gettext("%s: unknown printer\n"),
6719669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)");
6729669SGowtham.Thommandra@Sun.COM 			else
6739669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
6749669SGowtham.Thommandra@Sun.COM 				    "Failed to contact service for %s: %s\n"),
6759669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)",
6769669SGowtham.Thommandra@Sun.COM 				    verbose_papi_message(svc, status));
6772264Sjacobs 			papiServiceDestroy(svc);
6789669SGowtham.Thommandra@Sun.COM 			result--;
6799669SGowtham.Thommandra@Sun.COM 			continue;
6802264Sjacobs 		}
6812264Sjacobs 
6829669SGowtham.Thommandra@Sun.COM 		if (name == NULL) { /* all */
6839669SGowtham.Thommandra@Sun.COM 			char **interest = interest_list(svc);
6849669SGowtham.Thommandra@Sun.COM 
6859669SGowtham.Thommandra@Sun.COM 			if (interest != NULL) {
6869669SGowtham.Thommandra@Sun.COM 				int i;
6879669SGowtham.Thommandra@Sun.COM 
6889669SGowtham.Thommandra@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
6899669SGowtham.Thommandra@Sun.COM 					result += printer_query(interest[i],
6909669SGowtham.Thommandra@Sun.COM 					    report, encryption, verbose,
6919669SGowtham.Thommandra@Sun.COM 					    description);
6929669SGowtham.Thommandra@Sun.COM 			}
6939669SGowtham.Thommandra@Sun.COM 		} else {
6949669SGowtham.Thommandra@Sun.COM 			papi_printer_t printer = NULL;
6959669SGowtham.Thommandra@Sun.COM 			char **keys = NULL;
6962264Sjacobs 
6979669SGowtham.Thommandra@Sun.COM 			/*
6989669SGowtham.Thommandra@Sun.COM 			 * Limit the query to only required data
6999669SGowtham.Thommandra@Sun.COM 			 * to reduce the need to go remote for
7009669SGowtham.Thommandra@Sun.COM 			 * information.
7019669SGowtham.Thommandra@Sun.COM 			 */
7029669SGowtham.Thommandra@Sun.COM 			if (report == report_device)
7039669SGowtham.Thommandra@Sun.COM 				keys = report_device_keys;
7049669SGowtham.Thommandra@Sun.COM 			else if (report == report_class)
7059669SGowtham.Thommandra@Sun.COM 				keys = report_class_keys;
7069669SGowtham.Thommandra@Sun.COM 			else if (report == report_accepting)
7079669SGowtham.Thommandra@Sun.COM 				keys = report_accepting_keys;
7089669SGowtham.Thommandra@Sun.COM 			else if ((report == report_printer) && (verbose == 0))
7099669SGowtham.Thommandra@Sun.COM 				keys = report_printer_keys;
7109669SGowtham.Thommandra@Sun.COM 
7119669SGowtham.Thommandra@Sun.COM 			status = papiPrinterQuery(svc, name, keys,
7129669SGowtham.Thommandra@Sun.COM 			    NULL, &printer);
7139669SGowtham.Thommandra@Sun.COM 			if (status != PAPI_OK) {
7149669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
7159669SGowtham.Thommandra@Sun.COM 				    "Failed to get printer info for %s: %s\n"),
7169669SGowtham.Thommandra@Sun.COM 				    name, verbose_papi_message(svc, status));
7179669SGowtham.Thommandra@Sun.COM 				papiServiceDestroy(svc);
7189669SGowtham.Thommandra@Sun.COM 				result--;
7199669SGowtham.Thommandra@Sun.COM 				continue;
7209669SGowtham.Thommandra@Sun.COM 			}
7219669SGowtham.Thommandra@Sun.COM 
7229669SGowtham.Thommandra@Sun.COM 			if (printer != NULL)
7239669SGowtham.Thommandra@Sun.COM 				result += report(svc, name, printer, verbose,
7249669SGowtham.Thommandra@Sun.COM 				    description);
7259669SGowtham.Thommandra@Sun.COM 
7269669SGowtham.Thommandra@Sun.COM 			papiPrinterFree(printer);
7279669SGowtham.Thommandra@Sun.COM 		}
7289669SGowtham.Thommandra@Sun.COM 
7299669SGowtham.Thommandra@Sun.COM 		papiServiceDestroy(svc);
7309669SGowtham.Thommandra@Sun.COM 
7319669SGowtham.Thommandra@Sun.COM 		if (name == NULL)
7329669SGowtham.Thommandra@Sun.COM 			break;
7332264Sjacobs 	}
7342264Sjacobs 
7359669SGowtham.Thommandra@Sun.COM 	freelist(list);
7362264Sjacobs 
7372264Sjacobs 	return (result);
7382264Sjacobs }
7392264Sjacobs 
7402264Sjacobs static int
7412264Sjacobs match_user(char *user, char **list)
7422264Sjacobs {
7432264Sjacobs 	int i;
7442264Sjacobs 
7452264Sjacobs 	for (i = 0; list[i] != NULL; i++) {
7462264Sjacobs 		if (strcmp(user, list[i]) == 0)
7472264Sjacobs 			return (0);
7482264Sjacobs 	}
7492264Sjacobs 
7502264Sjacobs 	return (-1);
7512264Sjacobs }
7522264Sjacobs 
7532264Sjacobs static char **users = NULL;
7542264Sjacobs 
7552264Sjacobs static int
7569256SSonam.Gupta@Sun.COM report_job(char *printer, papi_job_t job, int show_rank, int verbose)
7572264Sjacobs {
7582264Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
7592264Sjacobs 	time_t clock = 0;
7602264Sjacobs 	char date[24];
7612264Sjacobs 	char request[26];
7622264Sjacobs 	char *user = "unknown";
7638321SSonam.Gupta@Sun.COM 	char *host = NULL;
7642264Sjacobs 	int32_t size = 0;
7652264Sjacobs 	int32_t jstate = 0;
7668321SSonam.Gupta@Sun.COM 	char User[50];
7672264Sjacobs 
7682264Sjacobs 	char *destination = "unknown";
7692264Sjacobs 	int32_t id = -1;
7709152SSonam.Gupta@Sun.COM 	static int check = 0;
7719152SSonam.Gupta@Sun.COM 	static char *uri = NULL;
7722264Sjacobs 
7732264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
7748962SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
7752264Sjacobs 
7762264Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
7772264Sjacobs 		return (0);
7782264Sjacobs 
7798321SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
7808321SSonam.Gupta@Sun.COM 	    "job-originating-host-name", &host);
7818321SSonam.Gupta@Sun.COM 
7829152SSonam.Gupta@Sun.COM 	if (check == 0) {
7839152SSonam.Gupta@Sun.COM 		/*
7849152SSonam.Gupta@Sun.COM 		 * Read the attribute "job-printer-uri"
7859152SSonam.Gupta@Sun.COM 		 * just once
7869152SSonam.Gupta@Sun.COM 		 */
7879152SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(attrs, NULL,
7889152SSonam.Gupta@Sun.COM 		    "job-printer-uri", &uri);
7899152SSonam.Gupta@Sun.COM 		check = 1;
7909152SSonam.Gupta@Sun.COM 	}
7919152SSonam.Gupta@Sun.COM 
7929152SSonam.Gupta@Sun.COM 	if (host) {
7939152SSonam.Gupta@Sun.COM 		/* Check if it is local printer or remote printer */
7949152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
7959152SSonam.Gupta@Sun.COM 
7969152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
7979152SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
7989152SSonam.Gupta@Sun.COM 
7999152SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
8009152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
8019152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0)) {
8028321SSonam.Gupta@Sun.COM 
8039152SSonam.Gupta@Sun.COM 				if (strcasecmp(host, nodename) == 0) {
8049152SSonam.Gupta@Sun.COM 					/*
8059152SSonam.Gupta@Sun.COM 					 * Request submitted locally
8069152SSonam.Gupta@Sun.COM 					 * for the local queue.
8079152SSonam.Gupta@Sun.COM 					 * Hostname will not be displayed
8089152SSonam.Gupta@Sun.COM 					 */
8099152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s",
8109152SSonam.Gupta@Sun.COM 					    user);
8119152SSonam.Gupta@Sun.COM 				}
8129152SSonam.Gupta@Sun.COM 				else
8139152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s@%s",
8149152SSonam.Gupta@Sun.COM 					    user, host);
8159152SSonam.Gupta@Sun.COM 			} else if (uri != NULL) {
8169152SSonam.Gupta@Sun.COM 				/*
8179152SSonam.Gupta@Sun.COM 				 * It's a remote printer.
8189152SSonam.Gupta@Sun.COM 				 * In case of remote printers hostname is
8199152SSonam.Gupta@Sun.COM 				 * always displayed.
8209152SSonam.Gupta@Sun.COM 				 */
8219152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
8229152SSonam.Gupta@Sun.COM 				    user, host);
8239152SSonam.Gupta@Sun.COM 			}
8249152SSonam.Gupta@Sun.COM 			uri_free(u);
8259152SSonam.Gupta@Sun.COM 		} else {
8269152SSonam.Gupta@Sun.COM 			/*
8279152SSonam.Gupta@Sun.COM 			 * If attribute "job-printer-uri"
8289152SSonam.Gupta@Sun.COM 			 * cannot be read
8299152SSonam.Gupta@Sun.COM 			 * by default append the hostname
8309152SSonam.Gupta@Sun.COM 			 */
8319152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s@%s", user, host);
8329152SSonam.Gupta@Sun.COM 		}
8339152SSonam.Gupta@Sun.COM 	} else {
8349152SSonam.Gupta@Sun.COM 		/*
8359152SSonam.Gupta@Sun.COM 		 * When print server is s10u4 and ipp service is used
8369152SSonam.Gupta@Sun.COM 		 * "job-originating-hostname" attribute is not set
8379152SSonam.Gupta@Sun.COM 		 * So get the host information from the uri
8389152SSonam.Gupta@Sun.COM 		 */
8399152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
8409152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
8419152SSonam.Gupta@Sun.COM 			if ((u != NULL) && (u->host != NULL))
8429152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
8439152SSonam.Gupta@Sun.COM 				    user, u->host);
8449152SSonam.Gupta@Sun.COM 			else
8459152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s", user);
8469152SSonam.Gupta@Sun.COM 
8479152SSonam.Gupta@Sun.COM 			uri_free(u);
8489152SSonam.Gupta@Sun.COM 		} else
8499152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s", user);
8509152SSonam.Gupta@Sun.COM 	}
8512264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
8522264Sjacobs 	size *= 1024;	/* for the approximate byte size */
8532264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
8542264Sjacobs 
8552264Sjacobs 	(void) time(&clock);
8562264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8578962SSonam.Gupta@Sun.COM 	    "time-at-creation", (int32_t *)&clock);
8582264Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
8592264Sjacobs 
8602264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
8618962SSonam.Gupta@Sun.COM 	    "job-printer-uri", &destination);
8622264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
8638962SSonam.Gupta@Sun.COM 	    "printer-name", &destination);
8642264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8658962SSonam.Gupta@Sun.COM 	    "job-id", &id);
8669330SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetInteger(attrs, NULL,
8679330SSonam.Gupta@Sun.COM 	    "job-id-requested", &id);
8689256SSonam.Gupta@Sun.COM 
8699331SSonam.Gupta@Sun.COM 
8709256SSonam.Gupta@Sun.COM 	snprintf(request, sizeof (request), "%s-%d", printer, id);
8712264Sjacobs 
8722264Sjacobs 	if (show_rank != 0) {
8732264Sjacobs 		int32_t rank = -1;
8742264Sjacobs 
8752264Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
8768962SSonam.Gupta@Sun.COM 		    "number-of-intervening-jobs", &rank);
8772264Sjacobs 		rank++;
8782264Sjacobs 
8792264Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
8808321SSonam.Gupta@Sun.COM 		    rank, request, User, size, date);
8812264Sjacobs 	} else
8828321SSonam.Gupta@Sun.COM 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
8832264Sjacobs 
8842264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8858962SSonam.Gupta@Sun.COM 	    "job-state", &jstate);
8869331SSonam.Gupta@Sun.COM 
8879257SGowtham.Thommandra@Sun.COM 	if (jstate == 0x0001)
8889257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being held"));
8899257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0800)
8909257SGowtham.Thommandra@Sun.COM 		printf(gettext(" notifying user"));
8919257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0040)
8929257SGowtham.Thommandra@Sun.COM 		printf(gettext(" cancelled"));
8939257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0010)
8949257SGowtham.Thommandra@Sun.COM 		printf(gettext(" finished printing"));
8959257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0008)
8969257SGowtham.Thommandra@Sun.COM 		printf(gettext(" on %s"), destination);
8979257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x2000)
8989257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held by admin"));
8999257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0002)
9009257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being filtered"));
9019257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0004)
9029257SGowtham.Thommandra@Sun.COM 		printf(gettext(" filtered"));
9039257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0020)
9049257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held for change"));
9052264Sjacobs 
9062264Sjacobs 	if (verbose == 1) {
9073125Sjacobs 		char *form = NULL;
9083125Sjacobs 
9092264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
9108962SSonam.Gupta@Sun.COM 		    "output-device-assigned", &destination);
9112264Sjacobs 		printf("\n\t assigned %s", destination);
9123127Sjacobs 
9133125Sjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
9143125Sjacobs 		if (form != NULL)
9153125Sjacobs 			printf(", form %s", form);
9162264Sjacobs 	} else if (verbose > 1) {
9172264Sjacobs 		printf("\n");
9182264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
9192264Sjacobs 	}
9202264Sjacobs 
9212264Sjacobs 	printf("\n");
9222264Sjacobs 
9232264Sjacobs 	return (0);
9242264Sjacobs }
9252264Sjacobs 
9262264Sjacobs static int
9279256SSonam.Gupta@Sun.COM job_query(char *request, int (*report)(char *, papi_job_t, int, int),
9282264Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
9292264Sjacobs {
9302264Sjacobs 	int result = 0;
9312264Sjacobs 	papi_status_t status;
9322264Sjacobs 	papi_service_t svc = NULL;
9338295SSonam.Gupta@Sun.COM 	char *printer = request;
9342264Sjacobs 	int32_t id = -1;
9358295SSonam.Gupta@Sun.COM 	int flag1 = 0;
9368295SSonam.Gupta@Sun.COM 	int flag = 1;
9378295SSonam.Gupta@Sun.COM 	int print_flag = 0;
9382264Sjacobs 
9398295SSonam.Gupta@Sun.COM 	do {
9408295SSonam.Gupta@Sun.COM 		status = papiServiceCreate(&svc, printer, NULL, NULL,
9418962SSonam.Gupta@Sun.COM 		    cli_auth_callback, encryption, NULL);
9422264Sjacobs 
9438295SSonam.Gupta@Sun.COM 		if ((status == PAPI_OK) && (printer != NULL))
9448295SSonam.Gupta@Sun.COM 			print_flag = 1;
9452264Sjacobs 
9468295SSonam.Gupta@Sun.COM 		/* <name>-# printer name does not exist */
9478295SSonam.Gupta@Sun.COM 		if (status != PAPI_OK) {
9488295SSonam.Gupta@Sun.COM 			/*
9498295SSonam.Gupta@Sun.COM 			 * Check if <name>-# is a request-id
9508295SSonam.Gupta@Sun.COM 			 * Once this check is done flag1 is set
9518295SSonam.Gupta@Sun.COM 			 */
9528295SSonam.Gupta@Sun.COM 			if (flag1 == 1)
9538295SSonam.Gupta@Sun.COM 				break;
9548295SSonam.Gupta@Sun.COM 
9558295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
9562264Sjacobs 
9578295SSonam.Gupta@Sun.COM 			status = papiServiceCreate(&svc, printer, NULL, NULL,
9588962SSonam.Gupta@Sun.COM 			    cli_auth_callback, encryption, NULL);
9592264Sjacobs 
9608295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
9618295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
9628295SSonam.Gupta@Sun.COM 				    "Failed to contact service for %s: %s\n"),
9638295SSonam.Gupta@Sun.COM 				    (printer ? printer : "all"),
9648295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
9658295SSonam.Gupta@Sun.COM 				return (-1);
9668295SSonam.Gupta@Sun.COM 			}
9672264Sjacobs 		}
9682264Sjacobs 
9698295SSonam.Gupta@Sun.COM 		if (printer == NULL) { /* all */
9708295SSonam.Gupta@Sun.COM 			char **interest = interest_list(svc);
9718295SSonam.Gupta@Sun.COM 
9728295SSonam.Gupta@Sun.COM 			if (interest != NULL) {
9738295SSonam.Gupta@Sun.COM 				int i;
9748295SSonam.Gupta@Sun.COM 
9758295SSonam.Gupta@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
9768295SSonam.Gupta@Sun.COM 					result += job_query(interest[i], report,
9778962SSonam.Gupta@Sun.COM 					    encryption, show_rank, verbose);
9788295SSonam.Gupta@Sun.COM 			}
9798295SSonam.Gupta@Sun.COM 		} else if (id == -1) { /* a printer */
9808295SSonam.Gupta@Sun.COM 			papi_job_t *jobs = NULL;
9818295SSonam.Gupta@Sun.COM 
9828295SSonam.Gupta@Sun.COM 			status = papiPrinterListJobs(svc, printer, NULL,
9838962SSonam.Gupta@Sun.COM 			    0, 0, &jobs);
9848295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
9858295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
9868295SSonam.Gupta@Sun.COM 				    "Failed to get job list: %s\n"),
9878295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
9888295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
9898295SSonam.Gupta@Sun.COM 				return (-1);
9908295SSonam.Gupta@Sun.COM 			}
9918295SSonam.Gupta@Sun.COM 
9928295SSonam.Gupta@Sun.COM 			if (jobs != NULL) {
9938295SSonam.Gupta@Sun.COM 				int i;
9942264Sjacobs 
9958295SSonam.Gupta@Sun.COM 				for (i = 0; jobs[i] != NULL; i++)
9969256SSonam.Gupta@Sun.COM 					result += report(printer,
9979256SSonam.Gupta@Sun.COM 					    jobs[i], show_rank,
9989256SSonam.Gupta@Sun.COM 					    verbose);
9998295SSonam.Gupta@Sun.COM 			}
10008295SSonam.Gupta@Sun.COM 
10018295SSonam.Gupta@Sun.COM 			papiJobListFree(jobs);
10028295SSonam.Gupta@Sun.COM 		} else {	/* a job */
10038295SSonam.Gupta@Sun.COM 			papi_job_t job = NULL;
10049331SSonam.Gupta@Sun.COM 			int rid = id;
10058295SSonam.Gupta@Sun.COM 
10068295SSonam.Gupta@Sun.COM 			/* Once a job has been found stop processing */
10078295SSonam.Gupta@Sun.COM 			flag = 0;
10088295SSonam.Gupta@Sun.COM 
10099331SSonam.Gupta@Sun.COM 			/*
10109331SSonam.Gupta@Sun.COM 			 * Job-id could be the job-id requested
10119331SSonam.Gupta@Sun.COM 			 * Check if it is job-id or job-id-requested
10129331SSonam.Gupta@Sun.COM 			 */
10139331SSonam.Gupta@Sun.COM 			id = job_to_be_queried(svc, printer, id);
10149331SSonam.Gupta@Sun.COM 
10159331SSonam.Gupta@Sun.COM 			if (id > 0)
10169669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, id,
10179669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
10189331SSonam.Gupta@Sun.COM 			else
10199669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, rid,
10209669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
10219331SSonam.Gupta@Sun.COM 
10228295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
10238295SSonam.Gupta@Sun.COM 				if (!print_flag)
10248295SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
10258962SSonam.Gupta@Sun.COM 					    "Failed to get job"\
10268962SSonam.Gupta@Sun.COM 					    " info for %s: %s\n"),
10278962SSonam.Gupta@Sun.COM 					    request,
10288962SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
10298295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
10308295SSonam.Gupta@Sun.COM 				return (-1);
10318295SSonam.Gupta@Sun.COM 			}
10328295SSonam.Gupta@Sun.COM 
10338295SSonam.Gupta@Sun.COM 			if (job != NULL)
10349256SSonam.Gupta@Sun.COM 				result = report(printer, job,
10359256SSonam.Gupta@Sun.COM 				    show_rank, verbose);
10368295SSonam.Gupta@Sun.COM 
10378295SSonam.Gupta@Sun.COM 			papiJobFree(job);
10382264Sjacobs 		}
10392264Sjacobs 
10408295SSonam.Gupta@Sun.COM 		if (flag) {
10418295SSonam.Gupta@Sun.COM 			id = -1;
10428295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
10438295SSonam.Gupta@Sun.COM 			if (id == -1)
10448295SSonam.Gupta@Sun.COM 				flag = 0;
10458295SSonam.Gupta@Sun.COM 			else
10468295SSonam.Gupta@Sun.COM 				flag1 = 1;
10472264Sjacobs 		}
10488295SSonam.Gupta@Sun.COM 	} while (flag);
10492264Sjacobs 
10502264Sjacobs 	papiServiceDestroy(svc);
10512264Sjacobs 
10522264Sjacobs 	return (result);
10532264Sjacobs }
10542264Sjacobs 
10552264Sjacobs static int
10562264Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
10572264Sjacobs {
10582264Sjacobs 	papi_status_t status;
10592264Sjacobs 	char *form = NULL;
10602264Sjacobs 	void *iter = NULL;
10612264Sjacobs 
10622264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
10638962SSonam.Gupta@Sun.COM 	    "form-supported", &form);
10648962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
10658962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter,
10668962SSonam.Gupta@Sun.COM 	    NULL, &form)) {
10672264Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
10682264Sjacobs 			printf(gettext("form %s is available to you\n"), form);
10692264Sjacobs 			if (verbose != 0) {
10702264Sjacobs 				char *detail = NULL;
10712264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
10728962SSonam.Gupta@Sun.COM 				    "form-supported-detail", &detail);
10732264Sjacobs 				if (status == PAPI_OK)
10742264Sjacobs 					printf("%s\n", detail);
10752264Sjacobs 			}
10762264Sjacobs 		}
10772264Sjacobs 	}
10782264Sjacobs 
10792264Sjacobs 	return (0);
10802264Sjacobs }
10812264Sjacobs 
10822264Sjacobs static int
10832264Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
10842264Sjacobs {
10852264Sjacobs 	papi_status_t status;
10862264Sjacobs 	char *pw = NULL;
10872264Sjacobs 	void *iter = NULL;
10882264Sjacobs 
10892264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
10908962SSonam.Gupta@Sun.COM 	    "pw-supported", &pw);
10918962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
10928962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
10932264Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
10942264Sjacobs 			printf(gettext("charset %s is available\n"), pw);
10952264Sjacobs 			if (verbose != 0) {
10962264Sjacobs 				char *info = NULL;
10972264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
10988962SSonam.Gupta@Sun.COM 				    "pw-supported-extra", &info);
10992264Sjacobs 				if (status == PAPI_OK)
11002264Sjacobs 					printf("%s\n", info);
11012264Sjacobs 			}
11022264Sjacobs 		}
11032264Sjacobs 	}
11042264Sjacobs 
11052264Sjacobs 	return (0);
11062264Sjacobs }
11072264Sjacobs 
11082264Sjacobs static int
11092264Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
11102264Sjacobs 		papi_encryption_t encryption, int verbose)
11112264Sjacobs {
11122264Sjacobs 	int result = 0;
11132264Sjacobs 	papi_status_t status;
11142264Sjacobs 	papi_service_t svc = NULL;
11152264Sjacobs 	papi_attribute_t **attrs = NULL;
11162264Sjacobs 
11172264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
11188962SSonam.Gupta@Sun.COM 	    encryption, NULL);
11192264Sjacobs 	if (status != PAPI_OK) {
11202264Sjacobs 		papiServiceDestroy(svc);
11212264Sjacobs 		return (-1);
11222264Sjacobs 	}
11232264Sjacobs 
11242264Sjacobs 	attrs = papiServiceGetAttributeList(svc);
11252264Sjacobs 	if (attrs != NULL) {
11262264Sjacobs 		result = report(name, attrs, verbose);
11272264Sjacobs 
11282264Sjacobs 		if (verbose > 1) {
11292264Sjacobs 			printf("\n");
11302264Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
11312264Sjacobs 			printf("\n");
11322264Sjacobs 		}
11332264Sjacobs 	}
11342264Sjacobs 
11352264Sjacobs 	papiServiceDestroy(svc);
11362264Sjacobs 
11372264Sjacobs 	return (result);
11382264Sjacobs }
11392264Sjacobs 
11402264Sjacobs int
11412264Sjacobs main(int ac, char *av[])
11422264Sjacobs {
11432264Sjacobs 	int exit_code = 0;
11442264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
11452264Sjacobs 	int rank = 0;
11462264Sjacobs 	int verbose = 0;
11472264Sjacobs 	int description = 0;
11482264Sjacobs 	int c;
11492264Sjacobs 	char **argv;
11502264Sjacobs 
11512264Sjacobs 	(void) setlocale(LC_ALL, "");
11522264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
11532264Sjacobs 
11542264Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
11554286Swendyp 	for (c = 0; c < ac; c++) {
11564286Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
11578962SSonam.Gupta@Sun.COM 		    (isalpha(av[c][2]) != 0)) {
11584286Swendyp 			/* preserve old "-l[po...]" behavior */
11594286Swendyp 			argv[c] = &av[c][1];
11604286Swendyp 			argv[c][0] = '-';
11614286Swendyp 			verbose = 1;
11624286Swendyp 
11634286Swendyp 		} else
11644286Swendyp 			argv[c] = av[c];
11654286Swendyp 	}
11664286Swendyp 
11672264Sjacobs 	argv[c++] = "--";
11682264Sjacobs 	ac = c;
11692264Sjacobs 
11702264Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
11719740SKeerthi.Kondaka@Sun.COM 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
11729740SKeerthi.Kondaka@Sun.COM 		switch (c) {    /* these may or may not have an option */
11739740SKeerthi.Kondaka@Sun.COM 		case 'a':
11749740SKeerthi.Kondaka@Sun.COM 		case 'c':
11759740SKeerthi.Kondaka@Sun.COM 		case 'p':
11769740SKeerthi.Kondaka@Sun.COM 		case 'o':
11779740SKeerthi.Kondaka@Sun.COM 		case 'R':
11789740SKeerthi.Kondaka@Sun.COM 		case 'u':
11799740SKeerthi.Kondaka@Sun.COM 		case 'v':
11809740SKeerthi.Kondaka@Sun.COM 		case 'l':
11819740SKeerthi.Kondaka@Sun.COM 		case 'f':
11829740SKeerthi.Kondaka@Sun.COM 		case 'S':
11839740SKeerthi.Kondaka@Sun.COM 			if (optarg[0] == '-') {
11849740SKeerthi.Kondaka@Sun.COM 				/* this check stop a possible infinite loop */
11859740SKeerthi.Kondaka@Sun.COM 				if ((optind > 1) && (argv[optind-1][1] != c))
11869740SKeerthi.Kondaka@Sun.COM 					optind--;
11879740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
11889740SKeerthi.Kondaka@Sun.COM 			} else if (strcmp(optarg, "all") == 0)
11899740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
11909740SKeerthi.Kondaka@Sun.COM 		}
11919740SKeerthi.Kondaka@Sun.COM 
11922264Sjacobs 		switch (c) {
11932264Sjacobs 		case 'l':
11942264Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
11952264Sjacobs 				optarg = "1";
11962264Sjacobs 			verbose = atoi(optarg);
11972264Sjacobs 			break;
11982264Sjacobs 		case 'D':
11992264Sjacobs 			description = 1;
12002264Sjacobs 			break;
12012264Sjacobs 		case 'R':
12022264Sjacobs 			rank = 1;
12032264Sjacobs 			break;
12042264Sjacobs 		case 'E':
12052264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
12062264Sjacobs 			break;
12072264Sjacobs 		default:
12082264Sjacobs 			break;
12092264Sjacobs 		}
12109740SKeerthi.Kondaka@Sun.COM 	}
12112264Sjacobs 	optind = 1;
12122264Sjacobs 
12132264Sjacobs 	/* process command line arguments */
12142264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
12152264Sjacobs 		switch (c) {	/* these may or may not have an option */
12162264Sjacobs 		case 'a':
12172264Sjacobs 		case 'c':
12182264Sjacobs 		case 'p':
12192264Sjacobs 		case 'o':
12202264Sjacobs 		case 'R':
12212264Sjacobs 		case 'u':
12222264Sjacobs 		case 'v':
12232264Sjacobs 		case 'l':
12242264Sjacobs 		case 'f':
12252264Sjacobs 		case 'S':
12262264Sjacobs 			if (optarg[0] == '-') {
12272264Sjacobs 				/* this check stop a possible infinite loop */
12282264Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
12292264Sjacobs 					optind--;
12302264Sjacobs 				optarg = NULL;
12312264Sjacobs 			} else if (strcmp(optarg, "all") == 0)
12322264Sjacobs 				optarg = NULL;
12332264Sjacobs 		}
12342264Sjacobs 
12352264Sjacobs 		switch (c) {
12362264Sjacobs 		case 'a':
12372264Sjacobs 			exit_code += printer_query(optarg, report_accepting,
12388962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12392264Sjacobs 			break;
12402264Sjacobs 		case 'c':
12412264Sjacobs 			exit_code += printer_query(optarg, report_class,
12428962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12432264Sjacobs 			break;
12442264Sjacobs 		case 'p':
12452264Sjacobs 			exit_code += printer_query(optarg, report_printer,
12468962SSonam.Gupta@Sun.COM 			    encryption, verbose, description);
12472264Sjacobs 			break;
12482264Sjacobs 		case 'd':
12492264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12502264Sjacobs 			break;
12512264Sjacobs 		case 'r':
12522264Sjacobs 			exit_code += lpstat_service_status(encryption);
12532264Sjacobs 			break;
12542264Sjacobs 		case 'u':
12552264Sjacobs 			if (optarg != NULL)
12562264Sjacobs 				users = strsplit(optarg, ", \n");
12572264Sjacobs 			exit_code += job_query(NULL, report_job,
12588962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
12592264Sjacobs 			if (users != NULL) {
12602264Sjacobs 				free(users);
12612264Sjacobs 				users = NULL;
12622264Sjacobs 			}
12632264Sjacobs 			break;
12642264Sjacobs 		case 'v':
12652264Sjacobs 			exit_code += printer_query(optarg, report_device,
12668962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12672264Sjacobs 			break;
12688604SGowtham.Thommandra@Sun.COM 		case 'R':	/* set "rank" flag in first pass */
12692264Sjacobs 		case 'o':
12702264Sjacobs 			exit_code += job_query(optarg, report_job,
12718962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
12722264Sjacobs 			break;
12732264Sjacobs 		case 'f':
12742264Sjacobs 			exit_code += service_query(optarg, report_form,
12758962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12762264Sjacobs 			break;
12772264Sjacobs 		case 'S':
12782264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
12798962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12802264Sjacobs 			break;
12812264Sjacobs 		case 's':
12822264Sjacobs 			exit_code += lpstat_service_status(encryption);
12832264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12842264Sjacobs 			exit_code += printer_query(NULL, report_class,
12858962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12862264Sjacobs 			exit_code += printer_query(NULL, report_device,
12878962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12882264Sjacobs 			exit_code += service_query(optarg, report_form,
12898962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12902264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
12918962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12922264Sjacobs 			break;
12932264Sjacobs 		case 't':
12942264Sjacobs 			exit_code += lpstat_service_status(encryption);
12952264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12962264Sjacobs 			exit_code += printer_query(NULL, report_class,
12978962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12982264Sjacobs 			exit_code += printer_query(NULL, report_device,
12998962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13002264Sjacobs 			exit_code += printer_query(NULL, report_accepting,
13018962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13022264Sjacobs 			exit_code += printer_query(NULL, report_printer,
13038962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13042264Sjacobs 			exit_code += service_query(optarg, report_form,
13058962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13062264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
13078962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13082264Sjacobs 			exit_code += job_query(NULL, report_job,
13098962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
13102264Sjacobs 			break;
13112264Sjacobs 		case 'L':	/* local-only, ignored */
13122264Sjacobs 		case 'l':	/* increased verbose level in first pass */
13132264Sjacobs 		case 'D':	/* set "description" flag in first pass */
13142264Sjacobs 		case 'E':	/* set encryption in the first pass */
13152264Sjacobs 			break;
13162264Sjacobs 		default:
13172264Sjacobs 			usage(av[0]);
13182264Sjacobs 		}
13192264Sjacobs 	}
13202264Sjacobs 	ac--;
13212264Sjacobs 
13222264Sjacobs 	if (ac == 1) {	/* report on my jobs */
13232264Sjacobs 		struct passwd *pw = getpwuid(getuid());
13242264Sjacobs 
13252264Sjacobs 		if (pw != NULL)
13262264Sjacobs 			users = strsplit(pw->pw_name, "");
13272264Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
13288962SSonam.Gupta@Sun.COM 		    rank, verbose);
13292264Sjacobs 		if (users != NULL) {
13302264Sjacobs 			free(users);
13312264Sjacobs 			users = NULL;
13322264Sjacobs 		}
13332264Sjacobs 	} else {
13342264Sjacobs 		for (c = optind; c < ac; c++)
13352264Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
13368962SSonam.Gupta@Sun.COM 			    rank, verbose);
13372264Sjacobs 	}
13382264Sjacobs 
13392264Sjacobs 
13402264Sjacobs 	if (exit_code != 0)
13412264Sjacobs 		exit_code = 1;
13422264Sjacobs 
13432264Sjacobs 	return (exit_code);
13442264Sjacobs }
1345