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.
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.
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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: lpstat.c 173 2006-05-25 04:52:06Z njacobs $ */
49 if ((name
= strrchr(program
, '/')) == NULL
)
54 fprintf(stdout
, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
55 "[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
56 "[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
57 "[-f [list] [-l]] [-u list]\n"),
66 struct tm
*tm
= localtime(t
);
68 (void) strftime(buf
, sizeof (buf
), "%c", tm
);
74 printer_name(papi_printer_t printer
)
76 papi_attribute_t
**attributes
= papiPrinterGetAttributeList(printer
);
79 if (attributes
!= NULL
)
80 papiAttributeListGetString(attributes
, NULL
,
81 "printer-name", &result
);
87 lpstat_default_printer(papi_encryption_t encryption
)
90 papi_service_t svc
= NULL
;
91 papi_printer_t p
= NULL
;
94 status
= papiServiceCreate(&svc
, NULL
, NULL
, NULL
, cli_auth_callback
,
96 if (status
== PAPI_OK
) {
97 char *req
[] = { "printer-name", NULL
};
99 status
= papiPrinterQuery(svc
, DEFAULT_DEST
, req
, NULL
, &p
);
101 name
= printer_name(p
);
104 printf(gettext("system default printer: %s\n"), name
);
106 printf(gettext("no system default destination\n"));
108 papiServiceDestroy(svc
);
114 lpstat_service_status(papi_encryption_t encryption
)
116 papi_status_t status
;
117 papi_service_t svc
= NULL
;
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
;
125 status
= papiServiceCreate(&svc
, name
, NULL
, NULL
, cli_auth_callback
,
127 if (status
!= PAPI_OK
) {
128 printf(gettext("scheduler is not running\n"));
130 printf(gettext("scheduler is running\n"));
131 papiServiceDestroy(svc
);
137 get_device_uri(papi_service_t svc
, char *name
)
139 papi_status_t status
;
140 papi_printer_t p
= NULL
;
141 char *keys
[] = { "device-uri", NULL
};
144 status
= papiPrinterQuery(svc
, name
, keys
, NULL
, &p
);
145 if ((status
== PAPI_OK
) && (p
!= NULL
)) {
146 papi_attribute_t
**attrs
= papiPrinterGetAttributeList(p
);
148 (void) papiAttributeListGetString(attrs
, NULL
,
149 "device-uri", &result
);
151 result
= strdup(result
);
160 print_description(papi_attribute_t
**list
, char *printer_name
)
164 (void) papiAttributeListGetString(list
, NULL
,
165 "printer-info", &str
);
168 * If no printer-info is read then
169 * by default the printer-info is <printer-name>@<server>
171 if (str
[0] == '\0') {
175 (void) papiAttributeListGetString(list
, NULL
,
176 "printer-uri-supported", &uri
);
178 if ((uri
!= NULL
) && (uri_from_string(uri
, &u
) == 0)) {
179 char *nodename
= localhostname();
181 if ((u
->host
== NULL
) ||
182 (strcasecmp(u
->host
, "localhost") == 0) ||
183 (strcasecmp(u
->host
, nodename
) == 0))
184 printf(gettext("\tDescription:\n"));
186 printf(gettext("\tDescription: %s@%s\n"),
187 printer_name
, u
->host
);
191 printf(gettext("\tDescription:\n"));
193 printf(gettext("\tDescription: %s\n"), str
);
196 static char *report_device_keys
[] = { "printer-name", "printer-uri-supported",
200 report_device(papi_service_t svc
, char *name
, papi_printer_t printer
,
201 int verbose
, int description
)
203 papi_status_t status
;
204 papi_attribute_t
**attrs
= papiPrinterGetAttributeList(printer
);
210 status
= papiAttributeListGetString(attrs
, NULL
,
211 "printer-name", &name
);
212 if (status
!= PAPI_OK
)
213 status
= papiAttributeListGetString(attrs
, NULL
,
214 "printer-uri-supported", &name
);
220 (void) papiAttributeListGetString(attrs
, NULL
,
221 "printer-uri-supported", &uri
);
223 if ((uri
!= NULL
) && (uri_from_string(uri
, &u
) == 0)) {
224 char *nodename
= localhostname();
226 if ((u
->host
== NULL
) ||
227 (strcasecmp(u
->host
, "localhost") == 0) ||
228 (strcasecmp(u
->host
, nodename
) == 0))
229 device
= get_device_uri(svc
, name
);
231 if (device
!= NULL
) {
232 printf(gettext("device for %s: %s\n"), name
, device
);
234 } else if (uri
!= NULL
) {
235 printf(gettext("system for %s: %s (as %s)\n"), name
,
236 u
->host
?u
->host
:"localhost", uri
);
246 static char *report_accepting_keys
[] = { "printer-name",
247 "printer-uri-supported", "printer-is-accepting-jobs",
248 "printer-up-time", "printer-state-time",
249 "lpsched-reject-date", "lpsched-reject-reason", NULL
};
252 report_accepting(papi_service_t svc
, char *name
, papi_printer_t printer
,
253 int verbose
, int description
)
255 papi_status_t status
;
256 papi_attribute_t
**attrs
= papiPrinterGetAttributeList(printer
);
258 char boolean
= PAPI_FALSE
;
261 status
= papiAttributeListGetString(attrs
, NULL
,
262 "printer-name", &name
);
263 if (status
!= PAPI_OK
)
264 status
= papiAttributeListGetString(attrs
, NULL
,
265 "printer-uri-supported", &name
);
270 (void) papiAttributeListGetBoolean(attrs
, NULL
,
271 "printer-is-accepting-jobs", &boolean
);
273 (void) papiAttributeListGetDatetime(attrs
, NULL
,
274 "printer-up-time", &curr
);
275 (void) papiAttributeListGetDatetime(attrs
, NULL
,
276 "printer-state-time", &curr
);
277 (void) papiAttributeListGetDatetime(attrs
, NULL
,
278 "lpsched-reject-date", &curr
);
280 if (boolean
== PAPI_TRUE
) {
281 printf(gettext("%s accepting requests since %s\n"),
282 name
, nctime(&curr
));
284 char *reason
= "unknown reason";
286 (void) papiAttributeListGetString(attrs
, NULL
,
287 "lpsched-reject-reason", &reason
);
289 printf(gettext("%s not accepting requests since %s\n\t%s\n"),
290 name
, nctime(&curr
), reason
);
296 static char *report_class_keys
[] = { "printer-name", "printer-uri-supported",
297 "member-names", NULL
};
300 report_class(papi_service_t svc
, char *name
, papi_printer_t printer
,
301 int verbose
, int description
)
303 papi_status_t status
;
304 papi_attribute_t
**attrs
= papiPrinterGetAttributeList(printer
);
308 status
= papiAttributeListGetString(attrs
, &iter
,
309 "member-names", &member
);
310 if (status
== PAPI_NOT_FOUND
) /* it's not a class */
314 status
= papiAttributeListGetString(attrs
, NULL
,
315 "printer-name", &name
);
316 if (status
!= PAPI_OK
)
317 status
= papiAttributeListGetString(attrs
, NULL
,
318 "printer-uri-supported", &name
);
323 printf(gettext("members of class %s:\n\t%s\n"), name
, member
);
324 while (papiAttributeListGetString(attrs
, &iter
, NULL
, &member
)
326 printf("\t%s\n", member
);
332 get_remote_hostname(papi_attribute_t
**attrs
, char **host
)
339 (void) papiAttributeListGetString(attrs
, NULL
,
340 "job-originating-host-name", host
);
341 (void) papiAttributeListGetString(attrs
, NULL
,
342 "printer-uri-supported", &uri
);
345 if (uri_from_string(uri
, &u
) == 0) {
346 if (u
->host
== NULL
) {
350 *host
= strdup(u
->host
);
359 nodename
= localhostname();
360 if ((strcasecmp(*host
, "localhost") == 0) ||
361 (strcasecmp(*host
, nodename
) == 0)) {
367 static char *report_printer_keys
[] = { "printer-name",
368 "printer-uri-supported", "printer-state",
369 "printer-up-time", "printer-state-time",
370 "lpsched-disable-date", "printer-state-reasons",
371 "lpsched-disable-reason", NULL
};
374 report_printer(papi_service_t svc
, char *name
, papi_printer_t printer
,
375 int verbose
, int description
)
377 papi_status_t status
;
378 papi_attribute_t
**attrs
= papiPrinterGetAttributeList(printer
);
382 papi_job_t
*j
= NULL
;
384 status
= papiAttributeListGetString(attrs
, NULL
,
385 "member-names", &member
);
386 if (status
== PAPI_OK
) /* it's a class */
390 status
= papiAttributeListGetString(attrs
, NULL
,
391 "printer-name", &name
);
392 if (status
!= PAPI_OK
)
393 status
= papiAttributeListGetString(attrs
, NULL
,
394 "printer-uri-supported", &name
);
399 printf(gettext("printer %s "), name
);
401 status
= papiAttributeListGetInteger(attrs
, NULL
,
402 "printer-state", &pstat
);
405 case 0x03: /* idle */
406 printf(gettext("is idle. enabled"));
408 case 0x04: /* processing */
409 case 0x06: /* faulted printing */
410 status
= papiPrinterListJobs(svc
, name
, NULL
,
413 if (status
== PAPI_OK
) {
419 for (i
= 0; j
[i
] != NULL
; ++i
) {
420 papi_attribute_t
**attr
=
421 papiJobGetAttributeList(j
[i
]);
423 papiAttributeListGetInteger(attr
,
424 NULL
, "job-state", &jstate
);
425 papiAttributeListGetInteger(attr
,
426 NULL
, "job-id", &jobid
);
428 * For lpd protocol "job-id-requested"
431 papiAttributeListGetInteger(attr
,
432 NULL
, "job-id-requested", &jobid
);
435 * When lpd protocol is used job-state
436 * cannot be retrieved, therefore
437 * job-state will be 0.
438 * When ipp protocol is used, the
439 * active/printing job-state will be
440 * RS_PRINTING (0x0008) post s10u5.
441 * For pre-s10u5 job-state will be
442 * RS_ACTIVE (0x05). So print only when
443 * the job-state is RS_PRINTING (0x0008)
444 * or RS_ACTIVE (0x05) or 0
446 if ((jstate
== 0x0008) ||
466 case 0x05: /* stopped */
467 printf(gettext("disabled"));
469 case 0x07: /* faulted printer */
470 printf(gettext("faulted. enabled"));
472 case 0x08: /* waiting for auto retry */
473 printf(gettext("waiting for auto-retry."));
476 printf(gettext("unknown state(0x%x)."), pstat
);
481 printf(gettext(" available.\n"));
484 (void) papiAttributeListGetDatetime(attrs
, NULL
,
485 "printer-up-time", &curr
);
486 (void) papiAttributeListGetDatetime(attrs
, NULL
,
487 "printer-state-time", &curr
);
488 (void) papiAttributeListGetDatetime(attrs
, NULL
,
489 "lpsched-disable-date", &curr
);
490 printf(gettext(" since %s. available.\n"), nctime(&curr
));
493 if ((pstat
== 0x05) ||
497 char *reason
= "unknown reason";
499 (void) papiAttributeListGetString(attrs
, NULL
,
500 "printer-state-reasons", &reason
);
501 (void) papiAttributeListGetString(attrs
, NULL
,
502 "lpsched-disable-reason", &reason
);
503 printf(gettext("\t%s\n"), reason
);
511 if ((get_remote_hostname(attrs
, &host
)) != 0) {
513 gettext("\tRemote Name: %s\n\tRemote Server: "
514 "%s\n"), name
, host
);
519 (void) papiAttributeListGetString(attrs
, NULL
,
521 printf(gettext("\tForm mounted: %s\n"), str
);
525 (void) papiAttributeListGetString(attrs
, &iter
,
526 "document-format-supported", &str
);
527 printf(gettext("\tContent types: %s"), str
);
528 while (papiAttributeListGetString(attrs
, &iter
, NULL
, &str
)
533 /* Display the printer description */
534 print_description(attrs
, name
);
538 (void) papiAttributeListGetString(attrs
, &iter
,
539 "lpsched-printer-type", &str
);
540 printf(gettext("\tPrinter types: %s"), str
);
541 while (papiAttributeListGetString(attrs
, &iter
, NULL
, &str
)
547 (void) papiAttributeListGetString(attrs
, NULL
,
548 "lpsched-dial-info", &str
);
549 printf(gettext("\tConnection: %s\n"),
550 ((str
[0] == '\0') ? gettext("direct") : str
));
553 (void) papiAttributeListGetString(attrs
, NULL
,
554 "lpsched-interface-script", &str
);
555 printf(gettext("\tInterface: %s\n"), str
);
558 (void) papiAttributeListGetString(attrs
, NULL
,
559 "ppd-file-uri", &str
);
560 (void) papiAttributeListGetString(attrs
, NULL
,
561 "lpsched-ppd-source-path", &str
);
563 printf(gettext("\tPPD: %s\n"), str
);
566 (void) papiAttributeListGetString(attrs
, NULL
,
567 "lpsched-fault-alert-command", &str
);
569 printf(gettext("\tOn fault: %s\n"), str
);
572 (void) papiAttributeListGetString(attrs
, NULL
,
573 "lpsched-fault-recovery", &str
);
574 printf(gettext("\tAfter fault: %s\n"),
575 ((str
[0] == '\0') ? gettext("continue") : str
));
579 (void) papiAttributeListGetString(attrs
, &iter
,
580 "requesting-user-name-allowed", &str
);
581 printf(gettext("\tUsers allowed:\n\t\t%s\n"),
582 ((str
[0] == '\0') ? gettext("(none)") : str
));
583 if ((str
!= NULL
) && (str
[0] != '\0'))
584 while (papiAttributeListGetString(attrs
, &iter
, NULL
,
586 printf("\t\t%s\n", str
);
590 (void) papiAttributeListGetString(attrs
, &iter
,
591 "requesting-user-name-denied", &str
);
593 printf(gettext("\tUsers denied:\n\t\t%s\n"),
594 ((str
[0] == '\0') ? gettext("(none)") : str
));
595 if ((str
!= NULL
) && (str
[0] != '\0'))
596 while (papiAttributeListGetString(attrs
, &iter
,
597 NULL
, &str
) == PAPI_OK
)
598 printf("\t\t%s\n", str
);
603 (void) papiAttributeListGetString(attrs
, &iter
,
604 "form-supported", &str
);
605 printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
606 ((str
[0] == '\0') ? gettext("none") : str
));
607 if ((str
!= NULL
) && (str
[0] != '\0'))
608 while (papiAttributeListGetString(attrs
, &iter
, NULL
,
610 printf("\t\t(%s)\n", str
);
614 (void) papiAttributeListGetString(attrs
, &iter
,
615 "media-supported", &str
);
616 printf(gettext("\tMedia supported:\n\t\t%s\n"),
617 ((str
[0] == '\0') ? gettext("(none)") : str
));
618 if ((str
!= NULL
) && (str
[0] != '\0'))
619 while (papiAttributeListGetString(attrs
, &iter
, NULL
,
621 printf("\t\t%s\n", str
);
624 (void) papiAttributeListGetString(attrs
, NULL
,
625 "job-sheets-supported", &str
);
626 if ((strcasecmp(str
, "none")) == 0)
627 str
= gettext("page never printed");
628 else if (strcasecmp(str
, "optional") == 0)
629 str
= gettext("not required");
631 str
= gettext("required");
633 printf(gettext("\tBanner %s\n"), str
);
638 (void) papiAttributeListGetString(attrs
, &iter
,
639 "lpsched-print-wheels", &str
);
640 printf(gettext("\tCharacter sets:\n\t\t%s\n"),
641 ((str
[0] == '\0') ? gettext("(none)") : str
));
642 if ((str
!= NULL
) && (str
[0] != '\0'))
643 while (papiAttributeListGetString(attrs
, &iter
, NULL
,
645 printf("\t\t%s\n", str
);
647 printf(gettext("\tDefault pitch:\n"));
648 printf(gettext("\tDefault page size:\n"));
649 printf(gettext("\tDefault port setting:\n"));
653 (void) papiAttributeListGetString(attrs
, &iter
,
654 "lpsched-options", &str
);
656 printf(gettext("\tOptions: %s"), str
);
657 while (papiAttributeListGetString(attrs
, &iter
, NULL
,
663 } else if (description
== 1)
664 /* Display printer description */
665 print_description(attrs
, name
);
666 else if (verbose
> 1)
667 papiAttributeListPrint(stdout
, attrs
, "\t");
676 printer_query(char *name
, int (*report
)(papi_service_t
, char *, papi_printer_t
,
677 int, int), papi_encryption_t encryption
,
678 int verbose
, int description
)
680 int result
= 0, i
= 0;
681 papi_status_t status
;
682 papi_service_t svc
= NULL
;
683 char **list
= getlist(name
, LP_WS
, LP_SEP
);
686 list
= (char **)malloc(sizeof (char *));
691 * The for loop executes once for every printer
692 * entry in list. If list is NULL that implies
693 * name is also NULL, the loop runs only one time.
696 for (i
= 0; name
== NULL
|| list
[i
] != NULL
; i
++) {
699 status
= papiServiceCreate(&svc
, name
, NULL
, NULL
,
700 cli_auth_callback
, encryption
, NULL
);
701 if (status
!= PAPI_OK
) {
702 if (status
== PAPI_NOT_FOUND
)
704 gettext("%s: unknown printer\n"),
705 name
? name
: "(NULL)");
707 fprintf(stderr
, gettext(
708 "Failed to contact service for %s: %s\n"),
709 name
? name
: "(NULL)",
710 verbose_papi_message(svc
, status
));
711 papiServiceDestroy(svc
);
716 if (name
== NULL
) { /* all */
717 char **interest
= interest_list(svc
);
719 if (interest
!= NULL
) {
722 for (i
= 0; interest
[i
] != NULL
; i
++)
723 result
+= printer_query(interest
[i
],
724 report
, encryption
, verbose
,
728 papi_printer_t printer
= NULL
;
732 * Limit the query to only required data
733 * to reduce the need to go remote for
736 if (report
== report_device
)
737 keys
= report_device_keys
;
738 else if (report
== report_class
)
739 keys
= report_class_keys
;
740 else if (report
== report_accepting
)
741 keys
= report_accepting_keys
;
742 else if ((report
== report_printer
) && (verbose
== 0))
743 keys
= report_printer_keys
;
745 status
= papiPrinterQuery(svc
, name
, keys
,
747 if (status
!= PAPI_OK
) {
748 fprintf(stderr
, gettext(
749 "Failed to get printer info for %s: %s\n"),
750 name
, verbose_papi_message(svc
, status
));
751 papiServiceDestroy(svc
);
757 result
+= report(svc
, name
, printer
, verbose
,
760 papiPrinterFree(printer
);
763 papiServiceDestroy(svc
);
775 match_user(char *user
, char **list
)
779 for (i
= 0; list
[i
] != NULL
; i
++) {
780 if (strcmp(user
, list
[i
]) == 0)
787 static char **users
= NULL
;
790 report_job(char *printer
, papi_job_t job
, int show_rank
, int verbose
)
792 papi_attribute_t
**attrs
= papiJobGetAttributeList(job
);
796 char *user
= "unknown";
802 char *destination
= "unknown";
804 static int check
= 0;
805 static char *uri
= NULL
;
806 static char *puri
= NULL
; /* printer-uri */
807 static char *pname
= NULL
; /* printer-name */
809 (void) papiAttributeListGetString(attrs
, NULL
,
810 "job-originating-user-name", &user
);
812 if ((users
!= NULL
) && (match_user(user
, users
) < 0))
815 (void) papiAttributeListGetString(attrs
, NULL
,
816 "job-originating-host-name", &host
);
819 * When lpstat is called for multiple printers
820 * internally the function 'report_job' gets
821 * called multiple times with different printer-names.
822 * The following block of code handles the case when lpstat is
823 * executed for multiple printers. In other words when 'report_job'
824 * is called multiple times for different printers for
826 * For e.g: lpstat printer1 printer2 printer3
830 * When lpstat is queried for the first time
831 * pname is NULL so this part of the code gets executed.
832 * Read the attribute "job-printer-uri"
835 (void) papiAttributeListGetString(attrs
, NULL
,
836 "job-printer-uri", &uri
);
838 if (printer
!= NULL
) {
840 * Set pname to the printer that is being
841 * queried so that this can be used later
842 * if 'report_job' is called multiple times for
843 * different printers for one lpstat command
850 * Set puri so that "job-printer-uri" corresponding
851 * to a particular printer can be used later when
852 * lpstat is queried for the same printer as
853 * "job-printer-uri" for a printer is read just once.
859 * This part of the code will get executed when
860 * 'report_job' is called more than once for the same
863 if (printer
!= NULL
) {
864 if (strcasecmp(pname
, printer
) != 0) {
866 * Read the job-printer-uri as
867 * it will be different for
871 (void) papiAttributeListGetString(attrs
,
872 NULL
, "job-printer-uri", &uri
);
880 * Same printer queried twice
881 * uri should be the same as
882 * already read in the previous call
884 * For the same printer 'job-printer-uri'
885 * is read just once because only in the
886 * first call it contains the host information
894 /* Check if it is local printer or remote printer */
897 if ((uri
!= NULL
) && (uri_from_string(uri
, &u
) == 0)) {
898 char *nodename
= localhostname();
900 if ((u
->host
== NULL
) ||
901 (strcasecmp(u
->host
, "localhost") == 0) ||
902 (strcasecmp(u
->host
, nodename
) == 0)) {
904 if (strcasecmp(host
, nodename
) == 0) {
906 * Request submitted locally
907 * for the local queue.
908 * Hostname will not be displayed
910 snprintf(User
, sizeof (User
), "%s",
914 snprintf(User
, sizeof (User
), "%s@%s",
916 } else if (uri
!= NULL
) {
918 * It's a remote printer.
919 * In case of remote printers hostname is
922 snprintf(User
, sizeof (User
), "%s@%s",
928 * If attribute "job-printer-uri"
930 * by default append the hostname
932 snprintf(User
, sizeof (User
), "%s@%s", user
, host
);
936 * When print server is s10u4 and ipp service is used
937 * "job-originating-hostname" attribute is not set
938 * So get the host information from the uri
941 if ((uri
!= NULL
) && (uri_from_string(uri
, &u
) == 0)) {
942 if ((u
!= NULL
) && (u
->host
!= NULL
))
943 snprintf(User
, sizeof (User
), "%s@%s",
946 snprintf(User
, sizeof (User
), "%s", user
);
950 snprintf(User
, sizeof (User
), "%s", user
);
952 (void) papiAttributeListGetInteger(attrs
, NULL
, "job-k-octets", &size
);
953 size
*= 1024; /* for the approximate byte size */
954 (void) papiAttributeListGetInteger(attrs
, NULL
, "job-octets", &size
);
957 (void) papiAttributeListGetInteger(attrs
, NULL
,
958 "time-at-creation", (int32_t *)&clock
);
959 (void) strftime(date
, sizeof (date
), "%b %d %R", localtime(&clock
));
961 (void) papiAttributeListGetString(attrs
, NULL
,
962 "job-printer-uri", &destination
);
963 (void) papiAttributeListGetString(attrs
, NULL
,
964 "printer-name", &destination
);
965 (void) papiAttributeListGetInteger(attrs
, NULL
,
967 (void) papiAttributeListGetInteger(attrs
, NULL
,
968 "job-id-requested", &id
);
971 snprintf(request
, sizeof (request
), "%s-%d", printer
, id
);
973 if (show_rank
!= 0) {
976 (void) papiAttributeListGetInteger(attrs
, NULL
,
977 "number-of-intervening-jobs", &rank
);
980 printf("%3d %-21s %-14s %7ld %s",
981 rank
, request
, User
, size
, date
);
983 printf("%-23s %-14s %7ld %s", request
, User
, size
, date
);
985 (void) papiAttributeListGetInteger(attrs
, NULL
,
986 "job-state", &jstate
);
988 if (jstate
== 0x0001)
989 printf(gettext(" being held"));
990 else if (jstate
== 0x0800)
991 printf(gettext(" notifying user"));
992 else if (jstate
== 0x0040)
993 printf(gettext(" cancelled"));
994 else if (jstate
== 0x0010)
995 printf(gettext(" finished printing"));
996 else if (jstate
== 0x0008)
997 printf(gettext(" on %s"), destination
);
998 else if (jstate
== 0x2000)
999 printf(gettext(" held by admin"));
1000 else if (jstate
== 0x0002)
1001 printf(gettext(" being filtered"));
1002 else if (jstate
== 0x0004)
1003 printf(gettext(" filtered"));
1004 else if (jstate
== 0x0020)
1005 printf(gettext(" held for change"));
1010 (void) papiAttributeListGetString(attrs
, NULL
,
1011 "output-device-assigned", &destination
);
1012 printf("\n\t assigned %s", destination
);
1014 (void) papiAttributeListGetString(attrs
, NULL
, "form", &form
);
1016 printf(", form %s", form
);
1017 } else if (verbose
> 1) {
1019 papiAttributeListPrint(stdout
, attrs
, "\t");
1028 job_query(char *request
, int (*report
)(char *, papi_job_t
, int, int),
1029 papi_encryption_t encryption
, int show_rank
, int verbose
)
1032 papi_status_t status
;
1033 papi_service_t svc
= NULL
;
1034 char *printer
= request
;
1041 status
= papiServiceCreate(&svc
, printer
, NULL
, NULL
,
1042 cli_auth_callback
, encryption
, NULL
);
1044 if ((status
== PAPI_OK
) && (printer
!= NULL
))
1047 /* <name>-# printer name does not exist */
1048 if (status
!= PAPI_OK
) {
1050 * Check if <name>-# is a request-id
1051 * Once this check is done flag1 is set
1056 get_printer_id(printer
, &printer
, &id
);
1058 status
= papiServiceCreate(&svc
, printer
, NULL
, NULL
,
1059 cli_auth_callback
, encryption
, NULL
);
1061 if (status
!= PAPI_OK
) {
1062 fprintf(stderr
, gettext(
1063 "Failed to contact service for %s: %s\n"),
1064 (printer
? printer
: "all"),
1065 verbose_papi_message(svc
, status
));
1070 if (printer
== NULL
) { /* all */
1071 char **interest
= interest_list(svc
);
1073 if (interest
!= NULL
) {
1076 for (i
= 0; interest
[i
] != NULL
; i
++)
1077 result
+= job_query(interest
[i
], report
,
1078 encryption
, show_rank
, verbose
);
1080 } else if (id
== -1) { /* a printer */
1081 papi_job_t
*jobs
= NULL
;
1083 status
= papiPrinterListJobs(svc
, printer
, NULL
,
1085 if (status
!= PAPI_OK
) {
1086 fprintf(stderr
, gettext(
1087 "Failed to get job list: %s\n"),
1088 verbose_papi_message(svc
, status
));
1089 papiServiceDestroy(svc
);
1096 for (i
= 0; jobs
[i
] != NULL
; i
++)
1097 result
+= report(printer
,
1102 papiJobListFree(jobs
);
1103 } else { /* a job */
1104 papi_job_t job
= NULL
;
1106 /* Once a job has been found stop processing */
1110 * Job-id could be the job-id requested
1111 * Check if it is job-id or job-id-requested
1113 id
= job_to_be_queried(svc
, printer
, id
);
1116 status
= papiJobQuery(svc
, printer
, id
,
1120 status
= PAPI_NOT_FOUND
;
1122 if (status
!= PAPI_OK
) {
1124 fprintf(stderr
, gettext(
1125 "Failed to get job"\
1126 " info for %s: %s\n"),
1128 verbose_papi_message(svc
, status
));
1129 papiServiceDestroy(svc
);
1134 result
= report(printer
, job
,
1135 show_rank
, verbose
);
1142 get_printer_id(printer
, &printer
, &id
);
1150 papiServiceDestroy(svc
);
1156 report_form(char *name
, papi_attribute_t
**attrs
, int verbose
)
1158 papi_status_t status
;
1162 for (status
= papiAttributeListGetString(attrs
, &iter
,
1163 "form-supported", &form
);
1165 status
= papiAttributeListGetString(attrs
, &iter
,
1167 if ((name
== NULL
) || (strcmp(name
, form
) == 0)) {
1168 printf(gettext("form %s is available to you\n"), form
);
1170 char *detail
= NULL
;
1171 status
= papiAttributeListGetString(attrs
, NULL
,
1172 "form-supported-detail", &detail
);
1173 if (status
== PAPI_OK
)
1174 printf("%s\n", detail
);
1183 report_print_wheels(char *name
, papi_attribute_t
**attrs
, int verbose
)
1185 papi_status_t status
;
1189 for (status
= papiAttributeListGetString(attrs
, &iter
,
1190 "pw-supported", &pw
);
1192 status
= papiAttributeListGetString(attrs
, &iter
, NULL
, &pw
)) {
1193 if ((name
== NULL
) || (strcmp(name
, pw
) == 0)) {
1194 printf(gettext("charset %s is available\n"), pw
);
1197 status
= papiAttributeListGetString(attrs
, NULL
,
1198 "pw-supported-extra", &info
);
1199 if (status
== PAPI_OK
)
1200 printf("%s\n", info
);
1209 service_query(char *name
, int (*report
)(char *, papi_attribute_t
**, int),
1210 papi_encryption_t encryption
, int verbose
)
1213 papi_status_t status
;
1214 papi_service_t svc
= NULL
;
1215 papi_attribute_t
**attrs
= NULL
;
1217 status
= papiServiceCreate(&svc
, name
, NULL
, NULL
, cli_auth_callback
,
1219 if (status
!= PAPI_OK
) {
1220 papiServiceDestroy(svc
);
1224 attrs
= papiServiceGetAttributeList(svc
);
1225 if (attrs
!= NULL
) {
1226 result
= report(name
, attrs
, verbose
);
1230 papiAttributeListPrint(stdout
, attrs
, "\t");
1235 papiServiceDestroy(svc
);
1241 main(int ac
, char *av
[])
1244 papi_encryption_t encryption
= PAPI_ENCRYPT_NEVER
;
1247 int description
= 0;
1251 (void) setlocale(LC_ALL
, "");
1252 (void) textdomain("SUNW_OST_OSCMD");
1254 argv
= (char **)calloc((ac
+ 1), sizeof (char *));
1255 for (c
= 0; c
< ac
; c
++) {
1256 if ((av
[c
][0] == '-') && (av
[c
][1] == 'l') &&
1257 (isalpha(av
[c
][2]) != 0)) {
1258 /* preserve old "-l[po...]" behavior */
1259 argv
[c
] = &av
[c
][1];
1270 /* preprocess argument list looking for '-l' or '-R' so it can trail */
1271 while ((c
= getopt(ac
, argv
, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF
) {
1272 switch (c
) { /* these may or may not have an option */
1283 if (optarg
[0] == '-') {
1284 /* this check stop a possible infinite loop */
1285 if ((optind
> 1) && (argv
[optind
-1][1] != c
))
1288 } else if (strcmp(optarg
, "all") == 0)
1294 if ((optarg
== NULL
) || (optarg
[0] == '-'))
1296 verbose
= atoi(optarg
);
1305 encryption
= PAPI_ENCRYPT_REQUIRED
;
1313 /* process command line arguments */
1314 while ((c
= getopt(ac
, argv
, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF
) {
1315 switch (c
) { /* these may or may not have an option */
1326 if (optarg
[0] == '-') {
1327 /* this check stop a possible infinite loop */
1328 if ((optind
> 1) && (argv
[optind
-1][1] != c
))
1331 } else if (strcmp(optarg
, "all") == 0)
1337 exit_code
+= printer_query(optarg
, report_accepting
,
1338 encryption
, verbose
, 0);
1341 exit_code
+= printer_query(optarg
, report_class
,
1342 encryption
, verbose
, 0);
1345 exit_code
+= printer_query(optarg
, report_printer
,
1346 encryption
, verbose
, description
);
1349 exit_code
+= lpstat_default_printer(encryption
);
1352 exit_code
+= lpstat_service_status(encryption
);
1356 users
= strsplit(optarg
, ", \n");
1357 exit_code
+= job_query(NULL
, report_job
,
1358 encryption
, rank
, verbose
);
1359 if (users
!= NULL
) {
1365 exit_code
+= printer_query(optarg
, report_device
,
1366 encryption
, verbose
, 0);
1368 case 'R': /* set "rank" flag in first pass */
1370 exit_code
+= job_query(optarg
, report_job
,
1371 encryption
, rank
, verbose
);
1374 exit_code
+= service_query(optarg
, report_form
,
1375 encryption
, verbose
);
1378 exit_code
+= service_query(optarg
, report_print_wheels
,
1379 encryption
, verbose
);
1382 exit_code
+= lpstat_service_status(encryption
);
1383 exit_code
+= lpstat_default_printer(encryption
);
1384 exit_code
+= printer_query(NULL
, report_class
,
1385 encryption
, verbose
, 0);
1386 exit_code
+= printer_query(NULL
, report_device
,
1387 encryption
, verbose
, 0);
1388 exit_code
+= service_query(optarg
, report_form
,
1389 encryption
, verbose
);
1390 exit_code
+= service_query(optarg
, report_print_wheels
,
1391 encryption
, verbose
);
1394 exit_code
+= lpstat_service_status(encryption
);
1395 exit_code
+= lpstat_default_printer(encryption
);
1396 exit_code
+= printer_query(NULL
, report_class
,
1397 encryption
, verbose
, 0);
1398 exit_code
+= printer_query(NULL
, report_device
,
1399 encryption
, verbose
, 0);
1400 exit_code
+= printer_query(NULL
, report_accepting
,
1401 encryption
, verbose
, 0);
1402 exit_code
+= printer_query(NULL
, report_printer
,
1403 encryption
, verbose
, 0);
1404 exit_code
+= service_query(optarg
, report_form
,
1405 encryption
, verbose
);
1406 exit_code
+= service_query(optarg
, report_print_wheels
,
1407 encryption
, verbose
);
1408 exit_code
+= job_query(NULL
, report_job
,
1409 encryption
, rank
, verbose
);
1411 case 'L': /* local-only, ignored */
1412 case 'l': /* increased verbose level in first pass */
1413 case 'D': /* set "description" flag in first pass */
1414 case 'E': /* set encryption in the first pass */
1422 if (ac
== 1) { /* report on my jobs */
1423 struct passwd
*pw
= getpwuid(getuid());
1426 users
= strsplit(pw
->pw_name
, "");
1427 exit_code
+= job_query(NULL
, report_job
, encryption
,
1429 if (users
!= NULL
) {
1434 for (c
= optind
; c
< ac
; c
++)
1435 exit_code
+= job_query(argv
[c
], report_job
, encryption
,