xref: /onnv-gate/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 12028:a1c417eab1c7)
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 /*
23*12028SSonam.Gupta@Sun.COM  * Copyright 2010 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
usage(char * program)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 *
nctime(time_t * t)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 *
printer_name(papi_printer_t printer)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
lpstat_default_printer(papi_encryption_t encryption)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
lpstat_service_status(papi_encryption_t encryption)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 *
get_device_uri(papi_service_t svc,char * name)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
print_description(papi_attribute_t ** list,char * printer_name)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
report_device(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)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
report_accepting(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)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
report_class(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)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
get_remote_hostname(papi_attribute_t ** attrs,char ** host)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
report_printer(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)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 */
40610578SSonam.Gupta@Sun.COM 		printf(gettext("is idle. enabled"));
4072264Sjacobs 		break;
4088962SSonam.Gupta@Sun.COM 	case 0x04: /* processing */
409*12028SSonam.Gupta@Sun.COM 	case 0x06: /* faulted printing */
4108962SSonam.Gupta@Sun.COM 		status = papiPrinterListJobs(svc, name, NULL,
4118962SSonam.Gupta@Sun.COM 		    0, 0, &j);
4128962SSonam.Gupta@Sun.COM 
4138962SSonam.Gupta@Sun.COM 		if (status == PAPI_OK) {
4148962SSonam.Gupta@Sun.COM 			if (j != NULL) {
4158962SSonam.Gupta@Sun.COM 				int i = 0;
4168962SSonam.Gupta@Sun.COM 				int32_t jobid = 0;
4178962SSonam.Gupta@Sun.COM 				int32_t jstate = 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);
42710338SSonam.Gupta@Sun.COM 					/*
42810338SSonam.Gupta@Sun.COM 					 * For lpd protocol "job-id-requested"
42910338SSonam.Gupta@Sun.COM 					 * should be read.
43010338SSonam.Gupta@Sun.COM 					 */
43110338SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
43210338SSonam.Gupta@Sun.COM 					    NULL, "job-id-requested", &jobid);
4332264Sjacobs 
4348962SSonam.Gupta@Sun.COM 					/*
43510338SSonam.Gupta@Sun.COM 					 * When lpd protocol is used job-state
43610338SSonam.Gupta@Sun.COM 					 * cannot be retrieved, therefore
43710338SSonam.Gupta@Sun.COM 					 * job-state will be 0.
43810338SSonam.Gupta@Sun.COM 					 * When ipp protocol is used, the
43910338SSonam.Gupta@Sun.COM 					 * active/printing job-state will be
44010338SSonam.Gupta@Sun.COM 					 * RS_PRINTING (0x0008) post s10u5.
44110338SSonam.Gupta@Sun.COM 					 * For pre-s10u5 job-state will be
44210338SSonam.Gupta@Sun.COM 					 * RS_ACTIVE (0x05). So print only when
44310338SSonam.Gupta@Sun.COM 					 * the job-state is RS_PRINTING (0x0008)
44410338SSonam.Gupta@Sun.COM 					 * or RS_ACTIVE (0x05) or 0
4458962SSonam.Gupta@Sun.COM 					 */
44610338SSonam.Gupta@Sun.COM 					if ((jstate == 0x0008) ||
44710338SSonam.Gupta@Sun.COM 					    (jstate == 0x05) ||
44810338SSonam.Gupta@Sun.COM 					    (jstate == 0)) {
449*12028SSonam.Gupta@Sun.COM 						if (pstat == 0x04)
450*12028SSonam.Gupta@Sun.COM 							printf(gettext
451*12028SSonam.Gupta@Sun.COM 							    ("now printing"\
452*12028SSonam.Gupta@Sun.COM 							    " %s-%d. enabled"),
453*12028SSonam.Gupta@Sun.COM 							    name, jobid);
454*12028SSonam.Gupta@Sun.COM 						if (pstat == 0x06)
455*12028SSonam.Gupta@Sun.COM 							printf(gettext
456*12028SSonam.Gupta@Sun.COM 							    ("faulted printing"\
457*12028SSonam.Gupta@Sun.COM 							    " %s-%d. enabled"),
458*12028SSonam.Gupta@Sun.COM 							    name, jobid);
45910338SSonam.Gupta@Sun.COM 						break;
4608962SSonam.Gupta@Sun.COM 					}
4618962SSonam.Gupta@Sun.COM 				}
4628962SSonam.Gupta@Sun.COM 				papiJobListFree(j);
4638962SSonam.Gupta@Sun.COM 			}
4642264Sjacobs 		}
4652264Sjacobs 		break;
4662264Sjacobs 	case 0x05:	/* stopped */
4672264Sjacobs 		printf(gettext("disabled"));
4682264Sjacobs 		break;
469*12028SSonam.Gupta@Sun.COM 	case 0x07:	/* faulted printer */
470*12028SSonam.Gupta@Sun.COM 		printf(gettext("faulted. enabled"));
471*12028SSonam.Gupta@Sun.COM 		break;
472*12028SSonam.Gupta@Sun.COM 	case 0x08:	/* waiting for auto retry */
473*12028SSonam.Gupta@Sun.COM 		printf(gettext("waiting for auto-retry."));
474*12028SSonam.Gupta@Sun.COM 		break;
4752264Sjacobs 	default:
4762264Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
4772264Sjacobs 		break;
4782264Sjacobs 	}
4792264Sjacobs 
480*12028SSonam.Gupta@Sun.COM 	if (pstat == 0x08)
481*12028SSonam.Gupta@Sun.COM 		printf(gettext(" available.\n"));
482*12028SSonam.Gupta@Sun.COM 	else {
483*12028SSonam.Gupta@Sun.COM 		(void) time(&curr);
484*12028SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetDatetime(attrs, NULL,
485*12028SSonam.Gupta@Sun.COM 		    "printer-up-time", &curr);
486*12028SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetDatetime(attrs, NULL,
487*12028SSonam.Gupta@Sun.COM 		    "printer-state-time", &curr);
488*12028SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetDatetime(attrs, NULL,
489*12028SSonam.Gupta@Sun.COM 		    "lpsched-disable-date", &curr);
490*12028SSonam.Gupta@Sun.COM 		printf(gettext(" since %s. available.\n"), nctime(&curr));
491*12028SSonam.Gupta@Sun.COM 	}
4922264Sjacobs 
493*12028SSonam.Gupta@Sun.COM 	if ((pstat == 0x05) ||
494*12028SSonam.Gupta@Sun.COM 	    (pstat == 0x06) ||
495*12028SSonam.Gupta@Sun.COM 	    (pstat == 0x07) ||
496*12028SSonam.Gupta@Sun.COM 	    (pstat == 0x08)) {
4972264Sjacobs 		char *reason = "unknown reason";
4982264Sjacobs 
4992264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5008962SSonam.Gupta@Sun.COM 		    "printer-state-reasons", &reason);
5012264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5028962SSonam.Gupta@Sun.COM 		    "lpsched-disable-reason", &reason);
5032264Sjacobs 		printf(gettext("\t%s\n"), reason);
5042264Sjacobs 	}
5052264Sjacobs 
5062264Sjacobs 	if (verbose == 1) {
5072264Sjacobs 		void *iter;
5082264Sjacobs 		char *str;
5099810SKeerthi.Kondaka@Sun.COM 		char *host = NULL;
5102264Sjacobs 
5119810SKeerthi.Kondaka@Sun.COM 		if ((get_remote_hostname(attrs, &host)) != 0) {
5129810SKeerthi.Kondaka@Sun.COM 			(void) printf(
5139810SKeerthi.Kondaka@Sun.COM 			    gettext("\tRemote Name: %s\n\tRemote Server: "
5149810SKeerthi.Kondaka@Sun.COM 			    "%s\n"), name, host);
5159810SKeerthi.Kondaka@Sun.COM 			free(host);
5169810SKeerthi.Kondaka@Sun.COM 			return (0);
5179810SKeerthi.Kondaka@Sun.COM 		}
5182264Sjacobs 		str = "";
5192264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5208962SSonam.Gupta@Sun.COM 		    "form-ready", &str);
5212264Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
5222264Sjacobs 
5232264Sjacobs 		str = "";
5242264Sjacobs 		iter = NULL;
5252264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5268962SSonam.Gupta@Sun.COM 		    "document-format-supported", &str);
5272264Sjacobs 		printf(gettext("\tContent types: %s"), str);
5282264Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
5298962SSonam.Gupta@Sun.COM 		    == PAPI_OK)
5302264Sjacobs 			printf(", %s", str);
5312264Sjacobs 		printf("\n");
5322264Sjacobs 
5338534SSonam.Gupta@Sun.COM 		/* Display the printer description */
5348534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
5352264Sjacobs 
5362264Sjacobs 		str = "";
5376676Swendyp 		iter = NULL;
5386676Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
5396676Swendyp 		    "lpsched-printer-type", &str);
5406676Swendyp 		printf(gettext("\tPrinter types: %s"), str);
5416676Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
5426676Swendyp 		    == PAPI_OK)
5436676Swendyp 			printf(", %s", str);
5446676Swendyp 		printf("\n");
5456676Swendyp 
5466676Swendyp 		str = "";
5472264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5488962SSonam.Gupta@Sun.COM 		    "lpsched-dial-info", &str);
5492264Sjacobs 		printf(gettext("\tConnection: %s\n"),
5507653SJonathan.Ca@Sun.COM 		    ((str[0] == '\0') ? gettext("direct") : str));
5512264Sjacobs 
5522264Sjacobs 		str = "";
5532264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5548962SSonam.Gupta@Sun.COM 		    "lpsched-interface-script", &str);
5552264Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
5562264Sjacobs 
5572264Sjacobs 		str = NULL;
5582264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5598962SSonam.Gupta@Sun.COM 		    "ppd-file-uri", &str);
5602264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5618962SSonam.Gupta@Sun.COM 		    "lpsched-ppd-source-path", &str);
5622264Sjacobs 		if (str != NULL)
5632264Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
5642264Sjacobs 
5652264Sjacobs 		str = NULL;
5662264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5678962SSonam.Gupta@Sun.COM 		    "lpsched-fault-alert-command", &str);
5682264Sjacobs 		if (str != NULL)
5692264Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
5702264Sjacobs 
5712264Sjacobs 		str = "";
5722264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5738962SSonam.Gupta@Sun.COM 		    "lpsched-fault-recovery", &str);
5742264Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
5758962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("continue") : str));
5762264Sjacobs 
5772264Sjacobs 		str = "(all)";
5782264Sjacobs 		iter = NULL;
5792264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5808962SSonam.Gupta@Sun.COM 		    "requesting-user-name-allowed", &str);
5812264Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
5828962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5832264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5842264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5858962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5862264Sjacobs 				printf("\t\t%s\n", str);
5872264Sjacobs 
5882264Sjacobs 		str = NULL;
5892264Sjacobs 		iter = NULL;
5902264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5918962SSonam.Gupta@Sun.COM 		    "requesting-user-name-denied", &str);
5922264Sjacobs 		if (str != NULL) {
5932264Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
5948962SSonam.Gupta@Sun.COM 			    ((str[0] == '\0') ? gettext("(none)") : str));
5952264Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
5962264Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
5978962SSonam.Gupta@Sun.COM 				    NULL, &str) == PAPI_OK)
5982264Sjacobs 					printf("\t\t%s\n", str);
5992264Sjacobs 		}
6002264Sjacobs 
6018238SSonam.Gupta@Sun.COM 		str = "none";
6022264Sjacobs 		iter = NULL;
6032264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6048962SSonam.Gupta@Sun.COM 		    "form-supported", &str);
6058238SSonam.Gupta@Sun.COM 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
6068962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("none") : str));
6072264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
6082264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6098962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6108238SSonam.Gupta@Sun.COM 				printf("\t\t(%s)\n", str);
6112264Sjacobs 
6122264Sjacobs 		str = "";
6132264Sjacobs 		iter = NULL;
6142264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6158962SSonam.Gupta@Sun.COM 		    "media-supported", &str);
6162264Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
6178962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
6182264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
6192264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6208962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6212264Sjacobs 				printf("\t\t%s\n", str);
6222264Sjacobs 
6232264Sjacobs 		str = "";
6242264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
6258962SSonam.Gupta@Sun.COM 		    "job-sheets-supported", &str);
6266676Swendyp 		if ((strcasecmp(str, "none")) == 0)
6276676Swendyp 			str = gettext("page never printed");
6286676Swendyp 		else if (strcasecmp(str, "optional") == 0)
6296676Swendyp 			str = gettext("not required");
6306676Swendyp 		else
6316676Swendyp 			str = gettext("required");
6326676Swendyp 
6336676Swendyp 		printf(gettext("\tBanner %s\n"), str);
6346676Swendyp 
6352264Sjacobs 
6362264Sjacobs 		str = "";
6372264Sjacobs 		iter = NULL;
6382264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6398962SSonam.Gupta@Sun.COM 		    "lpsched-print-wheels", &str);
6402264Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
6418962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
6422264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
6432264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6448962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6452264Sjacobs 				printf("\t\t%s\n", str);
6462264Sjacobs 
6472264Sjacobs 		printf(gettext("\tDefault pitch:\n"));
6482264Sjacobs 		printf(gettext("\tDefault page size:\n"));
6492264Sjacobs 		printf(gettext("\tDefault port setting:\n"));
6502264Sjacobs 
6512264Sjacobs 		str = "";
6522264Sjacobs 		iter = NULL;
6532264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6548962SSonam.Gupta@Sun.COM 		    "lpsched-options", &str);
6552264Sjacobs 		if (str != NULL) {
6562264Sjacobs 			printf(gettext("\tOptions: %s"), str);
6572264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6588962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6592264Sjacobs 				printf(", %s", str);
6602264Sjacobs 			printf("\n");
6612264Sjacobs 		}
6622264Sjacobs 
6638534SSonam.Gupta@Sun.COM 	} else if (description == 1)
6648534SSonam.Gupta@Sun.COM 		/* Display printer description */
6658534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
6668534SSonam.Gupta@Sun.COM 	else if (verbose > 1)
6672264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
6682264Sjacobs 
6692264Sjacobs 	if (verbose > 0)
6702264Sjacobs 		printf("\n");
6712264Sjacobs 
6722264Sjacobs 	return (0);
6732264Sjacobs }
6742264Sjacobs 
6752264Sjacobs static int
printer_query(char * name,int (* report)(papi_service_t,char *,papi_printer_t,int,int),papi_encryption_t encryption,int verbose,int description)6762264Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
6772264Sjacobs 					int, int), papi_encryption_t encryption,
6782264Sjacobs 		int verbose, int description)
6792264Sjacobs {
6809669SGowtham.Thommandra@Sun.COM 	int result = 0, i = 0;
6812264Sjacobs 	papi_status_t status;
6822264Sjacobs 	papi_service_t svc = NULL;
6839669SGowtham.Thommandra@Sun.COM 	char **list = getlist(name, LP_WS, LP_SEP);
6842264Sjacobs 
6859669SGowtham.Thommandra@Sun.COM 	if (list == NULL) {
6869669SGowtham.Thommandra@Sun.COM 		list = (char **)malloc(sizeof (char *));
6879669SGowtham.Thommandra@Sun.COM 		list[0] = name;
6882264Sjacobs 	}
6892264Sjacobs 
6909669SGowtham.Thommandra@Sun.COM 	/*
6919669SGowtham.Thommandra@Sun.COM 	 * The for loop executes once for every printer
6929669SGowtham.Thommandra@Sun.COM 	 * entry in list. If list is NULL that implies
6939669SGowtham.Thommandra@Sun.COM 	 * name is also NULL, the loop runs only one time.
6949669SGowtham.Thommandra@Sun.COM 	 */
6952264Sjacobs 
6969669SGowtham.Thommandra@Sun.COM 	for (i = 0; name == NULL || list[i] != NULL; i++) {
6979669SGowtham.Thommandra@Sun.COM 		name = list[i];
6982264Sjacobs 
6999669SGowtham.Thommandra@Sun.COM 		status = papiServiceCreate(&svc, name, NULL, NULL,
7009669SGowtham.Thommandra@Sun.COM 		    cli_auth_callback, encryption, NULL);
7012264Sjacobs 		if (status != PAPI_OK) {
7029669SGowtham.Thommandra@Sun.COM 			if (status == PAPI_NOT_FOUND)
7039669SGowtham.Thommandra@Sun.COM 				fprintf(stderr,
7049669SGowtham.Thommandra@Sun.COM 				    gettext("%s: unknown printer\n"),
7059669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)");
7069669SGowtham.Thommandra@Sun.COM 			else
7079669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
7089669SGowtham.Thommandra@Sun.COM 				    "Failed to contact service for %s: %s\n"),
7099669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)",
7109669SGowtham.Thommandra@Sun.COM 				    verbose_papi_message(svc, status));
7112264Sjacobs 			papiServiceDestroy(svc);
7129669SGowtham.Thommandra@Sun.COM 			result--;
7139669SGowtham.Thommandra@Sun.COM 			continue;
7142264Sjacobs 		}
7152264Sjacobs 
7169669SGowtham.Thommandra@Sun.COM 		if (name == NULL) { /* all */
7179669SGowtham.Thommandra@Sun.COM 			char **interest = interest_list(svc);
7189669SGowtham.Thommandra@Sun.COM 
7199669SGowtham.Thommandra@Sun.COM 			if (interest != NULL) {
7209669SGowtham.Thommandra@Sun.COM 				int i;
7219669SGowtham.Thommandra@Sun.COM 
7229669SGowtham.Thommandra@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
7239669SGowtham.Thommandra@Sun.COM 					result += printer_query(interest[i],
7249669SGowtham.Thommandra@Sun.COM 					    report, encryption, verbose,
7259669SGowtham.Thommandra@Sun.COM 					    description);
7269669SGowtham.Thommandra@Sun.COM 			}
7279669SGowtham.Thommandra@Sun.COM 		} else {
7289669SGowtham.Thommandra@Sun.COM 			papi_printer_t printer = NULL;
7299669SGowtham.Thommandra@Sun.COM 			char **keys = NULL;
7302264Sjacobs 
7319669SGowtham.Thommandra@Sun.COM 			/*
7329669SGowtham.Thommandra@Sun.COM 			 * Limit the query to only required data
7339669SGowtham.Thommandra@Sun.COM 			 * to reduce the need to go remote for
7349669SGowtham.Thommandra@Sun.COM 			 * information.
7359669SGowtham.Thommandra@Sun.COM 			 */
7369669SGowtham.Thommandra@Sun.COM 			if (report == report_device)
7379669SGowtham.Thommandra@Sun.COM 				keys = report_device_keys;
7389669SGowtham.Thommandra@Sun.COM 			else if (report == report_class)
7399669SGowtham.Thommandra@Sun.COM 				keys = report_class_keys;
7409669SGowtham.Thommandra@Sun.COM 			else if (report == report_accepting)
7419669SGowtham.Thommandra@Sun.COM 				keys = report_accepting_keys;
7429669SGowtham.Thommandra@Sun.COM 			else if ((report == report_printer) && (verbose == 0))
7439669SGowtham.Thommandra@Sun.COM 				keys = report_printer_keys;
7449669SGowtham.Thommandra@Sun.COM 
7459669SGowtham.Thommandra@Sun.COM 			status = papiPrinterQuery(svc, name, keys,
7469669SGowtham.Thommandra@Sun.COM 			    NULL, &printer);
7479669SGowtham.Thommandra@Sun.COM 			if (status != PAPI_OK) {
7489669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
7499669SGowtham.Thommandra@Sun.COM 				    "Failed to get printer info for %s: %s\n"),
7509669SGowtham.Thommandra@Sun.COM 				    name, verbose_papi_message(svc, status));
7519669SGowtham.Thommandra@Sun.COM 				papiServiceDestroy(svc);
7529669SGowtham.Thommandra@Sun.COM 				result--;
7539669SGowtham.Thommandra@Sun.COM 				continue;
7549669SGowtham.Thommandra@Sun.COM 			}
7559669SGowtham.Thommandra@Sun.COM 
7569669SGowtham.Thommandra@Sun.COM 			if (printer != NULL)
7579669SGowtham.Thommandra@Sun.COM 				result += report(svc, name, printer, verbose,
7589669SGowtham.Thommandra@Sun.COM 				    description);
7599669SGowtham.Thommandra@Sun.COM 
7609669SGowtham.Thommandra@Sun.COM 			papiPrinterFree(printer);
7619669SGowtham.Thommandra@Sun.COM 		}
7629669SGowtham.Thommandra@Sun.COM 
7639669SGowtham.Thommandra@Sun.COM 		papiServiceDestroy(svc);
7649669SGowtham.Thommandra@Sun.COM 
7659669SGowtham.Thommandra@Sun.COM 		if (name == NULL)
7669669SGowtham.Thommandra@Sun.COM 			break;
7672264Sjacobs 	}
7682264Sjacobs 
7699669SGowtham.Thommandra@Sun.COM 	freelist(list);
7702264Sjacobs 
7712264Sjacobs 	return (result);
7722264Sjacobs }
7732264Sjacobs 
7742264Sjacobs static int
match_user(char * user,char ** list)7752264Sjacobs match_user(char *user, char **list)
7762264Sjacobs {
7772264Sjacobs 	int i;
7782264Sjacobs 
7792264Sjacobs 	for (i = 0; list[i] != NULL; i++) {
7802264Sjacobs 		if (strcmp(user, list[i]) == 0)
7812264Sjacobs 			return (0);
7822264Sjacobs 	}
7832264Sjacobs 
7842264Sjacobs 	return (-1);
7852264Sjacobs }
7862264Sjacobs 
7872264Sjacobs static char **users = NULL;
7882264Sjacobs 
7892264Sjacobs static int
report_job(char * printer,papi_job_t job,int show_rank,int verbose)7909256SSonam.Gupta@Sun.COM report_job(char *printer, papi_job_t job, int show_rank, int verbose)
7912264Sjacobs {
7922264Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
7932264Sjacobs 	time_t clock = 0;
7942264Sjacobs 	char date[24];
7952264Sjacobs 	char request[26];
7962264Sjacobs 	char *user = "unknown";
7978321SSonam.Gupta@Sun.COM 	char *host = NULL;
7982264Sjacobs 	int32_t size = 0;
7992264Sjacobs 	int32_t jstate = 0;
8008321SSonam.Gupta@Sun.COM 	char User[50];
8012264Sjacobs 
8022264Sjacobs 	char *destination = "unknown";
8032264Sjacobs 	int32_t id = -1;
8049152SSonam.Gupta@Sun.COM 	static int check = 0;
8059152SSonam.Gupta@Sun.COM 	static char *uri = NULL;
80610441SSonam.Gupta@Sun.COM 	static char *puri = NULL;	/* printer-uri */
80710441SSonam.Gupta@Sun.COM 	static char *pname = NULL;	/* printer-name */
8082264Sjacobs 
8092264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
8108962SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
8112264Sjacobs 
8122264Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
8132264Sjacobs 		return (0);
8142264Sjacobs 
8158321SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
8168321SSonam.Gupta@Sun.COM 	    "job-originating-host-name", &host);
8178321SSonam.Gupta@Sun.COM 
81810441SSonam.Gupta@Sun.COM 	/*
81910441SSonam.Gupta@Sun.COM 	 * When lpstat is called for multiple printers
82010441SSonam.Gupta@Sun.COM 	 * internally the function 'report_job' gets
82110441SSonam.Gupta@Sun.COM 	 * called multiple times with different printer-names.
82210441SSonam.Gupta@Sun.COM 	 * The following block of code handles the case when lpstat is
82310441SSonam.Gupta@Sun.COM 	 * executed for multiple printers. In other words when 'report_job'
82410441SSonam.Gupta@Sun.COM 	 * is called multiple times for different printers for
82510441SSonam.Gupta@Sun.COM 	 * one lpstat command
82610441SSonam.Gupta@Sun.COM 	 * For e.g: lpstat printer1 printer2 printer3
82710441SSonam.Gupta@Sun.COM 	 */
82810441SSonam.Gupta@Sun.COM 	if (pname == NULL) {
8299152SSonam.Gupta@Sun.COM 		/*
83010441SSonam.Gupta@Sun.COM 		 * When lpstat is queried for the first time
83110441SSonam.Gupta@Sun.COM 		 * pname is NULL so this part of the code gets executed.
8329152SSonam.Gupta@Sun.COM 		 * Read the attribute "job-printer-uri"
83310441SSonam.Gupta@Sun.COM 		 * first time
8349152SSonam.Gupta@Sun.COM 		 */
8359152SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(attrs, NULL,
8369152SSonam.Gupta@Sun.COM 		    "job-printer-uri", &uri);
83710441SSonam.Gupta@Sun.COM 
83810441SSonam.Gupta@Sun.COM 		if (printer != NULL) {
83910441SSonam.Gupta@Sun.COM 			/*
84010441SSonam.Gupta@Sun.COM 			 * Set pname to the printer that is being
84110441SSonam.Gupta@Sun.COM 			 * queried so that this can be used later
84210441SSonam.Gupta@Sun.COM 			 * if 'report_job' is called multiple times for
84310441SSonam.Gupta@Sun.COM 			 * different printers for one lpstat command
84410441SSonam.Gupta@Sun.COM 			 */
84510441SSonam.Gupta@Sun.COM 			pname = printer;
84610441SSonam.Gupta@Sun.COM 		}
84710441SSonam.Gupta@Sun.COM 
84810441SSonam.Gupta@Sun.COM 		if (uri != NULL) {
84910441SSonam.Gupta@Sun.COM 			/*
85010441SSonam.Gupta@Sun.COM 			 * Set puri so that "job-printer-uri" corresponding
85110441SSonam.Gupta@Sun.COM 			 * to a particular printer can be used later when
85210441SSonam.Gupta@Sun.COM 			 * lpstat is queried for the same printer as
85310441SSonam.Gupta@Sun.COM 			 * "job-printer-uri" for a printer is read just once.
85410441SSonam.Gupta@Sun.COM 			 */
85510441SSonam.Gupta@Sun.COM 			puri = strdup(uri);
85610441SSonam.Gupta@Sun.COM 		}
85710441SSonam.Gupta@Sun.COM 	} else {
85810441SSonam.Gupta@Sun.COM 		/*
85910441SSonam.Gupta@Sun.COM 		 * This part of the code will get executed when
86010441SSonam.Gupta@Sun.COM 		 * 'report_job' is called more than once for the same
86110441SSonam.Gupta@Sun.COM 		 * lpstat command
86210441SSonam.Gupta@Sun.COM 		 */
86310441SSonam.Gupta@Sun.COM 		if (printer != NULL) {
86410441SSonam.Gupta@Sun.COM 			if (strcasecmp(pname, printer) != 0) {
86510441SSonam.Gupta@Sun.COM 				/*
86610441SSonam.Gupta@Sun.COM 				 * Read the job-printer-uri as
86710441SSonam.Gupta@Sun.COM 				 * it will be different for
86810441SSonam.Gupta@Sun.COM 				 * different printers
86910441SSonam.Gupta@Sun.COM 				 */
87010441SSonam.Gupta@Sun.COM 				uri = NULL;
87110441SSonam.Gupta@Sun.COM 				(void) papiAttributeListGetString(attrs,
87210441SSonam.Gupta@Sun.COM 				    NULL, "job-printer-uri", &uri);
87310441SSonam.Gupta@Sun.COM 				pname = printer;
87410441SSonam.Gupta@Sun.COM 				if (uri != NULL)
87510441SSonam.Gupta@Sun.COM 					puri = strdup(uri);
87610441SSonam.Gupta@Sun.COM 				else
87710441SSonam.Gupta@Sun.COM 					puri = NULL;
87810441SSonam.Gupta@Sun.COM 			} else {
87910441SSonam.Gupta@Sun.COM 				/*
88010441SSonam.Gupta@Sun.COM 				 * Same printer queried twice
88110441SSonam.Gupta@Sun.COM 				 * uri should be the same as
88210441SSonam.Gupta@Sun.COM 				 * already read in the previous call
88310441SSonam.Gupta@Sun.COM 				 * to 'report_job'.
88410441SSonam.Gupta@Sun.COM 				 * For the same printer 'job-printer-uri'
88510441SSonam.Gupta@Sun.COM 				 * is read just once because only in the
88610441SSonam.Gupta@Sun.COM 				 * first call it contains the host information
88710441SSonam.Gupta@Sun.COM 				 */
88810441SSonam.Gupta@Sun.COM 				uri = puri;
88910441SSonam.Gupta@Sun.COM 			}
89010441SSonam.Gupta@Sun.COM 		}
8919152SSonam.Gupta@Sun.COM 	}
8929152SSonam.Gupta@Sun.COM 
8939152SSonam.Gupta@Sun.COM 	if (host) {
8949152SSonam.Gupta@Sun.COM 		/* Check if it is local printer or remote printer */
8959152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
8969152SSonam.Gupta@Sun.COM 
8979152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
8989152SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
8999152SSonam.Gupta@Sun.COM 
9009152SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
9019152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
9029152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0)) {
9038321SSonam.Gupta@Sun.COM 
9049152SSonam.Gupta@Sun.COM 				if (strcasecmp(host, nodename) == 0) {
9059152SSonam.Gupta@Sun.COM 					/*
9069152SSonam.Gupta@Sun.COM 					 * Request submitted locally
9079152SSonam.Gupta@Sun.COM 					 * for the local queue.
9089152SSonam.Gupta@Sun.COM 					 * Hostname will not be displayed
9099152SSonam.Gupta@Sun.COM 					 */
9109152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s",
9119152SSonam.Gupta@Sun.COM 					    user);
9129152SSonam.Gupta@Sun.COM 				}
9139152SSonam.Gupta@Sun.COM 				else
9149152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s@%s",
9159152SSonam.Gupta@Sun.COM 					    user, host);
9169152SSonam.Gupta@Sun.COM 			} else if (uri != NULL) {
9179152SSonam.Gupta@Sun.COM 				/*
9189152SSonam.Gupta@Sun.COM 				 * It's a remote printer.
9199152SSonam.Gupta@Sun.COM 				 * In case of remote printers hostname is
9209152SSonam.Gupta@Sun.COM 				 * always displayed.
9219152SSonam.Gupta@Sun.COM 				 */
9229152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
9239152SSonam.Gupta@Sun.COM 				    user, host);
9249152SSonam.Gupta@Sun.COM 			}
9259152SSonam.Gupta@Sun.COM 			uri_free(u);
9269152SSonam.Gupta@Sun.COM 		} else {
9279152SSonam.Gupta@Sun.COM 			/*
9289152SSonam.Gupta@Sun.COM 			 * If attribute "job-printer-uri"
9299152SSonam.Gupta@Sun.COM 			 * cannot be read
9309152SSonam.Gupta@Sun.COM 			 * by default append the hostname
9319152SSonam.Gupta@Sun.COM 			 */
9329152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s@%s", user, host);
9339152SSonam.Gupta@Sun.COM 		}
9349152SSonam.Gupta@Sun.COM 	} else {
9359152SSonam.Gupta@Sun.COM 		/*
9369152SSonam.Gupta@Sun.COM 		 * When print server is s10u4 and ipp service is used
9379152SSonam.Gupta@Sun.COM 		 * "job-originating-hostname" attribute is not set
9389152SSonam.Gupta@Sun.COM 		 * So get the host information from the uri
9399152SSonam.Gupta@Sun.COM 		 */
9409152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
9419152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
9429152SSonam.Gupta@Sun.COM 			if ((u != NULL) && (u->host != NULL))
9439152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
9449152SSonam.Gupta@Sun.COM 				    user, u->host);
9459152SSonam.Gupta@Sun.COM 			else
9469152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s", user);
9479152SSonam.Gupta@Sun.COM 
9489152SSonam.Gupta@Sun.COM 			uri_free(u);
9499152SSonam.Gupta@Sun.COM 		} else
9509152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s", user);
9519152SSonam.Gupta@Sun.COM 	}
9522264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
9532264Sjacobs 	size *= 1024;	/* for the approximate byte size */
9542264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
9552264Sjacobs 
9562264Sjacobs 	(void) time(&clock);
9572264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
9588962SSonam.Gupta@Sun.COM 	    "time-at-creation", (int32_t *)&clock);
9592264Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
9602264Sjacobs 
9612264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
9628962SSonam.Gupta@Sun.COM 	    "job-printer-uri", &destination);
9632264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
9648962SSonam.Gupta@Sun.COM 	    "printer-name", &destination);
9652264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
9668962SSonam.Gupta@Sun.COM 	    "job-id", &id);
9679330SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetInteger(attrs, NULL,
9689330SSonam.Gupta@Sun.COM 	    "job-id-requested", &id);
9699256SSonam.Gupta@Sun.COM 
9709331SSonam.Gupta@Sun.COM 
9719256SSonam.Gupta@Sun.COM 	snprintf(request, sizeof (request), "%s-%d", printer, id);
9722264Sjacobs 
9732264Sjacobs 	if (show_rank != 0) {
9742264Sjacobs 		int32_t rank = -1;
9752264Sjacobs 
9762264Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
9778962SSonam.Gupta@Sun.COM 		    "number-of-intervening-jobs", &rank);
9782264Sjacobs 		rank++;
9792264Sjacobs 
9802264Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
9818321SSonam.Gupta@Sun.COM 		    rank, request, User, size, date);
9822264Sjacobs 	} else
9838321SSonam.Gupta@Sun.COM 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
9842264Sjacobs 
9852264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
9868962SSonam.Gupta@Sun.COM 	    "job-state", &jstate);
9879331SSonam.Gupta@Sun.COM 
9889257SGowtham.Thommandra@Sun.COM 	if (jstate == 0x0001)
9899257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being held"));
9909257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0800)
9919257SGowtham.Thommandra@Sun.COM 		printf(gettext(" notifying user"));
9929257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0040)
9939257SGowtham.Thommandra@Sun.COM 		printf(gettext(" cancelled"));
9949257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0010)
9959257SGowtham.Thommandra@Sun.COM 		printf(gettext(" finished printing"));
9969257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0008)
9979257SGowtham.Thommandra@Sun.COM 		printf(gettext(" on %s"), destination);
9989257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x2000)
9999257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held by admin"));
10009257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0002)
10019257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being filtered"));
10029257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0004)
10039257SGowtham.Thommandra@Sun.COM 		printf(gettext(" filtered"));
10049257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0020)
10059257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held for change"));
10062264Sjacobs 
10072264Sjacobs 	if (verbose == 1) {
10083125Sjacobs 		char *form = NULL;
10093125Sjacobs 
10102264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
10118962SSonam.Gupta@Sun.COM 		    "output-device-assigned", &destination);
10122264Sjacobs 		printf("\n\t assigned %s", destination);
10133127Sjacobs 
10143125Sjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
10153125Sjacobs 		if (form != NULL)
10163125Sjacobs 			printf(", form %s", form);
10172264Sjacobs 	} else if (verbose > 1) {
10182264Sjacobs 		printf("\n");
10192264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
10202264Sjacobs 	}
10212264Sjacobs 
10222264Sjacobs 	printf("\n");
10232264Sjacobs 
10242264Sjacobs 	return (0);
10252264Sjacobs }
10262264Sjacobs 
10272264Sjacobs static int
job_query(char * request,int (* report)(char *,papi_job_t,int,int),papi_encryption_t encryption,int show_rank,int verbose)10289256SSonam.Gupta@Sun.COM job_query(char *request, int (*report)(char *, papi_job_t, int, int),
10292264Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
10302264Sjacobs {
10312264Sjacobs 	int result = 0;
10322264Sjacobs 	papi_status_t status;
10332264Sjacobs 	papi_service_t svc = NULL;
10348295SSonam.Gupta@Sun.COM 	char *printer = request;
10352264Sjacobs 	int32_t id = -1;
10368295SSonam.Gupta@Sun.COM 	int flag1 = 0;
10378295SSonam.Gupta@Sun.COM 	int flag = 1;
10388295SSonam.Gupta@Sun.COM 	int print_flag = 0;
10392264Sjacobs 
10408295SSonam.Gupta@Sun.COM 	do {
10418295SSonam.Gupta@Sun.COM 		status = papiServiceCreate(&svc, printer, NULL, NULL,
10428962SSonam.Gupta@Sun.COM 		    cli_auth_callback, encryption, NULL);
10432264Sjacobs 
10448295SSonam.Gupta@Sun.COM 		if ((status == PAPI_OK) && (printer != NULL))
10458295SSonam.Gupta@Sun.COM 			print_flag = 1;
10462264Sjacobs 
10478295SSonam.Gupta@Sun.COM 		/* <name>-# printer name does not exist */
10488295SSonam.Gupta@Sun.COM 		if (status != PAPI_OK) {
10498295SSonam.Gupta@Sun.COM 			/*
10508295SSonam.Gupta@Sun.COM 			 * Check if <name>-# is a request-id
10518295SSonam.Gupta@Sun.COM 			 * Once this check is done flag1 is set
10528295SSonam.Gupta@Sun.COM 			 */
10538295SSonam.Gupta@Sun.COM 			if (flag1 == 1)
10548295SSonam.Gupta@Sun.COM 				break;
10558295SSonam.Gupta@Sun.COM 
10568295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
10572264Sjacobs 
10588295SSonam.Gupta@Sun.COM 			status = papiServiceCreate(&svc, printer, NULL, NULL,
10598962SSonam.Gupta@Sun.COM 			    cli_auth_callback, encryption, NULL);
10602264Sjacobs 
10618295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
10628295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
10638295SSonam.Gupta@Sun.COM 				    "Failed to contact service for %s: %s\n"),
10648295SSonam.Gupta@Sun.COM 				    (printer ? printer : "all"),
10658295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
10668295SSonam.Gupta@Sun.COM 				return (-1);
10678295SSonam.Gupta@Sun.COM 			}
10682264Sjacobs 		}
10692264Sjacobs 
10708295SSonam.Gupta@Sun.COM 		if (printer == NULL) { /* all */
10718295SSonam.Gupta@Sun.COM 			char **interest = interest_list(svc);
10728295SSonam.Gupta@Sun.COM 
10738295SSonam.Gupta@Sun.COM 			if (interest != NULL) {
10748295SSonam.Gupta@Sun.COM 				int i;
10758295SSonam.Gupta@Sun.COM 
10768295SSonam.Gupta@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
10778295SSonam.Gupta@Sun.COM 					result += job_query(interest[i], report,
10788962SSonam.Gupta@Sun.COM 					    encryption, show_rank, verbose);
10798295SSonam.Gupta@Sun.COM 			}
10808295SSonam.Gupta@Sun.COM 		} else if (id == -1) { /* a printer */
10818295SSonam.Gupta@Sun.COM 			papi_job_t *jobs = NULL;
10828295SSonam.Gupta@Sun.COM 
10838295SSonam.Gupta@Sun.COM 			status = papiPrinterListJobs(svc, printer, NULL,
10848962SSonam.Gupta@Sun.COM 			    0, 0, &jobs);
10858295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
10868295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
10878295SSonam.Gupta@Sun.COM 				    "Failed to get job list: %s\n"),
10888295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
10898295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
10908295SSonam.Gupta@Sun.COM 				return (-1);
10918295SSonam.Gupta@Sun.COM 			}
10928295SSonam.Gupta@Sun.COM 
10938295SSonam.Gupta@Sun.COM 			if (jobs != NULL) {
10948295SSonam.Gupta@Sun.COM 				int i;
10952264Sjacobs 
10968295SSonam.Gupta@Sun.COM 				for (i = 0; jobs[i] != NULL; i++)
10979256SSonam.Gupta@Sun.COM 					result += report(printer,
10989256SSonam.Gupta@Sun.COM 					    jobs[i], show_rank,
10999256SSonam.Gupta@Sun.COM 					    verbose);
11008295SSonam.Gupta@Sun.COM 			}
11018295SSonam.Gupta@Sun.COM 
11028295SSonam.Gupta@Sun.COM 			papiJobListFree(jobs);
11038295SSonam.Gupta@Sun.COM 		} else {	/* a job */
11048295SSonam.Gupta@Sun.COM 			papi_job_t job = NULL;
11058295SSonam.Gupta@Sun.COM 
11068295SSonam.Gupta@Sun.COM 			/* Once a job has been found stop processing */
11078295SSonam.Gupta@Sun.COM 			flag = 0;
11088295SSonam.Gupta@Sun.COM 
11099331SSonam.Gupta@Sun.COM 			/*
11109331SSonam.Gupta@Sun.COM 			 * Job-id could be the job-id requested
11119331SSonam.Gupta@Sun.COM 			 * Check if it is job-id or job-id-requested
11129331SSonam.Gupta@Sun.COM 			 */
11139331SSonam.Gupta@Sun.COM 			id = job_to_be_queried(svc, printer, id);
11149331SSonam.Gupta@Sun.COM 
111511397SSonam.Gupta@Sun.COM 			if (id >= 0)
11169669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, id,
11179669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
11189331SSonam.Gupta@Sun.COM 			else
111911397SSonam.Gupta@Sun.COM 				/* id not found */
112011397SSonam.Gupta@Sun.COM 				status = PAPI_NOT_FOUND;
11219331SSonam.Gupta@Sun.COM 
11228295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
11238295SSonam.Gupta@Sun.COM 				if (!print_flag)
11248295SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
11258962SSonam.Gupta@Sun.COM 					    "Failed to get job"\
11268962SSonam.Gupta@Sun.COM 					    " info for %s: %s\n"),
11278962SSonam.Gupta@Sun.COM 					    request,
11288962SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
11298295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
11308295SSonam.Gupta@Sun.COM 				return (-1);
11318295SSonam.Gupta@Sun.COM 			}
11328295SSonam.Gupta@Sun.COM 
11338295SSonam.Gupta@Sun.COM 			if (job != NULL)
11349256SSonam.Gupta@Sun.COM 				result = report(printer, job,
11359256SSonam.Gupta@Sun.COM 				    show_rank, verbose);
11368295SSonam.Gupta@Sun.COM 
11378295SSonam.Gupta@Sun.COM 			papiJobFree(job);
11382264Sjacobs 		}
11392264Sjacobs 
11408295SSonam.Gupta@Sun.COM 		if (flag) {
11418295SSonam.Gupta@Sun.COM 			id = -1;
11428295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
11438295SSonam.Gupta@Sun.COM 			if (id == -1)
11448295SSonam.Gupta@Sun.COM 				flag = 0;
11458295SSonam.Gupta@Sun.COM 			else
11468295SSonam.Gupta@Sun.COM 				flag1 = 1;
11472264Sjacobs 		}
11488295SSonam.Gupta@Sun.COM 	} while (flag);
11492264Sjacobs 
11502264Sjacobs 	papiServiceDestroy(svc);
11512264Sjacobs 
11522264Sjacobs 	return (result);
11532264Sjacobs }
11542264Sjacobs 
11552264Sjacobs static int
report_form(char * name,papi_attribute_t ** attrs,int verbose)11562264Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
11572264Sjacobs {
11582264Sjacobs 	papi_status_t status;
11592264Sjacobs 	char *form = NULL;
11602264Sjacobs 	void *iter = NULL;
11612264Sjacobs 
11622264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
11638962SSonam.Gupta@Sun.COM 	    "form-supported", &form);
11648962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
11658962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter,
11668962SSonam.Gupta@Sun.COM 	    NULL, &form)) {
11672264Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
11682264Sjacobs 			printf(gettext("form %s is available to you\n"), form);
11692264Sjacobs 			if (verbose != 0) {
11702264Sjacobs 				char *detail = NULL;
11712264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
11728962SSonam.Gupta@Sun.COM 				    "form-supported-detail", &detail);
11732264Sjacobs 				if (status == PAPI_OK)
11742264Sjacobs 					printf("%s\n", detail);
11752264Sjacobs 			}
11762264Sjacobs 		}
11772264Sjacobs 	}
11782264Sjacobs 
11792264Sjacobs 	return (0);
11802264Sjacobs }
11812264Sjacobs 
11822264Sjacobs static int
report_print_wheels(char * name,papi_attribute_t ** attrs,int verbose)11832264Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
11842264Sjacobs {
11852264Sjacobs 	papi_status_t status;
11862264Sjacobs 	char *pw = NULL;
11872264Sjacobs 	void *iter = NULL;
11882264Sjacobs 
11892264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
11908962SSonam.Gupta@Sun.COM 	    "pw-supported", &pw);
11918962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
11928962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
11932264Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
11942264Sjacobs 			printf(gettext("charset %s is available\n"), pw);
11952264Sjacobs 			if (verbose != 0) {
11962264Sjacobs 				char *info = NULL;
11972264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
11988962SSonam.Gupta@Sun.COM 				    "pw-supported-extra", &info);
11992264Sjacobs 				if (status == PAPI_OK)
12002264Sjacobs 					printf("%s\n", info);
12012264Sjacobs 			}
12022264Sjacobs 		}
12032264Sjacobs 	}
12042264Sjacobs 
12052264Sjacobs 	return (0);
12062264Sjacobs }
12072264Sjacobs 
12082264Sjacobs static int
service_query(char * name,int (* report)(char *,papi_attribute_t **,int),papi_encryption_t encryption,int verbose)12092264Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
12102264Sjacobs 		papi_encryption_t encryption, int verbose)
12112264Sjacobs {
12122264Sjacobs 	int result = 0;
12132264Sjacobs 	papi_status_t status;
12142264Sjacobs 	papi_service_t svc = NULL;
12152264Sjacobs 	papi_attribute_t **attrs = NULL;
12162264Sjacobs 
12172264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
12188962SSonam.Gupta@Sun.COM 	    encryption, NULL);
12192264Sjacobs 	if (status != PAPI_OK) {
12202264Sjacobs 		papiServiceDestroy(svc);
12212264Sjacobs 		return (-1);
12222264Sjacobs 	}
12232264Sjacobs 
12242264Sjacobs 	attrs = papiServiceGetAttributeList(svc);
12252264Sjacobs 	if (attrs != NULL) {
12262264Sjacobs 		result = report(name, attrs, verbose);
12272264Sjacobs 
12282264Sjacobs 		if (verbose > 1) {
12292264Sjacobs 			printf("\n");
12302264Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
12312264Sjacobs 			printf("\n");
12322264Sjacobs 		}
12332264Sjacobs 	}
12342264Sjacobs 
12352264Sjacobs 	papiServiceDestroy(svc);
12362264Sjacobs 
12372264Sjacobs 	return (result);
12382264Sjacobs }
12392264Sjacobs 
12402264Sjacobs int
main(int ac,char * av[])12412264Sjacobs main(int ac, char *av[])
12422264Sjacobs {
12432264Sjacobs 	int exit_code = 0;
12442264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
12452264Sjacobs 	int rank = 0;
12462264Sjacobs 	int verbose = 0;
12472264Sjacobs 	int description = 0;
12482264Sjacobs 	int c;
12492264Sjacobs 	char **argv;
12502264Sjacobs 
12512264Sjacobs 	(void) setlocale(LC_ALL, "");
12522264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
12532264Sjacobs 
12542264Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
12554286Swendyp 	for (c = 0; c < ac; c++) {
12564286Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
12578962SSonam.Gupta@Sun.COM 		    (isalpha(av[c][2]) != 0)) {
12584286Swendyp 			/* preserve old "-l[po...]" behavior */
12594286Swendyp 			argv[c] = &av[c][1];
12604286Swendyp 			argv[c][0] = '-';
12614286Swendyp 			verbose = 1;
12624286Swendyp 
12634286Swendyp 		} else
12644286Swendyp 			argv[c] = av[c];
12654286Swendyp 	}
12664286Swendyp 
12672264Sjacobs 	argv[c++] = "--";
12682264Sjacobs 	ac = c;
12692264Sjacobs 
12702264Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
12719740SKeerthi.Kondaka@Sun.COM 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
12729740SKeerthi.Kondaka@Sun.COM 		switch (c) {    /* these may or may not have an option */
12739740SKeerthi.Kondaka@Sun.COM 		case 'a':
12749740SKeerthi.Kondaka@Sun.COM 		case 'c':
12759740SKeerthi.Kondaka@Sun.COM 		case 'p':
12769740SKeerthi.Kondaka@Sun.COM 		case 'o':
12779740SKeerthi.Kondaka@Sun.COM 		case 'R':
12789740SKeerthi.Kondaka@Sun.COM 		case 'u':
12799740SKeerthi.Kondaka@Sun.COM 		case 'v':
12809740SKeerthi.Kondaka@Sun.COM 		case 'l':
12819740SKeerthi.Kondaka@Sun.COM 		case 'f':
12829740SKeerthi.Kondaka@Sun.COM 		case 'S':
12839740SKeerthi.Kondaka@Sun.COM 			if (optarg[0] == '-') {
12849740SKeerthi.Kondaka@Sun.COM 				/* this check stop a possible infinite loop */
12859740SKeerthi.Kondaka@Sun.COM 				if ((optind > 1) && (argv[optind-1][1] != c))
12869740SKeerthi.Kondaka@Sun.COM 					optind--;
12879740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
12889740SKeerthi.Kondaka@Sun.COM 			} else if (strcmp(optarg, "all") == 0)
12899740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
12909740SKeerthi.Kondaka@Sun.COM 		}
12919740SKeerthi.Kondaka@Sun.COM 
12922264Sjacobs 		switch (c) {
12932264Sjacobs 		case 'l':
12942264Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
12952264Sjacobs 				optarg = "1";
12962264Sjacobs 			verbose = atoi(optarg);
12972264Sjacobs 			break;
12982264Sjacobs 		case 'D':
12992264Sjacobs 			description = 1;
13002264Sjacobs 			break;
13012264Sjacobs 		case 'R':
13022264Sjacobs 			rank = 1;
13032264Sjacobs 			break;
13042264Sjacobs 		case 'E':
13052264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
13062264Sjacobs 			break;
13072264Sjacobs 		default:
13082264Sjacobs 			break;
13092264Sjacobs 		}
13109740SKeerthi.Kondaka@Sun.COM 	}
13112264Sjacobs 	optind = 1;
13122264Sjacobs 
13132264Sjacobs 	/* process command line arguments */
13142264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
13152264Sjacobs 		switch (c) {	/* these may or may not have an option */
13162264Sjacobs 		case 'a':
13172264Sjacobs 		case 'c':
13182264Sjacobs 		case 'p':
13192264Sjacobs 		case 'o':
13202264Sjacobs 		case 'R':
13212264Sjacobs 		case 'u':
13222264Sjacobs 		case 'v':
13232264Sjacobs 		case 'l':
13242264Sjacobs 		case 'f':
13252264Sjacobs 		case 'S':
13262264Sjacobs 			if (optarg[0] == '-') {
13272264Sjacobs 				/* this check stop a possible infinite loop */
13282264Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
13292264Sjacobs 					optind--;
13302264Sjacobs 				optarg = NULL;
13312264Sjacobs 			} else if (strcmp(optarg, "all") == 0)
13322264Sjacobs 				optarg = NULL;
13332264Sjacobs 		}
13342264Sjacobs 
13352264Sjacobs 		switch (c) {
13362264Sjacobs 		case 'a':
13372264Sjacobs 			exit_code += printer_query(optarg, report_accepting,
13388962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13392264Sjacobs 			break;
13402264Sjacobs 		case 'c':
13412264Sjacobs 			exit_code += printer_query(optarg, report_class,
13428962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13432264Sjacobs 			break;
13442264Sjacobs 		case 'p':
13452264Sjacobs 			exit_code += printer_query(optarg, report_printer,
13468962SSonam.Gupta@Sun.COM 			    encryption, verbose, description);
13472264Sjacobs 			break;
13482264Sjacobs 		case 'd':
13492264Sjacobs 			exit_code += lpstat_default_printer(encryption);
13502264Sjacobs 			break;
13512264Sjacobs 		case 'r':
13522264Sjacobs 			exit_code += lpstat_service_status(encryption);
13532264Sjacobs 			break;
13542264Sjacobs 		case 'u':
13552264Sjacobs 			if (optarg != NULL)
13562264Sjacobs 				users = strsplit(optarg, ", \n");
13572264Sjacobs 			exit_code += job_query(NULL, report_job,
13588962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
13592264Sjacobs 			if (users != NULL) {
13602264Sjacobs 				free(users);
13612264Sjacobs 				users = NULL;
13622264Sjacobs 			}
13632264Sjacobs 			break;
13642264Sjacobs 		case 'v':
13652264Sjacobs 			exit_code += printer_query(optarg, report_device,
13668962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13672264Sjacobs 			break;
13688604SGowtham.Thommandra@Sun.COM 		case 'R':	/* set "rank" flag in first pass */
13692264Sjacobs 		case 'o':
13702264Sjacobs 			exit_code += job_query(optarg, report_job,
13718962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
13722264Sjacobs 			break;
13732264Sjacobs 		case 'f':
13742264Sjacobs 			exit_code += service_query(optarg, report_form,
13758962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13762264Sjacobs 			break;
13772264Sjacobs 		case 'S':
13782264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
13798962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13802264Sjacobs 			break;
13812264Sjacobs 		case 's':
13822264Sjacobs 			exit_code += lpstat_service_status(encryption);
13832264Sjacobs 			exit_code += lpstat_default_printer(encryption);
13842264Sjacobs 			exit_code += printer_query(NULL, report_class,
13858962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13862264Sjacobs 			exit_code += printer_query(NULL, report_device,
13878962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13882264Sjacobs 			exit_code += service_query(optarg, report_form,
13898962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13902264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
13918962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13922264Sjacobs 			break;
13932264Sjacobs 		case 't':
13942264Sjacobs 			exit_code += lpstat_service_status(encryption);
13952264Sjacobs 			exit_code += lpstat_default_printer(encryption);
13962264Sjacobs 			exit_code += printer_query(NULL, report_class,
13978962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13982264Sjacobs 			exit_code += printer_query(NULL, report_device,
13998962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
14002264Sjacobs 			exit_code += printer_query(NULL, report_accepting,
14018962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
14022264Sjacobs 			exit_code += printer_query(NULL, report_printer,
14038962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
14042264Sjacobs 			exit_code += service_query(optarg, report_form,
14058962SSonam.Gupta@Sun.COM 			    encryption, verbose);
14062264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
14078962SSonam.Gupta@Sun.COM 			    encryption, verbose);
14082264Sjacobs 			exit_code += job_query(NULL, report_job,
14098962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
14102264Sjacobs 			break;
14112264Sjacobs 		case 'L':	/* local-only, ignored */
14122264Sjacobs 		case 'l':	/* increased verbose level in first pass */
14132264Sjacobs 		case 'D':	/* set "description" flag in first pass */
14142264Sjacobs 		case 'E':	/* set encryption in the first pass */
14152264Sjacobs 			break;
14162264Sjacobs 		default:
14172264Sjacobs 			usage(av[0]);
14182264Sjacobs 		}
14192264Sjacobs 	}
14202264Sjacobs 	ac--;
14212264Sjacobs 
14222264Sjacobs 	if (ac == 1) {	/* report on my jobs */
14232264Sjacobs 		struct passwd *pw = getpwuid(getuid());
14242264Sjacobs 
14252264Sjacobs 		if (pw != NULL)
14262264Sjacobs 			users = strsplit(pw->pw_name, "");
14272264Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
14288962SSonam.Gupta@Sun.COM 		    rank, verbose);
14292264Sjacobs 		if (users != NULL) {
14302264Sjacobs 			free(users);
14312264Sjacobs 			users = NULL;
14322264Sjacobs 		}
14332264Sjacobs 	} else {
14342264Sjacobs 		for (c = optind; c < ac; c++)
14352264Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
14368962SSonam.Gupta@Sun.COM 			    rank, verbose);
14372264Sjacobs 	}
14382264Sjacobs 
14392264Sjacobs 
14402264Sjacobs 	if (exit_code != 0)
14412264Sjacobs 		exit_code = 1;
14422264Sjacobs 
14432264Sjacobs 	return (exit_code);
14442264Sjacobs }
1445