xref: /illumos-gate/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 150d2c5288c645a1c1a7d2bee61199a3729406c7)
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 		char *form = NULL;
668 
669 		(void) papiAttributeListGetString(attrs, NULL,
670 				"output-device-assigned", &destination);
671 		printf("\n\t assigned %s", destination);
672 
673 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
674 		if (form != NULL)
675 			printf(", form %s", form);
676 	} else if (verbose > 1) {
677 		printf("\n");
678 		papiAttributeListPrint(stdout, attrs, "\t");
679 	}
680 
681 	printf("\n");
682 
683 	return (0);
684 }
685 
686 static int
687 job_query(char *request, int (*report)(papi_job_t, int, int),
688 		papi_encryption_t encryption, int show_rank, int verbose)
689 {
690 	int result = 0;
691 	papi_status_t status;
692 	papi_service_t svc = NULL;
693 	char *printer = NULL;
694 	int32_t id = -1;
695 
696 	get_printer_id(request, &printer, &id);
697 
698 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
699 					encryption, NULL);
700 	if (status != PAPI_OK) {
701 		fprintf(stderr, gettext(
702 			"Failed to contact service for %s: %s\n"),
703 			(printer ? printer : "all"),
704 			verbose_papi_message(svc, status));
705 		return (-1);
706 	}
707 
708 	if (printer == NULL) { /* all */
709 		char **interest = interest_list(svc);
710 
711 		if (interest != NULL) {
712 			int i;
713 
714 			for (i = 0; interest[i] != NULL; i++)
715 				result += job_query(interest[i], report,
716 						encryption, show_rank, verbose);
717 		}
718 	} else if (id == -1) { /* a printer */
719 		papi_job_t *jobs = NULL;
720 
721 		status = papiPrinterListJobs(svc, printer, NULL, 0, 0, &jobs);
722 		if (status != PAPI_OK) {
723 			fprintf(stderr, gettext(
724 				"Failed to get job list: %s\n"),
725 				verbose_papi_message(svc, status));
726 			papiServiceDestroy(svc);
727 			return (-1);
728 		}
729 
730 		if (jobs != NULL) {
731 			int i;
732 
733 			for (i = 0; jobs[i] != NULL; i++)
734 				result += report(jobs[i], show_rank, verbose);
735 		}
736 
737 		papiJobListFree(jobs);
738 	} else {	/* a job */
739 		papi_job_t job = NULL;
740 
741 		status = papiJobQuery(svc, printer, id, NULL, &job);
742 		if (status != PAPI_OK) {
743 			fprintf(stderr, gettext(
744 				"Failed to get job info for %s: %s\n"),
745 				request, verbose_papi_message(svc, status));
746 			papiServiceDestroy(svc);
747 			return (-1);
748 		}
749 
750 		if (job != NULL)
751 			result = report(job, show_rank, verbose);
752 
753 		papiJobFree(job);
754 	}
755 
756 	papiServiceDestroy(svc);
757 
758 	return (result);
759 }
760 
761 static int
762 report_form(char *name, papi_attribute_t **attrs, int verbose)
763 {
764 	papi_status_t status;
765 	char *form = NULL;
766 	void *iter = NULL;
767 
768 	for (status = papiAttributeListGetString(attrs, &iter,
769 					"form-supported", &form);
770 		status == PAPI_OK;
771 		status = papiAttributeListGetString(attrs, &iter,
772 							NULL, &form)) {
773 		if ((name == NULL) || (strcmp(name, form) == 0)) {
774 			printf(gettext("form %s is available to you\n"), form);
775 			if (verbose != 0) {
776 				char *detail = NULL;
777 				status = papiAttributeListGetString(attrs, NULL,
778 						"form-supported-detail",
779 						&detail);
780 				if (status == PAPI_OK)
781 					printf("%s\n", detail);
782 			}
783 		}
784 	}
785 
786 	return (0);
787 }
788 
789 static int
790 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
791 {
792 	papi_status_t status;
793 	char *pw = NULL;
794 	void *iter = NULL;
795 
796 	for (status = papiAttributeListGetString(attrs, &iter,
797 					"pw-supported", &pw);
798 		status == PAPI_OK;
799 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
800 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
801 			printf(gettext("charset %s is available\n"), pw);
802 			if (verbose != 0) {
803 				char *info = NULL;
804 				status = papiAttributeListGetString(attrs, NULL,
805 						"pw-supported-extra", &info);
806 				if (status == PAPI_OK)
807 					printf("%s\n", info);
808 			}
809 		}
810 	}
811 
812 	return (0);
813 }
814 
815 static int
816 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
817 		papi_encryption_t encryption, int verbose)
818 {
819 	int result = 0;
820 	papi_status_t status;
821 	papi_service_t svc = NULL;
822 	papi_attribute_t **attrs = NULL;
823 
824 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
825 					encryption, NULL);
826 	if (status != PAPI_OK) {
827 		papiServiceDestroy(svc);
828 		return (-1);
829 	}
830 
831 	attrs = papiServiceGetAttributeList(svc);
832 	if (attrs != NULL) {
833 		result = report(name, attrs, verbose);
834 
835 		if (verbose > 1) {
836 			printf("\n");
837 			papiAttributeListPrint(stdout, attrs, "\t");
838 			printf("\n");
839 		}
840 	}
841 
842 	papiServiceDestroy(svc);
843 
844 	return (result);
845 }
846 
847 int
848 main(int ac, char *av[])
849 {
850 	int exit_code = 0;
851 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
852 	int rank = 0;
853 	int verbose = 0;
854 	int description = 0;
855 	int c;
856 	char **argv;
857 
858 	(void) setlocale(LC_ALL, "");
859 	(void) textdomain("SUNW_OST_OSCMD");
860 
861 	argv = (char **)calloc((ac + 1), sizeof (char *));
862 	for (c = 0; c < ac; c++)
863 		argv[c] = av[c];
864 	argv[c++] = "--";
865 	ac = c;
866 
867 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
868 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
869 		switch (c) {
870 		case 'l':
871 			if ((optarg == NULL) || (optarg[0] == '-'))
872 				optarg = "1";
873 			verbose = atoi(optarg);
874 			break;
875 		case 'D':
876 			description = 1;
877 			break;
878 		case 'R':
879 			rank = 1;
880 			break;
881 		case 'E':
882 			encryption = PAPI_ENCRYPT_REQUIRED;
883 			break;
884 		default:
885 			break;
886 		}
887 	optind = 1;
888 
889 	/* process command line arguments */
890 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
891 		switch (c) {	/* these may or may not have an option */
892 		case 'a':
893 		case 'c':
894 		case 'p':
895 		case 'o':
896 		case 'R':
897 		case 'u':
898 		case 'v':
899 		case 'l':
900 		case 'f':
901 		case 'S':
902 			if (optarg[0] == '-') {
903 				/* this check stop a possible infinite loop */
904 				if ((optind > 1) && (argv[optind-1][1] != c))
905 					optind--;
906 				optarg = NULL;
907 			} else if (strcmp(optarg, "all") == 0)
908 				optarg = NULL;
909 		}
910 
911 		switch (c) {
912 		case 'a':
913 			exit_code += printer_query(optarg, report_accepting,
914 						encryption, verbose, 0);
915 			break;
916 		case 'c':
917 			exit_code += printer_query(optarg, report_class,
918 						encryption, verbose, 0);
919 			break;
920 		case 'p':
921 			exit_code += printer_query(optarg, report_printer,
922 						encryption, verbose,
923 						description);
924 			break;
925 		case 'd':
926 			exit_code += lpstat_default_printer(encryption);
927 			break;
928 		case 'r':
929 			exit_code += lpstat_service_status(encryption);
930 			break;
931 		case 'u':
932 			if (optarg != NULL)
933 				users = strsplit(optarg, ", \n");
934 			exit_code += job_query(NULL, report_job,
935 						encryption, rank, verbose);
936 			if (users != NULL) {
937 				free(users);
938 				users = NULL;
939 			}
940 			break;
941 		case 'v':
942 			exit_code += printer_query(optarg, report_device,
943 						encryption, verbose, 0);
944 			break;
945 		case 'o':
946 			exit_code += job_query(optarg, report_job,
947 						encryption, rank, verbose);
948 			break;
949 		case 'f':
950 			exit_code += service_query(optarg, report_form,
951 						encryption, verbose);
952 			break;
953 		case 'S':
954 			exit_code += service_query(optarg, report_print_wheels,
955 						encryption, verbose);
956 			break;
957 		case 's':
958 			exit_code += lpstat_service_status(encryption);
959 			exit_code += lpstat_default_printer(encryption);
960 			exit_code += printer_query(NULL, report_class,
961 						encryption, verbose, 0);
962 			exit_code += printer_query(NULL, report_device,
963 						encryption, verbose, 0);
964 			exit_code += service_query(optarg, report_form,
965 						encryption, verbose);
966 			exit_code += service_query(optarg, report_print_wheels,
967 						encryption, verbose);
968 			break;
969 		case 't':
970 			exit_code += lpstat_service_status(encryption);
971 			exit_code += lpstat_default_printer(encryption);
972 			exit_code += printer_query(NULL, report_class,
973 						encryption, verbose, 0);
974 			exit_code += printer_query(NULL, report_device,
975 						encryption, verbose, 0);
976 			exit_code += printer_query(NULL, report_accepting,
977 						encryption, verbose, 0);
978 			exit_code += printer_query(NULL, report_printer,
979 						encryption, verbose, 0);
980 			exit_code += service_query(optarg, report_form,
981 						encryption, verbose);
982 			exit_code += service_query(optarg, report_print_wheels,
983 						encryption, verbose);
984 			exit_code += job_query(NULL, report_job,
985 						encryption, rank, verbose);
986 			break;
987 		case 'L':	/* local-only, ignored */
988 		case 'l':	/* increased verbose level in first pass */
989 		case 'D':	/* set "description" flag in first pass */
990 		case 'R':	/* set "rank" flag in first pass */
991 		case 'E':	/* set encryption in the first pass */
992 			break;
993 		default:
994 			usage(av[0]);
995 		}
996 	}
997 	ac--;
998 
999 	if (ac == 1) {	/* report on my jobs */
1000 		struct passwd *pw = getpwuid(getuid());
1001 
1002 		if (pw != NULL)
1003 			users = strsplit(pw->pw_name, "");
1004 		exit_code += job_query(NULL, report_job, encryption,
1005 					rank, verbose);
1006 		if (users != NULL) {
1007 			free(users);
1008 			users = NULL;
1009 		}
1010 	} else {
1011 		for (c = optind; c < ac; c++)
1012 			exit_code += job_query(argv[c], report_job, encryption,
1013 					rank, verbose);
1014 	}
1015 
1016 
1017 	if (exit_code != 0)
1018 		exit_code = 1;
1019 
1020 	return (exit_code);
1021 }
1022