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