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 
1339852SGowtham.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 
4188962SSonam.Gupta@Sun.COM 				for (i = 0; j[i] != NULL; ++i) {
4199206SSonam.Gupta@Sun.COM 					papi_attribute_t **attr =
4209206SSonam.Gupta@Sun.COM 					    papiJobGetAttributeList(j[i]);
4218962SSonam.Gupta@Sun.COM 
4229206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
4238962SSonam.Gupta@Sun.COM 					    NULL, "job-state", &jstate);
4249206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
4258962SSonam.Gupta@Sun.COM 					    NULL, "job-id", &jobid);
42610338SSonam.Gupta@Sun.COM 					/*
42710338SSonam.Gupta@Sun.COM 					 * For lpd protocol "job-id-requested"
42810338SSonam.Gupta@Sun.COM 					 * should be read.
42910338SSonam.Gupta@Sun.COM 					 */
43010338SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
43110338SSonam.Gupta@Sun.COM 					    NULL, "job-id-requested", &jobid);
4322264Sjacobs 
4338962SSonam.Gupta@Sun.COM 					/*
43410338SSonam.Gupta@Sun.COM 					 * When lpd protocol is used job-state
43510338SSonam.Gupta@Sun.COM 					 * cannot be retrieved, therefore
43610338SSonam.Gupta@Sun.COM 					 * job-state will be 0.
43710338SSonam.Gupta@Sun.COM 					 * When ipp protocol is used, the
43810338SSonam.Gupta@Sun.COM 					 * active/printing job-state will be
43910338SSonam.Gupta@Sun.COM 					 * RS_PRINTING (0x0008) post s10u5.
44010338SSonam.Gupta@Sun.COM 					 * For pre-s10u5 job-state will be
44110338SSonam.Gupta@Sun.COM 					 * RS_ACTIVE (0x05). So print only when
44210338SSonam.Gupta@Sun.COM 					 * the job-state is RS_PRINTING (0x0008)
44310338SSonam.Gupta@Sun.COM 					 * or RS_ACTIVE (0x05) or 0
4448962SSonam.Gupta@Sun.COM 					 */
44510338SSonam.Gupta@Sun.COM 					if ((jstate == 0x0008) ||
44610338SSonam.Gupta@Sun.COM 					    (jstate == 0x05) ||
44710338SSonam.Gupta@Sun.COM 					    (jstate == 0)) {
44810338SSonam.Gupta@Sun.COM 						printf(gettext
44910338SSonam.Gupta@Sun.COM 						    ("now printing"\
45010338SSonam.Gupta@Sun.COM 						    " %s-%d. enabled"),
45110338SSonam.Gupta@Sun.COM 						    name, jobid);
45210338SSonam.Gupta@Sun.COM 						break;
4538962SSonam.Gupta@Sun.COM 					}
4548962SSonam.Gupta@Sun.COM 				}
4558962SSonam.Gupta@Sun.COM 				papiJobListFree(j);
4568962SSonam.Gupta@Sun.COM 			}
4572264Sjacobs 		}
4582264Sjacobs 		break;
4592264Sjacobs 	case 0x05:	/* stopped */
4602264Sjacobs 		printf(gettext("disabled"));
4612264Sjacobs 		break;
4622264Sjacobs 	default:
4632264Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
4642264Sjacobs 		break;
4652264Sjacobs 	}
4662264Sjacobs 
4672264Sjacobs 	(void) time(&curr);
4682264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4698962SSonam.Gupta@Sun.COM 	    "printer-up-time", &curr);
4702264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4718962SSonam.Gupta@Sun.COM 	    "printer-state-time", &curr);
4722264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4738962SSonam.Gupta@Sun.COM 	    "lpsched-disable-date", &curr);
4742264Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
4752264Sjacobs 
4762264Sjacobs 	if (pstat == 0x05) {
4772264Sjacobs 		char *reason = "unknown reason";
4782264Sjacobs 
4792264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4808962SSonam.Gupta@Sun.COM 		    "printer-state-reasons", &reason);
4812264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4828962SSonam.Gupta@Sun.COM 		    "lpsched-disable-reason", &reason);
4832264Sjacobs 		printf(gettext("\t%s\n"), reason);
4842264Sjacobs 	}
4852264Sjacobs 
4862264Sjacobs 	if (verbose == 1) {
4872264Sjacobs 		void *iter;
4882264Sjacobs 		char *str;
4899810SKeerthi.Kondaka@Sun.COM 		char *host = NULL;
4902264Sjacobs 
4919810SKeerthi.Kondaka@Sun.COM 		if ((get_remote_hostname(attrs, &host)) != 0) {
4929810SKeerthi.Kondaka@Sun.COM 			(void) printf(
4939810SKeerthi.Kondaka@Sun.COM 			    gettext("\tRemote Name: %s\n\tRemote Server: "
4949810SKeerthi.Kondaka@Sun.COM 			    "%s\n"), name, host);
4959810SKeerthi.Kondaka@Sun.COM 			free(host);
4969810SKeerthi.Kondaka@Sun.COM 			return (0);
4979810SKeerthi.Kondaka@Sun.COM 		}
4982264Sjacobs 		str = "";
4992264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5008962SSonam.Gupta@Sun.COM 		    "form-ready", &str);
5012264Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
5022264Sjacobs 
5032264Sjacobs 		str = "";
5042264Sjacobs 		iter = NULL;
5052264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5068962SSonam.Gupta@Sun.COM 		    "document-format-supported", &str);
5072264Sjacobs 		printf(gettext("\tContent types: %s"), str);
5082264Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
5098962SSonam.Gupta@Sun.COM 		    == PAPI_OK)
5102264Sjacobs 			printf(", %s", str);
5112264Sjacobs 		printf("\n");
5122264Sjacobs 
5138534SSonam.Gupta@Sun.COM 		/* Display the printer description */
5148534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
5152264Sjacobs 
5162264Sjacobs 		str = "";
5176676Swendyp 		iter = NULL;
5186676Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
5196676Swendyp 		    "lpsched-printer-type", &str);
5206676Swendyp 		printf(gettext("\tPrinter types: %s"), str);
5216676Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
5226676Swendyp 		    == PAPI_OK)
5236676Swendyp 			printf(", %s", str);
5246676Swendyp 		printf("\n");
5256676Swendyp 
5266676Swendyp 		str = "";
5272264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5288962SSonam.Gupta@Sun.COM 		    "lpsched-dial-info", &str);
5292264Sjacobs 		printf(gettext("\tConnection: %s\n"),
5307653SJonathan.Ca@Sun.COM 		    ((str[0] == '\0') ? gettext("direct") : str));
5312264Sjacobs 
5322264Sjacobs 		str = "";
5332264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5348962SSonam.Gupta@Sun.COM 		    "lpsched-interface-script", &str);
5352264Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
5362264Sjacobs 
5372264Sjacobs 		str = NULL;
5382264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5398962SSonam.Gupta@Sun.COM 		    "ppd-file-uri", &str);
5402264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5418962SSonam.Gupta@Sun.COM 		    "lpsched-ppd-source-path", &str);
5422264Sjacobs 		if (str != NULL)
5432264Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
5442264Sjacobs 
5452264Sjacobs 		str = NULL;
5462264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5478962SSonam.Gupta@Sun.COM 		    "lpsched-fault-alert-command", &str);
5482264Sjacobs 		if (str != NULL)
5492264Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
5502264Sjacobs 
5512264Sjacobs 		str = "";
5522264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5538962SSonam.Gupta@Sun.COM 		    "lpsched-fault-recovery", &str);
5542264Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
5558962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("continue") : str));
5562264Sjacobs 
5572264Sjacobs 		str = "(all)";
5582264Sjacobs 		iter = NULL;
5592264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5608962SSonam.Gupta@Sun.COM 		    "requesting-user-name-allowed", &str);
5612264Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
5628962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5632264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5642264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5658962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5662264Sjacobs 				printf("\t\t%s\n", str);
5672264Sjacobs 
5682264Sjacobs 		str = NULL;
5692264Sjacobs 		iter = NULL;
5702264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5718962SSonam.Gupta@Sun.COM 		    "requesting-user-name-denied", &str);
5722264Sjacobs 		if (str != NULL) {
5732264Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
5748962SSonam.Gupta@Sun.COM 			    ((str[0] == '\0') ? gettext("(none)") : str));
5752264Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
5762264Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
5778962SSonam.Gupta@Sun.COM 				    NULL, &str) == PAPI_OK)
5782264Sjacobs 					printf("\t\t%s\n", str);
5792264Sjacobs 		}
5802264Sjacobs 
5818238SSonam.Gupta@Sun.COM 		str = "none";
5822264Sjacobs 		iter = NULL;
5832264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5848962SSonam.Gupta@Sun.COM 		    "form-supported", &str);
5858238SSonam.Gupta@Sun.COM 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
5868962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("none") : str));
5872264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5882264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5898962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5908238SSonam.Gupta@Sun.COM 				printf("\t\t(%s)\n", str);
5912264Sjacobs 
5922264Sjacobs 		str = "";
5932264Sjacobs 		iter = NULL;
5942264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5958962SSonam.Gupta@Sun.COM 		    "media-supported", &str);
5962264Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
5978962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5982264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5992264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6008962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6012264Sjacobs 				printf("\t\t%s\n", str);
6022264Sjacobs 
6032264Sjacobs 		str = "";
6042264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
6058962SSonam.Gupta@Sun.COM 		    "job-sheets-supported", &str);
6066676Swendyp 		if ((strcasecmp(str, "none")) == 0)
6076676Swendyp 			str = gettext("page never printed");
6086676Swendyp 		else if (strcasecmp(str, "optional") == 0)
6096676Swendyp 			str = gettext("not required");
6106676Swendyp 		else
6116676Swendyp 			str = gettext("required");
6126676Swendyp 
6136676Swendyp 		printf(gettext("\tBanner %s\n"), str);
6146676Swendyp 
6152264Sjacobs 
6162264Sjacobs 		str = "";
6172264Sjacobs 		iter = NULL;
6182264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6198962SSonam.Gupta@Sun.COM 		    "lpsched-print-wheels", &str);
6202264Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
6218962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
6222264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
6232264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6248962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6252264Sjacobs 				printf("\t\t%s\n", str);
6262264Sjacobs 
6272264Sjacobs 		printf(gettext("\tDefault pitch:\n"));
6282264Sjacobs 		printf(gettext("\tDefault page size:\n"));
6292264Sjacobs 		printf(gettext("\tDefault port setting:\n"));
6302264Sjacobs 
6312264Sjacobs 		str = "";
6322264Sjacobs 		iter = NULL;
6332264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6348962SSonam.Gupta@Sun.COM 		    "lpsched-options", &str);
6352264Sjacobs 		if (str != NULL) {
6362264Sjacobs 			printf(gettext("\tOptions: %s"), str);
6372264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6388962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6392264Sjacobs 				printf(", %s", str);
6402264Sjacobs 			printf("\n");
6412264Sjacobs 		}
6422264Sjacobs 
6438534SSonam.Gupta@Sun.COM 	} else if (description == 1)
6448534SSonam.Gupta@Sun.COM 		/* Display printer description */
6458534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
6468534SSonam.Gupta@Sun.COM 	else if (verbose > 1)
6472264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
6482264Sjacobs 
6492264Sjacobs 	if (verbose > 0)
6502264Sjacobs 		printf("\n");
6512264Sjacobs 
6522264Sjacobs 	return (0);
6532264Sjacobs }
6542264Sjacobs 
6552264Sjacobs static int
6562264Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
6572264Sjacobs 					int, int), papi_encryption_t encryption,
6582264Sjacobs 		int verbose, int description)
6592264Sjacobs {
6609669SGowtham.Thommandra@Sun.COM 	int result = 0, i = 0;
6612264Sjacobs 	papi_status_t status;
6622264Sjacobs 	papi_service_t svc = NULL;
6639669SGowtham.Thommandra@Sun.COM 	char **list = getlist(name, LP_WS, LP_SEP);
6642264Sjacobs 
6659669SGowtham.Thommandra@Sun.COM 	if (list == NULL) {
6669669SGowtham.Thommandra@Sun.COM 		list = (char **)malloc(sizeof (char *));
6679669SGowtham.Thommandra@Sun.COM 		list[0] = name;
6682264Sjacobs 	}
6692264Sjacobs 
6709669SGowtham.Thommandra@Sun.COM 	/*
6719669SGowtham.Thommandra@Sun.COM 	 * The for loop executes once for every printer
6729669SGowtham.Thommandra@Sun.COM 	 * entry in list. If list is NULL that implies
6739669SGowtham.Thommandra@Sun.COM 	 * name is also NULL, the loop runs only one time.
6749669SGowtham.Thommandra@Sun.COM 	 */
6752264Sjacobs 
6769669SGowtham.Thommandra@Sun.COM 	for (i = 0; name == NULL || list[i] != NULL; i++) {
6779669SGowtham.Thommandra@Sun.COM 		name = list[i];
6782264Sjacobs 
6799669SGowtham.Thommandra@Sun.COM 		status = papiServiceCreate(&svc, name, NULL, NULL,
6809669SGowtham.Thommandra@Sun.COM 		    cli_auth_callback, encryption, NULL);
6812264Sjacobs 		if (status != PAPI_OK) {
6829669SGowtham.Thommandra@Sun.COM 			if (status == PAPI_NOT_FOUND)
6839669SGowtham.Thommandra@Sun.COM 				fprintf(stderr,
6849669SGowtham.Thommandra@Sun.COM 				    gettext("%s: unknown printer\n"),
6859669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)");
6869669SGowtham.Thommandra@Sun.COM 			else
6879669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
6889669SGowtham.Thommandra@Sun.COM 				    "Failed to contact service for %s: %s\n"),
6899669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)",
6909669SGowtham.Thommandra@Sun.COM 				    verbose_papi_message(svc, status));
6912264Sjacobs 			papiServiceDestroy(svc);
6929669SGowtham.Thommandra@Sun.COM 			result--;
6939669SGowtham.Thommandra@Sun.COM 			continue;
6942264Sjacobs 		}
6952264Sjacobs 
6969669SGowtham.Thommandra@Sun.COM 		if (name == NULL) { /* all */
6979669SGowtham.Thommandra@Sun.COM 			char **interest = interest_list(svc);
6989669SGowtham.Thommandra@Sun.COM 
6999669SGowtham.Thommandra@Sun.COM 			if (interest != NULL) {
7009669SGowtham.Thommandra@Sun.COM 				int i;
7019669SGowtham.Thommandra@Sun.COM 
7029669SGowtham.Thommandra@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
7039669SGowtham.Thommandra@Sun.COM 					result += printer_query(interest[i],
7049669SGowtham.Thommandra@Sun.COM 					    report, encryption, verbose,
7059669SGowtham.Thommandra@Sun.COM 					    description);
7069669SGowtham.Thommandra@Sun.COM 			}
7079669SGowtham.Thommandra@Sun.COM 		} else {
7089669SGowtham.Thommandra@Sun.COM 			papi_printer_t printer = NULL;
7099669SGowtham.Thommandra@Sun.COM 			char **keys = NULL;
7102264Sjacobs 
7119669SGowtham.Thommandra@Sun.COM 			/*
7129669SGowtham.Thommandra@Sun.COM 			 * Limit the query to only required data
7139669SGowtham.Thommandra@Sun.COM 			 * to reduce the need to go remote for
7149669SGowtham.Thommandra@Sun.COM 			 * information.
7159669SGowtham.Thommandra@Sun.COM 			 */
7169669SGowtham.Thommandra@Sun.COM 			if (report == report_device)
7179669SGowtham.Thommandra@Sun.COM 				keys = report_device_keys;
7189669SGowtham.Thommandra@Sun.COM 			else if (report == report_class)
7199669SGowtham.Thommandra@Sun.COM 				keys = report_class_keys;
7209669SGowtham.Thommandra@Sun.COM 			else if (report == report_accepting)
7219669SGowtham.Thommandra@Sun.COM 				keys = report_accepting_keys;
7229669SGowtham.Thommandra@Sun.COM 			else if ((report == report_printer) && (verbose == 0))
7239669SGowtham.Thommandra@Sun.COM 				keys = report_printer_keys;
7249669SGowtham.Thommandra@Sun.COM 
7259669SGowtham.Thommandra@Sun.COM 			status = papiPrinterQuery(svc, name, keys,
7269669SGowtham.Thommandra@Sun.COM 			    NULL, &printer);
7279669SGowtham.Thommandra@Sun.COM 			if (status != PAPI_OK) {
7289669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
7299669SGowtham.Thommandra@Sun.COM 				    "Failed to get printer info for %s: %s\n"),
7309669SGowtham.Thommandra@Sun.COM 				    name, verbose_papi_message(svc, status));
7319669SGowtham.Thommandra@Sun.COM 				papiServiceDestroy(svc);
7329669SGowtham.Thommandra@Sun.COM 				result--;
7339669SGowtham.Thommandra@Sun.COM 				continue;
7349669SGowtham.Thommandra@Sun.COM 			}
7359669SGowtham.Thommandra@Sun.COM 
7369669SGowtham.Thommandra@Sun.COM 			if (printer != NULL)
7379669SGowtham.Thommandra@Sun.COM 				result += report(svc, name, printer, verbose,
7389669SGowtham.Thommandra@Sun.COM 				    description);
7399669SGowtham.Thommandra@Sun.COM 
7409669SGowtham.Thommandra@Sun.COM 			papiPrinterFree(printer);
7419669SGowtham.Thommandra@Sun.COM 		}
7429669SGowtham.Thommandra@Sun.COM 
7439669SGowtham.Thommandra@Sun.COM 		papiServiceDestroy(svc);
7449669SGowtham.Thommandra@Sun.COM 
7459669SGowtham.Thommandra@Sun.COM 		if (name == NULL)
7469669SGowtham.Thommandra@Sun.COM 			break;
7472264Sjacobs 	}
7482264Sjacobs 
7499669SGowtham.Thommandra@Sun.COM 	freelist(list);
7502264Sjacobs 
7512264Sjacobs 	return (result);
7522264Sjacobs }
7532264Sjacobs 
7542264Sjacobs static int
7552264Sjacobs match_user(char *user, char **list)
7562264Sjacobs {
7572264Sjacobs 	int i;
7582264Sjacobs 
7592264Sjacobs 	for (i = 0; list[i] != NULL; i++) {
7602264Sjacobs 		if (strcmp(user, list[i]) == 0)
7612264Sjacobs 			return (0);
7622264Sjacobs 	}
7632264Sjacobs 
7642264Sjacobs 	return (-1);
7652264Sjacobs }
7662264Sjacobs 
7672264Sjacobs static char **users = NULL;
7682264Sjacobs 
7692264Sjacobs static int
7709256SSonam.Gupta@Sun.COM report_job(char *printer, papi_job_t job, int show_rank, int verbose)
7712264Sjacobs {
7722264Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
7732264Sjacobs 	time_t clock = 0;
7742264Sjacobs 	char date[24];
7752264Sjacobs 	char request[26];
7762264Sjacobs 	char *user = "unknown";
7778321SSonam.Gupta@Sun.COM 	char *host = NULL;
7782264Sjacobs 	int32_t size = 0;
7792264Sjacobs 	int32_t jstate = 0;
7808321SSonam.Gupta@Sun.COM 	char User[50];
7812264Sjacobs 
7822264Sjacobs 	char *destination = "unknown";
7832264Sjacobs 	int32_t id = -1;
7849152SSonam.Gupta@Sun.COM 	static int check = 0;
7859152SSonam.Gupta@Sun.COM 	static char *uri = NULL;
786*10441SSonam.Gupta@Sun.COM 	static char *puri = NULL;	/* printer-uri */
787*10441SSonam.Gupta@Sun.COM 	static char *pname = NULL;	/* printer-name */
7882264Sjacobs 
7892264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
7908962SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
7912264Sjacobs 
7922264Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
7932264Sjacobs 		return (0);
7942264Sjacobs 
7958321SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
7968321SSonam.Gupta@Sun.COM 	    "job-originating-host-name", &host);
7978321SSonam.Gupta@Sun.COM 
798*10441SSonam.Gupta@Sun.COM 	/*
799*10441SSonam.Gupta@Sun.COM 	 * When lpstat is called for multiple printers
800*10441SSonam.Gupta@Sun.COM 	 * internally the function 'report_job' gets
801*10441SSonam.Gupta@Sun.COM 	 * called multiple times with different printer-names.
802*10441SSonam.Gupta@Sun.COM 	 * The following block of code handles the case when lpstat is
803*10441SSonam.Gupta@Sun.COM 	 * executed for multiple printers. In other words when 'report_job'
804*10441SSonam.Gupta@Sun.COM 	 * is called multiple times for different printers for
805*10441SSonam.Gupta@Sun.COM 	 * one lpstat command
806*10441SSonam.Gupta@Sun.COM 	 * For e.g: lpstat printer1 printer2 printer3
807*10441SSonam.Gupta@Sun.COM 	 */
808*10441SSonam.Gupta@Sun.COM 	if (pname == NULL) {
8099152SSonam.Gupta@Sun.COM 		/*
810*10441SSonam.Gupta@Sun.COM 		 * When lpstat is queried for the first time
811*10441SSonam.Gupta@Sun.COM 		 * pname is NULL so this part of the code gets executed.
8129152SSonam.Gupta@Sun.COM 		 * Read the attribute "job-printer-uri"
813*10441SSonam.Gupta@Sun.COM 		 * first time
8149152SSonam.Gupta@Sun.COM 		 */
8159152SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(attrs, NULL,
8169152SSonam.Gupta@Sun.COM 		    "job-printer-uri", &uri);
817*10441SSonam.Gupta@Sun.COM 
818*10441SSonam.Gupta@Sun.COM 		if (printer != NULL) {
819*10441SSonam.Gupta@Sun.COM 			/*
820*10441SSonam.Gupta@Sun.COM 			 * Set pname to the printer that is being
821*10441SSonam.Gupta@Sun.COM 			 * queried so that this can be used later
822*10441SSonam.Gupta@Sun.COM 			 * if 'report_job' is called multiple times for
823*10441SSonam.Gupta@Sun.COM 			 * different printers for one lpstat command
824*10441SSonam.Gupta@Sun.COM 			 */
825*10441SSonam.Gupta@Sun.COM 			pname = printer;
826*10441SSonam.Gupta@Sun.COM 		}
827*10441SSonam.Gupta@Sun.COM 
828*10441SSonam.Gupta@Sun.COM 		if (uri != NULL) {
829*10441SSonam.Gupta@Sun.COM 			/*
830*10441SSonam.Gupta@Sun.COM 			 * Set puri so that "job-printer-uri" corresponding
831*10441SSonam.Gupta@Sun.COM 			 * to a particular printer can be used later when
832*10441SSonam.Gupta@Sun.COM 			 * lpstat is queried for the same printer as
833*10441SSonam.Gupta@Sun.COM 			 * "job-printer-uri" for a printer is read just once.
834*10441SSonam.Gupta@Sun.COM 			 */
835*10441SSonam.Gupta@Sun.COM 			puri = strdup(uri);
836*10441SSonam.Gupta@Sun.COM 		}
837*10441SSonam.Gupta@Sun.COM 	} else {
838*10441SSonam.Gupta@Sun.COM 		/*
839*10441SSonam.Gupta@Sun.COM 		 * This part of the code will get executed when
840*10441SSonam.Gupta@Sun.COM 		 * 'report_job' is called more than once for the same
841*10441SSonam.Gupta@Sun.COM 		 * lpstat command
842*10441SSonam.Gupta@Sun.COM 		 */
843*10441SSonam.Gupta@Sun.COM 		if (printer != NULL) {
844*10441SSonam.Gupta@Sun.COM 			if (strcasecmp(pname, printer) != 0) {
845*10441SSonam.Gupta@Sun.COM 				/*
846*10441SSonam.Gupta@Sun.COM 				 * Read the job-printer-uri as
847*10441SSonam.Gupta@Sun.COM 				 * it will be different for
848*10441SSonam.Gupta@Sun.COM 				 * different printers
849*10441SSonam.Gupta@Sun.COM 				 */
850*10441SSonam.Gupta@Sun.COM 				uri = NULL;
851*10441SSonam.Gupta@Sun.COM 				(void) papiAttributeListGetString(attrs,
852*10441SSonam.Gupta@Sun.COM 				    NULL, "job-printer-uri", &uri);
853*10441SSonam.Gupta@Sun.COM 				pname = printer;
854*10441SSonam.Gupta@Sun.COM 				if (uri != NULL)
855*10441SSonam.Gupta@Sun.COM 					puri = strdup(uri);
856*10441SSonam.Gupta@Sun.COM 				else
857*10441SSonam.Gupta@Sun.COM 					puri = NULL;
858*10441SSonam.Gupta@Sun.COM 			} else {
859*10441SSonam.Gupta@Sun.COM 				/*
860*10441SSonam.Gupta@Sun.COM 				 * Same printer queried twice
861*10441SSonam.Gupta@Sun.COM 				 * uri should be the same as
862*10441SSonam.Gupta@Sun.COM 				 * already read in the previous call
863*10441SSonam.Gupta@Sun.COM 				 * to 'report_job'.
864*10441SSonam.Gupta@Sun.COM 				 * For the same printer 'job-printer-uri'
865*10441SSonam.Gupta@Sun.COM 				 * is read just once because only in the
866*10441SSonam.Gupta@Sun.COM 				 * first call it contains the host information
867*10441SSonam.Gupta@Sun.COM 				 */
868*10441SSonam.Gupta@Sun.COM 				uri = puri;
869*10441SSonam.Gupta@Sun.COM 			}
870*10441SSonam.Gupta@Sun.COM 		}
8719152SSonam.Gupta@Sun.COM 	}
8729152SSonam.Gupta@Sun.COM 
8739152SSonam.Gupta@Sun.COM 	if (host) {
8749152SSonam.Gupta@Sun.COM 		/* Check if it is local printer or remote printer */
8759152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
8769152SSonam.Gupta@Sun.COM 
8779152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
8789152SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
8799152SSonam.Gupta@Sun.COM 
8809152SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
8819152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
8829152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0)) {
8838321SSonam.Gupta@Sun.COM 
8849152SSonam.Gupta@Sun.COM 				if (strcasecmp(host, nodename) == 0) {
8859152SSonam.Gupta@Sun.COM 					/*
8869152SSonam.Gupta@Sun.COM 					 * Request submitted locally
8879152SSonam.Gupta@Sun.COM 					 * for the local queue.
8889152SSonam.Gupta@Sun.COM 					 * Hostname will not be displayed
8899152SSonam.Gupta@Sun.COM 					 */
8909152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s",
8919152SSonam.Gupta@Sun.COM 					    user);
8929152SSonam.Gupta@Sun.COM 				}
8939152SSonam.Gupta@Sun.COM 				else
8949152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s@%s",
8959152SSonam.Gupta@Sun.COM 					    user, host);
8969152SSonam.Gupta@Sun.COM 			} else if (uri != NULL) {
8979152SSonam.Gupta@Sun.COM 				/*
8989152SSonam.Gupta@Sun.COM 				 * It's a remote printer.
8999152SSonam.Gupta@Sun.COM 				 * In case of remote printers hostname is
9009152SSonam.Gupta@Sun.COM 				 * always displayed.
9019152SSonam.Gupta@Sun.COM 				 */
9029152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
9039152SSonam.Gupta@Sun.COM 				    user, host);
9049152SSonam.Gupta@Sun.COM 			}
9059152SSonam.Gupta@Sun.COM 			uri_free(u);
9069152SSonam.Gupta@Sun.COM 		} else {
9079152SSonam.Gupta@Sun.COM 			/*
9089152SSonam.Gupta@Sun.COM 			 * If attribute "job-printer-uri"
9099152SSonam.Gupta@Sun.COM 			 * cannot be read
9109152SSonam.Gupta@Sun.COM 			 * by default append the hostname
9119152SSonam.Gupta@Sun.COM 			 */
9129152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s@%s", user, host);
9139152SSonam.Gupta@Sun.COM 		}
9149152SSonam.Gupta@Sun.COM 	} else {
9159152SSonam.Gupta@Sun.COM 		/*
9169152SSonam.Gupta@Sun.COM 		 * When print server is s10u4 and ipp service is used
9179152SSonam.Gupta@Sun.COM 		 * "job-originating-hostname" attribute is not set
9189152SSonam.Gupta@Sun.COM 		 * So get the host information from the uri
9199152SSonam.Gupta@Sun.COM 		 */
9209152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
9219152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
9229152SSonam.Gupta@Sun.COM 			if ((u != NULL) && (u->host != NULL))
9239152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
9249152SSonam.Gupta@Sun.COM 				    user, u->host);
9259152SSonam.Gupta@Sun.COM 			else
9269152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s", user);
9279152SSonam.Gupta@Sun.COM 
9289152SSonam.Gupta@Sun.COM 			uri_free(u);
9299152SSonam.Gupta@Sun.COM 		} else
9309152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s", user);
9319152SSonam.Gupta@Sun.COM 	}
9322264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
9332264Sjacobs 	size *= 1024;	/* for the approximate byte size */
9342264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
9352264Sjacobs 
9362264Sjacobs 	(void) time(&clock);
9372264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
9388962SSonam.Gupta@Sun.COM 	    "time-at-creation", (int32_t *)&clock);
9392264Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
9402264Sjacobs 
9412264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
9428962SSonam.Gupta@Sun.COM 	    "job-printer-uri", &destination);
9432264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
9448962SSonam.Gupta@Sun.COM 	    "printer-name", &destination);
9452264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
9468962SSonam.Gupta@Sun.COM 	    "job-id", &id);
9479330SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetInteger(attrs, NULL,
9489330SSonam.Gupta@Sun.COM 	    "job-id-requested", &id);
9499256SSonam.Gupta@Sun.COM 
9509331SSonam.Gupta@Sun.COM 
9519256SSonam.Gupta@Sun.COM 	snprintf(request, sizeof (request), "%s-%d", printer, id);
9522264Sjacobs 
9532264Sjacobs 	if (show_rank != 0) {
9542264Sjacobs 		int32_t rank = -1;
9552264Sjacobs 
9562264Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
9578962SSonam.Gupta@Sun.COM 		    "number-of-intervening-jobs", &rank);
9582264Sjacobs 		rank++;
9592264Sjacobs 
9602264Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
9618321SSonam.Gupta@Sun.COM 		    rank, request, User, size, date);
9622264Sjacobs 	} else
9638321SSonam.Gupta@Sun.COM 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
9642264Sjacobs 
9652264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
9668962SSonam.Gupta@Sun.COM 	    "job-state", &jstate);
9679331SSonam.Gupta@Sun.COM 
9689257SGowtham.Thommandra@Sun.COM 	if (jstate == 0x0001)
9699257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being held"));
9709257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0800)
9719257SGowtham.Thommandra@Sun.COM 		printf(gettext(" notifying user"));
9729257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0040)
9739257SGowtham.Thommandra@Sun.COM 		printf(gettext(" cancelled"));
9749257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0010)
9759257SGowtham.Thommandra@Sun.COM 		printf(gettext(" finished printing"));
9769257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0008)
9779257SGowtham.Thommandra@Sun.COM 		printf(gettext(" on %s"), destination);
9789257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x2000)
9799257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held by admin"));
9809257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0002)
9819257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being filtered"));
9829257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0004)
9839257SGowtham.Thommandra@Sun.COM 		printf(gettext(" filtered"));
9849257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0020)
9859257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held for change"));
9862264Sjacobs 
9872264Sjacobs 	if (verbose == 1) {
9883125Sjacobs 		char *form = NULL;
9893125Sjacobs 
9902264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
9918962SSonam.Gupta@Sun.COM 		    "output-device-assigned", &destination);
9922264Sjacobs 		printf("\n\t assigned %s", destination);
9933127Sjacobs 
9943125Sjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
9953125Sjacobs 		if (form != NULL)
9963125Sjacobs 			printf(", form %s", form);
9972264Sjacobs 	} else if (verbose > 1) {
9982264Sjacobs 		printf("\n");
9992264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
10002264Sjacobs 	}
10012264Sjacobs 
10022264Sjacobs 	printf("\n");
10032264Sjacobs 
10042264Sjacobs 	return (0);
10052264Sjacobs }
10062264Sjacobs 
10072264Sjacobs static int
10089256SSonam.Gupta@Sun.COM job_query(char *request, int (*report)(char *, papi_job_t, int, int),
10092264Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
10102264Sjacobs {
10112264Sjacobs 	int result = 0;
10122264Sjacobs 	papi_status_t status;
10132264Sjacobs 	papi_service_t svc = NULL;
10148295SSonam.Gupta@Sun.COM 	char *printer = request;
10152264Sjacobs 	int32_t id = -1;
10168295SSonam.Gupta@Sun.COM 	int flag1 = 0;
10178295SSonam.Gupta@Sun.COM 	int flag = 1;
10188295SSonam.Gupta@Sun.COM 	int print_flag = 0;
10192264Sjacobs 
10208295SSonam.Gupta@Sun.COM 	do {
10218295SSonam.Gupta@Sun.COM 		status = papiServiceCreate(&svc, printer, NULL, NULL,
10228962SSonam.Gupta@Sun.COM 		    cli_auth_callback, encryption, NULL);
10232264Sjacobs 
10248295SSonam.Gupta@Sun.COM 		if ((status == PAPI_OK) && (printer != NULL))
10258295SSonam.Gupta@Sun.COM 			print_flag = 1;
10262264Sjacobs 
10278295SSonam.Gupta@Sun.COM 		/* <name>-# printer name does not exist */
10288295SSonam.Gupta@Sun.COM 		if (status != PAPI_OK) {
10298295SSonam.Gupta@Sun.COM 			/*
10308295SSonam.Gupta@Sun.COM 			 * Check if <name>-# is a request-id
10318295SSonam.Gupta@Sun.COM 			 * Once this check is done flag1 is set
10328295SSonam.Gupta@Sun.COM 			 */
10338295SSonam.Gupta@Sun.COM 			if (flag1 == 1)
10348295SSonam.Gupta@Sun.COM 				break;
10358295SSonam.Gupta@Sun.COM 
10368295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
10372264Sjacobs 
10388295SSonam.Gupta@Sun.COM 			status = papiServiceCreate(&svc, printer, NULL, NULL,
10398962SSonam.Gupta@Sun.COM 			    cli_auth_callback, encryption, NULL);
10402264Sjacobs 
10418295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
10428295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
10438295SSonam.Gupta@Sun.COM 				    "Failed to contact service for %s: %s\n"),
10448295SSonam.Gupta@Sun.COM 				    (printer ? printer : "all"),
10458295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
10468295SSonam.Gupta@Sun.COM 				return (-1);
10478295SSonam.Gupta@Sun.COM 			}
10482264Sjacobs 		}
10492264Sjacobs 
10508295SSonam.Gupta@Sun.COM 		if (printer == NULL) { /* all */
10518295SSonam.Gupta@Sun.COM 			char **interest = interest_list(svc);
10528295SSonam.Gupta@Sun.COM 
10538295SSonam.Gupta@Sun.COM 			if (interest != NULL) {
10548295SSonam.Gupta@Sun.COM 				int i;
10558295SSonam.Gupta@Sun.COM 
10568295SSonam.Gupta@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
10578295SSonam.Gupta@Sun.COM 					result += job_query(interest[i], report,
10588962SSonam.Gupta@Sun.COM 					    encryption, show_rank, verbose);
10598295SSonam.Gupta@Sun.COM 			}
10608295SSonam.Gupta@Sun.COM 		} else if (id == -1) { /* a printer */
10618295SSonam.Gupta@Sun.COM 			papi_job_t *jobs = NULL;
10628295SSonam.Gupta@Sun.COM 
10638295SSonam.Gupta@Sun.COM 			status = papiPrinterListJobs(svc, printer, NULL,
10648962SSonam.Gupta@Sun.COM 			    0, 0, &jobs);
10658295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
10668295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
10678295SSonam.Gupta@Sun.COM 				    "Failed to get job list: %s\n"),
10688295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
10698295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
10708295SSonam.Gupta@Sun.COM 				return (-1);
10718295SSonam.Gupta@Sun.COM 			}
10728295SSonam.Gupta@Sun.COM 
10738295SSonam.Gupta@Sun.COM 			if (jobs != NULL) {
10748295SSonam.Gupta@Sun.COM 				int i;
10752264Sjacobs 
10768295SSonam.Gupta@Sun.COM 				for (i = 0; jobs[i] != NULL; i++)
10779256SSonam.Gupta@Sun.COM 					result += report(printer,
10789256SSonam.Gupta@Sun.COM 					    jobs[i], show_rank,
10799256SSonam.Gupta@Sun.COM 					    verbose);
10808295SSonam.Gupta@Sun.COM 			}
10818295SSonam.Gupta@Sun.COM 
10828295SSonam.Gupta@Sun.COM 			papiJobListFree(jobs);
10838295SSonam.Gupta@Sun.COM 		} else {	/* a job */
10848295SSonam.Gupta@Sun.COM 			papi_job_t job = NULL;
10859331SSonam.Gupta@Sun.COM 			int rid = id;
10868295SSonam.Gupta@Sun.COM 
10878295SSonam.Gupta@Sun.COM 			/* Once a job has been found stop processing */
10888295SSonam.Gupta@Sun.COM 			flag = 0;
10898295SSonam.Gupta@Sun.COM 
10909331SSonam.Gupta@Sun.COM 			/*
10919331SSonam.Gupta@Sun.COM 			 * Job-id could be the job-id requested
10929331SSonam.Gupta@Sun.COM 			 * Check if it is job-id or job-id-requested
10939331SSonam.Gupta@Sun.COM 			 */
10949331SSonam.Gupta@Sun.COM 			id = job_to_be_queried(svc, printer, id);
10959331SSonam.Gupta@Sun.COM 
10969331SSonam.Gupta@Sun.COM 			if (id > 0)
10979669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, id,
10989669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
10999331SSonam.Gupta@Sun.COM 			else
11009669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, rid,
11019669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
11029331SSonam.Gupta@Sun.COM 
11038295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
11048295SSonam.Gupta@Sun.COM 				if (!print_flag)
11058295SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
11068962SSonam.Gupta@Sun.COM 					    "Failed to get job"\
11078962SSonam.Gupta@Sun.COM 					    " info for %s: %s\n"),
11088962SSonam.Gupta@Sun.COM 					    request,
11098962SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
11108295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
11118295SSonam.Gupta@Sun.COM 				return (-1);
11128295SSonam.Gupta@Sun.COM 			}
11138295SSonam.Gupta@Sun.COM 
11148295SSonam.Gupta@Sun.COM 			if (job != NULL)
11159256SSonam.Gupta@Sun.COM 				result = report(printer, job,
11169256SSonam.Gupta@Sun.COM 				    show_rank, verbose);
11178295SSonam.Gupta@Sun.COM 
11188295SSonam.Gupta@Sun.COM 			papiJobFree(job);
11192264Sjacobs 		}
11202264Sjacobs 
11218295SSonam.Gupta@Sun.COM 		if (flag) {
11228295SSonam.Gupta@Sun.COM 			id = -1;
11238295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
11248295SSonam.Gupta@Sun.COM 			if (id == -1)
11258295SSonam.Gupta@Sun.COM 				flag = 0;
11268295SSonam.Gupta@Sun.COM 			else
11278295SSonam.Gupta@Sun.COM 				flag1 = 1;
11282264Sjacobs 		}
11298295SSonam.Gupta@Sun.COM 	} while (flag);
11302264Sjacobs 
11312264Sjacobs 	papiServiceDestroy(svc);
11322264Sjacobs 
11332264Sjacobs 	return (result);
11342264Sjacobs }
11352264Sjacobs 
11362264Sjacobs static int
11372264Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
11382264Sjacobs {
11392264Sjacobs 	papi_status_t status;
11402264Sjacobs 	char *form = NULL;
11412264Sjacobs 	void *iter = NULL;
11422264Sjacobs 
11432264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
11448962SSonam.Gupta@Sun.COM 	    "form-supported", &form);
11458962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
11468962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter,
11478962SSonam.Gupta@Sun.COM 	    NULL, &form)) {
11482264Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
11492264Sjacobs 			printf(gettext("form %s is available to you\n"), form);
11502264Sjacobs 			if (verbose != 0) {
11512264Sjacobs 				char *detail = NULL;
11522264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
11538962SSonam.Gupta@Sun.COM 				    "form-supported-detail", &detail);
11542264Sjacobs 				if (status == PAPI_OK)
11552264Sjacobs 					printf("%s\n", detail);
11562264Sjacobs 			}
11572264Sjacobs 		}
11582264Sjacobs 	}
11592264Sjacobs 
11602264Sjacobs 	return (0);
11612264Sjacobs }
11622264Sjacobs 
11632264Sjacobs static int
11642264Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
11652264Sjacobs {
11662264Sjacobs 	papi_status_t status;
11672264Sjacobs 	char *pw = NULL;
11682264Sjacobs 	void *iter = NULL;
11692264Sjacobs 
11702264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
11718962SSonam.Gupta@Sun.COM 	    "pw-supported", &pw);
11728962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
11738962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
11742264Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
11752264Sjacobs 			printf(gettext("charset %s is available\n"), pw);
11762264Sjacobs 			if (verbose != 0) {
11772264Sjacobs 				char *info = NULL;
11782264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
11798962SSonam.Gupta@Sun.COM 				    "pw-supported-extra", &info);
11802264Sjacobs 				if (status == PAPI_OK)
11812264Sjacobs 					printf("%s\n", info);
11822264Sjacobs 			}
11832264Sjacobs 		}
11842264Sjacobs 	}
11852264Sjacobs 
11862264Sjacobs 	return (0);
11872264Sjacobs }
11882264Sjacobs 
11892264Sjacobs static int
11902264Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
11912264Sjacobs 		papi_encryption_t encryption, int verbose)
11922264Sjacobs {
11932264Sjacobs 	int result = 0;
11942264Sjacobs 	papi_status_t status;
11952264Sjacobs 	papi_service_t svc = NULL;
11962264Sjacobs 	papi_attribute_t **attrs = NULL;
11972264Sjacobs 
11982264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
11998962SSonam.Gupta@Sun.COM 	    encryption, NULL);
12002264Sjacobs 	if (status != PAPI_OK) {
12012264Sjacobs 		papiServiceDestroy(svc);
12022264Sjacobs 		return (-1);
12032264Sjacobs 	}
12042264Sjacobs 
12052264Sjacobs 	attrs = papiServiceGetAttributeList(svc);
12062264Sjacobs 	if (attrs != NULL) {
12072264Sjacobs 		result = report(name, attrs, verbose);
12082264Sjacobs 
12092264Sjacobs 		if (verbose > 1) {
12102264Sjacobs 			printf("\n");
12112264Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
12122264Sjacobs 			printf("\n");
12132264Sjacobs 		}
12142264Sjacobs 	}
12152264Sjacobs 
12162264Sjacobs 	papiServiceDestroy(svc);
12172264Sjacobs 
12182264Sjacobs 	return (result);
12192264Sjacobs }
12202264Sjacobs 
12212264Sjacobs int
12222264Sjacobs main(int ac, char *av[])
12232264Sjacobs {
12242264Sjacobs 	int exit_code = 0;
12252264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
12262264Sjacobs 	int rank = 0;
12272264Sjacobs 	int verbose = 0;
12282264Sjacobs 	int description = 0;
12292264Sjacobs 	int c;
12302264Sjacobs 	char **argv;
12312264Sjacobs 
12322264Sjacobs 	(void) setlocale(LC_ALL, "");
12332264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
12342264Sjacobs 
12352264Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
12364286Swendyp 	for (c = 0; c < ac; c++) {
12374286Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
12388962SSonam.Gupta@Sun.COM 		    (isalpha(av[c][2]) != 0)) {
12394286Swendyp 			/* preserve old "-l[po...]" behavior */
12404286Swendyp 			argv[c] = &av[c][1];
12414286Swendyp 			argv[c][0] = '-';
12424286Swendyp 			verbose = 1;
12434286Swendyp 
12444286Swendyp 		} else
12454286Swendyp 			argv[c] = av[c];
12464286Swendyp 	}
12474286Swendyp 
12482264Sjacobs 	argv[c++] = "--";
12492264Sjacobs 	ac = c;
12502264Sjacobs 
12512264Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
12529740SKeerthi.Kondaka@Sun.COM 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
12539740SKeerthi.Kondaka@Sun.COM 		switch (c) {    /* these may or may not have an option */
12549740SKeerthi.Kondaka@Sun.COM 		case 'a':
12559740SKeerthi.Kondaka@Sun.COM 		case 'c':
12569740SKeerthi.Kondaka@Sun.COM 		case 'p':
12579740SKeerthi.Kondaka@Sun.COM 		case 'o':
12589740SKeerthi.Kondaka@Sun.COM 		case 'R':
12599740SKeerthi.Kondaka@Sun.COM 		case 'u':
12609740SKeerthi.Kondaka@Sun.COM 		case 'v':
12619740SKeerthi.Kondaka@Sun.COM 		case 'l':
12629740SKeerthi.Kondaka@Sun.COM 		case 'f':
12639740SKeerthi.Kondaka@Sun.COM 		case 'S':
12649740SKeerthi.Kondaka@Sun.COM 			if (optarg[0] == '-') {
12659740SKeerthi.Kondaka@Sun.COM 				/* this check stop a possible infinite loop */
12669740SKeerthi.Kondaka@Sun.COM 				if ((optind > 1) && (argv[optind-1][1] != c))
12679740SKeerthi.Kondaka@Sun.COM 					optind--;
12689740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
12699740SKeerthi.Kondaka@Sun.COM 			} else if (strcmp(optarg, "all") == 0)
12709740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
12719740SKeerthi.Kondaka@Sun.COM 		}
12729740SKeerthi.Kondaka@Sun.COM 
12732264Sjacobs 		switch (c) {
12742264Sjacobs 		case 'l':
12752264Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
12762264Sjacobs 				optarg = "1";
12772264Sjacobs 			verbose = atoi(optarg);
12782264Sjacobs 			break;
12792264Sjacobs 		case 'D':
12802264Sjacobs 			description = 1;
12812264Sjacobs 			break;
12822264Sjacobs 		case 'R':
12832264Sjacobs 			rank = 1;
12842264Sjacobs 			break;
12852264Sjacobs 		case 'E':
12862264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
12872264Sjacobs 			break;
12882264Sjacobs 		default:
12892264Sjacobs 			break;
12902264Sjacobs 		}
12919740SKeerthi.Kondaka@Sun.COM 	}
12922264Sjacobs 	optind = 1;
12932264Sjacobs 
12942264Sjacobs 	/* process command line arguments */
12952264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
12962264Sjacobs 		switch (c) {	/* these may or may not have an option */
12972264Sjacobs 		case 'a':
12982264Sjacobs 		case 'c':
12992264Sjacobs 		case 'p':
13002264Sjacobs 		case 'o':
13012264Sjacobs 		case 'R':
13022264Sjacobs 		case 'u':
13032264Sjacobs 		case 'v':
13042264Sjacobs 		case 'l':
13052264Sjacobs 		case 'f':
13062264Sjacobs 		case 'S':
13072264Sjacobs 			if (optarg[0] == '-') {
13082264Sjacobs 				/* this check stop a possible infinite loop */
13092264Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
13102264Sjacobs 					optind--;
13112264Sjacobs 				optarg = NULL;
13122264Sjacobs 			} else if (strcmp(optarg, "all") == 0)
13132264Sjacobs 				optarg = NULL;
13142264Sjacobs 		}
13152264Sjacobs 
13162264Sjacobs 		switch (c) {
13172264Sjacobs 		case 'a':
13182264Sjacobs 			exit_code += printer_query(optarg, report_accepting,
13198962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13202264Sjacobs 			break;
13212264Sjacobs 		case 'c':
13222264Sjacobs 			exit_code += printer_query(optarg, report_class,
13238962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13242264Sjacobs 			break;
13252264Sjacobs 		case 'p':
13262264Sjacobs 			exit_code += printer_query(optarg, report_printer,
13278962SSonam.Gupta@Sun.COM 			    encryption, verbose, description);
13282264Sjacobs 			break;
13292264Sjacobs 		case 'd':
13302264Sjacobs 			exit_code += lpstat_default_printer(encryption);
13312264Sjacobs 			break;
13322264Sjacobs 		case 'r':
13332264Sjacobs 			exit_code += lpstat_service_status(encryption);
13342264Sjacobs 			break;
13352264Sjacobs 		case 'u':
13362264Sjacobs 			if (optarg != NULL)
13372264Sjacobs 				users = strsplit(optarg, ", \n");
13382264Sjacobs 			exit_code += job_query(NULL, report_job,
13398962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
13402264Sjacobs 			if (users != NULL) {
13412264Sjacobs 				free(users);
13422264Sjacobs 				users = NULL;
13432264Sjacobs 			}
13442264Sjacobs 			break;
13452264Sjacobs 		case 'v':
13462264Sjacobs 			exit_code += printer_query(optarg, report_device,
13478962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13482264Sjacobs 			break;
13498604SGowtham.Thommandra@Sun.COM 		case 'R':	/* set "rank" flag in first pass */
13502264Sjacobs 		case 'o':
13512264Sjacobs 			exit_code += job_query(optarg, report_job,
13528962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
13532264Sjacobs 			break;
13542264Sjacobs 		case 'f':
13552264Sjacobs 			exit_code += service_query(optarg, report_form,
13568962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13572264Sjacobs 			break;
13582264Sjacobs 		case 'S':
13592264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
13608962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13612264Sjacobs 			break;
13622264Sjacobs 		case 's':
13632264Sjacobs 			exit_code += lpstat_service_status(encryption);
13642264Sjacobs 			exit_code += lpstat_default_printer(encryption);
13652264Sjacobs 			exit_code += printer_query(NULL, report_class,
13668962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13672264Sjacobs 			exit_code += printer_query(NULL, report_device,
13688962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13692264Sjacobs 			exit_code += service_query(optarg, report_form,
13708962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13712264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
13728962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13732264Sjacobs 			break;
13742264Sjacobs 		case 't':
13752264Sjacobs 			exit_code += lpstat_service_status(encryption);
13762264Sjacobs 			exit_code += lpstat_default_printer(encryption);
13772264Sjacobs 			exit_code += printer_query(NULL, report_class,
13788962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13792264Sjacobs 			exit_code += printer_query(NULL, report_device,
13808962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13812264Sjacobs 			exit_code += printer_query(NULL, report_accepting,
13828962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13832264Sjacobs 			exit_code += printer_query(NULL, report_printer,
13848962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13852264Sjacobs 			exit_code += service_query(optarg, report_form,
13868962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13872264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
13888962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13892264Sjacobs 			exit_code += job_query(NULL, report_job,
13908962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
13912264Sjacobs 			break;
13922264Sjacobs 		case 'L':	/* local-only, ignored */
13932264Sjacobs 		case 'l':	/* increased verbose level in first pass */
13942264Sjacobs 		case 'D':	/* set "description" flag in first pass */
13952264Sjacobs 		case 'E':	/* set encryption in the first pass */
13962264Sjacobs 			break;
13972264Sjacobs 		default:
13982264Sjacobs 			usage(av[0]);
13992264Sjacobs 		}
14002264Sjacobs 	}
14012264Sjacobs 	ac--;
14022264Sjacobs 
14032264Sjacobs 	if (ac == 1) {	/* report on my jobs */
14042264Sjacobs 		struct passwd *pw = getpwuid(getuid());
14052264Sjacobs 
14062264Sjacobs 		if (pw != NULL)
14072264Sjacobs 			users = strsplit(pw->pw_name, "");
14082264Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
14098962SSonam.Gupta@Sun.COM 		    rank, verbose);
14102264Sjacobs 		if (users != NULL) {
14112264Sjacobs 			free(users);
14122264Sjacobs 			users = NULL;
14132264Sjacobs 		}
14142264Sjacobs 	} else {
14152264Sjacobs 		for (c = optind; c < ac; c++)
14162264Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
14178962SSonam.Gupta@Sun.COM 			    rank, verbose);
14182264Sjacobs 	}
14192264Sjacobs 
14202264Sjacobs 
14212264Sjacobs 	if (exit_code != 0)
14222264Sjacobs 		exit_code = 1;
14232264Sjacobs 
14242264Sjacobs 	return (exit_code);
14252264Sjacobs }
1426