2 * "$Id: lpq.c 5023 2006-01-29 14:39:44Z 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 int show_jobs(const char *, http_t
*, const char *,
53 const char *, const int, const int);
54 static void show_printer(const char *, http_t
*, const char *);
55 static void usage(void);
59 * 'main()' - Parse options and commands.
63 main(int argc
, /* I - Number of command-line arguments */
64 char *argv
[]) /* I - Command-line arguments */
66 int i
; /* Looping var */
67 http_t
*http
; /* Connection to server */
68 const char *dest
, /* Desired printer */
69 *user
, /* Desired user */
70 *val
; /* Environment variable name */
71 char *instance
; /* Printer instance */
72 int id
, /* Desired job ID */
73 all
, /* All printers */
74 interval
, /* Reporting interval */
75 longstatus
; /* Show file details */
76 int num_dests
; /* Number of destinations */
77 cups_dest_t
*dests
; /* Destinations */
78 cups_lang_t
*language
; /* Language */
80 http_encryption_t encryption
; /* Encryption? */
84 language
= cupsLangDefault();
87 * Connect to the scheduler...
90 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
91 cupsEncryption())) == NULL
)
93 _cupsLangPrintf(stderr
, _("%s: Unable to contact server!\n"), argv
[0]);
98 * Check for command-line options...
107 num_dests
= cupsGetDests(&dests
);
109 for (i
= 1; i
< argc
; i
++)
110 if (argv
[i
][0] == '+')
111 interval
= atoi(argv
[i
] + 1);
112 else if (argv
[i
][0] == '-')
116 case 'E' : /* Encrypt */
118 encryption
= HTTP_ENCRYPT_REQUIRED
;
121 httpEncryption(http
, encryption
);
123 _cupsLangPrintf(stderr
,
124 _("%s: Sorry, no encryption support compiled in!\n"),
126 #endif /* HAVE_SSL */
129 case 'U' : /* Username */
130 if (argv
[i
][2] != '\0')
131 cupsSetUser(argv
[i
] + 2);
137 _cupsLangPrintf(stderr
,
138 _("%s: Error - expected username after "
144 cupsSetUser(argv
[i
]);
148 case 'P' : /* Printer */
158 cupsFreeDests(num_dests
, dests
);
166 if ((instance
= strchr(dest
, '/')) != NULL
)
169 if (cupsGetDest(dest
, instance
, num_dests
, dests
) == NULL
)
172 _cupsLangPrintf(stderr
,
173 _("%s: Error - unknown destination \"%s/%s\"!\n"),
174 argv
[0], dest
, instance
);
176 _cupsLangPrintf(stderr
,
177 _("%s: Unknown destination \"%s\"!\n"),
184 case 'a' : /* All printers */
188 case 'h' : /* Connect to host */
192 if (argv
[i
][2] != '\0')
193 cupsSetServer(argv
[i
] + 2);
200 _cupsLangPrintf(stderr
,
201 _("%s: Error - expected hostname after "
207 cupsSetServer(argv
[i
]);
211 case 'l' : /* Long status */
217 cupsFreeDests(num_dests
, dests
);
223 else if (isdigit(argv
[i
][0] & 255))
228 if (dest
== NULL
&& !all
)
230 for (i
= 0; i
< num_dests
; i
++)
231 if (dests
[i
].is_default
)
232 dest
= dests
[i
].name
;
238 if ((dest
= getenv("LPDEST")) == NULL
)
240 if ((dest
= getenv("PRINTER")) != NULL
)
242 if (!strcmp(dest
, "lp"))
251 if (dest
&& !cupsGetDest(dest
, NULL
, num_dests
, dests
))
252 _cupsLangPrintf(stderr
,
253 _("%s: error - %s environment variable names "
254 "non-existent destination \"%s\"!\n"),
257 _cupsLangPrintf(stderr
,
258 _("%s: error - no default destination available.\n"),
261 cupsFreeDests(num_dests
, dests
);
267 * Show the status in a loop...
273 show_printer(argv
[0], http
, dest
);
275 i
= show_jobs(argv
[0], http
, dest
, user
, id
, longstatus
);
287 * Close the connection to the server and return...
290 cupsFreeDests(num_dests
, dests
);
298 * 'show_jobs()' - Show jobs.
301 static int /* O - Number of jobs in queue */
302 show_jobs(const char *command
, /* I - Command name */
303 http_t
*http
, /* I - HTTP connection to server */
304 const char *dest
, /* I - Destination */
305 const char *user
, /* I - User */
306 const int id
, /* I - Job ID */
307 const int longstatus
) /* I - 1 if long report desired */
309 ipp_t
*request
, /* IPP Request */
310 *response
; /* IPP Response */
311 ipp_attribute_t
*attr
; /* Current attribute */
312 const char *jobdest
, /* Pointer into job-printer-uri */
313 *jobuser
, /* Pointer to job-originating-user-name */
314 *jobname
; /* Pointer to job-name */
315 ipp_jstate_t jobstate
; /* job-state */
316 int jobid
, /* job-id */
317 jobsize
, /* job-k-octets */
319 jobpriority
, /* job-priority */
321 jobcount
, /* Number of jobs */
322 jobcopies
, /* Number of copies */
323 rank
; /* Rank of job */
324 char resource
[1024]; /* Resource string */
325 char rankstr
[255]; /* Rank string */
326 char namestr
[1024]; /* Job name string */
327 static const char *ranks
[10] = /* Ranking strings */
342 DEBUG_printf(("show_jobs(%08x, %08x, %08x, %d, %d)\n", http
, dest
, user
, id
,
349 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
350 * the following attributes:
353 * attributes-natural-language
354 * job-uri or printer-uri
357 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
362 sprintf(resource
, "ipp://localhost/jobs/%d", id
);
364 strcpy(resource
, "ipp://localhost/jobs");
366 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
371 httpAssembleURIf(HTTP_URI_CODING_ALL
, resource
, sizeof(resource
), "ipp",
372 NULL
, "localhost", 0, "/printers/%s", dest
);
374 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
380 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
381 "requesting-user-name", NULL
, user
);
382 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
386 * Do the request and get back a response...
391 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
393 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
395 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
403 * Loop through the job list and display them...
406 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
409 * Skip leading attributes until we hit a job...
412 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
419 * Pull the needed attributes from this job...
427 jobstate
= IPP_JOB_PENDING
;
428 jobname
= "untitled";
433 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
435 if (!strcmp(attr
->name
, "job-id") &&
436 attr
->value_tag
== IPP_TAG_INTEGER
)
437 jobid
= attr
->values
[0].integer
;
439 if (!strcmp(attr
->name
, "job-k-octets") &&
440 attr
->value_tag
== IPP_TAG_INTEGER
)
441 jobsize
= attr
->values
[0].integer
;
444 if (!strcmp(attr
->name
, "job-priority") &&
445 attr
->value_tag
== IPP_TAG_INTEGER
)
446 jobpriority
= attr
->values
[0].integer
;
449 if (!strcmp(attr
->name
, "job-state") &&
450 attr
->value_tag
== IPP_TAG_ENUM
)
451 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
453 if (!strcmp(attr
->name
, "job-printer-uri") &&
454 attr
->value_tag
== IPP_TAG_URI
)
455 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
458 if (!strcmp(attr
->name
, "job-originating-user-name") &&
459 attr
->value_tag
== IPP_TAG_NAME
)
460 jobuser
= attr
->values
[0].string
.text
;
462 if (!strcmp(attr
->name
, "job-name") &&
463 attr
->value_tag
== IPP_TAG_NAME
)
464 jobname
= attr
->values
[0].string
.text
;
466 if (!strcmp(attr
->name
, "copies") &&
467 attr
->value_tag
== IPP_TAG_INTEGER
)
468 jobcopies
= attr
->values
[0].integer
;
474 * See if we have everything needed...
477 if (jobdest
== NULL
|| jobid
== 0)
485 if (!longstatus
&& jobcount
== 0)
487 _cupsLangPuts(stdout
,
488 _("Rank Owner Pri Job Files"
491 _cupsLangPuts(stdout
,
492 _("Rank Owner Job File(s)"
502 if (jobstate
== IPP_JOB_PROCESSING
)
503 strcpy(rankstr
, "active");
507 * Make the rank show the "correct" suffix for each number
508 * (11-13 are the only special cases, for English anyways...)
511 if ((rank
% 100) >= 11 && (rank
% 100) <= 13)
512 snprintf(rankstr
, sizeof(rankstr
), "%dth", rank
);
514 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
521 _cupsLangPuts(stdout
, "\n");
524 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
527 strlcpy(namestr
, jobname
, sizeof(namestr
));
529 _cupsLangPrintf(stdout
, _("%s: %-33.33s [job %d localhost]\n"),
530 jobuser
, rankstr
, jobid
);
531 _cupsLangPrintf(stdout
, _(" %-39.39s %.0f bytes\n"),
532 namestr
, 1024.0 * jobsize
);
536 _cupsLangPrintf(stdout
,
537 _("%-6s %-10.10s %-4d %-10d %-27.27s %.0f bytes\n"),
538 rankstr
, jobuser
, jobpriority
, jobid
, jobname
,
541 _cupsLangPrintf(stdout
,
542 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n"),
543 rankstr
, jobuser
, jobid
, jobname
, 1024.0 * jobsize
);
554 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
559 _cupsLangPuts(stdout
, _("no entries\n"));
566 * 'show_printer()' - Show printer status.
570 show_printer(const char *command
, /* I - Command name */
571 http_t
*http
, /* I - HTTP connection to server */
572 const char *dest
) /* I - Destination */
574 ipp_t
*request
, /* IPP Request */
575 *response
; /* IPP Response */
576 ipp_attribute_t
*attr
; /* Current attribute */
577 ipp_pstate_t state
; /* Printer state */
578 char uri
[HTTP_MAX_URI
]; /* Printer URI */
585 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
589 * attributes-natural-language
593 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
595 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
596 "localhost", 0, "/printers/%s", dest
);
597 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
598 "printer-uri", NULL
, uri
);
601 * Do the request and get back a response...
604 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
606 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
608 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
613 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
614 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
616 state
= IPP_PRINTER_STOPPED
;
620 case IPP_PRINTER_IDLE
:
621 _cupsLangPrintf(stdout
, _("%s is ready\n"), dest
);
623 case IPP_PRINTER_PROCESSING
:
624 _cupsLangPrintf(stdout
, _("%s is ready and printing\n"),
627 case IPP_PRINTER_STOPPED
:
628 _cupsLangPrintf(stdout
, _("%s is not ready\n"), dest
);
635 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
640 * 'usage()' - Show program usage.
646 _cupsLangPuts(stderr
,
647 _("Usage: lpq [-P dest] [-U username] [-h hostname[:port]] "
648 "[-l] [+interval]\n"));
654 * End of "$Id: lpq.c 5023 2006-01-29 14:39:44Z mike $".