4 * "lpq" command for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
20 #include <cups/cups-private.h>
27 static http_t
*connect_server(const char *, http_t
*);
28 static int show_jobs(const char *, http_t
*, const char *,
29 const char *, const int, const int);
30 static void show_printer(const char *, http_t
*, const char *);
31 static void usage(void) __attribute__((noreturn
));
35 * 'main()' - Parse options and commands.
39 main(int argc
, /* I - Number of command-line arguments */
40 char *argv
[]) /* I - Command-line arguments */
42 int i
; /* Looping var */
43 http_t
*http
; /* Connection to server */
44 const char *dest
, /* Desired printer */
45 *user
, /* Desired user */
46 *val
; /* Environment variable name */
47 char *instance
; /* Printer instance */
48 int id
, /* Desired job ID */
49 all
, /* All printers */
50 interval
, /* Reporting interval */
51 longstatus
; /* Show file details */
52 cups_dest_t
*named_dest
; /* Named destination */
58 * Check for command-line options...
69 for (i
= 1; i
< argc
; i
++)
70 if (argv
[i
][0] == '+')
71 interval
= atoi(argv
[i
] + 1);
72 else if (argv
[i
][0] == '-')
76 case 'E' : /* Encrypt */
78 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
81 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
83 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
88 case 'U' : /* Username */
89 if (argv
[i
][2] != '\0')
90 cupsSetUser(argv
[i
] + 2);
96 _cupsLangPrintf(stderr
,
97 _("%s: Error - expected username after "
98 "\"-U\" option."), argv
[0]);
102 cupsSetUser(argv
[i
]);
106 case 'P' : /* Printer */
123 if ((instance
= strchr(dest
, '/')) != NULL
)
126 http
= connect_server(argv
[0], http
);
128 if ((named_dest
= cupsGetNamedDest(http
, dest
, instance
)) == NULL
)
130 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
131 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
132 _cupsLangPrintf(stderr
,
133 _("%s: Error - add '/version=1.1' to server "
136 _cupsLangPrintf(stderr
,
137 _("%s: Error - unknown destination \"%s/%s\"."),
138 argv
[0], dest
, instance
);
140 _cupsLangPrintf(stderr
, _("%s: Unknown destination \"%s\"."),
146 cupsFreeDests(1, named_dest
);
149 case 'a' : /* All printers */
153 case 'h' : /* Connect to host */
160 if (argv
[i
][2] != '\0')
161 cupsSetServer(argv
[i
] + 2);
168 _cupsLangPrintf(stderr
,
169 _("%s: Error - expected hostname after "
170 "\"-h\" option."), argv
[0]);
174 cupsSetServer(argv
[i
]);
178 case 'l' : /* Long status */
188 else if (isdigit(argv
[i
][0] & 255))
193 http
= connect_server(argv
[0], http
);
195 if (dest
== NULL
&& !all
)
197 if ((named_dest
= cupsGetNamedDest(http
, NULL
, NULL
)) == NULL
)
199 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
200 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
202 _cupsLangPrintf(stderr
,
203 _("%s: Error - add '/version=1.1' to server name."),
210 if ((dest
= getenv("LPDEST")) == NULL
)
212 if ((dest
= getenv("PRINTER")) != NULL
)
214 if (!strcmp(dest
, "lp"))
224 _cupsLangPrintf(stderr
,
225 _("%s: Error - %s environment variable names "
226 "non-existent destination \"%s\"."), argv
[0], val
,
229 _cupsLangPrintf(stderr
,
230 _("%s: Error - no default destination available."),
236 dest
= named_dest
->name
;
240 * Show the status in a loop...
246 show_printer(argv
[0], http
, dest
);
248 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
253 sleep((unsigned)interval
);
260 * Close the connection to the server and return...
270 * 'connect_server()' - Connect to the server as necessary...
273 static http_t
* /* O - New HTTP connection */
274 connect_server(const char *command
, /* I - Command name */
275 http_t
*http
) /* I - Current HTTP connection */
279 http
= httpConnectEncrypt(cupsServer(), ippPort(),
284 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."), command
);
294 * 'show_jobs()' - Show jobs.
297 static int /* O - Number of jobs in queue */
298 show_jobs(const char *command
, /* I - Command name */
299 http_t
*http
, /* I - HTTP connection to server */
300 const char *dest
, /* I - Destination */
301 const char *user
, /* I - User */
302 const int id
, /* I - Job ID */
303 const int longstatus
) /* I - 1 if long report desired */
305 ipp_t
*request
, /* IPP Request */
306 *response
; /* IPP Response */
307 ipp_attribute_t
*attr
; /* Current attribute */
308 const char *jobdest
, /* Pointer into job-printer-uri */
309 *jobuser
, /* Pointer to job-originating-user-name */
310 *jobname
; /* Pointer to job-name */
311 ipp_jstate_t jobstate
; /* job-state */
312 int jobid
, /* job-id */
313 jobsize
, /* job-k-octets */
314 jobcount
, /* Number of jobs */
315 jobcopies
, /* Number of copies */
316 rank
; /* Rank of job */
317 char resource
[1024]; /* Resource string */
318 char rankstr
[255]; /* Rank string */
319 char namestr
[1024]; /* Job name string */
320 static const char * const jobattrs
[] =/* Job attributes we want to see */
326 "job-originating-user-name",
331 static const char * const ranks
[10] = /* Ranking strings */
346 DEBUG_printf(("show_jobs(http=%p, dest=%p, user=%p, id=%d, longstatus%d)\n",
347 http
, dest
, user
, id
, longstatus
));
353 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
354 * the following attributes:
357 * attributes-natural-language
358 * job-uri or printer-uri
359 * requested-attributes
360 * requesting-user-name
363 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
367 snprintf(resource
, sizeof(resource
), "ipp://localhost/jobs/%d", id
);
368 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
373 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
374 NULL
, "localhost", 0, "/printers/%s", dest
);
376 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
380 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
381 NULL
, "ipp://localhost/");
385 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
386 "requesting-user-name", NULL
, user
);
387 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
390 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
391 "requesting-user-name", NULL
, cupsUser());
393 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
394 "requested-attributes",
395 (int)(sizeof(jobattrs
) / sizeof(jobattrs
[0])), NULL
, jobattrs
);
398 * Do the request and get back a response...
403 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
405 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
407 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
415 * Loop through the job list and display them...
418 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
421 * Skip leading attributes until we hit a job...
424 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
431 * Pull the needed attributes from this job...
436 jobstate
= IPP_JOB_PENDING
;
442 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
444 if (!strcmp(attr
->name
, "job-id") &&
445 attr
->value_tag
== IPP_TAG_INTEGER
)
446 jobid
= attr
->values
[0].integer
;
448 if (!strcmp(attr
->name
, "job-k-octets") &&
449 attr
->value_tag
== IPP_TAG_INTEGER
)
450 jobsize
= attr
->values
[0].integer
;
452 if (!strcmp(attr
->name
, "job-state") &&
453 attr
->value_tag
== IPP_TAG_ENUM
)
454 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
456 if (!strcmp(attr
->name
, "job-printer-uri") &&
457 attr
->value_tag
== IPP_TAG_URI
)
458 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
461 if (!strcmp(attr
->name
, "job-originating-user-name") &&
462 attr
->value_tag
== IPP_TAG_NAME
)
463 jobuser
= attr
->values
[0].string
.text
;
465 if (!strcmp(attr
->name
, "job-name") &&
466 attr
->value_tag
== IPP_TAG_NAME
)
467 jobname
= attr
->values
[0].string
.text
;
469 if (!strcmp(attr
->name
, "copies") &&
470 attr
->value_tag
== IPP_TAG_INTEGER
)
471 jobcopies
= attr
->values
[0].integer
;
477 * See if we have everything needed...
480 if (jobdest
== NULL
|| jobid
== 0)
488 if (!longstatus
&& jobcount
== 0)
489 _cupsLangPuts(stdout
,
490 _("Rank Owner Job File(s)"
499 if (jobstate
== IPP_JOB_PROCESSING
)
500 strlcpy(rankstr
, "active", sizeof(rankstr
));
504 * Make the rank show the "correct" suffix for each number
505 * (11-13 are the only special cases, for English anyways...)
508 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
509 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
511 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
518 _cupsLangPuts(stdout
, "\n");
521 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
524 strlcpy(namestr
, jobname
, sizeof(namestr
));
526 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]"),
527 jobuser
, rankstr
, jobid
);
528 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes"),
529 namestr
, 1024.0 * jobsize
);
532 _cupsLangPrintf(stdout
,
533 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes"),
534 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
544 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
549 _cupsLangPuts(stdout
, _("no entries"));
556 * 'show_printer()' - Show printer status.
560 show_printer(const char *command
, /* I - Command name */
561 http_t
*http
, /* I - HTTP connection to server */
562 const char *dest
) /* I - Destination */
564 ipp_t
*request
, /* IPP Request */
565 *response
; /* IPP Response */
566 ipp_attribute_t
*attr
; /* Current attribute */
567 ipp_pstate_t state
; /* Printer state */
568 char uri
[HTTP_MAX_URI
]; /* Printer URI */
575 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
579 * attributes-natural-language
583 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
585 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
586 "localhost", 0, "/printers/%s", dest
);
587 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
588 "printer-uri", NULL
, uri
);
591 * Do the request and get back a response...
594 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
596 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
598 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
603 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
604 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
606 state
= IPP_PRINTER_STOPPED
;
610 case IPP_PRINTER_IDLE
:
611 _cupsLangPrintf(stdout
, _("%s is ready"), dest
);
613 case IPP_PRINTER_PROCESSING
:
614 _cupsLangPrintf(stdout
, _("%s is ready and printing"),
617 case IPP_PRINTER_STOPPED
:
618 _cupsLangPrintf(stdout
, _("%s is not ready"), dest
);
625 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
630 * 'usage()' - Show program usage.
636 _cupsLangPuts(stderr
,
637 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
638 "[-l] [+interval]"));