xref: /illumos-gate/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision f03fb72c9f13560757b95b26631635b5d2a6d6c5)
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 = request;
714 	int32_t id = -1;
715 	int flag1 = 0;
716 	int flag = 1;
717 	int print_flag = 0;
718 
719 	do {
720 		status = papiServiceCreate(&svc, printer, NULL, NULL,
721 			    cli_auth_callback, encryption, NULL);
722 
723 		if ((status == PAPI_OK) && (printer != NULL))
724 			print_flag = 1;
725 
726 		/* <name>-# printer name does not exist */
727 		if (status != PAPI_OK) {
728 			/*
729 			 * Check if <name>-# is a request-id
730 			 * Once this check is done flag1 is set
731 			 */
732 			if (flag1 == 1)
733 				break;
734 
735 			get_printer_id(printer, &printer, &id);
736 
737 			status = papiServiceCreate(&svc, printer, NULL, NULL,
738 				    cli_auth_callback, encryption, NULL);
739 
740 			if (status != PAPI_OK) {
741 				fprintf(stderr, gettext(
742 				    "Failed to contact service for %s: %s\n"),
743 				    (printer ? printer : "all"),
744 				    verbose_papi_message(svc, status));
745 				return (-1);
746 			}
747 		}
748 
749 		if (printer == NULL) { /* all */
750 			char **interest = interest_list(svc);
751 
752 			if (interest != NULL) {
753 				int i;
754 
755 				for (i = 0; interest[i] != NULL; i++)
756 					result += job_query(interest[i], report,
757 						    encryption, show_rank, verbose);
758 			}
759 		} else if (id == -1) { /* a printer */
760 			papi_job_t *jobs = NULL;
761 
762 			status = papiPrinterListJobs(svc, printer, NULL,
763 				    0, 0, &jobs);
764 			if (status != PAPI_OK) {
765 				fprintf(stderr, gettext(
766 				    "Failed to get job list: %s\n"),
767 				    verbose_papi_message(svc, status));
768 				papiServiceDestroy(svc);
769 				return (-1);
770 			}
771 
772 			if (jobs != NULL) {
773 				int i;
774 
775 				for (i = 0; jobs[i] != NULL; i++)
776 					result += report(jobs[i],
777 						    show_rank, verbose);
778 			}
779 
780 			papiJobListFree(jobs);
781 		} else {	/* a job */
782 			papi_job_t job = NULL;
783 
784 			/* Once a job has been found stop processing */
785 			flag = 0;
786 
787 			status = papiJobQuery(svc, printer, id, NULL, &job);
788 			if (status != PAPI_OK) {
789 				if (!print_flag)
790 					fprintf(stderr, gettext(
791 					    "Failed to get job info for %s: %s\n"),
792 					    request, verbose_papi_message(svc, status));
793 				papiServiceDestroy(svc);
794 				return (-1);
795 			}
796 
797 			if (job != NULL)
798 				result = report(job, show_rank, verbose);
799 
800 			papiJobFree(job);
801 		}
802 
803 		if (flag) {
804 			id = -1;
805 			get_printer_id(printer, &printer, &id);
806 			if (id == -1)
807 				flag = 0;
808 			else
809 				flag1 = 1;
810 		}
811 	} while (flag);
812 
813 	papiServiceDestroy(svc);
814 
815 	return (result);
816 }
817 
818 static int
819 report_form(char *name, papi_attribute_t **attrs, int verbose)
820 {
821 	papi_status_t status;
822 	char *form = NULL;
823 	void *iter = NULL;
824 
825 	for (status = papiAttributeListGetString(attrs, &iter,
826 					"form-supported", &form);
827 		status == PAPI_OK;
828 		status = papiAttributeListGetString(attrs, &iter,
829 							NULL, &form)) {
830 		if ((name == NULL) || (strcmp(name, form) == 0)) {
831 			printf(gettext("form %s is available to you\n"), form);
832 			if (verbose != 0) {
833 				char *detail = NULL;
834 				status = papiAttributeListGetString(attrs, NULL,
835 						"form-supported-detail",
836 						&detail);
837 				if (status == PAPI_OK)
838 					printf("%s\n", detail);
839 			}
840 		}
841 	}
842 
843 	return (0);
844 }
845 
846 static int
847 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
848 {
849 	papi_status_t status;
850 	char *pw = NULL;
851 	void *iter = NULL;
852 
853 	for (status = papiAttributeListGetString(attrs, &iter,
854 					"pw-supported", &pw);
855 		status == PAPI_OK;
856 		status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
857 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
858 			printf(gettext("charset %s is available\n"), pw);
859 			if (verbose != 0) {
860 				char *info = NULL;
861 				status = papiAttributeListGetString(attrs, NULL,
862 						"pw-supported-extra", &info);
863 				if (status == PAPI_OK)
864 					printf("%s\n", info);
865 			}
866 		}
867 	}
868 
869 	return (0);
870 }
871 
872 static int
873 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
874 		papi_encryption_t encryption, int verbose)
875 {
876 	int result = 0;
877 	papi_status_t status;
878 	papi_service_t svc = NULL;
879 	papi_attribute_t **attrs = NULL;
880 
881 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
882 					encryption, NULL);
883 	if (status != PAPI_OK) {
884 		papiServiceDestroy(svc);
885 		return (-1);
886 	}
887 
888 	attrs = papiServiceGetAttributeList(svc);
889 	if (attrs != NULL) {
890 		result = report(name, attrs, verbose);
891 
892 		if (verbose > 1) {
893 			printf("\n");
894 			papiAttributeListPrint(stdout, attrs, "\t");
895 			printf("\n");
896 		}
897 	}
898 
899 	papiServiceDestroy(svc);
900 
901 	return (result);
902 }
903 
904 int
905 main(int ac, char *av[])
906 {
907 	int exit_code = 0;
908 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
909 	int rank = 0;
910 	int verbose = 0;
911 	int description = 0;
912 	int c;
913 	char **argv;
914 
915 	(void) setlocale(LC_ALL, "");
916 	(void) textdomain("SUNW_OST_OSCMD");
917 
918 	argv = (char **)calloc((ac + 1), sizeof (char *));
919 	for (c = 0; c < ac; c++) {
920 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
921 			(isalpha(av[c][2]) != 0)) {
922 			/* preserve old "-l[po...]" behavior */
923 			argv[c] = &av[c][1];
924 			argv[c][0] = '-';
925 			verbose = 1;
926 
927 		} else
928 			argv[c] = av[c];
929 	}
930 
931 	argv[c++] = "--";
932 	ac = c;
933 
934 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
935 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
936 		switch (c) {
937 		case 'l':
938 			if ((optarg == NULL) || (optarg[0] == '-'))
939 				optarg = "1";
940 			verbose = atoi(optarg);
941 			break;
942 		case 'D':
943 			description = 1;
944 			break;
945 		case 'R':
946 			rank = 1;
947 			break;
948 		case 'E':
949 			encryption = PAPI_ENCRYPT_REQUIRED;
950 			break;
951 		default:
952 			break;
953 		}
954 	optind = 1;
955 
956 	/* process command line arguments */
957 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
958 		switch (c) {	/* these may or may not have an option */
959 		case 'a':
960 		case 'c':
961 		case 'p':
962 		case 'o':
963 		case 'R':
964 		case 'u':
965 		case 'v':
966 		case 'l':
967 		case 'f':
968 		case 'S':
969 			if (optarg[0] == '-') {
970 				/* this check stop a possible infinite loop */
971 				if ((optind > 1) && (argv[optind-1][1] != c))
972 					optind--;
973 				optarg = NULL;
974 			} else if (strcmp(optarg, "all") == 0)
975 				optarg = NULL;
976 		}
977 
978 		switch (c) {
979 		case 'a':
980 			exit_code += printer_query(optarg, report_accepting,
981 						encryption, verbose, 0);
982 			break;
983 		case 'c':
984 			exit_code += printer_query(optarg, report_class,
985 						encryption, verbose, 0);
986 			break;
987 		case 'p':
988 			exit_code += printer_query(optarg, report_printer,
989 						encryption, verbose,
990 						description);
991 			break;
992 		case 'd':
993 			exit_code += lpstat_default_printer(encryption);
994 			break;
995 		case 'r':
996 			exit_code += lpstat_service_status(encryption);
997 			break;
998 		case 'u':
999 			if (optarg != NULL)
1000 				users = strsplit(optarg, ", \n");
1001 			exit_code += job_query(NULL, report_job,
1002 						encryption, rank, verbose);
1003 			if (users != NULL) {
1004 				free(users);
1005 				users = NULL;
1006 			}
1007 			break;
1008 		case 'v':
1009 			exit_code += printer_query(optarg, report_device,
1010 						encryption, verbose, 0);
1011 			break;
1012 		case 'o':
1013 			exit_code += job_query(optarg, report_job,
1014 						encryption, rank, verbose);
1015 			break;
1016 		case 'f':
1017 			exit_code += service_query(optarg, report_form,
1018 						encryption, verbose);
1019 			break;
1020 		case 'S':
1021 			exit_code += service_query(optarg, report_print_wheels,
1022 						encryption, verbose);
1023 			break;
1024 		case 's':
1025 			exit_code += lpstat_service_status(encryption);
1026 			exit_code += lpstat_default_printer(encryption);
1027 			exit_code += printer_query(NULL, report_class,
1028 						encryption, verbose, 0);
1029 			exit_code += printer_query(NULL, report_device,
1030 						encryption, verbose, 0);
1031 			exit_code += service_query(optarg, report_form,
1032 						encryption, verbose);
1033 			exit_code += service_query(optarg, report_print_wheels,
1034 						encryption, verbose);
1035 			break;
1036 		case 't':
1037 			exit_code += lpstat_service_status(encryption);
1038 			exit_code += lpstat_default_printer(encryption);
1039 			exit_code += printer_query(NULL, report_class,
1040 						encryption, verbose, 0);
1041 			exit_code += printer_query(NULL, report_device,
1042 						encryption, verbose, 0);
1043 			exit_code += printer_query(NULL, report_accepting,
1044 						encryption, verbose, 0);
1045 			exit_code += printer_query(NULL, report_printer,
1046 						encryption, verbose, 0);
1047 			exit_code += service_query(optarg, report_form,
1048 						encryption, verbose);
1049 			exit_code += service_query(optarg, report_print_wheels,
1050 						encryption, verbose);
1051 			exit_code += job_query(NULL, report_job,
1052 						encryption, rank, verbose);
1053 			break;
1054 		case 'L':	/* local-only, ignored */
1055 		case 'l':	/* increased verbose level in first pass */
1056 		case 'D':	/* set "description" flag in first pass */
1057 		case 'R':	/* set "rank" flag in first pass */
1058 		case 'E':	/* set encryption in the first pass */
1059 			break;
1060 		default:
1061 			usage(av[0]);
1062 		}
1063 	}
1064 	ac--;
1065 
1066 	if (ac == 1) {	/* report on my jobs */
1067 		struct passwd *pw = getpwuid(getuid());
1068 
1069 		if (pw != NULL)
1070 			users = strsplit(pw->pw_name, "");
1071 		exit_code += job_query(NULL, report_job, encryption,
1072 					rank, verbose);
1073 		if (users != NULL) {
1074 			free(users);
1075 			users = NULL;
1076 		}
1077 	} else {
1078 		for (c = optind; c < ac; c++)
1079 			exit_code += job_query(argv[c], report_job, encryption,
1080 					rank, verbose);
1081 	}
1082 
1083 
1084 	if (exit_code != 0)
1085 		exit_code = 1;
1086 
1087 	return (exit_code);
1088 }
1089