2 * "$Id: lpq.c 5925 2006-09-05 19:43:11Z mike $"
4 * "lpq" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Parse options and commands.
27 * show_jobs() - Show jobs.
28 * show_printer() - Show printer status.
29 * usage() - Show program usage.
33 * Include necessary headers...
37 * Include necessary headers...
42 #include <cups/string.h>
43 #include <cups/cups.h>
44 #include <cups/i18n.h>
45 #include <cups/debug.h>
52 static http_t
*connect_server(const char *, http_t
*);
53 static int show_jobs(const char *, http_t
*, const char *,
54 const char *, const int, const int);
55 static void show_printer(const char *, http_t
*, const char *);
56 static void usage(void);
60 * 'main()' - Parse options and commands.
64 main(int argc
, /* I - Number of command-line arguments */
65 char *argv
[]) /* I - Command-line arguments */
67 int i
; /* Looping var */
68 http_t
*http
; /* Connection to server */
69 const char *dest
, /* Desired printer */
70 *user
, /* Desired user */
71 *val
; /* Environment variable name */
72 char *instance
; /* Printer instance */
73 int id
, /* Desired job ID */
74 all
, /* All printers */
75 interval
, /* Reporting interval */
76 longstatus
; /* Show file details */
77 int num_dests
; /* Number of destinations */
78 cups_dest_t
*dests
; /* Destinations */
84 * Check for command-line options...
97 for (i
= 1; i
< argc
; i
++)
98 if (argv
[i
][0] == '+')
99 interval
= atoi(argv
[i
] + 1);
100 else if (argv
[i
][0] == '-')
104 case 'E' : /* Encrypt */
106 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
109 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
111 _cupsLangPrintf(stderr
,
112 _("%s: Sorry, no encryption support compiled in!\n"),
114 #endif /* HAVE_SSL */
117 case 'U' : /* Username */
118 if (argv
[i
][2] != '\0')
119 cupsSetUser(argv
[i
] + 2);
125 _cupsLangPrintf(stderr
,
126 _("%s: Error - expected username after "
132 cupsSetUser(argv
[i
]);
136 case 'P' : /* Printer */
146 cupsFreeDests(num_dests
, dests
);
154 if ((instance
= strchr(dest
, '/')) != NULL
)
157 http
= connect_server(argv
[0], http
);
160 num_dests
= cupsGetDests2(http
, &dests
);
162 if (cupsGetDest(dest
, instance
, num_dests
, dests
) == NULL
)
165 _cupsLangPrintf(stderr
,
166 _("%s: Error - unknown destination \"%s/%s\"!\n"),
167 argv
[0], dest
, instance
);
169 _cupsLangPrintf(stderr
,
170 _("%s: Unknown destination \"%s\"!\n"),
177 case 'a' : /* All printers */
181 case 'h' : /* Connect to host */
188 if (argv
[i
][2] != '\0')
189 cupsSetServer(argv
[i
] + 2);
196 _cupsLangPrintf(stderr
,
197 _("%s: Error - expected hostname after "
203 cupsSetServer(argv
[i
]);
207 case 'l' : /* Long status */
213 cupsFreeDests(num_dests
, dests
);
219 else if (isdigit(argv
[i
][0] & 255))
224 http
= connect_server(argv
[0], http
);
226 if (dest
== NULL
&& !all
)
229 num_dests
= cupsGetDests2(http
, &dests
);
231 for (i
= 0; i
< num_dests
; i
++)
232 if (dests
[i
].is_default
)
233 dest
= dests
[i
].name
;
239 if ((dest
= getenv("LPDEST")) == NULL
)
241 if ((dest
= getenv("PRINTER")) != NULL
)
243 if (!strcmp(dest
, "lp"))
252 if (dest
&& !cupsGetDest(dest
, NULL
, num_dests
, dests
))
253 _cupsLangPrintf(stderr
,
254 _("%s: error - %s environment variable names "
255 "non-existent destination \"%s\"!\n"),
258 _cupsLangPrintf(stderr
,
259 _("%s: error - no default destination available.\n"),
262 cupsFreeDests(num_dests
, dests
);
268 * Show the status in a loop...
274 show_printer(argv
[0], http
, dest
);
276 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
288 * Close the connection to the server and return...
291 cupsFreeDests(num_dests
, dests
);
299 * 'connect_server()' - Connect to the server as necessary...
302 static http_t
* /* O - New HTTP connection */
303 connect_server(const char *command
, /* I - Command name */
304 http_t
*http
) /* I - Current HTTP connection */
308 http
= httpConnectEncrypt(cupsServer(), ippPort(),
313 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server\n"), command
);
323 * 'show_jobs()' - Show jobs.
326 static int /* O - Number of jobs in queue */
327 show_jobs(const char *command
, /* I - Command name */
328 http_t
*http
, /* I - HTTP connection to server */
329 const char *dest
, /* I - Destination */
330 const char *user
, /* I - User */
331 const int id
, /* I - Job ID */
332 const int longstatus
) /* I - 1 if long report desired */
334 ipp_t
*request
, /* IPP Request */
335 *response
; /* IPP Response */
336 ipp_attribute_t
*attr
; /* Current attribute */
337 const char *jobdest
, /* Pointer into job-printer-uri */
338 *jobuser
, /* Pointer to job-originating-user-name */
339 *jobname
; /* Pointer to job-name */
340 ipp_jstate_t jobstate
; /* job-state */
341 int jobid
, /* job-id */
342 jobsize
, /* job-k-octets */
344 jobpriority
, /* job-priority */
346 jobcount
, /* Number of jobs */
347 jobcopies
, /* Number of copies */
348 rank
; /* Rank of job */
349 char resource
[1024]; /* Resource string */
350 char rankstr
[255]; /* Rank string */
351 char namestr
[1024]; /* Job name string */
352 static const char *ranks
[10] = /* Ranking strings */
367 DEBUG_printf(("show_jobs(%08x, %08x, %08x, %d, %d)\n", http
, dest
, user
, id
,
374 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
375 * the following attributes:
378 * attributes-natural-language
379 * job-uri or printer-uri
382 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
387 sprintf(resource
, "ipp://localhost/jobs/%d", id
);
389 strcpy(resource
, "ipp://localhost/jobs");
391 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
396 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
397 NULL
, "localhost", 0, "/printers/%s", dest
);
399 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
405 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
406 "requesting-user-name", NULL
, user
);
407 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
411 * Do the request and get back a response...
416 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
418 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
420 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
428 * Loop through the job list and display them...
431 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
434 * Skip leading attributes until we hit a job...
437 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
444 * Pull the needed attributes from this job...
452 jobstate
= IPP_JOB_PENDING
;
453 jobname
= "untitled";
458 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
460 if (!strcmp(attr
->name
, "job-id") &&
461 attr
->value_tag
== IPP_TAG_INTEGER
)
462 jobid
= attr
->values
[0].integer
;
464 if (!strcmp(attr
->name
, "job-k-octets") &&
465 attr
->value_tag
== IPP_TAG_INTEGER
)
466 jobsize
= attr
->values
[0].integer
;
469 if (!strcmp(attr
->name
, "job-priority") &&
470 attr
->value_tag
== IPP_TAG_INTEGER
)
471 jobpriority
= attr
->values
[0].integer
;
474 if (!strcmp(attr
->name
, "job-state") &&
475 attr
->value_tag
== IPP_TAG_ENUM
)
476 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
478 if (!strcmp(attr
->name
, "job-printer-uri") &&
479 attr
->value_tag
== IPP_TAG_URI
)
480 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
483 if (!strcmp(attr
->name
, "job-originating-user-name") &&
484 attr
->value_tag
== IPP_TAG_NAME
)
485 jobuser
= attr
->values
[0].string
.text
;
487 if (!strcmp(attr
->name
, "job-name") &&
488 attr
->value_tag
== IPP_TAG_NAME
)
489 jobname
= attr
->values
[0].string
.text
;
491 if (!strcmp(attr
->name
, "copies") &&
492 attr
->value_tag
== IPP_TAG_INTEGER
)
493 jobcopies
= attr
->values
[0].integer
;
499 * See if we have everything needed...
502 if (jobdest
== NULL
|| jobid
== 0)
510 if (!longstatus
&& jobcount
== 0)
512 _cupsLangPuts(stdout
,
513 _("Rank Owner Pri Job Files"
516 _cupsLangPuts(stdout
,
517 _("Rank Owner Job File(s)"
527 if (jobstate
== IPP_JOB_PROCESSING
)
528 strcpy(rankstr
, "active");
532 * Make the rank show the "correct" suffix for each number
533 * (11-13 are the only special cases, for English anyways...)
536 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
537 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
539 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
546 _cupsLangPuts(stdout
, "\n");
549 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
552 strlcpy(namestr
, jobname
, sizeof(namestr
));
554 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]\n"),
555 jobuser
, rankstr
, jobid
);
556 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes\n"),
557 namestr
, 1024.0 * jobsize
);
561 _cupsLangPrintf(stdout
,
562 _("%-6s %-10.10s %-4d %-10d %-27.27s %.0f bytes\n"),
563 rankstr
, jobuser
, jobpriority
, jobid
, jobname
,
566 _cupsLangPrintf(stdout
,
567 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n"),
568 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
579 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
584 _cupsLangPuts(stdout
, _("no entries\n"));
591 * 'show_printer()' - Show printer status.
595 show_printer(const char *command
, /* I - Command name */
596 http_t
*http
, /* I - HTTP connection to server */
597 const char *dest
) /* I - Destination */
599 ipp_t
*request
, /* IPP Request */
600 *response
; /* IPP Response */
601 ipp_attribute_t
*attr
; /* Current attribute */
602 ipp_pstate_t state
; /* Printer state */
603 char uri
[HTTP_MAX_URI
]; /* Printer URI */
610 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
614 * attributes-natural-language
618 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
620 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
621 "localhost", 0, "/printers/%s", dest
);
622 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
623 "printer-uri", NULL
, uri
);
626 * Do the request and get back a response...
629 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
631 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
633 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
638 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
639 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
641 state
= IPP_PRINTER_STOPPED
;
645 case IPP_PRINTER_IDLE
:
646 _cupsLangPrintf(stdout
, _("%s is ready\n"), dest
);
648 case IPP_PRINTER_PROCESSING
:
649 _cupsLangPrintf(stdout
, _("%s is ready and printing\n"),
652 case IPP_PRINTER_STOPPED
:
653 _cupsLangPrintf(stdout
, _("%s is not ready\n"), dest
);
660 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
665 * 'usage()' - Show program usage.
671 _cupsLangPuts(stderr
,
672 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
673 "[-l] [+interval]\n"));
679 * End of "$Id: lpq.c 5925 2006-09-05 19:43:11Z mike $".