4 * "lpq" command for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
17 * main() - Parse options and commands.
18 * show_jobs() - Show jobs.
19 * show_printer() - Show printer status.
20 * usage() - Show program usage.
24 * Include necessary headers...
28 * Include necessary headers...
31 #include <cups/cups-private.h>
38 static http_t
*connect_server(const char *, http_t
*);
39 static int show_jobs(const char *, http_t
*, const char *,
40 const char *, const int, const int);
41 static void show_printer(const char *, http_t
*, const char *);
42 static void usage(void) __attribute__((noreturn
));
46 * 'main()' - Parse options and commands.
50 main(int argc
, /* I - Number of command-line arguments */
51 char *argv
[]) /* I - Command-line arguments */
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 cups_dest_t
*named_dest
; /* Named destination */
69 * Check for command-line options...
80 for (i
= 1; i
< argc
; i
++)
81 if (argv
[i
][0] == '+')
82 interval
= atoi(argv
[i
] + 1);
83 else if (argv
[i
][0] == '-')
87 case 'E' : /* Encrypt */
89 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
92 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
94 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
99 case 'U' : /* Username */
100 if (argv
[i
][2] != '\0')
101 cupsSetUser(argv
[i
] + 2);
107 _cupsLangPrintf(stderr
,
108 _("%s: Error - expected username after "
109 "\"-U\" option."), argv
[0]);
113 cupsSetUser(argv
[i
]);
117 case 'P' : /* Printer */
134 if ((instance
= strchr(dest
, '/')) != NULL
)
137 http
= connect_server(argv
[0], http
);
139 if ((named_dest
= cupsGetNamedDest(http
, dest
, instance
)) == NULL
)
141 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
142 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
143 _cupsLangPrintf(stderr
,
144 _("%s: Error - add '/version=1.1' to server "
147 _cupsLangPrintf(stderr
,
148 _("%s: Error - unknown destination \"%s/%s\"."),
149 argv
[0], dest
, instance
);
151 _cupsLangPrintf(stderr
, _("%s: Unknown destination \"%s\"."),
157 cupsFreeDests(1, named_dest
);
160 case 'a' : /* All printers */
164 case 'h' : /* Connect to host */
171 if (argv
[i
][2] != '\0')
172 cupsSetServer(argv
[i
] + 2);
179 _cupsLangPrintf(stderr
,
180 _("%s: Error - expected hostname after "
181 "\"-h\" option."), argv
[0]);
185 cupsSetServer(argv
[i
]);
189 case 'l' : /* Long status */
200 else if (isdigit(argv
[i
][0] & 255))
205 http
= connect_server(argv
[0], http
);
207 if (dest
== NULL
&& !all
)
209 if ((named_dest
= cupsGetNamedDest(http
, NULL
, NULL
)) == NULL
)
211 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
212 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
214 _cupsLangPrintf(stderr
,
215 _("%s: Error - add '/version=1.1' to server name."),
222 if ((dest
= getenv("LPDEST")) == NULL
)
224 if ((dest
= getenv("PRINTER")) != NULL
)
226 if (!strcmp(dest
, "lp"))
236 _cupsLangPrintf(stderr
,
237 _("%s: Error - %s environment variable names "
238 "non-existent destination \"%s\"."), argv
[0], val
,
241 _cupsLangPrintf(stderr
,
242 _("%s: Error - no default destination available."),
248 dest
= named_dest
->name
;
252 * Show the status in a loop...
258 show_printer(argv
[0], http
, dest
);
260 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
272 * Close the connection to the server and return...
282 * 'connect_server()' - Connect to the server as necessary...
285 static http_t
* /* O - New HTTP connection */
286 connect_server(const char *command
, /* I - Command name */
287 http_t
*http
) /* I - Current HTTP connection */
291 http
= httpConnectEncrypt(cupsServer(), ippPort(),
296 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."), command
);
306 * 'show_jobs()' - Show jobs.
309 static int /* O - Number of jobs in queue */
310 show_jobs(const char *command
, /* I - Command name */
311 http_t
*http
, /* I - HTTP connection to server */
312 const char *dest
, /* I - Destination */
313 const char *user
, /* I - User */
314 const int id
, /* I - Job ID */
315 const int longstatus
) /* I - 1 if long report desired */
317 ipp_t
*request
, /* IPP Request */
318 *response
; /* IPP Response */
319 ipp_attribute_t
*attr
; /* Current attribute */
320 const char *jobdest
, /* Pointer into job-printer-uri */
321 *jobuser
, /* Pointer to job-originating-user-name */
322 *jobname
; /* Pointer to job-name */
323 ipp_jstate_t jobstate
; /* job-state */
324 int jobid
, /* job-id */
325 jobsize
, /* job-k-octets */
327 jobpriority
, /* job-priority */
329 jobcount
, /* Number of jobs */
330 jobcopies
, /* Number of copies */
331 rank
; /* Rank of job */
332 char resource
[1024]; /* Resource string */
333 char rankstr
[255]; /* Rank string */
334 char namestr
[1024]; /* Job name string */
335 static const char * const jobattrs
[] =/* Job attributes we want to see */
341 "job-originating-user-name",
346 static const char * const ranks
[10] = /* Ranking strings */
361 DEBUG_printf(("show_jobs(http=%p, dest=%p, user=%p, id=%d, longstatus%d)\n",
362 http
, dest
, user
, id
, longstatus
));
368 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
369 * the following attributes:
372 * attributes-natural-language
373 * job-uri or printer-uri
374 * requested-attributes
375 * requesting-user-name
378 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
382 snprintf(resource
, sizeof(resource
), "ipp://localhost/jobs/%d", id
);
383 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
388 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
389 NULL
, "localhost", 0, "/printers/%s", dest
);
391 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
395 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
396 NULL
, "ipp://localhost/");
400 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
401 "requesting-user-name", NULL
, user
);
402 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
405 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
406 "requesting-user-name", NULL
, cupsUser());
408 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
409 "requested-attributes",
410 (int)(sizeof(jobattrs
) / sizeof(jobattrs
[0])), NULL
, jobattrs
);
413 * Do the request and get back a response...
418 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
420 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
422 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
430 * Loop through the job list and display them...
433 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
436 * Skip leading attributes until we hit a job...
439 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
446 * Pull the needed attributes from this job...
454 jobstate
= IPP_JOB_PENDING
;
460 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
462 if (!strcmp(attr
->name
, "job-id") &&
463 attr
->value_tag
== IPP_TAG_INTEGER
)
464 jobid
= attr
->values
[0].integer
;
466 if (!strcmp(attr
->name
, "job-k-octets") &&
467 attr
->value_tag
== IPP_TAG_INTEGER
)
468 jobsize
= attr
->values
[0].integer
;
471 if (!strcmp(attr
->name
, "job-priority") &&
472 attr
->value_tag
== IPP_TAG_INTEGER
)
473 jobpriority
= attr
->values
[0].integer
;
476 if (!strcmp(attr
->name
, "job-state") &&
477 attr
->value_tag
== IPP_TAG_ENUM
)
478 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
480 if (!strcmp(attr
->name
, "job-printer-uri") &&
481 attr
->value_tag
== IPP_TAG_URI
)
482 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
485 if (!strcmp(attr
->name
, "job-originating-user-name") &&
486 attr
->value_tag
== IPP_TAG_NAME
)
487 jobuser
= attr
->values
[0].string
.text
;
489 if (!strcmp(attr
->name
, "job-name") &&
490 attr
->value_tag
== IPP_TAG_NAME
)
491 jobname
= attr
->values
[0].string
.text
;
493 if (!strcmp(attr
->name
, "copies") &&
494 attr
->value_tag
== IPP_TAG_INTEGER
)
495 jobcopies
= attr
->values
[0].integer
;
501 * See if we have everything needed...
504 if (jobdest
== NULL
|| jobid
== 0)
512 if (!longstatus
&& jobcount
== 0)
514 _cupsLangPuts(stdout
,
515 /* TRANSLATORS: Pri is job priority. */
516 _("Rank Owner Pri Job Files"
519 _cupsLangPuts(stdout
,
520 _("Rank Owner Job File(s)"
530 if (jobstate
== IPP_JOB_PROCESSING
)
531 strlcpy(rankstr
, "active", sizeof(rankstr
));
535 * Make the rank show the "correct" suffix for each number
536 * (11-13 are the only special cases, for English anyways...)
539 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
540 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
542 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
549 _cupsLangPuts(stdout
, "\n");
552 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
555 strlcpy(namestr
, jobname
, sizeof(namestr
));
557 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]"),
558 jobuser
, rankstr
, jobid
);
559 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes"),
560 namestr
, 1024.0 * jobsize
);
564 _cupsLangPrintf(stdout
,
565 _("%-6s %-10.10s %-4d %-10d %-27.27s %.0f bytes"),
566 rankstr
, jobuser
, jobpriority
, jobid
, jobname
,
569 _cupsLangPrintf(stdout
,
570 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes"),
571 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
582 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
587 _cupsLangPuts(stdout
, _("no entries"));
594 * 'show_printer()' - Show printer status.
598 show_printer(const char *command
, /* I - Command name */
599 http_t
*http
, /* I - HTTP connection to server */
600 const char *dest
) /* I - Destination */
602 ipp_t
*request
, /* IPP Request */
603 *response
; /* IPP Response */
604 ipp_attribute_t
*attr
; /* Current attribute */
605 ipp_pstate_t state
; /* Printer state */
606 char uri
[HTTP_MAX_URI
]; /* Printer URI */
613 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
617 * attributes-natural-language
621 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
623 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
624 "localhost", 0, "/printers/%s", dest
);
625 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
626 "printer-uri", NULL
, uri
);
629 * Do the request and get back a response...
632 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
634 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
636 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
641 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
642 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
644 state
= IPP_PRINTER_STOPPED
;
648 case IPP_PRINTER_IDLE
:
649 _cupsLangPrintf(stdout
, _("%s is ready"), dest
);
651 case IPP_PRINTER_PROCESSING
:
652 _cupsLangPrintf(stdout
, _("%s is ready and printing"),
655 case IPP_PRINTER_STOPPED
:
656 _cupsLangPrintf(stdout
, _("%s is not ready"), dest
);
663 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
668 * 'usage()' - Show program usage.
674 _cupsLangPuts(stderr
,
675 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
676 "[-l] [+interval]"));