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 	(void) papiAttributeListGetInteger(attrs, NULL,
798 	    "job-id-requested", &id);
799 
800 	snprintf(request, sizeof (request), "%s-%d", printer, id);
801 
802 	if (show_rank != 0) {
803 		int32_t rank = -1;
804 
805 		(void) papiAttributeListGetInteger(attrs, NULL,
806 		    "number-of-intervening-jobs", &rank);
807 		rank++;
808 
809 		printf("%3d %-21s %-14s %7ld %s",
810 		    rank, request, User, size, date);
811 	} else
812 		printf("%-23s %-14s %7ld   %s", request, User, size, date);
813 
814 	(void) papiAttributeListGetInteger(attrs, NULL,
815 	    "job-state", &jstate);
816 	if (jstate == 0x0001)
817 		printf(gettext(" being held"));
818 	else if (jstate == 0x0800)
819 		printf(gettext(" notifying user"));
820 	else if (jstate == 0x0040)
821 		printf(gettext(" cancelled"));
822 	else if (jstate == 0x0010)
823 		printf(gettext(" finished printing"));
824 	else if (jstate == 0x0008)
825 		printf(gettext(" on %s"), destination);
826 	else if (jstate == 0x2000)
827 		printf(gettext(" held by admin"));
828 	else if (jstate == 0x0002)
829 		printf(gettext(" being filtered"));
830 	else if (jstate == 0x0004)
831 		printf(gettext(" filtered"));
832 	else if (jstate == 0x0020)
833 		printf(gettext(" held for change"));
834 
835 	if (verbose == 1) {
836 		char *form = NULL;
837 
838 		(void) papiAttributeListGetString(attrs, NULL,
839 		    "output-device-assigned", &destination);
840 		printf("\n\t assigned %s", destination);
841 
842 		(void) papiAttributeListGetString(attrs, NULL, "form", &form);
843 		if (form != NULL)
844 			printf(", form %s", form);
845 	} else if (verbose > 1) {
846 		printf("\n");
847 		papiAttributeListPrint(stdout, attrs, "\t");
848 	}
849 
850 	printf("\n");
851 
852 	return (0);
853 }
854 
855 static int
856 job_query(char *request, int (*report)(char *, papi_job_t, int, int),
857 		papi_encryption_t encryption, int show_rank, int verbose)
858 {
859 	int result = 0;
860 	papi_status_t status;
861 	papi_service_t svc = NULL;
862 	char *printer = request;
863 	int32_t id = -1;
864 	int flag1 = 0;
865 	int flag = 1;
866 	int print_flag = 0;
867 
868 	do {
869 		status = papiServiceCreate(&svc, printer, NULL, NULL,
870 		    cli_auth_callback, encryption, NULL);
871 
872 		if ((status == PAPI_OK) && (printer != NULL))
873 			print_flag = 1;
874 
875 		/* <name>-# printer name does not exist */
876 		if (status != PAPI_OK) {
877 			/*
878 			 * Check if <name>-# is a request-id
879 			 * Once this check is done flag1 is set
880 			 */
881 			if (flag1 == 1)
882 				break;
883 
884 			get_printer_id(printer, &printer, &id);
885 
886 			status = papiServiceCreate(&svc, printer, NULL, NULL,
887 			    cli_auth_callback, encryption, NULL);
888 
889 			if (status != PAPI_OK) {
890 				fprintf(stderr, gettext(
891 				    "Failed to contact service for %s: %s\n"),
892 				    (printer ? printer : "all"),
893 				    verbose_papi_message(svc, status));
894 				return (-1);
895 			}
896 		}
897 
898 		if (printer == NULL) { /* all */
899 			char **interest = interest_list(svc);
900 
901 			if (interest != NULL) {
902 				int i;
903 
904 				for (i = 0; interest[i] != NULL; i++)
905 					result += job_query(interest[i], report,
906 					    encryption, show_rank, verbose);
907 			}
908 		} else if (id == -1) { /* a printer */
909 			papi_job_t *jobs = NULL;
910 
911 			status = papiPrinterListJobs(svc, printer, NULL,
912 			    0, 0, &jobs);
913 			if (status != PAPI_OK) {
914 				fprintf(stderr, gettext(
915 				    "Failed to get job list: %s\n"),
916 				    verbose_papi_message(svc, status));
917 				papiServiceDestroy(svc);
918 				return (-1);
919 			}
920 
921 			if (jobs != NULL) {
922 				int i;
923 
924 				for (i = 0; jobs[i] != NULL; i++)
925 					result += report(printer,
926 					    jobs[i], show_rank,
927 					    verbose);
928 			}
929 
930 			papiJobListFree(jobs);
931 		} else {	/* a job */
932 			papi_job_t job = NULL;
933 
934 			/* Once a job has been found stop processing */
935 			flag = 0;
936 
937 			status = papiJobQuery(svc, printer, id, NULL, &job);
938 			if (status != PAPI_OK) {
939 				if (!print_flag)
940 					fprintf(stderr, gettext(
941 					    "Failed to get job"\
942 					    " info for %s: %s\n"),
943 					    request,
944 					    verbose_papi_message(svc, status));
945 				papiServiceDestroy(svc);
946 				return (-1);
947 			}
948 
949 			if (job != NULL)
950 				result = report(printer, job,
951 				    show_rank, verbose);
952 
953 			papiJobFree(job);
954 		}
955 
956 		if (flag) {
957 			id = -1;
958 			get_printer_id(printer, &printer, &id);
959 			if (id == -1)
960 				flag = 0;
961 			else
962 				flag1 = 1;
963 		}
964 	} while (flag);
965 
966 	papiServiceDestroy(svc);
967 
968 	return (result);
969 }
970 
971 static int
972 report_form(char *name, papi_attribute_t **attrs, int verbose)
973 {
974 	papi_status_t status;
975 	char *form = NULL;
976 	void *iter = NULL;
977 
978 	for (status = papiAttributeListGetString(attrs, &iter,
979 	    "form-supported", &form);
980 	    status == PAPI_OK;
981 	    status = papiAttributeListGetString(attrs, &iter,
982 	    NULL, &form)) {
983 		if ((name == NULL) || (strcmp(name, form) == 0)) {
984 			printf(gettext("form %s is available to you\n"), form);
985 			if (verbose != 0) {
986 				char *detail = NULL;
987 				status = papiAttributeListGetString(attrs, NULL,
988 				    "form-supported-detail", &detail);
989 				if (status == PAPI_OK)
990 					printf("%s\n", detail);
991 			}
992 		}
993 	}
994 
995 	return (0);
996 }
997 
998 static int
999 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
1000 {
1001 	papi_status_t status;
1002 	char *pw = NULL;
1003 	void *iter = NULL;
1004 
1005 	for (status = papiAttributeListGetString(attrs, &iter,
1006 	    "pw-supported", &pw);
1007 	    status == PAPI_OK;
1008 	    status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
1009 		if ((name == NULL) || (strcmp(name, pw) == 0)) {
1010 			printf(gettext("charset %s is available\n"), pw);
1011 			if (verbose != 0) {
1012 				char *info = NULL;
1013 				status = papiAttributeListGetString(attrs, NULL,
1014 				    "pw-supported-extra", &info);
1015 				if (status == PAPI_OK)
1016 					printf("%s\n", info);
1017 			}
1018 		}
1019 	}
1020 
1021 	return (0);
1022 }
1023 
1024 static int
1025 service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
1026 		papi_encryption_t encryption, int verbose)
1027 {
1028 	int result = 0;
1029 	papi_status_t status;
1030 	papi_service_t svc = NULL;
1031 	papi_attribute_t **attrs = NULL;
1032 
1033 	status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1034 	    encryption, NULL);
1035 	if (status != PAPI_OK) {
1036 		papiServiceDestroy(svc);
1037 		return (-1);
1038 	}
1039 
1040 	attrs = papiServiceGetAttributeList(svc);
1041 	if (attrs != NULL) {
1042 		result = report(name, attrs, verbose);
1043 
1044 		if (verbose > 1) {
1045 			printf("\n");
1046 			papiAttributeListPrint(stdout, attrs, "\t");
1047 			printf("\n");
1048 		}
1049 	}
1050 
1051 	papiServiceDestroy(svc);
1052 
1053 	return (result);
1054 }
1055 
1056 int
1057 main(int ac, char *av[])
1058 {
1059 	int exit_code = 0;
1060 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
1061 	int rank = 0;
1062 	int verbose = 0;
1063 	int description = 0;
1064 	int c;
1065 	char **argv;
1066 
1067 	(void) setlocale(LC_ALL, "");
1068 	(void) textdomain("SUNW_OST_OSCMD");
1069 
1070 	argv = (char **)calloc((ac + 1), sizeof (char *));
1071 	for (c = 0; c < ac; c++) {
1072 		if ((av[c][0] == '-') && (av[c][1] == 'l') &&
1073 		    (isalpha(av[c][2]) != 0)) {
1074 			/* preserve old "-l[po...]" behavior */
1075 			argv[c] = &av[c][1];
1076 			argv[c][0] = '-';
1077 			verbose = 1;
1078 
1079 		} else
1080 			argv[c] = av[c];
1081 	}
1082 
1083 	argv[c++] = "--";
1084 	ac = c;
1085 
1086 	/* preprocess argument list looking for '-l' or '-R' so it can trail */
1087 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF)
1088 		switch (c) {
1089 		case 'l':
1090 			if ((optarg == NULL) || (optarg[0] == '-'))
1091 				optarg = "1";
1092 			verbose = atoi(optarg);
1093 			break;
1094 		case 'D':
1095 			description = 1;
1096 			break;
1097 		case 'R':
1098 			rank = 1;
1099 			break;
1100 		case 'E':
1101 			encryption = PAPI_ENCRYPT_REQUIRED;
1102 			break;
1103 		default:
1104 			break;
1105 		}
1106 	optind = 1;
1107 
1108 	/* process command line arguments */
1109 	while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
1110 		switch (c) {	/* these may or may not have an option */
1111 		case 'a':
1112 		case 'c':
1113 		case 'p':
1114 		case 'o':
1115 		case 'R':
1116 		case 'u':
1117 		case 'v':
1118 		case 'l':
1119 		case 'f':
1120 		case 'S':
1121 			if (optarg[0] == '-') {
1122 				/* this check stop a possible infinite loop */
1123 				if ((optind > 1) && (argv[optind-1][1] != c))
1124 					optind--;
1125 				optarg = NULL;
1126 			} else if (strcmp(optarg, "all") == 0)
1127 				optarg = NULL;
1128 		}
1129 
1130 		switch (c) {
1131 		case 'a':
1132 			exit_code += printer_query(optarg, report_accepting,
1133 			    encryption, verbose, 0);
1134 			break;
1135 		case 'c':
1136 			exit_code += printer_query(optarg, report_class,
1137 			    encryption, verbose, 0);
1138 			break;
1139 		case 'p':
1140 			exit_code += printer_query(optarg, report_printer,
1141 			    encryption, verbose, description);
1142 			break;
1143 		case 'd':
1144 			exit_code += lpstat_default_printer(encryption);
1145 			break;
1146 		case 'r':
1147 			exit_code += lpstat_service_status(encryption);
1148 			break;
1149 		case 'u':
1150 			if (optarg != NULL)
1151 				users = strsplit(optarg, ", \n");
1152 			exit_code += job_query(NULL, report_job,
1153 			    encryption, rank, verbose);
1154 			if (users != NULL) {
1155 				free(users);
1156 				users = NULL;
1157 			}
1158 			break;
1159 		case 'v':
1160 			exit_code += printer_query(optarg, report_device,
1161 			    encryption, verbose, 0);
1162 			break;
1163 		case 'R':	/* set "rank" flag in first pass */
1164 		case 'o':
1165 			exit_code += job_query(optarg, report_job,
1166 			    encryption, rank, verbose);
1167 			break;
1168 		case 'f':
1169 			exit_code += service_query(optarg, report_form,
1170 			    encryption, verbose);
1171 			break;
1172 		case 'S':
1173 			exit_code += service_query(optarg, report_print_wheels,
1174 			    encryption, verbose);
1175 			break;
1176 		case 's':
1177 			exit_code += lpstat_service_status(encryption);
1178 			exit_code += lpstat_default_printer(encryption);
1179 			exit_code += printer_query(NULL, report_class,
1180 			    encryption, verbose, 0);
1181 			exit_code += printer_query(NULL, report_device,
1182 			    encryption, verbose, 0);
1183 			exit_code += service_query(optarg, report_form,
1184 			    encryption, verbose);
1185 			exit_code += service_query(optarg, report_print_wheels,
1186 			    encryption, verbose);
1187 			break;
1188 		case 't':
1189 			exit_code += lpstat_service_status(encryption);
1190 			exit_code += lpstat_default_printer(encryption);
1191 			exit_code += printer_query(NULL, report_class,
1192 			    encryption, verbose, 0);
1193 			exit_code += printer_query(NULL, report_device,
1194 			    encryption, verbose, 0);
1195 			exit_code += printer_query(NULL, report_accepting,
1196 			    encryption, verbose, 0);
1197 			exit_code += printer_query(NULL, report_printer,
1198 			    encryption, verbose, 0);
1199 			exit_code += service_query(optarg, report_form,
1200 			    encryption, verbose);
1201 			exit_code += service_query(optarg, report_print_wheels,
1202 			    encryption, verbose);
1203 			exit_code += job_query(NULL, report_job,
1204 			    encryption, rank, verbose);
1205 			break;
1206 		case 'L':	/* local-only, ignored */
1207 		case 'l':	/* increased verbose level in first pass */
1208 		case 'D':	/* set "description" flag in first pass */
1209 		case 'E':	/* set encryption in the first pass */
1210 			break;
1211 		default:
1212 			usage(av[0]);
1213 		}
1214 	}
1215 	ac--;
1216 
1217 	if (ac == 1) {	/* report on my jobs */
1218 		struct passwd *pw = getpwuid(getuid());
1219 
1220 		if (pw != NULL)
1221 			users = strsplit(pw->pw_name, "");
1222 		exit_code += job_query(NULL, report_job, encryption,
1223 		    rank, verbose);
1224 		if (users != NULL) {
1225 			free(users);
1226 			users = NULL;
1227 		}
1228 	} else {
1229 		for (c = optind; c < ac; c++)
1230 			exit_code += job_query(argv[c], report_job, encryption,
1231 			    rank, verbose);
1232 	}
1233 
1234 
1235 	if (exit_code != 0)
1236 		exit_code = 1;
1237 
1238 	return (exit_code);
1239 }
1240