xref: /titanic_51/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 3d09a4fec6be19a6f09e277d5d5d17942bb4abf4)
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 	static int check = 0;
704 	static char *uri = NULL;
705 
706 	(void) papiAttributeListGetString(attrs, NULL,
707 	    "job-originating-user-name", &user);
708 
709 	if ((users != NULL) && (match_user(user, users) < 0))
710 		return (0);
711 
712 	(void) papiAttributeListGetString(attrs, NULL,
713 	    "job-originating-host-name", &host);
714 
715 	if (check == 0) {
716 		/*
717 		 * Read the attribute "job-printer-uri"
718 		 * just once
719 		 */
720 		(void) papiAttributeListGetString(attrs, NULL,
721 		    "job-printer-uri", &uri);
722 		check = 1;
723 	}
724 
725 	if (host) {
726 		/* Check if it is local printer or remote printer */
727 		uri_t *u = NULL;
728 
729 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
730 			char *nodename = localhostname();
731 
732 			if ((u->host == NULL) ||
733 			    (strcasecmp(u->host, "localhost") == 0) ||
734 			    (strcasecmp(u->host, nodename) == 0)) {
735 
736 				if (strcasecmp(host, nodename) == 0) {
737 					/*
738 					 * Request submitted locally
739 					 * for the local queue.
740 					 * Hostname will not be displayed
741 					 */
742 					snprintf(User, sizeof (User), "%s",
743 					    user);
744 				}
745 				else
746 					snprintf(User, sizeof (User), "%s@%s",
747 					    user, host);
748 			} else if (uri != NULL) {
749 				/*
750 				 * It's a remote printer.
751 				 * In case of remote printers hostname is
752 				 * always displayed.
753 				 */
754 				snprintf(User, sizeof (User), "%s@%s",
755 				    user, host);
756 			}
757 			uri_free(u);
758 		} else {
759 			/*
760 			 * If attribute "job-printer-uri"
761 			 * cannot be read
762 			 * by default append the hostname
763 			 */
764 			snprintf(User, sizeof (User), "%s@%s", user, host);
765 		}
766 	} else {
767 		/*
768 		 * When print server is s10u4 and ipp service is used
769 		 * "job-originating-hostname" attribute is not set
770 		 * So get the host information from the uri
771 		 */
772 		uri_t *u = NULL;
773 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
774 			if ((u != NULL) && (u->host != NULL))
775 				snprintf(User, sizeof (User), "%s@%s",
776 				    user, u->host);
777 			else
778 				snprintf(User, sizeof (User), "%s", user);
779 
780 			uri_free(u);
781 		} else
782 			snprintf(User, sizeof (User), "%s", user);
783 	}
784 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
785 	size *= 1024;	/* for the approximate byte size */
786 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
787 
788 	(void) time(&clock);
789 	(void) papiAttributeListGetInteger(attrs, NULL,
790 	    "time-at-creation", (int32_t *)&clock);
791 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
792 
793 	(void) papiAttributeListGetString(attrs, NULL,
794 	    "job-printer-uri", &destination);
795 	(void) papiAttributeListGetString(attrs, NULL,
796 	    "printer-name", &destination);
797 	(void) papiAttributeListGetInteger(attrs, NULL,
798 	    "job-id", &id);
799 	snprintf(request, sizeof (request), "%s-%d", destination, id);
800 
801 	if (show_rank != 0) {
802 		int32_t rank = -1;
803 
804 		(void) papiAttributeListGetInteger(attrs, NULL,
805 		    "number-of-intervening-jobs", &rank);
806 		rank++;
807 
808 		printf("%3d %-21s %-14s %7ld %s",
809 		    rank, request, User, size, date);
810 	} else
811 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
812 
813 	(void) papiAttributeListGetInteger(attrs, NULL,
814 	    "job-state", &jstate);
815 	if (jstate == 0x04)
816 		printf(gettext(", being held"));
817 	else if (jstate == 0x07)
818 		printf(gettext(", cancelled"));
819 	else if (jstate == 0x09)
820 		printf(gettext(", complete"));
821 
822 	if (verbose == 1) {
823 		char *form = NULL;
824 
825 		(void) papiAttributeListGetString(attrs, NULL,
826 		    "output-device-assigned", &destination);
827 		printf("\n\t assigned %s", destination);
828 
829 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
830 		if (form != NULL)
831 			printf(", form %s", form);
832 	} else if (verbose > 1) {
833 		printf("\n");
834 		papiAttributeListPrint(stdout, attrs, "\t");
835 	}
836 
837 	printf("\n");
838 
839 	return (0);
840 }
841 
842 static int
843 job_query(char *request, int (*report)(papi_job_t, int, int),
844 		papi_encryption_t encryption, int show_rank, int verbose)
845 {
846 	int result = 0;
847 	papi_status_t status;
848 	papi_service_t svc = NULL;
849 	char *printer = request;
850 	int32_t id = -1;
851 	int flag1 = 0;
852 	int flag = 1;
853 	int print_flag = 0;
854 
855 	do {
856 		status = papiServiceCreate(&svc, printer, NULL, NULL,
857 		    cli_auth_callback, encryption, NULL);
858 
859 		if ((status == PAPI_OK) && (printer != NULL))
860 			print_flag = 1;
861 
862 		/* <name>-# printer name does not exist */
863 		if (status != PAPI_OK) {
864 			/*
865 			 * Check if <name>-# is a request-id
866 			 * Once this check is done flag1 is set
867 			 */
868 			if (flag1 == 1)
869 				break;
870 
871 			get_printer_id(printer, &printer, &id);
872 
873 			status = papiServiceCreate(&svc, printer, NULL, NULL,
874 			    cli_auth_callback, encryption, NULL);
875 
876 			if (status != PAPI_OK) {
877 				fprintf(stderr, gettext(
878 				    "Failed to contact service for %s: %s\n"),
879 				    (printer ? printer : "all"),
880 				    verbose_papi_message(svc, status));
881 				return (-1);
882 			}
883 		}
884 
885 		if (printer == NULL) { /* all */
886 			char **interest = interest_list(svc);
887 
888 			if (interest != NULL) {
889 				int i;
890 
891 				for (i = 0; interest[i] != NULL; i++)
892 					result += job_query(interest[i], report,
893 					    encryption, show_rank, verbose);
894 			}
895 		} else if (id == -1) { /* a printer */
896 			papi_job_t *jobs = NULL;
897 
898 			status = papiPrinterListJobs(svc, printer, NULL,
899 			    0, 0, &jobs);
900 			if (status != PAPI_OK) {
901 				fprintf(stderr, gettext(
902 				    "Failed to get job list: %s\n"),
903 				    verbose_papi_message(svc, status));
904 				papiServiceDestroy(svc);
905 				return (-1);
906 			}
907 
908 			if (jobs != NULL) {
909 				int i;
910 
911 				for (i = 0; jobs[i] != NULL; i++)
912 					result += report(jobs[i],
913 					    show_rank, verbose);
914 			}
915 
916 			papiJobListFree(jobs);
917 		} else {	/* a job */
918 			papi_job_t job = NULL;
919 
920 			/* Once a job has been found stop processing */
921 			flag = 0;
922 
923 			status = papiJobQuery(svc, printer, id, NULL, &job);
924 			if (status != PAPI_OK) {
925 				if (!print_flag)
926 					fprintf(stderr, gettext(
927 					    "Failed to get job"\
928 					    " info for %s: %s\n"),
929 					    request,
930 					    verbose_papi_message(svc, status));
931 				papiServiceDestroy(svc);
932 				return (-1);
933 			}
934 
935 			if (job != NULL)
936 				result = report(job, show_rank, verbose);
937 
938 			papiJobFree(job);
939 		}
940 
941 		if (flag) {
942 			id = -1;
943 			get_printer_id(printer, &printer, &id);
944 			if (id == -1)
945 				flag = 0;
946 			else
947 				flag1 = 1;
948 		}
949 	} while (flag);
950 
951 	papiServiceDestroy(svc);
952 
953 	return (result);
954 }
955 
956 static int
957 report_form(char *name, papi_attribute_t **attrs, int verbose)
958 {
959 	papi_status_t status;
960 	char *form = NULL;
961 	void *iter = NULL;
962 
963 	for (status = papiAttributeListGetString(attrs, &iter,
964 	    "form-supported", &form);
965 	    status == PAPI_OK;
966 	    status = papiAttributeListGetString(attrs, &iter,
967 	    NULL, &form)) {
968 		if ((name == NULL) || (strcmp(name, form) == 0)) {
969 			printf(gettext("form %s is available to you\n"), form);
970 			if (verbose != 0) {
971 				char *detail = NULL;
972 				status = papiAttributeListGetString(attrs, NULL,
973 				    "form-supported-detail", &detail);
974 				if (status == PAPI_OK)
975 					printf("%s\n", detail);
976 			}
977 		}
978 	}
979 
980 	return (0);
981 }
982 
983 static int
984 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
985 {
986 	papi_status_t status;
987 	char *pw = NULL;
988 	void *iter = NULL;
989 
990 	for (status = papiAttributeListGetString(attrs, &iter,
991 	    "pw-supported", &pw);
992 	    status == PAPI_OK;
993 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
994 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
995 			printf(gettext("charset %s is available\n"), pw);
996 			if (verbose != 0) {
997 				char *info = NULL;
998 				status = papiAttributeListGetString(attrs, NULL,
999 				    "pw-supported-extra", &info);
1000 				if (status == PAPI_OK)
1001 					printf("%s\n", info);
1002 			}
1003 		}
1004 	}
1005 
1006 	return (0);
1007 }
1008 
1009 static int
1010 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
1011 		papi_encryption_t encryption, int verbose)
1012 {
1013 	int result = 0;
1014 	papi_status_t status;
1015 	papi_service_t svc = NULL;
1016 	papi_attribute_t **attrs = NULL;
1017 
1018 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1019 	    encryption, NULL);
1020 	if (status != PAPI_OK) {
1021 		papiServiceDestroy(svc);
1022 		return (-1);
1023 	}
1024 
1025 	attrs = papiServiceGetAttributeList(svc);
1026 	if (attrs != NULL) {
1027 		result = report(name, attrs, verbose);
1028 
1029 		if (verbose > 1) {
1030 			printf("\n");
1031 			papiAttributeListPrint(stdout, attrs, "\t");
1032 			printf("\n");
1033 		}
1034 	}
1035 
1036 	papiServiceDestroy(svc);
1037 
1038 	return (result);
1039 }
1040 
1041 int
1042 main(int ac, char *av[])
1043 {
1044 	int exit_code = 0;
1045 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
1046 	int rank = 0;
1047 	int verbose = 0;
1048 	int description = 0;
1049 	int c;
1050 	char **argv;
1051 
1052 	(void) setlocale(LC_ALL, "");
1053 	(void) textdomain("SUNW_OST_OSCMD");
1054 
1055 	argv = (char **)calloc((ac + 1), sizeof (char *));
1056 	for (c = 0; c < ac; c++) {
1057 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
1058 		    (isalpha(av[c][2]) != 0)) {
1059 			/* preserve old "-l[po...]" behavior */
1060 			argv[c] = &av[c][1];
1061 			argv[c][0] = '-';
1062 			verbose = 1;
1063 
1064 		} else
1065 			argv[c] = av[c];
1066 	}
1067 
1068 	argv[c++] = "--";
1069 	ac = c;
1070 
1071 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
1072 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
1073 		switch (c) {
1074 		case 'l':
1075 			if ((optarg == NULL) || (optarg[0] == '-'))
1076 				optarg = "1";
1077 			verbose = atoi(optarg);
1078 			break;
1079 		case 'D':
1080 			description = 1;
1081 			break;
1082 		case 'R':
1083 			rank = 1;
1084 			break;
1085 		case 'E':
1086 			encryption = PAPI_ENCRYPT_REQUIRED;
1087 			break;
1088 		default:
1089 			break;
1090 		}
1091 	optind = 1;
1092 
1093 	/* process command line arguments */
1094 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
1095 		switch (c) {	/* these may or may not have an option */
1096 		case 'a':
1097 		case 'c':
1098 		case 'p':
1099 		case 'o':
1100 		case 'R':
1101 		case 'u':
1102 		case 'v':
1103 		case 'l':
1104 		case 'f':
1105 		case 'S':
1106 			if (optarg[0] == '-') {
1107 				/* this check stop a possible infinite loop */
1108 				if ((optind > 1) && (argv[optind-1][1] != c))
1109 					optind--;
1110 				optarg = NULL;
1111 			} else if (strcmp(optarg, "all") == 0)
1112 				optarg = NULL;
1113 		}
1114 
1115 		switch (c) {
1116 		case 'a':
1117 			exit_code += printer_query(optarg, report_accepting,
1118 			    encryption, verbose, 0);
1119 			break;
1120 		case 'c':
1121 			exit_code += printer_query(optarg, report_class,
1122 			    encryption, verbose, 0);
1123 			break;
1124 		case 'p':
1125 			exit_code += printer_query(optarg, report_printer,
1126 			    encryption, verbose, description);
1127 			break;
1128 		case 'd':
1129 			exit_code += lpstat_default_printer(encryption);
1130 			break;
1131 		case 'r':
1132 			exit_code += lpstat_service_status(encryption);
1133 			break;
1134 		case 'u':
1135 			if (optarg != NULL)
1136 				users = strsplit(optarg, ", \n");
1137 			exit_code += job_query(NULL, report_job,
1138 			    encryption, rank, verbose);
1139 			if (users != NULL) {
1140 				free(users);
1141 				users = NULL;
1142 			}
1143 			break;
1144 		case 'v':
1145 			exit_code += printer_query(optarg, report_device,
1146 			    encryption, verbose, 0);
1147 			break;
1148 		case 'R':	/* set "rank" flag in first pass */
1149 		case 'o':
1150 			exit_code += job_query(optarg, report_job,
1151 			    encryption, rank, verbose);
1152 			break;
1153 		case 'f':
1154 			exit_code += service_query(optarg, report_form,
1155 			    encryption, verbose);
1156 			break;
1157 		case 'S':
1158 			exit_code += service_query(optarg, report_print_wheels,
1159 			    encryption, verbose);
1160 			break;
1161 		case 's':
1162 			exit_code += lpstat_service_status(encryption);
1163 			exit_code += lpstat_default_printer(encryption);
1164 			exit_code += printer_query(NULL, report_class,
1165 			    encryption, verbose, 0);
1166 			exit_code += printer_query(NULL, report_device,
1167 			    encryption, verbose, 0);
1168 			exit_code += service_query(optarg, report_form,
1169 			    encryption, verbose);
1170 			exit_code += service_query(optarg, report_print_wheels,
1171 			    encryption, verbose);
1172 			break;
1173 		case 't':
1174 			exit_code += lpstat_service_status(encryption);
1175 			exit_code += lpstat_default_printer(encryption);
1176 			exit_code += printer_query(NULL, report_class,
1177 			    encryption, verbose, 0);
1178 			exit_code += printer_query(NULL, report_device,
1179 			    encryption, verbose, 0);
1180 			exit_code += printer_query(NULL, report_accepting,
1181 			    encryption, verbose, 0);
1182 			exit_code += printer_query(NULL, report_printer,
1183 			    encryption, verbose, 0);
1184 			exit_code += service_query(optarg, report_form,
1185 			    encryption, verbose);
1186 			exit_code += service_query(optarg, report_print_wheels,
1187 			    encryption, verbose);
1188 			exit_code += job_query(NULL, report_job,
1189 			    encryption, rank, verbose);
1190 			break;
1191 		case 'L':	/* local-only, ignored */
1192 		case 'l':	/* increased verbose level in first pass */
1193 		case 'D':	/* set "description" flag in first pass */
1194 		case 'E':	/* set encryption in the first pass */
1195 			break;
1196 		default:
1197 			usage(av[0]);
1198 		}
1199 	}
1200 	ac--;
1201 
1202 	if (ac == 1) {	/* report on my jobs */
1203 		struct passwd *pw = getpwuid(getuid());
1204 
1205 		if (pw != NULL)
1206 			users = strsplit(pw->pw_name, "");
1207 		exit_code += job_query(NULL, report_job, encryption,
1208 		    rank, verbose);
1209 		if (users != NULL) {
1210 			free(users);
1211 			users = NULL;
1212 		}
1213 	} else {
1214 		for (c = optind; c < ac; c++)
1215 			exit_code += job_query(argv[c], report_job, encryption,
1216 			    rank, verbose);
1217 	}
1218 
1219 
1220 	if (exit_code != 0)
1221 		exit_code = 1;
1222 
1223 	return (exit_code);
1224 }
1225