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*8534SSonam.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"
422264Sjacobs 
432264Sjacobs static void
442264Sjacobs usage(char *program)
452264Sjacobs {
462264Sjacobs 	char *name;
472264Sjacobs 
482264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
492264Sjacobs 		name = program;
502264Sjacobs 	else
512264Sjacobs 		name++;
522264Sjacobs 
532264Sjacobs 	fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
542264Sjacobs 		"[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
552264Sjacobs 		"[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
562264Sjacobs 		"[-f [list] [-l]] [-u list]\n"),
572264Sjacobs 		name);
582264Sjacobs 	exit(1);
592264Sjacobs }
602264Sjacobs 
612264Sjacobs static char *
622264Sjacobs nctime(time_t *t)
632264Sjacobs {
642264Sjacobs 	static char buf[64];
652264Sjacobs 	struct tm *tm = localtime(t);
662264Sjacobs 
672264Sjacobs 	(void) strftime(buf, sizeof (buf), "%c", tm);
682264Sjacobs 
692264Sjacobs 	return (buf);
702264Sjacobs }
712264Sjacobs 
722264Sjacobs static char *
732264Sjacobs printer_name(papi_printer_t printer)
742264Sjacobs {
752264Sjacobs 	papi_attribute_t **attributes = papiPrinterGetAttributeList(printer);
762264Sjacobs 	char *result = NULL;
772264Sjacobs 
782264Sjacobs 	if (attributes != NULL)
792264Sjacobs 		papiAttributeListGetString(attributes, NULL,
802264Sjacobs 				"printer-name", &result);
812264Sjacobs 
822264Sjacobs 	return (result);
832264Sjacobs }
842264Sjacobs 
852264Sjacobs static int
862264Sjacobs lpstat_default_printer(papi_encryption_t encryption)
872264Sjacobs {
882264Sjacobs 	papi_status_t status;
892264Sjacobs 	papi_service_t svc = NULL;
902264Sjacobs 	papi_printer_t p = NULL;
912264Sjacobs 	char *name = NULL;
922264Sjacobs 
932264Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback,
942264Sjacobs 					encryption, NULL);
952264Sjacobs 	if (status == PAPI_OK) {
962264Sjacobs 		char *req[] = { "printer-name", NULL };
972264Sjacobs 
982264Sjacobs 		status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p);
992264Sjacobs 		if (p != NULL)
1002264Sjacobs 			name = printer_name(p);
1012264Sjacobs 	}
1022264Sjacobs 	if (name != NULL)
1032264Sjacobs 		printf(gettext("system default printer: %s\n"), name);
1042264Sjacobs 	else
1052264Sjacobs 		printf(gettext("no system default destination\n"));
1062264Sjacobs 	papiPrinterFree(p);
1072264Sjacobs 	papiServiceDestroy(svc);
1082264Sjacobs 
1092264Sjacobs 	return (0);
1102264Sjacobs }
1112264Sjacobs 
1122264Sjacobs static int
1132264Sjacobs lpstat_service_status(papi_encryption_t encryption)
1142264Sjacobs {
1152264Sjacobs 	int result = 0;
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,
1262264Sjacobs 					encryption, NULL);
1272264Sjacobs 	if (status != PAPI_OK) {
1282264Sjacobs 		printf(gettext("scheduler is not running\n"));
1292264Sjacobs 		result = -1;
1302264Sjacobs 	} else
1312264Sjacobs 		printf(gettext("scheduler is running\n"));
1322264Sjacobs 	papiServiceDestroy(svc);
1332264Sjacobs 
1342264Sjacobs 	return (result);
1352264Sjacobs }
1362264Sjacobs 
1372264Sjacobs static char *
1382264Sjacobs get_device_uri(papi_service_t svc, char *name)
1392264Sjacobs {
1402264Sjacobs 	papi_status_t status;
1412264Sjacobs 	papi_printer_t p = NULL;
1422264Sjacobs 	char *keys[] = { "device-uri", NULL };
1432264Sjacobs 	char *result = NULL;
1442264Sjacobs 
1452264Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
1462264Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
1472264Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
1482264Sjacobs 
1492264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
1502264Sjacobs 					"device-uri", &result);
1512264Sjacobs 		if (result != NULL)
1522264Sjacobs 			result = strdup(result);
1532264Sjacobs 
1542264Sjacobs 		papiPrinterFree(p);
1552264Sjacobs 	}
1562264Sjacobs 
1572264Sjacobs 	return (result);
1582264Sjacobs }
1592264Sjacobs 
160*8534SSonam.Gupta@Sun.COM static void
161*8534SSonam.Gupta@Sun.COM print_description(papi_attribute_t **list, char *printer_name)
162*8534SSonam.Gupta@Sun.COM {
163*8534SSonam.Gupta@Sun.COM 	char *str = "";
164*8534SSonam.Gupta@Sun.COM 
165*8534SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(list, NULL,
166*8534SSonam.Gupta@Sun.COM 	    "printer-info", &str);
167*8534SSonam.Gupta@Sun.COM 
168*8534SSonam.Gupta@Sun.COM 	/*
169*8534SSonam.Gupta@Sun.COM 	 * If no printer-info is read then
170*8534SSonam.Gupta@Sun.COM 	 * by default the printer-info is <printer-name>@<server>
171*8534SSonam.Gupta@Sun.COM 	 */
172*8534SSonam.Gupta@Sun.COM 	if (str[0] == '\0') {
173*8534SSonam.Gupta@Sun.COM 		char *uri = NULL;
174*8534SSonam.Gupta@Sun.COM 		uri_t *u = NULL;
175*8534SSonam.Gupta@Sun.COM 
176*8534SSonam.Gupta@Sun.COM 		(void) papiAttributeListGetString(list, NULL,
177*8534SSonam.Gupta@Sun.COM 		    "printer-uri-supported", &uri);
178*8534SSonam.Gupta@Sun.COM 
179*8534SSonam.Gupta@Sun.COM 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
180*8534SSonam.Gupta@Sun.COM 			char *nodename = localhostname();
181*8534SSonam.Gupta@Sun.COM 
182*8534SSonam.Gupta@Sun.COM 			if ((u->host == NULL) ||
183*8534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, "localhost") == 0) ||
184*8534SSonam.Gupta@Sun.COM 			    (strcasecmp(u->host, nodename) == 0))
185*8534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription:\n"));
186*8534SSonam.Gupta@Sun.COM 			else
187*8534SSonam.Gupta@Sun.COM 				printf(gettext("\tDescription: %s@%s\n"),
188*8534SSonam.Gupta@Sun.COM 				    printer_name, u->host);
189*8534SSonam.Gupta@Sun.COM 
190*8534SSonam.Gupta@Sun.COM 			uri_free(u);
191*8534SSonam.Gupta@Sun.COM 		} else
192*8534SSonam.Gupta@Sun.COM 			printf(gettext("\tDescription:\n"));
193*8534SSonam.Gupta@Sun.COM 	} else
194*8534SSonam.Gupta@Sun.COM 		printf(gettext("\tDescription: %s\n"), str);
195*8534SSonam.Gupta@Sun.COM }
196*8534SSonam.Gupta@Sun.COM 
1972264Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
1982264Sjacobs 					NULL };
1992264Sjacobs /* ARGSUSED2 */
2002264Sjacobs static int
2012264Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
2022264Sjacobs 		int verbose, int description)
2032264Sjacobs {
2042264Sjacobs 	papi_status_t status;
2052264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2062264Sjacobs 	char *uri = NULL;
2072264Sjacobs 	char *device = NULL;
2082264Sjacobs 	uri_t *u = NULL;
2092264Sjacobs 
2102264Sjacobs 	if (name == NULL) {
2112264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2122264Sjacobs 					"printer-name", &name);
2132264Sjacobs 		if (status != PAPI_OK)
2142264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2152264Sjacobs 					"printer-uri-supported", &name);
2162264Sjacobs 	}
2172264Sjacobs 
2182264Sjacobs 	if (name == NULL)
2192264Sjacobs 		return (-1);
2202264Sjacobs 
2212264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
2222264Sjacobs 					"printer-uri-supported", &uri);
2232264Sjacobs 
2242264Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
2252264Sjacobs 		char *nodename = localhostname();
2262264Sjacobs 
2272264Sjacobs 		if ((u->host == NULL) ||
2282264Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
2292264Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
2302264Sjacobs 			device = get_device_uri(svc, name);
2312264Sjacobs 
2322264Sjacobs 		if (device != NULL) {
2332264Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
2342264Sjacobs 			return (0);
2352264Sjacobs 		} else if (uri != NULL) {
2362264Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
2372264Sjacobs 				u->host, uri);
2382264Sjacobs 			return (0);
2392264Sjacobs 		}
2402264Sjacobs 
2412264Sjacobs 		uri_free(u);
2422264Sjacobs 	}
2432264Sjacobs 
2442264Sjacobs 	return (0);
2452264Sjacobs }
2462264Sjacobs 
2472264Sjacobs static char *report_accepting_keys[] = { "printer-name",
2482264Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
2492264Sjacobs 			"printer-up-time", "printer-state-time",
2502264Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
2512264Sjacobs /* ARGSUSED2 */
2522264Sjacobs static int
2532264Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
2542264Sjacobs 		int verbose, int description)
2552264Sjacobs {
2562264Sjacobs 	papi_status_t status;
2572264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2582264Sjacobs 	time_t curr;
2592264Sjacobs 	char boolean = PAPI_FALSE;
2602264Sjacobs 
2612264Sjacobs 	if (name == NULL) {
2622264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2632264Sjacobs 					"printer-name", &name);
2642264Sjacobs 		if (status != PAPI_OK)
2652264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2662264Sjacobs 					"printer-uri-supported", &name);
2672264Sjacobs 	}
2682264Sjacobs 	if (name == NULL)
2692264Sjacobs 		return (-1);
2702264Sjacobs 
2712264Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
2722264Sjacobs 				"printer-is-accepting-jobs", &boolean);
2732264Sjacobs 	(void) time(&curr);
2742264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2752264Sjacobs 					"printer-up-time", &curr);
2762264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2772264Sjacobs 					"printer-state-time", &curr);
2782264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2792264Sjacobs 					"lpsched-reject-date", &curr);
2802264Sjacobs 
2812264Sjacobs 	if (boolean == PAPI_TRUE) {
2822264Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
2832264Sjacobs 			name, nctime(&curr));
2842264Sjacobs 	} else {
2852264Sjacobs 		char *reason = "unknown reason";
2862264Sjacobs 
2872264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
2882264Sjacobs 					"lpsched-reject-reason", &reason);
2892264Sjacobs 
2902264Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
2912264Sjacobs 			name, nctime(&curr), reason);
2922264Sjacobs 	}
2932264Sjacobs 
2942264Sjacobs 	return (0);
2952264Sjacobs }
2962264Sjacobs 
2972264Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
2982264Sjacobs 					"member-names", NULL };
2992264Sjacobs /* ARGSUSED2 */
3002264Sjacobs static int
3012264Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
3022264Sjacobs 		int verbose, int description)
3032264Sjacobs {
3042264Sjacobs 	papi_status_t status;
3052264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3062264Sjacobs 	char *member = NULL;
3072264Sjacobs 	void *iter = NULL;
3082264Sjacobs 
3092264Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
3102264Sjacobs 				"member-names", &member);
3112264Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
3122264Sjacobs 		return (0);
3132264Sjacobs 
3142264Sjacobs 	if (name == NULL) {
3152264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3162264Sjacobs 					"printer-name", &name);
3172264Sjacobs 		if (status != PAPI_OK)
3182264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3192264Sjacobs 					"printer-uri-supported", &name);
3202264Sjacobs 	}
3212264Sjacobs 	if (name == NULL)
3222264Sjacobs 		return (-1);
3232264Sjacobs 
3242264Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
3252264Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
3262264Sjacobs 			== PAPI_OK)
3272264Sjacobs 		printf("\t%s\n", member);
3282264Sjacobs 
3292264Sjacobs 	return (0);
3302264Sjacobs }
3312264Sjacobs 
3322264Sjacobs static char *report_printer_keys[] = { "printer-name",
3332264Sjacobs 			"printer-uri-supported", "printer-state",
3342264Sjacobs 			"printer-up-time", "printer-state-time",
3352264Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
3362264Sjacobs 			"lpsched-disable-reason", NULL };
3372264Sjacobs /* ARGSUSED2 */
3382264Sjacobs static int
3392264Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
3402264Sjacobs 		int verbose, int description)
3412264Sjacobs {
3422264Sjacobs 	papi_status_t status;
3432264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3442264Sjacobs 	time_t curr;
3452264Sjacobs 	int32_t pstat = 0;
3462264Sjacobs 	char *member = NULL;
3472264Sjacobs 
3482264Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
3492264Sjacobs 				"member-names", &member);
3502264Sjacobs 	if (status == PAPI_OK)	/* it's a class */
3512264Sjacobs 		return (0);
3522264Sjacobs 
3532264Sjacobs 	if (name == NULL) {
3542264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3552264Sjacobs 					"printer-name", &name);
3562264Sjacobs 		if (status != PAPI_OK)
3572264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3582264Sjacobs 					"printer-uri-supported", &name);
3592264Sjacobs 	}
3602264Sjacobs 	if (name == NULL)
3612264Sjacobs 		return (-1);
3622264Sjacobs 
3632264Sjacobs 	printf(gettext("printer %s "), name);
3642264Sjacobs 
3652264Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
3662264Sjacobs 					"printer-state", &pstat);
3672264Sjacobs 
3682264Sjacobs 	switch (pstat) {
3692264Sjacobs 	case 0x03:	/* idle */
3702264Sjacobs 		printf(gettext("idle. enabled"));
3712264Sjacobs 		break;
3722264Sjacobs 	case 0x04: {	/* processing */
3732264Sjacobs 		char *requested[] = { "job-id", NULL };
3742264Sjacobs 		papi_job_t *j = NULL;
3752264Sjacobs 		int32_t jobid = 0;
3762264Sjacobs 
3772264Sjacobs 		(void) papiPrinterListJobs(svc, name, requested, 0, 1, &j);
3782264Sjacobs 		if ((j != NULL) && (j[0] != NULL))
3792264Sjacobs 			jobid = papiJobGetId(j[0]);
3802264Sjacobs 		papiJobListFree(j);
3812264Sjacobs 
3822264Sjacobs 		printf(gettext("now printing %s-%d. enabled"), name, jobid);
3832264Sjacobs 		}
3842264Sjacobs 		break;
3852264Sjacobs 	case 0x05:	/* stopped */
3862264Sjacobs 		printf(gettext("disabled"));
3872264Sjacobs 		break;
3882264Sjacobs 	default:
3892264Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
3902264Sjacobs 		break;
3912264Sjacobs 	}
3922264Sjacobs 
3932264Sjacobs 	(void) time(&curr);
3942264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
3952264Sjacobs 					"printer-up-time", &curr);
3962264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
3972264Sjacobs 					"printer-state-time", &curr);
3982264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
3992264Sjacobs 					"lpsched-disable-date", &curr);
4002264Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
4012264Sjacobs 
4022264Sjacobs 	if (pstat == 0x05) {
4032264Sjacobs 		char *reason = "unknown reason";
4042264Sjacobs 
4052264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4062264Sjacobs 					"printer-state-reasons", &reason);
4072264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4082264Sjacobs 					"lpsched-disable-reason", &reason);
4092264Sjacobs 		printf(gettext("\t%s\n"), reason);
4102264Sjacobs 	}
4112264Sjacobs 
4122264Sjacobs 	if (verbose == 1) {
4132264Sjacobs 		void *iter;
4142264Sjacobs 		char *str;
4152264Sjacobs 
4162264Sjacobs 		str = "";
4172264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4182264Sjacobs 					"form-ready", &str);
4192264Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
4202264Sjacobs 
4212264Sjacobs 		str = "";
4222264Sjacobs 		iter = NULL;
4232264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4242264Sjacobs 					"document-format-supported", &str);
4252264Sjacobs 		printf(gettext("\tContent types: %s"), str);
4262264Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
4276679Swendyp 				== PAPI_OK)
4282264Sjacobs 			printf(", %s", str);
4292264Sjacobs 		printf("\n");
4302264Sjacobs 
431*8534SSonam.Gupta@Sun.COM 		/* Display the printer description */
432*8534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
4332264Sjacobs 
4342264Sjacobs 		str = "";
4356676Swendyp 		iter = NULL;
4366676Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
4376676Swendyp 		    "lpsched-printer-type", &str);
4386676Swendyp 		printf(gettext("\tPrinter types: %s"), str);
4396676Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
4406676Swendyp 		    == PAPI_OK)
4416676Swendyp 			printf(", %s", str);
4426676Swendyp 		printf("\n");
4436676Swendyp 
4446676Swendyp 		str = "";
4452264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4462264Sjacobs 					"lpsched-dial-info", &str);
4472264Sjacobs 		printf(gettext("\tConnection: %s\n"),
4487653SJonathan.Ca@Sun.COM 		    ((str[0] == '\0') ? gettext("direct") : str));
4492264Sjacobs 
4502264Sjacobs 		str = "";
4512264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4522264Sjacobs 					"lpsched-interface-script", &str);
4532264Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
4542264Sjacobs 
4552264Sjacobs 		str = NULL;
4562264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4572264Sjacobs 					"ppd-file-uri", &str);
4582264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4592264Sjacobs 					"lpsched-ppd-source-path", &str);
4602264Sjacobs 		if (str != NULL)
4612264Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
4622264Sjacobs 
4632264Sjacobs 		str = NULL;
4642264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4652264Sjacobs 					"lpsched-fault-alert-command", &str);
4662264Sjacobs 		if (str != NULL)
4672264Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
4682264Sjacobs 
4692264Sjacobs 		str = "";
4702264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4712264Sjacobs 					"lpsched-fault-recovery", &str);
4722264Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
4732264Sjacobs 			((str[0] == '\0') ? gettext("continue") : str));
4742264Sjacobs 
4752264Sjacobs 		str = "(all)";
4762264Sjacobs 		iter = NULL;
4772264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4782264Sjacobs 					"requesting-user-name-allowed", &str);
4792264Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
4802264Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
4812264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
4822264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
4832264Sjacobs 					&str) == PAPI_OK)
4842264Sjacobs 				printf("\t\t%s\n", str);
4852264Sjacobs 
4862264Sjacobs 		str = NULL;
4872264Sjacobs 		iter = NULL;
4882264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4892264Sjacobs 					"requesting-user-name-denied", &str);
4902264Sjacobs 		if (str != NULL) {
4912264Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
4922264Sjacobs 				((str[0] == '\0') ? gettext("(none)") : str));
4932264Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
4942264Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
4952264Sjacobs 						NULL, &str) == PAPI_OK)
4962264Sjacobs 					printf("\t\t%s\n", str);
4972264Sjacobs 		}
4982264Sjacobs 
4998238SSonam.Gupta@Sun.COM 		str = "none";
5002264Sjacobs 		iter = NULL;
5012264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5022264Sjacobs 					"form-supported", &str);
5038238SSonam.Gupta@Sun.COM 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
5048238SSonam.Gupta@Sun.COM 			((str[0] == '\0') ? gettext("none") : str));
5052264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5062264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5072264Sjacobs 					&str) == PAPI_OK)
5088238SSonam.Gupta@Sun.COM 				printf("\t\t(%s)\n", str);
5092264Sjacobs 
5102264Sjacobs 		str = "";
5112264Sjacobs 		iter = NULL;
5122264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5132264Sjacobs 					"media-supported", &str);
5142264Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
5152264Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
5162264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5172264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5182264Sjacobs 					&str) == PAPI_OK)
5192264Sjacobs 				printf("\t\t%s\n", str);
5202264Sjacobs 
5212264Sjacobs 		str = "";
5222264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5232264Sjacobs 					"job-sheets-supported", &str);
5246676Swendyp 		if ((strcasecmp(str, "none")) == 0)
5256676Swendyp 			str = gettext("page never printed");
5266676Swendyp 		else if (strcasecmp(str, "optional") == 0)
5276676Swendyp 			str = gettext("not required");
5286676Swendyp 		else
5296676Swendyp 			str = gettext("required");
5306676Swendyp 
5316676Swendyp 		printf(gettext("\tBanner %s\n"), str);
5326676Swendyp 
5332264Sjacobs 
5342264Sjacobs 		str = "";
5352264Sjacobs 		iter = NULL;
5362264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5372264Sjacobs 					"lpsched-print-wheels", &str);
5382264Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
5392264Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
5402264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5412264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5422264Sjacobs 					&str) == PAPI_OK)
5432264Sjacobs 				printf("\t\t%s\n", str);
5442264Sjacobs 
5452264Sjacobs 		printf(gettext("\tDefault pitch:\n"));
5462264Sjacobs 		printf(gettext("\tDefault page size:\n"));
5472264Sjacobs 		printf(gettext("\tDefault port setting:\n"));
5482264Sjacobs 
5492264Sjacobs 		str = "";
5502264Sjacobs 		iter = NULL;
5512264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5522264Sjacobs 					"lpsched-options", &str);
5532264Sjacobs 		if (str != NULL) {
5542264Sjacobs 			printf(gettext("\tOptions: %s"), str);
5552264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5562264Sjacobs 						&str) == PAPI_OK)
5572264Sjacobs 				printf(", %s", str);
5582264Sjacobs 			printf("\n");
5592264Sjacobs 		}
5602264Sjacobs 
561*8534SSonam.Gupta@Sun.COM 	} else if (description == 1)
562*8534SSonam.Gupta@Sun.COM 		/* Display printer description */
563*8534SSonam.Gupta@Sun.COM 		print_description(attrs, name);
564*8534SSonam.Gupta@Sun.COM 	else if (verbose > 1)
5652264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
5662264Sjacobs 
5672264Sjacobs 	if (verbose > 0)
5682264Sjacobs 		printf("\n");
5692264Sjacobs 
5702264Sjacobs 	return (0);
5712264Sjacobs }
5722264Sjacobs 
5732264Sjacobs static int
5742264Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
5752264Sjacobs 					int, int), papi_encryption_t encryption,
5762264Sjacobs 		int verbose, int description)
5772264Sjacobs {
5782264Sjacobs 	int result = 0;
5792264Sjacobs 	papi_status_t status;
5802264Sjacobs 	papi_service_t svc = NULL;
5812264Sjacobs 
5822264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
5832264Sjacobs 					encryption, NULL);
5842264Sjacobs 	if (status != PAPI_OK) {
5854062Sjacobs 		if (status == PAPI_NOT_FOUND)
5864062Sjacobs 			fprintf(stderr, gettext("%s: unknown printer\n"),
5874062Sjacobs 				name ? name : "(NULL)");
5884062Sjacobs 		else
5894062Sjacobs 			fprintf(stderr, gettext(
5904062Sjacobs 				"Failed to contact service for %s: %s\n"),
5914062Sjacobs 				name ? name : "(NULL)",
5924062Sjacobs 				verbose_papi_message(svc, status));
5932264Sjacobs 		papiServiceDestroy(svc);
5942264Sjacobs 		return (-1);
5952264Sjacobs 	}
5962264Sjacobs 
5972264Sjacobs 	if (name == NULL) { /* all */
5982264Sjacobs 		char **interest = interest_list(svc);
5992264Sjacobs 
6002264Sjacobs 		if (interest != NULL) {
6012264Sjacobs 			int i;
6022264Sjacobs 
6032264Sjacobs 			for (i = 0; interest[i] != NULL; i++)
6042264Sjacobs 				result += printer_query(interest[i], report,
6052264Sjacobs 							encryption, verbose,
6062264Sjacobs 							description);
6072264Sjacobs 		}
6082264Sjacobs 	} else {
6092264Sjacobs 		papi_printer_t printer = NULL;
6102264Sjacobs 		char **keys = NULL;
6112264Sjacobs 
6122264Sjacobs 		/*
6132264Sjacobs 		 * Limit the query to only required data to reduce the need
6142264Sjacobs 		 * to go remote for information.
6152264Sjacobs 		 */
6162264Sjacobs 		if (report == report_device)
6172264Sjacobs 			keys = report_device_keys;
6182264Sjacobs 		else if (report == report_class)
6192264Sjacobs 			keys = report_class_keys;
6202264Sjacobs 		else if (report == report_accepting)
6212264Sjacobs 			keys = report_accepting_keys;
6222264Sjacobs 		else if ((report == report_printer) && (verbose == 0))
6232264Sjacobs 			keys = report_printer_keys;
6242264Sjacobs 
6252264Sjacobs 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
6262264Sjacobs 		if (status != PAPI_OK) {
6272264Sjacobs 			fprintf(stderr, gettext(
6282264Sjacobs 				"Failed to get printer info for %s: %s\n"),
6292264Sjacobs 				name, verbose_papi_message(svc, status));
6302264Sjacobs 			papiServiceDestroy(svc);
6312264Sjacobs 			return (-1);
6322264Sjacobs 		}
6332264Sjacobs 
6342264Sjacobs 		if (printer != NULL)
6352264Sjacobs 			result = report(svc, name, printer, verbose,
6362264Sjacobs 					description);
6372264Sjacobs 
6382264Sjacobs 		papiPrinterFree(printer);
6392264Sjacobs 	}
6402264Sjacobs 
6412264Sjacobs 	papiServiceDestroy(svc);
6422264Sjacobs 
6432264Sjacobs 	return (result);
6442264Sjacobs }
6452264Sjacobs 
6462264Sjacobs static int
6472264Sjacobs match_user(char *user, char **list)
6482264Sjacobs {
6492264Sjacobs 	int i;
6502264Sjacobs 
6512264Sjacobs 	for (i = 0; list[i] != NULL; i++) {
6522264Sjacobs 		if (strcmp(user, list[i]) == 0)
6532264Sjacobs 			return (0);
6542264Sjacobs 	}
6552264Sjacobs 
6562264Sjacobs 	return (-1);
6572264Sjacobs }
6582264Sjacobs 
6592264Sjacobs static char **users = NULL;
6602264Sjacobs 
6612264Sjacobs static int
6622264Sjacobs report_job(papi_job_t job, int show_rank, int verbose)
6632264Sjacobs {
6642264Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
6652264Sjacobs 	time_t clock = 0;
6662264Sjacobs 	char date[24];
6672264Sjacobs 	char request[26];
6682264Sjacobs 	char *user = "unknown";
6698321SSonam.Gupta@Sun.COM 	char *host = NULL;
6702264Sjacobs 	int32_t size = 0;
6712264Sjacobs 	int32_t jstate = 0;
6728321SSonam.Gupta@Sun.COM 	char User[50];
6732264Sjacobs 
6742264Sjacobs 	char *destination = "unknown";
6752264Sjacobs 	int32_t id = -1;
6762264Sjacobs 
6772264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
6782264Sjacobs 				"job-originating-user-name", &user);
6792264Sjacobs 
6802264Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
6812264Sjacobs 		return (0);
6822264Sjacobs 
6838321SSonam.Gupta@Sun.COM 	(void) papiAttributeListGetString(attrs, NULL,
6848321SSonam.Gupta@Sun.COM 	    "job-originating-host-name", &host);
6858321SSonam.Gupta@Sun.COM 
6868321SSonam.Gupta@Sun.COM 	if (host)
6878321SSonam.Gupta@Sun.COM 		snprintf(User, sizeof (User), "%s@%s", user, host);
6888321SSonam.Gupta@Sun.COM 	else
6898321SSonam.Gupta@Sun.COM 		snprintf(User, sizeof (User), "%s", user);
6908321SSonam.Gupta@Sun.COM 
6912264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
6922264Sjacobs 	size *= 1024;	/* for the approximate byte size */
6932264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
6942264Sjacobs 
6952264Sjacobs 	(void) time(&clock);
6962264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
6972264Sjacobs 				"time-at-creation", (int32_t *)&clock);
6982264Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
6992264Sjacobs 
7002264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
7012264Sjacobs 				"job-printer-uri", &destination);
7022264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
7032264Sjacobs 				"printer-name", &destination);
7042264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
7052264Sjacobs 				"job-id", &id);
7062264Sjacobs 	snprintf(request, sizeof (request), "%s-%d", destination, id);
7072264Sjacobs 
7082264Sjacobs 	if (show_rank != 0) {
7092264Sjacobs 		int32_t rank = -1;
7102264Sjacobs 
7112264Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
7122264Sjacobs 				"number-of-intervening-jobs", &rank);
7132264Sjacobs 		rank++;
7142264Sjacobs 
7152264Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
7168321SSonam.Gupta@Sun.COM 		    rank, request, User, size, date);
7172264Sjacobs 	} else
7188321SSonam.Gupta@Sun.COM 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
7192264Sjacobs 
7202264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
7212264Sjacobs 				"job-state", &jstate);
7222264Sjacobs 	if (jstate == 0x04)
7232264Sjacobs 		printf(gettext(", being held"));
7242264Sjacobs 	else if (jstate == 0x07)
7252264Sjacobs 		printf(gettext(", cancelled"));
7262264Sjacobs 	else if (jstate == 0x09)
7272264Sjacobs 		printf(gettext(", complete"));
7282264Sjacobs 
7292264Sjacobs 	if (verbose == 1) {
7303125Sjacobs 		char *form = NULL;
7313125Sjacobs 
7322264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
7332264Sjacobs 				"output-device-assigned", &destination);
7342264Sjacobs 		printf("\n\t assigned %s", destination);
7353127Sjacobs 
7363125Sjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
7373125Sjacobs 		if (form != NULL)
7383125Sjacobs 			printf(", form %s", form);
7392264Sjacobs 	} else if (verbose > 1) {
7402264Sjacobs 		printf("\n");
7412264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
7422264Sjacobs 	}
7432264Sjacobs 
7442264Sjacobs 	printf("\n");
7452264Sjacobs 
7462264Sjacobs 	return (0);
7472264Sjacobs }
7482264Sjacobs 
7492264Sjacobs static int
7502264Sjacobs job_query(char *request, int (*report)(papi_job_t, int, int),
7512264Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
7522264Sjacobs {
7532264Sjacobs 	int result = 0;
7542264Sjacobs 	papi_status_t status;
7552264Sjacobs 	papi_service_t svc = NULL;
7568295SSonam.Gupta@Sun.COM 	char *printer = request;
7572264Sjacobs 	int32_t id = -1;
7588295SSonam.Gupta@Sun.COM 	int flag1 = 0;
7598295SSonam.Gupta@Sun.COM 	int flag = 1;
7608295SSonam.Gupta@Sun.COM 	int print_flag = 0;
7612264Sjacobs 
7628295SSonam.Gupta@Sun.COM 	do {
7638295SSonam.Gupta@Sun.COM 		status = papiServiceCreate(&svc, printer, NULL, NULL,
7648295SSonam.Gupta@Sun.COM 			    cli_auth_callback, encryption, NULL);
7652264Sjacobs 
7668295SSonam.Gupta@Sun.COM 		if ((status == PAPI_OK) && (printer != NULL))
7678295SSonam.Gupta@Sun.COM 			print_flag = 1;
7682264Sjacobs 
7698295SSonam.Gupta@Sun.COM 		/* <name>-# printer name does not exist */
7708295SSonam.Gupta@Sun.COM 		if (status != PAPI_OK) {
7718295SSonam.Gupta@Sun.COM 			/*
7728295SSonam.Gupta@Sun.COM 			 * Check if <name>-# is a request-id
7738295SSonam.Gupta@Sun.COM 			 * Once this check is done flag1 is set
7748295SSonam.Gupta@Sun.COM 			 */
7758295SSonam.Gupta@Sun.COM 			if (flag1 == 1)
7768295SSonam.Gupta@Sun.COM 				break;
7778295SSonam.Gupta@Sun.COM 
7788295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
7792264Sjacobs 
7808295SSonam.Gupta@Sun.COM 			status = papiServiceCreate(&svc, printer, NULL, NULL,
7818295SSonam.Gupta@Sun.COM 				    cli_auth_callback, encryption, NULL);
7822264Sjacobs 
7838295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
7848295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
7858295SSonam.Gupta@Sun.COM 				    "Failed to contact service for %s: %s\n"),
7868295SSonam.Gupta@Sun.COM 				    (printer ? printer : "all"),
7878295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
7888295SSonam.Gupta@Sun.COM 				return (-1);
7898295SSonam.Gupta@Sun.COM 			}
7902264Sjacobs 		}
7912264Sjacobs 
7928295SSonam.Gupta@Sun.COM 		if (printer == NULL) { /* all */
7938295SSonam.Gupta@Sun.COM 			char **interest = interest_list(svc);
7948295SSonam.Gupta@Sun.COM 
7958295SSonam.Gupta@Sun.COM 			if (interest != NULL) {
7968295SSonam.Gupta@Sun.COM 				int i;
7978295SSonam.Gupta@Sun.COM 
7988295SSonam.Gupta@Sun.COM 				for (i = 0; interest[i] != NULL; i++)
7998295SSonam.Gupta@Sun.COM 					result += job_query(interest[i], report,
8008295SSonam.Gupta@Sun.COM 						    encryption, show_rank, verbose);
8018295SSonam.Gupta@Sun.COM 			}
8028295SSonam.Gupta@Sun.COM 		} else if (id == -1) { /* a printer */
8038295SSonam.Gupta@Sun.COM 			papi_job_t *jobs = NULL;
8048295SSonam.Gupta@Sun.COM 
8058295SSonam.Gupta@Sun.COM 			status = papiPrinterListJobs(svc, printer, NULL,
8068295SSonam.Gupta@Sun.COM 				    0, 0, &jobs);
8078295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
8088295SSonam.Gupta@Sun.COM 				fprintf(stderr, gettext(
8098295SSonam.Gupta@Sun.COM 				    "Failed to get job list: %s\n"),
8108295SSonam.Gupta@Sun.COM 				    verbose_papi_message(svc, status));
8118295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
8128295SSonam.Gupta@Sun.COM 				return (-1);
8138295SSonam.Gupta@Sun.COM 			}
8148295SSonam.Gupta@Sun.COM 
8158295SSonam.Gupta@Sun.COM 			if (jobs != NULL) {
8168295SSonam.Gupta@Sun.COM 				int i;
8172264Sjacobs 
8188295SSonam.Gupta@Sun.COM 				for (i = 0; jobs[i] != NULL; i++)
8198295SSonam.Gupta@Sun.COM 					result += report(jobs[i],
8208295SSonam.Gupta@Sun.COM 						    show_rank, verbose);
8218295SSonam.Gupta@Sun.COM 			}
8228295SSonam.Gupta@Sun.COM 
8238295SSonam.Gupta@Sun.COM 			papiJobListFree(jobs);
8248295SSonam.Gupta@Sun.COM 		} else {	/* a job */
8258295SSonam.Gupta@Sun.COM 			papi_job_t job = NULL;
8268295SSonam.Gupta@Sun.COM 
8278295SSonam.Gupta@Sun.COM 			/* Once a job has been found stop processing */
8288295SSonam.Gupta@Sun.COM 			flag = 0;
8298295SSonam.Gupta@Sun.COM 
8308295SSonam.Gupta@Sun.COM 			status = papiJobQuery(svc, printer, id, NULL, &job);
8318295SSonam.Gupta@Sun.COM 			if (status != PAPI_OK) {
8328295SSonam.Gupta@Sun.COM 				if (!print_flag)
8338295SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
8348295SSonam.Gupta@Sun.COM 					    "Failed to get job info for %s: %s\n"),
8358295SSonam.Gupta@Sun.COM 					    request, verbose_papi_message(svc, status));
8368295SSonam.Gupta@Sun.COM 				papiServiceDestroy(svc);
8378295SSonam.Gupta@Sun.COM 				return (-1);
8388295SSonam.Gupta@Sun.COM 			}
8398295SSonam.Gupta@Sun.COM 
8408295SSonam.Gupta@Sun.COM 			if (job != NULL)
8418295SSonam.Gupta@Sun.COM 				result = report(job, show_rank, verbose);
8428295SSonam.Gupta@Sun.COM 
8438295SSonam.Gupta@Sun.COM 			papiJobFree(job);
8442264Sjacobs 		}
8452264Sjacobs 
8468295SSonam.Gupta@Sun.COM 		if (flag) {
8478295SSonam.Gupta@Sun.COM 			id = -1;
8488295SSonam.Gupta@Sun.COM 			get_printer_id(printer, &printer, &id);
8498295SSonam.Gupta@Sun.COM 			if (id == -1)
8508295SSonam.Gupta@Sun.COM 				flag = 0;
8518295SSonam.Gupta@Sun.COM 			else
8528295SSonam.Gupta@Sun.COM 				flag1 = 1;
8532264Sjacobs 		}
8548295SSonam.Gupta@Sun.COM 	} while (flag);
8552264Sjacobs 
8562264Sjacobs 	papiServiceDestroy(svc);
8572264Sjacobs 
8582264Sjacobs 	return (result);
8592264Sjacobs }
8602264Sjacobs 
8612264Sjacobs static int
8622264Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
8632264Sjacobs {
8642264Sjacobs 	papi_status_t status;
8652264Sjacobs 	char *form = NULL;
8662264Sjacobs 	void *iter = NULL;
8672264Sjacobs 
8682264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
8692264Sjacobs 					"form-supported", &form);
8702264Sjacobs 		status == PAPI_OK;
8712264Sjacobs 		status = papiAttributeListGetString(attrs, &iter,
8722264Sjacobs 							NULL, &form)) {
8732264Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
8742264Sjacobs 			printf(gettext("form %s is available to you\n"), form);
8752264Sjacobs 			if (verbose != 0) {
8762264Sjacobs 				char *detail = NULL;
8772264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
8782264Sjacobs 						"form-supported-detail",
8792264Sjacobs 						&detail);
8802264Sjacobs 				if (status == PAPI_OK)
8812264Sjacobs 					printf("%s\n", detail);
8822264Sjacobs 			}
8832264Sjacobs 		}
8842264Sjacobs 	}
8852264Sjacobs 
8862264Sjacobs 	return (0);
8872264Sjacobs }
8882264Sjacobs 
8892264Sjacobs static int
8902264Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
8912264Sjacobs {
8922264Sjacobs 	papi_status_t status;
8932264Sjacobs 	char *pw = NULL;
8942264Sjacobs 	void *iter = NULL;
8952264Sjacobs 
8962264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
8972264Sjacobs 					"pw-supported", &pw);
8982264Sjacobs 		status == PAPI_OK;
8992264Sjacobs 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
9002264Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
9012264Sjacobs 			printf(gettext("charset %s is available\n"), pw);
9022264Sjacobs 			if (verbose != 0) {
9032264Sjacobs 				char *info = NULL;
9042264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
9052264Sjacobs 						"pw-supported-extra", &info);
9062264Sjacobs 				if (status == PAPI_OK)
9072264Sjacobs 					printf("%s\n", info);
9082264Sjacobs 			}
9092264Sjacobs 		}
9102264Sjacobs 	}
9112264Sjacobs 
9122264Sjacobs 	return (0);
9132264Sjacobs }
9142264Sjacobs 
9152264Sjacobs static int
9162264Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
9172264Sjacobs 		papi_encryption_t encryption, int verbose)
9182264Sjacobs {
9192264Sjacobs 	int result = 0;
9202264Sjacobs 	papi_status_t status;
9212264Sjacobs 	papi_service_t svc = NULL;
9222264Sjacobs 	papi_attribute_t **attrs = NULL;
9232264Sjacobs 
9242264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
9252264Sjacobs 					encryption, NULL);
9262264Sjacobs 	if (status != PAPI_OK) {
9272264Sjacobs 		papiServiceDestroy(svc);
9282264Sjacobs 		return (-1);
9292264Sjacobs 	}
9302264Sjacobs 
9312264Sjacobs 	attrs = papiServiceGetAttributeList(svc);
9322264Sjacobs 	if (attrs != NULL) {
9332264Sjacobs 		result = report(name, attrs, verbose);
9342264Sjacobs 
9352264Sjacobs 		if (verbose > 1) {
9362264Sjacobs 			printf("\n");
9372264Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
9382264Sjacobs 			printf("\n");
9392264Sjacobs 		}
9402264Sjacobs 	}
9412264Sjacobs 
9422264Sjacobs 	papiServiceDestroy(svc);
9432264Sjacobs 
9442264Sjacobs 	return (result);
9452264Sjacobs }
9462264Sjacobs 
9472264Sjacobs int
9482264Sjacobs main(int ac, char *av[])
9492264Sjacobs {
9502264Sjacobs 	int exit_code = 0;
9512264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
9522264Sjacobs 	int rank = 0;
9532264Sjacobs 	int verbose = 0;
9542264Sjacobs 	int description = 0;
9552264Sjacobs 	int c;
9562264Sjacobs 	char **argv;
9572264Sjacobs 
9582264Sjacobs 	(void) setlocale(LC_ALL, "");
9592264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
9602264Sjacobs 
9612264Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
9624286Swendyp 	for (c = 0; c < ac; c++) {
9634286Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
9644286Swendyp 			(isalpha(av[c][2]) != 0)) {
9654286Swendyp 			/* preserve old "-l[po...]" behavior */
9664286Swendyp 			argv[c] = &av[c][1];
9674286Swendyp 			argv[c][0] = '-';
9684286Swendyp 			verbose = 1;
9694286Swendyp 
9704286Swendyp 		} else
9714286Swendyp 			argv[c] = av[c];
9724286Swendyp 	}
9734286Swendyp 
9742264Sjacobs 	argv[c++] = "--";
9752264Sjacobs 	ac = c;
9762264Sjacobs 
9772264Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
9782264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
9792264Sjacobs 		switch (c) {
9802264Sjacobs 		case 'l':
9812264Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
9822264Sjacobs 				optarg = "1";
9832264Sjacobs 			verbose = atoi(optarg);
9842264Sjacobs 			break;
9852264Sjacobs 		case 'D':
9862264Sjacobs 			description = 1;
9872264Sjacobs 			break;
9882264Sjacobs 		case 'R':
9892264Sjacobs 			rank = 1;
9902264Sjacobs 			break;
9912264Sjacobs 		case 'E':
9922264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
9932264Sjacobs 			break;
9942264Sjacobs 		default:
9952264Sjacobs 			break;
9962264Sjacobs 		}
9972264Sjacobs 	optind = 1;
9982264Sjacobs 
9992264Sjacobs 	/* process command line arguments */
10002264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
10012264Sjacobs 		switch (c) {	/* these may or may not have an option */
10022264Sjacobs 		case 'a':
10032264Sjacobs 		case 'c':
10042264Sjacobs 		case 'p':
10052264Sjacobs 		case 'o':
10062264Sjacobs 		case 'R':
10072264Sjacobs 		case 'u':
10082264Sjacobs 		case 'v':
10092264Sjacobs 		case 'l':
10102264Sjacobs 		case 'f':
10112264Sjacobs 		case 'S':
10122264Sjacobs 			if (optarg[0] == '-') {
10132264Sjacobs 				/* this check stop a possible infinite loop */
10142264Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
10152264Sjacobs 					optind--;
10162264Sjacobs 				optarg = NULL;
10172264Sjacobs 			} else if (strcmp(optarg, "all") == 0)
10182264Sjacobs 				optarg = NULL;
10192264Sjacobs 		}
10202264Sjacobs 
10212264Sjacobs 		switch (c) {
10222264Sjacobs 		case 'a':
10232264Sjacobs 			exit_code += printer_query(optarg, report_accepting,
10242264Sjacobs 						encryption, verbose, 0);
10252264Sjacobs 			break;
10262264Sjacobs 		case 'c':
10272264Sjacobs 			exit_code += printer_query(optarg, report_class,
10282264Sjacobs 						encryption, verbose, 0);
10292264Sjacobs 			break;
10302264Sjacobs 		case 'p':
10312264Sjacobs 			exit_code += printer_query(optarg, report_printer,
10322264Sjacobs 						encryption, verbose,
10332264Sjacobs 						description);
10342264Sjacobs 			break;
10352264Sjacobs 		case 'd':
10362264Sjacobs 			exit_code += lpstat_default_printer(encryption);
10372264Sjacobs 			break;
10382264Sjacobs 		case 'r':
10392264Sjacobs 			exit_code += lpstat_service_status(encryption);
10402264Sjacobs 			break;
10412264Sjacobs 		case 'u':
10422264Sjacobs 			if (optarg != NULL)
10432264Sjacobs 				users = strsplit(optarg, ", \n");
10442264Sjacobs 			exit_code += job_query(NULL, report_job,
10452264Sjacobs 						encryption, rank, verbose);
10462264Sjacobs 			if (users != NULL) {
10472264Sjacobs 				free(users);
10482264Sjacobs 				users = NULL;
10492264Sjacobs 			}
10502264Sjacobs 			break;
10512264Sjacobs 		case 'v':
10522264Sjacobs 			exit_code += printer_query(optarg, report_device,
10532264Sjacobs 						encryption, verbose, 0);
10542264Sjacobs 			break;
10552264Sjacobs 		case 'o':
10562264Sjacobs 			exit_code += job_query(optarg, report_job,
10572264Sjacobs 						encryption, rank, verbose);
10582264Sjacobs 			break;
10592264Sjacobs 		case 'f':
10602264Sjacobs 			exit_code += service_query(optarg, report_form,
10612264Sjacobs 						encryption, verbose);
10622264Sjacobs 			break;
10632264Sjacobs 		case 'S':
10642264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
10652264Sjacobs 						encryption, verbose);
10662264Sjacobs 			break;
10672264Sjacobs 		case 's':
10682264Sjacobs 			exit_code += lpstat_service_status(encryption);
10692264Sjacobs 			exit_code += lpstat_default_printer(encryption);
10702264Sjacobs 			exit_code += printer_query(NULL, report_class,
10712264Sjacobs 						encryption, verbose, 0);
10722264Sjacobs 			exit_code += printer_query(NULL, report_device,
10732264Sjacobs 						encryption, verbose, 0);
10742264Sjacobs 			exit_code += service_query(optarg, report_form,
10752264Sjacobs 						encryption, verbose);
10762264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
10772264Sjacobs 						encryption, verbose);
10782264Sjacobs 			break;
10792264Sjacobs 		case 't':
10802264Sjacobs 			exit_code += lpstat_service_status(encryption);
10812264Sjacobs 			exit_code += lpstat_default_printer(encryption);
10822264Sjacobs 			exit_code += printer_query(NULL, report_class,
10832264Sjacobs 						encryption, verbose, 0);
10842264Sjacobs 			exit_code += printer_query(NULL, report_device,
10852264Sjacobs 						encryption, verbose, 0);
10862264Sjacobs 			exit_code += printer_query(NULL, report_accepting,
10872264Sjacobs 						encryption, verbose, 0);
10882264Sjacobs 			exit_code += printer_query(NULL, report_printer,
10892264Sjacobs 						encryption, verbose, 0);
10902264Sjacobs 			exit_code += service_query(optarg, report_form,
10912264Sjacobs 						encryption, verbose);
10922264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
10932264Sjacobs 						encryption, verbose);
10942264Sjacobs 			exit_code += job_query(NULL, report_job,
10952264Sjacobs 						encryption, rank, verbose);
10962264Sjacobs 			break;
10972264Sjacobs 		case 'L':	/* local-only, ignored */
10982264Sjacobs 		case 'l':	/* increased verbose level in first pass */
10992264Sjacobs 		case 'D':	/* set "description" flag in first pass */
11002264Sjacobs 		case 'R':	/* set "rank" flag in first pass */
11012264Sjacobs 		case 'E':	/* set encryption in the first pass */
11022264Sjacobs 			break;
11032264Sjacobs 		default:
11042264Sjacobs 			usage(av[0]);
11052264Sjacobs 		}
11062264Sjacobs 	}
11072264Sjacobs 	ac--;
11082264Sjacobs 
11092264Sjacobs 	if (ac == 1) {	/* report on my jobs */
11102264Sjacobs 		struct passwd *pw = getpwuid(getuid());
11112264Sjacobs 
11122264Sjacobs 		if (pw != NULL)
11132264Sjacobs 			users = strsplit(pw->pw_name, "");
11142264Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
11152264Sjacobs 					rank, verbose);
11162264Sjacobs 		if (users != NULL) {
11172264Sjacobs 			free(users);
11182264Sjacobs 			users = NULL;
11192264Sjacobs 		}
11202264Sjacobs 	} else {
11212264Sjacobs 		for (c = optind; c < ac; c++)
11222264Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
11232264Sjacobs 					rank, verbose);
11242264Sjacobs 	}
11252264Sjacobs 
11262264Sjacobs 
11272264Sjacobs 	if (exit_code != 0)
11282264Sjacobs 		exit_code = 1;
11292264Sjacobs 
11302264Sjacobs 	return (exit_code);
11312264Sjacobs }
1132