2 * "$Id: lpq.c 5345 2006-03-28 16:00:17Z 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 */
79 cups_lang_t
*language
; /* Language */
85 * Check for command-line options...
95 language
= cupsLangDefault();
98 for (i
= 1; i
< argc
; i
++)
99 if (argv
[i
][0] == '+')
100 interval
= atoi(argv
[i
] + 1);
101 else if (argv
[i
][0] == '-')
105 case 'E' : /* Encrypt */
107 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
110 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
112 _cupsLangPrintf(stderr
,
113 _("%s: Sorry, no encryption support compiled in!\n"),
115 #endif /* HAVE_SSL */
118 case 'U' : /* Username */
119 if (argv
[i
][2] != '\0')
120 cupsSetUser(argv
[i
] + 2);
126 _cupsLangPrintf(stderr
,
127 _("%s: Error - expected username after "
133 cupsSetUser(argv
[i
]);
137 case 'P' : /* Printer */
147 cupsFreeDests(num_dests
, dests
);
155 if ((instance
= strchr(dest
, '/')) != NULL
)
158 http
= connect_server(argv
[0], http
);
161 num_dests
= cupsGetDests2(http
, &dests
);
163 if (cupsGetDest(dest
, instance
, num_dests
, dests
) == NULL
)
166 _cupsLangPrintf(stderr
,
167 _("%s: Error - unknown destination \"%s/%s\"!\n"),
168 argv
[0], dest
, instance
);
170 _cupsLangPrintf(stderr
,
171 _("%s: Unknown destination \"%s\"!\n"),
178 case 'a' : /* All printers */
182 case 'h' : /* Connect to host */
189 if (argv
[i
][2] != '\0')
190 cupsSetServer(argv
[i
] + 2);
197 _cupsLangPrintf(stderr
,
198 _("%s: Error - expected hostname after "
204 cupsSetServer(argv
[i
]);
208 case 'l' : /* Long status */
214 cupsFreeDests(num_dests
, dests
);
220 else if (isdigit(argv
[i
][0] & 255))
225 http
= connect_server(argv
[0], http
);
227 if (dest
== NULL
&& !all
)
230 num_dests
= cupsGetDests2(http
, &dests
);
232 for (i
= 0; i
< num_dests
; i
++)
233 if (dests
[i
].is_default
)
234 dest
= dests
[i
].name
;
240 if ((dest
= getenv("LPDEST")) == NULL
)
242 if ((dest
= getenv("PRINTER")) != NULL
)
244 if (!strcmp(dest
, "lp"))
253 if (dest
&& !cupsGetDest(dest
, NULL
, num_dests
, dests
))
254 _cupsLangPrintf(stderr
,
255 _("%s: error - %s environment variable names "
256 "non-existent destination \"%s\"!\n"),
259 _cupsLangPrintf(stderr
,
260 _("%s: error - no default destination available.\n"),
263 cupsFreeDests(num_dests
, dests
);
269 * Show the status in a loop...
275 show_printer(argv
[0], http
, dest
);
277 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
289 * Close the connection to the server and return...
292 cupsFreeDests(num_dests
, dests
);
300 * 'connect_server()' - Connect to the server as necessary...
303 static http_t
* /* O - New HTTP connection */
304 connect_server(const char *command
, /* I - Command name */
305 http_t
*http
) /* I - Current HTTP connection */
309 http
= httpConnectEncrypt(cupsServer(), ippPort(),
314 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server\n"), command
);
324 * 'show_jobs()' - Show jobs.
327 static int /* O - Number of jobs in queue */
328 show_jobs(const char *command
, /* I - Command name */
329 http_t
*http
, /* I - HTTP connection to server */
330 const char *dest
, /* I - Destination */
331 const char *user
, /* I - User */
332 const int id
, /* I - Job ID */
333 const int longstatus
) /* I - 1 if long report desired */
335 ipp_t
*request
, /* IPP Request */
336 *response
; /* IPP Response */
337 ipp_attribute_t
*attr
; /* Current attribute */
338 const char *jobdest
, /* Pointer into job-printer-uri */
339 *jobuser
, /* Pointer to job-originating-user-name */
340 *jobname
; /* Pointer to job-name */
341 ipp_jstate_t jobstate
; /* job-state */
342 int jobid
, /* job-id */
343 jobsize
, /* job-k-octets */
345 jobpriority
, /* job-priority */
347 jobcount
, /* Number of jobs */
348 jobcopies
, /* Number of copies */
349 rank
; /* Rank of job */
350 char resource
[1024]; /* Resource string */
351 char rankstr
[255]; /* Rank string */
352 char namestr
[1024]; /* Job name string */
353 static const char *ranks
[10] = /* Ranking strings */
368 DEBUG_printf(("show_jobs(%08x, %08x, %08x, %d, %d)\n", http
, dest
, user
, id
,
375 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
376 * the following attributes:
379 * attributes-natural-language
380 * job-uri or printer-uri
383 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
388 sprintf(resource
, "ipp://localhost/jobs/%d", id
);
390 strcpy(resource
, "ipp://localhost/jobs");
392 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
397 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
398 NULL
, "localhost", 0, "/printers/%s", dest
);
400 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
406 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
407 "requesting-user-name", NULL
, user
);
408 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
412 * Do the request and get back a response...
417 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
419 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
421 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
429 * Loop through the job list and display them...
432 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
435 * Skip leading attributes until we hit a job...
438 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
445 * Pull the needed attributes from this job...
453 jobstate
= IPP_JOB_PENDING
;
454 jobname
= "untitled";
459 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
461 if (!strcmp(attr
->name
, "job-id") &&
462 attr
->value_tag
== IPP_TAG_INTEGER
)
463 jobid
= attr
->values
[0].integer
;
465 if (!strcmp(attr
->name
, "job-k-octets") &&
466 attr
->value_tag
== IPP_TAG_INTEGER
)
467 jobsize
= attr
->values
[0].integer
;
470 if (!strcmp(attr
->name
, "job-priority") &&
471 attr
->value_tag
== IPP_TAG_INTEGER
)
472 jobpriority
= attr
->values
[0].integer
;
475 if (!strcmp(attr
->name
, "job-state") &&
476 attr
->value_tag
== IPP_TAG_ENUM
)
477 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
479 if (!strcmp(attr
->name
, "job-printer-uri") &&
480 attr
->value_tag
== IPP_TAG_URI
)
481 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
484 if (!strcmp(attr
->name
, "job-originating-user-name") &&
485 attr
->value_tag
== IPP_TAG_NAME
)
486 jobuser
= attr
->values
[0].string
.text
;
488 if (!strcmp(attr
->name
, "job-name") &&
489 attr
->value_tag
== IPP_TAG_NAME
)
490 jobname
= attr
->values
[0].string
.text
;
492 if (!strcmp(attr
->name
, "copies") &&
493 attr
->value_tag
== IPP_TAG_INTEGER
)
494 jobcopies
= attr
->values
[0].integer
;
500 * See if we have everything needed...
503 if (jobdest
== NULL
|| jobid
== 0)
511 if (!longstatus
&& jobcount
== 0)
513 _cupsLangPuts(stdout
,
514 _("Rank Owner Pri Job Files"
517 _cupsLangPuts(stdout
,
518 _("Rank Owner Job File(s)"
528 if (jobstate
== IPP_JOB_PROCESSING
)
529 strcpy(rankstr
, "active");
533 * Make the rank show the "correct" suffix for each number
534 * (11-13 are the only special cases, for English anyways...)
537 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
538 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
540 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
547 _cupsLangPuts(stdout
, "\n");
550 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
553 strlcpy(namestr
, jobname
, sizeof(namestr
));
555 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]\n"),
556 jobuser
, rankstr
, jobid
);
557 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes\n"),
558 namestr
, 1024.0 * jobsize
);
562 _cupsLangPrintf(stdout
,
563 _("%-6s %-10.10s %-4d %-10d %-27.27s %.0f bytes\n"),
564 rankstr
, jobuser
, jobpriority
, jobid
, jobname
,
567 _cupsLangPrintf(stdout
,
568 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n"),
569 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
580 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
585 _cupsLangPuts(stdout
, _("no entries\n"));
592 * 'show_printer()' - Show printer status.
596 show_printer(const char *command
, /* I - Command name */
597 http_t
*http
, /* I - HTTP connection to server */
598 const char *dest
) /* I - Destination */
600 ipp_t
*request
, /* IPP Request */
601 *response
; /* IPP Response */
602 ipp_attribute_t
*attr
; /* Current attribute */
603 ipp_pstate_t state
; /* Printer state */
604 char uri
[HTTP_MAX_URI
]; /* Printer URI */
611 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
615 * attributes-natural-language
619 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
621 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
622 "localhost", 0, "/printers/%s", dest
);
623 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
624 "printer-uri", NULL
, uri
);
627 * Do the request and get back a response...
630 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
632 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
634 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
639 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
640 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
642 state
= IPP_PRINTER_STOPPED
;
646 case IPP_PRINTER_IDLE
:
647 _cupsLangPrintf(stdout
, _("%s is ready\n"), dest
);
649 case IPP_PRINTER_PROCESSING
:
650 _cupsLangPrintf(stdout
, _("%s is ready and printing\n"),
653 case IPP_PRINTER_STOPPED
:
654 _cupsLangPrintf(stdout
, _("%s is not ready\n"), dest
);
661 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
666 * 'usage()' - Show program usage.
672 _cupsLangPuts(stderr
,
673 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
674 "[-l] [+interval]\n"));
680 * End of "$Id: lpq.c 5345 2006-03-28 16:00:17Z mike $".