2 * "$Id: lpq.c 7460 2008-04-16 02:19:54Z mike $"
4 * "lpq" command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * main() - Parse options and commands.
18 * show_jobs() - Show jobs.
19 * show_printer() - Show printer status.
20 * usage() - Show program usage.
24 * Include necessary headers...
28 * Include necessary headers...
33 #include <cups/string.h>
34 #include <cups/cups.h>
35 #include <cups/i18n.h>
36 #include <cups/debug.h>
43 static http_t
*connect_server(const char *, http_t
*);
44 static int show_jobs(const char *, http_t
*, const char *,
45 const char *, const int, const int);
46 static void show_printer(const char *, http_t
*, const char *);
47 static void usage(void);
51 * 'main()' - Parse options and commands.
55 main(int argc
, /* I - Number of command-line arguments */
56 char *argv
[]) /* I - Command-line arguments */
58 int i
; /* Looping var */
59 http_t
*http
; /* Connection to server */
60 const char *dest
, /* Desired printer */
61 *user
, /* Desired user */
62 *val
; /* Environment variable name */
63 char *instance
; /* Printer instance */
64 int id
, /* Desired job ID */
65 all
, /* All printers */
66 interval
, /* Reporting interval */
67 longstatus
; /* Show file details */
68 int num_dests
; /* Number of destinations */
69 cups_dest_t
*dests
; /* Destinations */
75 * Check for command-line options...
88 for (i
= 1; i
< argc
; i
++)
89 if (argv
[i
][0] == '+')
90 interval
= atoi(argv
[i
] + 1);
91 else if (argv
[i
][0] == '-')
95 case 'E' : /* Encrypt */
97 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
100 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
102 _cupsLangPrintf(stderr
,
103 _("%s: Sorry, no encryption support compiled in!\n"),
105 #endif /* HAVE_SSL */
108 case 'U' : /* Username */
109 if (argv
[i
][2] != '\0')
110 cupsSetUser(argv
[i
] + 2);
116 _cupsLangPrintf(stderr
,
117 _("%s: Error - expected username after "
123 cupsSetUser(argv
[i
]);
127 case 'P' : /* Printer */
137 cupsFreeDests(num_dests
, dests
);
145 if ((instance
= strchr(dest
, '/')) != NULL
)
148 http
= connect_server(argv
[0], http
);
151 num_dests
= cupsGetDests2(http
, &dests
);
153 if (cupsGetDest(dest
, instance
, num_dests
, dests
) == NULL
)
156 _cupsLangPrintf(stderr
,
157 _("%s: Error - unknown destination \"%s/%s\"!\n"),
158 argv
[0], dest
, instance
);
160 _cupsLangPrintf(stderr
,
161 _("%s: Unknown destination \"%s\"!\n"),
168 case 'a' : /* All printers */
172 case 'h' : /* Connect to host */
179 if (argv
[i
][2] != '\0')
180 cupsSetServer(argv
[i
] + 2);
187 _cupsLangPrintf(stderr
,
188 _("%s: Error - expected hostname after "
194 cupsSetServer(argv
[i
]);
198 case 'l' : /* Long status */
204 cupsFreeDests(num_dests
, dests
);
210 else if (isdigit(argv
[i
][0] & 255))
215 http
= connect_server(argv
[0], http
);
217 if (dest
== NULL
&& !all
)
220 num_dests
= cupsGetDests2(http
, &dests
);
222 for (i
= 0; i
< num_dests
; i
++)
223 if (dests
[i
].is_default
)
224 dest
= dests
[i
].name
;
230 if ((dest
= getenv("LPDEST")) == NULL
)
232 if ((dest
= getenv("PRINTER")) != NULL
)
234 if (!strcmp(dest
, "lp"))
243 if (dest
&& !cupsGetDest(dest
, NULL
, num_dests
, dests
))
244 _cupsLangPrintf(stderr
,
245 _("%s: error - %s environment variable names "
246 "non-existent destination \"%s\"!\n"),
249 _cupsLangPrintf(stderr
,
250 _("%s: error - no default destination available.\n"),
253 cupsFreeDests(num_dests
, dests
);
259 * Show the status in a loop...
265 show_printer(argv
[0], http
, dest
);
267 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
279 * Close the connection to the server and return...
282 cupsFreeDests(num_dests
, dests
);
290 * 'connect_server()' - Connect to the server as necessary...
293 static http_t
* /* O - New HTTP connection */
294 connect_server(const char *command
, /* I - Command name */
295 http_t
*http
) /* I - Current HTTP connection */
299 http
= httpConnectEncrypt(cupsServer(), ippPort(),
304 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server\n"), command
);
314 * 'show_jobs()' - Show jobs.
317 static int /* O - Number of jobs in queue */
318 show_jobs(const char *command
, /* I - Command name */
319 http_t
*http
, /* I - HTTP connection to server */
320 const char *dest
, /* I - Destination */
321 const char *user
, /* I - User */
322 const int id
, /* I - Job ID */
323 const int longstatus
) /* I - 1 if long report desired */
325 ipp_t
*request
, /* IPP Request */
326 *response
; /* IPP Response */
327 ipp_attribute_t
*attr
; /* Current attribute */
328 const char *jobdest
, /* Pointer into job-printer-uri */
329 *jobuser
, /* Pointer to job-originating-user-name */
330 *jobname
; /* Pointer to job-name */
331 ipp_jstate_t jobstate
; /* job-state */
332 int jobid
, /* job-id */
333 jobsize
, /* job-k-octets */
335 jobpriority
, /* job-priority */
337 jobcount
, /* Number of jobs */
338 jobcopies
, /* Number of copies */
339 rank
; /* Rank of job */
340 char resource
[1024]; /* Resource string */
341 char rankstr
[255]; /* Rank string */
342 char namestr
[1024]; /* Job name string */
343 static const char *ranks
[10] = /* Ranking strings */
358 DEBUG_printf(("show_jobs(http=%p, dest=%p, user=%p, id=%d, longstatus%d)\n",
359 http
, dest
, user
, id
, longstatus
));
365 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
366 * the following attributes:
369 * attributes-natural-language
370 * job-uri or printer-uri
373 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
377 snprintf(resource
, sizeof(resource
), "ipp://localhost/jobs/%d", id
);
378 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
383 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
384 NULL
, "localhost", 0, "/printers/%s", dest
);
386 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
390 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
391 NULL
, "ipp://localhost/");
395 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
396 "requesting-user-name", NULL
, user
);
397 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
401 * Do the request and get back a response...
406 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
408 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
410 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
418 * Loop through the job list and display them...
421 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
424 * Skip leading attributes until we hit a job...
427 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
434 * Pull the needed attributes from this job...
442 jobstate
= IPP_JOB_PENDING
;
443 jobname
= "untitled";
448 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
450 if (!strcmp(attr
->name
, "job-id") &&
451 attr
->value_tag
== IPP_TAG_INTEGER
)
452 jobid
= attr
->values
[0].integer
;
454 if (!strcmp(attr
->name
, "job-k-octets") &&
455 attr
->value_tag
== IPP_TAG_INTEGER
)
456 jobsize
= attr
->values
[0].integer
;
459 if (!strcmp(attr
->name
, "job-priority") &&
460 attr
->value_tag
== IPP_TAG_INTEGER
)
461 jobpriority
= attr
->values
[0].integer
;
464 if (!strcmp(attr
->name
, "job-state") &&
465 attr
->value_tag
== IPP_TAG_ENUM
)
466 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
468 if (!strcmp(attr
->name
, "job-printer-uri") &&
469 attr
->value_tag
== IPP_TAG_URI
)
470 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
473 if (!strcmp(attr
->name
, "job-originating-user-name") &&
474 attr
->value_tag
== IPP_TAG_NAME
)
475 jobuser
= attr
->values
[0].string
.text
;
477 if (!strcmp(attr
->name
, "job-name") &&
478 attr
->value_tag
== IPP_TAG_NAME
)
479 jobname
= attr
->values
[0].string
.text
;
481 if (!strcmp(attr
->name
, "copies") &&
482 attr
->value_tag
== IPP_TAG_INTEGER
)
483 jobcopies
= attr
->values
[0].integer
;
489 * See if we have everything needed...
492 if (jobdest
== NULL
|| jobid
== 0)
500 if (!longstatus
&& jobcount
== 0)
502 _cupsLangPuts(stdout
,
503 _("Rank Owner Pri Job Files"
506 _cupsLangPuts(stdout
,
507 _("Rank Owner Job File(s)"
517 if (jobstate
== IPP_JOB_PROCESSING
)
518 strcpy(rankstr
, "active");
522 * Make the rank show the "correct" suffix for each number
523 * (11-13 are the only special cases, for English anyways...)
526 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
527 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
529 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
536 _cupsLangPuts(stdout
, "\n");
539 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
542 strlcpy(namestr
, jobname
, sizeof(namestr
));
544 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]\n"),
545 jobuser
, rankstr
, jobid
);
546 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes\n"),
547 namestr
, 1024.0 * jobsize
);
551 _cupsLangPrintf(stdout
,
552 _("%-6s %-10.10s %-4d %-10d %-27.27s %.0f bytes\n"),
553 rankstr
, jobuser
, jobpriority
, jobid
, jobname
,
556 _cupsLangPrintf(stdout
,
557 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n"),
558 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
569 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
574 _cupsLangPuts(stdout
, _("no entries\n"));
581 * 'show_printer()' - Show printer status.
585 show_printer(const char *command
, /* I - Command name */
586 http_t
*http
, /* I - HTTP connection to server */
587 const char *dest
) /* I - Destination */
589 ipp_t
*request
, /* IPP Request */
590 *response
; /* IPP Response */
591 ipp_attribute_t
*attr
; /* Current attribute */
592 ipp_pstate_t state
; /* Printer state */
593 char uri
[HTTP_MAX_URI
]; /* Printer URI */
600 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
604 * attributes-natural-language
608 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
610 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
611 "localhost", 0, "/printers/%s", dest
);
612 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
613 "printer-uri", NULL
, uri
);
616 * Do the request and get back a response...
619 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
621 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
623 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
628 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
629 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
631 state
= IPP_PRINTER_STOPPED
;
635 case IPP_PRINTER_IDLE
:
636 _cupsLangPrintf(stdout
, _("%s is ready\n"), dest
);
638 case IPP_PRINTER_PROCESSING
:
639 _cupsLangPrintf(stdout
, _("%s is ready and printing\n"),
642 case IPP_PRINTER_STOPPED
:
643 _cupsLangPrintf(stdout
, _("%s is not ready\n"), dest
);
650 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
655 * 'usage()' - Show program usage.
661 _cupsLangPuts(stderr
,
662 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
663 "[-l] [+interval]\n"));
669 * End of "$Id: lpq.c 7460 2008-04-16 02:19:54Z mike $".