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