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