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*6676Swendyp  * Copyright 2008 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
312264Sjacobs 
322264Sjacobs #include <stdio.h>
332264Sjacobs #include <stdlib.h>
342264Sjacobs #include <unistd.h>
352264Sjacobs #include <string.h>
362264Sjacobs #include <locale.h>
372264Sjacobs #include <libintl.h>
384286Swendyp #include <ctype.h>
392264Sjacobs #include <pwd.h>
402264Sjacobs #include <papi.h>
412264Sjacobs #include <uri.h>
422264Sjacobs #include "common.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]] "
552264Sjacobs 		"[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
562264Sjacobs 		"[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
572264Sjacobs 		"[-f [list] [-l]] [-u list]\n"),
582264Sjacobs 		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,
812264Sjacobs 				"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,
952264Sjacobs 					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,
1272264Sjacobs 					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,
1512264Sjacobs 					"device-uri", &result);
1522264Sjacobs 		if (result != NULL)
1532264Sjacobs 			result = strdup(result);
1542264Sjacobs 
1552264Sjacobs 		papiPrinterFree(p);
1562264Sjacobs 	}
1572264Sjacobs 
1582264Sjacobs 	return (result);
1592264Sjacobs }
1602264Sjacobs 
1612264Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
1622264Sjacobs 					NULL };
1632264Sjacobs /* ARGSUSED2 */
1642264Sjacobs static int
1652264Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
1662264Sjacobs 		int verbose, int description)
1672264Sjacobs {
1682264Sjacobs 	papi_status_t status;
1692264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
1702264Sjacobs 	char *uri = NULL;
1712264Sjacobs 	char *device = NULL;
1722264Sjacobs 	uri_t *u = NULL;
1732264Sjacobs 
1742264Sjacobs 	if (name == NULL) {
1752264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
1762264Sjacobs 					"printer-name", &name);
1772264Sjacobs 		if (status != PAPI_OK)
1782264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
1792264Sjacobs 					"printer-uri-supported", &name);
1802264Sjacobs 	}
1812264Sjacobs 
1822264Sjacobs 	if (name == NULL)
1832264Sjacobs 		return (-1);
1842264Sjacobs 
1852264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
1862264Sjacobs 					"printer-uri-supported", &uri);
1872264Sjacobs 
1882264Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
1892264Sjacobs 		char *nodename = localhostname();
1902264Sjacobs 
1912264Sjacobs 		if ((u->host == NULL) ||
1922264Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
1932264Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
1942264Sjacobs 			device = get_device_uri(svc, name);
1952264Sjacobs 
1962264Sjacobs 		if (device != NULL) {
1972264Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
1982264Sjacobs 			return (0);
1992264Sjacobs 		} else if (uri != NULL) {
2002264Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
2012264Sjacobs 				u->host, uri);
2022264Sjacobs 			return (0);
2032264Sjacobs 		}
2042264Sjacobs 
2052264Sjacobs 		uri_free(u);
2062264Sjacobs 	}
2072264Sjacobs 
2082264Sjacobs 	return (0);
2092264Sjacobs }
2102264Sjacobs 
2112264Sjacobs static char *report_accepting_keys[] = { "printer-name",
2122264Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
2132264Sjacobs 			"printer-up-time", "printer-state-time",
2142264Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
2152264Sjacobs /* ARGSUSED2 */
2162264Sjacobs static int
2172264Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
2182264Sjacobs 		int verbose, int description)
2192264Sjacobs {
2202264Sjacobs 	papi_status_t status;
2212264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2222264Sjacobs 	time_t curr;
2232264Sjacobs 	char boolean = PAPI_FALSE;
2242264Sjacobs 
2252264Sjacobs 	if (name == NULL) {
2262264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2272264Sjacobs 					"printer-name", &name);
2282264Sjacobs 		if (status != PAPI_OK)
2292264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2302264Sjacobs 					"printer-uri-supported", &name);
2312264Sjacobs 	}
2322264Sjacobs 	if (name == NULL)
2332264Sjacobs 		return (-1);
2342264Sjacobs 
2352264Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
2362264Sjacobs 				"printer-is-accepting-jobs", &boolean);
2372264Sjacobs 	(void) time(&curr);
2382264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2392264Sjacobs 					"printer-up-time", &curr);
2402264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2412264Sjacobs 					"printer-state-time", &curr);
2422264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
2432264Sjacobs 					"lpsched-reject-date", &curr);
2442264Sjacobs 
2452264Sjacobs 	if (boolean == PAPI_TRUE) {
2462264Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
2472264Sjacobs 			name, nctime(&curr));
2482264Sjacobs 	} else {
2492264Sjacobs 		char *reason = "unknown reason";
2502264Sjacobs 
2512264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
2522264Sjacobs 					"lpsched-reject-reason", &reason);
2532264Sjacobs 
2542264Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
2552264Sjacobs 			name, nctime(&curr), reason);
2562264Sjacobs 	}
2572264Sjacobs 
2582264Sjacobs 	return (0);
2592264Sjacobs }
2602264Sjacobs 
2612264Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
2622264Sjacobs 					"member-names", NULL };
2632264Sjacobs /* ARGSUSED2 */
2642264Sjacobs static int
2652264Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
2662264Sjacobs 		int verbose, int description)
2672264Sjacobs {
2682264Sjacobs 	papi_status_t status;
2692264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
2702264Sjacobs 	char *member = NULL;
2712264Sjacobs 	void *iter = NULL;
2722264Sjacobs 
2732264Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
2742264Sjacobs 				"member-names", &member);
2752264Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
2762264Sjacobs 		return (0);
2772264Sjacobs 
2782264Sjacobs 	if (name == NULL) {
2792264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
2802264Sjacobs 					"printer-name", &name);
2812264Sjacobs 		if (status != PAPI_OK)
2822264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
2832264Sjacobs 					"printer-uri-supported", &name);
2842264Sjacobs 	}
2852264Sjacobs 	if (name == NULL)
2862264Sjacobs 		return (-1);
2872264Sjacobs 
2882264Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
2892264Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
2902264Sjacobs 			== PAPI_OK)
2912264Sjacobs 		printf("\t%s\n", member);
2922264Sjacobs 
2932264Sjacobs 	return (0);
2942264Sjacobs }
2952264Sjacobs 
2962264Sjacobs static char *report_printer_keys[] = { "printer-name",
2972264Sjacobs 			"printer-uri-supported", "printer-state",
2982264Sjacobs 			"printer-up-time", "printer-state-time",
2992264Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
3002264Sjacobs 			"lpsched-disable-reason", NULL };
3012264Sjacobs /* ARGSUSED2 */
3022264Sjacobs static int
3032264Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
3042264Sjacobs 		int verbose, int description)
3052264Sjacobs {
3062264Sjacobs 	papi_status_t status;
3072264Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
3082264Sjacobs 	time_t curr;
3092264Sjacobs 	int32_t pstat = 0;
3102264Sjacobs 	char *member = NULL;
3112264Sjacobs 
3122264Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
3132264Sjacobs 				"member-names", &member);
3142264Sjacobs 	if (status == PAPI_OK)	/* it's a class */
3152264Sjacobs 		return (0);
3162264Sjacobs 
3172264Sjacobs 	if (name == NULL) {
3182264Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
3192264Sjacobs 					"printer-name", &name);
3202264Sjacobs 		if (status != PAPI_OK)
3212264Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
3222264Sjacobs 					"printer-uri-supported", &name);
3232264Sjacobs 	}
3242264Sjacobs 	if (name == NULL)
3252264Sjacobs 		return (-1);
3262264Sjacobs 
3272264Sjacobs 	printf(gettext("printer %s "), name);
3282264Sjacobs 
3292264Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
3302264Sjacobs 					"printer-state", &pstat);
3312264Sjacobs 
3322264Sjacobs 	switch (pstat) {
3332264Sjacobs 	case 0x03:	/* idle */
3342264Sjacobs 		printf(gettext("idle. enabled"));
3352264Sjacobs 		break;
3362264Sjacobs 	case 0x04: {	/* processing */
3372264Sjacobs 		char *requested[] = { "job-id", NULL };
3382264Sjacobs 		papi_job_t *j = NULL;
3392264Sjacobs 		int32_t jobid = 0;
3402264Sjacobs 
3412264Sjacobs 		(void) papiPrinterListJobs(svc, name, requested, 0, 1, &j);
3422264Sjacobs 		if ((j != NULL) && (j[0] != NULL))
3432264Sjacobs 			jobid = papiJobGetId(j[0]);
3442264Sjacobs 		papiJobListFree(j);
3452264Sjacobs 
3462264Sjacobs 		printf(gettext("now printing %s-%d. enabled"), name, jobid);
3472264Sjacobs 		}
3482264Sjacobs 		break;
3492264Sjacobs 	case 0x05:	/* stopped */
3502264Sjacobs 		printf(gettext("disabled"));
3512264Sjacobs 		break;
3522264Sjacobs 	default:
3532264Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
3542264Sjacobs 		break;
3552264Sjacobs 	}
3562264Sjacobs 
3572264Sjacobs 	(void) time(&curr);
3582264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
3592264Sjacobs 					"printer-up-time", &curr);
3602264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
3612264Sjacobs 					"printer-state-time", &curr);
3622264Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
3632264Sjacobs 					"lpsched-disable-date", &curr);
3642264Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
3652264Sjacobs 
3662264Sjacobs 	if (pstat == 0x05) {
3672264Sjacobs 		char *reason = "unknown reason";
3682264Sjacobs 
3692264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
3702264Sjacobs 					"printer-state-reasons", &reason);
3712264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
3722264Sjacobs 					"lpsched-disable-reason", &reason);
3732264Sjacobs 		printf(gettext("\t%s\n"), reason);
3742264Sjacobs 	}
3752264Sjacobs 
3762264Sjacobs 	if (verbose == 1) {
3772264Sjacobs 		void *iter;
3782264Sjacobs 		char *str;
3792264Sjacobs 
3802264Sjacobs 		str = "";
3812264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
3822264Sjacobs 					"form-ready", &str);
3832264Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
3842264Sjacobs 
3852264Sjacobs 		str = "";
3862264Sjacobs 		iter = NULL;
3872264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
3882264Sjacobs 					"document-format-supported", &str);
3892264Sjacobs 		printf(gettext("\tContent types: %s"), str);
3902264Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
391*6676Swendyp 				== PAPI_OK)
3922264Sjacobs 			printf(", %s", str);
3932264Sjacobs 		printf("\n");
3942264Sjacobs 
3952264Sjacobs 		str = "";
3962264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
3972264Sjacobs 					"printer-info", &str);
3982264Sjacobs 		printf(gettext("\tDescription: %s\n"), str);
3992264Sjacobs 
4002264Sjacobs 		str = "";
401*6676Swendyp 		iter = NULL;
402*6676Swendyp 		(void) papiAttributeListGetString(attrs, &iter,
403*6676Swendyp 		    "lpsched-printer-type", &str);
404*6676Swendyp 		printf(gettext("\tPrinter types: %s"), str);
405*6676Swendyp 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
406*6676Swendyp 		    == PAPI_OK)
407*6676Swendyp 			printf(", %s", str);
408*6676Swendyp 		printf("\n");
409*6676Swendyp 
410*6676Swendyp 		str = "";
4112264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4122264Sjacobs 					"lpsched-dial-info", &str);
4132264Sjacobs 		printf(gettext("\tConnection: %s\n"),
4142264Sjacobs 			((str[0] != '\0') ? gettext("direct") : str));
4152264Sjacobs 
4162264Sjacobs 		str = "";
4172264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4182264Sjacobs 					"lpsched-interface-script", &str);
4192264Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
4202264Sjacobs 
4212264Sjacobs 		str = NULL;
4222264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4232264Sjacobs 					"ppd-file-uri", &str);
4242264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4252264Sjacobs 					"lpsched-ppd-source-path", &str);
4262264Sjacobs 		if (str != NULL)
4272264Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
4282264Sjacobs 
4292264Sjacobs 		str = NULL;
4302264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4312264Sjacobs 					"lpsched-fault-alert-command", &str);
4322264Sjacobs 		if (str != NULL)
4332264Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
4342264Sjacobs 
4352264Sjacobs 		str = "";
4362264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4372264Sjacobs 					"lpsched-fault-recovery", &str);
4382264Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
4392264Sjacobs 			((str[0] == '\0') ? gettext("continue") : str));
4402264Sjacobs 
4412264Sjacobs 		str = "(all)";
4422264Sjacobs 		iter = NULL;
4432264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4442264Sjacobs 					"requesting-user-name-allowed", &str);
4452264Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
4462264Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
4472264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
4482264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
4492264Sjacobs 					&str) == PAPI_OK)
4502264Sjacobs 				printf("\t\t%s\n", str);
4512264Sjacobs 
4522264Sjacobs 		str = NULL;
4532264Sjacobs 		iter = NULL;
4542264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4552264Sjacobs 					"requesting-user-name-denied", &str);
4562264Sjacobs 		if (str != NULL) {
4572264Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
4582264Sjacobs 				((str[0] == '\0') ? gettext("(none)") : str));
4592264Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
4602264Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
4612264Sjacobs 						NULL, &str) == PAPI_OK)
4622264Sjacobs 					printf("\t\t%s\n", str);
4632264Sjacobs 		}
4642264Sjacobs 
4652264Sjacobs 		str = "(none)";
4662264Sjacobs 		iter = NULL;
4672264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4682264Sjacobs 					"form-supported", &str);
4692264Sjacobs 		printf(gettext("\tForms allowed:\n\t\t%s\n"),
4702264Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
4712264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
4722264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
4732264Sjacobs 					&str) == PAPI_OK)
4742264Sjacobs 				printf("\t\t%s\n", str);
4752264Sjacobs 
4762264Sjacobs 		str = "";
4772264Sjacobs 		iter = NULL;
4782264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
4792264Sjacobs 					"media-supported", &str);
4802264Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
4812264Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
4822264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
4832264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
4842264Sjacobs 					&str) == PAPI_OK)
4852264Sjacobs 				printf("\t\t%s\n", str);
4862264Sjacobs 
4872264Sjacobs 		str = "";
4882264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
4892264Sjacobs 					"job-sheets-supported", &str);
490*6676Swendyp 		if ((strcasecmp(str, "none")) == 0)
491*6676Swendyp 			str = gettext("page never printed");
492*6676Swendyp 		else if (strcasecmp(str, "optional") == 0)
493*6676Swendyp 			str = gettext("not required");
494*6676Swendyp 		else
495*6676Swendyp 			str = gettext("required");
496*6676Swendyp 
497*6676Swendyp 		printf(gettext("\tBanner %s\n"), str);
498*6676Swendyp 
4992264Sjacobs 
5002264Sjacobs 		str = "";
5012264Sjacobs 		iter = NULL;
5022264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5032264Sjacobs 					"lpsched-print-wheels", &str);
5042264Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
5052264Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
5062264Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
5072264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5082264Sjacobs 					&str) == PAPI_OK)
5092264Sjacobs 				printf("\t\t%s\n", str);
5102264Sjacobs 
5112264Sjacobs 		printf(gettext("\tDefault pitch:\n"));
5122264Sjacobs 		printf(gettext("\tDefault page size:\n"));
5132264Sjacobs 		printf(gettext("\tDefault port setting:\n"));
5142264Sjacobs 
5152264Sjacobs 		str = "";
5162264Sjacobs 		iter = NULL;
5172264Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
5182264Sjacobs 					"lpsched-options", &str);
5192264Sjacobs 		if (str != NULL) {
5202264Sjacobs 			printf(gettext("\tOptions: %s"), str);
5212264Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
5222264Sjacobs 						&str) == PAPI_OK)
5232264Sjacobs 				printf(", %s", str);
5242264Sjacobs 			printf("\n");
5252264Sjacobs 		}
5262264Sjacobs 
5272264Sjacobs 	} else if (description == 1) {
5282264Sjacobs 		char *str = "";
5292264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
5302264Sjacobs 					"printer-description", &str);
5312264Sjacobs 		printf(gettext("\tDescription: %s\n"), str);
5322264Sjacobs 	} else if (verbose > 1)
5332264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
5342264Sjacobs 
5352264Sjacobs 	if (verbose > 0)
5362264Sjacobs 		printf("\n");
5372264Sjacobs 
5382264Sjacobs 	return (0);
5392264Sjacobs }
5402264Sjacobs 
5412264Sjacobs static int
5422264Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
5432264Sjacobs 					int, int), papi_encryption_t encryption,
5442264Sjacobs 		int verbose, int description)
5452264Sjacobs {
5462264Sjacobs 	int result = 0;
5472264Sjacobs 	papi_status_t status;
5482264Sjacobs 	papi_service_t svc = NULL;
5492264Sjacobs 
5502264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
5512264Sjacobs 					encryption, NULL);
5522264Sjacobs 	if (status != PAPI_OK) {
5534062Sjacobs 		if (status == PAPI_NOT_FOUND)
5544062Sjacobs 			fprintf(stderr, gettext("%s: unknown printer\n"),
5554062Sjacobs 				name ? name : "(NULL)");
5564062Sjacobs 		else
5574062Sjacobs 			fprintf(stderr, gettext(
5584062Sjacobs 				"Failed to contact service for %s: %s\n"),
5594062Sjacobs 				name ? name : "(NULL)",
5604062Sjacobs 				verbose_papi_message(svc, status));
5612264Sjacobs 		papiServiceDestroy(svc);
5622264Sjacobs 		return (-1);
5632264Sjacobs 	}
5642264Sjacobs 
5652264Sjacobs 	if (name == NULL) { /* all */
5662264Sjacobs 		char **interest = interest_list(svc);
5672264Sjacobs 
5682264Sjacobs 		if (interest != NULL) {
5692264Sjacobs 			int i;
5702264Sjacobs 
5712264Sjacobs 			for (i = 0; interest[i] != NULL; i++)
5722264Sjacobs 				result += printer_query(interest[i], report,
5732264Sjacobs 							encryption, verbose,
5742264Sjacobs 							description);
5752264Sjacobs 		}
5762264Sjacobs 	} else {
5772264Sjacobs 		papi_printer_t printer = NULL;
5782264Sjacobs 		char **keys = NULL;
5792264Sjacobs 
5802264Sjacobs 		/*
5812264Sjacobs 		 * Limit the query to only required data to reduce the need
5822264Sjacobs 		 * to go remote for information.
5832264Sjacobs 		 */
5842264Sjacobs 		if (report == report_device)
5852264Sjacobs 			keys = report_device_keys;
5862264Sjacobs 		else if (report == report_class)
5872264Sjacobs 			keys = report_class_keys;
5882264Sjacobs 		else if (report == report_accepting)
5892264Sjacobs 			keys = report_accepting_keys;
5902264Sjacobs 		else if ((report == report_printer) && (verbose == 0))
5912264Sjacobs 			keys = report_printer_keys;
5922264Sjacobs 
5932264Sjacobs 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
5942264Sjacobs 		if (status != PAPI_OK) {
5952264Sjacobs 			fprintf(stderr, gettext(
5962264Sjacobs 				"Failed to get printer info for %s: %s\n"),
5972264Sjacobs 				name, verbose_papi_message(svc, status));
5982264Sjacobs 			papiServiceDestroy(svc);
5992264Sjacobs 			return (-1);
6002264Sjacobs 		}
6012264Sjacobs 
6022264Sjacobs 		if (printer != NULL)
6032264Sjacobs 			result = report(svc, name, printer, verbose,
6042264Sjacobs 					description);
6052264Sjacobs 
6062264Sjacobs 		papiPrinterFree(printer);
6072264Sjacobs 	}
6082264Sjacobs 
6092264Sjacobs 	papiServiceDestroy(svc);
6102264Sjacobs 
6112264Sjacobs 	return (result);
6122264Sjacobs }
6132264Sjacobs 
6142264Sjacobs static int
6152264Sjacobs match_user(char *user, char **list)
6162264Sjacobs {
6172264Sjacobs 	int i;
6182264Sjacobs 
6192264Sjacobs 	for (i = 0; list[i] != NULL; i++) {
6202264Sjacobs 		if (strcmp(user, list[i]) == 0)
6212264Sjacobs 			return (0);
6222264Sjacobs 	}
6232264Sjacobs 
6242264Sjacobs 	return (-1);
6252264Sjacobs }
6262264Sjacobs 
6272264Sjacobs static char **users = NULL;
6282264Sjacobs 
6292264Sjacobs static int
6302264Sjacobs report_job(papi_job_t job, int show_rank, int verbose)
6312264Sjacobs {
6322264Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
6332264Sjacobs 	time_t clock = 0;
6342264Sjacobs 	char date[24];
6352264Sjacobs 	char request[26];
6362264Sjacobs 	char *user = "unknown";
6372264Sjacobs 	int32_t size = 0;
6382264Sjacobs 	int32_t jstate = 0;
6392264Sjacobs 
6402264Sjacobs 	char *destination = "unknown";
6412264Sjacobs 	int32_t id = -1;
6422264Sjacobs 
6432264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
6442264Sjacobs 				"job-originating-user-name", &user);
6452264Sjacobs 
6462264Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
6472264Sjacobs 		return (0);
6482264Sjacobs 
6492264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
6502264Sjacobs 	size *= 1024;	/* for the approximate byte size */
6512264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
6522264Sjacobs 
6532264Sjacobs 	(void) time(&clock);
6542264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
6552264Sjacobs 				"time-at-creation", (int32_t *)&clock);
6562264Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
6572264Sjacobs 
6582264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
6592264Sjacobs 				"job-printer-uri", &destination);
6602264Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
6612264Sjacobs 				"printer-name", &destination);
6622264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
6632264Sjacobs 				"job-id", &id);
6642264Sjacobs 	snprintf(request, sizeof (request), "%s-%d", destination, id);
6652264Sjacobs 
6662264Sjacobs 	if (show_rank != 0) {
6672264Sjacobs 		int32_t rank = -1;
6682264Sjacobs 
6692264Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
6702264Sjacobs 				"number-of-intervening-jobs", &rank);
6712264Sjacobs 		rank++;
6722264Sjacobs 
6732264Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
6742264Sjacobs 			rank, request, user, size, date);
6752264Sjacobs 	} else
6762264Sjacobs 		printf("%-23s %-14s %7ld   %s", request, user, size, date);
6772264Sjacobs 
6782264Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
6792264Sjacobs 				"job-state", &jstate);
6802264Sjacobs 	if (jstate == 0x04)
6812264Sjacobs 		printf(gettext(", being held"));
6822264Sjacobs 	else if (jstate == 0x07)
6832264Sjacobs 		printf(gettext(", cancelled"));
6842264Sjacobs 	else if (jstate == 0x09)
6852264Sjacobs 		printf(gettext(", complete"));
6862264Sjacobs 
6872264Sjacobs 	if (verbose == 1) {
6883125Sjacobs 		char *form = NULL;
6893125Sjacobs 
6902264Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
6912264Sjacobs 				"output-device-assigned", &destination);
6922264Sjacobs 		printf("\n\t assigned %s", destination);
6933127Sjacobs 
6943125Sjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
6953125Sjacobs 		if (form != NULL)
6963125Sjacobs 			printf(", form %s", form);
6972264Sjacobs 	} else if (verbose > 1) {
6982264Sjacobs 		printf("\n");
6992264Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
7002264Sjacobs 	}
7012264Sjacobs 
7022264Sjacobs 	printf("\n");
7032264Sjacobs 
7042264Sjacobs 	return (0);
7052264Sjacobs }
7062264Sjacobs 
7072264Sjacobs static int
7082264Sjacobs job_query(char *request, int (*report)(papi_job_t, int, int),
7092264Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
7102264Sjacobs {
7112264Sjacobs 	int result = 0;
7122264Sjacobs 	papi_status_t status;
7132264Sjacobs 	papi_service_t svc = NULL;
7142264Sjacobs 	char *printer = NULL;
7152264Sjacobs 	int32_t id = -1;
7162264Sjacobs 
7172264Sjacobs 	get_printer_id(request, &printer, &id);
7182264Sjacobs 
7192264Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
7202264Sjacobs 					encryption, NULL);
7212264Sjacobs 	if (status != PAPI_OK) {
7222264Sjacobs 		fprintf(stderr, gettext(
7232264Sjacobs 			"Failed to contact service for %s: %s\n"),
7242264Sjacobs 			(printer ? printer : "all"),
7252264Sjacobs 			verbose_papi_message(svc, status));
7262264Sjacobs 		return (-1);
7272264Sjacobs 	}
7282264Sjacobs 
7292264Sjacobs 	if (printer == NULL) { /* all */
7302264Sjacobs 		char **interest = interest_list(svc);
7312264Sjacobs 
7322264Sjacobs 		if (interest != NULL) {
7332264Sjacobs 			int i;
7342264Sjacobs 
7352264Sjacobs 			for (i = 0; interest[i] != NULL; i++)
7362264Sjacobs 				result += job_query(interest[i], report,
7372264Sjacobs 						encryption, show_rank, verbose);
7382264Sjacobs 		}
7392264Sjacobs 	} else if (id == -1) { /* a printer */
7402264Sjacobs 		papi_job_t *jobs = NULL;
7412264Sjacobs 
7422264Sjacobs 		status = papiPrinterListJobs(svc, printer, NULL, 0, 0, &jobs);
7432264Sjacobs 		if (status != PAPI_OK) {
7442264Sjacobs 			fprintf(stderr, gettext(
7452264Sjacobs 				"Failed to get job list: %s\n"),
7462264Sjacobs 				verbose_papi_message(svc, status));
7472264Sjacobs 			papiServiceDestroy(svc);
7482264Sjacobs 			return (-1);
7492264Sjacobs 		}
7502264Sjacobs 
7512264Sjacobs 		if (jobs != NULL) {
7522264Sjacobs 			int i;
7532264Sjacobs 
7542264Sjacobs 			for (i = 0; jobs[i] != NULL; i++)
7552264Sjacobs 				result += report(jobs[i], show_rank, verbose);
7562264Sjacobs 		}
7572264Sjacobs 
7582264Sjacobs 		papiJobListFree(jobs);
7592264Sjacobs 	} else {	/* a job */
7602264Sjacobs 		papi_job_t job = NULL;
7612264Sjacobs 
7622264Sjacobs 		status = papiJobQuery(svc, printer, id, NULL, &job);
7632264Sjacobs 		if (status != PAPI_OK) {
7642264Sjacobs 			fprintf(stderr, gettext(
7652264Sjacobs 				"Failed to get job info for %s: %s\n"),
7662264Sjacobs 				request, verbose_papi_message(svc, status));
7672264Sjacobs 			papiServiceDestroy(svc);
7682264Sjacobs 			return (-1);
7692264Sjacobs 		}
7702264Sjacobs 
7712264Sjacobs 		if (job != NULL)
7722264Sjacobs 			result = report(job, show_rank, verbose);
7732264Sjacobs 
7742264Sjacobs 		papiJobFree(job);
7752264Sjacobs 	}
7762264Sjacobs 
7772264Sjacobs 	papiServiceDestroy(svc);
7782264Sjacobs 
7792264Sjacobs 	return (result);
7802264Sjacobs }
7812264Sjacobs 
7822264Sjacobs static int
7832264Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
7842264Sjacobs {
7852264Sjacobs 	papi_status_t status;
7862264Sjacobs 	char *form = NULL;
7872264Sjacobs 	void *iter = NULL;
7882264Sjacobs 
7892264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
7902264Sjacobs 					"form-supported", &form);
7912264Sjacobs 		status == PAPI_OK;
7922264Sjacobs 		status = papiAttributeListGetString(attrs, &iter,
7932264Sjacobs 							NULL, &form)) {
7942264Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
7952264Sjacobs 			printf(gettext("form %s is available to you\n"), form);
7962264Sjacobs 			if (verbose != 0) {
7972264Sjacobs 				char *detail = NULL;
7982264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
7992264Sjacobs 						"form-supported-detail",
8002264Sjacobs 						&detail);
8012264Sjacobs 				if (status == PAPI_OK)
8022264Sjacobs 					printf("%s\n", detail);
8032264Sjacobs 			}
8042264Sjacobs 		}
8052264Sjacobs 	}
8062264Sjacobs 
8072264Sjacobs 	return (0);
8082264Sjacobs }
8092264Sjacobs 
8102264Sjacobs static int
8112264Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
8122264Sjacobs {
8132264Sjacobs 	papi_status_t status;
8142264Sjacobs 	char *pw = NULL;
8152264Sjacobs 	void *iter = NULL;
8162264Sjacobs 
8172264Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
8182264Sjacobs 					"pw-supported", &pw);
8192264Sjacobs 		status == PAPI_OK;
8202264Sjacobs 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
8212264Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
8222264Sjacobs 			printf(gettext("charset %s is available\n"), pw);
8232264Sjacobs 			if (verbose != 0) {
8242264Sjacobs 				char *info = NULL;
8252264Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
8262264Sjacobs 						"pw-supported-extra", &info);
8272264Sjacobs 				if (status == PAPI_OK)
8282264Sjacobs 					printf("%s\n", info);
8292264Sjacobs 			}
8302264Sjacobs 		}
8312264Sjacobs 	}
8322264Sjacobs 
8332264Sjacobs 	return (0);
8342264Sjacobs }
8352264Sjacobs 
8362264Sjacobs static int
8372264Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
8382264Sjacobs 		papi_encryption_t encryption, int verbose)
8392264Sjacobs {
8402264Sjacobs 	int result = 0;
8412264Sjacobs 	papi_status_t status;
8422264Sjacobs 	papi_service_t svc = NULL;
8432264Sjacobs 	papi_attribute_t **attrs = NULL;
8442264Sjacobs 
8452264Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
8462264Sjacobs 					encryption, NULL);
8472264Sjacobs 	if (status != PAPI_OK) {
8482264Sjacobs 		papiServiceDestroy(svc);
8492264Sjacobs 		return (-1);
8502264Sjacobs 	}
8512264Sjacobs 
8522264Sjacobs 	attrs = papiServiceGetAttributeList(svc);
8532264Sjacobs 	if (attrs != NULL) {
8542264Sjacobs 		result = report(name, attrs, verbose);
8552264Sjacobs 
8562264Sjacobs 		if (verbose > 1) {
8572264Sjacobs 			printf("\n");
8582264Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
8592264Sjacobs 			printf("\n");
8602264Sjacobs 		}
8612264Sjacobs 	}
8622264Sjacobs 
8632264Sjacobs 	papiServiceDestroy(svc);
8642264Sjacobs 
8652264Sjacobs 	return (result);
8662264Sjacobs }
8672264Sjacobs 
8682264Sjacobs int
8692264Sjacobs main(int ac, char *av[])
8702264Sjacobs {
8712264Sjacobs 	int exit_code = 0;
8722264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
8732264Sjacobs 	int rank = 0;
8742264Sjacobs 	int verbose = 0;
8752264Sjacobs 	int description = 0;
8762264Sjacobs 	int c;
8772264Sjacobs 	char **argv;
8782264Sjacobs 
8792264Sjacobs 	(void) setlocale(LC_ALL, "");
8802264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
8812264Sjacobs 
8822264Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
8834286Swendyp 	for (c = 0; c < ac; c++) {
8844286Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
8854286Swendyp 			(isalpha(av[c][2]) != 0)) {
8864286Swendyp 			/* preserve old "-l[po...]" behavior */
8874286Swendyp 			argv[c] = &av[c][1];
8884286Swendyp 			argv[c][0] = '-';
8894286Swendyp 			verbose = 1;
8904286Swendyp 
8914286Swendyp 		} else
8924286Swendyp 			argv[c] = av[c];
8934286Swendyp 	}
8944286Swendyp 
8952264Sjacobs 	argv[c++] = "--";
8962264Sjacobs 	ac = c;
8972264Sjacobs 
8982264Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
8992264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
9002264Sjacobs 		switch (c) {
9012264Sjacobs 		case 'l':
9022264Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
9032264Sjacobs 				optarg = "1";
9042264Sjacobs 			verbose = atoi(optarg);
9052264Sjacobs 			break;
9062264Sjacobs 		case 'D':
9072264Sjacobs 			description = 1;
9082264Sjacobs 			break;
9092264Sjacobs 		case 'R':
9102264Sjacobs 			rank = 1;
9112264Sjacobs 			break;
9122264Sjacobs 		case 'E':
9132264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
9142264Sjacobs 			break;
9152264Sjacobs 		default:
9162264Sjacobs 			break;
9172264Sjacobs 		}
9182264Sjacobs 	optind = 1;
9192264Sjacobs 
9202264Sjacobs 	/* process command line arguments */
9212264Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
9222264Sjacobs 		switch (c) {	/* these may or may not have an option */
9232264Sjacobs 		case 'a':
9242264Sjacobs 		case 'c':
9252264Sjacobs 		case 'p':
9262264Sjacobs 		case 'o':
9272264Sjacobs 		case 'R':
9282264Sjacobs 		case 'u':
9292264Sjacobs 		case 'v':
9302264Sjacobs 		case 'l':
9312264Sjacobs 		case 'f':
9322264Sjacobs 		case 'S':
9332264Sjacobs 			if (optarg[0] == '-') {
9342264Sjacobs 				/* this check stop a possible infinite loop */
9352264Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
9362264Sjacobs 					optind--;
9372264Sjacobs 				optarg = NULL;
9382264Sjacobs 			} else if (strcmp(optarg, "all") == 0)
9392264Sjacobs 				optarg = NULL;
9402264Sjacobs 		}
9412264Sjacobs 
9422264Sjacobs 		switch (c) {
9432264Sjacobs 		case 'a':
9442264Sjacobs 			exit_code += printer_query(optarg, report_accepting,
9452264Sjacobs 						encryption, verbose, 0);
9462264Sjacobs 			break;
9472264Sjacobs 		case 'c':
9482264Sjacobs 			exit_code += printer_query(optarg, report_class,
9492264Sjacobs 						encryption, verbose, 0);
9502264Sjacobs 			break;
9512264Sjacobs 		case 'p':
9522264Sjacobs 			exit_code += printer_query(optarg, report_printer,
9532264Sjacobs 						encryption, verbose,
9542264Sjacobs 						description);
9552264Sjacobs 			break;
9562264Sjacobs 		case 'd':
9572264Sjacobs 			exit_code += lpstat_default_printer(encryption);
9582264Sjacobs 			break;
9592264Sjacobs 		case 'r':
9602264Sjacobs 			exit_code += lpstat_service_status(encryption);
9612264Sjacobs 			break;
9622264Sjacobs 		case 'u':
9632264Sjacobs 			if (optarg != NULL)
9642264Sjacobs 				users = strsplit(optarg, ", \n");
9652264Sjacobs 			exit_code += job_query(NULL, report_job,
9662264Sjacobs 						encryption, rank, verbose);
9672264Sjacobs 			if (users != NULL) {
9682264Sjacobs 				free(users);
9692264Sjacobs 				users = NULL;
9702264Sjacobs 			}
9712264Sjacobs 			break;
9722264Sjacobs 		case 'v':
9732264Sjacobs 			exit_code += printer_query(optarg, report_device,
9742264Sjacobs 						encryption, verbose, 0);
9752264Sjacobs 			break;
9762264Sjacobs 		case 'o':
9772264Sjacobs 			exit_code += job_query(optarg, report_job,
9782264Sjacobs 						encryption, rank, verbose);
9792264Sjacobs 			break;
9802264Sjacobs 		case 'f':
9812264Sjacobs 			exit_code += service_query(optarg, report_form,
9822264Sjacobs 						encryption, verbose);
9832264Sjacobs 			break;
9842264Sjacobs 		case 'S':
9852264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
9862264Sjacobs 						encryption, verbose);
9872264Sjacobs 			break;
9882264Sjacobs 		case 's':
9892264Sjacobs 			exit_code += lpstat_service_status(encryption);
9902264Sjacobs 			exit_code += lpstat_default_printer(encryption);
9912264Sjacobs 			exit_code += printer_query(NULL, report_class,
9922264Sjacobs 						encryption, verbose, 0);
9932264Sjacobs 			exit_code += printer_query(NULL, report_device,
9942264Sjacobs 						encryption, verbose, 0);
9952264Sjacobs 			exit_code += service_query(optarg, report_form,
9962264Sjacobs 						encryption, verbose);
9972264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
9982264Sjacobs 						encryption, verbose);
9992264Sjacobs 			break;
10002264Sjacobs 		case 't':
10012264Sjacobs 			exit_code += lpstat_service_status(encryption);
10022264Sjacobs 			exit_code += lpstat_default_printer(encryption);
10032264Sjacobs 			exit_code += printer_query(NULL, report_class,
10042264Sjacobs 						encryption, verbose, 0);
10052264Sjacobs 			exit_code += printer_query(NULL, report_device,
10062264Sjacobs 						encryption, verbose, 0);
10072264Sjacobs 			exit_code += printer_query(NULL, report_accepting,
10082264Sjacobs 						encryption, verbose, 0);
10092264Sjacobs 			exit_code += printer_query(NULL, report_printer,
10102264Sjacobs 						encryption, verbose, 0);
10112264Sjacobs 			exit_code += service_query(optarg, report_form,
10122264Sjacobs 						encryption, verbose);
10132264Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
10142264Sjacobs 						encryption, verbose);
10152264Sjacobs 			exit_code += job_query(NULL, report_job,
10162264Sjacobs 						encryption, rank, verbose);
10172264Sjacobs 			break;
10182264Sjacobs 		case 'L':	/* local-only, ignored */
10192264Sjacobs 		case 'l':	/* increased verbose level in first pass */
10202264Sjacobs 		case 'D':	/* set "description" flag in first pass */
10212264Sjacobs 		case 'R':	/* set "rank" flag in first pass */
10222264Sjacobs 		case 'E':	/* set encryption in the first pass */
10232264Sjacobs 			break;
10242264Sjacobs 		default:
10252264Sjacobs 			usage(av[0]);
10262264Sjacobs 		}
10272264Sjacobs 	}
10282264Sjacobs 	ac--;
10292264Sjacobs 
10302264Sjacobs 	if (ac == 1) {	/* report on my jobs */
10312264Sjacobs 		struct passwd *pw = getpwuid(getuid());
10322264Sjacobs 
10332264Sjacobs 		if (pw != NULL)
10342264Sjacobs 			users = strsplit(pw->pw_name, "");
10352264Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
10362264Sjacobs 					rank, verbose);
10372264Sjacobs 		if (users != NULL) {
10382264Sjacobs 			free(users);
10392264Sjacobs 			users = NULL;
10402264Sjacobs 		}
10412264Sjacobs 	} else {
10422264Sjacobs 		for (c = optind; c < ac; c++)
10432264Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
10442264Sjacobs 					rank, verbose);
10452264Sjacobs 	}
10462264Sjacobs 
10472264Sjacobs 
10482264Sjacobs 	if (exit_code != 0)
10492264Sjacobs 		exit_code = 1;
10502264Sjacobs 
10512264Sjacobs 	return (exit_code);
10522264Sjacobs }
1053