2 * "lpq" command for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
21 static http_t
*connect_server(const char *, http_t
*);
22 static int show_jobs(const char *, http_t
*, const char *,
23 const char *, const int, const int);
24 static void show_printer(const char *, http_t
*, const char *);
25 static void usage(void) _CUPS_NORETURN
;
29 * 'main()' - Parse options and commands.
33 main(int argc
, /* I - Number of command-line arguments */
34 char *argv
[]) /* I - Command-line arguments */
36 int i
; /* Looping var */
37 http_t
*http
; /* Connection to server */
38 const char *opt
, /* Option pointer */
39 *dest
, /* Desired printer */
40 *user
, /* Desired user */
41 *val
; /* Environment variable name */
42 char *instance
; /* Printer instance */
43 int id
, /* Desired job ID */
44 all
, /* All printers */
45 interval
, /* Reporting interval */
46 longstatus
; /* Show file details */
47 cups_dest_t
*named_dest
; /* Named destination */
53 * Check for command-line options...
64 for (i
= 1; i
< argc
; i
++)
66 if (argv
[i
][0] == '+')
68 interval
= atoi(argv
[i
] + 1);
70 else if (argv
[i
][0] == '-')
72 for (opt
= argv
[i
] + 1; *opt
; opt
++)
76 case 'E' : /* Encrypt */
78 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
81 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
83 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
87 case 'U' : /* Username */
91 opt
+= strlen(opt
) - 1;
98 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
102 cupsSetUser(argv
[i
]);
106 case 'P' : /* Printer */
110 opt
+= strlen(opt
) - 1;
126 if ((instance
= strchr(dest
, '/')) != NULL
)
129 http
= connect_server(argv
[0], http
);
131 if ((named_dest
= cupsGetNamedDest(http
, dest
, instance
)) == NULL
)
133 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
134 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
135 _cupsLangPrintf(stderr
, _("%s: Error - add '/version=1.1' to server name."), argv
[0]);
137 _cupsLangPrintf(stderr
, _("%s: Error - unknown destination \"%s/%s\"."), argv
[0], dest
, instance
);
139 _cupsLangPrintf(stderr
, _("%s: Unknown destination \"%s\"."), argv
[0], dest
);
144 cupsFreeDests(1, named_dest
);
147 case 'a' : /* All printers */
151 case 'h' : /* Connect to host */
160 cupsSetServer(opt
+ 1);
161 opt
+= strlen(opt
) - 1;
169 _cupsLangPrintf(stderr
, _("%s: Error - expected hostname after \"-h\" option."), argv
[0]);
173 cupsSetServer(argv
[i
]);
177 case 'l' : /* Long status */
188 else if (isdigit(argv
[i
][0] & 255))
198 http
= connect_server(argv
[0], http
);
200 if (dest
== NULL
&& !all
)
202 if ((named_dest
= cupsGetNamedDest(http
, NULL
, NULL
)) == NULL
)
204 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
205 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
207 _cupsLangPrintf(stderr
,
208 _("%s: Error - add '/version=1.1' to server name."),
215 if ((dest
= getenv("LPDEST")) == NULL
)
217 if ((dest
= getenv("PRINTER")) != NULL
)
219 if (!strcmp(dest
, "lp"))
229 _cupsLangPrintf(stderr
,
230 _("%s: Error - %s environment variable names "
231 "non-existent destination \"%s\"."), argv
[0], val
,
234 _cupsLangPrintf(stderr
,
235 _("%s: Error - no default destination available."),
241 dest
= named_dest
->name
;
245 * Show the status in a loop...
251 show_printer(argv
[0], http
, dest
);
253 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
258 sleep((unsigned)interval
);
265 * Close the connection to the server and return...
275 * 'connect_server()' - Connect to the server as necessary...
278 static http_t
* /* O - New HTTP connection */
279 connect_server(const char *command
, /* I - Command name */
280 http_t
*http
) /* I - Current HTTP connection */
284 http
= httpConnectEncrypt(cupsServer(), ippPort(),
289 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."), command
);
299 * 'show_jobs()' - Show jobs.
302 static int /* O - Number of jobs in queue */
303 show_jobs(const char *command
, /* I - Command name */
304 http_t
*http
, /* I - HTTP connection to server */
305 const char *dest
, /* I - Destination */
306 const char *user
, /* I - User */
307 const int id
, /* I - Job ID */
308 const int longstatus
) /* I - 1 if long report desired */
310 ipp_t
*request
, /* IPP Request */
311 *response
; /* IPP Response */
312 ipp_attribute_t
*attr
; /* Current attribute */
313 const char *jobdest
, /* Pointer into job-printer-uri */
314 *jobuser
, /* Pointer to job-originating-user-name */
315 *jobname
; /* Pointer to job-name */
316 ipp_jstate_t jobstate
; /* job-state */
317 int jobid
, /* job-id */
318 jobsize
, /* job-k-octets */
319 jobcount
, /* Number of jobs */
320 jobcopies
, /* Number of copies */
321 rank
; /* Rank of job */
322 char resource
[1024]; /* Resource string */
323 char rankstr
[255]; /* Rank string */
324 char namestr
[1024]; /* Job name string */
325 static const char * const jobattrs
[] =/* Job attributes we want to see */
331 "job-originating-user-name",
336 static const char * const ranks
[10] = /* Ranking strings */
355 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
356 * the following attributes:
359 * attributes-natural-language
360 * job-uri or printer-uri
361 * requested-attributes
362 * requesting-user-name
365 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
369 snprintf(resource
, sizeof(resource
), "ipp://localhost/jobs/%d", id
);
370 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
375 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
376 NULL
, "localhost", 0, "/printers/%s", dest
);
378 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
382 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
383 NULL
, "ipp://localhost/");
387 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
388 "requesting-user-name", NULL
, user
);
389 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
392 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
393 "requesting-user-name", NULL
, cupsUser());
395 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
396 "requested-attributes",
397 (int)(sizeof(jobattrs
) / sizeof(jobattrs
[0])), NULL
, jobattrs
);
400 * Do the request and get back a response...
405 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
407 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
409 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
417 * Loop through the job list and display them...
420 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
423 * Skip leading attributes until we hit a job...
426 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
433 * Pull the needed attributes from this job...
438 jobstate
= IPP_JOB_PENDING
;
444 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
446 if (!strcmp(attr
->name
, "job-id") &&
447 attr
->value_tag
== IPP_TAG_INTEGER
)
448 jobid
= attr
->values
[0].integer
;
450 if (!strcmp(attr
->name
, "job-k-octets") &&
451 attr
->value_tag
== IPP_TAG_INTEGER
)
452 jobsize
= attr
->values
[0].integer
;
454 if (!strcmp(attr
->name
, "job-state") &&
455 attr
->value_tag
== IPP_TAG_ENUM
)
456 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
458 if (!strcmp(attr
->name
, "job-printer-uri") &&
459 attr
->value_tag
== IPP_TAG_URI
)
460 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
463 if (!strcmp(attr
->name
, "job-originating-user-name") &&
464 attr
->value_tag
== IPP_TAG_NAME
)
465 jobuser
= attr
->values
[0].string
.text
;
467 if (!strcmp(attr
->name
, "job-name") &&
468 attr
->value_tag
== IPP_TAG_NAME
)
469 jobname
= attr
->values
[0].string
.text
;
471 if (!strcmp(attr
->name
, "copies") &&
472 attr
->value_tag
== IPP_TAG_INTEGER
)
473 jobcopies
= attr
->values
[0].integer
;
479 * See if we have everything needed...
482 if (jobdest
== NULL
|| jobid
== 0)
490 if (!longstatus
&& jobcount
== 0)
491 _cupsLangPuts(stdout
,
492 _("Rank Owner Job File(s)"
501 if (jobstate
== IPP_JOB_PROCESSING
)
502 strlcpy(rankstr
, "active", sizeof(rankstr
));
506 * Make the rank show the "correct" suffix for each number
507 * (11-13 are the only special cases, for English anyways...)
510 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
511 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
513 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
520 _cupsLangPuts(stdout
, "\n");
523 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
526 strlcpy(namestr
, jobname
, sizeof(namestr
));
528 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]"),
529 jobuser
, rankstr
, jobid
);
530 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes"),
531 namestr
, 1024.0 * jobsize
);
534 _cupsLangPrintf(stdout
,
535 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes"),
536 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
546 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
551 _cupsLangPuts(stdout
, _("no entries"));
558 * 'show_printer()' - Show printer status.
562 show_printer(const char *command
, /* I - Command name */
563 http_t
*http
, /* I - HTTP connection to server */
564 const char *dest
) /* I - Destination */
566 ipp_t
*request
, /* IPP Request */
567 *response
; /* IPP Response */
568 ipp_attribute_t
*attr
; /* Current attribute */
569 ipp_pstate_t state
; /* Printer state */
570 char uri
[HTTP_MAX_URI
]; /* Printer URI */
577 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
581 * attributes-natural-language
585 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
587 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
588 "localhost", 0, "/printers/%s", dest
);
589 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
590 "printer-uri", NULL
, uri
);
593 * Do the request and get back a response...
596 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
598 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
600 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
605 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
606 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
608 state
= IPP_PRINTER_STOPPED
;
612 case IPP_PRINTER_IDLE
:
613 _cupsLangPrintf(stdout
, _("%s is ready"), dest
);
615 case IPP_PRINTER_PROCESSING
:
616 _cupsLangPrintf(stdout
, _("%s is ready and printing"),
619 case IPP_PRINTER_STOPPED
:
620 _cupsLangPrintf(stdout
, _("%s is not ready"), dest
);
627 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
632 * 'usage()' - Show program usage.
638 _cupsLangPuts(stderr
,
639 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
640 "[-l] [+interval]"));