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 	int result = 0;
1172264Sjacobs 	papi_status_t status;
1182264Sjacobs 	papi_service_t svc = NULL;
1192264Sjacobs 	char *name = NULL;
1202264Sjacobs 
1212264Sjacobs 	if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
1222264Sjacobs 	    ((name = getenv("IPP_SERVER")) == NULL) &&
1232264Sjacobs 	    ((name = getenv("CUPS_SERVER")) == NULL))
1242264Sjacobs 		name = DEFAULT_SERVICE_URI;
1252264Sjacobs 
1262264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1278962SSonam.Gupta@Sun.COM 	    encryption, NULL);
1282264Sjacobs 	if (status != PAPI_OK) {
1292264Sjacobs 		printf(gettext("scheduler is not running\n"));
1302264Sjacobs 		result = -1;
1312264Sjacobs 	} else
1322264Sjacobs 		printf(gettext("scheduler is running\n"));
1332264Sjacobs 	papiServiceDestroy(svc);
1342264Sjacobs 
1352264Sjacobs 	return (result);
1362264Sjacobs }
1372264Sjacobs 
1382264Sjacobs static char *
1392264Sjacobs get_device_uri(papi_service_t svc, char *name)
1402264Sjacobs {
1412264Sjacobs 	papi_status_t status;
1422264Sjacobs 	papi_printer_t p = NULL;
1432264Sjacobs 	char *keys[] = { "device-uri", NULL };
1442264Sjacobs 	char *result = NULL;
1452264Sjacobs 
1462264Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
1472264Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
1482264Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
1492264Sjacobs 
1502264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
1518962SSonam.Gupta@Sun.COM 		    "device-uri", &result);
1522264Sjacobs 		if (result != NULL)
1532264Sjacobs 			result = strdup(result);
1542264Sjacobs 
1552264Sjacobs 		papiPrinterFree(p);
1562264Sjacobs 	}
1572264Sjacobs 
1582264Sjacobs 	return (result);
1592264Sjacobs }
1602264Sjacobs 
1618534SSonam.Gupta@Sun.COM static void
1628534SSonam.Gupta@Sun.COM print_description(papi_attribute_t **list, char *printer_name)
1638534SSonam.Gupta@Sun.COM {
1648534SSonam.Gupta@Sun.COM 	char *str = "";
1658534SSonam.Gupta@Sun.COM 
1668534SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(list, NULL,
1678534SSonam.Gupta@Sun.COM 	    "printer-info", &str);
1688534SSonam.Gupta@Sun.COM 
1698534SSonam.Gupta@Sun.COM 	/*
1708534SSonam.Gupta@Sun.COM 	 * If no printer-info is read then
1718534SSonam.Gupta@Sun.COM 	 * by default the printer-info is <printer-name>@<server>
1728534SSonam.Gupta@Sun.COM 	 */
1738534SSonam.Gupta@Sun.COM 	if (str[0] == '\0') {
1748534SSonam.Gupta@Sun.COM 		char *uri = NULL;
1758534SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
1768534SSonam.Gupta@Sun.COM 
1778534SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(list, NULL,
1788534SSonam.Gupta@Sun.COM 		    "printer-uri-supported", &uri);
1798534SSonam.Gupta@Sun.COM 
1808534SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
1818534SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
1828534SSonam.Gupta@Sun.COM 
1838534SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
1848534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
1858534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0))
1868534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription:\n"));
1878534SSonam.Gupta@Sun.COM 			else
1888534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription: %s@%s\n"),
1898534SSonam.Gupta@Sun.COM 				    printer_name, u->host);
1908534SSonam.Gupta@Sun.COM 
1918534SSonam.Gupta@Sun.COM 			uri_free(u);
1928534SSonam.Gupta@Sun.COM 		} else
1938534SSonam.Gupta@Sun.COM 			printf(gettext("\tDescription:\n"));
1948534SSonam.Gupta@Sun.COM 	} else
1958534SSonam.Gupta@Sun.COM 		printf(gettext("\tDescription: %s\n"), str);
1968534SSonam.Gupta@Sun.COM }
1978534SSonam.Gupta@Sun.COM 
1982264Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
1992264Sjacobs 					NULL };
2002264Sjacobs /* ARGSUSED2 */
2012264Sjacobs static int
2022264Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
2032264Sjacobs 		int verbose, int description)
2042264Sjacobs {
2052264Sjacobs 	papi_status_t status;
2062264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2072264Sjacobs 	char *uri = NULL;
2082264Sjacobs 	char *device = NULL;
2092264Sjacobs 	uri_t *u = NULL;
2102264Sjacobs 
2112264Sjacobs 	if (name == NULL) {
2122264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2138962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
2142264Sjacobs 		if (status != PAPI_OK)
2152264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2168962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
2172264Sjacobs 	}
2182264Sjacobs 
2192264Sjacobs 	if (name == NULL)
2202264Sjacobs 		return (-1);
2212264Sjacobs 
2222264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
2238962SSonam.Gupta@Sun.COM 	    "printer-uri-supported", &uri);
2242264Sjacobs 
2252264Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
2262264Sjacobs 		char *nodename = localhostname();
2272264Sjacobs 
2282264Sjacobs 		if ((u->host == NULL) ||
2292264Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
2302264Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
2312264Sjacobs 			device = get_device_uri(svc, name);
2322264Sjacobs 
2332264Sjacobs 		if (device != NULL) {
2342264Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
2352264Sjacobs 			return (0);
2362264Sjacobs 		} else if (uri != NULL) {
2372264Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
2389411SKeerthi.Kondaka@Sun.COM 			    u->host?u->host:"localhost", uri);
2392264Sjacobs 			return (0);
2402264Sjacobs 		}
2412264Sjacobs 
2422264Sjacobs 		uri_free(u);
2432264Sjacobs 	}
2442264Sjacobs 
2452264Sjacobs 	return (0);
2462264Sjacobs }
2472264Sjacobs 
2482264Sjacobs static char *report_accepting_keys[] = { "printer-name",
2492264Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
2502264Sjacobs 			"printer-up-time", "printer-state-time",
2512264Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
2522264Sjacobs /* ARGSUSED2 */
2532264Sjacobs static int
2542264Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
2552264Sjacobs 		int verbose, int description)
2562264Sjacobs {
2572264Sjacobs 	papi_status_t status;
2582264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2592264Sjacobs 	time_t curr;
2602264Sjacobs 	char boolean = PAPI_FALSE;
2612264Sjacobs 
2622264Sjacobs 	if (name == NULL) {
2632264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2648962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
2652264Sjacobs 		if (status != PAPI_OK)
2662264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2678962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
2682264Sjacobs 	}
2692264Sjacobs 	if (name == NULL)
2702264Sjacobs 		return (-1);
2712264Sjacobs 
2722264Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
2738962SSonam.Gupta@Sun.COM 	    "printer-is-accepting-jobs", &boolean);
2742264Sjacobs 	(void) time(&curr);
2752264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2768962SSonam.Gupta@Sun.COM 	    "printer-up-time", &curr);
2772264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2788962SSonam.Gupta@Sun.COM 	    "printer-state-time", &curr);
2792264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2808962SSonam.Gupta@Sun.COM 	    "lpsched-reject-date", &curr);
2812264Sjacobs 
2822264Sjacobs 	if (boolean == PAPI_TRUE) {
2832264Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
2848962SSonam.Gupta@Sun.COM 		    name, nctime(&curr));
2852264Sjacobs 	} else {
2862264Sjacobs 		char *reason = "unknown reason";
2872264Sjacobs 
2882264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
2898962SSonam.Gupta@Sun.COM 		    "lpsched-reject-reason", &reason);
2902264Sjacobs 
2912264Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
2928962SSonam.Gupta@Sun.COM 		    name, nctime(&curr), reason);
2932264Sjacobs 	}
2942264Sjacobs 
2952264Sjacobs 	return (0);
2962264Sjacobs }
2972264Sjacobs 
2982264Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
2992264Sjacobs 					"member-names", NULL };
3002264Sjacobs /* ARGSUSED2 */
3012264Sjacobs static int
3022264Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
3032264Sjacobs 		int verbose, int description)
3042264Sjacobs {
3052264Sjacobs 	papi_status_t status;
3062264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3072264Sjacobs 	char *member = NULL;
3082264Sjacobs 	void *iter = NULL;
3092264Sjacobs 
3102264Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
3118962SSonam.Gupta@Sun.COM 	    "member-names", &member);
3122264Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
3132264Sjacobs 		return (0);
3142264Sjacobs 
3152264Sjacobs 	if (name == NULL) {
3162264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3178962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
3182264Sjacobs 		if (status != PAPI_OK)
3192264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3208962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
3212264Sjacobs 	}
3222264Sjacobs 	if (name == NULL)
3232264Sjacobs 		return (-1);
3242264Sjacobs 
3252264Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
3262264Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
3278962SSonam.Gupta@Sun.COM 	    == PAPI_OK)
3282264Sjacobs 		printf("\t%s\n", member);
3292264Sjacobs 
3302264Sjacobs 	return (0);
3312264Sjacobs }
3322264Sjacobs 
333*9810SKeerthi.Kondaka@Sun.COM static int
334*9810SKeerthi.Kondaka@Sun.COM get_remote_hostname(papi_attribute_t **attrs, char **host)
335*9810SKeerthi.Kondaka@Sun.COM {
336*9810SKeerthi.Kondaka@Sun.COM 	char *uri = NULL;
337*9810SKeerthi.Kondaka@Sun.COM 	uri_t *u;
338*9810SKeerthi.Kondaka@Sun.COM 	char *nodename;
339*9810SKeerthi.Kondaka@Sun.COM 
340*9810SKeerthi.Kondaka@Sun.COM 	*host = NULL;
341*9810SKeerthi.Kondaka@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
342*9810SKeerthi.Kondaka@Sun.COM 	    "job-originating-host-name", host);
343*9810SKeerthi.Kondaka@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
344*9810SKeerthi.Kondaka@Sun.COM 	    "printer-uri-supported", &uri);
345*9810SKeerthi.Kondaka@Sun.COM 	if (*host == NULL) {
346*9810SKeerthi.Kondaka@Sun.COM 		if (uri != NULL) {
347*9810SKeerthi.Kondaka@Sun.COM 			if (uri_from_string(uri, &u) == 0) {
348*9810SKeerthi.Kondaka@Sun.COM 				if (u->host == NULL) {
349*9810SKeerthi.Kondaka@Sun.COM 					uri_free(u);
350*9810SKeerthi.Kondaka@Sun.COM 					return (0);
351*9810SKeerthi.Kondaka@Sun.COM 				}
352*9810SKeerthi.Kondaka@Sun.COM 				*host = strdup(u->host);
353*9810SKeerthi.Kondaka@Sun.COM 				uri_free(u);
354*9810SKeerthi.Kondaka@Sun.COM 			} else {
355*9810SKeerthi.Kondaka@Sun.COM 				return (0);
356*9810SKeerthi.Kondaka@Sun.COM 			}
357*9810SKeerthi.Kondaka@Sun.COM 		} else {
358*9810SKeerthi.Kondaka@Sun.COM 			return (0);
359*9810SKeerthi.Kondaka@Sun.COM 		}
360*9810SKeerthi.Kondaka@Sun.COM 	}
361*9810SKeerthi.Kondaka@Sun.COM 	nodename = localhostname();
362*9810SKeerthi.Kondaka@Sun.COM 	if ((strcasecmp(*host, "localhost") == 0) ||
363*9810SKeerthi.Kondaka@Sun.COM 	    (strcasecmp(*host, nodename) == 0)) {
364*9810SKeerthi.Kondaka@Sun.COM 		return (0);
365*9810SKeerthi.Kondaka@Sun.COM 	}
366*9810SKeerthi.Kondaka@Sun.COM 	return (1);
367*9810SKeerthi.Kondaka@Sun.COM }
368*9810SKeerthi.Kondaka@Sun.COM 
3692264Sjacobs static char *report_printer_keys[] = { "printer-name",
3702264Sjacobs 			"printer-uri-supported", "printer-state",
3712264Sjacobs 			"printer-up-time", "printer-state-time",
3722264Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
3732264Sjacobs 			"lpsched-disable-reason", NULL };
3742264Sjacobs /* ARGSUSED2 */
3752264Sjacobs static int
3762264Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
3772264Sjacobs 		int verbose, int description)
3782264Sjacobs {
3792264Sjacobs 	papi_status_t status;
3802264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3812264Sjacobs 	time_t curr;
3822264Sjacobs 	int32_t pstat = 0;
3832264Sjacobs 	char *member = NULL;
3848962SSonam.Gupta@Sun.COM 	papi_job_t *j = NULL;
3852264Sjacobs 
3862264Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
3878962SSonam.Gupta@Sun.COM 	    "member-names", &member);
3882264Sjacobs 	if (status == PAPI_OK)	/* it's a class */
3892264Sjacobs 		return (0);
3902264Sjacobs 
3912264Sjacobs 	if (name == NULL) {
3922264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3938962SSonam.Gupta@Sun.COM 		    "printer-name", &name);
3942264Sjacobs 		if (status != PAPI_OK)
3952264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3968962SSonam.Gupta@Sun.COM 			    "printer-uri-supported", &name);
3972264Sjacobs 	}
3982264Sjacobs 	if (name == NULL)
3992264Sjacobs 		return (-1);
4002264Sjacobs 
4012264Sjacobs 	printf(gettext("printer %s "), name);
4022264Sjacobs 
4032264Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
4048962SSonam.Gupta@Sun.COM 	    "printer-state", &pstat);
4052264Sjacobs 
4062264Sjacobs 	switch (pstat) {
4072264Sjacobs 	case 0x03:	/* idle */
4082264Sjacobs 		printf(gettext("idle. enabled"));
4092264Sjacobs 		break;
4108962SSonam.Gupta@Sun.COM 	case 0x04: /* processing */
4118962SSonam.Gupta@Sun.COM 		status = papiPrinterListJobs(svc, name, NULL,
4128962SSonam.Gupta@Sun.COM 		    0, 0, &j);
4138962SSonam.Gupta@Sun.COM 
4148962SSonam.Gupta@Sun.COM 		if (status == PAPI_OK) {
4158962SSonam.Gupta@Sun.COM 			if (j != NULL) {
4168962SSonam.Gupta@Sun.COM 				int i = 0;
4178962SSonam.Gupta@Sun.COM 				int32_t jobid = 0;
4188962SSonam.Gupta@Sun.COM 				int32_t jstate = 0;
4198962SSonam.Gupta@Sun.COM 				int flag = 0;
4208962SSonam.Gupta@Sun.COM 
4218962SSonam.Gupta@Sun.COM 				for (i = 0; j[i] != NULL; ++i) {
4229206SSonam.Gupta@Sun.COM 					papi_attribute_t **attr =
4239206SSonam.Gupta@Sun.COM 					    papiJobGetAttributeList(j[i]);
4248962SSonam.Gupta@Sun.COM 
4259206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
4268962SSonam.Gupta@Sun.COM 					    NULL, "job-state", &jstate);
4279206SSonam.Gupta@Sun.COM 					papiAttributeListGetInteger(attr,
4288962SSonam.Gupta@Sun.COM 					    NULL, "job-id", &jobid);
4292264Sjacobs 
4308962SSonam.Gupta@Sun.COM 					/*
4319257SGowtham.Thommandra@Sun.COM 					 * If the job-state is in
4329257SGowtham.Thommandra@Sun.COM 					 * RS_PRINTING then only print.
4338962SSonam.Gupta@Sun.COM 					 */
4349257SGowtham.Thommandra@Sun.COM 					if (jstate == 0x0008) {
4358962SSonam.Gupta@Sun.COM 						if (flag == 0)
4368962SSonam.Gupta@Sun.COM 							printf(gettext
4378962SSonam.Gupta@Sun.COM 							    ("now printing"\
4388962SSonam.Gupta@Sun.COM 							    " %s-%d. enabled"),
4398962SSonam.Gupta@Sun.COM 							    name, jobid);
4408962SSonam.Gupta@Sun.COM 						flag = 1;
4418962SSonam.Gupta@Sun.COM 					}
4428962SSonam.Gupta@Sun.COM 				}
4438962SSonam.Gupta@Sun.COM 				papiJobListFree(j);
4448962SSonam.Gupta@Sun.COM 			}
4452264Sjacobs 		}
4462264Sjacobs 		break;
4472264Sjacobs 	case 0x05:	/* stopped */
4482264Sjacobs 		printf(gettext("disabled"));
4492264Sjacobs 		break;
4502264Sjacobs 	default:
4512264Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
4522264Sjacobs 		break;
4532264Sjacobs 	}
4542264Sjacobs 
4552264Sjacobs 	(void) time(&curr);
4562264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4578962SSonam.Gupta@Sun.COM 	    "printer-up-time", &curr);
4582264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4598962SSonam.Gupta@Sun.COM 	    "printer-state-time", &curr);
4602264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
4618962SSonam.Gupta@Sun.COM 	    "lpsched-disable-date", &curr);
4622264Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
4632264Sjacobs 
4642264Sjacobs 	if (pstat == 0x05) {
4652264Sjacobs 		char *reason = "unknown reason";
4662264Sjacobs 
4672264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4688962SSonam.Gupta@Sun.COM 		    "printer-state-reasons", &reason);
4692264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4708962SSonam.Gupta@Sun.COM 		    "lpsched-disable-reason", &reason);
4712264Sjacobs 		printf(gettext("\t%s\n"), reason);
4722264Sjacobs 	}
4732264Sjacobs 
4742264Sjacobs 	if (verbose == 1) {
4752264Sjacobs 		void *iter;
4762264Sjacobs 		char *str;
477*9810SKeerthi.Kondaka@Sun.COM 		char *host = NULL;
4782264Sjacobs 
479*9810SKeerthi.Kondaka@Sun.COM 		if ((get_remote_hostname(attrs, &host)) != 0) {
480*9810SKeerthi.Kondaka@Sun.COM 			(void) printf(
481*9810SKeerthi.Kondaka@Sun.COM 			    gettext("\tRemote Name: %s\n\tRemote Server: "
482*9810SKeerthi.Kondaka@Sun.COM 			    "%s\n"), name, host);
483*9810SKeerthi.Kondaka@Sun.COM 			free(host);
484*9810SKeerthi.Kondaka@Sun.COM 			return (0);
485*9810SKeerthi.Kondaka@Sun.COM 		}
4862264Sjacobs 		str = "";
4872264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4888962SSonam.Gupta@Sun.COM 		    "form-ready", &str);
4892264Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
4902264Sjacobs 
4912264Sjacobs 		str = "";
4922264Sjacobs 		iter = NULL;
4932264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4948962SSonam.Gupta@Sun.COM 		    "document-format-supported", &str);
4952264Sjacobs 		printf(gettext("\tContent types: %s"), str);
4962264Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
4978962SSonam.Gupta@Sun.COM 		    == PAPI_OK)
4982264Sjacobs 			printf(", %s", str);
4992264Sjacobs 		printf("\n");
5002264Sjacobs 
5018534SSonam.Gupta@Sun.COM 		/* Display the printer description */
5028534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
5032264Sjacobs 
5042264Sjacobs 		str = "";
5056676Swendyp 		iter = NULL;
5066676Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
5076676Swendyp 		    "lpsched-printer-type", &str);
5086676Swendyp 		printf(gettext("\tPrinter types: %s"), str);
5096676Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
5106676Swendyp 		    == PAPI_OK)
5116676Swendyp 			printf(", %s", str);
5126676Swendyp 		printf("\n");
5136676Swendyp 
5146676Swendyp 		str = "";
5152264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5168962SSonam.Gupta@Sun.COM 		    "lpsched-dial-info", &str);
5172264Sjacobs 		printf(gettext("\tConnection: %s\n"),
5187653SJonathan.Ca@Sun.COM 		    ((str[0] == '\0') ? gettext("direct") : str));
5192264Sjacobs 
5202264Sjacobs 		str = "";
5212264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5228962SSonam.Gupta@Sun.COM 		    "lpsched-interface-script", &str);
5232264Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
5242264Sjacobs 
5252264Sjacobs 		str = NULL;
5262264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5278962SSonam.Gupta@Sun.COM 		    "ppd-file-uri", &str);
5282264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5298962SSonam.Gupta@Sun.COM 		    "lpsched-ppd-source-path", &str);
5302264Sjacobs 		if (str != NULL)
5312264Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
5322264Sjacobs 
5332264Sjacobs 		str = NULL;
5342264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5358962SSonam.Gupta@Sun.COM 		    "lpsched-fault-alert-command", &str);
5362264Sjacobs 		if (str != NULL)
5372264Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
5382264Sjacobs 
5392264Sjacobs 		str = "";
5402264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5418962SSonam.Gupta@Sun.COM 		    "lpsched-fault-recovery", &str);
5422264Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
5438962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("continue") : str));
5442264Sjacobs 
5452264Sjacobs 		str = "(all)";
5462264Sjacobs 		iter = NULL;
5472264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5488962SSonam.Gupta@Sun.COM 		    "requesting-user-name-allowed", &str);
5492264Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
5508962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5512264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5522264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5538962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5542264Sjacobs 				printf("\t\t%s\n", str);
5552264Sjacobs 
5562264Sjacobs 		str = NULL;
5572264Sjacobs 		iter = NULL;
5582264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5598962SSonam.Gupta@Sun.COM 		    "requesting-user-name-denied", &str);
5602264Sjacobs 		if (str != NULL) {
5612264Sjacobs 			printf(gettext("\tUsers denied:\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,
5658962SSonam.Gupta@Sun.COM 				    NULL, &str) == PAPI_OK)
5662264Sjacobs 					printf("\t\t%s\n", str);
5672264Sjacobs 		}
5682264Sjacobs 
5698238SSonam.Gupta@Sun.COM 		str = "none";
5702264Sjacobs 		iter = NULL;
5712264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5728962SSonam.Gupta@Sun.COM 		    "form-supported", &str);
5738238SSonam.Gupta@Sun.COM 		printf(gettext("\tForms allowed:\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, NULL,
5778962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5788238SSonam.Gupta@Sun.COM 				printf("\t\t(%s)\n", str);
5792264Sjacobs 
5802264Sjacobs 		str = "";
5812264Sjacobs 		iter = NULL;
5822264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5838962SSonam.Gupta@Sun.COM 		    "media-supported", &str);
5842264Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
5858962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
5862264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5872264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5888962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
5892264Sjacobs 				printf("\t\t%s\n", str);
5902264Sjacobs 
5912264Sjacobs 		str = "";
5922264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5938962SSonam.Gupta@Sun.COM 		    "job-sheets-supported", &str);
5946676Swendyp 		if ((strcasecmp(str, "none")) == 0)
5956676Swendyp 			str = gettext("page never printed");
5966676Swendyp 		else if (strcasecmp(str, "optional") == 0)
5976676Swendyp 			str = gettext("not required");
5986676Swendyp 		else
5996676Swendyp 			str = gettext("required");
6006676Swendyp 
6016676Swendyp 		printf(gettext("\tBanner %s\n"), str);
6026676Swendyp 
6032264Sjacobs 
6042264Sjacobs 		str = "";
6052264Sjacobs 		iter = NULL;
6062264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6078962SSonam.Gupta@Sun.COM 		    "lpsched-print-wheels", &str);
6082264Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
6098962SSonam.Gupta@Sun.COM 		    ((str[0] == '\0') ? gettext("(none)") : str));
6102264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
6112264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6128962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6132264Sjacobs 				printf("\t\t%s\n", str);
6142264Sjacobs 
6152264Sjacobs 		printf(gettext("\tDefault pitch:\n"));
6162264Sjacobs 		printf(gettext("\tDefault page size:\n"));
6172264Sjacobs 		printf(gettext("\tDefault port setting:\n"));
6182264Sjacobs 
6192264Sjacobs 		str = "";
6202264Sjacobs 		iter = NULL;
6212264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
6228962SSonam.Gupta@Sun.COM 		    "lpsched-options", &str);
6232264Sjacobs 		if (str != NULL) {
6242264Sjacobs 			printf(gettext("\tOptions: %s"), str);
6252264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
6268962SSonam.Gupta@Sun.COM 			    &str) == PAPI_OK)
6272264Sjacobs 				printf(", %s", str);
6282264Sjacobs 			printf("\n");
6292264Sjacobs 		}
6302264Sjacobs 
6318534SSonam.Gupta@Sun.COM 	} else if (description == 1)
6328534SSonam.Gupta@Sun.COM 		/* Display printer description */
6338534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
6348534SSonam.Gupta@Sun.COM 	else if (verbose > 1)
6352264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
6362264Sjacobs 
6372264Sjacobs 	if (verbose > 0)
6382264Sjacobs 		printf("\n");
6392264Sjacobs 
6402264Sjacobs 	return (0);
6412264Sjacobs }
6422264Sjacobs 
6432264Sjacobs static int
6442264Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
6452264Sjacobs 					int, int), papi_encryption_t encryption,
6462264Sjacobs 		int verbose, int description)
6472264Sjacobs {
6489669SGowtham.Thommandra@Sun.COM 	int result = 0, i = 0;
6492264Sjacobs 	papi_status_t status;
6502264Sjacobs 	papi_service_t svc = NULL;
6519669SGowtham.Thommandra@Sun.COM 	char **list = getlist(name, LP_WS, LP_SEP);
6522264Sjacobs 
6539669SGowtham.Thommandra@Sun.COM 	if (list == NULL) {
6549669SGowtham.Thommandra@Sun.COM 		list = (char **)malloc(sizeof (char *));
6559669SGowtham.Thommandra@Sun.COM 		list[0] = name;
6562264Sjacobs 	}
6572264Sjacobs 
6589669SGowtham.Thommandra@Sun.COM 	/*
6599669SGowtham.Thommandra@Sun.COM 	 * The for loop executes once for every printer
6609669SGowtham.Thommandra@Sun.COM 	 * entry in list. If list is NULL that implies
6619669SGowtham.Thommandra@Sun.COM 	 * name is also NULL, the loop runs only one time.
6629669SGowtham.Thommandra@Sun.COM 	 */
6632264Sjacobs 
6649669SGowtham.Thommandra@Sun.COM 	for (i = 0; name == NULL || list[i] != NULL; i++) {
6659669SGowtham.Thommandra@Sun.COM 		name = list[i];
6662264Sjacobs 
6679669SGowtham.Thommandra@Sun.COM 		status = papiServiceCreate(&svc, name, NULL, NULL,
6689669SGowtham.Thommandra@Sun.COM 		    cli_auth_callback, encryption, NULL);
6692264Sjacobs 		if (status != PAPI_OK) {
6709669SGowtham.Thommandra@Sun.COM 			if (status == PAPI_NOT_FOUND)
6719669SGowtham.Thommandra@Sun.COM 				fprintf(stderr,
6729669SGowtham.Thommandra@Sun.COM 				    gettext("%s: unknown printer\n"),
6739669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)");
6749669SGowtham.Thommandra@Sun.COM 			else
6759669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
6769669SGowtham.Thommandra@Sun.COM 				    "Failed to contact service for %s: %s\n"),
6779669SGowtham.Thommandra@Sun.COM 				    name ? name : "(NULL)",
6789669SGowtham.Thommandra@Sun.COM 				    verbose_papi_message(svc, status));
6792264Sjacobs 			papiServiceDestroy(svc);
6809669SGowtham.Thommandra@Sun.COM 			result--;
6819669SGowtham.Thommandra@Sun.COM 			continue;
6822264Sjacobs 		}
6832264Sjacobs 
6849669SGowtham.Thommandra@Sun.COM 		if (name == NULL) { /* all */
6859669SGowtham.Thommandra@Sun.COM 			char **interest = interest_list(svc);
6869669SGowtham.Thommandra@Sun.COM 
6879669SGowtham.Thommandra@Sun.COM 			if (interest != NULL) {
6889669SGowtham.Thommandra@Sun.COM 				int i;
6899669SGowtham.Thommandra@Sun.COM 
6909669SGowtham.Thommandra@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
6919669SGowtham.Thommandra@Sun.COM 					result += printer_query(interest[i],
6929669SGowtham.Thommandra@Sun.COM 					    report, encryption, verbose,
6939669SGowtham.Thommandra@Sun.COM 					    description);
6949669SGowtham.Thommandra@Sun.COM 			}
6959669SGowtham.Thommandra@Sun.COM 		} else {
6969669SGowtham.Thommandra@Sun.COM 			papi_printer_t printer = NULL;
6979669SGowtham.Thommandra@Sun.COM 			char **keys = NULL;
6982264Sjacobs 
6999669SGowtham.Thommandra@Sun.COM 			/*
7009669SGowtham.Thommandra@Sun.COM 			 * Limit the query to only required data
7019669SGowtham.Thommandra@Sun.COM 			 * to reduce the need to go remote for
7029669SGowtham.Thommandra@Sun.COM 			 * information.
7039669SGowtham.Thommandra@Sun.COM 			 */
7049669SGowtham.Thommandra@Sun.COM 			if (report == report_device)
7059669SGowtham.Thommandra@Sun.COM 				keys = report_device_keys;
7069669SGowtham.Thommandra@Sun.COM 			else if (report == report_class)
7079669SGowtham.Thommandra@Sun.COM 				keys = report_class_keys;
7089669SGowtham.Thommandra@Sun.COM 			else if (report == report_accepting)
7099669SGowtham.Thommandra@Sun.COM 				keys = report_accepting_keys;
7109669SGowtham.Thommandra@Sun.COM 			else if ((report == report_printer) && (verbose == 0))
7119669SGowtham.Thommandra@Sun.COM 				keys = report_printer_keys;
7129669SGowtham.Thommandra@Sun.COM 
7139669SGowtham.Thommandra@Sun.COM 			status = papiPrinterQuery(svc, name, keys,
7149669SGowtham.Thommandra@Sun.COM 			    NULL, &printer);
7159669SGowtham.Thommandra@Sun.COM 			if (status != PAPI_OK) {
7169669SGowtham.Thommandra@Sun.COM 				fprintf(stderr, gettext(
7179669SGowtham.Thommandra@Sun.COM 				    "Failed to get printer info for %s: %s\n"),
7189669SGowtham.Thommandra@Sun.COM 				    name, verbose_papi_message(svc, status));
7199669SGowtham.Thommandra@Sun.COM 				papiServiceDestroy(svc);
7209669SGowtham.Thommandra@Sun.COM 				result--;
7219669SGowtham.Thommandra@Sun.COM 				continue;
7229669SGowtham.Thommandra@Sun.COM 			}
7239669SGowtham.Thommandra@Sun.COM 
7249669SGowtham.Thommandra@Sun.COM 			if (printer != NULL)
7259669SGowtham.Thommandra@Sun.COM 				result += report(svc, name, printer, verbose,
7269669SGowtham.Thommandra@Sun.COM 				    description);
7279669SGowtham.Thommandra@Sun.COM 
7289669SGowtham.Thommandra@Sun.COM 			papiPrinterFree(printer);
7299669SGowtham.Thommandra@Sun.COM 		}
7309669SGowtham.Thommandra@Sun.COM 
7319669SGowtham.Thommandra@Sun.COM 		papiServiceDestroy(svc);
7329669SGowtham.Thommandra@Sun.COM 
7339669SGowtham.Thommandra@Sun.COM 		if (name == NULL)
7349669SGowtham.Thommandra@Sun.COM 			break;
7352264Sjacobs 	}
7362264Sjacobs 
7379669SGowtham.Thommandra@Sun.COM 	freelist(list);
7382264Sjacobs 
7392264Sjacobs 	return (result);
7402264Sjacobs }
7412264Sjacobs 
7422264Sjacobs static int
7432264Sjacobs match_user(char *user, char **list)
7442264Sjacobs {
7452264Sjacobs 	int i;
7462264Sjacobs 
7472264Sjacobs 	for (i = 0; list[i] != NULL; i++) {
7482264Sjacobs 		if (strcmp(user, list[i]) == 0)
7492264Sjacobs 			return (0);
7502264Sjacobs 	}
7512264Sjacobs 
7522264Sjacobs 	return (-1);
7532264Sjacobs }
7542264Sjacobs 
7552264Sjacobs static char **users = NULL;
7562264Sjacobs 
7572264Sjacobs static int
7589256SSonam.Gupta@Sun.COM report_job(char *printer, papi_job_t job, int show_rank, int verbose)
7592264Sjacobs {
7602264Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
7612264Sjacobs 	time_t clock = 0;
7622264Sjacobs 	char date[24];
7632264Sjacobs 	char request[26];
7642264Sjacobs 	char *user = "unknown";
7658321SSonam.Gupta@Sun.COM 	char *host = NULL;
7662264Sjacobs 	int32_t size = 0;
7672264Sjacobs 	int32_t jstate = 0;
7688321SSonam.Gupta@Sun.COM 	char User[50];
7692264Sjacobs 
7702264Sjacobs 	char *destination = "unknown";
7712264Sjacobs 	int32_t id = -1;
7729152SSonam.Gupta@Sun.COM 	static int check = 0;
7739152SSonam.Gupta@Sun.COM 	static char *uri = NULL;
7742264Sjacobs 
7752264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
7768962SSonam.Gupta@Sun.COM 	    "job-originating-user-name", &user);
7772264Sjacobs 
7782264Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
7792264Sjacobs 		return (0);
7802264Sjacobs 
7818321SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
7828321SSonam.Gupta@Sun.COM 	    "job-originating-host-name", &host);
7838321SSonam.Gupta@Sun.COM 
7849152SSonam.Gupta@Sun.COM 	if (check == 0) {
7859152SSonam.Gupta@Sun.COM 		/*
7869152SSonam.Gupta@Sun.COM 		 * Read the attribute "job-printer-uri"
7879152SSonam.Gupta@Sun.COM 		 * just once
7889152SSonam.Gupta@Sun.COM 		 */
7899152SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(attrs, NULL,
7909152SSonam.Gupta@Sun.COM 		    "job-printer-uri", &uri);
7919152SSonam.Gupta@Sun.COM 		check = 1;
7929152SSonam.Gupta@Sun.COM 	}
7939152SSonam.Gupta@Sun.COM 
7949152SSonam.Gupta@Sun.COM 	if (host) {
7959152SSonam.Gupta@Sun.COM 		/* Check if it is local printer or remote printer */
7969152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
7979152SSonam.Gupta@Sun.COM 
7989152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
7999152SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
8009152SSonam.Gupta@Sun.COM 
8019152SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
8029152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
8039152SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0)) {
8048321SSonam.Gupta@Sun.COM 
8059152SSonam.Gupta@Sun.COM 				if (strcasecmp(host, nodename) == 0) {
8069152SSonam.Gupta@Sun.COM 					/*
8079152SSonam.Gupta@Sun.COM 					 * Request submitted locally
8089152SSonam.Gupta@Sun.COM 					 * for the local queue.
8099152SSonam.Gupta@Sun.COM 					 * Hostname will not be displayed
8109152SSonam.Gupta@Sun.COM 					 */
8119152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s",
8129152SSonam.Gupta@Sun.COM 					    user);
8139152SSonam.Gupta@Sun.COM 				}
8149152SSonam.Gupta@Sun.COM 				else
8159152SSonam.Gupta@Sun.COM 					snprintf(User, sizeof (User), "%s@%s",
8169152SSonam.Gupta@Sun.COM 					    user, host);
8179152SSonam.Gupta@Sun.COM 			} else if (uri != NULL) {
8189152SSonam.Gupta@Sun.COM 				/*
8199152SSonam.Gupta@Sun.COM 				 * It's a remote printer.
8209152SSonam.Gupta@Sun.COM 				 * In case of remote printers hostname is
8219152SSonam.Gupta@Sun.COM 				 * always displayed.
8229152SSonam.Gupta@Sun.COM 				 */
8239152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
8249152SSonam.Gupta@Sun.COM 				    user, host);
8259152SSonam.Gupta@Sun.COM 			}
8269152SSonam.Gupta@Sun.COM 			uri_free(u);
8279152SSonam.Gupta@Sun.COM 		} else {
8289152SSonam.Gupta@Sun.COM 			/*
8299152SSonam.Gupta@Sun.COM 			 * If attribute "job-printer-uri"
8309152SSonam.Gupta@Sun.COM 			 * cannot be read
8319152SSonam.Gupta@Sun.COM 			 * by default append the hostname
8329152SSonam.Gupta@Sun.COM 			 */
8339152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s@%s", user, host);
8349152SSonam.Gupta@Sun.COM 		}
8359152SSonam.Gupta@Sun.COM 	} else {
8369152SSonam.Gupta@Sun.COM 		/*
8379152SSonam.Gupta@Sun.COM 		 * When print server is s10u4 and ipp service is used
8389152SSonam.Gupta@Sun.COM 		 * "job-originating-hostname" attribute is not set
8399152SSonam.Gupta@Sun.COM 		 * So get the host information from the uri
8409152SSonam.Gupta@Sun.COM 		 */
8419152SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
8429152SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
8439152SSonam.Gupta@Sun.COM 			if ((u != NULL) && (u->host != NULL))
8449152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s@%s",
8459152SSonam.Gupta@Sun.COM 				    user, u->host);
8469152SSonam.Gupta@Sun.COM 			else
8479152SSonam.Gupta@Sun.COM 				snprintf(User, sizeof (User), "%s", user);
8489152SSonam.Gupta@Sun.COM 
8499152SSonam.Gupta@Sun.COM 			uri_free(u);
8509152SSonam.Gupta@Sun.COM 		} else
8519152SSonam.Gupta@Sun.COM 			snprintf(User, sizeof (User), "%s", user);
8529152SSonam.Gupta@Sun.COM 	}
8532264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
8542264Sjacobs 	size *= 1024;	/* for the approximate byte size */
8552264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
8562264Sjacobs 
8572264Sjacobs 	(void) time(&clock);
8582264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8598962SSonam.Gupta@Sun.COM 	    "time-at-creation", (int32_t *)&clock);
8602264Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
8612264Sjacobs 
8622264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
8638962SSonam.Gupta@Sun.COM 	    "job-printer-uri", &destination);
8642264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
8658962SSonam.Gupta@Sun.COM 	    "printer-name", &destination);
8662264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8678962SSonam.Gupta@Sun.COM 	    "job-id", &id);
8689330SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetInteger(attrs, NULL,
8699330SSonam.Gupta@Sun.COM 	    "job-id-requested", &id);
8709256SSonam.Gupta@Sun.COM 
8719331SSonam.Gupta@Sun.COM 
8729256SSonam.Gupta@Sun.COM 	snprintf(request, sizeof (request), "%s-%d", printer, id);
8732264Sjacobs 
8742264Sjacobs 	if (show_rank != 0) {
8752264Sjacobs 		int32_t rank = -1;
8762264Sjacobs 
8772264Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
8788962SSonam.Gupta@Sun.COM 		    "number-of-intervening-jobs", &rank);
8792264Sjacobs 		rank++;
8802264Sjacobs 
8812264Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
8828321SSonam.Gupta@Sun.COM 		    rank, request, User, size, date);
8832264Sjacobs 	} else
8848321SSonam.Gupta@Sun.COM 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
8852264Sjacobs 
8862264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
8878962SSonam.Gupta@Sun.COM 	    "job-state", &jstate);
8889331SSonam.Gupta@Sun.COM 
8899257SGowtham.Thommandra@Sun.COM 	if (jstate == 0x0001)
8909257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being held"));
8919257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0800)
8929257SGowtham.Thommandra@Sun.COM 		printf(gettext(" notifying user"));
8939257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0040)
8949257SGowtham.Thommandra@Sun.COM 		printf(gettext(" cancelled"));
8959257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0010)
8969257SGowtham.Thommandra@Sun.COM 		printf(gettext(" finished printing"));
8979257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0008)
8989257SGowtham.Thommandra@Sun.COM 		printf(gettext(" on %s"), destination);
8999257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x2000)
9009257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held by admin"));
9019257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0002)
9029257SGowtham.Thommandra@Sun.COM 		printf(gettext(" being filtered"));
9039257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0004)
9049257SGowtham.Thommandra@Sun.COM 		printf(gettext(" filtered"));
9059257SGowtham.Thommandra@Sun.COM 	else if (jstate == 0x0020)
9069257SGowtham.Thommandra@Sun.COM 		printf(gettext(" held for change"));
9072264Sjacobs 
9082264Sjacobs 	if (verbose == 1) {
9093125Sjacobs 		char *form = NULL;
9103125Sjacobs 
9112264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
9128962SSonam.Gupta@Sun.COM 		    "output-device-assigned", &destination);
9132264Sjacobs 		printf("\n\t assigned %s", destination);
9143127Sjacobs 
9153125Sjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
9163125Sjacobs 		if (form != NULL)
9173125Sjacobs 			printf(", form %s", form);
9182264Sjacobs 	} else if (verbose > 1) {
9192264Sjacobs 		printf("\n");
9202264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
9212264Sjacobs 	}
9222264Sjacobs 
9232264Sjacobs 	printf("\n");
9242264Sjacobs 
9252264Sjacobs 	return (0);
9262264Sjacobs }
9272264Sjacobs 
9282264Sjacobs static int
9299256SSonam.Gupta@Sun.COM job_query(char *request, int (*report)(char *, papi_job_t, int, int),
9302264Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
9312264Sjacobs {
9322264Sjacobs 	int result = 0;
9332264Sjacobs 	papi_status_t status;
9342264Sjacobs 	papi_service_t svc = NULL;
9358295SSonam.Gupta@Sun.COM 	char *printer = request;
9362264Sjacobs 	int32_t id = -1;
9378295SSonam.Gupta@Sun.COM 	int flag1 = 0;
9388295SSonam.Gupta@Sun.COM 	int flag = 1;
9398295SSonam.Gupta@Sun.COM 	int print_flag = 0;
9402264Sjacobs 
9418295SSonam.Gupta@Sun.COM 	do {
9428295SSonam.Gupta@Sun.COM 		status = papiServiceCreate(&svc, printer, NULL, NULL,
9438962SSonam.Gupta@Sun.COM 		    cli_auth_callback, encryption, NULL);
9442264Sjacobs 
9458295SSonam.Gupta@Sun.COM 		if ((status == PAPI_OK) && (printer != NULL))
9468295SSonam.Gupta@Sun.COM 			print_flag = 1;
9472264Sjacobs 
9488295SSonam.Gupta@Sun.COM 		/* <name>-# printer name does not exist */
9498295SSonam.Gupta@Sun.COM 		if (status != PAPI_OK) {
9508295SSonam.Gupta@Sun.COM 			/*
9518295SSonam.Gupta@Sun.COM 			 * Check if <name>-# is a request-id
9528295SSonam.Gupta@Sun.COM 			 * Once this check is done flag1 is set
9538295SSonam.Gupta@Sun.COM 			 */
9548295SSonam.Gupta@Sun.COM 			if (flag1 == 1)
9558295SSonam.Gupta@Sun.COM 				break;
9568295SSonam.Gupta@Sun.COM 
9578295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
9582264Sjacobs 
9598295SSonam.Gupta@Sun.COM 			status = papiServiceCreate(&svc, printer, NULL, NULL,
9608962SSonam.Gupta@Sun.COM 			    cli_auth_callback, encryption, NULL);
9612264Sjacobs 
9628295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
9638295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
9648295SSonam.Gupta@Sun.COM 				    "Failed to contact service for %s: %s\n"),
9658295SSonam.Gupta@Sun.COM 				    (printer ? printer : "all"),
9668295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
9678295SSonam.Gupta@Sun.COM 				return (-1);
9688295SSonam.Gupta@Sun.COM 			}
9692264Sjacobs 		}
9702264Sjacobs 
9718295SSonam.Gupta@Sun.COM 		if (printer == NULL) { /* all */
9728295SSonam.Gupta@Sun.COM 			char **interest = interest_list(svc);
9738295SSonam.Gupta@Sun.COM 
9748295SSonam.Gupta@Sun.COM 			if (interest != NULL) {
9758295SSonam.Gupta@Sun.COM 				int i;
9768295SSonam.Gupta@Sun.COM 
9778295SSonam.Gupta@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
9788295SSonam.Gupta@Sun.COM 					result += job_query(interest[i], report,
9798962SSonam.Gupta@Sun.COM 					    encryption, show_rank, verbose);
9808295SSonam.Gupta@Sun.COM 			}
9818295SSonam.Gupta@Sun.COM 		} else if (id == -1) { /* a printer */
9828295SSonam.Gupta@Sun.COM 			papi_job_t *jobs = NULL;
9838295SSonam.Gupta@Sun.COM 
9848295SSonam.Gupta@Sun.COM 			status = papiPrinterListJobs(svc, printer, NULL,
9858962SSonam.Gupta@Sun.COM 			    0, 0, &jobs);
9868295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
9878295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
9888295SSonam.Gupta@Sun.COM 				    "Failed to get job list: %s\n"),
9898295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
9908295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
9918295SSonam.Gupta@Sun.COM 				return (-1);
9928295SSonam.Gupta@Sun.COM 			}
9938295SSonam.Gupta@Sun.COM 
9948295SSonam.Gupta@Sun.COM 			if (jobs != NULL) {
9958295SSonam.Gupta@Sun.COM 				int i;
9962264Sjacobs 
9978295SSonam.Gupta@Sun.COM 				for (i = 0; jobs[i] != NULL; i++)
9989256SSonam.Gupta@Sun.COM 					result += report(printer,
9999256SSonam.Gupta@Sun.COM 					    jobs[i], show_rank,
10009256SSonam.Gupta@Sun.COM 					    verbose);
10018295SSonam.Gupta@Sun.COM 			}
10028295SSonam.Gupta@Sun.COM 
10038295SSonam.Gupta@Sun.COM 			papiJobListFree(jobs);
10048295SSonam.Gupta@Sun.COM 		} else {	/* a job */
10058295SSonam.Gupta@Sun.COM 			papi_job_t job = NULL;
10069331SSonam.Gupta@Sun.COM 			int rid = id;
10078295SSonam.Gupta@Sun.COM 
10088295SSonam.Gupta@Sun.COM 			/* Once a job has been found stop processing */
10098295SSonam.Gupta@Sun.COM 			flag = 0;
10108295SSonam.Gupta@Sun.COM 
10119331SSonam.Gupta@Sun.COM 			/*
10129331SSonam.Gupta@Sun.COM 			 * Job-id could be the job-id requested
10139331SSonam.Gupta@Sun.COM 			 * Check if it is job-id or job-id-requested
10149331SSonam.Gupta@Sun.COM 			 */
10159331SSonam.Gupta@Sun.COM 			id = job_to_be_queried(svc, printer, id);
10169331SSonam.Gupta@Sun.COM 
10179331SSonam.Gupta@Sun.COM 			if (id > 0)
10189669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, id,
10199669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
10209331SSonam.Gupta@Sun.COM 			else
10219669SGowtham.Thommandra@Sun.COM 				status = papiJobQuery(svc, printer, rid,
10229669SGowtham.Thommandra@Sun.COM 				    NULL, &job);
10239331SSonam.Gupta@Sun.COM 
10248295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
10258295SSonam.Gupta@Sun.COM 				if (!print_flag)
10268295SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
10278962SSonam.Gupta@Sun.COM 					    "Failed to get job"\
10288962SSonam.Gupta@Sun.COM 					    " info for %s: %s\n"),
10298962SSonam.Gupta@Sun.COM 					    request,
10308962SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
10318295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
10328295SSonam.Gupta@Sun.COM 				return (-1);
10338295SSonam.Gupta@Sun.COM 			}
10348295SSonam.Gupta@Sun.COM 
10358295SSonam.Gupta@Sun.COM 			if (job != NULL)
10369256SSonam.Gupta@Sun.COM 				result = report(printer, job,
10379256SSonam.Gupta@Sun.COM 				    show_rank, verbose);
10388295SSonam.Gupta@Sun.COM 
10398295SSonam.Gupta@Sun.COM 			papiJobFree(job);
10402264Sjacobs 		}
10412264Sjacobs 
10428295SSonam.Gupta@Sun.COM 		if (flag) {
10438295SSonam.Gupta@Sun.COM 			id = -1;
10448295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
10458295SSonam.Gupta@Sun.COM 			if (id == -1)
10468295SSonam.Gupta@Sun.COM 				flag = 0;
10478295SSonam.Gupta@Sun.COM 			else
10488295SSonam.Gupta@Sun.COM 				flag1 = 1;
10492264Sjacobs 		}
10508295SSonam.Gupta@Sun.COM 	} while (flag);
10512264Sjacobs 
10522264Sjacobs 	papiServiceDestroy(svc);
10532264Sjacobs 
10542264Sjacobs 	return (result);
10552264Sjacobs }
10562264Sjacobs 
10572264Sjacobs static int
10582264Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
10592264Sjacobs {
10602264Sjacobs 	papi_status_t status;
10612264Sjacobs 	char *form = NULL;
10622264Sjacobs 	void *iter = NULL;
10632264Sjacobs 
10642264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
10658962SSonam.Gupta@Sun.COM 	    "form-supported", &form);
10668962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
10678962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter,
10688962SSonam.Gupta@Sun.COM 	    NULL, &form)) {
10692264Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
10702264Sjacobs 			printf(gettext("form %s is available to you\n"), form);
10712264Sjacobs 			if (verbose != 0) {
10722264Sjacobs 				char *detail = NULL;
10732264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
10748962SSonam.Gupta@Sun.COM 				    "form-supported-detail", &detail);
10752264Sjacobs 				if (status == PAPI_OK)
10762264Sjacobs 					printf("%s\n", detail);
10772264Sjacobs 			}
10782264Sjacobs 		}
10792264Sjacobs 	}
10802264Sjacobs 
10812264Sjacobs 	return (0);
10822264Sjacobs }
10832264Sjacobs 
10842264Sjacobs static int
10852264Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
10862264Sjacobs {
10872264Sjacobs 	papi_status_t status;
10882264Sjacobs 	char *pw = NULL;
10892264Sjacobs 	void *iter = NULL;
10902264Sjacobs 
10912264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
10928962SSonam.Gupta@Sun.COM 	    "pw-supported", &pw);
10938962SSonam.Gupta@Sun.COM 	    status == PAPI_OK;
10948962SSonam.Gupta@Sun.COM 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
10952264Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
10962264Sjacobs 			printf(gettext("charset %s is available\n"), pw);
10972264Sjacobs 			if (verbose != 0) {
10982264Sjacobs 				char *info = NULL;
10992264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
11008962SSonam.Gupta@Sun.COM 				    "pw-supported-extra", &info);
11012264Sjacobs 				if (status == PAPI_OK)
11022264Sjacobs 					printf("%s\n", info);
11032264Sjacobs 			}
11042264Sjacobs 		}
11052264Sjacobs 	}
11062264Sjacobs 
11072264Sjacobs 	return (0);
11082264Sjacobs }
11092264Sjacobs 
11102264Sjacobs static int
11112264Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
11122264Sjacobs 		papi_encryption_t encryption, int verbose)
11132264Sjacobs {
11142264Sjacobs 	int result = 0;
11152264Sjacobs 	papi_status_t status;
11162264Sjacobs 	papi_service_t svc = NULL;
11172264Sjacobs 	papi_attribute_t **attrs = NULL;
11182264Sjacobs 
11192264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
11208962SSonam.Gupta@Sun.COM 	    encryption, NULL);
11212264Sjacobs 	if (status != PAPI_OK) {
11222264Sjacobs 		papiServiceDestroy(svc);
11232264Sjacobs 		return (-1);
11242264Sjacobs 	}
11252264Sjacobs 
11262264Sjacobs 	attrs = papiServiceGetAttributeList(svc);
11272264Sjacobs 	if (attrs != NULL) {
11282264Sjacobs 		result = report(name, attrs, verbose);
11292264Sjacobs 
11302264Sjacobs 		if (verbose > 1) {
11312264Sjacobs 			printf("\n");
11322264Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
11332264Sjacobs 			printf("\n");
11342264Sjacobs 		}
11352264Sjacobs 	}
11362264Sjacobs 
11372264Sjacobs 	papiServiceDestroy(svc);
11382264Sjacobs 
11392264Sjacobs 	return (result);
11402264Sjacobs }
11412264Sjacobs 
11422264Sjacobs int
11432264Sjacobs main(int ac, char *av[])
11442264Sjacobs {
11452264Sjacobs 	int exit_code = 0;
11462264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
11472264Sjacobs 	int rank = 0;
11482264Sjacobs 	int verbose = 0;
11492264Sjacobs 	int description = 0;
11502264Sjacobs 	int c;
11512264Sjacobs 	char **argv;
11522264Sjacobs 
11532264Sjacobs 	(void) setlocale(LC_ALL, "");
11542264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
11552264Sjacobs 
11562264Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
11574286Swendyp 	for (c = 0; c < ac; c++) {
11584286Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
11598962SSonam.Gupta@Sun.COM 		    (isalpha(av[c][2]) != 0)) {
11604286Swendyp 			/* preserve old "-l[po...]" behavior */
11614286Swendyp 			argv[c] = &av[c][1];
11624286Swendyp 			argv[c][0] = '-';
11634286Swendyp 			verbose = 1;
11644286Swendyp 
11654286Swendyp 		} else
11664286Swendyp 			argv[c] = av[c];
11674286Swendyp 	}
11684286Swendyp 
11692264Sjacobs 	argv[c++] = "--";
11702264Sjacobs 	ac = c;
11712264Sjacobs 
11722264Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
11739740SKeerthi.Kondaka@Sun.COM 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
11749740SKeerthi.Kondaka@Sun.COM 		switch (c) {    /* these may or may not have an option */
11759740SKeerthi.Kondaka@Sun.COM 		case 'a':
11769740SKeerthi.Kondaka@Sun.COM 		case 'c':
11779740SKeerthi.Kondaka@Sun.COM 		case 'p':
11789740SKeerthi.Kondaka@Sun.COM 		case 'o':
11799740SKeerthi.Kondaka@Sun.COM 		case 'R':
11809740SKeerthi.Kondaka@Sun.COM 		case 'u':
11819740SKeerthi.Kondaka@Sun.COM 		case 'v':
11829740SKeerthi.Kondaka@Sun.COM 		case 'l':
11839740SKeerthi.Kondaka@Sun.COM 		case 'f':
11849740SKeerthi.Kondaka@Sun.COM 		case 'S':
11859740SKeerthi.Kondaka@Sun.COM 			if (optarg[0] == '-') {
11869740SKeerthi.Kondaka@Sun.COM 				/* this check stop a possible infinite loop */
11879740SKeerthi.Kondaka@Sun.COM 				if ((optind > 1) && (argv[optind-1][1] != c))
11889740SKeerthi.Kondaka@Sun.COM 					optind--;
11899740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
11909740SKeerthi.Kondaka@Sun.COM 			} else if (strcmp(optarg, "all") == 0)
11919740SKeerthi.Kondaka@Sun.COM 				optarg = NULL;
11929740SKeerthi.Kondaka@Sun.COM 		}
11939740SKeerthi.Kondaka@Sun.COM 
11942264Sjacobs 		switch (c) {
11952264Sjacobs 		case 'l':
11962264Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
11972264Sjacobs 				optarg = "1";
11982264Sjacobs 			verbose = atoi(optarg);
11992264Sjacobs 			break;
12002264Sjacobs 		case 'D':
12012264Sjacobs 			description = 1;
12022264Sjacobs 			break;
12032264Sjacobs 		case 'R':
12042264Sjacobs 			rank = 1;
12052264Sjacobs 			break;
12062264Sjacobs 		case 'E':
12072264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
12082264Sjacobs 			break;
12092264Sjacobs 		default:
12102264Sjacobs 			break;
12112264Sjacobs 		}
12129740SKeerthi.Kondaka@Sun.COM 	}
12132264Sjacobs 	optind = 1;
12142264Sjacobs 
12152264Sjacobs 	/* process command line arguments */
12162264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
12172264Sjacobs 		switch (c) {	/* these may or may not have an option */
12182264Sjacobs 		case 'a':
12192264Sjacobs 		case 'c':
12202264Sjacobs 		case 'p':
12212264Sjacobs 		case 'o':
12222264Sjacobs 		case 'R':
12232264Sjacobs 		case 'u':
12242264Sjacobs 		case 'v':
12252264Sjacobs 		case 'l':
12262264Sjacobs 		case 'f':
12272264Sjacobs 		case 'S':
12282264Sjacobs 			if (optarg[0] == '-') {
12292264Sjacobs 				/* this check stop a possible infinite loop */
12302264Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
12312264Sjacobs 					optind--;
12322264Sjacobs 				optarg = NULL;
12332264Sjacobs 			} else if (strcmp(optarg, "all") == 0)
12342264Sjacobs 				optarg = NULL;
12352264Sjacobs 		}
12362264Sjacobs 
12372264Sjacobs 		switch (c) {
12382264Sjacobs 		case 'a':
12392264Sjacobs 			exit_code += printer_query(optarg, report_accepting,
12408962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12412264Sjacobs 			break;
12422264Sjacobs 		case 'c':
12432264Sjacobs 			exit_code += printer_query(optarg, report_class,
12448962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12452264Sjacobs 			break;
12462264Sjacobs 		case 'p':
12472264Sjacobs 			exit_code += printer_query(optarg, report_printer,
12488962SSonam.Gupta@Sun.COM 			    encryption, verbose, description);
12492264Sjacobs 			break;
12502264Sjacobs 		case 'd':
12512264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12522264Sjacobs 			break;
12532264Sjacobs 		case 'r':
12542264Sjacobs 			exit_code += lpstat_service_status(encryption);
12552264Sjacobs 			break;
12562264Sjacobs 		case 'u':
12572264Sjacobs 			if (optarg != NULL)
12582264Sjacobs 				users = strsplit(optarg, ", \n");
12592264Sjacobs 			exit_code += job_query(NULL, report_job,
12608962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
12612264Sjacobs 			if (users != NULL) {
12622264Sjacobs 				free(users);
12632264Sjacobs 				users = NULL;
12642264Sjacobs 			}
12652264Sjacobs 			break;
12662264Sjacobs 		case 'v':
12672264Sjacobs 			exit_code += printer_query(optarg, report_device,
12688962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12692264Sjacobs 			break;
12708604SGowtham.Thommandra@Sun.COM 		case 'R':	/* set "rank" flag in first pass */
12712264Sjacobs 		case 'o':
12722264Sjacobs 			exit_code += job_query(optarg, report_job,
12738962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
12742264Sjacobs 			break;
12752264Sjacobs 		case 'f':
12762264Sjacobs 			exit_code += service_query(optarg, report_form,
12778962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12782264Sjacobs 			break;
12792264Sjacobs 		case 'S':
12802264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
12818962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12822264Sjacobs 			break;
12832264Sjacobs 		case 's':
12842264Sjacobs 			exit_code += lpstat_service_status(encryption);
12852264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12862264Sjacobs 			exit_code += printer_query(NULL, report_class,
12878962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12882264Sjacobs 			exit_code += printer_query(NULL, report_device,
12898962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
12902264Sjacobs 			exit_code += service_query(optarg, report_form,
12918962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12922264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
12938962SSonam.Gupta@Sun.COM 			    encryption, verbose);
12942264Sjacobs 			break;
12952264Sjacobs 		case 't':
12962264Sjacobs 			exit_code += lpstat_service_status(encryption);
12972264Sjacobs 			exit_code += lpstat_default_printer(encryption);
12982264Sjacobs 			exit_code += printer_query(NULL, report_class,
12998962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13002264Sjacobs 			exit_code += printer_query(NULL, report_device,
13018962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13022264Sjacobs 			exit_code += printer_query(NULL, report_accepting,
13038962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13042264Sjacobs 			exit_code += printer_query(NULL, report_printer,
13058962SSonam.Gupta@Sun.COM 			    encryption, verbose, 0);
13062264Sjacobs 			exit_code += service_query(optarg, report_form,
13078962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13082264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
13098962SSonam.Gupta@Sun.COM 			    encryption, verbose);
13102264Sjacobs 			exit_code += job_query(NULL, report_job,
13118962SSonam.Gupta@Sun.COM 			    encryption, rank, verbose);
13122264Sjacobs 			break;
13132264Sjacobs 		case 'L':	/* local-only, ignored */
13142264Sjacobs 		case 'l':	/* increased verbose level in first pass */
13152264Sjacobs 		case 'D':	/* set "description" flag in first pass */
13162264Sjacobs 		case 'E':	/* set encryption in the first pass */
13172264Sjacobs 			break;
13182264Sjacobs 		default:
13192264Sjacobs 			usage(av[0]);
13202264Sjacobs 		}
13212264Sjacobs 	}
13222264Sjacobs 	ac--;
13232264Sjacobs 
13242264Sjacobs 	if (ac == 1) {	/* report on my jobs */
13252264Sjacobs 		struct passwd *pw = getpwuid(getuid());
13262264Sjacobs 
13272264Sjacobs 		if (pw != NULL)
13282264Sjacobs 			users = strsplit(pw->pw_name, "");
13292264Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
13308962SSonam.Gupta@Sun.COM 		    rank, verbose);
13312264Sjacobs 		if (users != NULL) {
13322264Sjacobs 			free(users);
13332264Sjacobs 			users = NULL;
13342264Sjacobs 		}
13352264Sjacobs 	} else {
13362264Sjacobs 		for (c = optind; c < ac; c++)
13372264Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
13388962SSonam.Gupta@Sun.COM 			    rank, verbose);
13392264Sjacobs 	}
13402264Sjacobs 
13412264Sjacobs 
13422264Sjacobs 	if (exit_code != 0)
13432264Sjacobs 		exit_code = 1;
13442264Sjacobs 
13452264Sjacobs 	return (exit_code);
13462264Sjacobs }
1347