xref: /titanic_50/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 968633ad8faee931821fd6b656eb0d96d4b186c0)
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 2008 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 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <locale.h>
36 #include <libintl.h>
37 #include <ctype.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 		iter = NULL;
401 		(void) papiAttributeListGetString(attrs, &iter,
402 		    "lpsched-printer-type", &str);
403 		printf(gettext("\tPrinter types: %s"), str);
404 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
405 		    == PAPI_OK)
406 			printf(", %s", str);
407 		printf("\n");
408 
409 		str = "";
410 		(void) papiAttributeListGetString(attrs, NULL,
411 					"lpsched-dial-info", &str);
412 		printf(gettext("\tConnection: %s\n"),
413 		    ((str[0] == '\0') ? gettext("direct") : str));
414 
415 		str = "";
416 		(void) papiAttributeListGetString(attrs, NULL,
417 					"lpsched-interface-script", &str);
418 		printf(gettext("\tInterface: %s\n"), str);
419 
420 		str = NULL;
421 		(void) papiAttributeListGetString(attrs, NULL,
422 					"ppd-file-uri", &str);
423 		(void) papiAttributeListGetString(attrs, NULL,
424 					"lpsched-ppd-source-path", &str);
425 		if (str != NULL)
426 			printf(gettext("\tPPD: %s\n"), str);
427 
428 		str = NULL;
429 		(void) papiAttributeListGetString(attrs, NULL,
430 					"lpsched-fault-alert-command", &str);
431 		if (str != NULL)
432 			printf(gettext("\tOn fault: %s\n"), str);
433 
434 		str = "";
435 		(void) papiAttributeListGetString(attrs, NULL,
436 					"lpsched-fault-recovery", &str);
437 		printf(gettext("\tAfter fault: %s\n"),
438 			((str[0] == '\0') ? gettext("continue") : str));
439 
440 		str = "(all)";
441 		iter = NULL;
442 		(void) papiAttributeListGetString(attrs, &iter,
443 					"requesting-user-name-allowed", &str);
444 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
445 			((str[0] == '\0') ? gettext("(none)") : str));
446 		if ((str != NULL) && (str[0] != '\0'))
447 			while (papiAttributeListGetString(attrs, &iter, NULL,
448 					&str) == PAPI_OK)
449 				printf("\t\t%s\n", str);
450 
451 		str = NULL;
452 		iter = NULL;
453 		(void) papiAttributeListGetString(attrs, &iter,
454 					"requesting-user-name-denied", &str);
455 		if (str != NULL) {
456 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
457 				((str[0] == '\0') ? gettext("(none)") : str));
458 			if ((str != NULL) && (str[0] != '\0'))
459 				while (papiAttributeListGetString(attrs, &iter,
460 						NULL, &str) == PAPI_OK)
461 					printf("\t\t%s\n", str);
462 		}
463 
464 		str = "(none)";
465 		iter = NULL;
466 		(void) papiAttributeListGetString(attrs, &iter,
467 					"form-supported", &str);
468 		printf(gettext("\tForms allowed:\n\t\t%s\n"),
469 			((str[0] == '\0') ? gettext("(none)") : str));
470 		if ((str != NULL) && (str[0] != '\0'))
471 			while (papiAttributeListGetString(attrs, &iter, NULL,
472 					&str) == PAPI_OK)
473 				printf("\t\t%s\n", str);
474 
475 		str = "";
476 		iter = NULL;
477 		(void) papiAttributeListGetString(attrs, &iter,
478 					"media-supported", &str);
479 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
480 			((str[0] == '\0') ? gettext("(none)") : str));
481 		if ((str != NULL) && (str[0] != '\0'))
482 			while (papiAttributeListGetString(attrs, &iter, NULL,
483 					&str) == PAPI_OK)
484 				printf("\t\t%s\n", str);
485 
486 		str = "";
487 		(void) papiAttributeListGetString(attrs, NULL,
488 					"job-sheets-supported", &str);
489 		if ((strcasecmp(str, "none")) == 0)
490 			str = gettext("page never printed");
491 		else if (strcasecmp(str, "optional") == 0)
492 			str = gettext("not required");
493 		else
494 			str = gettext("required");
495 
496 		printf(gettext("\tBanner %s\n"), str);
497 
498 
499 		str = "";
500 		iter = NULL;
501 		(void) papiAttributeListGetString(attrs, &iter,
502 					"lpsched-print-wheels", &str);
503 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
504 			((str[0] == '\0') ? gettext("(none)") : str));
505 		if ((str != NULL) && (str[0] != '\0'))
506 			while (papiAttributeListGetString(attrs, &iter, NULL,
507 					&str) == PAPI_OK)
508 				printf("\t\t%s\n", str);
509 
510 		printf(gettext("\tDefault pitch:\n"));
511 		printf(gettext("\tDefault page size:\n"));
512 		printf(gettext("\tDefault port setting:\n"));
513 
514 		str = "";
515 		iter = NULL;
516 		(void) papiAttributeListGetString(attrs, &iter,
517 					"lpsched-options", &str);
518 		if (str != NULL) {
519 			printf(gettext("\tOptions: %s"), str);
520 			while (papiAttributeListGetString(attrs, &iter, NULL,
521 						&str) == PAPI_OK)
522 				printf(", %s", str);
523 			printf("\n");
524 		}
525 
526 	} else if (description == 1) {
527 		char *str = "";
528 		(void) papiAttributeListGetString(attrs, NULL,
529 					"printer-description", &str);
530 		printf(gettext("\tDescription: %s\n"), str);
531 	} else if (verbose > 1)
532 		papiAttributeListPrint(stdout, attrs, "\t");
533 
534 	if (verbose > 0)
535 		printf("\n");
536 
537 	return (0);
538 }
539 
540 static int
541 printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
542 					int, int), papi_encryption_t encryption,
543 		int verbose, int description)
544 {
545 	int result = 0;
546 	papi_status_t status;
547 	papi_service_t svc = NULL;
548 
549 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
550 					encryption, NULL);
551 	if (status != PAPI_OK) {
552 		if (status == PAPI_NOT_FOUND)
553 			fprintf(stderr, gettext("%s: unknown printer\n"),
554 				name ? name : "(NULL)");
555 		else
556 			fprintf(stderr, gettext(
557 				"Failed to contact service for %s: %s\n"),
558 				name ? name : "(NULL)",
559 				verbose_papi_message(svc, status));
560 		papiServiceDestroy(svc);
561 		return (-1);
562 	}
563 
564 	if (name == NULL) { /* all */
565 		char **interest = interest_list(svc);
566 
567 		if (interest != NULL) {
568 			int i;
569 
570 			for (i = 0; interest[i] != NULL; i++)
571 				result += printer_query(interest[i], report,
572 							encryption, verbose,
573 							description);
574 		}
575 	} else {
576 		papi_printer_t printer = NULL;
577 		char **keys = NULL;
578 
579 		/*
580 		 * Limit the query to only required data to reduce the need
581 		 * to go remote for information.
582 		 */
583 		if (report == report_device)
584 			keys = report_device_keys;
585 		else if (report == report_class)
586 			keys = report_class_keys;
587 		else if (report == report_accepting)
588 			keys = report_accepting_keys;
589 		else if ((report == report_printer) && (verbose == 0))
590 			keys = report_printer_keys;
591 
592 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
593 		if (status != PAPI_OK) {
594 			fprintf(stderr, gettext(
595 				"Failed to get printer info for %s: %s\n"),
596 				name, verbose_papi_message(svc, status));
597 			papiServiceDestroy(svc);
598 			return (-1);
599 		}
600 
601 		if (printer != NULL)
602 			result = report(svc, name, printer, verbose,
603 					description);
604 
605 		papiPrinterFree(printer);
606 	}
607 
608 	papiServiceDestroy(svc);
609 
610 	return (result);
611 }
612 
613 static int
614 match_user(char *user, char **list)
615 {
616 	int i;
617 
618 	for (i = 0; list[i] != NULL; i++) {
619 		if (strcmp(user, list[i]) == 0)
620 			return (0);
621 	}
622 
623 	return (-1);
624 }
625 
626 static char **users = NULL;
627 
628 static int
629 report_job(papi_job_t job, int show_rank, int verbose)
630 {
631 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
632 	time_t clock = 0;
633 	char date[24];
634 	char request[26];
635 	char *user = "unknown";
636 	int32_t size = 0;
637 	int32_t jstate = 0;
638 
639 	char *destination = "unknown";
640 	int32_t id = -1;
641 
642 	(void) papiAttributeListGetString(attrs, NULL,
643 				"job-originating-user-name", &user);
644 
645 	if ((users != NULL) && (match_user(user, users) < 0))
646 		return (0);
647 
648 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
649 	size *= 1024;	/* for the approximate byte size */
650 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
651 
652 	(void) time(&clock);
653 	(void) papiAttributeListGetInteger(attrs, NULL,
654 				"time-at-creation", (int32_t *)&clock);
655 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
656 
657 	(void) papiAttributeListGetString(attrs, NULL,
658 				"job-printer-uri", &destination);
659 	(void) papiAttributeListGetString(attrs, NULL,
660 				"printer-name", &destination);
661 	(void) papiAttributeListGetInteger(attrs, NULL,
662 				"job-id", &id);
663 	snprintf(request, sizeof (request), "%s-%d", destination, id);
664 
665 	if (show_rank != 0) {
666 		int32_t rank = -1;
667 
668 		(void) papiAttributeListGetInteger(attrs, NULL,
669 				"number-of-intervening-jobs", &rank);
670 		rank++;
671 
672 		printf("%3d %-21s %-14s %7ld %s",
673 			rank, request, user, size, date);
674 	} else
675 		printf("%-23s %-14s %7ld   %s", request, user, size, date);
676 
677 	(void) papiAttributeListGetInteger(attrs, NULL,
678 				"job-state", &jstate);
679 	if (jstate == 0x04)
680 		printf(gettext(", being held"));
681 	else if (jstate == 0x07)
682 		printf(gettext(", cancelled"));
683 	else if (jstate == 0x09)
684 		printf(gettext(", complete"));
685 
686 	if (verbose == 1) {
687 		char *form = NULL;
688 
689 		(void) papiAttributeListGetString(attrs, NULL,
690 				"output-device-assigned", &destination);
691 		printf("\n\t assigned %s", destination);
692 
693 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
694 		if (form != NULL)
695 			printf(", form %s", form);
696 	} else if (verbose > 1) {
697 		printf("\n");
698 		papiAttributeListPrint(stdout, attrs, "\t");
699 	}
700 
701 	printf("\n");
702 
703 	return (0);
704 }
705 
706 static int
707 job_query(char *request, int (*report)(papi_job_t, int, int),
708 		papi_encryption_t encryption, int show_rank, int verbose)
709 {
710 	int result = 0;
711 	papi_status_t status;
712 	papi_service_t svc = NULL;
713 	char *printer = NULL;
714 	int32_t id = -1;
715 
716 	get_printer_id(request, &printer, &id);
717 
718 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
719 					encryption, NULL);
720 	if (status != PAPI_OK) {
721 		fprintf(stderr, gettext(
722 			"Failed to contact service for %s: %s\n"),
723 			(printer ? printer : "all"),
724 			verbose_papi_message(svc, status));
725 		return (-1);
726 	}
727 
728 	if (printer == NULL) { /* all */
729 		char **interest = interest_list(svc);
730 
731 		if (interest != NULL) {
732 			int i;
733 
734 			for (i = 0; interest[i] != NULL; i++)
735 				result += job_query(interest[i], report,
736 						encryption, show_rank, verbose);
737 		}
738 	} else if (id == -1) { /* a printer */
739 		papi_job_t *jobs = NULL;
740 
741 		status = papiPrinterListJobs(svc, printer, NULL, 0, 0, &jobs);
742 		if (status != PAPI_OK) {
743 			fprintf(stderr, gettext(
744 				"Failed to get job list: %s\n"),
745 				verbose_papi_message(svc, status));
746 			papiServiceDestroy(svc);
747 			return (-1);
748 		}
749 
750 		if (jobs != NULL) {
751 			int i;
752 
753 			for (i = 0; jobs[i] != NULL; i++)
754 				result += report(jobs[i], show_rank, verbose);
755 		}
756 
757 		papiJobListFree(jobs);
758 	} else {	/* a job */
759 		papi_job_t job = NULL;
760 
761 		status = papiJobQuery(svc, printer, id, NULL, &job);
762 		if (status != PAPI_OK) {
763 			fprintf(stderr, gettext(
764 				"Failed to get job info for %s: %s\n"),
765 				request, verbose_papi_message(svc, status));
766 			papiServiceDestroy(svc);
767 			return (-1);
768 		}
769 
770 		if (job != NULL)
771 			result = report(job, show_rank, verbose);
772 
773 		papiJobFree(job);
774 	}
775 
776 	papiServiceDestroy(svc);
777 
778 	return (result);
779 }
780 
781 static int
782 report_form(char *name, papi_attribute_t **attrs, int verbose)
783 {
784 	papi_status_t status;
785 	char *form = NULL;
786 	void *iter = NULL;
787 
788 	for (status = papiAttributeListGetString(attrs, &iter,
789 					"form-supported", &form);
790 		status == PAPI_OK;
791 		status = papiAttributeListGetString(attrs, &iter,
792 							NULL, &form)) {
793 		if ((name == NULL) || (strcmp(name, form) == 0)) {
794 			printf(gettext("form %s is available to you\n"), form);
795 			if (verbose != 0) {
796 				char *detail = NULL;
797 				status = papiAttributeListGetString(attrs, NULL,
798 						"form-supported-detail",
799 						&detail);
800 				if (status == PAPI_OK)
801 					printf("%s\n", detail);
802 			}
803 		}
804 	}
805 
806 	return (0);
807 }
808 
809 static int
810 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
811 {
812 	papi_status_t status;
813 	char *pw = NULL;
814 	void *iter = NULL;
815 
816 	for (status = papiAttributeListGetString(attrs, &iter,
817 					"pw-supported", &pw);
818 		status == PAPI_OK;
819 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
820 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
821 			printf(gettext("charset %s is available\n"), pw);
822 			if (verbose != 0) {
823 				char *info = NULL;
824 				status = papiAttributeListGetString(attrs, NULL,
825 						"pw-supported-extra", &info);
826 				if (status == PAPI_OK)
827 					printf("%s\n", info);
828 			}
829 		}
830 	}
831 
832 	return (0);
833 }
834 
835 static int
836 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
837 		papi_encryption_t encryption, int verbose)
838 {
839 	int result = 0;
840 	papi_status_t status;
841 	papi_service_t svc = NULL;
842 	papi_attribute_t **attrs = NULL;
843 
844 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
845 					encryption, NULL);
846 	if (status != PAPI_OK) {
847 		papiServiceDestroy(svc);
848 		return (-1);
849 	}
850 
851 	attrs = papiServiceGetAttributeList(svc);
852 	if (attrs != NULL) {
853 		result = report(name, attrs, verbose);
854 
855 		if (verbose > 1) {
856 			printf("\n");
857 			papiAttributeListPrint(stdout, attrs, "\t");
858 			printf("\n");
859 		}
860 	}
861 
862 	papiServiceDestroy(svc);
863 
864 	return (result);
865 }
866 
867 int
868 main(int ac, char *av[])
869 {
870 	int exit_code = 0;
871 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
872 	int rank = 0;
873 	int verbose = 0;
874 	int description = 0;
875 	int c;
876 	char **argv;
877 
878 	(void) setlocale(LC_ALL, "");
879 	(void) textdomain("SUNW_OST_OSCMD");
880 
881 	argv = (char **)calloc((ac + 1), sizeof (char *));
882 	for (c = 0; c < ac; c++) {
883 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
884 			(isalpha(av[c][2]) != 0)) {
885 			/* preserve old "-l[po...]" behavior */
886 			argv[c] = &av[c][1];
887 			argv[c][0] = '-';
888 			verbose = 1;
889 
890 		} else
891 			argv[c] = av[c];
892 	}
893 
894 	argv[c++] = "--";
895 	ac = c;
896 
897 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
898 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
899 		switch (c) {
900 		case 'l':
901 			if ((optarg == NULL) || (optarg[0] == '-'))
902 				optarg = "1";
903 			verbose = atoi(optarg);
904 			break;
905 		case 'D':
906 			description = 1;
907 			break;
908 		case 'R':
909 			rank = 1;
910 			break;
911 		case 'E':
912 			encryption = PAPI_ENCRYPT_REQUIRED;
913 			break;
914 		default:
915 			break;
916 		}
917 	optind = 1;
918 
919 	/* process command line arguments */
920 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
921 		switch (c) {	/* these may or may not have an option */
922 		case 'a':
923 		case 'c':
924 		case 'p':
925 		case 'o':
926 		case 'R':
927 		case 'u':
928 		case 'v':
929 		case 'l':
930 		case 'f':
931 		case 'S':
932 			if (optarg[0] == '-') {
933 				/* this check stop a possible infinite loop */
934 				if ((optind > 1) && (argv[optind-1][1] != c))
935 					optind--;
936 				optarg = NULL;
937 			} else if (strcmp(optarg, "all") == 0)
938 				optarg = NULL;
939 		}
940 
941 		switch (c) {
942 		case 'a':
943 			exit_code += printer_query(optarg, report_accepting,
944 						encryption, verbose, 0);
945 			break;
946 		case 'c':
947 			exit_code += printer_query(optarg, report_class,
948 						encryption, verbose, 0);
949 			break;
950 		case 'p':
951 			exit_code += printer_query(optarg, report_printer,
952 						encryption, verbose,
953 						description);
954 			break;
955 		case 'd':
956 			exit_code += lpstat_default_printer(encryption);
957 			break;
958 		case 'r':
959 			exit_code += lpstat_service_status(encryption);
960 			break;
961 		case 'u':
962 			if (optarg != NULL)
963 				users = strsplit(optarg, ", \n");
964 			exit_code += job_query(NULL, report_job,
965 						encryption, rank, verbose);
966 			if (users != NULL) {
967 				free(users);
968 				users = NULL;
969 			}
970 			break;
971 		case 'v':
972 			exit_code += printer_query(optarg, report_device,
973 						encryption, verbose, 0);
974 			break;
975 		case 'o':
976 			exit_code += job_query(optarg, report_job,
977 						encryption, rank, verbose);
978 			break;
979 		case 'f':
980 			exit_code += service_query(optarg, report_form,
981 						encryption, verbose);
982 			break;
983 		case 'S':
984 			exit_code += service_query(optarg, report_print_wheels,
985 						encryption, verbose);
986 			break;
987 		case 's':
988 			exit_code += lpstat_service_status(encryption);
989 			exit_code += lpstat_default_printer(encryption);
990 			exit_code += printer_query(NULL, report_class,
991 						encryption, verbose, 0);
992 			exit_code += printer_query(NULL, report_device,
993 						encryption, verbose, 0);
994 			exit_code += service_query(optarg, report_form,
995 						encryption, verbose);
996 			exit_code += service_query(optarg, report_print_wheels,
997 						encryption, verbose);
998 			break;
999 		case 't':
1000 			exit_code += lpstat_service_status(encryption);
1001 			exit_code += lpstat_default_printer(encryption);
1002 			exit_code += printer_query(NULL, report_class,
1003 						encryption, verbose, 0);
1004 			exit_code += printer_query(NULL, report_device,
1005 						encryption, verbose, 0);
1006 			exit_code += printer_query(NULL, report_accepting,
1007 						encryption, verbose, 0);
1008 			exit_code += printer_query(NULL, report_printer,
1009 						encryption, verbose, 0);
1010 			exit_code += service_query(optarg, report_form,
1011 						encryption, verbose);
1012 			exit_code += service_query(optarg, report_print_wheels,
1013 						encryption, verbose);
1014 			exit_code += job_query(NULL, report_job,
1015 						encryption, rank, verbose);
1016 			break;
1017 		case 'L':	/* local-only, ignored */
1018 		case 'l':	/* increased verbose level in first pass */
1019 		case 'D':	/* set "description" flag in first pass */
1020 		case 'R':	/* set "rank" flag in first pass */
1021 		case 'E':	/* set encryption in the first pass */
1022 			break;
1023 		default:
1024 			usage(av[0]);
1025 		}
1026 	}
1027 	ac--;
1028 
1029 	if (ac == 1) {	/* report on my jobs */
1030 		struct passwd *pw = getpwuid(getuid());
1031 
1032 		if (pw != NULL)
1033 			users = strsplit(pw->pw_name, "");
1034 		exit_code += job_query(NULL, report_job, encryption,
1035 					rank, verbose);
1036 		if (users != NULL) {
1037 			free(users);
1038 			users = NULL;
1039 		}
1040 	} else {
1041 		for (c = optind; c < ac; c++)
1042 			exit_code += job_query(argv[c], report_job, encryption,
1043 					rank, verbose);
1044 	}
1045 
1046 
1047 	if (exit_code != 0)
1048 		exit_code = 1;
1049 
1050 	return (exit_code);
1051 }
1052