xref: /titanic_51/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision ee9ef9e5478646701c1f0cc347324b1c7bad1efa)
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 2009 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 void
161 print_description(papi_attribute_t **list, char *printer_name)
162 {
163 	char *str = "";
164 
165 	(void) papiAttributeListGetString(list, NULL,
166 	    "printer-info", &str);
167 
168 	/*
169 	 * If no printer-info is read then
170 	 * by default the printer-info is <printer-name>@<server>
171 	 */
172 	if (str[0] == '\0') {
173 		char *uri = NULL;
174 		uri_t *u = NULL;
175 
176 		(void) papiAttributeListGetString(list, NULL,
177 		    "printer-uri-supported", &uri);
178 
179 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
180 			char *nodename = localhostname();
181 
182 			if ((u->host == NULL) ||
183 			    (strcasecmp(u->host, "localhost") == 0) ||
184 			    (strcasecmp(u->host, nodename) == 0))
185 				printf(gettext("\tDescription:\n"));
186 			else
187 				printf(gettext("\tDescription: %s@%s\n"),
188 				    printer_name, u->host);
189 
190 			uri_free(u);
191 		} else
192 			printf(gettext("\tDescription:\n"));
193 	} else
194 		printf(gettext("\tDescription: %s\n"), str);
195 }
196 
197 static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
198 					NULL };
199 /* ARGSUSED2 */
200 static int
201 report_device(papi_service_t svc, char *name, papi_printer_t printer,
202 		int verbose, int description)
203 {
204 	papi_status_t status;
205 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
206 	char *uri = NULL;
207 	char *device = NULL;
208 	uri_t *u = NULL;
209 
210 	if (name == NULL) {
211 		status = papiAttributeListGetString(attrs, NULL,
212 		    "printer-name", &name);
213 		if (status != PAPI_OK)
214 			status = papiAttributeListGetString(attrs, NULL,
215 			    "printer-uri-supported", &name);
216 	}
217 
218 	if (name == NULL)
219 		return (-1);
220 
221 	(void) papiAttributeListGetString(attrs, NULL,
222 	    "printer-uri-supported", &uri);
223 
224 	if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
225 		char *nodename = localhostname();
226 
227 		if ((u->host == NULL) ||
228 		    (strcasecmp(u->host, "localhost") == 0) ||
229 		    (strcasecmp(u->host, nodename) == 0))
230 			device = get_device_uri(svc, name);
231 
232 		if (device != NULL) {
233 			printf(gettext("device for %s: %s\n"), name, device);
234 			return (0);
235 		} else if (uri != NULL) {
236 			printf(gettext("system for %s: %s (as %s)\n"), name,
237 			    u->host, uri);
238 			return (0);
239 		}
240 
241 		uri_free(u);
242 	}
243 
244 	return (0);
245 }
246 
247 static char *report_accepting_keys[] = { "printer-name",
248 			"printer-uri-supported", "printer-is-accepting-jobs",
249 			"printer-up-time", "printer-state-time",
250 			"lpsched-reject-date", "lpsched-reject-reason", NULL };
251 /* ARGSUSED2 */
252 static int
253 report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
254 		int verbose, int description)
255 {
256 	papi_status_t status;
257 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
258 	time_t curr;
259 	char boolean = PAPI_FALSE;
260 
261 	if (name == NULL) {
262 		status = papiAttributeListGetString(attrs, NULL,
263 		    "printer-name", &name);
264 		if (status != PAPI_OK)
265 			status = papiAttributeListGetString(attrs, NULL,
266 			    "printer-uri-supported", &name);
267 	}
268 	if (name == NULL)
269 		return (-1);
270 
271 	(void) papiAttributeListGetBoolean(attrs, NULL,
272 	    "printer-is-accepting-jobs", &boolean);
273 	(void) time(&curr);
274 	(void) papiAttributeListGetDatetime(attrs, NULL,
275 	    "printer-up-time", &curr);
276 	(void) papiAttributeListGetDatetime(attrs, NULL,
277 	    "printer-state-time", &curr);
278 	(void) papiAttributeListGetDatetime(attrs, NULL,
279 	    "lpsched-reject-date", &curr);
280 
281 	if (boolean == PAPI_TRUE) {
282 		printf(gettext("%s accepting requests since %s\n"),
283 		    name, nctime(&curr));
284 	} else {
285 		char *reason = "unknown reason";
286 
287 		(void) papiAttributeListGetString(attrs, NULL,
288 		    "lpsched-reject-reason", &reason);
289 
290 		printf(gettext("%s not accepting requests since %s\n\t%s\n"),
291 		    name, nctime(&curr), reason);
292 	}
293 
294 	return (0);
295 }
296 
297 static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
298 					"member-names", NULL };
299 /* ARGSUSED2 */
300 static int
301 report_class(papi_service_t svc, char *name, papi_printer_t printer,
302 		int verbose, int description)
303 {
304 	papi_status_t status;
305 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
306 	char *member = NULL;
307 	void *iter = NULL;
308 
309 	status = papiAttributeListGetString(attrs, &iter,
310 	    "member-names", &member);
311 	if (status == PAPI_NOT_FOUND)	/* it's not a class */
312 		return (0);
313 
314 	if (name == NULL) {
315 		status = papiAttributeListGetString(attrs, NULL,
316 		    "printer-name", &name);
317 		if (status != PAPI_OK)
318 			status = papiAttributeListGetString(attrs, NULL,
319 			    "printer-uri-supported", &name);
320 	}
321 	if (name == NULL)
322 		return (-1);
323 
324 	printf(gettext("members of class %s:\n\t%s\n"), name, member);
325 	while (papiAttributeListGetString(attrs, &iter, NULL, &member)
326 	    == PAPI_OK)
327 		printf("\t%s\n", member);
328 
329 	return (0);
330 }
331 
332 static char *report_printer_keys[] = { "printer-name",
333 			"printer-uri-supported", "printer-state",
334 			"printer-up-time", "printer-state-time",
335 			"lpsched-disable-date", "printer-state-reasons",
336 			"lpsched-disable-reason", NULL };
337 /* ARGSUSED2 */
338 static int
339 report_printer(papi_service_t svc, char *name, papi_printer_t printer,
340 		int verbose, int description)
341 {
342 	papi_status_t status;
343 	papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
344 	time_t curr;
345 	int32_t pstat = 0;
346 	char *member = NULL;
347 	papi_job_t *j = NULL;
348 
349 	status = papiAttributeListGetString(attrs, NULL,
350 	    "member-names", &member);
351 	if (status == PAPI_OK)	/* it's a class */
352 		return (0);
353 
354 	if (name == NULL) {
355 		status = papiAttributeListGetString(attrs, NULL,
356 		    "printer-name", &name);
357 		if (status != PAPI_OK)
358 			status = papiAttributeListGetString(attrs, NULL,
359 			    "printer-uri-supported", &name);
360 	}
361 	if (name == NULL)
362 		return (-1);
363 
364 	printf(gettext("printer %s "), name);
365 
366 	status = papiAttributeListGetInteger(attrs, NULL,
367 	    "printer-state", &pstat);
368 
369 	switch (pstat) {
370 	case 0x03:	/* idle */
371 		printf(gettext("idle. enabled"));
372 		break;
373 	case 0x04: /* processing */
374 		status = papiPrinterListJobs(svc, name, NULL,
375 		    0, 0, &j);
376 
377 		if (status == PAPI_OK) {
378 			if (j != NULL) {
379 				int i = 0;
380 				int32_t jobid = 0;
381 				int32_t jstate = 0;
382 				int flag = 0;
383 
384 				for (i = 0; j[i] != NULL; ++i) {
385 					attrs = papiJobGetAttributeList(j[i]);
386 
387 					papiAttributeListGetInteger(attrs,
388 					    NULL, "job-state", &jstate);
389 					papiAttributeListGetInteger(attrs,
390 					    NULL, "job-id", &jobid);
391 
392 					/*
393 					 * If the job-state is not
394 					 * "held", "cancelled" or
395 					 * "completed" then only print.
396 					 */
397 					if ((jstate != 0x04) &&
398 					    (jstate != 0x07) &&
399 					    (jstate != 0x09)) {
400 						if (flag == 0)
401 							printf(gettext
402 							    ("now printing"\
403 							    " %s-%d. enabled"),
404 							    name, jobid);
405 						flag = 1;
406 					}
407 				}
408 				papiJobListFree(j);
409 			}
410 		}
411 		break;
412 	case 0x05:	/* stopped */
413 		printf(gettext("disabled"));
414 		break;
415 	default:
416 		printf(gettext("unknown state(0x%x)."), pstat);
417 		break;
418 	}
419 
420 	(void) time(&curr);
421 	(void) papiAttributeListGetDatetime(attrs, NULL,
422 	    "printer-up-time", &curr);
423 	(void) papiAttributeListGetDatetime(attrs, NULL,
424 	    "printer-state-time", &curr);
425 	(void) papiAttributeListGetDatetime(attrs, NULL,
426 	    "lpsched-disable-date", &curr);
427 	printf(gettext(" since %s. available.\n"), nctime(&curr));
428 
429 	if (pstat == 0x05) {
430 		char *reason = "unknown reason";
431 
432 		(void) papiAttributeListGetString(attrs, NULL,
433 		    "printer-state-reasons", &reason);
434 		(void) papiAttributeListGetString(attrs, NULL,
435 		    "lpsched-disable-reason", &reason);
436 		printf(gettext("\t%s\n"), reason);
437 	}
438 
439 	if (verbose == 1) {
440 		void *iter;
441 		char *str;
442 
443 		str = "";
444 		(void) papiAttributeListGetString(attrs, NULL,
445 		    "form-ready", &str);
446 		printf(gettext("\tForm mounted: %s\n"), str);
447 
448 		str = "";
449 		iter = NULL;
450 		(void) papiAttributeListGetString(attrs, &iter,
451 		    "document-format-supported", &str);
452 		printf(gettext("\tContent types: %s"), str);
453 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
454 		    == PAPI_OK)
455 			printf(", %s", str);
456 		printf("\n");
457 
458 		/* Display the printer description */
459 		print_description(attrs, name);
460 
461 		str = "";
462 		iter = NULL;
463 		(void) papiAttributeListGetString(attrs, &iter,
464 		    "lpsched-printer-type", &str);
465 		printf(gettext("\tPrinter types: %s"), str);
466 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
467 		    == PAPI_OK)
468 			printf(", %s", str);
469 		printf("\n");
470 
471 		str = "";
472 		(void) papiAttributeListGetString(attrs, NULL,
473 		    "lpsched-dial-info", &str);
474 		printf(gettext("\tConnection: %s\n"),
475 		    ((str[0] == '\0') ? gettext("direct") : str));
476 
477 		str = "";
478 		(void) papiAttributeListGetString(attrs, NULL,
479 		    "lpsched-interface-script", &str);
480 		printf(gettext("\tInterface: %s\n"), str);
481 
482 		str = NULL;
483 		(void) papiAttributeListGetString(attrs, NULL,
484 		    "ppd-file-uri", &str);
485 		(void) papiAttributeListGetString(attrs, NULL,
486 		    "lpsched-ppd-source-path", &str);
487 		if (str != NULL)
488 			printf(gettext("\tPPD: %s\n"), str);
489 
490 		str = NULL;
491 		(void) papiAttributeListGetString(attrs, NULL,
492 		    "lpsched-fault-alert-command", &str);
493 		if (str != NULL)
494 			printf(gettext("\tOn fault: %s\n"), str);
495 
496 		str = "";
497 		(void) papiAttributeListGetString(attrs, NULL,
498 		    "lpsched-fault-recovery", &str);
499 		printf(gettext("\tAfter fault: %s\n"),
500 		    ((str[0] == '\0') ? gettext("continue") : str));
501 
502 		str = "(all)";
503 		iter = NULL;
504 		(void) papiAttributeListGetString(attrs, &iter,
505 		    "requesting-user-name-allowed", &str);
506 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
507 		    ((str[0] == '\0') ? gettext("(none)") : str));
508 		if ((str != NULL) && (str[0] != '\0'))
509 			while (papiAttributeListGetString(attrs, &iter, NULL,
510 			    &str) == PAPI_OK)
511 				printf("\t\t%s\n", str);
512 
513 		str = NULL;
514 		iter = NULL;
515 		(void) papiAttributeListGetString(attrs, &iter,
516 		    "requesting-user-name-denied", &str);
517 		if (str != NULL) {
518 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
519 			    ((str[0] == '\0') ? gettext("(none)") : str));
520 			if ((str != NULL) && (str[0] != '\0'))
521 				while (papiAttributeListGetString(attrs, &iter,
522 				    NULL, &str) == PAPI_OK)
523 					printf("\t\t%s\n", str);
524 		}
525 
526 		str = "none";
527 		iter = NULL;
528 		(void) papiAttributeListGetString(attrs, &iter,
529 		    "form-supported", &str);
530 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
531 		    ((str[0] == '\0') ? gettext("none") : str));
532 		if ((str != NULL) && (str[0] != '\0'))
533 			while (papiAttributeListGetString(attrs, &iter, NULL,
534 			    &str) == PAPI_OK)
535 				printf("\t\t(%s)\n", str);
536 
537 		str = "";
538 		iter = NULL;
539 		(void) papiAttributeListGetString(attrs, &iter,
540 		    "media-supported", &str);
541 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
542 		    ((str[0] == '\0') ? gettext("(none)") : str));
543 		if ((str != NULL) && (str[0] != '\0'))
544 			while (papiAttributeListGetString(attrs, &iter, NULL,
545 			    &str) == PAPI_OK)
546 				printf("\t\t%s\n", str);
547 
548 		str = "";
549 		(void) papiAttributeListGetString(attrs, NULL,
550 		    "job-sheets-supported", &str);
551 		if ((strcasecmp(str, "none")) == 0)
552 			str = gettext("page never printed");
553 		else if (strcasecmp(str, "optional") == 0)
554 			str = gettext("not required");
555 		else
556 			str = gettext("required");
557 
558 		printf(gettext("\tBanner %s\n"), str);
559 
560 
561 		str = "";
562 		iter = NULL;
563 		(void) papiAttributeListGetString(attrs, &iter,
564 		    "lpsched-print-wheels", &str);
565 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
566 		    ((str[0] == '\0') ? gettext("(none)") : str));
567 		if ((str != NULL) && (str[0] != '\0'))
568 			while (papiAttributeListGetString(attrs, &iter, NULL,
569 			    &str) == PAPI_OK)
570 				printf("\t\t%s\n", str);
571 
572 		printf(gettext("\tDefault pitch:\n"));
573 		printf(gettext("\tDefault page size:\n"));
574 		printf(gettext("\tDefault port setting:\n"));
575 
576 		str = "";
577 		iter = NULL;
578 		(void) papiAttributeListGetString(attrs, &iter,
579 		    "lpsched-options", &str);
580 		if (str != NULL) {
581 			printf(gettext("\tOptions: %s"), str);
582 			while (papiAttributeListGetString(attrs, &iter, NULL,
583 			    &str) == PAPI_OK)
584 				printf(", %s", str);
585 			printf("\n");
586 		}
587 
588 	} else if (description == 1)
589 		/* Display printer description */
590 		print_description(attrs, name);
591 	else if (verbose > 1)
592 		papiAttributeListPrint(stdout, attrs, "\t");
593 
594 	if (verbose > 0)
595 		printf("\n");
596 
597 	return (0);
598 }
599 
600 static int
601 printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
602 					int, int), papi_encryption_t encryption,
603 		int verbose, int description)
604 {
605 	int result = 0;
606 	papi_status_t status;
607 	papi_service_t svc = NULL;
608 
609 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
610 	    encryption, NULL);
611 	if (status != PAPI_OK) {
612 		if (status == PAPI_NOT_FOUND)
613 			fprintf(stderr, gettext("%s: unknown printer\n"),
614 			    name ? name : "(NULL)");
615 		else
616 			fprintf(stderr, gettext(
617 			    "Failed to contact service for %s: %s\n"),
618 			    name ? name : "(NULL)",
619 			    verbose_papi_message(svc, status));
620 		papiServiceDestroy(svc);
621 		return (-1);
622 	}
623 
624 	if (name == NULL) { /* all */
625 		char **interest = interest_list(svc);
626 
627 		if (interest != NULL) {
628 			int i;
629 
630 			for (i = 0; interest[i] != NULL; i++)
631 				result += printer_query(interest[i], report,
632 				    encryption, verbose,
633 				    description);
634 		}
635 	} else {
636 		papi_printer_t printer = NULL;
637 		char **keys = NULL;
638 
639 		/*
640 		 * Limit the query to only required data to reduce the need
641 		 * to go remote for information.
642 		 */
643 		if (report == report_device)
644 			keys = report_device_keys;
645 		else if (report == report_class)
646 			keys = report_class_keys;
647 		else if (report == report_accepting)
648 			keys = report_accepting_keys;
649 		else if ((report == report_printer) && (verbose == 0))
650 			keys = report_printer_keys;
651 
652 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
653 		if (status != PAPI_OK) {
654 			fprintf(stderr, gettext(
655 			    "Failed to get printer info for %s: %s\n"),
656 			    name, verbose_papi_message(svc, status));
657 			papiServiceDestroy(svc);
658 			return (-1);
659 		}
660 
661 		if (printer != NULL)
662 			result = report(svc, name, printer, verbose,
663 			    description);
664 
665 		papiPrinterFree(printer);
666 	}
667 
668 	papiServiceDestroy(svc);
669 
670 	return (result);
671 }
672 
673 static int
674 match_user(char *user, char **list)
675 {
676 	int i;
677 
678 	for (i = 0; list[i] != NULL; i++) {
679 		if (strcmp(user, list[i]) == 0)
680 			return (0);
681 	}
682 
683 	return (-1);
684 }
685 
686 static char **users = NULL;
687 
688 static int
689 report_job(papi_job_t job, int show_rank, int verbose)
690 {
691 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
692 	time_t clock = 0;
693 	char date[24];
694 	char request[26];
695 	char *user = "unknown";
696 	char *host = NULL;
697 	int32_t size = 0;
698 	int32_t jstate = 0;
699 	char User[50];
700 
701 	char *destination = "unknown";
702 	int32_t id = -1;
703 
704 	(void) papiAttributeListGetString(attrs, NULL,
705 	    "job-originating-user-name", &user);
706 
707 	if ((users != NULL) && (match_user(user, users) < 0))
708 		return (0);
709 
710 	(void) papiAttributeListGetString(attrs, NULL,
711 	    "job-originating-host-name", &host);
712 
713 	if (host)
714 		snprintf(User, sizeof (User), "%s@%s", user, host);
715 	else
716 		snprintf(User, sizeof (User), "%s", user);
717 
718 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
719 	size *= 1024;	/* for the approximate byte size */
720 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
721 
722 	(void) time(&clock);
723 	(void) papiAttributeListGetInteger(attrs, NULL,
724 	    "time-at-creation", (int32_t *)&clock);
725 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
726 
727 	(void) papiAttributeListGetString(attrs, NULL,
728 	    "job-printer-uri", &destination);
729 	(void) papiAttributeListGetString(attrs, NULL,
730 	    "printer-name", &destination);
731 	(void) papiAttributeListGetInteger(attrs, NULL,
732 	    "job-id", &id);
733 	snprintf(request, sizeof (request), "%s-%d", destination, id);
734 
735 	if (show_rank != 0) {
736 		int32_t rank = -1;
737 
738 		(void) papiAttributeListGetInteger(attrs, NULL,
739 		    "number-of-intervening-jobs", &rank);
740 		rank++;
741 
742 		printf("%3d %-21s %-14s %7ld %s",
743 		    rank, request, User, size, date);
744 	} else
745 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
746 
747 	(void) papiAttributeListGetInteger(attrs, NULL,
748 	    "job-state", &jstate);
749 	if (jstate == 0x04)
750 		printf(gettext(", being held"));
751 	else if (jstate == 0x07)
752 		printf(gettext(", cancelled"));
753 	else if (jstate == 0x09)
754 		printf(gettext(", complete"));
755 
756 	if (verbose == 1) {
757 		char *form = NULL;
758 
759 		(void) papiAttributeListGetString(attrs, NULL,
760 		    "output-device-assigned", &destination);
761 		printf("\n\t assigned %s", destination);
762 
763 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
764 		if (form != NULL)
765 			printf(", form %s", form);
766 	} else if (verbose > 1) {
767 		printf("\n");
768 		papiAttributeListPrint(stdout, attrs, "\t");
769 	}
770 
771 	printf("\n");
772 
773 	return (0);
774 }
775 
776 static int
777 job_query(char *request, int (*report)(papi_job_t, int, int),
778 		papi_encryption_t encryption, int show_rank, int verbose)
779 {
780 	int result = 0;
781 	papi_status_t status;
782 	papi_service_t svc = NULL;
783 	char *printer = request;
784 	int32_t id = -1;
785 	int flag1 = 0;
786 	int flag = 1;
787 	int print_flag = 0;
788 
789 	do {
790 		status = papiServiceCreate(&svc, printer, NULL, NULL,
791 		    cli_auth_callback, encryption, NULL);
792 
793 		if ((status == PAPI_OK) && (printer != NULL))
794 			print_flag = 1;
795 
796 		/* <name>-# printer name does not exist */
797 		if (status != PAPI_OK) {
798 			/*
799 			 * Check if <name>-# is a request-id
800 			 * Once this check is done flag1 is set
801 			 */
802 			if (flag1 == 1)
803 				break;
804 
805 			get_printer_id(printer, &printer, &id);
806 
807 			status = papiServiceCreate(&svc, printer, NULL, NULL,
808 			    cli_auth_callback, encryption, NULL);
809 
810 			if (status != PAPI_OK) {
811 				fprintf(stderr, gettext(
812 				    "Failed to contact service for %s: %s\n"),
813 				    (printer ? printer : "all"),
814 				    verbose_papi_message(svc, status));
815 				return (-1);
816 			}
817 		}
818 
819 		if (printer == NULL) { /* all */
820 			char **interest = interest_list(svc);
821 
822 			if (interest != NULL) {
823 				int i;
824 
825 				for (i = 0; interest[i] != NULL; i++)
826 					result += job_query(interest[i], report,
827 					    encryption, show_rank, verbose);
828 			}
829 		} else if (id == -1) { /* a printer */
830 			papi_job_t *jobs = NULL;
831 
832 			status = papiPrinterListJobs(svc, printer, NULL,
833 			    0, 0, &jobs);
834 			if (status != PAPI_OK) {
835 				fprintf(stderr, gettext(
836 				    "Failed to get job list: %s\n"),
837 				    verbose_papi_message(svc, status));
838 				papiServiceDestroy(svc);
839 				return (-1);
840 			}
841 
842 			if (jobs != NULL) {
843 				int i;
844 
845 				for (i = 0; jobs[i] != NULL; i++)
846 					result += report(jobs[i],
847 					    show_rank, verbose);
848 			}
849 
850 			papiJobListFree(jobs);
851 		} else {	/* a job */
852 			papi_job_t job = NULL;
853 
854 			/* Once a job has been found stop processing */
855 			flag = 0;
856 
857 			status = papiJobQuery(svc, printer, id, NULL, &job);
858 			if (status != PAPI_OK) {
859 				if (!print_flag)
860 					fprintf(stderr, gettext(
861 					    "Failed to get job"\
862 					    " info for %s: %s\n"),
863 					    request,
864 					    verbose_papi_message(svc, status));
865 				papiServiceDestroy(svc);
866 				return (-1);
867 			}
868 
869 			if (job != NULL)
870 				result = report(job, show_rank, verbose);
871 
872 			papiJobFree(job);
873 		}
874 
875 		if (flag) {
876 			id = -1;
877 			get_printer_id(printer, &printer, &id);
878 			if (id == -1)
879 				flag = 0;
880 			else
881 				flag1 = 1;
882 		}
883 	} while (flag);
884 
885 	papiServiceDestroy(svc);
886 
887 	return (result);
888 }
889 
890 static int
891 report_form(char *name, papi_attribute_t **attrs, int verbose)
892 {
893 	papi_status_t status;
894 	char *form = NULL;
895 	void *iter = NULL;
896 
897 	for (status = papiAttributeListGetString(attrs, &iter,
898 	    "form-supported", &form);
899 	    status == PAPI_OK;
900 	    status = papiAttributeListGetString(attrs, &iter,
901 	    NULL, &form)) {
902 		if ((name == NULL) || (strcmp(name, form) == 0)) {
903 			printf(gettext("form %s is available to you\n"), form);
904 			if (verbose != 0) {
905 				char *detail = NULL;
906 				status = papiAttributeListGetString(attrs, NULL,
907 				    "form-supported-detail", &detail);
908 				if (status == PAPI_OK)
909 					printf("%s\n", detail);
910 			}
911 		}
912 	}
913 
914 	return (0);
915 }
916 
917 static int
918 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
919 {
920 	papi_status_t status;
921 	char *pw = NULL;
922 	void *iter = NULL;
923 
924 	for (status = papiAttributeListGetString(attrs, &iter,
925 	    "pw-supported", &pw);
926 	    status == PAPI_OK;
927 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
928 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
929 			printf(gettext("charset %s is available\n"), pw);
930 			if (verbose != 0) {
931 				char *info = NULL;
932 				status = papiAttributeListGetString(attrs, NULL,
933 				    "pw-supported-extra", &info);
934 				if (status == PAPI_OK)
935 					printf("%s\n", info);
936 			}
937 		}
938 	}
939 
940 	return (0);
941 }
942 
943 static int
944 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
945 		papi_encryption_t encryption, int verbose)
946 {
947 	int result = 0;
948 	papi_status_t status;
949 	papi_service_t svc = NULL;
950 	papi_attribute_t **attrs = NULL;
951 
952 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
953 	    encryption, NULL);
954 	if (status != PAPI_OK) {
955 		papiServiceDestroy(svc);
956 		return (-1);
957 	}
958 
959 	attrs = papiServiceGetAttributeList(svc);
960 	if (attrs != NULL) {
961 		result = report(name, attrs, verbose);
962 
963 		if (verbose > 1) {
964 			printf("\n");
965 			papiAttributeListPrint(stdout, attrs, "\t");
966 			printf("\n");
967 		}
968 	}
969 
970 	papiServiceDestroy(svc);
971 
972 	return (result);
973 }
974 
975 int
976 main(int ac, char *av[])
977 {
978 	int exit_code = 0;
979 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
980 	int rank = 0;
981 	int verbose = 0;
982 	int description = 0;
983 	int c;
984 	char **argv;
985 
986 	(void) setlocale(LC_ALL, "");
987 	(void) textdomain("SUNW_OST_OSCMD");
988 
989 	argv = (char **)calloc((ac + 1), sizeof (char *));
990 	for (c = 0; c < ac; c++) {
991 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
992 		    (isalpha(av[c][2]) != 0)) {
993 			/* preserve old "-l[po...]" behavior */
994 			argv[c] = &av[c][1];
995 			argv[c][0] = '-';
996 			verbose = 1;
997 
998 		} else
999 			argv[c] = av[c];
1000 	}
1001 
1002 	argv[c++] = "--";
1003 	ac = c;
1004 
1005 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
1006 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
1007 		switch (c) {
1008 		case 'l':
1009 			if ((optarg == NULL) || (optarg[0] == '-'))
1010 				optarg = "1";
1011 			verbose = atoi(optarg);
1012 			break;
1013 		case 'D':
1014 			description = 1;
1015 			break;
1016 		case 'R':
1017 			rank = 1;
1018 			break;
1019 		case 'E':
1020 			encryption = PAPI_ENCRYPT_REQUIRED;
1021 			break;
1022 		default:
1023 			break;
1024 		}
1025 	optind = 1;
1026 
1027 	/* process command line arguments */
1028 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
1029 		switch (c) {	/* these may or may not have an option */
1030 		case 'a':
1031 		case 'c':
1032 		case 'p':
1033 		case 'o':
1034 		case 'R':
1035 		case 'u':
1036 		case 'v':
1037 		case 'l':
1038 		case 'f':
1039 		case 'S':
1040 			if (optarg[0] == '-') {
1041 				/* this check stop a possible infinite loop */
1042 				if ((optind > 1) && (argv[optind-1][1] != c))
1043 					optind--;
1044 				optarg = NULL;
1045 			} else if (strcmp(optarg, "all") == 0)
1046 				optarg = NULL;
1047 		}
1048 
1049 		switch (c) {
1050 		case 'a':
1051 			exit_code += printer_query(optarg, report_accepting,
1052 			    encryption, verbose, 0);
1053 			break;
1054 		case 'c':
1055 			exit_code += printer_query(optarg, report_class,
1056 			    encryption, verbose, 0);
1057 			break;
1058 		case 'p':
1059 			exit_code += printer_query(optarg, report_printer,
1060 			    encryption, verbose, description);
1061 			break;
1062 		case 'd':
1063 			exit_code += lpstat_default_printer(encryption);
1064 			break;
1065 		case 'r':
1066 			exit_code += lpstat_service_status(encryption);
1067 			break;
1068 		case 'u':
1069 			if (optarg != NULL)
1070 				users = strsplit(optarg, ", \n");
1071 			exit_code += job_query(NULL, report_job,
1072 			    encryption, rank, verbose);
1073 			if (users != NULL) {
1074 				free(users);
1075 				users = NULL;
1076 			}
1077 			break;
1078 		case 'v':
1079 			exit_code += printer_query(optarg, report_device,
1080 			    encryption, verbose, 0);
1081 			break;
1082 		case 'R':	/* set "rank" flag in first pass */
1083 		case 'o':
1084 			exit_code += job_query(optarg, report_job,
1085 			    encryption, rank, verbose);
1086 			break;
1087 		case 'f':
1088 			exit_code += service_query(optarg, report_form,
1089 			    encryption, verbose);
1090 			break;
1091 		case 'S':
1092 			exit_code += service_query(optarg, report_print_wheels,
1093 			    encryption, verbose);
1094 			break;
1095 		case 's':
1096 			exit_code += lpstat_service_status(encryption);
1097 			exit_code += lpstat_default_printer(encryption);
1098 			exit_code += printer_query(NULL, report_class,
1099 			    encryption, verbose, 0);
1100 			exit_code += printer_query(NULL, report_device,
1101 			    encryption, verbose, 0);
1102 			exit_code += service_query(optarg, report_form,
1103 			    encryption, verbose);
1104 			exit_code += service_query(optarg, report_print_wheels,
1105 			    encryption, verbose);
1106 			break;
1107 		case 't':
1108 			exit_code += lpstat_service_status(encryption);
1109 			exit_code += lpstat_default_printer(encryption);
1110 			exit_code += printer_query(NULL, report_class,
1111 			    encryption, verbose, 0);
1112 			exit_code += printer_query(NULL, report_device,
1113 			    encryption, verbose, 0);
1114 			exit_code += printer_query(NULL, report_accepting,
1115 			    encryption, verbose, 0);
1116 			exit_code += printer_query(NULL, report_printer,
1117 			    encryption, verbose, 0);
1118 			exit_code += service_query(optarg, report_form,
1119 			    encryption, verbose);
1120 			exit_code += service_query(optarg, report_print_wheels,
1121 			    encryption, verbose);
1122 			exit_code += job_query(NULL, report_job,
1123 			    encryption, rank, verbose);
1124 			break;
1125 		case 'L':	/* local-only, ignored */
1126 		case 'l':	/* increased verbose level in first pass */
1127 		case 'D':	/* set "description" flag in first pass */
1128 		case 'E':	/* set encryption in the first pass */
1129 			break;
1130 		default:
1131 			usage(av[0]);
1132 		}
1133 	}
1134 	ac--;
1135 
1136 	if (ac == 1) {	/* report on my jobs */
1137 		struct passwd *pw = getpwuid(getuid());
1138 
1139 		if (pw != NULL)
1140 			users = strsplit(pw->pw_name, "");
1141 		exit_code += job_query(NULL, report_job, encryption,
1142 		    rank, verbose);
1143 		if (users != NULL) {
1144 			free(users);
1145 			users = NULL;
1146 		}
1147 	} else {
1148 		for (c = optind; c < ac; c++)
1149 			exit_code += job_query(argv[c], report_job, encryption,
1150 			    rank, verbose);
1151 	}
1152 
1153 
1154 	if (exit_code != 0)
1155 		exit_code = 1;
1156 
1157 	return (exit_code);
1158 }
1159