xref: /titanic_50/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 91216fe4fad15d011cd2eaf16747b7eb64f1e290)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
23022ba35cSjacobs  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpstat.c 173 2006-05-25 04:52:06Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
34355b4669Sjacobs #include <unistd.h>
35355b4669Sjacobs #include <string.h>
36355b4669Sjacobs #include <locale.h>
37355b4669Sjacobs #include <libintl.h>
38*91216fe4Swendyp #include <ctype.h>
39355b4669Sjacobs #include <pwd.h>
40355b4669Sjacobs #include <papi.h>
41355b4669Sjacobs #include <uri.h>
42355b4669Sjacobs #include "common.h"
43355b4669Sjacobs 
44355b4669Sjacobs static void
45355b4669Sjacobs usage(char *program)
46355b4669Sjacobs {
47355b4669Sjacobs 	char *name;
48355b4669Sjacobs 
49355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
50355b4669Sjacobs 		name = program;
51355b4669Sjacobs 	else
52355b4669Sjacobs 		name++;
53355b4669Sjacobs 
54355b4669Sjacobs 	fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
55355b4669Sjacobs 		"[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
56355b4669Sjacobs 		"[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
57355b4669Sjacobs 		"[-f [list] [-l]] [-u list]\n"),
58355b4669Sjacobs 		name);
59355b4669Sjacobs 	exit(1);
60355b4669Sjacobs }
61355b4669Sjacobs 
62355b4669Sjacobs static char *
63355b4669Sjacobs nctime(time_t *t)
64355b4669Sjacobs {
65355b4669Sjacobs 	static char buf[64];
66355b4669Sjacobs 	struct tm *tm = localtime(t);
67355b4669Sjacobs 
68355b4669Sjacobs 	(void) strftime(buf, sizeof (buf), "%c", tm);
69355b4669Sjacobs 
70355b4669Sjacobs 	return (buf);
71355b4669Sjacobs }
72355b4669Sjacobs 
73355b4669Sjacobs static char *
74355b4669Sjacobs printer_name(papi_printer_t printer)
75355b4669Sjacobs {
76355b4669Sjacobs 	papi_attribute_t **attributes = papiPrinterGetAttributeList(printer);
77355b4669Sjacobs 	char *result = NULL;
78355b4669Sjacobs 
79355b4669Sjacobs 	if (attributes != NULL)
80355b4669Sjacobs 		papiAttributeListGetString(attributes, NULL,
81355b4669Sjacobs 				"printer-name", &result);
82355b4669Sjacobs 
83355b4669Sjacobs 	return (result);
84355b4669Sjacobs }
85355b4669Sjacobs 
86355b4669Sjacobs static int
87355b4669Sjacobs lpstat_default_printer(papi_encryption_t encryption)
88355b4669Sjacobs {
89355b4669Sjacobs 	papi_status_t status;
90355b4669Sjacobs 	papi_service_t svc = NULL;
91355b4669Sjacobs 	papi_printer_t p = NULL;
92355b4669Sjacobs 	char *name = NULL;
93355b4669Sjacobs 
94355b4669Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback,
95355b4669Sjacobs 					encryption, NULL);
96355b4669Sjacobs 	if (status == PAPI_OK) {
97355b4669Sjacobs 		char *req[] = { "printer-name", NULL };
98355b4669Sjacobs 
99355b4669Sjacobs 		status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p);
100355b4669Sjacobs 		if (p != NULL)
101355b4669Sjacobs 			name = printer_name(p);
102355b4669Sjacobs 	}
103355b4669Sjacobs 	if (name != NULL)
104355b4669Sjacobs 		printf(gettext("system default printer: %s\n"), name);
105355b4669Sjacobs 	else
106355b4669Sjacobs 		printf(gettext("no system default destination\n"));
107355b4669Sjacobs 	papiPrinterFree(p);
108355b4669Sjacobs 	papiServiceDestroy(svc);
109355b4669Sjacobs 
110355b4669Sjacobs 	return (0);
111355b4669Sjacobs }
112355b4669Sjacobs 
113355b4669Sjacobs static int
114355b4669Sjacobs lpstat_service_status(papi_encryption_t encryption)
115355b4669Sjacobs {
116355b4669Sjacobs 	int result = 0;
117355b4669Sjacobs 	papi_status_t status;
118355b4669Sjacobs 	papi_service_t svc = NULL;
119355b4669Sjacobs 	char *name = NULL;
120355b4669Sjacobs 
121355b4669Sjacobs 	if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
122355b4669Sjacobs 	    ((name = getenv("IPP_SERVER")) == NULL) &&
123355b4669Sjacobs 	    ((name = getenv("CUPS_SERVER")) == NULL))
124355b4669Sjacobs 		name = DEFAULT_SERVICE_URI;
125355b4669Sjacobs 
126355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
127355b4669Sjacobs 					encryption, NULL);
128355b4669Sjacobs 	if (status != PAPI_OK) {
129355b4669Sjacobs 		printf(gettext("scheduler is not running\n"));
130355b4669Sjacobs 		result = -1;
131355b4669Sjacobs 	} else
132355b4669Sjacobs 		printf(gettext("scheduler is running\n"));
133355b4669Sjacobs 	papiServiceDestroy(svc);
134355b4669Sjacobs 
135355b4669Sjacobs 	return (result);
136355b4669Sjacobs }
137355b4669Sjacobs 
138355b4669Sjacobs static char *
139355b4669Sjacobs get_device_uri(papi_service_t svc, char *name)
140355b4669Sjacobs {
141355b4669Sjacobs 	papi_status_t status;
142355b4669Sjacobs 	papi_printer_t p = NULL;
143355b4669Sjacobs 	char *keys[] = { "device-uri", NULL };
144355b4669Sjacobs 	char *result = NULL;
145355b4669Sjacobs 
146355b4669Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
147355b4669Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
148355b4669Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
149355b4669Sjacobs 
150355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
151355b4669Sjacobs 					"device-uri", &result);
152355b4669Sjacobs 		if (result != NULL)
153355b4669Sjacobs 			result = strdup(result);
154355b4669Sjacobs 
155355b4669Sjacobs 		papiPrinterFree(p);
156355b4669Sjacobs 	}
157355b4669Sjacobs 
158355b4669Sjacobs 	return (result);
159355b4669Sjacobs }
160355b4669Sjacobs 
161355b4669Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
162355b4669Sjacobs 					NULL };
163355b4669Sjacobs /* ARGSUSED2 */
164355b4669Sjacobs static int
165355b4669Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
166355b4669Sjacobs 		int verbose, int description)
167355b4669Sjacobs {
168355b4669Sjacobs 	papi_status_t status;
169355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
170355b4669Sjacobs 	char *uri = NULL;
171355b4669Sjacobs 	char *device = NULL;
172355b4669Sjacobs 	uri_t *u = NULL;
173355b4669Sjacobs 
174355b4669Sjacobs 	if (name == NULL) {
175355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
176355b4669Sjacobs 					"printer-name", &name);
177355b4669Sjacobs 		if (status != PAPI_OK)
178355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
179355b4669Sjacobs 					"printer-uri-supported", &name);
180355b4669Sjacobs 	}
181355b4669Sjacobs 
182355b4669Sjacobs 	if (name == NULL)
183355b4669Sjacobs 		return (-1);
184355b4669Sjacobs 
185355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
186355b4669Sjacobs 					"printer-uri-supported", &uri);
187355b4669Sjacobs 
188355b4669Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
189355b4669Sjacobs 		char *nodename = localhostname();
190355b4669Sjacobs 
191355b4669Sjacobs 		if ((u->host == NULL) ||
192355b4669Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
193355b4669Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
194355b4669Sjacobs 			device = get_device_uri(svc, name);
195355b4669Sjacobs 
196355b4669Sjacobs 		if (device != NULL) {
197355b4669Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
198355b4669Sjacobs 			return (0);
199355b4669Sjacobs 		} else if (uri != NULL) {
200355b4669Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
201355b4669Sjacobs 				u->host, uri);
202355b4669Sjacobs 			return (0);
203355b4669Sjacobs 		}
204355b4669Sjacobs 
205355b4669Sjacobs 		uri_free(u);
206355b4669Sjacobs 	}
207355b4669Sjacobs 
208355b4669Sjacobs 	return (0);
209355b4669Sjacobs }
210355b4669Sjacobs 
211355b4669Sjacobs static char *report_accepting_keys[] = { "printer-name",
212355b4669Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
213355b4669Sjacobs 			"printer-up-time", "printer-state-time",
214355b4669Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
215355b4669Sjacobs /* ARGSUSED2 */
216355b4669Sjacobs static int
217355b4669Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
218355b4669Sjacobs 		int verbose, int description)
219355b4669Sjacobs {
220355b4669Sjacobs 	papi_status_t status;
221355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
222355b4669Sjacobs 	time_t curr;
223355b4669Sjacobs 	char boolean = PAPI_FALSE;
224355b4669Sjacobs 
225355b4669Sjacobs 	if (name == NULL) {
226355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
227355b4669Sjacobs 					"printer-name", &name);
228355b4669Sjacobs 		if (status != PAPI_OK)
229355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
230355b4669Sjacobs 					"printer-uri-supported", &name);
231355b4669Sjacobs 	}
232355b4669Sjacobs 	if (name == NULL)
233355b4669Sjacobs 		return (-1);
234355b4669Sjacobs 
235355b4669Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
236355b4669Sjacobs 				"printer-is-accepting-jobs", &boolean);
237355b4669Sjacobs 	(void) time(&curr);
238355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
239355b4669Sjacobs 					"printer-up-time", &curr);
240355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
241355b4669Sjacobs 					"printer-state-time", &curr);
242355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
243355b4669Sjacobs 					"lpsched-reject-date", &curr);
244355b4669Sjacobs 
245355b4669Sjacobs 	if (boolean == PAPI_TRUE) {
246355b4669Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
247355b4669Sjacobs 			name, nctime(&curr));
248355b4669Sjacobs 	} else {
249355b4669Sjacobs 		char *reason = "unknown reason";
250355b4669Sjacobs 
251355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
252355b4669Sjacobs 					"lpsched-reject-reason", &reason);
253355b4669Sjacobs 
254355b4669Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
255355b4669Sjacobs 			name, nctime(&curr), reason);
256355b4669Sjacobs 	}
257355b4669Sjacobs 
258355b4669Sjacobs 	return (0);
259355b4669Sjacobs }
260355b4669Sjacobs 
261355b4669Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
262355b4669Sjacobs 					"member-names", NULL };
263355b4669Sjacobs /* ARGSUSED2 */
264355b4669Sjacobs static int
265355b4669Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
266355b4669Sjacobs 		int verbose, int description)
267355b4669Sjacobs {
268355b4669Sjacobs 	papi_status_t status;
269355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
270355b4669Sjacobs 	char *member = NULL;
271355b4669Sjacobs 	void *iter = NULL;
272355b4669Sjacobs 
273355b4669Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
274355b4669Sjacobs 				"member-names", &member);
275355b4669Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
276355b4669Sjacobs 		return (0);
277355b4669Sjacobs 
278355b4669Sjacobs 	if (name == NULL) {
279355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
280355b4669Sjacobs 					"printer-name", &name);
281355b4669Sjacobs 		if (status != PAPI_OK)
282355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
283355b4669Sjacobs 					"printer-uri-supported", &name);
284355b4669Sjacobs 	}
285355b4669Sjacobs 	if (name == NULL)
286355b4669Sjacobs 		return (-1);
287355b4669Sjacobs 
288355b4669Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
289355b4669Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
290355b4669Sjacobs 			== PAPI_OK)
291355b4669Sjacobs 		printf("\t%s\n", member);
292355b4669Sjacobs 
293355b4669Sjacobs 	return (0);
294355b4669Sjacobs }
295355b4669Sjacobs 
296355b4669Sjacobs static char *report_printer_keys[] = { "printer-name",
297355b4669Sjacobs 			"printer-uri-supported", "printer-state",
298355b4669Sjacobs 			"printer-up-time", "printer-state-time",
299355b4669Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
300355b4669Sjacobs 			"lpsched-disable-reason", NULL };
301355b4669Sjacobs /* ARGSUSED2 */
302355b4669Sjacobs static int
303355b4669Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
304355b4669Sjacobs 		int verbose, int description)
305355b4669Sjacobs {
306355b4669Sjacobs 	papi_status_t status;
307355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
308355b4669Sjacobs 	time_t curr;
309355b4669Sjacobs 	int32_t pstat = 0;
310355b4669Sjacobs 	char *member = NULL;
311355b4669Sjacobs 
312355b4669Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
313355b4669Sjacobs 				"member-names", &member);
314355b4669Sjacobs 	if (status == PAPI_OK)	/* it's a class */
315355b4669Sjacobs 		return (0);
316355b4669Sjacobs 
317355b4669Sjacobs 	if (name == NULL) {
318355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
319355b4669Sjacobs 					"printer-name", &name);
320355b4669Sjacobs 		if (status != PAPI_OK)
321355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
322355b4669Sjacobs 					"printer-uri-supported", &name);
323355b4669Sjacobs 	}
324355b4669Sjacobs 	if (name == NULL)
325355b4669Sjacobs 		return (-1);
326355b4669Sjacobs 
327355b4669Sjacobs 	printf(gettext("printer %s "), name);
328355b4669Sjacobs 
329355b4669Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
330355b4669Sjacobs 					"printer-state", &pstat);
331355b4669Sjacobs 
332355b4669Sjacobs 	switch (pstat) {
333355b4669Sjacobs 	case 0x03:	/* idle */
334355b4669Sjacobs 		printf(gettext("idle. enabled"));
335355b4669Sjacobs 		break;
336355b4669Sjacobs 	case 0x04: {	/* processing */
337355b4669Sjacobs 		char *requested[] = { "job-id", NULL };
338355b4669Sjacobs 		papi_job_t *j = NULL;
339355b4669Sjacobs 		int32_t jobid = 0;
340355b4669Sjacobs 
341355b4669Sjacobs 		(void) papiPrinterListJobs(svc, name, requested, 0, 1, &j);
342355b4669Sjacobs 		if ((j != NULL) && (j[0] != NULL))
343355b4669Sjacobs 			jobid = papiJobGetId(j[0]);
344355b4669Sjacobs 		papiJobListFree(j);
345355b4669Sjacobs 
346355b4669Sjacobs 		printf(gettext("now printing %s-%d. enabled"), name, jobid);
347355b4669Sjacobs 		}
348355b4669Sjacobs 		break;
349355b4669Sjacobs 	case 0x05:	/* stopped */
350355b4669Sjacobs 		printf(gettext("disabled"));
351355b4669Sjacobs 		break;
352355b4669Sjacobs 	default:
353355b4669Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
354355b4669Sjacobs 		break;
355355b4669Sjacobs 	}
356355b4669Sjacobs 
357355b4669Sjacobs 	(void) time(&curr);
358355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
359355b4669Sjacobs 					"printer-up-time", &curr);
360355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
361355b4669Sjacobs 					"printer-state-time", &curr);
362355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
363355b4669Sjacobs 					"lpsched-disable-date", &curr);
364355b4669Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
365355b4669Sjacobs 
366355b4669Sjacobs 	if (pstat == 0x05) {
367355b4669Sjacobs 		char *reason = "unknown reason";
368355b4669Sjacobs 
369355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
370355b4669Sjacobs 					"printer-state-reasons", &reason);
371355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
372355b4669Sjacobs 					"lpsched-disable-reason", &reason);
373355b4669Sjacobs 		printf(gettext("\t%s\n"), reason);
374355b4669Sjacobs 	}
375355b4669Sjacobs 
376355b4669Sjacobs 	if (verbose == 1) {
377355b4669Sjacobs 		void *iter;
378355b4669Sjacobs 		char *str;
379355b4669Sjacobs 
380355b4669Sjacobs 		str = "";
381355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
382355b4669Sjacobs 					"form-ready", &str);
383355b4669Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
384355b4669Sjacobs 
385355b4669Sjacobs 		str = "";
386355b4669Sjacobs 		iter = NULL;
387355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
388355b4669Sjacobs 					"document-format-supported", &str);
389355b4669Sjacobs 		printf(gettext("\tContent types: %s"), str);
390355b4669Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
391355b4669Sjacobs 				== PAPI_OK)
392355b4669Sjacobs 			printf(", %s", str);
393355b4669Sjacobs 		printf("\n");
394355b4669Sjacobs 
395355b4669Sjacobs 		str = "";
396355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
397355b4669Sjacobs 					"printer-info", &str);
398355b4669Sjacobs 		printf(gettext("\tDescription: %s\n"), str);
399355b4669Sjacobs 
400355b4669Sjacobs 		str = "";
401355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
402355b4669Sjacobs 					"lpsched-dial-info", &str);
403355b4669Sjacobs 		printf(gettext("\tConnection: %s\n"),
404355b4669Sjacobs 			((str[0] != '\0') ? gettext("direct") : str));
405355b4669Sjacobs 
406355b4669Sjacobs 		str = "";
407355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
408355b4669Sjacobs 					"lpsched-interface-script", &str);
409355b4669Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
410355b4669Sjacobs 
411355b4669Sjacobs 		str = NULL;
412355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
413355b4669Sjacobs 					"ppd-file-uri", &str);
414355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
415355b4669Sjacobs 					"lpsched-ppd-source-path", &str);
416355b4669Sjacobs 		if (str != NULL)
417355b4669Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
418355b4669Sjacobs 
419355b4669Sjacobs 		str = NULL;
420355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
421355b4669Sjacobs 					"lpsched-fault-alert-command", &str);
422355b4669Sjacobs 		if (str != NULL)
423355b4669Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
424355b4669Sjacobs 
425355b4669Sjacobs 		str = "";
426355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
427355b4669Sjacobs 					"lpsched-fault-recovery", &str);
428355b4669Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
429355b4669Sjacobs 			((str[0] == '\0') ? gettext("continue") : str));
430355b4669Sjacobs 
431355b4669Sjacobs 		str = "(all)";
432355b4669Sjacobs 		iter = NULL;
433355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
434355b4669Sjacobs 					"requesting-user-name-allowed", &str);
435355b4669Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
436355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
437355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
438355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
439355b4669Sjacobs 					&str) == PAPI_OK)
440355b4669Sjacobs 				printf("\t\t%s\n", str);
441355b4669Sjacobs 
442355b4669Sjacobs 		str = NULL;
443355b4669Sjacobs 		iter = NULL;
444355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
445355b4669Sjacobs 					"requesting-user-name-denied", &str);
446355b4669Sjacobs 		if (str != NULL) {
447355b4669Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
448355b4669Sjacobs 				((str[0] == '\0') ? gettext("(none)") : str));
449355b4669Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
450355b4669Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
451355b4669Sjacobs 						NULL, &str) == PAPI_OK)
452355b4669Sjacobs 					printf("\t\t%s\n", str);
453355b4669Sjacobs 		}
454355b4669Sjacobs 
455355b4669Sjacobs 		str = "(none)";
456355b4669Sjacobs 		iter = NULL;
457355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
458355b4669Sjacobs 					"form-supported", &str);
459355b4669Sjacobs 		printf(gettext("\tForms allowed:\n\t\t%s\n"),
460355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
461355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
462355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
463355b4669Sjacobs 					&str) == PAPI_OK)
464355b4669Sjacobs 				printf("\t\t%s\n", str);
465355b4669Sjacobs 
466355b4669Sjacobs 		str = "";
467355b4669Sjacobs 		iter = NULL;
468355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
469355b4669Sjacobs 					"media-supported", &str);
470355b4669Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
471355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
472355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
473355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
474355b4669Sjacobs 					&str) == PAPI_OK)
475355b4669Sjacobs 				printf("\t\t%s\n", str);
476355b4669Sjacobs 
477355b4669Sjacobs 		str = "";
478355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
479355b4669Sjacobs 					"job-sheets-supported", &str);
480355b4669Sjacobs 		printf(gettext("\tBanner %s\n"),
481355b4669Sjacobs 			(strcasecmp(str, "none") == 0 ?
482355b4669Sjacobs 				gettext("not required") : gettext("required")));
483355b4669Sjacobs 
484355b4669Sjacobs 		str = "";
485355b4669Sjacobs 		iter = NULL;
486355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
487355b4669Sjacobs 					"lpsched-print-wheels", &str);
488355b4669Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
489355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
490355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
491355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
492355b4669Sjacobs 					&str) == PAPI_OK)
493355b4669Sjacobs 				printf("\t\t%s\n", str);
494355b4669Sjacobs 
495355b4669Sjacobs 		printf(gettext("\tDefault pitch:\n"));
496355b4669Sjacobs 		printf(gettext("\tDefault page size:\n"));
497355b4669Sjacobs 		printf(gettext("\tDefault port setting:\n"));
498355b4669Sjacobs 
499355b4669Sjacobs 		str = "";
500355b4669Sjacobs 		iter = NULL;
501355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
502355b4669Sjacobs 					"lpsched-options", &str);
503355b4669Sjacobs 		if (str != NULL) {
504355b4669Sjacobs 			printf(gettext("\tOptions: %s"), str);
505355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
506355b4669Sjacobs 						&str) == PAPI_OK)
507355b4669Sjacobs 				printf(", %s", str);
508355b4669Sjacobs 			printf("\n");
509355b4669Sjacobs 		}
510355b4669Sjacobs 
511355b4669Sjacobs 	} else if (description == 1) {
512355b4669Sjacobs 		char *str = "";
513355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
514355b4669Sjacobs 					"printer-description", &str);
515355b4669Sjacobs 		printf(gettext("\tDescription: %s\n"), str);
516355b4669Sjacobs 	} else if (verbose > 1)
517355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
518355b4669Sjacobs 
519355b4669Sjacobs 	if (verbose > 0)
520355b4669Sjacobs 		printf("\n");
521355b4669Sjacobs 
522355b4669Sjacobs 	return (0);
523355b4669Sjacobs }
524355b4669Sjacobs 
525355b4669Sjacobs static int
526355b4669Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
527355b4669Sjacobs 					int, int), papi_encryption_t encryption,
528355b4669Sjacobs 		int verbose, int description)
529355b4669Sjacobs {
530355b4669Sjacobs 	int result = 0;
531355b4669Sjacobs 	papi_status_t status;
532355b4669Sjacobs 	papi_service_t svc = NULL;
533355b4669Sjacobs 
534355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
535355b4669Sjacobs 					encryption, NULL);
536355b4669Sjacobs 	if (status != PAPI_OK) {
537022ba35cSjacobs 		if (status == PAPI_NOT_FOUND)
538022ba35cSjacobs 			fprintf(stderr, gettext("%s: unknown printer\n"),
539022ba35cSjacobs 				name ? name : "(NULL)");
540022ba35cSjacobs 		else
541355b4669Sjacobs 			fprintf(stderr, gettext(
542355b4669Sjacobs 				"Failed to contact service for %s: %s\n"),
543355b4669Sjacobs 				name ? name : "(NULL)",
544355b4669Sjacobs 				verbose_papi_message(svc, status));
545355b4669Sjacobs 		papiServiceDestroy(svc);
546355b4669Sjacobs 		return (-1);
547355b4669Sjacobs 	}
548355b4669Sjacobs 
549355b4669Sjacobs 	if (name == NULL) { /* all */
550355b4669Sjacobs 		char **interest = interest_list(svc);
551355b4669Sjacobs 
552355b4669Sjacobs 		if (interest != NULL) {
553355b4669Sjacobs 			int i;
554355b4669Sjacobs 
555355b4669Sjacobs 			for (i = 0; interest[i] != NULL; i++)
556355b4669Sjacobs 				result += printer_query(interest[i], report,
557355b4669Sjacobs 							encryption, verbose,
558355b4669Sjacobs 							description);
559355b4669Sjacobs 		}
560355b4669Sjacobs 	} else {
561355b4669Sjacobs 		papi_printer_t printer = NULL;
562355b4669Sjacobs 		char **keys = NULL;
563355b4669Sjacobs 
564355b4669Sjacobs 		/*
565355b4669Sjacobs 		 * Limit the query to only required data to reduce the need
566355b4669Sjacobs 		 * to go remote for information.
567355b4669Sjacobs 		 */
568355b4669Sjacobs 		if (report == report_device)
569355b4669Sjacobs 			keys = report_device_keys;
570355b4669Sjacobs 		else if (report == report_class)
571355b4669Sjacobs 			keys = report_class_keys;
572355b4669Sjacobs 		else if (report == report_accepting)
573355b4669Sjacobs 			keys = report_accepting_keys;
574355b4669Sjacobs 		else if ((report == report_printer) && (verbose == 0))
575355b4669Sjacobs 			keys = report_printer_keys;
576355b4669Sjacobs 
577355b4669Sjacobs 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
578355b4669Sjacobs 		if (status != PAPI_OK) {
579355b4669Sjacobs 			fprintf(stderr, gettext(
580355b4669Sjacobs 				"Failed to get printer info for %s: %s\n"),
581355b4669Sjacobs 				name, verbose_papi_message(svc, status));
582355b4669Sjacobs 			papiServiceDestroy(svc);
583355b4669Sjacobs 			return (-1);
584355b4669Sjacobs 		}
585355b4669Sjacobs 
586355b4669Sjacobs 		if (printer != NULL)
587355b4669Sjacobs 			result = report(svc, name, printer, verbose,
588355b4669Sjacobs 					description);
589355b4669Sjacobs 
590355b4669Sjacobs 		papiPrinterFree(printer);
591355b4669Sjacobs 	}
592355b4669Sjacobs 
593355b4669Sjacobs 	papiServiceDestroy(svc);
594355b4669Sjacobs 
595355b4669Sjacobs 	return (result);
596355b4669Sjacobs }
597355b4669Sjacobs 
598355b4669Sjacobs static int
599355b4669Sjacobs match_user(char *user, char **list)
600355b4669Sjacobs {
601355b4669Sjacobs 	int i;
602355b4669Sjacobs 
603355b4669Sjacobs 	for (i = 0; list[i] != NULL; i++) {
604355b4669Sjacobs 		if (strcmp(user, list[i]) == 0)
605355b4669Sjacobs 			return (0);
606355b4669Sjacobs 	}
607355b4669Sjacobs 
608355b4669Sjacobs 	return (-1);
609355b4669Sjacobs }
610355b4669Sjacobs 
611355b4669Sjacobs static char **users = NULL;
612355b4669Sjacobs 
613355b4669Sjacobs static int
614355b4669Sjacobs report_job(papi_job_t job, int show_rank, int verbose)
615355b4669Sjacobs {
616355b4669Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
617355b4669Sjacobs 	time_t clock = 0;
618355b4669Sjacobs 	char date[24];
619355b4669Sjacobs 	char request[26];
620355b4669Sjacobs 	char *user = "unknown";
621355b4669Sjacobs 	int32_t size = 0;
622355b4669Sjacobs 	int32_t jstate = 0;
623355b4669Sjacobs 
624355b4669Sjacobs 	char *destination = "unknown";
625355b4669Sjacobs 	int32_t id = -1;
626355b4669Sjacobs 
627355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
628355b4669Sjacobs 				"job-originating-user-name", &user);
629355b4669Sjacobs 
630355b4669Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
631355b4669Sjacobs 		return (0);
632355b4669Sjacobs 
633355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
634355b4669Sjacobs 	size *= 1024;	/* for the approximate byte size */
635355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
636355b4669Sjacobs 
637355b4669Sjacobs 	(void) time(&clock);
638355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
639355b4669Sjacobs 				"time-at-creation", (int32_t *)&clock);
640355b4669Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
641355b4669Sjacobs 
642355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
643355b4669Sjacobs 				"job-printer-uri", &destination);
644355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
645355b4669Sjacobs 				"printer-name", &destination);
646355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
647355b4669Sjacobs 				"job-id", &id);
648355b4669Sjacobs 	snprintf(request, sizeof (request), "%s-%d", destination, id);
649355b4669Sjacobs 
650355b4669Sjacobs 	if (show_rank != 0) {
651355b4669Sjacobs 		int32_t rank = -1;
652355b4669Sjacobs 
653355b4669Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
654355b4669Sjacobs 				"number-of-intervening-jobs", &rank);
655355b4669Sjacobs 		rank++;
656355b4669Sjacobs 
657355b4669Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
658355b4669Sjacobs 			rank, request, user, size, date);
659355b4669Sjacobs 	} else
660355b4669Sjacobs 		printf("%-23s %-14s %7ld   %s", request, user, size, date);
661355b4669Sjacobs 
662355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
663355b4669Sjacobs 				"job-state", &jstate);
664355b4669Sjacobs 	if (jstate == 0x04)
665355b4669Sjacobs 		printf(gettext(", being held"));
666355b4669Sjacobs 	else if (jstate == 0x07)
667355b4669Sjacobs 		printf(gettext(", cancelled"));
668355b4669Sjacobs 	else if (jstate == 0x09)
669355b4669Sjacobs 		printf(gettext(", complete"));
670355b4669Sjacobs 
671355b4669Sjacobs 	if (verbose == 1) {
6720a44ef6dSjacobs 		char *form = NULL;
6730a44ef6dSjacobs 
674355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
675355b4669Sjacobs 				"output-device-assigned", &destination);
676355b4669Sjacobs 		printf("\n\t assigned %s", destination);
6770a44ef6dSjacobs 
6780a44ef6dSjacobs 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
6790a44ef6dSjacobs 		if (form != NULL)
6800a44ef6dSjacobs 			printf(", form %s", form);
681355b4669Sjacobs 	} else if (verbose > 1) {
682355b4669Sjacobs 		printf("\n");
683355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
684355b4669Sjacobs 	}
685355b4669Sjacobs 
686355b4669Sjacobs 	printf("\n");
687355b4669Sjacobs 
688355b4669Sjacobs 	return (0);
689355b4669Sjacobs }
690355b4669Sjacobs 
691355b4669Sjacobs static int
692355b4669Sjacobs job_query(char *request, int (*report)(papi_job_t, int, int),
693355b4669Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
694355b4669Sjacobs {
695355b4669Sjacobs 	int result = 0;
696355b4669Sjacobs 	papi_status_t status;
697355b4669Sjacobs 	papi_service_t svc = NULL;
698355b4669Sjacobs 	char *printer = NULL;
699355b4669Sjacobs 	int32_t id = -1;
700355b4669Sjacobs 
701355b4669Sjacobs 	get_printer_id(request, &printer, &id);
702355b4669Sjacobs 
703355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
704355b4669Sjacobs 					encryption, NULL);
705355b4669Sjacobs 	if (status != PAPI_OK) {
706355b4669Sjacobs 		fprintf(stderr, gettext(
707355b4669Sjacobs 			"Failed to contact service for %s: %s\n"),
708355b4669Sjacobs 			(printer ? printer : "all"),
709355b4669Sjacobs 			verbose_papi_message(svc, status));
710355b4669Sjacobs 		return (-1);
711355b4669Sjacobs 	}
712355b4669Sjacobs 
713355b4669Sjacobs 	if (printer == NULL) { /* all */
714355b4669Sjacobs 		char **interest = interest_list(svc);
715355b4669Sjacobs 
716355b4669Sjacobs 		if (interest != NULL) {
717355b4669Sjacobs 			int i;
718355b4669Sjacobs 
719355b4669Sjacobs 			for (i = 0; interest[i] != NULL; i++)
720355b4669Sjacobs 				result += job_query(interest[i], report,
721355b4669Sjacobs 						encryption, show_rank, verbose);
722355b4669Sjacobs 		}
723355b4669Sjacobs 	} else if (id == -1) { /* a printer */
724355b4669Sjacobs 		papi_job_t *jobs = NULL;
725355b4669Sjacobs 
726355b4669Sjacobs 		status = papiPrinterListJobs(svc, printer, NULL, 0, 0, &jobs);
727355b4669Sjacobs 		if (status != PAPI_OK) {
728355b4669Sjacobs 			fprintf(stderr, gettext(
729355b4669Sjacobs 				"Failed to get job list: %s\n"),
730355b4669Sjacobs 				verbose_papi_message(svc, status));
731355b4669Sjacobs 			papiServiceDestroy(svc);
732355b4669Sjacobs 			return (-1);
733355b4669Sjacobs 		}
734355b4669Sjacobs 
735355b4669Sjacobs 		if (jobs != NULL) {
736355b4669Sjacobs 			int i;
737355b4669Sjacobs 
738355b4669Sjacobs 			for (i = 0; jobs[i] != NULL; i++)
739355b4669Sjacobs 				result += report(jobs[i], show_rank, verbose);
740355b4669Sjacobs 		}
741355b4669Sjacobs 
742355b4669Sjacobs 		papiJobListFree(jobs);
743355b4669Sjacobs 	} else {	/* a job */
744355b4669Sjacobs 		papi_job_t job = NULL;
745355b4669Sjacobs 
746355b4669Sjacobs 		status = papiJobQuery(svc, printer, id, NULL, &job);
747355b4669Sjacobs 		if (status != PAPI_OK) {
748355b4669Sjacobs 			fprintf(stderr, gettext(
749355b4669Sjacobs 				"Failed to get job info for %s: %s\n"),
750355b4669Sjacobs 				request, verbose_papi_message(svc, status));
751355b4669Sjacobs 			papiServiceDestroy(svc);
752355b4669Sjacobs 			return (-1);
753355b4669Sjacobs 		}
754355b4669Sjacobs 
755355b4669Sjacobs 		if (job != NULL)
756355b4669Sjacobs 			result = report(job, show_rank, verbose);
757355b4669Sjacobs 
758355b4669Sjacobs 		papiJobFree(job);
759355b4669Sjacobs 	}
760355b4669Sjacobs 
761355b4669Sjacobs 	papiServiceDestroy(svc);
762355b4669Sjacobs 
763355b4669Sjacobs 	return (result);
764355b4669Sjacobs }
765355b4669Sjacobs 
766355b4669Sjacobs static int
767355b4669Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
768355b4669Sjacobs {
769355b4669Sjacobs 	papi_status_t status;
770355b4669Sjacobs 	char *form = NULL;
771355b4669Sjacobs 	void *iter = NULL;
772355b4669Sjacobs 
773355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
774355b4669Sjacobs 					"form-supported", &form);
775355b4669Sjacobs 		status == PAPI_OK;
776355b4669Sjacobs 		status = papiAttributeListGetString(attrs, &iter,
777355b4669Sjacobs 							NULL, &form)) {
778355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
779355b4669Sjacobs 			printf(gettext("form %s is available to you\n"), form);
780355b4669Sjacobs 			if (verbose != 0) {
781355b4669Sjacobs 				char *detail = NULL;
782355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
783355b4669Sjacobs 						"form-supported-detail",
784355b4669Sjacobs 						&detail);
785355b4669Sjacobs 				if (status == PAPI_OK)
786355b4669Sjacobs 					printf("%s\n", detail);
787355b4669Sjacobs 			}
788355b4669Sjacobs 		}
789355b4669Sjacobs 	}
790355b4669Sjacobs 
791355b4669Sjacobs 	return (0);
792355b4669Sjacobs }
793355b4669Sjacobs 
794355b4669Sjacobs static int
795355b4669Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
796355b4669Sjacobs {
797355b4669Sjacobs 	papi_status_t status;
798355b4669Sjacobs 	char *pw = NULL;
799355b4669Sjacobs 	void *iter = NULL;
800355b4669Sjacobs 
801355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
802355b4669Sjacobs 					"pw-supported", &pw);
803355b4669Sjacobs 		status == PAPI_OK;
804355b4669Sjacobs 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
805355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
806355b4669Sjacobs 			printf(gettext("charset %s is available\n"), pw);
807355b4669Sjacobs 			if (verbose != 0) {
808355b4669Sjacobs 				char *info = NULL;
809355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
810355b4669Sjacobs 						"pw-supported-extra", &info);
811355b4669Sjacobs 				if (status == PAPI_OK)
812355b4669Sjacobs 					printf("%s\n", info);
813355b4669Sjacobs 			}
814355b4669Sjacobs 		}
815355b4669Sjacobs 	}
816355b4669Sjacobs 
817355b4669Sjacobs 	return (0);
818355b4669Sjacobs }
819355b4669Sjacobs 
820355b4669Sjacobs static int
821355b4669Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
822355b4669Sjacobs 		papi_encryption_t encryption, int verbose)
823355b4669Sjacobs {
824355b4669Sjacobs 	int result = 0;
825355b4669Sjacobs 	papi_status_t status;
826355b4669Sjacobs 	papi_service_t svc = NULL;
827355b4669Sjacobs 	papi_attribute_t **attrs = NULL;
828355b4669Sjacobs 
829355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
830355b4669Sjacobs 					encryption, NULL);
831355b4669Sjacobs 	if (status != PAPI_OK) {
832355b4669Sjacobs 		papiServiceDestroy(svc);
833355b4669Sjacobs 		return (-1);
834355b4669Sjacobs 	}
835355b4669Sjacobs 
836355b4669Sjacobs 	attrs = papiServiceGetAttributeList(svc);
837355b4669Sjacobs 	if (attrs != NULL) {
838355b4669Sjacobs 		result = report(name, attrs, verbose);
839355b4669Sjacobs 
840355b4669Sjacobs 		if (verbose > 1) {
841355b4669Sjacobs 			printf("\n");
842355b4669Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
843355b4669Sjacobs 			printf("\n");
844355b4669Sjacobs 		}
845355b4669Sjacobs 	}
846355b4669Sjacobs 
847355b4669Sjacobs 	papiServiceDestroy(svc);
848355b4669Sjacobs 
849355b4669Sjacobs 	return (result);
850355b4669Sjacobs }
851355b4669Sjacobs 
852355b4669Sjacobs int
853355b4669Sjacobs main(int ac, char *av[])
854355b4669Sjacobs {
855355b4669Sjacobs 	int exit_code = 0;
856355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
857355b4669Sjacobs 	int rank = 0;
858355b4669Sjacobs 	int verbose = 0;
859355b4669Sjacobs 	int description = 0;
860355b4669Sjacobs 	int c;
861355b4669Sjacobs 	char **argv;
862355b4669Sjacobs 
863355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
864355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
865355b4669Sjacobs 
866355b4669Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
867*91216fe4Swendyp 	for (c = 0; c < ac; c++) {
868*91216fe4Swendyp 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
869*91216fe4Swendyp 			(isalpha(av[c][2]) != 0)) {
870*91216fe4Swendyp 			/* preserve old "-l[po...]" behavior */
871*91216fe4Swendyp 			argv[c] = &av[c][1];
872*91216fe4Swendyp 			argv[c][0] = '-';
873*91216fe4Swendyp 			verbose = 1;
874*91216fe4Swendyp 
875*91216fe4Swendyp 		} else
876355b4669Sjacobs 			argv[c] = av[c];
877*91216fe4Swendyp 	}
878*91216fe4Swendyp 
879355b4669Sjacobs 	argv[c++] = "--";
880355b4669Sjacobs 	ac = c;
881355b4669Sjacobs 
882355b4669Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
883355b4669Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
884355b4669Sjacobs 		switch (c) {
885355b4669Sjacobs 		case 'l':
886355b4669Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
887355b4669Sjacobs 				optarg = "1";
888355b4669Sjacobs 			verbose = atoi(optarg);
889355b4669Sjacobs 			break;
890355b4669Sjacobs 		case 'D':
891355b4669Sjacobs 			description = 1;
892355b4669Sjacobs 			break;
893355b4669Sjacobs 		case 'R':
894355b4669Sjacobs 			rank = 1;
895355b4669Sjacobs 			break;
896355b4669Sjacobs 		case 'E':
897355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
898355b4669Sjacobs 			break;
899355b4669Sjacobs 		default:
900355b4669Sjacobs 			break;
901355b4669Sjacobs 		}
902355b4669Sjacobs 	optind = 1;
903355b4669Sjacobs 
904355b4669Sjacobs 	/* process command line arguments */
905355b4669Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
906355b4669Sjacobs 		switch (c) {	/* these may or may not have an option */
907355b4669Sjacobs 		case 'a':
908355b4669Sjacobs 		case 'c':
909355b4669Sjacobs 		case 'p':
910355b4669Sjacobs 		case 'o':
911355b4669Sjacobs 		case 'R':
912355b4669Sjacobs 		case 'u':
913355b4669Sjacobs 		case 'v':
914355b4669Sjacobs 		case 'l':
915355b4669Sjacobs 		case 'f':
916355b4669Sjacobs 		case 'S':
917355b4669Sjacobs 			if (optarg[0] == '-') {
918355b4669Sjacobs 				/* this check stop a possible infinite loop */
919355b4669Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
920355b4669Sjacobs 					optind--;
921355b4669Sjacobs 				optarg = NULL;
922355b4669Sjacobs 			} else if (strcmp(optarg, "all") == 0)
923355b4669Sjacobs 				optarg = NULL;
924355b4669Sjacobs 		}
925355b4669Sjacobs 
926355b4669Sjacobs 		switch (c) {
927355b4669Sjacobs 		case 'a':
928355b4669Sjacobs 			exit_code += printer_query(optarg, report_accepting,
929355b4669Sjacobs 						encryption, verbose, 0);
930355b4669Sjacobs 			break;
931355b4669Sjacobs 		case 'c':
932355b4669Sjacobs 			exit_code += printer_query(optarg, report_class,
933355b4669Sjacobs 						encryption, verbose, 0);
934355b4669Sjacobs 			break;
935355b4669Sjacobs 		case 'p':
936355b4669Sjacobs 			exit_code += printer_query(optarg, report_printer,
937355b4669Sjacobs 						encryption, verbose,
938355b4669Sjacobs 						description);
939355b4669Sjacobs 			break;
940355b4669Sjacobs 		case 'd':
941355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
942355b4669Sjacobs 			break;
943355b4669Sjacobs 		case 'r':
944355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
945355b4669Sjacobs 			break;
946355b4669Sjacobs 		case 'u':
947355b4669Sjacobs 			if (optarg != NULL)
948355b4669Sjacobs 				users = strsplit(optarg, ", \n");
949355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
950355b4669Sjacobs 						encryption, rank, verbose);
951355b4669Sjacobs 			if (users != NULL) {
952355b4669Sjacobs 				free(users);
953355b4669Sjacobs 				users = NULL;
954355b4669Sjacobs 			}
955355b4669Sjacobs 			break;
956355b4669Sjacobs 		case 'v':
957355b4669Sjacobs 			exit_code += printer_query(optarg, report_device,
958355b4669Sjacobs 						encryption, verbose, 0);
959355b4669Sjacobs 			break;
960355b4669Sjacobs 		case 'o':
961355b4669Sjacobs 			exit_code += job_query(optarg, report_job,
962355b4669Sjacobs 						encryption, rank, verbose);
963355b4669Sjacobs 			break;
964355b4669Sjacobs 		case 'f':
965355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
966355b4669Sjacobs 						encryption, verbose);
967355b4669Sjacobs 			break;
968355b4669Sjacobs 		case 'S':
969355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
970355b4669Sjacobs 						encryption, verbose);
971355b4669Sjacobs 			break;
972355b4669Sjacobs 		case 's':
973355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
974355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
975355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
976355b4669Sjacobs 						encryption, verbose, 0);
977355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
978355b4669Sjacobs 						encryption, verbose, 0);
979355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
980355b4669Sjacobs 						encryption, verbose);
981355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
982355b4669Sjacobs 						encryption, verbose);
983355b4669Sjacobs 			break;
984355b4669Sjacobs 		case 't':
985355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
986355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
987355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
988355b4669Sjacobs 						encryption, verbose, 0);
989355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
990355b4669Sjacobs 						encryption, verbose, 0);
991355b4669Sjacobs 			exit_code += printer_query(NULL, report_accepting,
992355b4669Sjacobs 						encryption, verbose, 0);
993355b4669Sjacobs 			exit_code += printer_query(NULL, report_printer,
994355b4669Sjacobs 						encryption, verbose, 0);
995355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
996355b4669Sjacobs 						encryption, verbose);
997355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
998355b4669Sjacobs 						encryption, verbose);
999355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
1000355b4669Sjacobs 						encryption, rank, verbose);
1001355b4669Sjacobs 			break;
1002355b4669Sjacobs 		case 'L':	/* local-only, ignored */
1003355b4669Sjacobs 		case 'l':	/* increased verbose level in first pass */
1004355b4669Sjacobs 		case 'D':	/* set "description" flag in first pass */
1005355b4669Sjacobs 		case 'R':	/* set "rank" flag in first pass */
1006355b4669Sjacobs 		case 'E':	/* set encryption in the first pass */
1007355b4669Sjacobs 			break;
1008355b4669Sjacobs 		default:
1009355b4669Sjacobs 			usage(av[0]);
1010355b4669Sjacobs 		}
1011355b4669Sjacobs 	}
1012355b4669Sjacobs 	ac--;
1013355b4669Sjacobs 
1014355b4669Sjacobs 	if (ac == 1) {	/* report on my jobs */
1015355b4669Sjacobs 		struct passwd *pw = getpwuid(getuid());
1016355b4669Sjacobs 
1017355b4669Sjacobs 		if (pw != NULL)
1018355b4669Sjacobs 			users = strsplit(pw->pw_name, "");
1019355b4669Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
1020355b4669Sjacobs 					rank, verbose);
1021355b4669Sjacobs 		if (users != NULL) {
1022355b4669Sjacobs 			free(users);
1023355b4669Sjacobs 			users = NULL;
1024355b4669Sjacobs 		}
1025355b4669Sjacobs 	} else {
1026355b4669Sjacobs 		for (c = optind; c < ac; c++)
1027355b4669Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
1028355b4669Sjacobs 					rank, verbose);
1029355b4669Sjacobs 	}
1030355b4669Sjacobs 
1031355b4669Sjacobs 
1032355b4669Sjacobs 	if (exit_code != 0)
1033355b4669Sjacobs 		exit_code = 1;
1034355b4669Sjacobs 
1035355b4669Sjacobs 	return (exit_code);
1036355b4669Sjacobs }
1037