xref: /titanic_44/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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 2007 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 		if (status == PAPI_NOT_FOUND)
537 			fprintf(stderr, gettext("%s: unknown printer\n"),
538 				name ? name : "(NULL)");
539 		else
540 			fprintf(stderr, gettext(
541 				"Failed to contact service for %s: %s\n"),
542 				name ? name : "(NULL)",
543 				verbose_papi_message(svc, status));
544 		papiServiceDestroy(svc);
545 		return (-1);
546 	}
547 
548 	if (name == NULL) { /* all */
549 		char **interest = interest_list(svc);
550 
551 		if (interest != NULL) {
552 			int i;
553 
554 			for (i = 0; interest[i] != NULL; i++)
555 				result += printer_query(interest[i], report,
556 							encryption, verbose,
557 							description);
558 		}
559 	} else {
560 		papi_printer_t printer = NULL;
561 		char **keys = NULL;
562 
563 		/*
564 		 * Limit the query to only required data to reduce the need
565 		 * to go remote for information.
566 		 */
567 		if (report == report_device)
568 			keys = report_device_keys;
569 		else if (report == report_class)
570 			keys = report_class_keys;
571 		else if (report == report_accepting)
572 			keys = report_accepting_keys;
573 		else if ((report == report_printer) && (verbose == 0))
574 			keys = report_printer_keys;
575 
576 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
577 		if (status != PAPI_OK) {
578 			fprintf(stderr, gettext(
579 				"Failed to get printer info for %s: %s\n"),
580 				name, verbose_papi_message(svc, status));
581 			papiServiceDestroy(svc);
582 			return (-1);
583 		}
584 
585 		if (printer != NULL)
586 			result = report(svc, name, printer, verbose,
587 					description);
588 
589 		papiPrinterFree(printer);
590 	}
591 
592 	papiServiceDestroy(svc);
593 
594 	return (result);
595 }
596 
597 static int
598 match_user(char *user, char **list)
599 {
600 	int i;
601 
602 	for (i = 0; list[i] != NULL; i++) {
603 		if (strcmp(user, list[i]) == 0)
604 			return (0);
605 	}
606 
607 	return (-1);
608 }
609 
610 static char **users = NULL;
611 
612 static int
613 report_job(papi_job_t job, int show_rank, int verbose)
614 {
615 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
616 	time_t clock = 0;
617 	char date[24];
618 	char request[26];
619 	char *user = "unknown";
620 	int32_t size = 0;
621 	int32_t jstate = 0;
622 
623 	char *destination = "unknown";
624 	int32_t id = -1;
625 
626 	(void) papiAttributeListGetString(attrs, NULL,
627 				"job-originating-user-name", &user);
628 
629 	if ((users != NULL) && (match_user(user, users) < 0))
630 		return (0);
631 
632 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
633 	size *= 1024;	/* for the approximate byte size */
634 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
635 
636 	(void) time(&clock);
637 	(void) papiAttributeListGetInteger(attrs, NULL,
638 				"time-at-creation", (int32_t *)&clock);
639 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
640 
641 	(void) papiAttributeListGetString(attrs, NULL,
642 				"job-printer-uri", &destination);
643 	(void) papiAttributeListGetString(attrs, NULL,
644 				"printer-name", &destination);
645 	(void) papiAttributeListGetInteger(attrs, NULL,
646 				"job-id", &id);
647 	snprintf(request, sizeof (request), "%s-%d", destination, id);
648 
649 	if (show_rank != 0) {
650 		int32_t rank = -1;
651 
652 		(void) papiAttributeListGetInteger(attrs, NULL,
653 				"number-of-intervening-jobs", &rank);
654 		rank++;
655 
656 		printf("%3d %-21s %-14s %7ld %s",
657 			rank, request, user, size, date);
658 	} else
659 		printf("%-23s %-14s %7ld   %s", request, user, size, date);
660 
661 	(void) papiAttributeListGetInteger(attrs, NULL,
662 				"job-state", &jstate);
663 	if (jstate == 0x04)
664 		printf(gettext(", being held"));
665 	else if (jstate == 0x07)
666 		printf(gettext(", cancelled"));
667 	else if (jstate == 0x09)
668 		printf(gettext(", complete"));
669 
670 	if (verbose == 1) {
671 		char *form = NULL;
672 
673 		(void) papiAttributeListGetString(attrs, NULL,
674 				"output-device-assigned", &destination);
675 		printf("\n\t assigned %s", destination);
676 
677 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
678 		if (form != NULL)
679 			printf(", form %s", form);
680 	} else if (verbose > 1) {
681 		printf("\n");
682 		papiAttributeListPrint(stdout, attrs, "\t");
683 	}
684 
685 	printf("\n");
686 
687 	return (0);
688 }
689 
690 static int
691 job_query(char *request, int (*report)(papi_job_t, int, int),
692 		papi_encryption_t encryption, int show_rank, int verbose)
693 {
694 	int result = 0;
695 	papi_status_t status;
696 	papi_service_t svc = NULL;
697 	char *printer = NULL;
698 	int32_t id = -1;
699 
700 	get_printer_id(request, &printer, &id);
701 
702 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
703 					encryption, NULL);
704 	if (status != PAPI_OK) {
705 		fprintf(stderr, gettext(
706 			"Failed to contact service for %s: %s\n"),
707 			(printer ? printer : "all"),
708 			verbose_papi_message(svc, status));
709 		return (-1);
710 	}
711 
712 	if (printer == NULL) { /* all */
713 		char **interest = interest_list(svc);
714 
715 		if (interest != NULL) {
716 			int i;
717 
718 			for (i = 0; interest[i] != NULL; i++)
719 				result += job_query(interest[i], report,
720 						encryption, show_rank, verbose);
721 		}
722 	} else if (id == -1) { /* a printer */
723 		papi_job_t *jobs = NULL;
724 
725 		status = papiPrinterListJobs(svc, printer, NULL, 0, 0, &jobs);
726 		if (status != PAPI_OK) {
727 			fprintf(stderr, gettext(
728 				"Failed to get job list: %s\n"),
729 				verbose_papi_message(svc, status));
730 			papiServiceDestroy(svc);
731 			return (-1);
732 		}
733 
734 		if (jobs != NULL) {
735 			int i;
736 
737 			for (i = 0; jobs[i] != NULL; i++)
738 				result += report(jobs[i], show_rank, verbose);
739 		}
740 
741 		papiJobListFree(jobs);
742 	} else {	/* a job */
743 		papi_job_t job = NULL;
744 
745 		status = papiJobQuery(svc, printer, id, NULL, &job);
746 		if (status != PAPI_OK) {
747 			fprintf(stderr, gettext(
748 				"Failed to get job info for %s: %s\n"),
749 				request, verbose_papi_message(svc, status));
750 			papiServiceDestroy(svc);
751 			return (-1);
752 		}
753 
754 		if (job != NULL)
755 			result = report(job, show_rank, verbose);
756 
757 		papiJobFree(job);
758 	}
759 
760 	papiServiceDestroy(svc);
761 
762 	return (result);
763 }
764 
765 static int
766 report_form(char *name, papi_attribute_t **attrs, int verbose)
767 {
768 	papi_status_t status;
769 	char *form = NULL;
770 	void *iter = NULL;
771 
772 	for (status = papiAttributeListGetString(attrs, &iter,
773 					"form-supported", &form);
774 		status == PAPI_OK;
775 		status = papiAttributeListGetString(attrs, &iter,
776 							NULL, &form)) {
777 		if ((name == NULL) || (strcmp(name, form) == 0)) {
778 			printf(gettext("form %s is available to you\n"), form);
779 			if (verbose != 0) {
780 				char *detail = NULL;
781 				status = papiAttributeListGetString(attrs, NULL,
782 						"form-supported-detail",
783 						&detail);
784 				if (status == PAPI_OK)
785 					printf("%s\n", detail);
786 			}
787 		}
788 	}
789 
790 	return (0);
791 }
792 
793 static int
794 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
795 {
796 	papi_status_t status;
797 	char *pw = NULL;
798 	void *iter = NULL;
799 
800 	for (status = papiAttributeListGetString(attrs, &iter,
801 					"pw-supported", &pw);
802 		status == PAPI_OK;
803 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
804 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
805 			printf(gettext("charset %s is available\n"), pw);
806 			if (verbose != 0) {
807 				char *info = NULL;
808 				status = papiAttributeListGetString(attrs, NULL,
809 						"pw-supported-extra", &info);
810 				if (status == PAPI_OK)
811 					printf("%s\n", info);
812 			}
813 		}
814 	}
815 
816 	return (0);
817 }
818 
819 static int
820 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
821 		papi_encryption_t encryption, int verbose)
822 {
823 	int result = 0;
824 	papi_status_t status;
825 	papi_service_t svc = NULL;
826 	papi_attribute_t **attrs = NULL;
827 
828 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
829 					encryption, NULL);
830 	if (status != PAPI_OK) {
831 		papiServiceDestroy(svc);
832 		return (-1);
833 	}
834 
835 	attrs = papiServiceGetAttributeList(svc);
836 	if (attrs != NULL) {
837 		result = report(name, attrs, verbose);
838 
839 		if (verbose > 1) {
840 			printf("\n");
841 			papiAttributeListPrint(stdout, attrs, "\t");
842 			printf("\n");
843 		}
844 	}
845 
846 	papiServiceDestroy(svc);
847 
848 	return (result);
849 }
850 
851 int
852 main(int ac, char *av[])
853 {
854 	int exit_code = 0;
855 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
856 	int rank = 0;
857 	int verbose = 0;
858 	int description = 0;
859 	int c;
860 	char **argv;
861 
862 	(void) setlocale(LC_ALL, "");
863 	(void) textdomain("SUNW_OST_OSCMD");
864 
865 	argv = (char **)calloc((ac + 1), sizeof (char *));
866 	for (c = 0; c < ac; c++)
867 		argv[c] = av[c];
868 	argv[c++] = "--";
869 	ac = c;
870 
871 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
872 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
873 		switch (c) {
874 		case 'l':
875 			if ((optarg == NULL) || (optarg[0] == '-'))
876 				optarg = "1";
877 			verbose = atoi(optarg);
878 			break;
879 		case 'D':
880 			description = 1;
881 			break;
882 		case 'R':
883 			rank = 1;
884 			break;
885 		case 'E':
886 			encryption = PAPI_ENCRYPT_REQUIRED;
887 			break;
888 		default:
889 			break;
890 		}
891 	optind = 1;
892 
893 	/* process command line arguments */
894 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
895 		switch (c) {	/* these may or may not have an option */
896 		case 'a':
897 		case 'c':
898 		case 'p':
899 		case 'o':
900 		case 'R':
901 		case 'u':
902 		case 'v':
903 		case 'l':
904 		case 'f':
905 		case 'S':
906 			if (optarg[0] == '-') {
907 				/* this check stop a possible infinite loop */
908 				if ((optind > 1) && (argv[optind-1][1] != c))
909 					optind--;
910 				optarg = NULL;
911 			} else if (strcmp(optarg, "all") == 0)
912 				optarg = NULL;
913 		}
914 
915 		switch (c) {
916 		case 'a':
917 			exit_code += printer_query(optarg, report_accepting,
918 						encryption, verbose, 0);
919 			break;
920 		case 'c':
921 			exit_code += printer_query(optarg, report_class,
922 						encryption, verbose, 0);
923 			break;
924 		case 'p':
925 			exit_code += printer_query(optarg, report_printer,
926 						encryption, verbose,
927 						description);
928 			break;
929 		case 'd':
930 			exit_code += lpstat_default_printer(encryption);
931 			break;
932 		case 'r':
933 			exit_code += lpstat_service_status(encryption);
934 			break;
935 		case 'u':
936 			if (optarg != NULL)
937 				users = strsplit(optarg, ", \n");
938 			exit_code += job_query(NULL, report_job,
939 						encryption, rank, verbose);
940 			if (users != NULL) {
941 				free(users);
942 				users = NULL;
943 			}
944 			break;
945 		case 'v':
946 			exit_code += printer_query(optarg, report_device,
947 						encryption, verbose, 0);
948 			break;
949 		case 'o':
950 			exit_code += job_query(optarg, report_job,
951 						encryption, rank, verbose);
952 			break;
953 		case 'f':
954 			exit_code += service_query(optarg, report_form,
955 						encryption, verbose);
956 			break;
957 		case 'S':
958 			exit_code += service_query(optarg, report_print_wheels,
959 						encryption, verbose);
960 			break;
961 		case 's':
962 			exit_code += lpstat_service_status(encryption);
963 			exit_code += lpstat_default_printer(encryption);
964 			exit_code += printer_query(NULL, report_class,
965 						encryption, verbose, 0);
966 			exit_code += printer_query(NULL, report_device,
967 						encryption, verbose, 0);
968 			exit_code += service_query(optarg, report_form,
969 						encryption, verbose);
970 			exit_code += service_query(optarg, report_print_wheels,
971 						encryption, verbose);
972 			break;
973 		case 't':
974 			exit_code += lpstat_service_status(encryption);
975 			exit_code += lpstat_default_printer(encryption);
976 			exit_code += printer_query(NULL, report_class,
977 						encryption, verbose, 0);
978 			exit_code += printer_query(NULL, report_device,
979 						encryption, verbose, 0);
980 			exit_code += printer_query(NULL, report_accepting,
981 						encryption, verbose, 0);
982 			exit_code += printer_query(NULL, report_printer,
983 						encryption, verbose, 0);
984 			exit_code += service_query(optarg, report_form,
985 						encryption, verbose);
986 			exit_code += service_query(optarg, report_print_wheels,
987 						encryption, verbose);
988 			exit_code += job_query(NULL, report_job,
989 						encryption, rank, verbose);
990 			break;
991 		case 'L':	/* local-only, ignored */
992 		case 'l':	/* increased verbose level in first pass */
993 		case 'D':	/* set "description" flag in first pass */
994 		case 'R':	/* set "rank" flag in first pass */
995 		case 'E':	/* set encryption in the first pass */
996 			break;
997 		default:
998 			usage(av[0]);
999 		}
1000 	}
1001 	ac--;
1002 
1003 	if (ac == 1) {	/* report on my jobs */
1004 		struct passwd *pw = getpwuid(getuid());
1005 
1006 		if (pw != NULL)
1007 			users = strsplit(pw->pw_name, "");
1008 		exit_code += job_query(NULL, report_job, encryption,
1009 					rank, verbose);
1010 		if (users != NULL) {
1011 			free(users);
1012 			users = NULL;
1013 		}
1014 	} else {
1015 		for (c = optind; c < ac; c++)
1016 			exit_code += job_query(argv[c], report_job, encryption,
1017 					rank, verbose);
1018 	}
1019 
1020 
1021 	if (exit_code != 0)
1022 		exit_code = 1;
1023 
1024 	return (exit_code);
1025 }
1026