xref: /titanic_50/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs 
22*355b4669Sjacobs /*
23*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*355b4669Sjacobs  * Use is subject to license terms.
25*355b4669Sjacobs  *
26*355b4669Sjacobs  */
27*355b4669Sjacobs 
28*355b4669Sjacobs /* $Id: lpstat.c 173 2006-05-25 04:52:06Z njacobs $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*355b4669Sjacobs 
32*355b4669Sjacobs #include <stdio.h>
33*355b4669Sjacobs #include <stdlib.h>
34*355b4669Sjacobs #include <unistd.h>
35*355b4669Sjacobs #include <string.h>
36*355b4669Sjacobs #include <locale.h>
37*355b4669Sjacobs #include <libintl.h>
38*355b4669Sjacobs #include <pwd.h>
39*355b4669Sjacobs #include <papi.h>
40*355b4669Sjacobs #include <uri.h>
41*355b4669Sjacobs #include "common.h"
42*355b4669Sjacobs 
43*355b4669Sjacobs static void
44*355b4669Sjacobs usage(char *program)
45*355b4669Sjacobs {
46*355b4669Sjacobs 	char *name;
47*355b4669Sjacobs 
48*355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
49*355b4669Sjacobs 		name = program;
50*355b4669Sjacobs 	else
51*355b4669Sjacobs 		name++;
52*355b4669Sjacobs 
53*355b4669Sjacobs 	fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
54*355b4669Sjacobs 		"[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
55*355b4669Sjacobs 		"[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
56*355b4669Sjacobs 		"[-f [list] [-l]] [-u list]\n"),
57*355b4669Sjacobs 		name);
58*355b4669Sjacobs 	exit(1);
59*355b4669Sjacobs }
60*355b4669Sjacobs 
61*355b4669Sjacobs static char *
62*355b4669Sjacobs nctime(time_t *t)
63*355b4669Sjacobs {
64*355b4669Sjacobs 	static char buf[64];
65*355b4669Sjacobs 	struct tm *tm = localtime(t);
66*355b4669Sjacobs 
67*355b4669Sjacobs 	(void) strftime(buf, sizeof (buf), "%c", tm);
68*355b4669Sjacobs 
69*355b4669Sjacobs 	return (buf);
70*355b4669Sjacobs }
71*355b4669Sjacobs 
72*355b4669Sjacobs static char *
73*355b4669Sjacobs printer_name(papi_printer_t printer)
74*355b4669Sjacobs {
75*355b4669Sjacobs 	papi_attribute_t **attributes = papiPrinterGetAttributeList(printer);
76*355b4669Sjacobs 	char *result = NULL;
77*355b4669Sjacobs 
78*355b4669Sjacobs 	if (attributes != NULL)
79*355b4669Sjacobs 		papiAttributeListGetString(attributes, NULL,
80*355b4669Sjacobs 				"printer-name", &result);
81*355b4669Sjacobs 
82*355b4669Sjacobs 	return (result);
83*355b4669Sjacobs }
84*355b4669Sjacobs 
85*355b4669Sjacobs static int
86*355b4669Sjacobs lpstat_default_printer(papi_encryption_t encryption)
87*355b4669Sjacobs {
88*355b4669Sjacobs 	papi_status_t status;
89*355b4669Sjacobs 	papi_service_t svc = NULL;
90*355b4669Sjacobs 	papi_printer_t p = NULL;
91*355b4669Sjacobs 	char *name = NULL;
92*355b4669Sjacobs 
93*355b4669Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback,
94*355b4669Sjacobs 					encryption, NULL);
95*355b4669Sjacobs 	if (status == PAPI_OK) {
96*355b4669Sjacobs 		char *req[] = { "printer-name", NULL };
97*355b4669Sjacobs 
98*355b4669Sjacobs 		status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p);
99*355b4669Sjacobs 		if (p != NULL)
100*355b4669Sjacobs 			name = printer_name(p);
101*355b4669Sjacobs 	}
102*355b4669Sjacobs 	if (name != NULL)
103*355b4669Sjacobs 		printf(gettext("system default printer: %s\n"), name);
104*355b4669Sjacobs 	else
105*355b4669Sjacobs 		printf(gettext("no system default destination\n"));
106*355b4669Sjacobs 	papiPrinterFree(p);
107*355b4669Sjacobs 	papiServiceDestroy(svc);
108*355b4669Sjacobs 
109*355b4669Sjacobs 	return (0);
110*355b4669Sjacobs }
111*355b4669Sjacobs 
112*355b4669Sjacobs static int
113*355b4669Sjacobs lpstat_service_status(papi_encryption_t encryption)
114*355b4669Sjacobs {
115*355b4669Sjacobs 	int result = 0;
116*355b4669Sjacobs 	papi_status_t status;
117*355b4669Sjacobs 	papi_service_t svc = NULL;
118*355b4669Sjacobs 	char *name = NULL;
119*355b4669Sjacobs 
120*355b4669Sjacobs 	if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
121*355b4669Sjacobs 	    ((name = getenv("IPP_SERVER")) == NULL) &&
122*355b4669Sjacobs 	    ((name = getenv("CUPS_SERVER")) == NULL))
123*355b4669Sjacobs 		name = DEFAULT_SERVICE_URI;
124*355b4669Sjacobs 
125*355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
126*355b4669Sjacobs 					encryption, NULL);
127*355b4669Sjacobs 	if (status != PAPI_OK) {
128*355b4669Sjacobs 		printf(gettext("scheduler is not running\n"));
129*355b4669Sjacobs 		result = -1;
130*355b4669Sjacobs 	} else
131*355b4669Sjacobs 		printf(gettext("scheduler is running\n"));
132*355b4669Sjacobs 	papiServiceDestroy(svc);
133*355b4669Sjacobs 
134*355b4669Sjacobs 	return (result);
135*355b4669Sjacobs }
136*355b4669Sjacobs 
137*355b4669Sjacobs static char *
138*355b4669Sjacobs get_device_uri(papi_service_t svc, char *name)
139*355b4669Sjacobs {
140*355b4669Sjacobs 	papi_status_t status;
141*355b4669Sjacobs 	papi_printer_t p = NULL;
142*355b4669Sjacobs 	char *keys[] = { "device-uri", NULL };
143*355b4669Sjacobs 	char *result = NULL;
144*355b4669Sjacobs 
145*355b4669Sjacobs 	status = papiPrinterQuery(svc, name, keys, NULL, &p);
146*355b4669Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
147*355b4669Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
148*355b4669Sjacobs 
149*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
150*355b4669Sjacobs 					"device-uri", &result);
151*355b4669Sjacobs 		if (result != NULL)
152*355b4669Sjacobs 			result = strdup(result);
153*355b4669Sjacobs 
154*355b4669Sjacobs 		papiPrinterFree(p);
155*355b4669Sjacobs 	}
156*355b4669Sjacobs 
157*355b4669Sjacobs 	return (result);
158*355b4669Sjacobs }
159*355b4669Sjacobs 
160*355b4669Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
161*355b4669Sjacobs 					NULL };
162*355b4669Sjacobs /* ARGSUSED2 */
163*355b4669Sjacobs static int
164*355b4669Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
165*355b4669Sjacobs 		int verbose, int description)
166*355b4669Sjacobs {
167*355b4669Sjacobs 	papi_status_t status;
168*355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
169*355b4669Sjacobs 	char *uri = NULL;
170*355b4669Sjacobs 	char *device = NULL;
171*355b4669Sjacobs 	uri_t *u = NULL;
172*355b4669Sjacobs 
173*355b4669Sjacobs 	if (name == NULL) {
174*355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
175*355b4669Sjacobs 					"printer-name", &name);
176*355b4669Sjacobs 		if (status != PAPI_OK)
177*355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
178*355b4669Sjacobs 					"printer-uri-supported", &name);
179*355b4669Sjacobs 	}
180*355b4669Sjacobs 
181*355b4669Sjacobs 	if (name == NULL)
182*355b4669Sjacobs 		return (-1);
183*355b4669Sjacobs 
184*355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
185*355b4669Sjacobs 					"printer-uri-supported", &uri);
186*355b4669Sjacobs 
187*355b4669Sjacobs 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
188*355b4669Sjacobs 		char *nodename = localhostname();
189*355b4669Sjacobs 
190*355b4669Sjacobs 		if ((u->host == NULL) ||
191*355b4669Sjacobs 		    (strcasecmp(u->host, "localhost") == 0) ||
192*355b4669Sjacobs 		    (strcasecmp(u->host, nodename) == 0))
193*355b4669Sjacobs 			device = get_device_uri(svc, name);
194*355b4669Sjacobs 
195*355b4669Sjacobs 		if (device != NULL) {
196*355b4669Sjacobs 			printf(gettext("device for %s: %s\n"), name, device);
197*355b4669Sjacobs 			return (0);
198*355b4669Sjacobs 		} else if (uri != NULL) {
199*355b4669Sjacobs 			printf(gettext("system for %s: %s (as %s)\n"), name,
200*355b4669Sjacobs 				u->host, uri);
201*355b4669Sjacobs 			return (0);
202*355b4669Sjacobs 		}
203*355b4669Sjacobs 
204*355b4669Sjacobs 		uri_free(u);
205*355b4669Sjacobs 	}
206*355b4669Sjacobs 
207*355b4669Sjacobs 	return (0);
208*355b4669Sjacobs }
209*355b4669Sjacobs 
210*355b4669Sjacobs static char *report_accepting_keys[] = { "printer-name",
211*355b4669Sjacobs 			"printer-uri-supported", "printer-is-accepting-jobs",
212*355b4669Sjacobs 			"printer-up-time", "printer-state-time",
213*355b4669Sjacobs 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
214*355b4669Sjacobs /* ARGSUSED2 */
215*355b4669Sjacobs static int
216*355b4669Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
217*355b4669Sjacobs 		int verbose, int description)
218*355b4669Sjacobs {
219*355b4669Sjacobs 	papi_status_t status;
220*355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
221*355b4669Sjacobs 	time_t curr;
222*355b4669Sjacobs 	char boolean = PAPI_FALSE;
223*355b4669Sjacobs 
224*355b4669Sjacobs 	if (name == NULL) {
225*355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
226*355b4669Sjacobs 					"printer-name", &name);
227*355b4669Sjacobs 		if (status != PAPI_OK)
228*355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
229*355b4669Sjacobs 					"printer-uri-supported", &name);
230*355b4669Sjacobs 	}
231*355b4669Sjacobs 	if (name == NULL)
232*355b4669Sjacobs 		return (-1);
233*355b4669Sjacobs 
234*355b4669Sjacobs 	(void) papiAttributeListGetBoolean(attrs, NULL,
235*355b4669Sjacobs 				"printer-is-accepting-jobs", &boolean);
236*355b4669Sjacobs 	(void) time(&curr);
237*355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
238*355b4669Sjacobs 					"printer-up-time", &curr);
239*355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
240*355b4669Sjacobs 					"printer-state-time", &curr);
241*355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
242*355b4669Sjacobs 					"lpsched-reject-date", &curr);
243*355b4669Sjacobs 
244*355b4669Sjacobs 	if (boolean == PAPI_TRUE) {
245*355b4669Sjacobs 		printf(gettext("%s accepting requests since %s\n"),
246*355b4669Sjacobs 			name, nctime(&curr));
247*355b4669Sjacobs 	} else {
248*355b4669Sjacobs 		char *reason = "unknown reason";
249*355b4669Sjacobs 
250*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
251*355b4669Sjacobs 					"lpsched-reject-reason", &reason);
252*355b4669Sjacobs 
253*355b4669Sjacobs 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
254*355b4669Sjacobs 			name, nctime(&curr), reason);
255*355b4669Sjacobs 	}
256*355b4669Sjacobs 
257*355b4669Sjacobs 	return (0);
258*355b4669Sjacobs }
259*355b4669Sjacobs 
260*355b4669Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
261*355b4669Sjacobs 					"member-names", NULL };
262*355b4669Sjacobs /* ARGSUSED2 */
263*355b4669Sjacobs static int
264*355b4669Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
265*355b4669Sjacobs 		int verbose, int description)
266*355b4669Sjacobs {
267*355b4669Sjacobs 	papi_status_t status;
268*355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
269*355b4669Sjacobs 	char *member = NULL;
270*355b4669Sjacobs 	void *iter = NULL;
271*355b4669Sjacobs 
272*355b4669Sjacobs 	status = papiAttributeListGetString(attrs, &iter,
273*355b4669Sjacobs 				"member-names", &member);
274*355b4669Sjacobs 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
275*355b4669Sjacobs 		return (0);
276*355b4669Sjacobs 
277*355b4669Sjacobs 	if (name == NULL) {
278*355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
279*355b4669Sjacobs 					"printer-name", &name);
280*355b4669Sjacobs 		if (status != PAPI_OK)
281*355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
282*355b4669Sjacobs 					"printer-uri-supported", &name);
283*355b4669Sjacobs 	}
284*355b4669Sjacobs 	if (name == NULL)
285*355b4669Sjacobs 		return (-1);
286*355b4669Sjacobs 
287*355b4669Sjacobs 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
288*355b4669Sjacobs 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
289*355b4669Sjacobs 			== PAPI_OK)
290*355b4669Sjacobs 		printf("\t%s\n", member);
291*355b4669Sjacobs 
292*355b4669Sjacobs 	return (0);
293*355b4669Sjacobs }
294*355b4669Sjacobs 
295*355b4669Sjacobs static char *report_printer_keys[] = { "printer-name",
296*355b4669Sjacobs 			"printer-uri-supported", "printer-state",
297*355b4669Sjacobs 			"printer-up-time", "printer-state-time",
298*355b4669Sjacobs 			"lpsched-disable-date", "printer-state-reasons",
299*355b4669Sjacobs 			"lpsched-disable-reason", NULL };
300*355b4669Sjacobs /* ARGSUSED2 */
301*355b4669Sjacobs static int
302*355b4669Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
303*355b4669Sjacobs 		int verbose, int description)
304*355b4669Sjacobs {
305*355b4669Sjacobs 	papi_status_t status;
306*355b4669Sjacobs 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
307*355b4669Sjacobs 	time_t curr;
308*355b4669Sjacobs 	int32_t pstat = 0;
309*355b4669Sjacobs 	char *member = NULL;
310*355b4669Sjacobs 
311*355b4669Sjacobs 	status = papiAttributeListGetString(attrs, NULL,
312*355b4669Sjacobs 				"member-names", &member);
313*355b4669Sjacobs 	if (status == PAPI_OK)	/* it's a class */
314*355b4669Sjacobs 		return (0);
315*355b4669Sjacobs 
316*355b4669Sjacobs 	if (name == NULL) {
317*355b4669Sjacobs 		status = papiAttributeListGetString(attrs, NULL,
318*355b4669Sjacobs 					"printer-name", &name);
319*355b4669Sjacobs 		if (status != PAPI_OK)
320*355b4669Sjacobs 			status = papiAttributeListGetString(attrs, NULL,
321*355b4669Sjacobs 					"printer-uri-supported", &name);
322*355b4669Sjacobs 	}
323*355b4669Sjacobs 	if (name == NULL)
324*355b4669Sjacobs 		return (-1);
325*355b4669Sjacobs 
326*355b4669Sjacobs 	printf(gettext("printer %s "), name);
327*355b4669Sjacobs 
328*355b4669Sjacobs 	status = papiAttributeListGetInteger(attrs, NULL,
329*355b4669Sjacobs 					"printer-state", &pstat);
330*355b4669Sjacobs 
331*355b4669Sjacobs 	switch (pstat) {
332*355b4669Sjacobs 	case 0x03:	/* idle */
333*355b4669Sjacobs 		printf(gettext("idle. enabled"));
334*355b4669Sjacobs 		break;
335*355b4669Sjacobs 	case 0x04: {	/* processing */
336*355b4669Sjacobs 		char *requested[] = { "job-id", NULL };
337*355b4669Sjacobs 		papi_job_t *j = NULL;
338*355b4669Sjacobs 		int32_t jobid = 0;
339*355b4669Sjacobs 
340*355b4669Sjacobs 		(void) papiPrinterListJobs(svc, name, requested, 0, 1, &j);
341*355b4669Sjacobs 		if ((j != NULL) && (j[0] != NULL))
342*355b4669Sjacobs 			jobid = papiJobGetId(j[0]);
343*355b4669Sjacobs 		papiJobListFree(j);
344*355b4669Sjacobs 
345*355b4669Sjacobs 		printf(gettext("now printing %s-%d. enabled"), name, jobid);
346*355b4669Sjacobs 		}
347*355b4669Sjacobs 		break;
348*355b4669Sjacobs 	case 0x05:	/* stopped */
349*355b4669Sjacobs 		printf(gettext("disabled"));
350*355b4669Sjacobs 		break;
351*355b4669Sjacobs 	default:
352*355b4669Sjacobs 		printf(gettext("unknown state(0x%x)."), pstat);
353*355b4669Sjacobs 		break;
354*355b4669Sjacobs 	}
355*355b4669Sjacobs 
356*355b4669Sjacobs 	(void) time(&curr);
357*355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
358*355b4669Sjacobs 					"printer-up-time", &curr);
359*355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
360*355b4669Sjacobs 					"printer-state-time", &curr);
361*355b4669Sjacobs 	(void) papiAttributeListGetDatetime(attrs, NULL,
362*355b4669Sjacobs 					"lpsched-disable-date", &curr);
363*355b4669Sjacobs 	printf(gettext(" since %s. available.\n"), nctime(&curr));
364*355b4669Sjacobs 
365*355b4669Sjacobs 	if (pstat == 0x05) {
366*355b4669Sjacobs 		char *reason = "unknown reason";
367*355b4669Sjacobs 
368*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
369*355b4669Sjacobs 					"printer-state-reasons", &reason);
370*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
371*355b4669Sjacobs 					"lpsched-disable-reason", &reason);
372*355b4669Sjacobs 		printf(gettext("\t%s\n"), reason);
373*355b4669Sjacobs 	}
374*355b4669Sjacobs 
375*355b4669Sjacobs 	if (verbose == 1) {
376*355b4669Sjacobs 		void *iter;
377*355b4669Sjacobs 		char *str;
378*355b4669Sjacobs 
379*355b4669Sjacobs 		str = "";
380*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
381*355b4669Sjacobs 					"form-ready", &str);
382*355b4669Sjacobs 		printf(gettext("\tForm mounted: %s\n"), str);
383*355b4669Sjacobs 
384*355b4669Sjacobs 		str = "";
385*355b4669Sjacobs 		iter = NULL;
386*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
387*355b4669Sjacobs 					"document-format-supported", &str);
388*355b4669Sjacobs 		printf(gettext("\tContent types: %s"), str);
389*355b4669Sjacobs 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
390*355b4669Sjacobs 				== PAPI_OK)
391*355b4669Sjacobs 			printf(", %s", str);
392*355b4669Sjacobs 		printf("\n");
393*355b4669Sjacobs 
394*355b4669Sjacobs 		str = "";
395*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
396*355b4669Sjacobs 					"printer-info", &str);
397*355b4669Sjacobs 		printf(gettext("\tDescription: %s\n"), str);
398*355b4669Sjacobs 
399*355b4669Sjacobs 		str = "";
400*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
401*355b4669Sjacobs 					"lpsched-dial-info", &str);
402*355b4669Sjacobs 		printf(gettext("\tConnection: %s\n"),
403*355b4669Sjacobs 			((str[0] != '\0') ? gettext("direct") : str));
404*355b4669Sjacobs 
405*355b4669Sjacobs 		str = "";
406*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
407*355b4669Sjacobs 					"lpsched-interface-script", &str);
408*355b4669Sjacobs 		printf(gettext("\tInterface: %s\n"), str);
409*355b4669Sjacobs 
410*355b4669Sjacobs 		str = NULL;
411*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
412*355b4669Sjacobs 					"ppd-file-uri", &str);
413*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
414*355b4669Sjacobs 					"lpsched-ppd-source-path", &str);
415*355b4669Sjacobs 		if (str != NULL)
416*355b4669Sjacobs 			printf(gettext("\tPPD: %s\n"), str);
417*355b4669Sjacobs 
418*355b4669Sjacobs 		str = NULL;
419*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
420*355b4669Sjacobs 					"lpsched-fault-alert-command", &str);
421*355b4669Sjacobs 		if (str != NULL)
422*355b4669Sjacobs 			printf(gettext("\tOn fault: %s\n"), str);
423*355b4669Sjacobs 
424*355b4669Sjacobs 		str = "";
425*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
426*355b4669Sjacobs 					"lpsched-fault-recovery", &str);
427*355b4669Sjacobs 		printf(gettext("\tAfter fault: %s\n"),
428*355b4669Sjacobs 			((str[0] == '\0') ? gettext("continue") : str));
429*355b4669Sjacobs 
430*355b4669Sjacobs 		str = "(all)";
431*355b4669Sjacobs 		iter = NULL;
432*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
433*355b4669Sjacobs 					"requesting-user-name-allowed", &str);
434*355b4669Sjacobs 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
435*355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
436*355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
437*355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
438*355b4669Sjacobs 					&str) == PAPI_OK)
439*355b4669Sjacobs 				printf("\t\t%s\n", str);
440*355b4669Sjacobs 
441*355b4669Sjacobs 		str = NULL;
442*355b4669Sjacobs 		iter = NULL;
443*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
444*355b4669Sjacobs 					"requesting-user-name-denied", &str);
445*355b4669Sjacobs 		if (str != NULL) {
446*355b4669Sjacobs 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
447*355b4669Sjacobs 				((str[0] == '\0') ? gettext("(none)") : str));
448*355b4669Sjacobs 			if ((str != NULL) && (str[0] != '\0'))
449*355b4669Sjacobs 				while (papiAttributeListGetString(attrs, &iter,
450*355b4669Sjacobs 						NULL, &str) == PAPI_OK)
451*355b4669Sjacobs 					printf("\t\t%s\n", str);
452*355b4669Sjacobs 		}
453*355b4669Sjacobs 
454*355b4669Sjacobs 		str = "(none)";
455*355b4669Sjacobs 		iter = NULL;
456*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
457*355b4669Sjacobs 					"form-supported", &str);
458*355b4669Sjacobs 		printf(gettext("\tForms allowed:\n\t\t%s\n"),
459*355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
460*355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
461*355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
462*355b4669Sjacobs 					&str) == PAPI_OK)
463*355b4669Sjacobs 				printf("\t\t%s\n", str);
464*355b4669Sjacobs 
465*355b4669Sjacobs 		str = "";
466*355b4669Sjacobs 		iter = NULL;
467*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
468*355b4669Sjacobs 					"media-supported", &str);
469*355b4669Sjacobs 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
470*355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
471*355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
472*355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
473*355b4669Sjacobs 					&str) == PAPI_OK)
474*355b4669Sjacobs 				printf("\t\t%s\n", str);
475*355b4669Sjacobs 
476*355b4669Sjacobs 		str = "";
477*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
478*355b4669Sjacobs 					"job-sheets-supported", &str);
479*355b4669Sjacobs 		printf(gettext("\tBanner %s\n"),
480*355b4669Sjacobs 			(strcasecmp(str, "none") == 0 ?
481*355b4669Sjacobs 				gettext("not required") : gettext("required")));
482*355b4669Sjacobs 
483*355b4669Sjacobs 		str = "";
484*355b4669Sjacobs 		iter = NULL;
485*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
486*355b4669Sjacobs 					"lpsched-print-wheels", &str);
487*355b4669Sjacobs 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
488*355b4669Sjacobs 			((str[0] == '\0') ? gettext("(none)") : str));
489*355b4669Sjacobs 		if ((str != NULL) && (str[0] != '\0'))
490*355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
491*355b4669Sjacobs 					&str) == PAPI_OK)
492*355b4669Sjacobs 				printf("\t\t%s\n", str);
493*355b4669Sjacobs 
494*355b4669Sjacobs 		printf(gettext("\tDefault pitch:\n"));
495*355b4669Sjacobs 		printf(gettext("\tDefault page size:\n"));
496*355b4669Sjacobs 		printf(gettext("\tDefault port setting:\n"));
497*355b4669Sjacobs 
498*355b4669Sjacobs 		str = "";
499*355b4669Sjacobs 		iter = NULL;
500*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, &iter,
501*355b4669Sjacobs 					"lpsched-options", &str);
502*355b4669Sjacobs 		if (str != NULL) {
503*355b4669Sjacobs 			printf(gettext("\tOptions: %s"), str);
504*355b4669Sjacobs 			while (papiAttributeListGetString(attrs, &iter, NULL,
505*355b4669Sjacobs 						&str) == PAPI_OK)
506*355b4669Sjacobs 				printf(", %s", str);
507*355b4669Sjacobs 			printf("\n");
508*355b4669Sjacobs 		}
509*355b4669Sjacobs 
510*355b4669Sjacobs 	} else if (description == 1) {
511*355b4669Sjacobs 		char *str = "";
512*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
513*355b4669Sjacobs 					"printer-description", &str);
514*355b4669Sjacobs 		printf(gettext("\tDescription: %s\n"), str);
515*355b4669Sjacobs 	} else if (verbose > 1)
516*355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
517*355b4669Sjacobs 
518*355b4669Sjacobs 	if (verbose > 0)
519*355b4669Sjacobs 		printf("\n");
520*355b4669Sjacobs 
521*355b4669Sjacobs 	return (0);
522*355b4669Sjacobs }
523*355b4669Sjacobs 
524*355b4669Sjacobs static int
525*355b4669Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
526*355b4669Sjacobs 					int, int), papi_encryption_t encryption,
527*355b4669Sjacobs 		int verbose, int description)
528*355b4669Sjacobs {
529*355b4669Sjacobs 	int result = 0;
530*355b4669Sjacobs 	papi_status_t status;
531*355b4669Sjacobs 	papi_service_t svc = NULL;
532*355b4669Sjacobs 
533*355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
534*355b4669Sjacobs 					encryption, NULL);
535*355b4669Sjacobs 	if (status != PAPI_OK) {
536*355b4669Sjacobs 		fprintf(stderr, gettext(
537*355b4669Sjacobs 			"Failed to contact service for %s: %s\n"),
538*355b4669Sjacobs 			name ? name : "(NULL)",
539*355b4669Sjacobs 			verbose_papi_message(svc, status));
540*355b4669Sjacobs 		papiServiceDestroy(svc);
541*355b4669Sjacobs 		return (-1);
542*355b4669Sjacobs 	}
543*355b4669Sjacobs 
544*355b4669Sjacobs 	if (name == NULL) { /* all */
545*355b4669Sjacobs 		char **interest = interest_list(svc);
546*355b4669Sjacobs 
547*355b4669Sjacobs 		if (interest != NULL) {
548*355b4669Sjacobs 			int i;
549*355b4669Sjacobs 
550*355b4669Sjacobs 			for (i = 0; interest[i] != NULL; i++)
551*355b4669Sjacobs 				result += printer_query(interest[i], report,
552*355b4669Sjacobs 							encryption, verbose,
553*355b4669Sjacobs 							description);
554*355b4669Sjacobs 		}
555*355b4669Sjacobs 	} else {
556*355b4669Sjacobs 		papi_printer_t printer = NULL;
557*355b4669Sjacobs 		char **keys = NULL;
558*355b4669Sjacobs 
559*355b4669Sjacobs 		/*
560*355b4669Sjacobs 		 * Limit the query to only required data to reduce the need
561*355b4669Sjacobs 		 * to go remote for information.
562*355b4669Sjacobs 		 */
563*355b4669Sjacobs 		if (report == report_device)
564*355b4669Sjacobs 			keys = report_device_keys;
565*355b4669Sjacobs 		else if (report == report_class)
566*355b4669Sjacobs 			keys = report_class_keys;
567*355b4669Sjacobs 		else if (report == report_accepting)
568*355b4669Sjacobs 			keys = report_accepting_keys;
569*355b4669Sjacobs 		else if ((report == report_printer) && (verbose == 0))
570*355b4669Sjacobs 			keys = report_printer_keys;
571*355b4669Sjacobs 
572*355b4669Sjacobs 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
573*355b4669Sjacobs 		if (status != PAPI_OK) {
574*355b4669Sjacobs 			fprintf(stderr, gettext(
575*355b4669Sjacobs 				"Failed to get printer info for %s: %s\n"),
576*355b4669Sjacobs 				name, verbose_papi_message(svc, status));
577*355b4669Sjacobs 			papiServiceDestroy(svc);
578*355b4669Sjacobs 			return (-1);
579*355b4669Sjacobs 		}
580*355b4669Sjacobs 
581*355b4669Sjacobs 		if (printer != NULL)
582*355b4669Sjacobs 			result = report(svc, name, printer, verbose,
583*355b4669Sjacobs 					description);
584*355b4669Sjacobs 
585*355b4669Sjacobs 		papiPrinterFree(printer);
586*355b4669Sjacobs 	}
587*355b4669Sjacobs 
588*355b4669Sjacobs 	papiServiceDestroy(svc);
589*355b4669Sjacobs 
590*355b4669Sjacobs 	return (result);
591*355b4669Sjacobs }
592*355b4669Sjacobs 
593*355b4669Sjacobs static int
594*355b4669Sjacobs match_user(char *user, char **list)
595*355b4669Sjacobs {
596*355b4669Sjacobs 	int i;
597*355b4669Sjacobs 
598*355b4669Sjacobs 	for (i = 0; list[i] != NULL; i++) {
599*355b4669Sjacobs 		if (strcmp(user, list[i]) == 0)
600*355b4669Sjacobs 			return (0);
601*355b4669Sjacobs 	}
602*355b4669Sjacobs 
603*355b4669Sjacobs 	return (-1);
604*355b4669Sjacobs }
605*355b4669Sjacobs 
606*355b4669Sjacobs static char **users = NULL;
607*355b4669Sjacobs 
608*355b4669Sjacobs static int
609*355b4669Sjacobs report_job(papi_job_t job, int show_rank, int verbose)
610*355b4669Sjacobs {
611*355b4669Sjacobs 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
612*355b4669Sjacobs 	time_t clock = 0;
613*355b4669Sjacobs 	char date[24];
614*355b4669Sjacobs 	char request[26];
615*355b4669Sjacobs 	char *user = "unknown";
616*355b4669Sjacobs 	int32_t size = 0;
617*355b4669Sjacobs 	int32_t jstate = 0;
618*355b4669Sjacobs 
619*355b4669Sjacobs 	char *destination = "unknown";
620*355b4669Sjacobs 	int32_t id = -1;
621*355b4669Sjacobs 
622*355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
623*355b4669Sjacobs 				"job-originating-user-name", &user);
624*355b4669Sjacobs 
625*355b4669Sjacobs 	if ((users != NULL) && (match_user(user, users) < 0))
626*355b4669Sjacobs 		return (0);
627*355b4669Sjacobs 
628*355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
629*355b4669Sjacobs 	size *= 1024;	/* for the approximate byte size */
630*355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
631*355b4669Sjacobs 
632*355b4669Sjacobs 	(void) time(&clock);
633*355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
634*355b4669Sjacobs 				"time-at-creation", (int32_t *)&clock);
635*355b4669Sjacobs 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
636*355b4669Sjacobs 
637*355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
638*355b4669Sjacobs 				"job-printer-uri", &destination);
639*355b4669Sjacobs 	(void) papiAttributeListGetString(attrs, NULL,
640*355b4669Sjacobs 				"printer-name", &destination);
641*355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
642*355b4669Sjacobs 				"job-id", &id);
643*355b4669Sjacobs 	snprintf(request, sizeof (request), "%s-%d", destination, id);
644*355b4669Sjacobs 
645*355b4669Sjacobs 	if (show_rank != 0) {
646*355b4669Sjacobs 		int32_t rank = -1;
647*355b4669Sjacobs 
648*355b4669Sjacobs 		(void) papiAttributeListGetInteger(attrs, NULL,
649*355b4669Sjacobs 				"number-of-intervening-jobs", &rank);
650*355b4669Sjacobs 		rank++;
651*355b4669Sjacobs 
652*355b4669Sjacobs 		printf("%3d %-21s %-14s %7ld %s",
653*355b4669Sjacobs 			rank, request, user, size, date);
654*355b4669Sjacobs 	} else
655*355b4669Sjacobs 		printf("%-23s %-14s %7ld   %s", request, user, size, date);
656*355b4669Sjacobs 
657*355b4669Sjacobs 	(void) papiAttributeListGetInteger(attrs, NULL,
658*355b4669Sjacobs 				"job-state", &jstate);
659*355b4669Sjacobs 	if (jstate == 0x04)
660*355b4669Sjacobs 		printf(gettext(", being held"));
661*355b4669Sjacobs 	else if (jstate == 0x07)
662*355b4669Sjacobs 		printf(gettext(", cancelled"));
663*355b4669Sjacobs 	else if (jstate == 0x09)
664*355b4669Sjacobs 		printf(gettext(", complete"));
665*355b4669Sjacobs 
666*355b4669Sjacobs 	if (verbose == 1) {
667*355b4669Sjacobs 		(void) papiAttributeListGetString(attrs, NULL,
668*355b4669Sjacobs 				"output-device-assigned", &destination);
669*355b4669Sjacobs 		printf("\n\t assigned %s", destination);
670*355b4669Sjacobs 	} else if (verbose > 1) {
671*355b4669Sjacobs 		printf("\n");
672*355b4669Sjacobs 		papiAttributeListPrint(stdout, attrs, "\t");
673*355b4669Sjacobs 	}
674*355b4669Sjacobs 
675*355b4669Sjacobs 	printf("\n");
676*355b4669Sjacobs 
677*355b4669Sjacobs 	return (0);
678*355b4669Sjacobs }
679*355b4669Sjacobs 
680*355b4669Sjacobs static int
681*355b4669Sjacobs job_query(char *request, int (*report)(papi_job_t, int, int),
682*355b4669Sjacobs 		papi_encryption_t encryption, int show_rank, int verbose)
683*355b4669Sjacobs {
684*355b4669Sjacobs 	int result = 0;
685*355b4669Sjacobs 	papi_status_t status;
686*355b4669Sjacobs 	papi_service_t svc = NULL;
687*355b4669Sjacobs 	char *printer = NULL;
688*355b4669Sjacobs 	int32_t id = -1;
689*355b4669Sjacobs 
690*355b4669Sjacobs 	get_printer_id(request, &printer, &id);
691*355b4669Sjacobs 
692*355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
693*355b4669Sjacobs 					encryption, NULL);
694*355b4669Sjacobs 	if (status != PAPI_OK) {
695*355b4669Sjacobs 		fprintf(stderr, gettext(
696*355b4669Sjacobs 			"Failed to contact service for %s: %s\n"),
697*355b4669Sjacobs 			(printer ? printer : "all"),
698*355b4669Sjacobs 			verbose_papi_message(svc, status));
699*355b4669Sjacobs 		return (-1);
700*355b4669Sjacobs 	}
701*355b4669Sjacobs 
702*355b4669Sjacobs 	if (printer == NULL) { /* all */
703*355b4669Sjacobs 		char **interest = interest_list(svc);
704*355b4669Sjacobs 
705*355b4669Sjacobs 		if (interest != NULL) {
706*355b4669Sjacobs 			int i;
707*355b4669Sjacobs 
708*355b4669Sjacobs 			for (i = 0; interest[i] != NULL; i++)
709*355b4669Sjacobs 				result += job_query(interest[i], report,
710*355b4669Sjacobs 						encryption, show_rank, verbose);
711*355b4669Sjacobs 		}
712*355b4669Sjacobs 	} else if (id == -1) { /* a printer */
713*355b4669Sjacobs 		papi_job_t *jobs = NULL;
714*355b4669Sjacobs 
715*355b4669Sjacobs 		status = papiPrinterListJobs(svc, printer, NULL, 0, 0, &jobs);
716*355b4669Sjacobs 		if (status != PAPI_OK) {
717*355b4669Sjacobs 			fprintf(stderr, gettext(
718*355b4669Sjacobs 				"Failed to get job list: %s\n"),
719*355b4669Sjacobs 				verbose_papi_message(svc, status));
720*355b4669Sjacobs 			papiServiceDestroy(svc);
721*355b4669Sjacobs 			return (-1);
722*355b4669Sjacobs 		}
723*355b4669Sjacobs 
724*355b4669Sjacobs 		if (jobs != NULL) {
725*355b4669Sjacobs 			int i;
726*355b4669Sjacobs 
727*355b4669Sjacobs 			for (i = 0; jobs[i] != NULL; i++)
728*355b4669Sjacobs 				result += report(jobs[i], show_rank, verbose);
729*355b4669Sjacobs 		}
730*355b4669Sjacobs 
731*355b4669Sjacobs 		papiJobListFree(jobs);
732*355b4669Sjacobs 	} else {	/* a job */
733*355b4669Sjacobs 		papi_job_t job = NULL;
734*355b4669Sjacobs 
735*355b4669Sjacobs 		status = papiJobQuery(svc, printer, id, NULL, &job);
736*355b4669Sjacobs 		if (status != PAPI_OK) {
737*355b4669Sjacobs 			fprintf(stderr, gettext(
738*355b4669Sjacobs 				"Failed to get job info for %s: %s\n"),
739*355b4669Sjacobs 				request, verbose_papi_message(svc, status));
740*355b4669Sjacobs 			papiServiceDestroy(svc);
741*355b4669Sjacobs 			return (-1);
742*355b4669Sjacobs 		}
743*355b4669Sjacobs 
744*355b4669Sjacobs 		if (job != NULL)
745*355b4669Sjacobs 			result = report(job, show_rank, verbose);
746*355b4669Sjacobs 
747*355b4669Sjacobs 		papiJobFree(job);
748*355b4669Sjacobs 	}
749*355b4669Sjacobs 
750*355b4669Sjacobs 	papiServiceDestroy(svc);
751*355b4669Sjacobs 
752*355b4669Sjacobs 	return (result);
753*355b4669Sjacobs }
754*355b4669Sjacobs 
755*355b4669Sjacobs static int
756*355b4669Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
757*355b4669Sjacobs {
758*355b4669Sjacobs 	papi_status_t status;
759*355b4669Sjacobs 	char *form = NULL;
760*355b4669Sjacobs 	void *iter = NULL;
761*355b4669Sjacobs 
762*355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
763*355b4669Sjacobs 					"form-supported", &form);
764*355b4669Sjacobs 		status == PAPI_OK;
765*355b4669Sjacobs 		status = papiAttributeListGetString(attrs, &iter,
766*355b4669Sjacobs 							NULL, &form)) {
767*355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, form) == 0)) {
768*355b4669Sjacobs 			printf(gettext("form %s is available to you\n"), form);
769*355b4669Sjacobs 			if (verbose != 0) {
770*355b4669Sjacobs 				char *detail = NULL;
771*355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
772*355b4669Sjacobs 						"form-supported-detail",
773*355b4669Sjacobs 						&detail);
774*355b4669Sjacobs 				if (status == PAPI_OK)
775*355b4669Sjacobs 					printf("%s\n", detail);
776*355b4669Sjacobs 			}
777*355b4669Sjacobs 		}
778*355b4669Sjacobs 	}
779*355b4669Sjacobs 
780*355b4669Sjacobs 	return (0);
781*355b4669Sjacobs }
782*355b4669Sjacobs 
783*355b4669Sjacobs static int
784*355b4669Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
785*355b4669Sjacobs {
786*355b4669Sjacobs 	papi_status_t status;
787*355b4669Sjacobs 	char *pw = NULL;
788*355b4669Sjacobs 	void *iter = NULL;
789*355b4669Sjacobs 
790*355b4669Sjacobs 	for (status = papiAttributeListGetString(attrs, &iter,
791*355b4669Sjacobs 					"pw-supported", &pw);
792*355b4669Sjacobs 		status == PAPI_OK;
793*355b4669Sjacobs 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
794*355b4669Sjacobs 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
795*355b4669Sjacobs 			printf(gettext("charset %s is available\n"), pw);
796*355b4669Sjacobs 			if (verbose != 0) {
797*355b4669Sjacobs 				char *info = NULL;
798*355b4669Sjacobs 				status = papiAttributeListGetString(attrs, NULL,
799*355b4669Sjacobs 						"pw-supported-extra", &info);
800*355b4669Sjacobs 				if (status == PAPI_OK)
801*355b4669Sjacobs 					printf("%s\n", info);
802*355b4669Sjacobs 			}
803*355b4669Sjacobs 		}
804*355b4669Sjacobs 	}
805*355b4669Sjacobs 
806*355b4669Sjacobs 	return (0);
807*355b4669Sjacobs }
808*355b4669Sjacobs 
809*355b4669Sjacobs static int
810*355b4669Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
811*355b4669Sjacobs 		papi_encryption_t encryption, int verbose)
812*355b4669Sjacobs {
813*355b4669Sjacobs 	int result = 0;
814*355b4669Sjacobs 	papi_status_t status;
815*355b4669Sjacobs 	papi_service_t svc = NULL;
816*355b4669Sjacobs 	papi_attribute_t **attrs = NULL;
817*355b4669Sjacobs 
818*355b4669Sjacobs 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
819*355b4669Sjacobs 					encryption, NULL);
820*355b4669Sjacobs 	if (status != PAPI_OK) {
821*355b4669Sjacobs 		papiServiceDestroy(svc);
822*355b4669Sjacobs 		return (-1);
823*355b4669Sjacobs 	}
824*355b4669Sjacobs 
825*355b4669Sjacobs 	attrs = papiServiceGetAttributeList(svc);
826*355b4669Sjacobs 	if (attrs != NULL) {
827*355b4669Sjacobs 		result = report(name, attrs, verbose);
828*355b4669Sjacobs 
829*355b4669Sjacobs 		if (verbose > 1) {
830*355b4669Sjacobs 			printf("\n");
831*355b4669Sjacobs 			papiAttributeListPrint(stdout, attrs, "\t");
832*355b4669Sjacobs 			printf("\n");
833*355b4669Sjacobs 		}
834*355b4669Sjacobs 	}
835*355b4669Sjacobs 
836*355b4669Sjacobs 	papiServiceDestroy(svc);
837*355b4669Sjacobs 
838*355b4669Sjacobs 	return (result);
839*355b4669Sjacobs }
840*355b4669Sjacobs 
841*355b4669Sjacobs int
842*355b4669Sjacobs main(int ac, char *av[])
843*355b4669Sjacobs {
844*355b4669Sjacobs 	int exit_code = 0;
845*355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
846*355b4669Sjacobs 	int rank = 0;
847*355b4669Sjacobs 	int verbose = 0;
848*355b4669Sjacobs 	int description = 0;
849*355b4669Sjacobs 	int c;
850*355b4669Sjacobs 	char **argv;
851*355b4669Sjacobs 
852*355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
853*355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
854*355b4669Sjacobs 
855*355b4669Sjacobs 	argv = (char **)calloc((ac + 1), sizeof (char *));
856*355b4669Sjacobs 	for (c = 0; c < ac; c++)
857*355b4669Sjacobs 		argv[c] = av[c];
858*355b4669Sjacobs 	argv[c++] = "--";
859*355b4669Sjacobs 	ac = c;
860*355b4669Sjacobs 
861*355b4669Sjacobs 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
862*355b4669Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
863*355b4669Sjacobs 		switch (c) {
864*355b4669Sjacobs 		case 'l':
865*355b4669Sjacobs 			if ((optarg == NULL) || (optarg[0] == '-'))
866*355b4669Sjacobs 				optarg = "1";
867*355b4669Sjacobs 			verbose = atoi(optarg);
868*355b4669Sjacobs 			break;
869*355b4669Sjacobs 		case 'D':
870*355b4669Sjacobs 			description = 1;
871*355b4669Sjacobs 			break;
872*355b4669Sjacobs 		case 'R':
873*355b4669Sjacobs 			rank = 1;
874*355b4669Sjacobs 			break;
875*355b4669Sjacobs 		case 'E':
876*355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
877*355b4669Sjacobs 			break;
878*355b4669Sjacobs 		default:
879*355b4669Sjacobs 			break;
880*355b4669Sjacobs 		}
881*355b4669Sjacobs 	optind = 1;
882*355b4669Sjacobs 
883*355b4669Sjacobs 	/* process command line arguments */
884*355b4669Sjacobs 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
885*355b4669Sjacobs 		switch (c) {	/* these may or may not have an option */
886*355b4669Sjacobs 		case 'a':
887*355b4669Sjacobs 		case 'c':
888*355b4669Sjacobs 		case 'p':
889*355b4669Sjacobs 		case 'o':
890*355b4669Sjacobs 		case 'R':
891*355b4669Sjacobs 		case 'u':
892*355b4669Sjacobs 		case 'v':
893*355b4669Sjacobs 		case 'l':
894*355b4669Sjacobs 		case 'f':
895*355b4669Sjacobs 		case 'S':
896*355b4669Sjacobs 			if (optarg[0] == '-') {
897*355b4669Sjacobs 				/* this check stop a possible infinite loop */
898*355b4669Sjacobs 				if ((optind > 1) && (argv[optind-1][1] != c))
899*355b4669Sjacobs 					optind--;
900*355b4669Sjacobs 				optarg = NULL;
901*355b4669Sjacobs 			} else if (strcmp(optarg, "all") == 0)
902*355b4669Sjacobs 				optarg = NULL;
903*355b4669Sjacobs 		}
904*355b4669Sjacobs 
905*355b4669Sjacobs 		switch (c) {
906*355b4669Sjacobs 		case 'a':
907*355b4669Sjacobs 			exit_code += printer_query(optarg, report_accepting,
908*355b4669Sjacobs 						encryption, verbose, 0);
909*355b4669Sjacobs 			break;
910*355b4669Sjacobs 		case 'c':
911*355b4669Sjacobs 			exit_code += printer_query(optarg, report_class,
912*355b4669Sjacobs 						encryption, verbose, 0);
913*355b4669Sjacobs 			break;
914*355b4669Sjacobs 		case 'p':
915*355b4669Sjacobs 			exit_code += printer_query(optarg, report_printer,
916*355b4669Sjacobs 						encryption, verbose,
917*355b4669Sjacobs 						description);
918*355b4669Sjacobs 			break;
919*355b4669Sjacobs 		case 'd':
920*355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
921*355b4669Sjacobs 			break;
922*355b4669Sjacobs 		case 'r':
923*355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
924*355b4669Sjacobs 			break;
925*355b4669Sjacobs 		case 'u':
926*355b4669Sjacobs 			if (optarg != NULL)
927*355b4669Sjacobs 				users = strsplit(optarg, ", \n");
928*355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
929*355b4669Sjacobs 						encryption, rank, verbose);
930*355b4669Sjacobs 			if (users != NULL) {
931*355b4669Sjacobs 				free(users);
932*355b4669Sjacobs 				users = NULL;
933*355b4669Sjacobs 			}
934*355b4669Sjacobs 			break;
935*355b4669Sjacobs 		case 'v':
936*355b4669Sjacobs 			exit_code += printer_query(optarg, report_device,
937*355b4669Sjacobs 						encryption, verbose, 0);
938*355b4669Sjacobs 			break;
939*355b4669Sjacobs 		case 'o':
940*355b4669Sjacobs 			exit_code += job_query(optarg, report_job,
941*355b4669Sjacobs 						encryption, rank, verbose);
942*355b4669Sjacobs 			break;
943*355b4669Sjacobs 		case 'f':
944*355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
945*355b4669Sjacobs 						encryption, verbose);
946*355b4669Sjacobs 			break;
947*355b4669Sjacobs 		case 'S':
948*355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
949*355b4669Sjacobs 						encryption, verbose);
950*355b4669Sjacobs 			break;
951*355b4669Sjacobs 		case 's':
952*355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
953*355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
954*355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
955*355b4669Sjacobs 						encryption, verbose, 0);
956*355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
957*355b4669Sjacobs 						encryption, verbose, 0);
958*355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
959*355b4669Sjacobs 						encryption, verbose);
960*355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
961*355b4669Sjacobs 						encryption, verbose);
962*355b4669Sjacobs 			break;
963*355b4669Sjacobs 		case 't':
964*355b4669Sjacobs 			exit_code += lpstat_service_status(encryption);
965*355b4669Sjacobs 			exit_code += lpstat_default_printer(encryption);
966*355b4669Sjacobs 			exit_code += printer_query(NULL, report_class,
967*355b4669Sjacobs 						encryption, verbose, 0);
968*355b4669Sjacobs 			exit_code += printer_query(NULL, report_device,
969*355b4669Sjacobs 						encryption, verbose, 0);
970*355b4669Sjacobs 			exit_code += printer_query(NULL, report_accepting,
971*355b4669Sjacobs 						encryption, verbose, 0);
972*355b4669Sjacobs 			exit_code += printer_query(NULL, report_printer,
973*355b4669Sjacobs 						encryption, verbose, 0);
974*355b4669Sjacobs 			exit_code += service_query(optarg, report_form,
975*355b4669Sjacobs 						encryption, verbose);
976*355b4669Sjacobs 			exit_code += service_query(optarg, report_print_wheels,
977*355b4669Sjacobs 						encryption, verbose);
978*355b4669Sjacobs 			exit_code += job_query(NULL, report_job,
979*355b4669Sjacobs 						encryption, rank, verbose);
980*355b4669Sjacobs 			break;
981*355b4669Sjacobs 		case 'L':	/* local-only, ignored */
982*355b4669Sjacobs 		case 'l':	/* increased verbose level in first pass */
983*355b4669Sjacobs 		case 'D':	/* set "description" flag in first pass */
984*355b4669Sjacobs 		case 'R':	/* set "rank" flag in first pass */
985*355b4669Sjacobs 		case 'E':	/* set encryption in the first pass */
986*355b4669Sjacobs 			break;
987*355b4669Sjacobs 		default:
988*355b4669Sjacobs 			usage(av[0]);
989*355b4669Sjacobs 		}
990*355b4669Sjacobs 	}
991*355b4669Sjacobs 	ac--;
992*355b4669Sjacobs 
993*355b4669Sjacobs 	if (ac == 1) {	/* report on my jobs */
994*355b4669Sjacobs 		struct passwd *pw = getpwuid(getuid());
995*355b4669Sjacobs 
996*355b4669Sjacobs 		if (pw != NULL)
997*355b4669Sjacobs 			users = strsplit(pw->pw_name, "");
998*355b4669Sjacobs 		exit_code += job_query(NULL, report_job, encryption,
999*355b4669Sjacobs 					rank, verbose);
1000*355b4669Sjacobs 		if (users != NULL) {
1001*355b4669Sjacobs 			free(users);
1002*355b4669Sjacobs 			users = NULL;
1003*355b4669Sjacobs 		}
1004*355b4669Sjacobs 	} else {
1005*355b4669Sjacobs 		for (c = optind; c < ac; c++)
1006*355b4669Sjacobs 			exit_code += job_query(argv[c], report_job, encryption,
1007*355b4669Sjacobs 					rank, verbose);
1008*355b4669Sjacobs 	}
1009*355b4669Sjacobs 
1010*355b4669Sjacobs 
1011*355b4669Sjacobs 	if (exit_code != 0)
1012*355b4669Sjacobs 		exit_code = 1;
1013*355b4669Sjacobs 
1014*355b4669Sjacobs 	return (exit_code);
1015*355b4669Sjacobs }
1016