2 * "lpq" command for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/cups-private.h>
25 static http_t
*connect_server(const char *, http_t
*);
26 static int show_jobs(const char *, http_t
*, const char *,
27 const char *, const int, const int);
28 static void show_printer(const char *, http_t
*, const char *);
29 static void usage(void) __attribute__((noreturn
));
33 * 'main()' - Parse options and commands.
37 main(int argc
, /* I - Number of command-line arguments */
38 char *argv
[]) /* I - Command-line arguments */
40 int i
; /* Looping var */
41 http_t
*http
; /* Connection to server */
42 const char *opt
, /* Option pointer */
43 *dest
, /* Desired printer */
44 *user
, /* Desired user */
45 *val
; /* Environment variable name */
46 char *instance
; /* Printer instance */
47 int id
, /* Desired job ID */
48 all
, /* All printers */
49 interval
, /* Reporting interval */
50 longstatus
; /* Show file details */
51 cups_dest_t
*named_dest
; /* Named destination */
57 * Check for command-line options...
68 for (i
= 1; i
< argc
; i
++)
70 if (argv
[i
][0] == '+')
72 interval
= atoi(argv
[i
] + 1);
74 else if (argv
[i
][0] == '-')
76 for (opt
= argv
[i
] + 1; *opt
; opt
++)
80 case 'E' : /* Encrypt */
82 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
85 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
87 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
91 case 'U' : /* Username */
95 opt
+= strlen(opt
) - 1;
102 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
106 cupsSetUser(argv
[i
]);
110 case 'P' : /* Printer */
114 opt
+= strlen(opt
) - 1;
130 if ((instance
= strchr(dest
, '/')) != NULL
)
133 http
= connect_server(argv
[0], http
);
135 if ((named_dest
= cupsGetNamedDest(http
, dest
, instance
)) == NULL
)
137 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
138 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
139 _cupsLangPrintf(stderr
, _("%s: Error - add '/version=1.1' to server name."), argv
[0]);
141 _cupsLangPrintf(stderr
, _("%s: Error - unknown destination \"%s/%s\"."), argv
[0], dest
, instance
);
143 _cupsLangPrintf(stderr
, _("%s: Unknown destination \"%s\"."), argv
[0], dest
);
148 cupsFreeDests(1, named_dest
);
151 case 'a' : /* All printers */
155 case 'h' : /* Connect to host */
164 cupsSetServer(opt
+ 1);
165 opt
+= strlen(opt
) - 1;
173 _cupsLangPrintf(stderr
, _("%s: Error - expected hostname after \"-h\" option."), argv
[0]);
177 cupsSetServer(argv
[i
]);
181 case 'l' : /* Long status */
192 else if (isdigit(argv
[i
][0] & 255))
202 http
= connect_server(argv
[0], http
);
204 if (dest
== NULL
&& !all
)
206 if ((named_dest
= cupsGetNamedDest(http
, NULL
, NULL
)) == NULL
)
208 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
209 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
211 _cupsLangPrintf(stderr
,
212 _("%s: Error - add '/version=1.1' to server name."),
219 if ((dest
= getenv("LPDEST")) == NULL
)
221 if ((dest
= getenv("PRINTER")) != NULL
)
223 if (!strcmp(dest
, "lp"))
233 _cupsLangPrintf(stderr
,
234 _("%s: Error - %s environment variable names "
235 "non-existent destination \"%s\"."), argv
[0], val
,
238 _cupsLangPrintf(stderr
,
239 _("%s: Error - no default destination available."),
245 dest
= named_dest
->name
;
249 * Show the status in a loop...
255 show_printer(argv
[0], http
, dest
);
257 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
262 sleep((unsigned)interval
);
269 * Close the connection to the server and return...
279 * 'connect_server()' - Connect to the server as necessary...
282 static http_t
* /* O - New HTTP connection */
283 connect_server(const char *command
, /* I - Command name */
284 http_t
*http
) /* I - Current HTTP connection */
288 http
= httpConnectEncrypt(cupsServer(), ippPort(),
293 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."), command
);
303 * 'show_jobs()' - Show jobs.
306 static int /* O - Number of jobs in queue */
307 show_jobs(const char *command
, /* I - Command name */
308 http_t
*http
, /* I - HTTP connection to server */
309 const char *dest
, /* I - Destination */
310 const char *user
, /* I - User */
311 const int id
, /* I - Job ID */
312 const int longstatus
) /* I - 1 if long report desired */
314 ipp_t
*request
, /* IPP Request */
315 *response
; /* IPP Response */
316 ipp_attribute_t
*attr
; /* Current attribute */
317 const char *jobdest
, /* Pointer into job-printer-uri */
318 *jobuser
, /* Pointer to job-originating-user-name */
319 *jobname
; /* Pointer to job-name */
320 ipp_jstate_t jobstate
; /* job-state */
321 int jobid
, /* job-id */
322 jobsize
, /* job-k-octets */
323 jobcount
, /* Number of jobs */
324 jobcopies
, /* Number of copies */
325 rank
; /* Rank of job */
326 char resource
[1024]; /* Resource string */
327 char rankstr
[255]; /* Rank string */
328 char namestr
[1024]; /* Job name string */
329 static const char * const jobattrs
[] =/* Job attributes we want to see */
335 "job-originating-user-name",
340 static const char * const ranks
[10] = /* Ranking strings */
355 DEBUG_printf(("show_jobs(http=%p, dest=%p, user=%p, id=%d, longstatus%d)\n",
356 http
, dest
, user
, id
, longstatus
));
362 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
363 * the following attributes:
366 * attributes-natural-language
367 * job-uri or printer-uri
368 * requested-attributes
369 * requesting-user-name
372 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
376 snprintf(resource
, sizeof(resource
), "ipp://localhost/jobs/%d", id
);
377 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
382 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
383 NULL
, "localhost", 0, "/printers/%s", dest
);
385 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
389 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
390 NULL
, "ipp://localhost/");
394 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
395 "requesting-user-name", NULL
, user
);
396 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
399 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
400 "requesting-user-name", NULL
, cupsUser());
402 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
403 "requested-attributes",
404 (int)(sizeof(jobattrs
) / sizeof(jobattrs
[0])), NULL
, jobattrs
);
407 * Do the request and get back a response...
412 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
414 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
416 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
424 * Loop through the job list and display them...
427 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
430 * Skip leading attributes until we hit a job...
433 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
440 * Pull the needed attributes from this job...
445 jobstate
= IPP_JOB_PENDING
;
451 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
453 if (!strcmp(attr
->name
, "job-id") &&
454 attr
->value_tag
== IPP_TAG_INTEGER
)
455 jobid
= attr
->values
[0].integer
;
457 if (!strcmp(attr
->name
, "job-k-octets") &&
458 attr
->value_tag
== IPP_TAG_INTEGER
)
459 jobsize
= attr
->values
[0].integer
;
461 if (!strcmp(attr
->name
, "job-state") &&
462 attr
->value_tag
== IPP_TAG_ENUM
)
463 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
465 if (!strcmp(attr
->name
, "job-printer-uri") &&
466 attr
->value_tag
== IPP_TAG_URI
)
467 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
470 if (!strcmp(attr
->name
, "job-originating-user-name") &&
471 attr
->value_tag
== IPP_TAG_NAME
)
472 jobuser
= attr
->values
[0].string
.text
;
474 if (!strcmp(attr
->name
, "job-name") &&
475 attr
->value_tag
== IPP_TAG_NAME
)
476 jobname
= attr
->values
[0].string
.text
;
478 if (!strcmp(attr
->name
, "copies") &&
479 attr
->value_tag
== IPP_TAG_INTEGER
)
480 jobcopies
= attr
->values
[0].integer
;
486 * See if we have everything needed...
489 if (jobdest
== NULL
|| jobid
== 0)
497 if (!longstatus
&& jobcount
== 0)
498 _cupsLangPuts(stdout
,
499 _("Rank Owner Job File(s)"
508 if (jobstate
== IPP_JOB_PROCESSING
)
509 strlcpy(rankstr
, "active", sizeof(rankstr
));
513 * Make the rank show the "correct" suffix for each number
514 * (11-13 are the only special cases, for English anyways...)
517 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
518 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
520 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
527 _cupsLangPuts(stdout
, "\n");
530 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
533 strlcpy(namestr
, jobname
, sizeof(namestr
));
535 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]"),
536 jobuser
, rankstr
, jobid
);
537 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes"),
538 namestr
, 1024.0 * jobsize
);
541 _cupsLangPrintf(stdout
,
542 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes"),
543 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
553 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
558 _cupsLangPuts(stdout
, _("no entries"));
565 * 'show_printer()' - Show printer status.
569 show_printer(const char *command
, /* I - Command name */
570 http_t
*http
, /* I - HTTP connection to server */
571 const char *dest
) /* I - Destination */
573 ipp_t
*request
, /* IPP Request */
574 *response
; /* IPP Response */
575 ipp_attribute_t
*attr
; /* Current attribute */
576 ipp_pstate_t state
; /* Printer state */
577 char uri
[HTTP_MAX_URI
]; /* Printer URI */
584 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
588 * attributes-natural-language
592 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
594 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
595 "localhost", 0, "/printers/%s", dest
);
596 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
597 "printer-uri", NULL
, uri
);
600 * Do the request and get back a response...
603 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
605 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
607 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
612 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
613 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
615 state
= IPP_PRINTER_STOPPED
;
619 case IPP_PRINTER_IDLE
:
620 _cupsLangPrintf(stdout
, _("%s is ready"), dest
);
622 case IPP_PRINTER_PROCESSING
:
623 _cupsLangPrintf(stdout
, _("%s is ready and printing"),
626 case IPP_PRINTER_STOPPED
:
627 _cupsLangPrintf(stdout
, _("%s is not ready"), dest
);
634 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
639 * 'usage()' - Show program usage.
645 _cupsLangPuts(stderr
,
646 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
647 "[-l] [+interval]"));