]> git.ipfire.org Git - thirdparty/cups.git/blame - berkeley/lpq.c
Merge changes from CUPS 1.5svn-r9641
[thirdparty/cups.git] / berkeley / lpq.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: lpq.c 7460 2008-04-16 02:19:54Z mike $"
ef416fc2 3 *
71e16022 4 * "lpq" command for CUPS.
ef416fc2 5 *
84315f46 6 * Copyright 2007-2011 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 commands.
18 * show_jobs() - Show jobs.
19 * show_printer() - Show printer status.
20 * usage() - Show program usage.
21 */
22
23/*
24 * Include necessary headers...
25 */
26
27/*
28 * Include necessary headers...
29 */
30
71e16022 31#include <cups/cups-private.h>
ef416fc2 32
33
34/*
35 * Local functions...
36 */
37
d6ae789d 38static http_t *connect_server(const char *, http_t *);
fa73b229 39static int show_jobs(const char *, http_t *, const char *,
40 const char *, const int, const int);
41static void show_printer(const char *, http_t *, const char *);
ef416fc2 42static void usage(void);
43
44
45/*
46 * 'main()' - Parse options and commands.
47 */
48
49int
fa73b229 50main(int argc, /* I - Number of command-line arguments */
51 char *argv[]) /* I - Command-line arguments */
ef416fc2 52{
fa73b229 53 int i; /* Looping var */
54 http_t *http; /* Connection to server */
55 const char *dest, /* Desired printer */
56 *user, /* Desired user */
57 *val; /* Environment variable name */
58 char *instance; /* Printer instance */
59 int id, /* Desired job ID */
60 all, /* All printers */
61 interval, /* Reporting interval */
62 longstatus; /* Show file details */
63 int num_dests; /* Number of destinations */
64 cups_dest_t *dests; /* Destinations */
ef416fc2 65
66
07725fee 67 _cupsSetLocale(argv);
d09495fa 68
ef416fc2 69 /*
70 * Check for command-line options...
71 */
72
d6ae789d 73 http = NULL;
ef416fc2 74 dest = NULL;
75 user = NULL;
76 id = 0;
77 interval = 0;
78 longstatus = 0;
79 all = 0;
d6ae789d 80 num_dests = 0;
d09495fa 81 dests = NULL;
ef416fc2 82
83 for (i = 1; i < argc; i ++)
84 if (argv[i][0] == '+')
85 interval = atoi(argv[i] + 1);
86 else if (argv[i][0] == '-')
87 {
88 switch (argv[i][1])
89 {
90 case 'E' : /* Encrypt */
91#ifdef HAVE_SSL
d6ae789d 92 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
ef416fc2 93
94 if (http)
d6ae789d 95 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
ef416fc2 96#else
0837b7e8 97 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."),
ef416fc2 98 argv[0]);
99#endif /* HAVE_SSL */
100 break;
101
fa73b229 102 case 'U' : /* Username */
103 if (argv[i][2] != '\0')
104 cupsSetUser(argv[i] + 2);
105 else
106 {
107 i ++;
108 if (i >= argc)
109 {
110 _cupsLangPrintf(stderr,
111 _("%s: Error - expected username after "
0837b7e8 112 "\"-U\" option."), argv[0]);
fa73b229 113 return (1);
114 }
115
116 cupsSetUser(argv[i]);
117 }
118 break;
119
ef416fc2 120 case 'P' : /* Printer */
121 if (argv[i][2])
122 dest = argv[i] + 2;
123 else
124 {
125 i ++;
126
127 if (i >= argc)
128 {
129 httpClose(http);
130 cupsFreeDests(num_dests, dests);
131
132 usage();
133 }
134
135 dest = argv[i];
136 }
137
138 if ((instance = strchr(dest, '/')) != NULL)
139 *instance++ = '\0';
140
d6ae789d 141 http = connect_server(argv[0], http);
142
143 if (num_dests == 0)
144 num_dests = cupsGetDests2(http, &dests);
145
ef416fc2 146 if (cupsGetDest(dest, instance, num_dests, dests) == NULL)
147 {
148 if (instance)
fa73b229 149 _cupsLangPrintf(stderr,
0837b7e8 150 _("%s: Error - unknown destination \"%s/%s\"."),
fa73b229 151 argv[0], dest, instance);
ef416fc2 152 else
0837b7e8 153 _cupsLangPrintf(stderr, _("%s: Unknown destination \"%s\"."),
fa73b229 154 argv[0], dest);
ef416fc2 155
156 return (1);
157 }
158 break;
159
160 case 'a' : /* All printers */
161 all = 1;
162 break;
163
fa73b229 164 case 'h' : /* Connect to host */
d6ae789d 165 if (http)
166 {
fa73b229 167 httpClose(http);
d6ae789d 168 http = NULL;
169 }
fa73b229 170
171 if (argv[i][2] != '\0')
172 cupsSetServer(argv[i] + 2);
173 else
174 {
175 i ++;
176
177 if (i >= argc)
178 {
179 _cupsLangPrintf(stderr,
180 _("%s: Error - expected hostname after "
0837b7e8 181 "\"-h\" option."), argv[0]);
fa73b229 182 return (1);
183 }
184 else
185 cupsSetServer(argv[i]);
186 }
187 break;
188
ef416fc2 189 case 'l' : /* Long status */
190 longstatus = 1;
191 break;
192
193 default :
194 httpClose(http);
195 cupsFreeDests(num_dests, dests);
196
197 usage();
198 break;
199 }
200 }
201 else if (isdigit(argv[i][0] & 255))
202 id = atoi(argv[i]);
203 else
204 user = argv[i];
205
d6ae789d 206 http = connect_server(argv[0], http);
207
ef416fc2 208 if (dest == NULL && !all)
209 {
d6ae789d 210 if (num_dests == 0)
211 num_dests = cupsGetDests2(http, &dests);
212
ef416fc2 213 for (i = 0; i < num_dests; i ++)
214 if (dests[i].is_default)
215 dest = dests[i].name;
216
217 if (dest == NULL)
218 {
219 val = NULL;
220
221 if ((dest = getenv("LPDEST")) == NULL)
222 {
223 if ((dest = getenv("PRINTER")) != NULL)
224 {
225 if (!strcmp(dest, "lp"))
226 dest = NULL;
227 else
228 val = "PRINTER";
229 }
230 }
231 else
232 val = "LPDEST";
233
234 if (dest && !cupsGetDest(dest, NULL, num_dests, dests))
fa73b229 235 _cupsLangPrintf(stderr,
84315f46 236 _("%s: Error - %s environment variable names "
0837b7e8
MS
237 "non-existent destination \"%s\"."), argv[0], val,
238 dest);
ef416fc2 239 else
fa73b229 240 _cupsLangPrintf(stderr,
84315f46 241 _("%s: Error - no default destination available."),
fa73b229 242 argv[0]);
ef416fc2 243 httpClose(http);
244 cupsFreeDests(num_dests, dests);
245 return (1);
246 }
247 }
248
249 /*
250 * Show the status in a loop...
251 */
252
253 for (;;)
254 {
255 if (dest)
fa73b229 256 show_printer(argv[0], http, dest);
ef416fc2 257
fa73b229 258 i = show_jobs(argv[0], http, dest, user, id, longstatus);
ef416fc2 259
260 if (i && interval)
261 {
262 fflush(stdout);
263 sleep(interval);
264 }
265 else
266 break;
267 }
268
269 /*
270 * Close the connection to the server and return...
271 */
272
273 cupsFreeDests(num_dests, dests);
274 httpClose(http);
275
276 return (0);
277}
278
279
d6ae789d 280/*
281 * 'connect_server()' - Connect to the server as necessary...
282 */
283
284static http_t * /* O - New HTTP connection */
285connect_server(const char *command, /* I - Command name */
286 http_t *http) /* I - Current HTTP connection */
287{
288 if (!http)
289 {
290 http = httpConnectEncrypt(cupsServer(), ippPort(),
291 cupsEncryption());
292
293 if (http == NULL)
294 {
0837b7e8 295 _cupsLangPrintf(stderr, _("%s: Unable to connect to server."), command);
d6ae789d 296 exit(1);
297 }
298 }
299
300 return (http);
301}
302
303
ef416fc2 304/*
305 * 'show_jobs()' - Show jobs.
306 */
307
fa73b229 308static int /* O - Number of jobs in queue */
309show_jobs(const char *command, /* I - Command name */
310 http_t *http, /* I - HTTP connection to server */
311 const char *dest, /* I - Destination */
312 const char *user, /* I - User */
313 const int id, /* I - Job ID */
314 const int longstatus) /* I - 1 if long report desired */
ef416fc2 315{
fa73b229 316 ipp_t *request, /* IPP Request */
317 *response; /* IPP Response */
318 ipp_attribute_t *attr; /* Current attribute */
319 const char *jobdest, /* Pointer into job-printer-uri */
320 *jobuser, /* Pointer to job-originating-user-name */
321 *jobname; /* Pointer to job-name */
322 ipp_jstate_t jobstate; /* job-state */
323 int jobid, /* job-id */
324 jobsize, /* job-k-octets */
ef416fc2 325#ifdef __osf__
fa73b229 326 jobpriority, /* job-priority */
ef416fc2 327#endif /* __osf__ */
fa73b229 328 jobcount, /* Number of jobs */
329 jobcopies, /* Number of copies */
330 rank; /* Rank of job */
331 char resource[1024]; /* Resource string */
332 char rankstr[255]; /* Rank string */
333 char namestr[1024]; /* Job name string */
5a662dc0
MS
334 static const char * const jobattrs[] =/* Job attributes we want to see */
335 {
336 "copies",
337 "job-id",
338 "job-k-octets",
339 "job-name",
340 "job-originating-user-name",
341 "job-printer-uri",
342 "job-priority",
343 "job-state"
344 };
345 static const char * const ranks[10] = /* Ranking strings */
ef416fc2 346 {
347 "th",
348 "st",
349 "nd",
350 "rd",
351 "th",
352 "th",
353 "th",
354 "th",
355 "th",
356 "th"
357 };
358
359
ae71f5de
MS
360 DEBUG_printf(("show_jobs(http=%p, dest=%p, user=%p, id=%d, longstatus%d)\n",
361 http, dest, user, id, longstatus));
ef416fc2 362
363 if (http == NULL)
364 return (0);
365
366 /*
367 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
368 * the following attributes:
369 *
370 * attributes-charset
371 * attributes-natural-language
372 * job-uri or printer-uri
5a662dc0 373 * requested-attributes
ef416fc2 374 */
375
fa73b229 376 request = ippNewRequest(id ? IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS);
ef416fc2 377
1340db2d 378 if (id)
ef416fc2 379 {
1340db2d 380 snprintf(resource, sizeof(resource), "ipp://localhost/jobs/%d", id);
ef416fc2 381 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
382 NULL, resource);
383 }
1340db2d 384 else if (dest)
ef416fc2 385 {
a4d04587 386 httpAssembleURIf(HTTP_URI_CODING_ALL, resource, sizeof(resource), "ipp",
387 NULL, "localhost", 0, "/printers/%s", dest);
ef416fc2 388
389 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
390 NULL, resource);
391 }
1340db2d
MS
392 else
393 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
394 NULL, "ipp://localhost/");
ef416fc2 395
396 if (user)
397 {
398 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
399 "requesting-user-name", NULL, user);
400 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
401 }
402
5a662dc0
MS
403 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
404 "requested-attributes",
405 (int)(sizeof(jobattrs) / sizeof(jobattrs[0])), NULL, jobattrs);
406
ef416fc2 407 /*
408 * Do the request and get back a response...
409 */
410
411 jobcount = 0;
412
413 if ((response = cupsDoRequest(http, request, "/")) != NULL)
414 {
415 if (response->request.status.status_code > IPP_OK_CONFLICT)
416 {
0837b7e8 417 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
ef416fc2 418 ippDelete(response);
419 return (0);
420 }
421
422 rank = 1;
423
424 /*
425 * Loop through the job list and display them...
426 */
427
428 for (attr = response->attrs; attr != NULL; attr = attr->next)
429 {
430 /*
431 * Skip leading attributes until we hit a job...
432 */
433
434 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
435 attr = attr->next;
436
437 if (attr == NULL)
438 break;
439
440 /*
441 * Pull the needed attributes from this job...
442 */
443
444 jobid = 0;
445 jobsize = 0;
446#ifdef __osf__
447 jobpriority = 50;
448#endif /* __osf__ */
449 jobstate = IPP_JOB_PENDING;
450 jobname = "untitled";
451 jobuser = NULL;
452 jobdest = NULL;
453 jobcopies = 1;
454
455 while (attr != NULL && attr->group_tag == IPP_TAG_JOB)
456 {
fa73b229 457 if (!strcmp(attr->name, "job-id") &&
ef416fc2 458 attr->value_tag == IPP_TAG_INTEGER)
459 jobid = attr->values[0].integer;
460
fa73b229 461 if (!strcmp(attr->name, "job-k-octets") &&
ef416fc2 462 attr->value_tag == IPP_TAG_INTEGER)
463 jobsize = attr->values[0].integer;
464
465#ifdef __osf__
fa73b229 466 if (!strcmp(attr->name, "job-priority") &&
ef416fc2 467 attr->value_tag == IPP_TAG_INTEGER)
468 jobpriority = attr->values[0].integer;
469#endif /* __osf__ */
470
fa73b229 471 if (!strcmp(attr->name, "job-state") &&
ef416fc2 472 attr->value_tag == IPP_TAG_ENUM)
473 jobstate = (ipp_jstate_t)attr->values[0].integer;
474
fa73b229 475 if (!strcmp(attr->name, "job-printer-uri") &&
ef416fc2 476 attr->value_tag == IPP_TAG_URI)
477 if ((jobdest = strrchr(attr->values[0].string.text, '/')) != NULL)
478 jobdest ++;
479
fa73b229 480 if (!strcmp(attr->name, "job-originating-user-name") &&
ef416fc2 481 attr->value_tag == IPP_TAG_NAME)
482 jobuser = attr->values[0].string.text;
483
fa73b229 484 if (!strcmp(attr->name, "job-name") &&
ef416fc2 485 attr->value_tag == IPP_TAG_NAME)
486 jobname = attr->values[0].string.text;
487
fa73b229 488 if (!strcmp(attr->name, "copies") &&
ef416fc2 489 attr->value_tag == IPP_TAG_INTEGER)
490 jobcopies = attr->values[0].integer;
491
492 attr = attr->next;
493 }
494
495 /*
496 * See if we have everything needed...
497 */
498
499 if (jobdest == NULL || jobid == 0)
500 {
501 if (attr == NULL)
502 break;
503 else
504 continue;
505 }
506
507 if (!longstatus && jobcount == 0)
508#ifdef __osf__
fa73b229 509 _cupsLangPuts(stdout,
ef416fc2 510 _("Rank Owner Pri Job Files"
0837b7e8 511 " Total Size"));
ef416fc2 512#else
fa73b229 513 _cupsLangPuts(stdout,
ef416fc2 514 _("Rank Owner Job File(s)"
0837b7e8 515 " Total Size"));
ef416fc2 516#endif /* __osf__ */
517
518 jobcount ++;
519
520 /*
521 * Display the job...
522 */
523
524 if (jobstate == IPP_JOB_PROCESSING)
525 strcpy(rankstr, "active");
526 else
527 {
528 /*
529 * Make the rank show the "correct" suffix for each number
530 * (11-13 are the only special cases, for English anyways...)
531 */
532
533 if ((rank % 100) >= 11 && (rank % 100) <= 13)
534 snprintf(rankstr, sizeof(rankstr), "%dth", rank);
535 else
536 snprintf(rankstr, sizeof(rankstr), "%d%s", rank, ranks[rank % 10]);
537
538 rank ++;
539 }
540
541 if (longstatus)
542 {
fa73b229 543 _cupsLangPuts(stdout, "\n");
ef416fc2 544
545 if (jobcopies > 1)
546 snprintf(namestr, sizeof(namestr), "%d copies of %s", jobcopies,
547 jobname);
548 else
549 strlcpy(namestr, jobname, sizeof(namestr));
550
0837b7e8 551 _cupsLangPrintf(stdout, _("%s: %-33.33s [job %d localhost]"),
ef416fc2 552 jobuser, rankstr, jobid);
0837b7e8 553 _cupsLangPrintf(stdout, _(" %-39.39s %.0f bytes"),
ef416fc2 554 namestr, 1024.0 * jobsize);
555 }
556 else
557#ifdef __osf__
fa73b229 558 _cupsLangPrintf(stdout,
0837b7e8 559 _("%-6s %-10.10s %-4d %-10d %-27.27s %.0f bytes"),
ef416fc2 560 rankstr, jobuser, jobpriority, jobid, jobname,
561 1024.0 * jobsize);
562#else
fa73b229 563 _cupsLangPrintf(stdout,
0837b7e8 564 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes"),
ef416fc2 565 rankstr, jobuser, jobid, jobname, 1024.0 * jobsize);
566#endif /* __osf */
567
568 if (attr == NULL)
569 break;
570 }
571
572 ippDelete(response);
573 }
574 else
575 {
0837b7e8 576 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
ef416fc2 577 return (0);
578 }
579
580 if (jobcount == 0)
0837b7e8 581 _cupsLangPuts(stdout, _("no entries"));
ef416fc2 582
583 return (jobcount);
584}
585
586
587/*
588 * 'show_printer()' - Show printer status.
589 */
590
591static void
fa73b229 592show_printer(const char *command, /* I - Command name */
593 http_t *http, /* I - HTTP connection to server */
594 const char *dest) /* I - Destination */
ef416fc2 595{
fa73b229 596 ipp_t *request, /* IPP Request */
597 *response; /* IPP Response */
598 ipp_attribute_t *attr; /* Current attribute */
599 ipp_pstate_t state; /* Printer state */
600 char uri[HTTP_MAX_URI]; /* Printer URI */
ef416fc2 601
602
603 if (http == NULL)
604 return;
605
606 /*
607 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
608 * attributes:
609 *
610 * attributes-charset
611 * attributes-natural-language
612 * printer-uri
613 */
614
fa73b229 615 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
ef416fc2 616
a4d04587 617 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
618 "localhost", 0, "/printers/%s", dest);
ef416fc2 619 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
620 "printer-uri", NULL, uri);
621
622 /*
623 * Do the request and get back a response...
624 */
625
626 if ((response = cupsDoRequest(http, request, "/")) != NULL)
627 {
628 if (response->request.status.status_code > IPP_OK_CONFLICT)
629 {
0837b7e8 630 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
ef416fc2 631 ippDelete(response);
632 return;
633 }
634
635 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL)
636 state = (ipp_pstate_t)attr->values[0].integer;
637 else
638 state = IPP_PRINTER_STOPPED;
639
640 switch (state)
641 {
642 case IPP_PRINTER_IDLE :
0837b7e8 643 _cupsLangPrintf(stdout, _("%s is ready"), dest);
ef416fc2 644 break;
645 case IPP_PRINTER_PROCESSING :
0837b7e8 646 _cupsLangPrintf(stdout, _("%s is ready and printing"),
ef416fc2 647 dest);
648 break;
649 case IPP_PRINTER_STOPPED :
0837b7e8 650 _cupsLangPrintf(stdout, _("%s is not ready"), dest);
ef416fc2 651 break;
652 }
653
654 ippDelete(response);
655 }
656 else
0837b7e8 657 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
ef416fc2 658}
659
660
661/*
662 * 'usage()' - Show program usage.
663 */
664
665static void
666usage(void)
667{
fa73b229 668 _cupsLangPuts(stderr,
669 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
0837b7e8 670 "[-l] [+interval]"));
ef416fc2 671 exit(1);
672}
673
674
675/*
75bd9771 676 * End of "$Id: lpq.c 7460 2008-04-16 02:19:54Z mike $".
ef416fc2 677 */