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