2 * "$Id: lpq.c,v 1.18 2001/05/06 00:11:22 mike Exp $"
4 * "lpq" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2001 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-3111 USA
20 * Voice: (301) 373-9603
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.
32 * Include necessary headers...
36 * Include necessary headers...
43 #include <cups/cups.h>
44 #include <cups/language.h>
45 #include <cups/debug.h>
52 static int show_jobs(http_t
*, const char *, const char *, const int,
54 static void show_printer(http_t
*, const char *);
58 * 'main()' - Parse options and commands.
62 main(int argc
, /* I - Number of command-line arguments */
63 char *argv
[]) /* I - Command-line arguments */
65 int i
; /* Looping var */
66 http_t
*http
; /* Connection to server */
67 const char *dest
, /* Desired printer */
68 *user
; /* Desired user */
69 char *instance
; /* Printer instance */
70 int id
, /* Desired job ID */
71 interval
, /* Reporting interval */
72 longstatus
; /* Show file details */
73 int num_dests
; /* Number of destinations */
74 cups_dest_t
*dests
; /* Destinations */
75 http_encryption_t encryption
; /* Encryption? */
79 * Connect to the scheduler...
82 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
83 cupsEncryption())) == NULL
)
85 fputs("lpq: Unable to contact server!\n", stderr
);
90 * Check for command-line options...
99 num_dests
= cupsGetDests(&dests
);
101 for (i
= 0; i
< num_dests
; i
++)
102 if (dests
[i
].is_default
)
103 dest
= dests
[i
].name
;
105 for (i
= 1; i
< argc
; i
++)
106 if (argv
[i
][0] == '+')
107 interval
= atoi(argv
[i
] + 1);
108 else if (argv
[i
][0] == '-')
112 case 'E' : /* Encrypt */
114 encryption
= HTTP_ENCRYPT_REQUIRED
;
117 httpEncryption(http
, encryption
);
119 fprintf(stderr
, "%s: Sorry, no encryption support compiled in!\n",
121 #endif /* HAVE_LIBSSL */
124 case 'P' : /* Printer */
133 if ((instance
= strchr(dest
, '/')) != NULL
)
137 case 'a' : /* All printers */
141 case 'l' : /* Long status */
146 fputs("Usage: lpq [-P dest] [-l] [+interval]\n", stderr
);
148 cupsFreeDests(num_dests
, dests
);
152 else if (isdigit(argv
[i
][0]))
158 * Show the status in a loop...
164 show_printer(http
, dest
);
166 i
= show_jobs(http
, dest
, user
, id
, longstatus
);
178 * Close the connection to the server and return...
181 cupsFreeDests(num_dests
, dests
);
189 * 'show_jobs()' - Show jobs.
192 static int /* O - Number of jobs in queue */
193 show_jobs(http_t
*http
, /* I - HTTP connection to server */
194 const char *dest
, /* I - Destination */
195 const char *user
, /* I - User */
196 const int id
, /* I - Job ID */
197 const int longstatus
)/* I - 1 if long report desired */
199 ipp_t
*request
, /* IPP Request */
200 *response
; /* IPP Response */
201 ipp_attribute_t
*attr
; /* Current attribute */
202 cups_lang_t
*language
; /* Default language */
203 const char *jobdest
, /* Pointer into job-printer-uri */
204 *jobuser
, /* Pointer to job-originating-user-name */
205 *jobname
; /* Pointer to job-name */
206 ipp_jstate_t jobstate
; /* job-state */
207 int jobid
, /* job-id */
208 jobsize
, /* job-k-octets */
210 jobpriority
, /* job-priority */
212 jobcount
, /* Number of jobs */
213 jobcopies
, /* Number of copies */
214 rank
; /* Rank of job */
215 char resource
[1024]; /* Resource string */
216 char rankstr
[255]; /* Rank string */
217 char namestr
[1024]; /* Job name string */
218 static const char *ranks
[10] =/* Ranking strings */
233 DEBUG_printf(("show_jobs(%08x, %08x, %08x, %d, %d)\n", http
, dest
, user
, id
,
240 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
241 * the following attributes:
244 * attributes-natural-language
245 * job-uri or printer-uri
250 request
->request
.op
.operation_id
= id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
251 request
->request
.op
.request_id
= 1;
253 language
= cupsLangDefault();
255 attr
= ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
256 "attributes-charset", NULL
, cupsLangEncoding(language
));
258 attr
= ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
259 "attributes-natural-language", NULL
, language
->language
);
264 sprintf(resource
, "ipp://localhost/jobs/%d", id
);
266 strcpy(resource
, "ipp://localhost/jobs");
268 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri",
273 snprintf(resource
, sizeof(resource
), "ipp://localhost/printers/%s", dest
);
275 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
281 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
282 "requesting-user-name", NULL
, user
);
283 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
287 * Do the request and get back a response...
292 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
294 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
296 fprintf(stderr
, "lpq: get-jobs failed: %s\n",
297 ippErrorString(response
->request
.status
.status_code
));
305 * Loop through the job list and display them...
308 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
311 * Skip leading attributes until we hit a job...
314 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_JOB
)
321 * Pull the needed attributes from this job...
329 jobstate
= IPP_JOB_PENDING
;
330 jobname
= "untitled";
335 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
337 if (strcmp(attr
->name
, "job-id") == 0 &&
338 attr
->value_tag
== IPP_TAG_INTEGER
)
339 jobid
= attr
->values
[0].integer
;
341 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
342 attr
->value_tag
== IPP_TAG_INTEGER
)
343 jobsize
= attr
->values
[0].integer
* 1024;
346 if (strcmp(attr
->name
, "job-priority") == 0 &&
347 attr
->value_tag
== IPP_TAG_INTEGER
)
348 jobpriority
= attr
->values
[0].integer
;
351 if (strcmp(attr
->name
, "job-state") == 0 &&
352 attr
->value_tag
== IPP_TAG_ENUM
)
353 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
355 if (strcmp(attr
->name
, "job-printer-uri") == 0 &&
356 attr
->value_tag
== IPP_TAG_URI
)
357 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
360 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
361 attr
->value_tag
== IPP_TAG_NAME
)
362 jobuser
= attr
->values
[0].string
.text
;
364 if (strcmp(attr
->name
, "job-name") == 0 &&
365 attr
->value_tag
== IPP_TAG_NAME
)
366 jobname
= attr
->values
[0].string
.text
;
368 if (strcmp(attr
->name
, "copies") == 0 &&
369 attr
->value_tag
== IPP_TAG_INTEGER
)
370 jobcopies
= attr
->values
[0].integer
;
376 * See if we have everything needed...
379 if (jobdest
== NULL
|| jobid
== 0)
387 if (!longstatus
&& jobcount
== 0)
389 puts("Rank Owner Pri Job Files Total Size");
391 puts("Rank Owner Job File(s) Total Size");
400 if (jobstate
== IPP_JOB_PROCESSING
)
401 strcpy(rankstr
, "active");
404 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
413 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
417 strncpy(namestr
, jobname
, sizeof(namestr
) - 1);
418 namestr
[sizeof(namestr
) - 1] = '\0';
421 printf("%s: %-34.34s[job %d localhost]\n", jobuser
, rankstr
, jobid
);
422 printf(" %-40.40s%d bytes\n", namestr
, jobsize
);
426 printf("%-6s %-10.10s %-4d %-10d %-27.27s %d bytes\n", rankstr
, jobuser
,
427 jobpriority
, jobid
, jobname
, jobsize
);
429 printf("%-7s %-8.8s%-8d%-32.32s%d bytes\n", rankstr
, jobuser
,
430 jobid
, jobname
, jobsize
);
441 fprintf(stderr
, "lpq: get-jobs failed: %s\n", ippErrorString(cupsLastError()));
453 * 'show_printer()' - Show printer status.
457 show_printer(http_t
*http
, /* I - HTTP connection to server */
458 const char *dest
) /* I - Destination */
460 ipp_t
*request
, /* IPP Request */
461 *response
; /* IPP Response */
462 ipp_attribute_t
*attr
; /* Current attribute */
463 cups_lang_t
*language
; /* Default language */
464 ipp_pstate_t state
; /* Printer state */
465 char uri
[HTTP_MAX_URI
];
473 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
477 * attributes-natural-language
483 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
484 request
->request
.op
.request_id
= 1;
486 language
= cupsLangDefault();
488 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
489 "attributes-charset", NULL
, cupsLangEncoding(language
));
491 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
492 "attributes-natural-language", NULL
, language
->language
);
494 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", dest
);
495 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
496 "printer-uri", NULL
, uri
);
499 * Do the request and get back a response...
502 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
504 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
506 fprintf(stderr
, "lpq: get-printer-attributes failed: %s\n",
507 ippErrorString(response
->request
.status
.status_code
));
512 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
513 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
515 state
= IPP_PRINTER_STOPPED
;
519 case IPP_PRINTER_IDLE
:
520 printf("%s is ready\n", dest
);
522 case IPP_PRINTER_PROCESSING
:
523 printf("%s is ready and printing\n", dest
);
525 case IPP_PRINTER_STOPPED
:
526 printf("%s is not ready\n", dest
);
533 fprintf(stderr
, "lpq: get-printer-attributes failed: %s\n",
534 ippErrorString(cupsLastError()));
539 * End of "$Id: lpq.c,v 1.18 2001/05/06 00:11:22 mike Exp $".