]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpstat.c
4d6495637f7b5eaf464079a5f0f3d3ea4b3e7abd
[thirdparty/cups.git] / systemv / lpstat.c
1 /*
2 * "$Id$"
3 *
4 * "lpstat" command for CUPS.
5 *
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * main() - Parse options and show status information.
18 * check_dest() - Verify that the named destination(s) exists.
19 * match_list() - Match a name from a list of comma or space-separated
20 * names.
21 * show_accepting() - Show acceptance status.
22 * show_classes() - Show printer classes.
23 * show_default() - Show default destination.
24 * show_devices() - Show printer devices.
25 * show_jobs() - Show active print jobs.
26 * show_printers() - Show printers.
27 * show_scheduler() - Show scheduler status.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include <cups/cups-private.h>
35
36
37 /*
38 * Local functions...
39 */
40
41 static void check_dest(const char *command, const char *name,
42 int *num_dests, cups_dest_t **dests);
43 static int match_list(const char *list, const char *name);
44 static int show_accepting(const char *printers, int num_dests,
45 cups_dest_t *dests);
46 static int show_classes(const char *dests);
47 static void show_default(cups_dest_t *dest);
48 static int show_devices(const char *printers, int num_dests,
49 cups_dest_t *dests);
50 static int show_jobs(const char *dests, const char *users, int long_status,
51 int ranking, const char *which);
52 static int show_printers(const char *printers, int num_dests,
53 cups_dest_t *dests, int long_status);
54 static void show_scheduler(void);
55
56
57 /*
58 * 'main()' - Parse options and show status information.
59 */
60
61 int
62 main(int argc, /* I - Number of command-line arguments */
63 char *argv[]) /* I - Command-line arguments */
64 {
65 int i, /* Looping var */
66 status; /* Exit status */
67 int num_dests; /* Number of user destinations */
68 cups_dest_t *dests; /* User destinations */
69 int long_status; /* Long status report? */
70 int ranking; /* Show job ranking? */
71 const char *which; /* Which jobs to show? */
72 char op; /* Last operation on command-line */
73
74
75 _cupsSetLocale(argv);
76
77 /*
78 * Parse command-line options...
79 */
80
81 num_dests = 0;
82 dests = NULL;
83 long_status = 0;
84 ranking = 0;
85 status = 0;
86 which = "not-completed";
87 op = 0;
88
89 for (i = 1; i < argc; i ++)
90 if (argv[i][0] == '-')
91 switch (argv[i][1])
92 {
93 case 'D' : /* Show description */
94 long_status = 1;
95 break;
96
97 case 'E' : /* Encrypt */
98 #ifdef HAVE_SSL
99 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
100 #else
101 _cupsLangPrintf(stderr,
102 _("%s: Sorry, no encryption support."),
103 argv[0]);
104 #endif /* HAVE_SSL */
105 break;
106
107 case 'H' : /* Show server and port */
108 if (cupsServer()[0] == '/')
109 _cupsLangPuts(stdout, cupsServer());
110 else
111 _cupsLangPrintf(stdout, "%s:%d", cupsServer(), ippPort());
112 break;
113
114 case 'P' : /* Show paper types */
115 op = 'P';
116 break;
117
118 case 'R' : /* Show ranking */
119 ranking = 1;
120 break;
121
122 case 'S' : /* Show charsets */
123 op = 'S';
124 if (!argv[i][2])
125 i ++;
126 break;
127
128 case 'U' : /* Username */
129 if (argv[i][2])
130 cupsSetUser(argv[i] + 2);
131 else
132 {
133 i ++;
134 if (i >= argc)
135 {
136 _cupsLangPrintf(stderr,
137 _("%s: Error - expected username after "
138 "\"-U\" option."),
139 argv[0]);
140 return (1);
141 }
142
143 cupsSetUser(argv[i]);
144 }
145 break;
146
147 case 'W' : /* Show which jobs? */
148 if (argv[i][2])
149 which = argv[i] + 2;
150 else
151 {
152 i ++;
153
154 if (i >= argc)
155 {
156 _cupsLangPrintf(stderr,
157 _("%s: Error - need \"completed\", "
158 "\"not-completed\", or \"all\" after "
159 "\"-W\" option."),
160 argv[0]);
161 return (1);
162 }
163
164 which = argv[i];
165 }
166
167 if (strcmp(which, "completed") && strcmp(which, "not-completed") &&
168 strcmp(which, "all"))
169 {
170 _cupsLangPrintf(stderr,
171 _("%s: Error - need \"completed\", "
172 "\"not-completed\", or \"all\" after "
173 "\"-W\" option."),
174 argv[0]);
175 return (1);
176 }
177 break;
178
179 case 'a' : /* Show acceptance status */
180 op = 'a';
181
182 if (argv[i][2])
183 {
184 check_dest(argv[0], argv[i] + 2, &num_dests, &dests);
185
186 status |= show_accepting(argv[i] + 2, num_dests, dests);
187 }
188 else if ((i + 1) < argc && argv[i + 1][0] != '-')
189 {
190 i ++;
191
192 check_dest(argv[0], argv[i], &num_dests, &dests);
193
194 status |= show_accepting(argv[i], num_dests, dests);
195 }
196 else
197 {
198 if (num_dests <= 1)
199 {
200 cupsFreeDests(num_dests, dests);
201 num_dests = cupsGetDests(&dests);
202 }
203
204 status |= show_accepting(NULL, num_dests, dests);
205 }
206 break;
207
208 case 'c' : /* Show classes and members */
209 op = 'c';
210
211 if (argv[i][2])
212 {
213 check_dest(argv[0], argv[i] + 2, &num_dests, &dests);
214
215 status |= show_classes(argv[i] + 2);
216 }
217 else if ((i + 1) < argc && argv[i + 1][0] != '-')
218 {
219 i ++;
220
221 check_dest(argv[0], argv[i], &num_dests, &dests);
222
223 status |= show_classes(argv[i]);
224 }
225 else
226 status |= show_classes(NULL);
227 break;
228
229 case 'd' : /* Show default destination */
230 op = 'd';
231
232 if (num_dests != 1 || !dests[0].is_default)
233 {
234 cupsFreeDests(num_dests, dests);
235
236 dests = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL);
237 num_dests = dests ? 1 : 0;
238 }
239
240 show_default(dests);
241 break;
242
243 case 'f' : /* Show forms */
244 op = 'f';
245 if (!argv[i][2])
246 i ++;
247 break;
248
249 case 'h' : /* Connect to host */
250 if (argv[i][2])
251 cupsSetServer(argv[i] + 2);
252 else
253 {
254 i ++;
255
256 if (i >= argc)
257 {
258 _cupsLangPrintf(stderr,
259 _("%s: Error - expected hostname after "
260 "\"-h\" option."),
261 argv[0]);
262 return (1);
263 }
264
265 cupsSetServer(argv[i]);
266 }
267 break;
268
269 case 'l' : /* Long status or long job status */
270 long_status = 2;
271 break;
272
273 case 'o' : /* Show jobs by destination */
274 op = 'o';
275
276 if (argv[i][2])
277 {
278 check_dest(argv[0], argv[i] + 2, &num_dests, &dests);
279
280 status |= show_jobs(argv[i] + 2, NULL, long_status, ranking,
281 which);
282 }
283 else if ((i + 1) < argc && argv[i + 1][0] != '-')
284 {
285 i ++;
286
287 check_dest(argv[0], argv[i], &num_dests, &dests);
288
289 status |= show_jobs(argv[i], NULL, long_status, ranking, which);
290 }
291 else
292 status |= show_jobs(NULL, NULL, long_status, ranking, which);
293 break;
294
295 case 'p' : /* Show printers */
296 op = 'p';
297
298 if (argv[i][2])
299 {
300 check_dest(argv[0], argv[i] + 2, &num_dests, &dests);
301
302 status |= show_printers(argv[i] + 2, num_dests, dests,
303 long_status);
304 }
305 else if ((i + 1) < argc && argv[i + 1][0] != '-')
306 {
307 i ++;
308
309 check_dest(argv[0], argv[i], &num_dests, &dests);
310
311 status |= show_printers(argv[i], num_dests, dests, long_status);
312 }
313 else
314 {
315 if (num_dests <= 1)
316 {
317 cupsFreeDests(num_dests, dests);
318 num_dests = cupsGetDests(&dests);
319 }
320
321 status |= show_printers(NULL, num_dests, dests, long_status);
322 }
323 break;
324
325 case 'r' : /* Show scheduler status */
326 op = 'r';
327
328 show_scheduler();
329 break;
330
331 case 's' : /* Show summary */
332 op = 's';
333
334 if (num_dests <= 1)
335 {
336 cupsFreeDests(num_dests, dests);
337 num_dests = cupsGetDests(&dests);
338 }
339
340 show_default(cupsGetDest(NULL, NULL, num_dests, dests));
341 status |= show_classes(NULL);
342 status |= show_devices(NULL, num_dests, dests);
343 break;
344
345 case 't' : /* Show all info */
346 op = 't';
347
348 if (num_dests <= 1)
349 {
350 cupsFreeDests(num_dests, dests);
351 num_dests = cupsGetDests(&dests);
352 }
353
354 show_scheduler();
355 show_default(cupsGetDest(NULL, NULL, num_dests, dests));
356 status |= show_classes(NULL);
357 status |= show_devices(NULL, num_dests, dests);
358 status |= show_accepting(NULL, num_dests, dests);
359 status |= show_printers(NULL, num_dests, dests, long_status);
360 status |= show_jobs(NULL, NULL, long_status, ranking, which);
361 break;
362
363 case 'u' : /* Show jobs by user */
364 op = 'u';
365
366 if (argv[i][2])
367 status |= show_jobs(NULL, argv[i] + 2, long_status, ranking,
368 which);
369 else if ((i + 1) < argc && argv[i + 1][0] != '-')
370 {
371 i ++;
372 status |= show_jobs(NULL, argv[i], long_status, ranking, which);
373 }
374 else
375 status |= show_jobs(NULL, NULL, long_status, ranking, which);
376 break;
377
378 case 'v' : /* Show printer devices */
379 op = 'v';
380
381 if (argv[i][2])
382 {
383 check_dest(argv[0], argv[i] + 2, &num_dests, &dests);
384
385 status |= show_devices(argv[i] + 2, num_dests, dests);
386 }
387 else if ((i + 1) < argc && argv[i + 1][0] != '-')
388 {
389 i ++;
390
391 check_dest(argv[0], argv[i], &num_dests, &dests);
392
393 status |= show_devices(argv[i], num_dests, dests);
394 }
395 else
396 {
397 if (num_dests <= 1)
398 {
399 cupsFreeDests(num_dests, dests);
400 num_dests = cupsGetDests(&dests);
401 }
402
403 status |= show_devices(NULL, num_dests, dests);
404 }
405 break;
406
407 default :
408 _cupsLangPrintf(stderr,
409 _("%s: Error - unknown option \"%c\"."),
410 argv[0], argv[i][1]);
411 return (1);
412 }
413 else
414 {
415 status |= show_jobs(argv[i], NULL, long_status, ranking, which);
416 op = 'o';
417 }
418
419 if (!op)
420 status |= show_jobs(NULL, cupsUser(), long_status, ranking, which);
421
422 return (status);
423 }
424
425
426 /*
427 * 'check_dest()' - Verify that the named destination(s) exists.
428 */
429
430 static void
431 check_dest(const char *command, /* I - Command name */
432 const char *name, /* I - List of printer/class names */
433 int *num_dests, /* IO - Number of destinations */
434 cups_dest_t **dests) /* IO - Destinations */
435 {
436 const char *dptr; /* Pointer into name */
437 char *pptr, /* Pointer into printer */
438 printer[1024]; /* Current printer/class name */
439
440
441 /*
442 * Load the destination list as necessary...
443 */
444
445 if (*num_dests <= 1)
446 {
447 if (*num_dests)
448 cupsFreeDests(*num_dests, *dests);
449
450 if (strchr(name, ','))
451 *num_dests = cupsGetDests(dests);
452 else
453 {
454 strlcpy(printer, name, sizeof(printer));
455 if ((pptr = strchr(printer, '/')) != NULL)
456 *pptr++ = '\0';
457
458 if ((*dests = cupsGetNamedDest(CUPS_HTTP_DEFAULT, printer, pptr)) == NULL)
459 {
460 _cupsLangPrintf(stderr,
461 _("%s: Invalid destination name in list \"%s\"."),
462 command, name);
463 exit(1);
464 }
465 else
466 {
467 *num_dests = 1;
468 return;
469 }
470 }
471 }
472
473 /*
474 * Scan the name string for printer/class name(s)...
475 */
476
477 for (dptr = name; *dptr;)
478 {
479 /*
480 * Skip leading whitespace and commas...
481 */
482
483 while (isspace(*dptr & 255) || *dptr == ',')
484 dptr ++;
485
486 if (!*dptr)
487 break;
488
489 /*
490 * Extract a single destination name from the name string...
491 */
492
493 for (pptr = printer; !isspace(*dptr & 255) && *dptr != ',' && *dptr;)
494 {
495 if ((pptr - printer) < (sizeof(printer) - 1))
496 *pptr++ = *dptr++;
497 else
498 {
499 _cupsLangPrintf(stderr,
500 _("%s: Invalid destination name in list \"%s\"."),
501 command, name);
502 exit(1);
503 }
504 }
505
506 *pptr = '\0';
507
508 /*
509 * Check the destination...
510 */
511
512 if (!cupsGetDest(printer, NULL, *num_dests, *dests))
513 {
514 _cupsLangPrintf(stderr,
515 _("%s: Unknown destination \"%s\"."), command, printer);
516 exit(1);
517 }
518 }
519 }
520
521
522 /*
523 * 'match_list()' - Match a name from a list of comma or space-separated names.
524 */
525
526 static int /* O - 1 on match, 0 on no match */
527 match_list(const char *list, /* I - List of names */
528 const char *name) /* I - Name to find */
529 {
530 const char *nameptr; /* Pointer into name */
531
532
533 /*
534 * An empty list always matches...
535 */
536
537 if (!list || !*list)
538 return (1);
539
540 if (!name)
541 return (0);
542
543 while (*list)
544 {
545 /*
546 * Skip leading whitespace and commas...
547 */
548
549 while (isspace(*list & 255) || *list == ',')
550 list ++;
551
552 if (!*list)
553 break;
554
555 /*
556 * Compare names...
557 */
558
559 for (nameptr = name;
560 *nameptr && *list && tolower(*nameptr & 255) == tolower(*list & 255);
561 nameptr ++, list ++);
562
563 if (!*nameptr && (!*list || *list == ',' || isspace(*list & 255)))
564 return (1);
565
566 while (*list && !isspace(*list & 255) && *list != ',')
567 list ++;
568 }
569
570 return (0);
571 }
572
573
574 /*
575 * 'show_accepting()' - Show acceptance status.
576 */
577
578 static int /* O - 0 on success, 1 on fail */
579 show_accepting(const char *printers, /* I - Destinations */
580 int num_dests, /* I - Number of user-defined dests */
581 cups_dest_t *dests) /* I - User-defined destinations */
582 {
583 int i; /* Looping var */
584 ipp_t *request, /* IPP Request */
585 *response; /* IPP Response */
586 ipp_attribute_t *attr; /* Current attribute */
587 const char *printer, /* Printer name */
588 *message; /* Printer device URI */
589 int accepting; /* Accepting requests? */
590 time_t ptime; /* Printer state time */
591 struct tm *pdate; /* Printer state date & time */
592 char printer_state_time[255];/* Printer state time */
593 static const char *pattrs[] = /* Attributes we need for printers... */
594 {
595 "printer-name",
596 "printer-state-change-time",
597 "printer-state-message",
598 "printer-is-accepting-jobs"
599 };
600
601
602 DEBUG_printf(("show_accepting(printers=\"%s\")\n", printers));
603
604 if (printers != NULL && !strcmp(printers, "all"))
605 printers = NULL;
606
607 /*
608 * Build a CUPS_GET_PRINTERS request, which requires the following
609 * attributes:
610 *
611 * attributes-charset
612 * attributes-natural-language
613 * requested-attributes
614 * requesting-user-name
615 */
616
617 request = ippNewRequest(CUPS_GET_PRINTERS);
618
619 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
620 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
621 NULL, pattrs);
622
623 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
624 NULL, cupsUser());
625
626 /*
627 * Do the request and get back a response...
628 */
629
630 if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
631 {
632 DEBUG_puts("show_accepting: request succeeded...");
633
634 if (response->request.status.status_code > IPP_OK_CONFLICT)
635 {
636 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
637 ippDelete(response);
638 return (1);
639 }
640
641 /*
642 * Loop through the printers returned in the list and display
643 * their devices...
644 */
645
646 for (attr = response->attrs; attr != NULL; attr = attr->next)
647 {
648 /*
649 * Skip leading attributes until we hit a printer...
650 */
651
652 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
653 attr = attr->next;
654
655 if (attr == NULL)
656 break;
657
658 /*
659 * Pull the needed attributes from this printer...
660 */
661
662 printer = NULL;
663 message = NULL;
664 accepting = 1;
665 ptime = 0;
666
667 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
668 {
669 if (!strcmp(attr->name, "printer-name") &&
670 attr->value_tag == IPP_TAG_NAME)
671 printer = attr->values[0].string.text;
672 else if (!strcmp(attr->name, "printer-state-change-time") &&
673 attr->value_tag == IPP_TAG_INTEGER)
674 ptime = (time_t)attr->values[0].integer;
675 else if (!strcmp(attr->name, "printer-state-message") &&
676 attr->value_tag == IPP_TAG_TEXT)
677 message = attr->values[0].string.text;
678 else if (!strcmp(attr->name, "printer-is-accepting-jobs") &&
679 attr->value_tag == IPP_TAG_BOOLEAN)
680 accepting = attr->values[0].boolean;
681
682 attr = attr->next;
683 }
684
685 /*
686 * See if we have everything needed...
687 */
688
689 if (printer == NULL)
690 {
691 if (attr == NULL)
692 break;
693 else
694 continue;
695 }
696
697 /*
698 * Display the printer entry if needed...
699 */
700
701 if (match_list(printers, printer))
702 {
703 pdate = localtime(&ptime);
704 strftime(printer_state_time, sizeof(printer_state_time), "%c", pdate);
705
706 if (accepting)
707 _cupsLangPrintf(stdout, _("%s accepting requests since %s"),
708 printer, printer_state_time);
709 else
710 {
711 _cupsLangPrintf(stdout, _("%s not accepting requests since %s -"),
712 printer, printer_state_time);
713 _cupsLangPrintf(stdout, _("\t%s"),
714 (message == NULL || !*message) ?
715 "reason unknown" : message);
716 }
717
718 for (i = 0; i < num_dests; i ++)
719 if (!_cups_strcasecmp(dests[i].name, printer) && dests[i].instance)
720 {
721 if (accepting)
722 _cupsLangPrintf(stdout, _("%s/%s accepting requests since %s"),
723 printer, dests[i].instance, printer_state_time);
724 else
725 {
726 _cupsLangPrintf(stdout,
727 _("%s/%s not accepting requests since %s -"),
728 printer, dests[i].instance, printer_state_time);
729 _cupsLangPrintf(stdout, _("\t%s"),
730 (message == NULL || !*message) ?
731 "reason unknown" : message);
732 }
733 }
734 }
735
736 if (attr == NULL)
737 break;
738 }
739
740 ippDelete(response);
741 }
742 else
743 {
744 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
745 return (1);
746 }
747
748 return (0);
749 }
750
751
752 /*
753 * 'show_classes()' - Show printer classes.
754 */
755
756 static int /* O - 0 on success, 1 on fail */
757 show_classes(const char *dests) /* I - Destinations */
758 {
759 int i; /* Looping var */
760 ipp_t *request, /* IPP Request */
761 *response, /* IPP Response */
762 *response2; /* IPP response from remote server */
763 http_t *http2; /* Remote server */
764 ipp_attribute_t *attr; /* Current attribute */
765 const char *printer, /* Printer class name */
766 *printer_uri; /* Printer class URI */
767 ipp_attribute_t *members; /* Printer members */
768 char method[HTTP_MAX_URI], /* Request method */
769 username[HTTP_MAX_URI], /* Username:password */
770 server[HTTP_MAX_URI], /* Server name */
771 resource[HTTP_MAX_URI]; /* Resource name */
772 int port; /* Port number */
773 static const char *cattrs[] = /* Attributes we need for classes... */
774 {
775 "printer-name",
776 "printer-uri-supported",
777 "member-names"
778 };
779
780
781 DEBUG_printf(("show_classes(dests=\"%s\")\n", dests));
782
783 if (dests != NULL && !strcmp(dests, "all"))
784 dests = NULL;
785
786 /*
787 * Build a CUPS_GET_CLASSES request, which requires the following
788 * attributes:
789 *
790 * attributes-charset
791 * attributes-natural-language
792 * requested-attributes
793 * requesting-user-name
794 */
795
796 request = ippNewRequest(CUPS_GET_CLASSES);
797
798 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
799 "requested-attributes", sizeof(cattrs) / sizeof(cattrs[0]),
800 NULL, cattrs);
801
802 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
803 NULL, cupsUser());
804
805 /*
806 * Do the request and get back a response...
807 */
808
809 if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
810 {
811 DEBUG_puts("show_classes: request succeeded...");
812
813 if (response->request.status.status_code > IPP_OK_CONFLICT)
814 {
815 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
816 ippDelete(response);
817 return (1);
818 }
819
820 /*
821 * Loop through the printers returned in the list and display
822 * their devices...
823 */
824
825 for (attr = response->attrs; attr != NULL; attr = attr->next)
826 {
827 /*
828 * Skip leading attributes until we hit a job...
829 */
830
831 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
832 attr = attr->next;
833
834 if (attr == NULL)
835 break;
836
837 /*
838 * Pull the needed attributes from this job...
839 */
840
841 printer = NULL;
842 printer_uri = NULL;
843 members = NULL;
844
845 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
846 {
847 if (!strcmp(attr->name, "printer-name") &&
848 attr->value_tag == IPP_TAG_NAME)
849 printer = attr->values[0].string.text;
850
851 if (!strcmp(attr->name, "printer-uri-supported") &&
852 attr->value_tag == IPP_TAG_URI)
853 printer_uri = attr->values[0].string.text;
854
855 if (!strcmp(attr->name, "member-names") &&
856 attr->value_tag == IPP_TAG_NAME)
857 members = attr;
858
859 attr = attr->next;
860 }
861
862 /*
863 * If this is a remote class, grab the class info from the
864 * remote server...
865 */
866
867 response2 = NULL;
868 if (members == NULL && printer_uri != NULL)
869 {
870 httpSeparateURI(HTTP_URI_CODING_ALL, printer_uri, method, sizeof(method),
871 username, sizeof(username), server, sizeof(server),
872 &port, resource, sizeof(resource));
873
874 if (!_cups_strcasecmp(server, cupsServer()))
875 http2 = CUPS_HTTP_DEFAULT;
876 else
877 http2 = httpConnectEncrypt(server, port, cupsEncryption());
878
879 /*
880 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
881 * following attributes:
882 *
883 * attributes-charset
884 * attributes-natural-language
885 * printer-uri
886 * requested-attributes
887 */
888
889 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
890
891 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
892 "printer-uri", NULL, printer_uri);
893
894 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
895 "requested-attributes",
896 sizeof(cattrs) / sizeof(cattrs[0]),
897 NULL, cattrs);
898
899 if ((response2 = cupsDoRequest(http2, request, "/")) != NULL)
900 members = ippFindAttribute(response2, "member-names", IPP_TAG_NAME);
901
902 if (http2)
903 httpClose(http2);
904 }
905
906 /*
907 * See if we have everything needed...
908 */
909
910 if (printer == NULL)
911 {
912 if (response2)
913 ippDelete(response2);
914
915 if (attr == NULL)
916 break;
917 else
918 continue;
919 }
920
921 /*
922 * Display the printer entry if needed...
923 */
924
925 if (match_list(dests, printer))
926 {
927 _cupsLangPrintf(stdout, _("members of class %s:"), printer);
928
929 if (members)
930 {
931 for (i = 0; i < members->num_values; i ++)
932 _cupsLangPrintf(stdout, "\t%s", members->values[i].string.text);
933 }
934 else
935 _cupsLangPuts(stdout, "\tunknown");
936 }
937
938 if (response2)
939 ippDelete(response2);
940
941 if (attr == NULL)
942 break;
943 }
944
945 ippDelete(response);
946 }
947 else
948 {
949 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
950 return (1);
951 }
952
953 return (0);
954 }
955
956
957 /*
958 * 'show_default()' - Show default destination.
959 */
960
961 static void
962 show_default(cups_dest_t *dest) /* I - Default destination */
963 {
964 const char *printer, /* Printer name */
965 *val; /* Environment variable name */
966
967
968 if (dest)
969 {
970 if (dest->instance)
971 _cupsLangPrintf(stdout, _("system default destination: %s/%s"),
972 dest->name, dest->instance);
973 else
974 _cupsLangPrintf(stdout, _("system default destination: %s"),
975 dest->name);
976 }
977 else
978 {
979 val = NULL;
980
981 if ((printer = getenv("LPDEST")) == NULL)
982 {
983 if ((printer = getenv("PRINTER")) != NULL)
984 {
985 if (!strcmp(printer, "lp"))
986 printer = NULL;
987 else
988 val = "PRINTER";
989 }
990 }
991 else
992 val = "LPDEST";
993
994 if (printer)
995 _cupsLangPrintf(stdout,
996 _("lpstat: error - %s environment variable names "
997 "non-existent destination \"%s\"."),
998 val, printer);
999 else
1000 _cupsLangPuts(stdout, _("no system default destination"));
1001 }
1002 }
1003
1004
1005 /*
1006 * 'show_devices()' - Show printer devices.
1007 */
1008
1009 static int /* O - 0 on success, 1 on fail */
1010 show_devices(const char *printers, /* I - Destinations */
1011 int num_dests, /* I - Number of user-defined dests */
1012 cups_dest_t *dests) /* I - User-defined destinations */
1013 {
1014 int i; /* Looping var */
1015 ipp_t *request, /* IPP Request */
1016 *response; /* IPP Response */
1017 ipp_attribute_t *attr; /* Current attribute */
1018 const char *printer, /* Printer name */
1019 *uri, /* Printer URI */
1020 *device; /* Printer device URI */
1021 static const char *pattrs[] = /* Attributes we need for printers... */
1022 {
1023 "printer-name",
1024 "printer-uri-supported",
1025 "device-uri"
1026 };
1027
1028
1029 DEBUG_printf(("show_devices(printers=\"%s\")\n", printers));
1030
1031 if (printers != NULL && !strcmp(printers, "all"))
1032 printers = NULL;
1033
1034 /*
1035 * Build a CUPS_GET_PRINTERS request, which requires the following
1036 * attributes:
1037 *
1038 * attributes-charset
1039 * attributes-natural-language
1040 * requested-attributes
1041 * requesting-user-name
1042 */
1043
1044 request = ippNewRequest(CUPS_GET_PRINTERS);
1045
1046 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1047 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
1048 NULL, pattrs);
1049
1050 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1051 NULL, cupsUser());
1052
1053 /*
1054 * Do the request and get back a response...
1055 */
1056
1057 if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
1058 {
1059 DEBUG_puts("show_devices: request succeeded...");
1060
1061 if (response->request.status.status_code > IPP_OK_CONFLICT)
1062 {
1063 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
1064 ippDelete(response);
1065 return (1);
1066 }
1067
1068 /*
1069 * Loop through the printers returned in the list and display
1070 * their devices...
1071 */
1072
1073 for (attr = response->attrs; attr != NULL; attr = attr->next)
1074 {
1075 /*
1076 * Skip leading attributes until we hit a job...
1077 */
1078
1079 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1080 attr = attr->next;
1081
1082 if (attr == NULL)
1083 break;
1084
1085 /*
1086 * Pull the needed attributes from this job...
1087 */
1088
1089 printer = NULL;
1090 device = NULL;
1091 uri = NULL;
1092
1093 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1094 {
1095 if (!strcmp(attr->name, "printer-name") &&
1096 attr->value_tag == IPP_TAG_NAME)
1097 printer = attr->values[0].string.text;
1098
1099 if (!strcmp(attr->name, "printer-uri-supported") &&
1100 attr->value_tag == IPP_TAG_URI)
1101 uri = attr->values[0].string.text;
1102
1103 if (!strcmp(attr->name, "device-uri") &&
1104 attr->value_tag == IPP_TAG_URI)
1105 device = attr->values[0].string.text;
1106
1107 attr = attr->next;
1108 }
1109
1110 /*
1111 * See if we have everything needed...
1112 */
1113
1114 if (printer == NULL)
1115 {
1116 if (attr == NULL)
1117 break;
1118 else
1119 continue;
1120 }
1121
1122 /*
1123 * Display the printer entry if needed...
1124 */
1125
1126 if (match_list(printers, printer))
1127 {
1128 #ifdef __osf__ /* Compaq/Digital like to do it their own way... */
1129 char scheme[HTTP_MAX_URI], /* Components of printer URI */
1130 username[HTTP_MAX_URI],
1131 hostname[HTTP_MAX_URI],
1132 resource[HTTP_MAX_URI];
1133 int port;
1134
1135
1136 if (device == NULL)
1137 {
1138 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
1139 username, sizeof(username), hostname,
1140 sizeof(hostname), &port, resource, sizeof(resource));
1141 _cupsLangPrintf(stdout,
1142 _("Output for printer %s is sent to remote "
1143 "printer %s on %s"),
1144 printer, strrchr(resource, '/') + 1, hostname);
1145 }
1146 else if (!strncmp(device, "file:", 5))
1147 _cupsLangPrintf(stdout,
1148 _("Output for printer %s is sent to %s"),
1149 printer, device + 5);
1150 else
1151 _cupsLangPrintf(stdout,
1152 _("Output for printer %s is sent to %s"),
1153 printer, device);
1154
1155 for (i = 0; i < num_dests; i ++)
1156 if (!_cups_strcasecmp(printer, dests[i].name) && dests[i].instance)
1157 {
1158 if (device == NULL)
1159 _cupsLangPrintf(stdout,
1160 _("Output for printer %s/%s is sent to "
1161 "remote printer %s on %s"),
1162 printer, dests[i].instance,
1163 strrchr(resource, '/') + 1, hostname);
1164 else if (!strncmp(device, "file:", 5))
1165 _cupsLangPrintf(stdout,
1166 _("Output for printer %s/%s is sent to %s"),
1167 printer, dests[i].instance, device + 5);
1168 else
1169 _cupsLangPrintf(stdout,
1170 _("Output for printer %s/%s is sent to %s"),
1171 printer, dests[i].instance, device);
1172 }
1173 #else
1174 if (device == NULL)
1175 _cupsLangPrintf(stdout, _("device for %s: %s"),
1176 printer, uri);
1177 else if (!strncmp(device, "file:", 5))
1178 _cupsLangPrintf(stdout, _("device for %s: %s"),
1179 printer, device + 5);
1180 else
1181 _cupsLangPrintf(stdout, _("device for %s: %s"),
1182 printer, device);
1183
1184 for (i = 0; i < num_dests; i ++)
1185 if (!_cups_strcasecmp(printer, dests[i].name) && dests[i].instance)
1186 {
1187 if (device == NULL)
1188 _cupsLangPrintf(stdout, _("device for %s/%s: %s"),
1189 printer, dests[i].instance, uri);
1190 else if (!strncmp(device, "file:", 5))
1191 _cupsLangPrintf(stdout, _("device for %s/%s: %s"),
1192 printer, dests[i].instance, device + 5);
1193 else
1194 _cupsLangPrintf(stdout, _("device for %s/%s: %s"),
1195 printer, dests[i].instance, device);
1196 }
1197 #endif /* __osf__ */
1198 }
1199
1200 if (attr == NULL)
1201 break;
1202 }
1203
1204 ippDelete(response);
1205 }
1206 else
1207 {
1208 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
1209 return (1);
1210 }
1211
1212 return (0);
1213 }
1214
1215
1216 /*
1217 * 'show_jobs()' - Show active print jobs.
1218 */
1219
1220 static int /* O - 0 on success, 1 on fail */
1221 show_jobs(const char *dests, /* I - Destinations */
1222 const char *users, /* I - Users */
1223 int long_status, /* I - Show long status? */
1224 int ranking, /* I - Show job ranking? */
1225 const char *which) /* I - Show which jobs? */
1226 {
1227 int i; /* Looping var */
1228 ipp_t *request, /* IPP Request */
1229 *response; /* IPP Response */
1230 ipp_attribute_t *attr, /* Current attribute */
1231 *reasons; /* Job state reasons attribute */
1232 const char *dest, /* Pointer into job-printer-uri */
1233 *username, /* Pointer to job-originating-user-name */
1234 *title, /* Pointer to job-name */
1235 *message; /* Pointer to job-printer-state-message */
1236 int rank, /* Rank in queue */
1237 jobid, /* job-id */
1238 size; /* job-k-octets */
1239 time_t jobtime; /* time-at-creation */
1240 struct tm *jobdate; /* Date & time */
1241 char temp[255], /* Temporary buffer */
1242 date[255]; /* Date buffer */
1243 static const char *jattrs[] = /* Attributes we need for jobs... */
1244 {
1245 "job-id",
1246 "job-k-octets",
1247 "job-name",
1248 "job-originating-user-name",
1249 "job-printer-state-message",
1250 "job-printer-uri",
1251 "job-state-reasons",
1252 "time-at-creation"
1253 };
1254
1255
1256 DEBUG_printf(("show_jobs(dests=\"%s\", users=\"%s\", long_status=%d, "
1257 "ranking=%d, which=\"%s\")\n", dests, users, long_status,
1258 ranking, which));
1259
1260 if (dests != NULL && !strcmp(dests, "all"))
1261 dests = NULL;
1262
1263 /*
1264 * Build a IPP_GET_JOBS request, which requires the following
1265 * attributes:
1266 *
1267 * attributes-charset
1268 * attributes-natural-language
1269 * printer-uri
1270 * requested-attributes
1271 * requesting-user-name
1272 * which-jobs
1273 */
1274
1275 request = ippNewRequest(IPP_GET_JOBS);
1276
1277 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1278 NULL, "ipp://localhost/");
1279
1280 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1281 "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
1282 NULL, jattrs);
1283
1284 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1285 NULL, cupsUser());
1286
1287 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
1288 NULL, which);
1289
1290 /*
1291 * Do the request and get back a response...
1292 */
1293
1294 if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
1295 {
1296 /*
1297 * Loop through the job list and display them...
1298 */
1299
1300 if (response->request.status.status_code > IPP_OK_CONFLICT)
1301 {
1302 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
1303 ippDelete(response);
1304 return (1);
1305 }
1306
1307 rank = -1;
1308
1309 for (attr = response->attrs; attr != NULL; attr = attr->next)
1310 {
1311 /*
1312 * Skip leading attributes until we hit a job...
1313 */
1314
1315 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1316 attr = attr->next;
1317
1318 if (attr == NULL)
1319 break;
1320
1321 /*
1322 * Pull the needed attributes from this job...
1323 */
1324
1325 jobid = 0;
1326 size = 0;
1327 username = NULL;
1328 dest = NULL;
1329 jobtime = 0;
1330 title = "no title";
1331 message = NULL;
1332 reasons = NULL;
1333
1334 while (attr != NULL && attr->group_tag == IPP_TAG_JOB)
1335 {
1336 if (!strcmp(attr->name, "job-id") &&
1337 attr->value_tag == IPP_TAG_INTEGER)
1338 jobid = attr->values[0].integer;
1339 else if (!strcmp(attr->name, "job-k-octets") &&
1340 attr->value_tag == IPP_TAG_INTEGER)
1341 size = attr->values[0].integer;
1342 else if (!strcmp(attr->name, "time-at-creation") &&
1343 attr->value_tag == IPP_TAG_INTEGER)
1344 jobtime = attr->values[0].integer;
1345 else if (!strcmp(attr->name, "job-printer-state-message") &&
1346 attr->value_tag == IPP_TAG_TEXT)
1347 message = attr->values[0].string.text;
1348 else if (!strcmp(attr->name, "job-printer-uri") &&
1349 attr->value_tag == IPP_TAG_URI)
1350 {
1351 if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
1352 dest ++;
1353 }
1354 else if (!strcmp(attr->name, "job-originating-user-name") &&
1355 attr->value_tag == IPP_TAG_NAME)
1356 username = attr->values[0].string.text;
1357 else if (!strcmp(attr->name, "job-name") &&
1358 attr->value_tag == IPP_TAG_NAME)
1359 title = attr->values[0].string.text;
1360 else if (!strcmp(attr->name, "job-state-reasons") &&
1361 attr->value_tag == IPP_TAG_KEYWORD)
1362 reasons = attr;
1363
1364 attr = attr->next;
1365 }
1366
1367 /*
1368 * See if we have everything needed...
1369 */
1370
1371 if (dest == NULL || jobid == 0)
1372 {
1373 if (attr == NULL)
1374 break;
1375 else
1376 continue;
1377 }
1378
1379 /*
1380 * Display the job...
1381 */
1382
1383 rank ++;
1384
1385 if (match_list(dests, dest) && match_list(users, username))
1386 {
1387 jobdate = localtime(&jobtime);
1388 snprintf(temp, sizeof(temp), "%s-%d", dest, jobid);
1389
1390 if (long_status == 3)
1391 {
1392 /*
1393 * Show the consolidated output format for the SGI tools...
1394 */
1395
1396 if (!strftime(date, sizeof(date), "%b %d %H:%M", jobdate))
1397 strlcpy(date, "Unknown", sizeof(date));
1398
1399 _cupsLangPrintf(stdout, "%s;%s;%d;%s;%s",
1400 temp, username ? username : "unknown",
1401 size, title ? title : "unknown", date);
1402 }
1403 else
1404 {
1405 if (!strftime(date, sizeof(date), "%c", jobdate))
1406 strlcpy(date, "Unknown", sizeof(date));
1407
1408 if (ranking)
1409 _cupsLangPrintf(stdout, "%3d %-21s %-13s %8.0f %s",
1410 rank, temp, username ? username : "unknown",
1411 1024.0 * size, date);
1412 else
1413 _cupsLangPrintf(stdout, "%-23s %-13s %8.0f %s",
1414 temp, username ? username : "unknown",
1415 1024.0 * size, date);
1416 if (long_status)
1417 {
1418 if (message)
1419 _cupsLangPrintf(stdout, _("\tStatus: %s"), message);
1420
1421 if (reasons)
1422 {
1423 char alerts[1024], /* Alerts string */
1424 *aptr; /* Pointer into alerts string */
1425
1426 for (i = 0, aptr = alerts; i < reasons->num_values; i ++)
1427 {
1428 if (i)
1429 snprintf(aptr, sizeof(alerts) - (aptr - alerts), " %s",
1430 reasons->values[i].string.text);
1431 else
1432 strlcpy(alerts, reasons->values[i].string.text,
1433 sizeof(alerts));
1434
1435 aptr += strlen(aptr);
1436 }
1437
1438 _cupsLangPrintf(stdout, _("\tAlerts: %s"), alerts);
1439 }
1440
1441 _cupsLangPrintf(stdout, _("\tqueued for %s"), dest);
1442 }
1443 }
1444 }
1445
1446 if (attr == NULL)
1447 break;
1448 }
1449
1450 ippDelete(response);
1451 }
1452 else
1453 {
1454 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
1455 return (1);
1456 }
1457
1458 return (0);
1459 }
1460
1461
1462 /*
1463 * 'show_printers()' - Show printers.
1464 */
1465
1466 static int /* O - 0 on success, 1 on fail */
1467 show_printers(const char *printers, /* I - Destinations */
1468 int num_dests, /* I - Number of user-defined dests */
1469 cups_dest_t *dests, /* I - User-defined destinations */
1470 int long_status) /* I - Show long status? */
1471 {
1472 int i, j; /* Looping vars */
1473 ipp_t *request, /* IPP Request */
1474 *response, /* IPP Response */
1475 *jobs; /* IPP Get Jobs response */
1476 ipp_attribute_t *attr, /* Current attribute */
1477 *jobattr, /* Job ID attribute */
1478 *reasons; /* Job state reasons attribute */
1479 const char *printer, /* Printer name */
1480 *message, /* Printer state message */
1481 *description, /* Description of printer */
1482 *location, /* Location of printer */
1483 *make_model, /* Make and model of printer */
1484 *uri; /* URI of printer */
1485 ipp_attribute_t *allowed, /* requesting-user-name-allowed */
1486 *denied; /* requestint-user-name-denied */
1487 ipp_pstate_t pstate; /* Printer state */
1488 cups_ptype_t ptype; /* Printer type */
1489 time_t ptime; /* Printer state time */
1490 struct tm *pdate; /* Printer state date & time */
1491 int jobid; /* Job ID of current job */
1492 char printer_uri[HTTP_MAX_URI],
1493 /* Printer URI */
1494 printer_state_time[255];/* Printer state time */
1495 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1496 static const char *pattrs[] = /* Attributes we need for printers... */
1497 {
1498 "printer-name",
1499 "printer-state",
1500 "printer-state-message",
1501 "printer-state-reasons",
1502 "printer-state-change-time",
1503 "printer-type",
1504 "printer-info",
1505 "printer-location",
1506 "printer-make-and-model",
1507 "printer-uri-supported",
1508 "requesting-user-name-allowed",
1509 "requesting-user-name-denied"
1510 };
1511 static const char *jattrs[] = /* Attributes we need for jobs... */
1512 {
1513 "job-id",
1514 "job-state"
1515 };
1516
1517
1518 DEBUG_printf(("show_printers(printers=\"%s\", num_dests=%d, dests=%p, "
1519 "long_status=%d)\n", printers, num_dests, dests, long_status));
1520
1521 if (printers != NULL && !strcmp(printers, "all"))
1522 printers = NULL;
1523
1524 /*
1525 * Build a CUPS_GET_PRINTERS request, which requires the following
1526 * attributes:
1527 *
1528 * attributes-charset
1529 * attributes-natural-language
1530 * requested-attributes
1531 * requesting-user-name
1532 */
1533
1534 request = ippNewRequest(CUPS_GET_PRINTERS);
1535
1536 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1537 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
1538 NULL, pattrs);
1539
1540 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1541 NULL, cupsUser());
1542
1543 /*
1544 * Do the request and get back a response...
1545 */
1546
1547 if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
1548 {
1549 DEBUG_puts("show_printers: request succeeded...");
1550
1551 if (response->request.status.status_code > IPP_OK_CONFLICT)
1552 {
1553 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
1554 ippDelete(response);
1555 return (1);
1556 }
1557
1558 /*
1559 * Loop through the printers returned in the list and display
1560 * their status...
1561 */
1562
1563 for (attr = response->attrs; attr != NULL; attr = attr->next)
1564 {
1565 /*
1566 * Skip leading attributes until we hit a job...
1567 */
1568
1569 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1570 attr = attr->next;
1571
1572 if (attr == NULL)
1573 break;
1574
1575 /*
1576 * Pull the needed attributes from this job...
1577 */
1578
1579 printer = NULL;
1580 ptime = 0;
1581 ptype = CUPS_PRINTER_LOCAL;
1582 pstate = IPP_PRINTER_IDLE;
1583 message = NULL;
1584 description = NULL;
1585 location = NULL;
1586 make_model = NULL;
1587 reasons = NULL;
1588 uri = NULL;
1589 jobid = 0;
1590 allowed = NULL;
1591 denied = NULL;
1592
1593 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1594 {
1595 if (!strcmp(attr->name, "printer-name") &&
1596 attr->value_tag == IPP_TAG_NAME)
1597 printer = attr->values[0].string.text;
1598 else if (!strcmp(attr->name, "printer-state") &&
1599 attr->value_tag == IPP_TAG_ENUM)
1600 pstate = (ipp_pstate_t)attr->values[0].integer;
1601 else if (!strcmp(attr->name, "printer-type") &&
1602 attr->value_tag == IPP_TAG_ENUM)
1603 ptype = (cups_ptype_t)attr->values[0].integer;
1604 else if (!strcmp(attr->name, "printer-state-message") &&
1605 attr->value_tag == IPP_TAG_TEXT)
1606 message = attr->values[0].string.text;
1607 else if (!strcmp(attr->name, "printer-state-change-time") &&
1608 attr->value_tag == IPP_TAG_INTEGER)
1609 ptime = (time_t)attr->values[0].integer;
1610 else if (!strcmp(attr->name, "printer-info") &&
1611 attr->value_tag == IPP_TAG_TEXT)
1612 description = attr->values[0].string.text;
1613 else if (!strcmp(attr->name, "printer-location") &&
1614 attr->value_tag == IPP_TAG_TEXT)
1615 location = attr->values[0].string.text;
1616 else if (!strcmp(attr->name, "printer-make-and-model") &&
1617 attr->value_tag == IPP_TAG_TEXT)
1618 make_model = attr->values[0].string.text;
1619 else if (!strcmp(attr->name, "printer-uri-supported") &&
1620 attr->value_tag == IPP_TAG_URI)
1621 uri = attr->values[0].string.text;
1622 else if (!strcmp(attr->name, "printer-state-reasons") &&
1623 attr->value_tag == IPP_TAG_KEYWORD)
1624 reasons = attr;
1625 else if (!strcmp(attr->name, "requesting-user-name-allowed") &&
1626 attr->value_tag == IPP_TAG_NAME)
1627 allowed = attr;
1628 else if (!strcmp(attr->name, "requesting-user-name-denied") &&
1629 attr->value_tag == IPP_TAG_NAME)
1630 denied = attr;
1631
1632 attr = attr->next;
1633 }
1634
1635 /*
1636 * See if we have everything needed...
1637 */
1638
1639 if (printer == NULL)
1640 {
1641 if (attr == NULL)
1642 break;
1643 else
1644 continue;
1645 }
1646
1647 /*
1648 * Display the printer entry if needed...
1649 */
1650
1651 if (match_list(printers, printer))
1652 {
1653 /*
1654 * If the printer state is "IPP_PRINTER_PROCESSING", then grab the
1655 * current job for the printer.
1656 */
1657
1658 if (pstate == IPP_PRINTER_PROCESSING)
1659 {
1660 /*
1661 * Build an IPP_GET_JOBS request, which requires the following
1662 * attributes:
1663 *
1664 * attributes-charset
1665 * attributes-natural-language
1666 * printer-uri
1667 * limit
1668 * requested-attributes
1669 */
1670
1671 request = ippNewRequest(IPP_GET_JOBS);
1672
1673 request->request.op.operation_id = IPP_GET_JOBS;
1674 request->request.op.request_id = 1;
1675
1676 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1677 "requested-attributes",
1678 sizeof(jattrs) / sizeof(jattrs[0]), NULL, jattrs);
1679
1680 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
1681 "ipp", NULL, "localhost", 0, "/printers/%s", printer);
1682 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1683 "printer-uri", NULL, printer_uri);
1684
1685 if ((jobs = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
1686 {
1687 /*
1688 * Get the current active job on this queue...
1689 */
1690
1691 ipp_jstate_t jobstate = IPP_JOB_PENDING;
1692 jobid = 0;
1693
1694 for (jobattr = jobs->attrs; jobattr; jobattr = jobattr->next)
1695 {
1696 if (!jobattr->name)
1697 {
1698 if (jobstate == IPP_JOB_PROCESSING)
1699 break;
1700 else
1701 continue;
1702 }
1703
1704 if (!strcmp(jobattr->name, "job-id") &&
1705 jobattr->value_tag == IPP_TAG_INTEGER)
1706 jobid = jobattr->values[0].integer;
1707 else if (!strcmp(jobattr->name, "job-state") &&
1708 jobattr->value_tag == IPP_TAG_ENUM)
1709 jobstate = jobattr->values[0].integer;
1710 }
1711
1712 if (jobstate != IPP_JOB_PROCESSING)
1713 jobid = 0;
1714
1715 ippDelete(jobs);
1716 }
1717 }
1718
1719 /*
1720 * Display it...
1721 */
1722
1723 pdate = localtime(&ptime);
1724 strftime(printer_state_time, sizeof(printer_state_time), "%c", pdate);
1725
1726 switch (pstate)
1727 {
1728 case IPP_PRINTER_IDLE :
1729 _cupsLangPrintf(stdout,
1730 _("printer %s is idle. enabled since %s"),
1731 printer, printer_state_time);
1732 break;
1733 case IPP_PRINTER_PROCESSING :
1734 _cupsLangPrintf(stdout,
1735 _("printer %s now printing %s-%d. "
1736 "enabled since %s"),
1737 printer, printer, jobid, printer_state_time);
1738 break;
1739 case IPP_PRINTER_STOPPED :
1740 _cupsLangPrintf(stdout,
1741 _("printer %s disabled since %s -"),
1742 printer, printer_state_time);
1743 break;
1744 }
1745
1746 if ((message && *message) || pstate == IPP_PRINTER_STOPPED)
1747 {
1748 if (!message || !*message)
1749 _cupsLangPuts(stdout, _("\treason unknown"));
1750 else
1751 _cupsLangPrintf(stdout, "\t%s", message);
1752 }
1753
1754 if (long_status > 1)
1755 {
1756 _cupsLangPuts(stdout, _("\tForm mounted:"));
1757 _cupsLangPuts(stdout, _("\tContent types: any"));
1758 _cupsLangPuts(stdout, _("\tPrinter types: unknown"));
1759 }
1760
1761 if (long_status)
1762 {
1763 _cupsLangPrintf(stdout, _("\tDescription: %s"),
1764 description ? description : "");
1765
1766 if (reasons)
1767 {
1768 char alerts[1024], /* Alerts string */
1769 *aptr; /* Pointer into alerts string */
1770
1771 for (i = 0, aptr = alerts; i < reasons->num_values; i ++)
1772 {
1773 if (i)
1774 snprintf(aptr, sizeof(alerts) - (aptr - alerts), " %s",
1775 reasons->values[i].string.text);
1776 else
1777 strlcpy(alerts, reasons->values[i].string.text,
1778 sizeof(alerts));
1779
1780 aptr += strlen(aptr);
1781 }
1782
1783 _cupsLangPrintf(stdout, _("\tAlerts: %s"), alerts);
1784 }
1785 }
1786 if (long_status > 1)
1787 {
1788 _cupsLangPrintf(stdout, _("\tLocation: %s"),
1789 location ? location : "");
1790
1791 if (ptype & CUPS_PRINTER_REMOTE)
1792 {
1793 _cupsLangPuts(stdout, _("\tConnection: remote"));
1794
1795 if (make_model && !strstr(make_model, "System V Printer") &&
1796 !strstr(make_model, "Raw Printer") && uri)
1797 _cupsLangPrintf(stdout, _("\tInterface: %s.ppd"),
1798 uri);
1799 }
1800 else
1801 {
1802 _cupsLangPuts(stdout, _("\tConnection: direct"));
1803
1804 if (make_model && strstr(make_model, "System V Printer"))
1805 _cupsLangPrintf(stdout,
1806 _("\tInterface: %s/interfaces/%s"),
1807 cg->cups_serverroot, printer);
1808 else if (make_model && !strstr(make_model, "Raw Printer"))
1809 _cupsLangPrintf(stdout,
1810 _("\tInterface: %s/ppd/%s.ppd"),
1811 cg->cups_serverroot, printer);
1812 }
1813 _cupsLangPuts(stdout, _("\tOn fault: no alert"));
1814 _cupsLangPuts(stdout, _("\tAfter fault: continue"));
1815 /* TODO update to use printer-error-policy */
1816 if (allowed)
1817 {
1818 _cupsLangPuts(stdout, _("\tUsers allowed:"));
1819 for (j = 0; j < allowed->num_values; j ++)
1820 _cupsLangPrintf(stdout, "\t\t%s",
1821 allowed->values[j].string.text);
1822 }
1823 else if (denied)
1824 {
1825 _cupsLangPuts(stdout, _("\tUsers denied:"));
1826 for (j = 0; j < denied->num_values; j ++)
1827 _cupsLangPrintf(stdout, "\t\t%s",
1828 denied->values[j].string.text);
1829 }
1830 else
1831 {
1832 _cupsLangPuts(stdout, _("\tUsers allowed:"));
1833 _cupsLangPuts(stdout, _("\t\t(all)"));
1834 }
1835 _cupsLangPuts(stdout, _("\tForms allowed:"));
1836 _cupsLangPuts(stdout, _("\t\t(none)"));
1837 _cupsLangPuts(stdout, _("\tBanner required"));
1838 _cupsLangPuts(stdout, _("\tCharset sets:"));
1839 _cupsLangPuts(stdout, _("\t\t(none)"));
1840 _cupsLangPuts(stdout, _("\tDefault pitch:"));
1841 _cupsLangPuts(stdout, _("\tDefault page size:"));
1842 _cupsLangPuts(stdout, _("\tDefault port settings:"));
1843 }
1844
1845 for (i = 0; i < num_dests; i ++)
1846 if (!_cups_strcasecmp(printer, dests[i].name) && dests[i].instance)
1847 {
1848 switch (pstate)
1849 {
1850 case IPP_PRINTER_IDLE :
1851 _cupsLangPrintf(stdout,
1852 _("printer %s/%s is idle. "
1853 "enabled since %s"),
1854 printer, dests[i].instance,
1855 printer_state_time);
1856 break;
1857 case IPP_PRINTER_PROCESSING :
1858 _cupsLangPrintf(stdout,
1859 _("printer %s/%s now printing %s-%d. "
1860 "enabled since %s"),
1861 printer, dests[i].instance, printer, jobid,
1862 printer_state_time);
1863 break;
1864 case IPP_PRINTER_STOPPED :
1865 _cupsLangPrintf(stdout,
1866 _("printer %s/%s disabled since %s -"),
1867 printer, dests[i].instance,
1868 printer_state_time);
1869 break;
1870 }
1871
1872 if ((message && *message) || pstate == IPP_PRINTER_STOPPED)
1873 {
1874 if (!message || !*message)
1875 _cupsLangPuts(stdout, _("\treason unknown"));
1876 else
1877 _cupsLangPrintf(stdout, "\t%s", message);
1878 }
1879
1880 if (long_status > 1)
1881 {
1882 _cupsLangPuts(stdout, _("\tForm mounted:"));
1883 _cupsLangPuts(stdout, _("\tContent types: any"));
1884 _cupsLangPuts(stdout, _("\tPrinter types: unknown"));
1885 }
1886
1887 if (long_status)
1888 {
1889 _cupsLangPrintf(stdout, _("\tDescription: %s"),
1890 description ? description : "");
1891
1892 if (reasons)
1893 {
1894 char alerts[1024], /* Alerts string */
1895 *aptr; /* Pointer into alerts string */
1896
1897 for (i = 0, aptr = alerts; i < reasons->num_values; i ++)
1898 {
1899 if (i)
1900 snprintf(aptr, sizeof(alerts) - (aptr - alerts), " %s",
1901 reasons->values[i].string.text);
1902 else
1903 strlcpy(alerts, reasons->values[i].string.text,
1904 sizeof(alerts));
1905
1906 aptr += strlen(aptr);
1907 }
1908
1909 _cupsLangPrintf(stdout, _("\tAlerts: %s"), alerts);
1910 }
1911 }
1912 if (long_status > 1)
1913 {
1914 _cupsLangPrintf(stdout, _("\tLocation: %s"),
1915 location ? location : "");
1916
1917 if (ptype & CUPS_PRINTER_REMOTE)
1918 {
1919 _cupsLangPuts(stdout, _("\tConnection: remote"));
1920
1921 if (make_model && !strstr(make_model, "System V Printer") &&
1922 !strstr(make_model, "Raw Printer") && uri)
1923 _cupsLangPrintf(stdout, _("\tInterface: %s.ppd"), uri);
1924 }
1925 else
1926 {
1927 _cupsLangPuts(stdout, _("\tConnection: direct"));
1928
1929 if (make_model && strstr(make_model, "System V Printer"))
1930 _cupsLangPrintf(stdout,
1931 _("\tInterface: %s/interfaces/%s"),
1932 cg->cups_serverroot, printer);
1933 else if (make_model && !strstr(make_model, "Raw Printer"))
1934 _cupsLangPrintf(stdout,
1935 _("\tInterface: %s/ppd/%s.ppd"),
1936 cg->cups_serverroot, printer);
1937 }
1938 _cupsLangPuts(stdout, _("\tOn fault: no alert"));
1939 _cupsLangPuts(stdout, _("\tAfter fault: continue"));
1940 /* TODO update to use printer-error-policy */
1941 if (allowed)
1942 {
1943 _cupsLangPuts(stdout, _("\tUsers allowed:"));
1944 for (j = 0; j < allowed->num_values; j ++)
1945 _cupsLangPrintf(stdout, "\t\t%s",
1946 allowed->values[j].string.text);
1947 }
1948 else if (denied)
1949 {
1950 _cupsLangPuts(stdout, _("\tUsers denied:"));
1951 for (j = 0; j < denied->num_values; j ++)
1952 _cupsLangPrintf(stdout, "\t\t%s",
1953 denied->values[j].string.text);
1954 }
1955 else
1956 {
1957 _cupsLangPuts(stdout, _("\tUsers allowed:"));
1958 _cupsLangPuts(stdout, _("\t\t(all)"));
1959 }
1960 _cupsLangPuts(stdout, _("\tForms allowed:"));
1961 _cupsLangPuts(stdout, _("\t\t(none)"));
1962 _cupsLangPuts(stdout, _("\tBanner required"));
1963 _cupsLangPuts(stdout, _("\tCharset sets:"));
1964 _cupsLangPuts(stdout, _("\t\t(none)"));
1965 _cupsLangPuts(stdout, _("\tDefault pitch:"));
1966 _cupsLangPuts(stdout, _("\tDefault page size:"));
1967 _cupsLangPuts(stdout, _("\tDefault port settings:"));
1968 }
1969 }
1970 }
1971
1972 if (attr == NULL)
1973 break;
1974 }
1975
1976 ippDelete(response);
1977 }
1978 else
1979 {
1980 _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString());
1981 return (1);
1982 }
1983
1984 return (0);
1985 }
1986
1987
1988 /*
1989 * 'show_scheduler()' - Show scheduler status.
1990 */
1991
1992 static void
1993 show_scheduler(void)
1994 {
1995 http_t *http; /* Connection to server */
1996
1997
1998 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
1999 cupsEncryption())) != NULL)
2000 {
2001 _cupsLangPuts(stdout, _("scheduler is running"));
2002 httpClose(http);
2003 }
2004 else
2005 _cupsLangPuts(stdout, _("scheduler is not running"));
2006 }
2007
2008
2009 /*
2010 * End of "$Id$".
2011 */