]> git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpq.c
Load cups into easysw/current.
[thirdparty/cups.git] / berkeley / lpq.c
1 /*
2 * "$Id: lpq.c 4906 2006-01-10 20:53:28Z mike $"
3 *
4 * "lpq" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Parse options and commands.
27 * show_jobs() - Show jobs.
28 * show_printer() - Show printer status.
29 * usage() - Show program usage.
30 */
31
32 /*
33 * Include necessary headers...
34 */
35
36 /*
37 * Include necessary headers...
38 */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <cups/string.h>
43 #include <cups/cups.h>
44 #include <cups/i18n.h>
45 #include <cups/debug.h>
46
47
48 /*
49 * Local functions...
50 */
51
52 static int show_jobs(http_t *, const char *, const char *, const int,
53 const int);
54 static void show_printer(http_t *, const char *);
55 static void usage(void);
56
57
58 /*
59 * 'main()' - Parse options and commands.
60 */
61
62 int
63 main(int argc, /* I - Number of command-line arguments */
64 char *argv[]) /* I - Command-line arguments */
65 {
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 */
79 #ifdef HAVE_SSL
80 http_encryption_t encryption; /* Encryption? */
81 #endif /* HAVE_SSL */
82
83
84 language = cupsLangDefault();
85
86 /*
87 * Connect to the scheduler...
88 */
89
90 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
91 cupsEncryption())) == NULL)
92 {
93 _cupsLangPuts(stderr, language,
94 _("lpq: Unable to contact server!\n"));
95 return (1);
96 }
97
98 /*
99 * Check for command-line options...
100 */
101
102 dest = NULL;
103 user = NULL;
104 id = 0;
105 interval = 0;
106 longstatus = 0;
107 all = 0;
108 num_dests = cupsGetDests(&dests);
109
110 for (i = 1; i < argc; i ++)
111 if (argv[i][0] == '+')
112 interval = atoi(argv[i] + 1);
113 else if (argv[i][0] == '-')
114 {
115 switch (argv[i][1])
116 {
117 case 'E' : /* Encrypt */
118 #ifdef HAVE_SSL
119 encryption = HTTP_ENCRYPT_REQUIRED;
120
121 if (http)
122 httpEncryption(http, encryption);
123 #else
124 _cupsLangPrintf(stderr, language,
125 _("%s: Sorry, no encryption support compiled in!\n"),
126 argv[0]);
127 #endif /* HAVE_SSL */
128 break;
129
130 case 'P' : /* Printer */
131 if (argv[i][2])
132 dest = argv[i] + 2;
133 else
134 {
135 i ++;
136
137 if (i >= argc)
138 {
139 httpClose(http);
140 cupsFreeDests(num_dests, dests);
141
142 usage();
143 }
144
145 dest = argv[i];
146 }
147
148 if ((instance = strchr(dest, '/')) != NULL)
149 *instance++ = '\0';
150
151 if (cupsGetDest(dest, instance, num_dests, dests) == NULL)
152 {
153 if (instance)
154 _cupsLangPrintf(stderr, language,
155 _("lpq: Unknown destination \"%s/%s\"!\n"),
156 dest, instance);
157 else
158 _cupsLangPrintf(stderr, language,
159 _("lpq: Unknown destination \"%s\"!\n"), dest);
160
161 return (1);
162 }
163 break;
164
165 case 'a' : /* All printers */
166 all = 1;
167 break;
168
169 case 'l' : /* Long status */
170 longstatus = 1;
171 break;
172
173 default :
174 httpClose(http);
175 cupsFreeDests(num_dests, dests);
176
177 usage();
178 break;
179 }
180 }
181 else if (isdigit(argv[i][0] & 255))
182 id = atoi(argv[i]);
183 else
184 user = argv[i];
185
186 if (dest == NULL && !all)
187 {
188 for (i = 0; i < num_dests; i ++)
189 if (dests[i].is_default)
190 dest = dests[i].name;
191
192 if (dest == NULL)
193 {
194 val = NULL;
195
196 if ((dest = getenv("LPDEST")) == NULL)
197 {
198 if ((dest = getenv("PRINTER")) != NULL)
199 {
200 if (!strcmp(dest, "lp"))
201 dest = NULL;
202 else
203 val = "PRINTER";
204 }
205 }
206 else
207 val = "LPDEST";
208
209 if (dest && !cupsGetDest(dest, NULL, num_dests, dests))
210 _cupsLangPrintf(stderr, language,
211 _("lp: error - %s environment variable names "
212 "non-existent destination \"%s\"!\n"),
213 val, dest);
214 else
215 _cupsLangPuts(stderr, language,
216 _("lpq: error - no default destination available.\n"));
217 httpClose(http);
218 cupsFreeDests(num_dests, dests);
219 return (1);
220 }
221 }
222
223 /*
224 * Show the status in a loop...
225 */
226
227 for (;;)
228 {
229 if (dest)
230 show_printer(http, dest);
231
232 i = show_jobs(http, dest, user, id, longstatus);
233
234 if (i && interval)
235 {
236 fflush(stdout);
237 sleep(interval);
238 }
239 else
240 break;
241 }
242
243 /*
244 * Close the connection to the server and return...
245 */
246
247 cupsFreeDests(num_dests, dests);
248 httpClose(http);
249
250 return (0);
251 }
252
253
254 /*
255 * 'show_jobs()' - Show jobs.
256 */
257
258 static int /* O - Number of jobs in queue */
259 show_jobs(http_t *http, /* I - HTTP connection to server */
260 const char *dest, /* I - Destination */
261 const char *user, /* I - User */
262 const int id, /* I - Job ID */
263 const int longstatus)/* I - 1 if long report desired */
264 {
265 ipp_t *request, /* IPP Request */
266 *response; /* IPP Response */
267 ipp_attribute_t *attr; /* Current attribute */
268 cups_lang_t *language; /* Default language */
269 const char *jobdest, /* Pointer into job-printer-uri */
270 *jobuser, /* Pointer to job-originating-user-name */
271 *jobname; /* Pointer to job-name */
272 ipp_jstate_t jobstate; /* job-state */
273 int jobid, /* job-id */
274 jobsize, /* job-k-octets */
275 #ifdef __osf__
276 jobpriority, /* job-priority */
277 #endif /* __osf__ */
278 jobcount, /* Number of jobs */
279 jobcopies, /* Number of copies */
280 rank; /* Rank of job */
281 char resource[1024]; /* Resource string */
282 char rankstr[255]; /* Rank string */
283 char namestr[1024]; /* Job name string */
284 static const char *ranks[10] =/* Ranking strings */
285 {
286 "th",
287 "st",
288 "nd",
289 "rd",
290 "th",
291 "th",
292 "th",
293 "th",
294 "th",
295 "th"
296 };
297
298
299 DEBUG_printf(("show_jobs(%08x, %08x, %08x, %d, %d)\n", http, dest, user, id,
300 longstatus));
301
302 if (http == NULL)
303 return (0);
304
305 /*
306 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
307 * the following attributes:
308 *
309 * attributes-charset
310 * attributes-natural-language
311 * job-uri or printer-uri
312 */
313
314 request = ippNew();
315
316 request->request.op.operation_id = id ? IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS;
317 request->request.op.request_id = 1;
318
319 language = cupsLangDefault();
320
321 attr = ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
322 "attributes-charset", NULL, cupsLangEncoding(language));
323
324 attr = ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
325 "attributes-natural-language", NULL, language->language);
326
327 if (dest == NULL)
328 {
329 if (id)
330 sprintf(resource, "ipp://localhost/jobs/%d", id);
331 else
332 strcpy(resource, "ipp://localhost/jobs");
333
334 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
335 NULL, resource);
336 }
337 else
338 {
339 httpAssembleURIf(resource, sizeof(resource), "ipp", NULL, "localhost", 0,
340 "/printers/%s", dest);
341
342 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
343 NULL, resource);
344 }
345
346 if (user)
347 {
348 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
349 "requesting-user-name", NULL, user);
350 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
351 }
352
353 /*
354 * Do the request and get back a response...
355 */
356
357 jobcount = 0;
358
359 if ((response = cupsDoRequest(http, request, "/")) != NULL)
360 {
361 if (response->request.status.status_code > IPP_OK_CONFLICT)
362 {
363 _cupsLangPrintf(stderr, language, _("lpq: get-jobs failed: %s\n"),
364 ippErrorString(response->request.status.status_code));
365 ippDelete(response);
366 return (0);
367 }
368
369 rank = 1;
370
371 /*
372 * Loop through the job list and display them...
373 */
374
375 for (attr = response->attrs; attr != NULL; attr = attr->next)
376 {
377 /*
378 * Skip leading attributes until we hit a job...
379 */
380
381 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
382 attr = attr->next;
383
384 if (attr == NULL)
385 break;
386
387 /*
388 * Pull the needed attributes from this job...
389 */
390
391 jobid = 0;
392 jobsize = 0;
393 #ifdef __osf__
394 jobpriority = 50;
395 #endif /* __osf__ */
396 jobstate = IPP_JOB_PENDING;
397 jobname = "untitled";
398 jobuser = NULL;
399 jobdest = NULL;
400 jobcopies = 1;
401
402 while (attr != NULL && attr->group_tag == IPP_TAG_JOB)
403 {
404 if (strcmp(attr->name, "job-id") == 0 &&
405 attr->value_tag == IPP_TAG_INTEGER)
406 jobid = attr->values[0].integer;
407
408 if (strcmp(attr->name, "job-k-octets") == 0 &&
409 attr->value_tag == IPP_TAG_INTEGER)
410 jobsize = attr->values[0].integer;
411
412 #ifdef __osf__
413 if (strcmp(attr->name, "job-priority") == 0 &&
414 attr->value_tag == IPP_TAG_INTEGER)
415 jobpriority = attr->values[0].integer;
416 #endif /* __osf__ */
417
418 if (strcmp(attr->name, "job-state") == 0 &&
419 attr->value_tag == IPP_TAG_ENUM)
420 jobstate = (ipp_jstate_t)attr->values[0].integer;
421
422 if (strcmp(attr->name, "job-printer-uri") == 0 &&
423 attr->value_tag == IPP_TAG_URI)
424 if ((jobdest = strrchr(attr->values[0].string.text, '/')) != NULL)
425 jobdest ++;
426
427 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
428 attr->value_tag == IPP_TAG_NAME)
429 jobuser = attr->values[0].string.text;
430
431 if (strcmp(attr->name, "job-name") == 0 &&
432 attr->value_tag == IPP_TAG_NAME)
433 jobname = attr->values[0].string.text;
434
435 if (strcmp(attr->name, "copies") == 0 &&
436 attr->value_tag == IPP_TAG_INTEGER)
437 jobcopies = attr->values[0].integer;
438
439 attr = attr->next;
440 }
441
442 /*
443 * See if we have everything needed...
444 */
445
446 if (jobdest == NULL || jobid == 0)
447 {
448 if (attr == NULL)
449 break;
450 else
451 continue;
452 }
453
454 if (!longstatus && jobcount == 0)
455 #ifdef __osf__
456 _cupsLangPuts(stdout, language,
457 _("Rank Owner Pri Job Files"
458 " Total Size\n"));
459 #else
460 _cupsLangPuts(stdout, language,
461 _("Rank Owner Job File(s)"
462 " Total Size\n"));
463 #endif /* __osf__ */
464
465 jobcount ++;
466
467 /*
468 * Display the job...
469 */
470
471 if (jobstate == IPP_JOB_PROCESSING)
472 strcpy(rankstr, "active");
473 else
474 {
475 /*
476 * Make the rank show the "correct" suffix for each number
477 * (11-13 are the only special cases, for English anyways...)
478 */
479
480 if ((rank % 100) >= 11 && (rank % 100) <= 13)
481 snprintf(rankstr, sizeof(rankstr), "%dth", rank);
482 else
483 snprintf(rankstr, sizeof(rankstr), "%d%s", rank, ranks[rank % 10]);
484
485 rank ++;
486 }
487
488 if (longstatus)
489 {
490 _cupsLangPuts(stdout, language, "");
491
492 if (jobcopies > 1)
493 snprintf(namestr, sizeof(namestr), "%d copies of %s", jobcopies,
494 jobname);
495 else
496 strlcpy(namestr, jobname, sizeof(namestr));
497
498 _cupsLangPrintf(stdout, language, _("%s: %-33.33s [job %d localhost]\n"),
499 jobuser, rankstr, jobid);
500 _cupsLangPrintf(stdout, language, _(" %-39.39s %.0f bytes\n"),
501 namestr, 1024.0 * jobsize);
502 }
503 else
504 #ifdef __osf__
505 _cupsLangPrintf(stdout, language,
506 _("%-6s %-10.10s %-4d %-10d %-27.27s %.0f bytes\n"),
507 rankstr, jobuser, jobpriority, jobid, jobname,
508 1024.0 * jobsize);
509 #else
510 _cupsLangPrintf(stdout, language,
511 _("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n"),
512 rankstr, jobuser, jobid, jobname, 1024.0 * jobsize);
513 #endif /* __osf */
514
515 if (attr == NULL)
516 break;
517 }
518
519 ippDelete(response);
520 }
521 else
522 {
523 _cupsLangPrintf(stderr, language, _("lpq: get-jobs failed: %s\n"),
524 ippErrorString(cupsLastError()));
525 return (0);
526 }
527
528 if (jobcount == 0)
529 _cupsLangPuts(stdout, language, _("no entries\n"));
530
531 return (jobcount);
532 }
533
534
535 /*
536 * 'show_printer()' - Show printer status.
537 */
538
539 static void
540 show_printer(http_t *http, /* I - HTTP connection to server */
541 const char *dest) /* I - Destination */
542 {
543 ipp_t *request, /* IPP Request */
544 *response; /* IPP Response */
545 ipp_attribute_t *attr; /* Current attribute */
546 cups_lang_t *language; /* Default language */
547 ipp_pstate_t state; /* Printer state */
548 char uri[HTTP_MAX_URI];
549 /* Printer URI */
550
551
552 if (http == NULL)
553 return;
554
555 /*
556 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
557 * attributes:
558 *
559 * attributes-charset
560 * attributes-natural-language
561 * printer-uri
562 */
563
564 request = ippNew();
565
566 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
567 request->request.op.request_id = 1;
568
569 language = cupsLangDefault();
570
571 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
572 "attributes-charset", NULL, cupsLangEncoding(language));
573
574 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
575 "attributes-natural-language", NULL, language->language);
576
577 httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0,
578 "/printers/%s", dest);
579 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
580 "printer-uri", NULL, uri);
581
582 /*
583 * Do the request and get back a response...
584 */
585
586 if ((response = cupsDoRequest(http, request, "/")) != NULL)
587 {
588 if (response->request.status.status_code > IPP_OK_CONFLICT)
589 {
590 _cupsLangPrintf(stderr, language,
591 _("lpq: get-printer-attributes failed: %s\n"),
592 ippErrorString(response->request.status.status_code));
593 ippDelete(response);
594 return;
595 }
596
597 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL)
598 state = (ipp_pstate_t)attr->values[0].integer;
599 else
600 state = IPP_PRINTER_STOPPED;
601
602 switch (state)
603 {
604 case IPP_PRINTER_IDLE :
605 _cupsLangPrintf(stdout, language, _("%s is ready\n"), dest);
606 break;
607 case IPP_PRINTER_PROCESSING :
608 _cupsLangPrintf(stdout, language, _("%s is ready and printing\n"),
609 dest);
610 break;
611 case IPP_PRINTER_STOPPED :
612 _cupsLangPrintf(stdout, language, _("%s is not ready\n"), dest);
613 break;
614 }
615
616 ippDelete(response);
617 }
618 else
619 _cupsLangPrintf(stderr, language,
620 _("lpq: get-printer-attributes failed: %s\n"),
621 ippErrorString(cupsLastError()));
622 }
623
624
625 /*
626 * 'usage()' - Show program usage.
627 */
628
629 static void
630 usage(void)
631 {
632 _cupsLangPuts(stderr, cupsLangDefault(),
633 _("Usage: lpq [-P dest] [-l] [+interval]\n"));
634 exit(1);
635 }
636
637
638 /*
639 * End of "$Id: lpq.c 4906 2006-01-10 20:53:28Z mike $".
640 */