xref: /titanic_51/usr/src/cmd/print/bsd-sysv-commands/lpstat.c (revision 6d0c08ff2e4569eb21185939268b24b2f2bb102a)
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 					papi_attribute_t **attr =
386 					    papiJobGetAttributeList(j[i]);
387 
388 					papiAttributeListGetInteger(attr,
389 					    NULL, "job-state", &jstate);
390 					papiAttributeListGetInteger(attr,
391 					    NULL, "job-id", &jobid);
392 
393 					/*
394 					 * If the job-state is in
395 					 * RS_PRINTING then only print.
396 					 */
397 					if (jstate == 0x0008) {
398 						if (flag == 0)
399 							printf(gettext
400 							    ("now printing"\
401 							    " %s-%d. enabled"),
402 							    name, jobid);
403 						flag = 1;
404 					}
405 				}
406 				papiJobListFree(j);
407 			}
408 		}
409 		break;
410 	case 0x05:	/* stopped */
411 		printf(gettext("disabled"));
412 		break;
413 	default:
414 		printf(gettext("unknown state(0x%x)."), pstat);
415 		break;
416 	}
417 
418 	(void) time(&curr);
419 	(void) papiAttributeListGetDatetime(attrs, NULL,
420 	    "printer-up-time", &curr);
421 	(void) papiAttributeListGetDatetime(attrs, NULL,
422 	    "printer-state-time", &curr);
423 	(void) papiAttributeListGetDatetime(attrs, NULL,
424 	    "lpsched-disable-date", &curr);
425 	printf(gettext(" since %s. available.\n"), nctime(&curr));
426 
427 	if (pstat == 0x05) {
428 		char *reason = "unknown reason";
429 
430 		(void) papiAttributeListGetString(attrs, NULL,
431 		    "printer-state-reasons", &reason);
432 		(void) papiAttributeListGetString(attrs, NULL,
433 		    "lpsched-disable-reason", &reason);
434 		printf(gettext("\t%s\n"), reason);
435 	}
436 
437 	if (verbose == 1) {
438 		void *iter;
439 		char *str;
440 
441 		str = "";
442 		(void) papiAttributeListGetString(attrs, NULL,
443 		    "form-ready", &str);
444 		printf(gettext("\tForm mounted: %s\n"), str);
445 
446 		str = "";
447 		iter = NULL;
448 		(void) papiAttributeListGetString(attrs, &iter,
449 		    "document-format-supported", &str);
450 		printf(gettext("\tContent types: %s"), str);
451 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
452 		    == PAPI_OK)
453 			printf(", %s", str);
454 		printf("\n");
455 
456 		/* Display the printer description */
457 		print_description(attrs, name);
458 
459 		str = "";
460 		iter = NULL;
461 		(void) papiAttributeListGetString(attrs, &iter,
462 		    "lpsched-printer-type", &str);
463 		printf(gettext("\tPrinter types: %s"), str);
464 		while (papiAttributeListGetString(attrs, &iter, NULL, &str)
465 		    == PAPI_OK)
466 			printf(", %s", str);
467 		printf("\n");
468 
469 		str = "";
470 		(void) papiAttributeListGetString(attrs, NULL,
471 		    "lpsched-dial-info", &str);
472 		printf(gettext("\tConnection: %s\n"),
473 		    ((str[0] == '\0') ? gettext("direct") : str));
474 
475 		str = "";
476 		(void) papiAttributeListGetString(attrs, NULL,
477 		    "lpsched-interface-script", &str);
478 		printf(gettext("\tInterface: %s\n"), str);
479 
480 		str = NULL;
481 		(void) papiAttributeListGetString(attrs, NULL,
482 		    "ppd-file-uri", &str);
483 		(void) papiAttributeListGetString(attrs, NULL,
484 		    "lpsched-ppd-source-path", &str);
485 		if (str != NULL)
486 			printf(gettext("\tPPD: %s\n"), str);
487 
488 		str = NULL;
489 		(void) papiAttributeListGetString(attrs, NULL,
490 		    "lpsched-fault-alert-command", &str);
491 		if (str != NULL)
492 			printf(gettext("\tOn fault: %s\n"), str);
493 
494 		str = "";
495 		(void) papiAttributeListGetString(attrs, NULL,
496 		    "lpsched-fault-recovery", &str);
497 		printf(gettext("\tAfter fault: %s\n"),
498 		    ((str[0] == '\0') ? gettext("continue") : str));
499 
500 		str = "(all)";
501 		iter = NULL;
502 		(void) papiAttributeListGetString(attrs, &iter,
503 		    "requesting-user-name-allowed", &str);
504 		printf(gettext("\tUsers allowed:\n\t\t%s\n"),
505 		    ((str[0] == '\0') ? gettext("(none)") : str));
506 		if ((str != NULL) && (str[0] != '\0'))
507 			while (papiAttributeListGetString(attrs, &iter, NULL,
508 			    &str) == PAPI_OK)
509 				printf("\t\t%s\n", str);
510 
511 		str = NULL;
512 		iter = NULL;
513 		(void) papiAttributeListGetString(attrs, &iter,
514 		    "requesting-user-name-denied", &str);
515 		if (str != NULL) {
516 			printf(gettext("\tUsers denied:\n\t\t%s\n"),
517 			    ((str[0] == '\0') ? gettext("(none)") : str));
518 			if ((str != NULL) && (str[0] != '\0'))
519 				while (papiAttributeListGetString(attrs, &iter,
520 				    NULL, &str) == PAPI_OK)
521 					printf("\t\t%s\n", str);
522 		}
523 
524 		str = "none";
525 		iter = NULL;
526 		(void) papiAttributeListGetString(attrs, &iter,
527 		    "form-supported", &str);
528 		printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
529 		    ((str[0] == '\0') ? gettext("none") : str));
530 		if ((str != NULL) && (str[0] != '\0'))
531 			while (papiAttributeListGetString(attrs, &iter, NULL,
532 			    &str) == PAPI_OK)
533 				printf("\t\t(%s)\n", str);
534 
535 		str = "";
536 		iter = NULL;
537 		(void) papiAttributeListGetString(attrs, &iter,
538 		    "media-supported", &str);
539 		printf(gettext("\tMedia supported:\n\t\t%s\n"),
540 		    ((str[0] == '\0') ? gettext("(none)") : str));
541 		if ((str != NULL) && (str[0] != '\0'))
542 			while (papiAttributeListGetString(attrs, &iter, NULL,
543 			    &str) == PAPI_OK)
544 				printf("\t\t%s\n", str);
545 
546 		str = "";
547 		(void) papiAttributeListGetString(attrs, NULL,
548 		    "job-sheets-supported", &str);
549 		if ((strcasecmp(str, "none")) == 0)
550 			str = gettext("page never printed");
551 		else if (strcasecmp(str, "optional") == 0)
552 			str = gettext("not required");
553 		else
554 			str = gettext("required");
555 
556 		printf(gettext("\tBanner %s\n"), str);
557 
558 
559 		str = "";
560 		iter = NULL;
561 		(void) papiAttributeListGetString(attrs, &iter,
562 		    "lpsched-print-wheels", &str);
563 		printf(gettext("\tCharacter sets:\n\t\t%s\n"),
564 		    ((str[0] == '\0') ? gettext("(none)") : str));
565 		if ((str != NULL) && (str[0] != '\0'))
566 			while (papiAttributeListGetString(attrs, &iter, NULL,
567 			    &str) == PAPI_OK)
568 				printf("\t\t%s\n", str);
569 
570 		printf(gettext("\tDefault pitch:\n"));
571 		printf(gettext("\tDefault page size:\n"));
572 		printf(gettext("\tDefault port setting:\n"));
573 
574 		str = "";
575 		iter = NULL;
576 		(void) papiAttributeListGetString(attrs, &iter,
577 		    "lpsched-options", &str);
578 		if (str != NULL) {
579 			printf(gettext("\tOptions: %s"), str);
580 			while (papiAttributeListGetString(attrs, &iter, NULL,
581 			    &str) == PAPI_OK)
582 				printf(", %s", str);
583 			printf("\n");
584 		}
585 
586 	} else if (description == 1)
587 		/* Display printer description */
588 		print_description(attrs, name);
589 	else if (verbose > 1)
590 		papiAttributeListPrint(stdout, attrs, "\t");
591 
592 	if (verbose > 0)
593 		printf("\n");
594 
595 	return (0);
596 }
597 
598 static int
599 printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
600 					int, int), papi_encryption_t encryption,
601 		int verbose, int description)
602 {
603 	int result = 0;
604 	papi_status_t status;
605 	papi_service_t svc = NULL;
606 
607 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
608 	    encryption, NULL);
609 	if (status != PAPI_OK) {
610 		if (status == PAPI_NOT_FOUND)
611 			fprintf(stderr, gettext("%s: unknown printer\n"),
612 			    name ? name : "(NULL)");
613 		else
614 			fprintf(stderr, gettext(
615 			    "Failed to contact service for %s: %s\n"),
616 			    name ? name : "(NULL)",
617 			    verbose_papi_message(svc, status));
618 		papiServiceDestroy(svc);
619 		return (-1);
620 	}
621 
622 	if (name == NULL) { /* all */
623 		char **interest = interest_list(svc);
624 
625 		if (interest != NULL) {
626 			int i;
627 
628 			for (i = 0; interest[i] != NULL; i++)
629 				result += printer_query(interest[i], report,
630 				    encryption, verbose,
631 				    description);
632 		}
633 	} else {
634 		papi_printer_t printer = NULL;
635 		char **keys = NULL;
636 
637 		/*
638 		 * Limit the query to only required data to reduce the need
639 		 * to go remote for information.
640 		 */
641 		if (report == report_device)
642 			keys = report_device_keys;
643 		else if (report == report_class)
644 			keys = report_class_keys;
645 		else if (report == report_accepting)
646 			keys = report_accepting_keys;
647 		else if ((report == report_printer) && (verbose == 0))
648 			keys = report_printer_keys;
649 
650 		status = papiPrinterQuery(svc, name, keys, NULL, &printer);
651 		if (status != PAPI_OK) {
652 			fprintf(stderr, gettext(
653 			    "Failed to get printer info for %s: %s\n"),
654 			    name, verbose_papi_message(svc, status));
655 			papiServiceDestroy(svc);
656 			return (-1);
657 		}
658 
659 		if (printer != NULL)
660 			result = report(svc, name, printer, verbose,
661 			    description);
662 
663 		papiPrinterFree(printer);
664 	}
665 
666 	papiServiceDestroy(svc);
667 
668 	return (result);
669 }
670 
671 static int
672 match_user(char *user, char **list)
673 {
674 	int i;
675 
676 	for (i = 0; list[i] != NULL; i++) {
677 		if (strcmp(user, list[i]) == 0)
678 			return (0);
679 	}
680 
681 	return (-1);
682 }
683 
684 static char **users = NULL;
685 
686 static int
687 report_job(char *printer, papi_job_t job, int show_rank, int verbose)
688 {
689 	papi_attribute_t **attrs = papiJobGetAttributeList(job);
690 	time_t clock = 0;
691 	char date[24];
692 	char request[26];
693 	char *user = "unknown";
694 	char *host = NULL;
695 	int32_t size = 0;
696 	int32_t jstate = 0;
697 	char User[50];
698 
699 	char *destination = "unknown";
700 	int32_t id = -1;
701 	static int check = 0;
702 	static char *uri = NULL;
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 (check == 0) {
714 		/*
715 		 * Read the attribute "job-printer-uri"
716 		 * just once
717 		 */
718 		(void) papiAttributeListGetString(attrs, NULL,
719 		    "job-printer-uri", &uri);
720 		check = 1;
721 	}
722 
723 	if (host) {
724 		/* Check if it is local printer or remote printer */
725 		uri_t *u = NULL;
726 
727 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
728 			char *nodename = localhostname();
729 
730 			if ((u->host == NULL) ||
731 			    (strcasecmp(u->host, "localhost") == 0) ||
732 			    (strcasecmp(u->host, nodename) == 0)) {
733 
734 				if (strcasecmp(host, nodename) == 0) {
735 					/*
736 					 * Request submitted locally
737 					 * for the local queue.
738 					 * Hostname will not be displayed
739 					 */
740 					snprintf(User, sizeof (User), "%s",
741 					    user);
742 				}
743 				else
744 					snprintf(User, sizeof (User), "%s@%s",
745 					    user, host);
746 			} else if (uri != NULL) {
747 				/*
748 				 * It's a remote printer.
749 				 * In case of remote printers hostname is
750 				 * always displayed.
751 				 */
752 				snprintf(User, sizeof (User), "%s@%s",
753 				    user, host);
754 			}
755 			uri_free(u);
756 		} else {
757 			/*
758 			 * If attribute "job-printer-uri"
759 			 * cannot be read
760 			 * by default append the hostname
761 			 */
762 			snprintf(User, sizeof (User), "%s@%s", user, host);
763 		}
764 	} else {
765 		/*
766 		 * When print server is s10u4 and ipp service is used
767 		 * "job-originating-hostname" attribute is not set
768 		 * So get the host information from the uri
769 		 */
770 		uri_t *u = NULL;
771 		if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
772 			if ((u != NULL) && (u->host != NULL))
773 				snprintf(User, sizeof (User), "%s@%s",
774 				    user, u->host);
775 			else
776 				snprintf(User, sizeof (User), "%s", user);
777 
778 			uri_free(u);
779 		} else
780 			snprintf(User, sizeof (User), "%s", user);
781 	}
782 	(void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
783 	size *= 1024;	/* for the approximate byte size */
784 	(void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
785 
786 	(void) time(&clock);
787 	(void) papiAttributeListGetInteger(attrs, NULL,
788 	    "time-at-creation", (int32_t *)&clock);
789 	(void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
790 
791 	(void) papiAttributeListGetString(attrs, NULL,
792 	    "job-printer-uri", &destination);
793 	(void) papiAttributeListGetString(attrs, NULL,
794 	    "printer-name", &destination);
795 	(void) papiAttributeListGetInteger(attrs, NULL,
796 	    "job-id", &id);
797 
798 	snprintf(request, sizeof (request), "%s-%d", printer, id);
799 
800 	if (show_rank != 0) {
801 		int32_t rank = -1;
802 
803 		(void) papiAttributeListGetInteger(attrs, NULL,
804 		    "number-of-intervening-jobs", &rank);
805 		rank++;
806 
807 		printf("%3d %-21s %-14s %7ld %s",
808 		    rank, request, User, size, date);
809 	} else
810 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
811 
812 	(void) papiAttributeListGetInteger(attrs, NULL,
813 	    "job-state", &jstate);
814 
815 	if (jstate == 0x0001)
816 		printf(gettext(" being held"));
817 	else if (jstate == 0x0800)
818 		printf(gettext(" notifying user"));
819 	else if (jstate == 0x0040)
820 		printf(gettext(" cancelled"));
821 	else if (jstate == 0x0010)
822 		printf(gettext(" finished printing"));
823 	else if (jstate == 0x0008)
824 		printf(gettext(" on %s"), destination);
825 	else if (jstate == 0x2000)
826 		printf(gettext(" held by admin"));
827 	else if (jstate == 0x0002)
828 		printf(gettext(" being filtered"));
829 	else if (jstate == 0x0004)
830 		printf(gettext(" filtered"));
831 	else if (jstate == 0x0020)
832 		printf(gettext(" held for change"));
833 
834 	if (verbose == 1) {
835 		char *form = NULL;
836 
837 		(void) papiAttributeListGetString(attrs, NULL,
838 		    "output-device-assigned", &destination);
839 		printf("\n\t assigned %s", destination);
840 
841 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
842 		if (form != NULL)
843 			printf(", form %s", form);
844 	} else if (verbose > 1) {
845 		printf("\n");
846 		papiAttributeListPrint(stdout, attrs, "\t");
847 	}
848 
849 	printf("\n");
850 
851 	return (0);
852 }
853 
854 static int
855 job_query(char *request, int (*report)(char *, papi_job_t, int, int),
856 		papi_encryption_t encryption, int show_rank, int verbose)
857 {
858 	int result = 0;
859 	papi_status_t status;
860 	papi_service_t svc = NULL;
861 	char *printer = request;
862 	int32_t id = -1;
863 	int flag1 = 0;
864 	int flag = 1;
865 	int print_flag = 0;
866 
867 	do {
868 		status = papiServiceCreate(&svc, printer, NULL, NULL,
869 		    cli_auth_callback, encryption, NULL);
870 
871 		if ((status == PAPI_OK) && (printer != NULL))
872 			print_flag = 1;
873 
874 		/* <name>-# printer name does not exist */
875 		if (status != PAPI_OK) {
876 			/*
877 			 * Check if <name>-# is a request-id
878 			 * Once this check is done flag1 is set
879 			 */
880 			if (flag1 == 1)
881 				break;
882 
883 			get_printer_id(printer, &printer, &id);
884 
885 			status = papiServiceCreate(&svc, printer, NULL, NULL,
886 			    cli_auth_callback, encryption, NULL);
887 
888 			if (status != PAPI_OK) {
889 				fprintf(stderr, gettext(
890 				    "Failed to contact service for %s: %s\n"),
891 				    (printer ? printer : "all"),
892 				    verbose_papi_message(svc, status));
893 				return (-1);
894 			}
895 		}
896 
897 		if (printer == NULL) { /* all */
898 			char **interest = interest_list(svc);
899 
900 			if (interest != NULL) {
901 				int i;
902 
903 				for (i = 0; interest[i] != NULL; i++)
904 					result += job_query(interest[i], report,
905 					    encryption, show_rank, verbose);
906 			}
907 		} else if (id == -1) { /* a printer */
908 			papi_job_t *jobs = NULL;
909 
910 			status = papiPrinterListJobs(svc, printer, NULL,
911 			    0, 0, &jobs);
912 			if (status != PAPI_OK) {
913 				fprintf(stderr, gettext(
914 				    "Failed to get job list: %s\n"),
915 				    verbose_papi_message(svc, status));
916 				papiServiceDestroy(svc);
917 				return (-1);
918 			}
919 
920 			if (jobs != NULL) {
921 				int i;
922 
923 				for (i = 0; jobs[i] != NULL; i++)
924 					result += report(printer,
925 					    jobs[i], show_rank,
926 					    verbose);
927 			}
928 
929 			papiJobListFree(jobs);
930 		} else {	/* a job */
931 			papi_job_t job = NULL;
932 
933 			/* Once a job has been found stop processing */
934 			flag = 0;
935 
936 			status = papiJobQuery(svc, printer, id, NULL, &job);
937 			if (status != PAPI_OK) {
938 				if (!print_flag)
939 					fprintf(stderr, gettext(
940 					    "Failed to get job"\
941 					    " info for %s: %s\n"),
942 					    request,
943 					    verbose_papi_message(svc, status));
944 				papiServiceDestroy(svc);
945 				return (-1);
946 			}
947 
948 			if (job != NULL)
949 				result = report(printer, job,
950 				    show_rank, verbose);
951 
952 			papiJobFree(job);
953 		}
954 
955 		if (flag) {
956 			id = -1;
957 			get_printer_id(printer, &printer, &id);
958 			if (id == -1)
959 				flag = 0;
960 			else
961 				flag1 = 1;
962 		}
963 	} while (flag);
964 
965 	papiServiceDestroy(svc);
966 
967 	return (result);
968 }
969 
970 static int
971 report_form(char *name, papi_attribute_t **attrs, int verbose)
972 {
973 	papi_status_t status;
974 	char *form = NULL;
975 	void *iter = NULL;
976 
977 	for (status = papiAttributeListGetString(attrs, &iter,
978 	    "form-supported", &form);
979 	    status == PAPI_OK;
980 	    status = papiAttributeListGetString(attrs, &iter,
981 	    NULL, &form)) {
982 		if ((name == NULL) || (strcmp(name, form) == 0)) {
983 			printf(gettext("form %s is available to you\n"), form);
984 			if (verbose != 0) {
985 				char *detail = NULL;
986 				status = papiAttributeListGetString(attrs, NULL,
987 				    "form-supported-detail", &detail);
988 				if (status == PAPI_OK)
989 					printf("%s\n", detail);
990 			}
991 		}
992 	}
993 
994 	return (0);
995 }
996 
997 static int
998 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
999 {
1000 	papi_status_t status;
1001 	char *pw = NULL;
1002 	void *iter = NULL;
1003 
1004 	for (status = papiAttributeListGetString(attrs, &iter,
1005 	    "pw-supported", &pw);
1006 	    status == PAPI_OK;
1007 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
1008 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
1009 			printf(gettext("charset %s is available\n"), pw);
1010 			if (verbose != 0) {
1011 				char *info = NULL;
1012 				status = papiAttributeListGetString(attrs, NULL,
1013 				    "pw-supported-extra", &info);
1014 				if (status == PAPI_OK)
1015 					printf("%s\n", info);
1016 			}
1017 		}
1018 	}
1019 
1020 	return (0);
1021 }
1022 
1023 static int
1024 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
1025 		papi_encryption_t encryption, int verbose)
1026 {
1027 	int result = 0;
1028 	papi_status_t status;
1029 	papi_service_t svc = NULL;
1030 	papi_attribute_t **attrs = NULL;
1031 
1032 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1033 	    encryption, NULL);
1034 	if (status != PAPI_OK) {
1035 		papiServiceDestroy(svc);
1036 		return (-1);
1037 	}
1038 
1039 	attrs = papiServiceGetAttributeList(svc);
1040 	if (attrs != NULL) {
1041 		result = report(name, attrs, verbose);
1042 
1043 		if (verbose > 1) {
1044 			printf("\n");
1045 			papiAttributeListPrint(stdout, attrs, "\t");
1046 			printf("\n");
1047 		}
1048 	}
1049 
1050 	papiServiceDestroy(svc);
1051 
1052 	return (result);
1053 }
1054 
1055 int
1056 main(int ac, char *av[])
1057 {
1058 	int exit_code = 0;
1059 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
1060 	int rank = 0;
1061 	int verbose = 0;
1062 	int description = 0;
1063 	int c;
1064 	char **argv;
1065 
1066 	(void) setlocale(LC_ALL, "");
1067 	(void) textdomain("SUNW_OST_OSCMD");
1068 
1069 	argv = (char **)calloc((ac + 1), sizeof (char *));
1070 	for (c = 0; c < ac; c++) {
1071 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
1072 		    (isalpha(av[c][2]) != 0)) {
1073 			/* preserve old "-l[po...]" behavior */
1074 			argv[c] = &av[c][1];
1075 			argv[c][0] = '-';
1076 			verbose = 1;
1077 
1078 		} else
1079 			argv[c] = av[c];
1080 	}
1081 
1082 	argv[c++] = "--";
1083 	ac = c;
1084 
1085 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
1086 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
1087 		switch (c) {
1088 		case 'l':
1089 			if ((optarg == NULL) || (optarg[0] == '-'))
1090 				optarg = "1";
1091 			verbose = atoi(optarg);
1092 			break;
1093 		case 'D':
1094 			description = 1;
1095 			break;
1096 		case 'R':
1097 			rank = 1;
1098 			break;
1099 		case 'E':
1100 			encryption = PAPI_ENCRYPT_REQUIRED;
1101 			break;
1102 		default:
1103 			break;
1104 		}
1105 	optind = 1;
1106 
1107 	/* process command line arguments */
1108 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
1109 		switch (c) {	/* these may or may not have an option */
1110 		case 'a':
1111 		case 'c':
1112 		case 'p':
1113 		case 'o':
1114 		case 'R':
1115 		case 'u':
1116 		case 'v':
1117 		case 'l':
1118 		case 'f':
1119 		case 'S':
1120 			if (optarg[0] == '-') {
1121 				/* this check stop a possible infinite loop */
1122 				if ((optind > 1) && (argv[optind-1][1] != c))
1123 					optind--;
1124 				optarg = NULL;
1125 			} else if (strcmp(optarg, "all") == 0)
1126 				optarg = NULL;
1127 		}
1128 
1129 		switch (c) {
1130 		case 'a':
1131 			exit_code += printer_query(optarg, report_accepting,
1132 			    encryption, verbose, 0);
1133 			break;
1134 		case 'c':
1135 			exit_code += printer_query(optarg, report_class,
1136 			    encryption, verbose, 0);
1137 			break;
1138 		case 'p':
1139 			exit_code += printer_query(optarg, report_printer,
1140 			    encryption, verbose, description);
1141 			break;
1142 		case 'd':
1143 			exit_code += lpstat_default_printer(encryption);
1144 			break;
1145 		case 'r':
1146 			exit_code += lpstat_service_status(encryption);
1147 			break;
1148 		case 'u':
1149 			if (optarg != NULL)
1150 				users = strsplit(optarg, ", \n");
1151 			exit_code += job_query(NULL, report_job,
1152 			    encryption, rank, verbose);
1153 			if (users != NULL) {
1154 				free(users);
1155 				users = NULL;
1156 			}
1157 			break;
1158 		case 'v':
1159 			exit_code += printer_query(optarg, report_device,
1160 			    encryption, verbose, 0);
1161 			break;
1162 		case 'R':	/* set "rank" flag in first pass */
1163 		case 'o':
1164 			exit_code += job_query(optarg, report_job,
1165 			    encryption, rank, verbose);
1166 			break;
1167 		case 'f':
1168 			exit_code += service_query(optarg, report_form,
1169 			    encryption, verbose);
1170 			break;
1171 		case 'S':
1172 			exit_code += service_query(optarg, report_print_wheels,
1173 			    encryption, verbose);
1174 			break;
1175 		case 's':
1176 			exit_code += lpstat_service_status(encryption);
1177 			exit_code += lpstat_default_printer(encryption);
1178 			exit_code += printer_query(NULL, report_class,
1179 			    encryption, verbose, 0);
1180 			exit_code += printer_query(NULL, report_device,
1181 			    encryption, verbose, 0);
1182 			exit_code += service_query(optarg, report_form,
1183 			    encryption, verbose);
1184 			exit_code += service_query(optarg, report_print_wheels,
1185 			    encryption, verbose);
1186 			break;
1187 		case 't':
1188 			exit_code += lpstat_service_status(encryption);
1189 			exit_code += lpstat_default_printer(encryption);
1190 			exit_code += printer_query(NULL, report_class,
1191 			    encryption, verbose, 0);
1192 			exit_code += printer_query(NULL, report_device,
1193 			    encryption, verbose, 0);
1194 			exit_code += printer_query(NULL, report_accepting,
1195 			    encryption, verbose, 0);
1196 			exit_code += printer_query(NULL, report_printer,
1197 			    encryption, verbose, 0);
1198 			exit_code += service_query(optarg, report_form,
1199 			    encryption, verbose);
1200 			exit_code += service_query(optarg, report_print_wheels,
1201 			    encryption, verbose);
1202 			exit_code += job_query(NULL, report_job,
1203 			    encryption, rank, verbose);
1204 			break;
1205 		case 'L':	/* local-only, ignored */
1206 		case 'l':	/* increased verbose level in first pass */
1207 		case 'D':	/* set "description" flag in first pass */
1208 		case 'E':	/* set encryption in the first pass */
1209 			break;
1210 		default:
1211 			usage(av[0]);
1212 		}
1213 	}
1214 	ac--;
1215 
1216 	if (ac == 1) {	/* report on my jobs */
1217 		struct passwd *pw = getpwuid(getuid());
1218 
1219 		if (pw != NULL)
1220 			users = strsplit(pw->pw_name, "");
1221 		exit_code += job_query(NULL, report_job, encryption,
1222 		    rank, verbose);
1223 		if (users != NULL) {
1224 			free(users);
1225 			users = NULL;
1226 		}
1227 	} else {
1228 		for (c = optind; c < ac; c++)
1229 			exit_code += job_query(argv[c], report_job, encryption,
1230 			    rank, verbose);
1231 	}
1232 
1233 
1234 	if (exit_code != 0)
1235 		exit_code = 1;
1236 
1237 	return (exit_code);
1238 }
1239