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