]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
a51f28ec | 2 | * "$Id: lpstat.c 11890 2014-05-22 13:59:21Z msweet $" |
ef416fc2 | 3 | * |
4bdbabf7 | 4 | * "lpstat" command for CUPS. |
ef416fc2 | 5 | * |
a51f28ec | 6 | * Copyright 2007-2014 by Apple Inc. |
4bdbabf7 | 7 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 8 | * |
4bdbabf7 MS |
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/". | |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include <cups/cups-private.h> |
ef416fc2 | 21 | |
22 | ||
23 | /* | |
24 | * Local functions... | |
25 | */ | |
26 | ||
61cf44e2 MS |
27 | static void check_dest(const char *command, const char *name, |
28 | int *num_dests, cups_dest_t **dests); | |
29 | static int match_list(const char *list, const char *name); | |
30 | static int show_accepting(const char *printers, int num_dests, | |
31 | cups_dest_t *dests); | |
32 | static int show_classes(const char *dests); | |
33 | static void show_default(cups_dest_t *dest); | |
34 | static int show_devices(const char *printers, int num_dests, | |
35 | cups_dest_t *dests); | |
36 | static int show_jobs(const char *dests, const char *users, int long_status, | |
37 | int ranking, const char *which); | |
38 | static int show_printers(const char *printers, int num_dests, | |
39 | cups_dest_t *dests, int long_status); | |
40 | static void show_scheduler(void); | |
ef416fc2 | 41 | |
42 | ||
43 | /* | |
44 | * 'main()' - Parse options and show status information. | |
45 | */ | |
46 | ||
47 | int | |
ed486911 | 48 | main(int argc, /* I - Number of command-line arguments */ |
49 | char *argv[]) /* I - Command-line arguments */ | |
ef416fc2 | 50 | { |
ed486911 | 51 | int i, /* Looping var */ |
52 | status; /* Exit status */ | |
ed486911 | 53 | int num_dests; /* Number of user destinations */ |
54 | cups_dest_t *dests; /* User destinations */ | |
55 | int long_status; /* Long status report? */ | |
56 | int ranking; /* Show job ranking? */ | |
57 | const char *which; /* Which jobs to show? */ | |
58 | char op; /* Last operation on command-line */ | |
ef416fc2 | 59 | |
60 | ||
07725fee | 61 | _cupsSetLocale(argv); |
ed486911 | 62 | |
ed486911 | 63 | /* |
64 | * Parse command-line options... | |
65 | */ | |
66 | ||
ef416fc2 | 67 | num_dests = 0; |
68 | dests = NULL; | |
69 | long_status = 0; | |
70 | ranking = 0; | |
71 | status = 0; | |
72 | which = "not-completed"; | |
73 | op = 0; | |
74 | ||
75 | for (i = 1; i < argc; i ++) | |
76 | if (argv[i][0] == '-') | |
77 | switch (argv[i][1]) | |
78 | { | |
79 | case 'D' : /* Show description */ | |
80 | long_status = 1; | |
81 | break; | |
82 | ||
83 | case 'E' : /* Encrypt */ | |
84 | #ifdef HAVE_SSL | |
85 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
ef416fc2 | 86 | #else |
fa73b229 | 87 | _cupsLangPrintf(stderr, |
0837b7e8 | 88 | _("%s: Sorry, no encryption support."), |
ef416fc2 | 89 | argv[0]); |
90 | #endif /* HAVE_SSL */ | |
91 | break; | |
92 | ||
49d87452 MS |
93 | case 'H' : /* Show server and port */ |
94 | if (cupsServer()[0] == '/') | |
0837b7e8 | 95 | _cupsLangPuts(stdout, cupsServer()); |
49d87452 | 96 | else |
0837b7e8 | 97 | _cupsLangPrintf(stdout, "%s:%d", cupsServer(), ippPort()); |
4bdbabf7 | 98 | op = 'H'; |
49d87452 MS |
99 | break; |
100 | ||
ef416fc2 | 101 | case 'P' : /* Show paper types */ |
102 | op = 'P'; | |
103 | break; | |
104 | ||
105 | case 'R' : /* Show ranking */ | |
106 | ranking = 1; | |
107 | break; | |
108 | ||
109 | case 'S' : /* Show charsets */ | |
110 | op = 'S'; | |
111 | if (!argv[i][2]) | |
112 | i ++; | |
113 | break; | |
114 | ||
fa73b229 | 115 | case 'U' : /* Username */ |
61cf44e2 | 116 | if (argv[i][2]) |
fa73b229 | 117 | cupsSetUser(argv[i] + 2); |
118 | else | |
119 | { | |
120 | i ++; | |
121 | if (i >= argc) | |
122 | { | |
123 | _cupsLangPrintf(stderr, | |
124 | _("%s: Error - expected username after " | |
0837b7e8 | 125 | "\"-U\" option."), |
fa73b229 | 126 | argv[0]); |
127 | return (1); | |
128 | } | |
129 | ||
130 | cupsSetUser(argv[i]); | |
131 | } | |
132 | break; | |
88f9aafc | 133 | |
ef416fc2 | 134 | case 'W' : /* Show which jobs? */ |
135 | if (argv[i][2]) | |
136 | which = argv[i] + 2; | |
137 | else | |
138 | { | |
139 | i ++; | |
140 | ||
141 | if (i >= argc) | |
142 | { | |
fa73b229 | 143 | _cupsLangPrintf(stderr, |
144 | _("%s: Error - need \"completed\", " | |
145 | "\"not-completed\", or \"all\" after " | |
0837b7e8 | 146 | "\"-W\" option."), |
fa73b229 | 147 | argv[0]); |
ef416fc2 | 148 | return (1); |
149 | } | |
150 | ||
151 | which = argv[i]; | |
152 | } | |
153 | ||
fa73b229 | 154 | if (strcmp(which, "completed") && strcmp(which, "not-completed") && |
155 | strcmp(which, "all")) | |
ef416fc2 | 156 | { |
fa73b229 | 157 | _cupsLangPrintf(stderr, |
158 | _("%s: Error - need \"completed\", " | |
159 | "\"not-completed\", or \"all\" after " | |
0837b7e8 | 160 | "\"-W\" option."), |
fa73b229 | 161 | argv[0]); |
ef416fc2 | 162 | return (1); |
163 | } | |
164 | break; | |
165 | ||
166 | case 'a' : /* Show acceptance status */ | |
61cf44e2 | 167 | op = 'a'; |
ef416fc2 | 168 | |
61cf44e2 | 169 | if (argv[i][2]) |
ef416fc2 | 170 | { |
61cf44e2 | 171 | check_dest(argv[0], argv[i] + 2, &num_dests, &dests); |
ef416fc2 | 172 | |
61cf44e2 | 173 | status |= show_accepting(argv[i] + 2, num_dests, dests); |
ef416fc2 | 174 | } |
175 | else if ((i + 1) < argc && argv[i + 1][0] != '-') | |
176 | { | |
177 | i ++; | |
178 | ||
61cf44e2 | 179 | check_dest(argv[0], argv[i], &num_dests, &dests); |
ef416fc2 | 180 | |
61cf44e2 | 181 | status |= show_accepting(argv[i], num_dests, dests); |
ef416fc2 | 182 | } |
183 | else | |
184 | { | |
61cf44e2 MS |
185 | if (num_dests <= 1) |
186 | { | |
187 | cupsFreeDests(num_dests, dests); | |
188 | num_dests = cupsGetDests(&dests); | |
c606bcae MS |
189 | |
190 | if (num_dests == 0 && | |
191 | (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
192 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)) | |
193 | { | |
194 | _cupsLangPrintf(stderr, | |
195 | _("%s: Error - add '/version=1.1' to server " | |
196 | "name."), argv[0]); | |
197 | return (1); | |
198 | } | |
61cf44e2 | 199 | } |
ef416fc2 | 200 | |
61cf44e2 | 201 | status |= show_accepting(NULL, num_dests, dests); |
ef416fc2 | 202 | } |
203 | break; | |
204 | ||
ef416fc2 | 205 | case 'c' : /* Show classes and members */ |
61cf44e2 | 206 | op = 'c'; |
ef416fc2 | 207 | |
61cf44e2 | 208 | if (argv[i][2]) |
ef416fc2 | 209 | { |
61cf44e2 | 210 | check_dest(argv[0], argv[i] + 2, &num_dests, &dests); |
ef416fc2 | 211 | |
61cf44e2 | 212 | status |= show_classes(argv[i] + 2); |
ef416fc2 | 213 | } |
214 | else if ((i + 1) < argc && argv[i + 1][0] != '-') | |
215 | { | |
216 | i ++; | |
217 | ||
61cf44e2 | 218 | check_dest(argv[0], argv[i], &num_dests, &dests); |
ef416fc2 | 219 | |
61cf44e2 | 220 | status |= show_classes(argv[i]); |
ef416fc2 | 221 | } |
222 | else | |
61cf44e2 | 223 | status |= show_classes(NULL); |
ef416fc2 | 224 | break; |
225 | ||
226 | case 'd' : /* Show default destination */ | |
61cf44e2 | 227 | op = 'd'; |
ef416fc2 | 228 | |
61cf44e2 MS |
229 | if (num_dests != 1 || !dests[0].is_default) |
230 | { | |
231 | cupsFreeDests(num_dests, dests); | |
ef416fc2 | 232 | |
61cf44e2 MS |
233 | dests = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL); |
234 | num_dests = dests ? 1 : 0; | |
c606bcae MS |
235 | |
236 | if (num_dests == 0 && | |
237 | (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
238 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)) | |
239 | { | |
240 | _cupsLangPrintf(stderr, | |
241 | _("%s: Error - add '/version=1.1' to server " | |
242 | "name."), argv[0]); | |
243 | return (1); | |
244 | } | |
61cf44e2 MS |
245 | } |
246 | ||
247 | show_default(dests); | |
ef416fc2 | 248 | break; |
249 | ||
250 | case 'f' : /* Show forms */ | |
251 | op = 'f'; | |
252 | if (!argv[i][2]) | |
253 | i ++; | |
254 | break; | |
255 | ||
256 | case 'h' : /* Connect to host */ | |
61cf44e2 | 257 | if (argv[i][2]) |
ef416fc2 | 258 | cupsSetServer(argv[i] + 2); |
259 | else | |
260 | { | |
261 | i ++; | |
262 | ||
263 | if (i >= argc) | |
264 | { | |
fa73b229 | 265 | _cupsLangPrintf(stderr, |
266 | _("%s: Error - expected hostname after " | |
0837b7e8 | 267 | "\"-h\" option."), |
fa73b229 | 268 | argv[0]); |
ef416fc2 | 269 | return (1); |
270 | } | |
271 | ||
272 | cupsSetServer(argv[i]); | |
273 | } | |
274 | break; | |
275 | ||
276 | case 'l' : /* Long status or long job status */ | |
3dd9c340 | 277 | long_status = 2; |
ef416fc2 | 278 | break; |
279 | ||
280 | case 'o' : /* Show jobs by destination */ | |
61cf44e2 | 281 | op = 'o'; |
ef416fc2 | 282 | |
61cf44e2 | 283 | if (argv[i][2]) |
ef416fc2 | 284 | { |
61cf44e2 | 285 | check_dest(argv[0], argv[i] + 2, &num_dests, &dests); |
ef416fc2 | 286 | |
61cf44e2 MS |
287 | status |= show_jobs(argv[i] + 2, NULL, long_status, ranking, |
288 | which); | |
ef416fc2 | 289 | } |
290 | else if ((i + 1) < argc && argv[i + 1][0] != '-') | |
291 | { | |
292 | i ++; | |
293 | ||
61cf44e2 | 294 | check_dest(argv[0], argv[i], &num_dests, &dests); |
ef416fc2 | 295 | |
61cf44e2 | 296 | status |= show_jobs(argv[i], NULL, long_status, ranking, which); |
ef416fc2 | 297 | } |
298 | else | |
61cf44e2 | 299 | status |= show_jobs(NULL, NULL, long_status, ranking, which); |
ef416fc2 | 300 | break; |
301 | ||
302 | case 'p' : /* Show printers */ | |
61cf44e2 | 303 | op = 'p'; |
ef416fc2 | 304 | |
61cf44e2 | 305 | if (argv[i][2]) |
ef416fc2 | 306 | { |
61cf44e2 | 307 | check_dest(argv[0], argv[i] + 2, &num_dests, &dests); |
ef416fc2 | 308 | |
61cf44e2 MS |
309 | status |= show_printers(argv[i] + 2, num_dests, dests, |
310 | long_status); | |
ef416fc2 | 311 | } |
312 | else if ((i + 1) < argc && argv[i + 1][0] != '-') | |
313 | { | |
314 | i ++; | |
315 | ||
61cf44e2 | 316 | check_dest(argv[0], argv[i], &num_dests, &dests); |
ef416fc2 | 317 | |
61cf44e2 | 318 | status |= show_printers(argv[i], num_dests, dests, long_status); |
ef416fc2 | 319 | } |
320 | else | |
321 | { | |
61cf44e2 MS |
322 | if (num_dests <= 1) |
323 | { | |
324 | cupsFreeDests(num_dests, dests); | |
325 | num_dests = cupsGetDests(&dests); | |
c606bcae MS |
326 | |
327 | if (num_dests == 0 && | |
328 | (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
329 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)) | |
330 | { | |
331 | _cupsLangPrintf(stderr, | |
332 | _("%s: Error - add '/version=1.1' to server " | |
333 | "name."), argv[0]); | |
334 | return (1); | |
335 | } | |
61cf44e2 | 336 | } |
ef416fc2 | 337 | |
61cf44e2 | 338 | status |= show_printers(NULL, num_dests, dests, long_status); |
ef416fc2 | 339 | } |
340 | break; | |
341 | ||
342 | case 'r' : /* Show scheduler status */ | |
61cf44e2 | 343 | op = 'r'; |
ef416fc2 | 344 | |
61cf44e2 | 345 | show_scheduler(); |
ef416fc2 | 346 | break; |
347 | ||
348 | case 's' : /* Show summary */ | |
61cf44e2 | 349 | op = 's'; |
ef416fc2 | 350 | |
61cf44e2 MS |
351 | if (num_dests <= 1) |
352 | { | |
353 | cupsFreeDests(num_dests, dests); | |
354 | num_dests = cupsGetDests(&dests); | |
c606bcae MS |
355 | |
356 | if (num_dests == 0 && | |
357 | (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
358 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)) | |
359 | { | |
360 | _cupsLangPrintf(stderr, | |
361 | _("%s: Error - add '/version=1.1' to server " | |
362 | "name."), argv[0]); | |
363 | return (1); | |
364 | } | |
61cf44e2 | 365 | } |
ef416fc2 | 366 | |
61cf44e2 MS |
367 | show_default(cupsGetDest(NULL, NULL, num_dests, dests)); |
368 | status |= show_classes(NULL); | |
369 | status |= show_devices(NULL, num_dests, dests); | |
ef416fc2 | 370 | break; |
371 | ||
372 | case 't' : /* Show all info */ | |
61cf44e2 MS |
373 | op = 't'; |
374 | ||
375 | if (num_dests <= 1) | |
376 | { | |
377 | cupsFreeDests(num_dests, dests); | |
378 | num_dests = cupsGetDests(&dests); | |
c606bcae MS |
379 | |
380 | if (num_dests == 0 && | |
381 | (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
382 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)) | |
383 | { | |
384 | _cupsLangPrintf(stderr, | |
385 | _("%s: Error - add '/version=1.1' to server " | |
386 | "name."), argv[0]); | |
387 | return (1); | |
388 | } | |
61cf44e2 MS |
389 | } |
390 | ||
391 | show_scheduler(); | |
392 | show_default(cupsGetDest(NULL, NULL, num_dests, dests)); | |
393 | status |= show_classes(NULL); | |
394 | status |= show_devices(NULL, num_dests, dests); | |
395 | status |= show_accepting(NULL, num_dests, dests); | |
396 | status |= show_printers(NULL, num_dests, dests, long_status); | |
397 | status |= show_jobs(NULL, NULL, long_status, ranking, which); | |
ef416fc2 | 398 | break; |
399 | ||
400 | case 'u' : /* Show jobs by user */ | |
61cf44e2 | 401 | op = 'u'; |
ef416fc2 | 402 | |
61cf44e2 MS |
403 | if (argv[i][2]) |
404 | status |= show_jobs(NULL, argv[i] + 2, long_status, ranking, | |
405 | which); | |
ef416fc2 | 406 | else if ((i + 1) < argc && argv[i + 1][0] != '-') |
407 | { | |
408 | i ++; | |
61cf44e2 | 409 | status |= show_jobs(NULL, argv[i], long_status, ranking, which); |
ef416fc2 | 410 | } |
411 | else | |
61cf44e2 | 412 | status |= show_jobs(NULL, NULL, long_status, ranking, which); |
ef416fc2 | 413 | break; |
414 | ||
415 | case 'v' : /* Show printer devices */ | |
61cf44e2 | 416 | op = 'v'; |
ef416fc2 | 417 | |
61cf44e2 | 418 | if (argv[i][2]) |
ef416fc2 | 419 | { |
61cf44e2 | 420 | check_dest(argv[0], argv[i] + 2, &num_dests, &dests); |
ef416fc2 | 421 | |
61cf44e2 | 422 | status |= show_devices(argv[i] + 2, num_dests, dests); |
ef416fc2 | 423 | } |
424 | else if ((i + 1) < argc && argv[i + 1][0] != '-') | |
425 | { | |
426 | i ++; | |
427 | ||
61cf44e2 | 428 | check_dest(argv[0], argv[i], &num_dests, &dests); |
ef416fc2 | 429 | |
61cf44e2 | 430 | status |= show_devices(argv[i], num_dests, dests); |
ef416fc2 | 431 | } |
432 | else | |
433 | { | |
61cf44e2 MS |
434 | if (num_dests <= 1) |
435 | { | |
436 | cupsFreeDests(num_dests, dests); | |
437 | num_dests = cupsGetDests(&dests); | |
c606bcae MS |
438 | |
439 | if (num_dests == 0 && | |
440 | (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
441 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)) | |
442 | { | |
443 | _cupsLangPrintf(stderr, | |
444 | _("%s: Error - add '/version=1.1' to server " | |
445 | "name."), argv[0]); | |
446 | return (1); | |
447 | } | |
61cf44e2 | 448 | } |
ef416fc2 | 449 | |
61cf44e2 | 450 | status |= show_devices(NULL, num_dests, dests); |
ef416fc2 | 451 | } |
452 | break; | |
453 | ||
ef416fc2 | 454 | default : |
fa73b229 | 455 | _cupsLangPrintf(stderr, |
0837b7e8 | 456 | _("%s: Error - unknown option \"%c\"."), |
fa73b229 | 457 | argv[0], argv[i][1]); |
ef416fc2 | 458 | return (1); |
459 | } | |
460 | else | |
461 | { | |
61cf44e2 | 462 | status |= show_jobs(argv[i], NULL, long_status, ranking, which); |
ef416fc2 | 463 | op = 'o'; |
464 | } | |
465 | ||
466 | if (!op) | |
61cf44e2 | 467 | status |= show_jobs(NULL, cupsUser(), long_status, ranking, which); |
ef416fc2 | 468 | |
469 | return (status); | |
470 | } | |
471 | ||
472 | ||
473 | /* | |
474 | * 'check_dest()' - Verify that the named destination(s) exists. | |
475 | */ | |
476 | ||
477 | static void | |
fa73b229 | 478 | check_dest(const char *command, /* I - Command name */ |
61cf44e2 | 479 | const char *name, /* I - List of printer/class names */ |
ef416fc2 | 480 | int *num_dests, /* IO - Number of destinations */ |
481 | cups_dest_t **dests) /* IO - Destinations */ | |
482 | { | |
61cf44e2 MS |
483 | const char *dptr; /* Pointer into name */ |
484 | char *pptr, /* Pointer into printer */ | |
485 | printer[1024]; /* Current printer/class name */ | |
ef416fc2 | 486 | |
487 | ||
488 | /* | |
489 | * Load the destination list as necessary... | |
490 | */ | |
491 | ||
61cf44e2 MS |
492 | if (*num_dests <= 1) |
493 | { | |
494 | if (*num_dests) | |
495 | cupsFreeDests(*num_dests, *dests); | |
496 | ||
497 | if (strchr(name, ',')) | |
498 | *num_dests = cupsGetDests(dests); | |
499 | else | |
500 | { | |
501 | strlcpy(printer, name, sizeof(printer)); | |
502 | if ((pptr = strchr(printer, '/')) != NULL) | |
503 | *pptr++ = '\0'; | |
504 | ||
505 | if ((*dests = cupsGetNamedDest(CUPS_HTTP_DEFAULT, printer, pptr)) == NULL) | |
506 | { | |
c606bcae MS |
507 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || |
508 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
509 | _cupsLangPrintf(stderr, | |
510 | _("%s: Error - add '/version=1.1' to server name."), | |
511 | command); | |
512 | else | |
513 | _cupsLangPrintf(stderr, | |
514 | _("%s: Invalid destination name in list \"%s\"."), | |
515 | command, name); | |
516 | ||
61cf44e2 MS |
517 | exit(1); |
518 | } | |
519 | else | |
520 | { | |
521 | *num_dests = 1; | |
522 | return; | |
523 | } | |
524 | } | |
525 | } | |
ef416fc2 | 526 | |
527 | /* | |
528 | * Scan the name string for printer/class name(s)... | |
529 | */ | |
530 | ||
88f9aafc | 531 | for (dptr = name; *dptr;) |
ef416fc2 | 532 | { |
533 | /* | |
534 | * Skip leading whitespace and commas... | |
535 | */ | |
536 | ||
537 | while (isspace(*dptr & 255) || *dptr == ',') | |
538 | dptr ++; | |
539 | ||
61cf44e2 | 540 | if (!*dptr) |
ef416fc2 | 541 | break; |
542 | ||
543 | /* | |
544 | * Extract a single destination name from the name string... | |
545 | */ | |
546 | ||
61cf44e2 | 547 | for (pptr = printer; !isspace(*dptr & 255) && *dptr != ',' && *dptr;) |
ef416fc2 | 548 | { |
549 | if ((pptr - printer) < (sizeof(printer) - 1)) | |
550 | *pptr++ = *dptr++; | |
551 | else | |
552 | { | |
fa73b229 | 553 | _cupsLangPrintf(stderr, |
0837b7e8 | 554 | _("%s: Invalid destination name in list \"%s\"."), |
fa73b229 | 555 | command, name); |
ef416fc2 | 556 | exit(1); |
557 | } | |
558 | } | |
559 | ||
560 | *pptr = '\0'; | |
561 | ||
562 | /* | |
563 | * Check the destination... | |
564 | */ | |
565 | ||
61cf44e2 | 566 | if (!cupsGetDest(printer, NULL, *num_dests, *dests)) |
ef416fc2 | 567 | { |
c606bcae MS |
568 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || |
569 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
570 | _cupsLangPrintf(stderr, | |
571 | _("%s: Error - add '/version=1.1' to server name."), | |
572 | command); | |
573 | else | |
574 | _cupsLangPrintf(stderr, | |
575 | _("%s: Unknown destination \"%s\"."), command, printer); | |
576 | ||
ef416fc2 | 577 | exit(1); |
578 | } | |
579 | } | |
580 | } | |
581 | ||
582 | ||
583 | /* | |
61cf44e2 | 584 | * 'match_list()' - Match a name from a list of comma or space-separated names. |
ef416fc2 | 585 | */ |
586 | ||
61cf44e2 MS |
587 | static int /* O - 1 on match, 0 on no match */ |
588 | match_list(const char *list, /* I - List of names */ | |
589 | const char *name) /* I - Name to find */ | |
ef416fc2 | 590 | { |
61cf44e2 MS |
591 | const char *nameptr; /* Pointer into name */ |
592 | ||
593 | ||
594 | /* | |
cc754834 | 595 | * An empty list always matches... |
61cf44e2 MS |
596 | */ |
597 | ||
598 | if (!list || !*list) | |
cc754834 | 599 | return (1); |
61cf44e2 | 600 | |
a2326b5b MS |
601 | if (!name) |
602 | return (0); | |
603 | ||
61cf44e2 | 604 | while (*list) |
ef416fc2 | 605 | { |
61cf44e2 MS |
606 | /* |
607 | * Skip leading whitespace and commas... | |
608 | */ | |
ef416fc2 | 609 | |
61cf44e2 MS |
610 | while (isspace(*list & 255) || *list == ',') |
611 | list ++; | |
612 | ||
613 | if (!*list) | |
614 | break; | |
615 | ||
616 | /* | |
617 | * Compare names... | |
618 | */ | |
619 | ||
620 | for (nameptr = name; | |
621 | *nameptr && *list && tolower(*nameptr & 255) == tolower(*list & 255); | |
622 | nameptr ++, list ++); | |
623 | ||
624 | if (!*nameptr && (!*list || *list == ',' || isspace(*list & 255))) | |
625 | return (1); | |
626 | ||
627 | while (*list && !isspace(*list & 255) && *list != ',') | |
628 | list ++; | |
ef416fc2 | 629 | } |
630 | ||
61cf44e2 | 631 | return (0); |
ef416fc2 | 632 | } |
633 | ||
634 | ||
635 | /* | |
636 | * 'show_accepting()' - Show acceptance status. | |
637 | */ | |
638 | ||
639 | static int /* O - 0 on success, 1 on fail */ | |
61cf44e2 | 640 | show_accepting(const char *printers, /* I - Destinations */ |
ef416fc2 | 641 | int num_dests, /* I - Number of user-defined dests */ |
642 | cups_dest_t *dests) /* I - User-defined destinations */ | |
643 | { | |
644 | int i; /* Looping var */ | |
645 | ipp_t *request, /* IPP Request */ | |
646 | *response; /* IPP Response */ | |
647 | ipp_attribute_t *attr; /* Current attribute */ | |
648 | const char *printer, /* Printer name */ | |
649 | *message; /* Printer device URI */ | |
650 | int accepting; /* Accepting requests? */ | |
fa73b229 | 651 | time_t ptime; /* Printer state time */ |
fa73b229 | 652 | char printer_state_time[255];/* Printer state time */ |
ef416fc2 | 653 | static const char *pattrs[] = /* Attributes we need for printers... */ |
654 | { | |
655 | "printer-name", | |
fa73b229 | 656 | "printer-state-change-time", |
ef416fc2 | 657 | "printer-state-message", |
658 | "printer-is-accepting-jobs" | |
659 | }; | |
660 | ||
661 | ||
61cf44e2 | 662 | DEBUG_printf(("show_accepting(printers=\"%s\")\n", printers)); |
ef416fc2 | 663 | |
fa73b229 | 664 | if (printers != NULL && !strcmp(printers, "all")) |
ef416fc2 | 665 | printers = NULL; |
666 | ||
667 | /* | |
668 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
669 | * attributes: | |
670 | * | |
671 | * attributes-charset | |
672 | * attributes-natural-language | |
673 | * requested-attributes | |
fa73b229 | 674 | * requesting-user-name |
ef416fc2 | 675 | */ |
676 | ||
677 | request = ippNewRequest(CUPS_GET_PRINTERS); | |
678 | ||
679 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
680 | "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), | |
681 | NULL, pattrs); | |
682 | ||
fa73b229 | 683 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", |
684 | NULL, cupsUser()); | |
685 | ||
ef416fc2 | 686 | /* |
687 | * Do the request and get back a response... | |
688 | */ | |
689 | ||
c606bcae MS |
690 | response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/"); |
691 | ||
692 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
693 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
ef416fc2 | 694 | { |
c606bcae MS |
695 | _cupsLangPrintf(stderr, |
696 | _("%s: Error - add '/version=1.1' to server name."), | |
697 | "lpstat"); | |
698 | ippDelete(response); | |
699 | return (1); | |
700 | } | |
701 | else if (cupsLastError() > IPP_STATUS_OK_CONFLICTING) | |
702 | { | |
703 | _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString()); | |
704 | ippDelete(response); | |
705 | return (1); | |
706 | } | |
ef416fc2 | 707 | |
c606bcae MS |
708 | if (response) |
709 | { | |
710 | DEBUG_puts("show_accepting: request succeeded..."); | |
ef416fc2 | 711 | |
712 | /* | |
713 | * Loop through the printers returned in the list and display | |
714 | * their devices... | |
715 | */ | |
716 | ||
717 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
718 | { | |
719 | /* | |
720 | * Skip leading attributes until we hit a printer... | |
721 | */ | |
722 | ||
723 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
724 | attr = attr->next; | |
725 | ||
726 | if (attr == NULL) | |
727 | break; | |
728 | ||
729 | /* | |
730 | * Pull the needed attributes from this printer... | |
731 | */ | |
732 | ||
733 | printer = NULL; | |
734 | message = NULL; | |
735 | accepting = 1; | |
fa73b229 | 736 | ptime = 0; |
ef416fc2 | 737 | |
738 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
739 | { | |
740 | if (!strcmp(attr->name, "printer-name") && | |
741 | attr->value_tag == IPP_TAG_NAME) | |
742 | printer = attr->values[0].string.text; | |
fa73b229 | 743 | else if (!strcmp(attr->name, "printer-state-change-time") && |
744 | attr->value_tag == IPP_TAG_INTEGER) | |
745 | ptime = (time_t)attr->values[0].integer; | |
746 | else if (!strcmp(attr->name, "printer-state-message") && | |
747 | attr->value_tag == IPP_TAG_TEXT) | |
ef416fc2 | 748 | message = attr->values[0].string.text; |
fa73b229 | 749 | else if (!strcmp(attr->name, "printer-is-accepting-jobs") && |
750 | attr->value_tag == IPP_TAG_BOOLEAN) | |
ef416fc2 | 751 | accepting = attr->values[0].boolean; |
752 | ||
753 | attr = attr->next; | |
754 | } | |
755 | ||
756 | /* | |
757 | * See if we have everything needed... | |
758 | */ | |
759 | ||
760 | if (printer == NULL) | |
761 | { | |
762 | if (attr == NULL) | |
763 | break; | |
764 | else | |
765 | continue; | |
766 | } | |
767 | ||
ef416fc2 | 768 | /* |
769 | * Display the printer entry if needed... | |
770 | */ | |
771 | ||
61cf44e2 | 772 | if (match_list(printers, printer)) |
ef416fc2 | 773 | { |
a51f28ec | 774 | _cupsStrDate(printer_state_time, sizeof(printer_state_time), ptime); |
fa73b229 | 775 | |
ef416fc2 | 776 | if (accepting) |
0837b7e8 | 777 | _cupsLangPrintf(stdout, _("%s accepting requests since %s"), |
fa73b229 | 778 | printer, printer_state_time); |
ef416fc2 | 779 | else |
0837b7e8 MS |
780 | { |
781 | _cupsLangPrintf(stdout, _("%s not accepting requests since %s -"), | |
782 | printer, printer_state_time); | |
783 | _cupsLangPrintf(stdout, _("\t%s"), | |
61cf44e2 MS |
784 | (message == NULL || !*message) ? |
785 | "reason unknown" : message); | |
0837b7e8 | 786 | } |
ef416fc2 | 787 | |
788 | for (i = 0; i < num_dests; i ++) | |
88f9aafc | 789 | if (!_cups_strcasecmp(dests[i].name, printer) && dests[i].instance) |
ef416fc2 | 790 | { |
791 | if (accepting) | |
0837b7e8 | 792 | _cupsLangPrintf(stdout, _("%s/%s accepting requests since %s"), |
fa73b229 | 793 | printer, dests[i].instance, printer_state_time); |
ef416fc2 | 794 | else |
0837b7e8 MS |
795 | { |
796 | _cupsLangPrintf(stdout, | |
797 | _("%s/%s not accepting requests since %s -"), | |
798 | printer, dests[i].instance, printer_state_time); | |
799 | _cupsLangPrintf(stdout, _("\t%s"), | |
61cf44e2 MS |
800 | (message == NULL || !*message) ? |
801 | "reason unknown" : message); | |
0837b7e8 | 802 | } |
ef416fc2 | 803 | } |
804 | } | |
805 | ||
806 | if (attr == NULL) | |
807 | break; | |
808 | } | |
809 | ||
810 | ippDelete(response); | |
811 | } | |
ef416fc2 | 812 | |
813 | return (0); | |
814 | } | |
815 | ||
816 | ||
817 | /* | |
818 | * 'show_classes()' - Show printer classes. | |
819 | */ | |
820 | ||
821 | static int /* O - 0 on success, 1 on fail */ | |
61cf44e2 | 822 | show_classes(const char *dests) /* I - Destinations */ |
ef416fc2 | 823 | { |
824 | int i; /* Looping var */ | |
825 | ipp_t *request, /* IPP Request */ | |
826 | *response, /* IPP Response */ | |
827 | *response2; /* IPP response from remote server */ | |
828 | http_t *http2; /* Remote server */ | |
829 | ipp_attribute_t *attr; /* Current attribute */ | |
830 | const char *printer, /* Printer class name */ | |
831 | *printer_uri; /* Printer class URI */ | |
832 | ipp_attribute_t *members; /* Printer members */ | |
833 | char method[HTTP_MAX_URI], /* Request method */ | |
834 | username[HTTP_MAX_URI], /* Username:password */ | |
835 | server[HTTP_MAX_URI], /* Server name */ | |
836 | resource[HTTP_MAX_URI]; /* Resource name */ | |
837 | int port; /* Port number */ | |
ef416fc2 | 838 | static const char *cattrs[] = /* Attributes we need for classes... */ |
839 | { | |
840 | "printer-name", | |
841 | "printer-uri-supported", | |
842 | "member-names" | |
843 | }; | |
844 | ||
845 | ||
61cf44e2 | 846 | DEBUG_printf(("show_classes(dests=\"%s\")\n", dests)); |
ef416fc2 | 847 | |
fa73b229 | 848 | if (dests != NULL && !strcmp(dests, "all")) |
ef416fc2 | 849 | dests = NULL; |
850 | ||
851 | /* | |
852 | * Build a CUPS_GET_CLASSES request, which requires the following | |
853 | * attributes: | |
854 | * | |
855 | * attributes-charset | |
856 | * attributes-natural-language | |
857 | * requested-attributes | |
fa73b229 | 858 | * requesting-user-name |
ef416fc2 | 859 | */ |
860 | ||
861 | request = ippNewRequest(CUPS_GET_CLASSES); | |
862 | ||
863 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
864 | "requested-attributes", sizeof(cattrs) / sizeof(cattrs[0]), | |
865 | NULL, cattrs); | |
866 | ||
fa73b229 | 867 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", |
868 | NULL, cupsUser()); | |
869 | ||
ef416fc2 | 870 | /* |
871 | * Do the request and get back a response... | |
872 | */ | |
873 | ||
c606bcae MS |
874 | response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/"); |
875 | ||
876 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
877 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
878 | { | |
879 | _cupsLangPrintf(stderr, | |
880 | _("%s: Error - add '/version=1.1' to server name."), | |
881 | "lpstat"); | |
882 | ippDelete(response); | |
883 | return (1); | |
884 | } | |
885 | else if (cupsLastError() > IPP_STATUS_OK_CONFLICTING) | |
886 | { | |
887 | _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString()); | |
888 | ippDelete(response); | |
889 | return (1); | |
890 | } | |
891 | ||
892 | if (response) | |
ef416fc2 | 893 | { |
894 | DEBUG_puts("show_classes: request succeeded..."); | |
895 | ||
896 | if (response->request.status.status_code > IPP_OK_CONFLICT) | |
897 | { | |
0837b7e8 | 898 | _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString()); |
ef416fc2 | 899 | ippDelete(response); |
900 | return (1); | |
901 | } | |
902 | ||
903 | /* | |
904 | * Loop through the printers returned in the list and display | |
905 | * their devices... | |
906 | */ | |
907 | ||
908 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
909 | { | |
910 | /* | |
911 | * Skip leading attributes until we hit a job... | |
912 | */ | |
913 | ||
914 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
915 | attr = attr->next; | |
916 | ||
917 | if (attr == NULL) | |
918 | break; | |
919 | ||
920 | /* | |
921 | * Pull the needed attributes from this job... | |
922 | */ | |
923 | ||
924 | printer = NULL; | |
925 | printer_uri = NULL; | |
926 | members = NULL; | |
927 | ||
928 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
929 | { | |
930 | if (!strcmp(attr->name, "printer-name") && | |
931 | attr->value_tag == IPP_TAG_NAME) | |
932 | printer = attr->values[0].string.text; | |
933 | ||
934 | if (!strcmp(attr->name, "printer-uri-supported") && | |
935 | attr->value_tag == IPP_TAG_URI) | |
936 | printer_uri = attr->values[0].string.text; | |
937 | ||
938 | if (!strcmp(attr->name, "member-names") && | |
939 | attr->value_tag == IPP_TAG_NAME) | |
940 | members = attr; | |
941 | ||
942 | attr = attr->next; | |
943 | } | |
944 | ||
945 | /* | |
946 | * If this is a remote class, grab the class info from the | |
947 | * remote server... | |
948 | */ | |
949 | ||
950 | response2 = NULL; | |
951 | if (members == NULL && printer_uri != NULL) | |
952 | { | |
a4d04587 | 953 | httpSeparateURI(HTTP_URI_CODING_ALL, printer_uri, method, sizeof(method), |
ef416fc2 | 954 | username, sizeof(username), server, sizeof(server), |
955 | &port, resource, sizeof(resource)); | |
956 | ||
88f9aafc | 957 | if (!_cups_strcasecmp(server, cupsServer())) |
61cf44e2 | 958 | http2 = CUPS_HTTP_DEFAULT; |
ef416fc2 | 959 | else |
960 | http2 = httpConnectEncrypt(server, port, cupsEncryption()); | |
961 | ||
61cf44e2 MS |
962 | /* |
963 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the | |
964 | * following attributes: | |
965 | * | |
966 | * attributes-charset | |
967 | * attributes-natural-language | |
968 | * printer-uri | |
969 | * requested-attributes | |
970 | */ | |
ef416fc2 | 971 | |
61cf44e2 | 972 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); |
ef416fc2 | 973 | |
61cf44e2 MS |
974 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
975 | "printer-uri", NULL, printer_uri); | |
ef416fc2 | 976 | |
61cf44e2 MS |
977 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
978 | "requested-attributes", | |
979 | sizeof(cattrs) / sizeof(cattrs[0]), | |
980 | NULL, cattrs); | |
ef416fc2 | 981 | |
61cf44e2 MS |
982 | if ((response2 = cupsDoRequest(http2, request, "/")) != NULL) |
983 | members = ippFindAttribute(response2, "member-names", IPP_TAG_NAME); | |
ef416fc2 | 984 | |
61cf44e2 MS |
985 | if (http2) |
986 | httpClose(http2); | |
ef416fc2 | 987 | } |
988 | ||
989 | /* | |
990 | * See if we have everything needed... | |
991 | */ | |
992 | ||
993 | if (printer == NULL) | |
994 | { | |
995 | if (response2) | |
996 | ippDelete(response2); | |
997 | ||
998 | if (attr == NULL) | |
999 | break; | |
1000 | else | |
1001 | continue; | |
1002 | } | |
1003 | ||
ef416fc2 | 1004 | /* |
1005 | * Display the printer entry if needed... | |
1006 | */ | |
1007 | ||
61cf44e2 | 1008 | if (match_list(dests, printer)) |
ef416fc2 | 1009 | { |
0837b7e8 | 1010 | _cupsLangPrintf(stdout, _("members of class %s:"), printer); |
ef416fc2 | 1011 | |
1012 | if (members) | |
1013 | { | |
1014 | for (i = 0; i < members->num_values; i ++) | |
0837b7e8 | 1015 | _cupsLangPrintf(stdout, "\t%s", members->values[i].string.text); |
ef416fc2 | 1016 | } |
1017 | else | |
0837b7e8 | 1018 | _cupsLangPuts(stdout, "\tunknown"); |
ef416fc2 | 1019 | } |
1020 | ||
1021 | if (response2) | |
1022 | ippDelete(response2); | |
1023 | ||
1024 | if (attr == NULL) | |
1025 | break; | |
1026 | } | |
1027 | ||
1028 | ippDelete(response); | |
1029 | } | |
ef416fc2 | 1030 | |
1031 | return (0); | |
1032 | } | |
1033 | ||
1034 | ||
1035 | /* | |
1036 | * 'show_default()' - Show default destination. | |
1037 | */ | |
1038 | ||
1039 | static void | |
61cf44e2 | 1040 | show_default(cups_dest_t *dest) /* I - Default destination */ |
ef416fc2 | 1041 | { |
ef416fc2 | 1042 | const char *printer, /* Printer name */ |
1043 | *val; /* Environment variable name */ | |
1044 | ||
61cf44e2 MS |
1045 | |
1046 | if (dest) | |
ef416fc2 | 1047 | { |
1048 | if (dest->instance) | |
0837b7e8 | 1049 | _cupsLangPrintf(stdout, _("system default destination: %s/%s"), |
ef416fc2 | 1050 | dest->name, dest->instance); |
1051 | else | |
0837b7e8 | 1052 | _cupsLangPrintf(stdout, _("system default destination: %s"), |
ef416fc2 | 1053 | dest->name); |
1054 | } | |
1055 | else | |
1056 | { | |
1057 | val = NULL; | |
1058 | ||
1059 | if ((printer = getenv("LPDEST")) == NULL) | |
1060 | { | |
1061 | if ((printer = getenv("PRINTER")) != NULL) | |
1062 | { | |
1063 | if (!strcmp(printer, "lp")) | |
1064 | printer = NULL; | |
1065 | else | |
1066 | val = "PRINTER"; | |
1067 | } | |
1068 | } | |
1069 | else | |
1070 | val = "LPDEST"; | |
1071 | ||
61cf44e2 | 1072 | if (printer) |
fa73b229 | 1073 | _cupsLangPrintf(stdout, |
ef416fc2 | 1074 | _("lpstat: error - %s environment variable names " |
0837b7e8 | 1075 | "non-existent destination \"%s\"."), |
ef416fc2 | 1076 | val, printer); |
1077 | else | |
0837b7e8 | 1078 | _cupsLangPuts(stdout, _("no system default destination")); |
ef416fc2 | 1079 | } |
1080 | } | |
1081 | ||
1082 | ||
1083 | /* | |
1084 | * 'show_devices()' - Show printer devices. | |
1085 | */ | |
1086 | ||
1087 | static int /* O - 0 on success, 1 on fail */ | |
61cf44e2 | 1088 | show_devices(const char *printers, /* I - Destinations */ |
ef416fc2 | 1089 | int num_dests, /* I - Number of user-defined dests */ |
1090 | cups_dest_t *dests) /* I - User-defined destinations */ | |
1091 | { | |
1092 | int i; /* Looping var */ | |
1093 | ipp_t *request, /* IPP Request */ | |
1094 | *response; /* IPP Response */ | |
1095 | ipp_attribute_t *attr; /* Current attribute */ | |
1096 | const char *printer, /* Printer name */ | |
1097 | *uri, /* Printer URI */ | |
61cf44e2 | 1098 | *device; /* Printer device URI */ |
ef416fc2 | 1099 | static const char *pattrs[] = /* Attributes we need for printers... */ |
1100 | { | |
1101 | "printer-name", | |
1102 | "printer-uri-supported", | |
1103 | "device-uri" | |
1104 | }; | |
1105 | ||
1106 | ||
61cf44e2 | 1107 | DEBUG_printf(("show_devices(printers=\"%s\")\n", printers)); |
ef416fc2 | 1108 | |
fa73b229 | 1109 | if (printers != NULL && !strcmp(printers, "all")) |
ef416fc2 | 1110 | printers = NULL; |
1111 | ||
1112 | /* | |
1113 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
1114 | * attributes: | |
1115 | * | |
1116 | * attributes-charset | |
1117 | * attributes-natural-language | |
1118 | * requested-attributes | |
fa73b229 | 1119 | * requesting-user-name |
ef416fc2 | 1120 | */ |
1121 | ||
1122 | request = ippNewRequest(CUPS_GET_PRINTERS); | |
1123 | ||
1124 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1125 | "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), | |
1126 | NULL, pattrs); | |
1127 | ||
fa73b229 | 1128 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", |
1129 | NULL, cupsUser()); | |
1130 | ||
ef416fc2 | 1131 | /* |
1132 | * Do the request and get back a response... | |
1133 | */ | |
1134 | ||
c606bcae MS |
1135 | response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/"); |
1136 | ||
1137 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
1138 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
ef416fc2 | 1139 | { |
c606bcae MS |
1140 | _cupsLangPrintf(stderr, |
1141 | _("%s: Error - add '/version=1.1' to server name."), | |
1142 | "lpstat"); | |
1143 | ippDelete(response); | |
1144 | return (1); | |
1145 | } | |
1146 | else if (cupsLastError() > IPP_STATUS_OK_CONFLICTING) | |
1147 | { | |
1148 | _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString()); | |
1149 | ippDelete(response); | |
1150 | return (1); | |
1151 | } | |
ef416fc2 | 1152 | |
c606bcae MS |
1153 | if (response) |
1154 | { | |
1155 | DEBUG_puts("show_devices: request succeeded..."); | |
ef416fc2 | 1156 | |
1157 | /* | |
1158 | * Loop through the printers returned in the list and display | |
1159 | * their devices... | |
1160 | */ | |
1161 | ||
1162 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
1163 | { | |
1164 | /* | |
1165 | * Skip leading attributes until we hit a job... | |
1166 | */ | |
1167 | ||
1168 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
1169 | attr = attr->next; | |
1170 | ||
1171 | if (attr == NULL) | |
1172 | break; | |
1173 | ||
1174 | /* | |
1175 | * Pull the needed attributes from this job... | |
1176 | */ | |
1177 | ||
1178 | printer = NULL; | |
1179 | device = NULL; | |
1180 | uri = NULL; | |
1181 | ||
1182 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
1183 | { | |
1184 | if (!strcmp(attr->name, "printer-name") && | |
1185 | attr->value_tag == IPP_TAG_NAME) | |
1186 | printer = attr->values[0].string.text; | |
1187 | ||
1188 | if (!strcmp(attr->name, "printer-uri-supported") && | |
1189 | attr->value_tag == IPP_TAG_URI) | |
1190 | uri = attr->values[0].string.text; | |
1191 | ||
1192 | if (!strcmp(attr->name, "device-uri") && | |
1193 | attr->value_tag == IPP_TAG_URI) | |
1194 | device = attr->values[0].string.text; | |
1195 | ||
1196 | attr = attr->next; | |
1197 | } | |
1198 | ||
1199 | /* | |
1200 | * See if we have everything needed... | |
1201 | */ | |
1202 | ||
1203 | if (printer == NULL) | |
1204 | { | |
1205 | if (attr == NULL) | |
1206 | break; | |
1207 | else | |
1208 | continue; | |
1209 | } | |
1210 | ||
ef416fc2 | 1211 | /* |
1212 | * Display the printer entry if needed... | |
1213 | */ | |
1214 | ||
61cf44e2 | 1215 | if (match_list(printers, printer)) |
ef416fc2 | 1216 | { |
1217 | #ifdef __osf__ /* Compaq/Digital like to do it their own way... */ | |
61cf44e2 | 1218 | char scheme[HTTP_MAX_URI], /* Components of printer URI */ |
ef416fc2 | 1219 | username[HTTP_MAX_URI], |
1220 | hostname[HTTP_MAX_URI], | |
1221 | resource[HTTP_MAX_URI]; | |
1222 | int port; | |
1223 | ||
1224 | ||
1225 | if (device == NULL) | |
1226 | { | |
61cf44e2 | 1227 | httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), |
a4d04587 | 1228 | username, sizeof(username), hostname, |
1229 | sizeof(hostname), &port, resource, sizeof(resource)); | |
fa73b229 | 1230 | _cupsLangPrintf(stdout, |
ef416fc2 | 1231 | _("Output for printer %s is sent to remote " |
0837b7e8 | 1232 | "printer %s on %s"), |
ef416fc2 | 1233 | printer, strrchr(resource, '/') + 1, hostname); |
1234 | } | |
fa73b229 | 1235 | else if (!strncmp(device, "file:", 5)) |
1236 | _cupsLangPrintf(stdout, | |
0837b7e8 | 1237 | _("Output for printer %s is sent to %s"), |
ef416fc2 | 1238 | printer, device + 5); |
1239 | else | |
fa73b229 | 1240 | _cupsLangPrintf(stdout, |
0837b7e8 | 1241 | _("Output for printer %s is sent to %s"), |
ef416fc2 | 1242 | printer, device); |
1243 | ||
1244 | for (i = 0; i < num_dests; i ++) | |
88f9aafc | 1245 | if (!_cups_strcasecmp(printer, dests[i].name) && dests[i].instance) |
ef416fc2 | 1246 | { |
1247 | if (device == NULL) | |
fa73b229 | 1248 | _cupsLangPrintf(stdout, |
ef416fc2 | 1249 | _("Output for printer %s/%s is sent to " |
0837b7e8 | 1250 | "remote printer %s on %s"), |
ef416fc2 | 1251 | printer, dests[i].instance, |
1252 | strrchr(resource, '/') + 1, hostname); | |
1253 | else if (!strncmp(device, "file:", 5)) | |
fa73b229 | 1254 | _cupsLangPrintf(stdout, |
0837b7e8 | 1255 | _("Output for printer %s/%s is sent to %s"), |
ef416fc2 | 1256 | printer, dests[i].instance, device + 5); |
1257 | else | |
fa73b229 | 1258 | _cupsLangPrintf(stdout, |
0837b7e8 | 1259 | _("Output for printer %s/%s is sent to %s"), |
ef416fc2 | 1260 | printer, dests[i].instance, device); |
1261 | } | |
1262 | #else | |
1263 | if (device == NULL) | |
0837b7e8 | 1264 | _cupsLangPrintf(stdout, _("device for %s: %s"), |
ef416fc2 | 1265 | printer, uri); |
1266 | else if (!strncmp(device, "file:", 5)) | |
0837b7e8 | 1267 | _cupsLangPrintf(stdout, _("device for %s: %s"), |
ef416fc2 | 1268 | printer, device + 5); |
1269 | else | |
0837b7e8 | 1270 | _cupsLangPrintf(stdout, _("device for %s: %s"), |
ef416fc2 | 1271 | printer, device); |
1272 | ||
1273 | for (i = 0; i < num_dests; i ++) | |
88f9aafc | 1274 | if (!_cups_strcasecmp(printer, dests[i].name) && dests[i].instance) |
ef416fc2 | 1275 | { |
1276 | if (device == NULL) | |
0837b7e8 | 1277 | _cupsLangPrintf(stdout, _("device for %s/%s: %s"), |
ef416fc2 | 1278 | printer, dests[i].instance, uri); |
1279 | else if (!strncmp(device, "file:", 5)) | |
0837b7e8 | 1280 | _cupsLangPrintf(stdout, _("device for %s/%s: %s"), |
ef416fc2 | 1281 | printer, dests[i].instance, device + 5); |
1282 | else | |
0837b7e8 | 1283 | _cupsLangPrintf(stdout, _("device for %s/%s: %s"), |
ef416fc2 | 1284 | printer, dests[i].instance, device); |
1285 | } | |
1286 | #endif /* __osf__ */ | |
1287 | } | |
1288 | ||
1289 | if (attr == NULL) | |
1290 | break; | |
1291 | } | |
1292 | ||
1293 | ippDelete(response); | |
1294 | } | |
ef416fc2 | 1295 | |
1296 | return (0); | |
1297 | } | |
1298 | ||
1299 | ||
1300 | /* | |
1301 | * 'show_jobs()' - Show active print jobs. | |
1302 | */ | |
1303 | ||
1304 | static int /* O - 0 on success, 1 on fail */ | |
61cf44e2 | 1305 | show_jobs(const char *dests, /* I - Destinations */ |
ef416fc2 | 1306 | const char *users, /* I - Users */ |
1307 | int long_status, /* I - Show long status? */ | |
1308 | int ranking, /* I - Show job ranking? */ | |
1309 | const char *which) /* I - Show which jobs? */ | |
1310 | { | |
09a101d6 | 1311 | int i; /* Looping var */ |
ef416fc2 | 1312 | ipp_t *request, /* IPP Request */ |
1313 | *response; /* IPP Response */ | |
09a101d6 | 1314 | ipp_attribute_t *attr, /* Current attribute */ |
1315 | *reasons; /* Job state reasons attribute */ | |
ef416fc2 | 1316 | const char *dest, /* Pointer into job-printer-uri */ |
1317 | *username, /* Pointer to job-originating-user-name */ | |
229681c1 MS |
1318 | *title, /* Pointer to job-name */ |
1319 | *message; /* Pointer to job-printer-state-message */ | |
ef416fc2 | 1320 | int rank, /* Rank in queue */ |
1321 | jobid, /* job-id */ | |
1322 | size; /* job-k-octets */ | |
1323 | time_t jobtime; /* time-at-creation */ | |
ef416fc2 | 1324 | char temp[255], /* Temporary buffer */ |
1325 | date[255]; /* Date buffer */ | |
1326 | static const char *jattrs[] = /* Attributes we need for jobs... */ | |
1327 | { | |
1328 | "job-id", | |
1329 | "job-k-octets", | |
1330 | "job-name", | |
09a101d6 | 1331 | "job-originating-user-name", |
229681c1 | 1332 | "job-printer-state-message", |
5a662dc0 MS |
1333 | "job-printer-uri", |
1334 | "job-state-reasons", | |
1335 | "time-at-creation" | |
ef416fc2 | 1336 | }; |
1337 | ||
1338 | ||
61cf44e2 MS |
1339 | DEBUG_printf(("show_jobs(dests=\"%s\", users=\"%s\", long_status=%d, " |
1340 | "ranking=%d, which=\"%s\")\n", dests, users, long_status, | |
1341 | ranking, which)); | |
ef416fc2 | 1342 | |
fa73b229 | 1343 | if (dests != NULL && !strcmp(dests, "all")) |
ef416fc2 | 1344 | dests = NULL; |
1345 | ||
1346 | /* | |
1347 | * Build a IPP_GET_JOBS request, which requires the following | |
1348 | * attributes: | |
1349 | * | |
1350 | * attributes-charset | |
1351 | * attributes-natural-language | |
52f6f666 | 1352 | * printer-uri |
ef416fc2 | 1353 | * requested-attributes |
52f6f666 MS |
1354 | * requesting-user-name |
1355 | * which-jobs | |
ef416fc2 | 1356 | */ |
1357 | ||
1358 | request = ippNewRequest(IPP_GET_JOBS); | |
1359 | ||
52f6f666 MS |
1360 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
1361 | NULL, "ipp://localhost/"); | |
1362 | ||
ef416fc2 | 1363 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
1364 | "requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]), | |
1365 | NULL, jattrs); | |
1366 | ||
52f6f666 MS |
1367 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", |
1368 | NULL, cupsUser()); | |
ef416fc2 | 1369 | |
1370 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs", | |
1371 | NULL, which); | |
1372 | ||
1373 | /* | |
1374 | * Do the request and get back a response... | |
1375 | */ | |
1376 | ||
c606bcae MS |
1377 | response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/"); |
1378 | ||
1379 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
1380 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
1381 | { | |
1382 | _cupsLangPrintf(stderr, | |
1383 | _("%s: Error - add '/version=1.1' to server name."), | |
1384 | "lpstat"); | |
1385 | ippDelete(response); | |
1386 | return (1); | |
1387 | } | |
1388 | else if (cupsLastError() > IPP_STATUS_OK_CONFLICTING) | |
1389 | { | |
1390 | _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString()); | |
1391 | ippDelete(response); | |
1392 | return (1); | |
1393 | } | |
1394 | ||
1395 | if (response) | |
ef416fc2 | 1396 | { |
1397 | /* | |
1398 | * Loop through the job list and display them... | |
1399 | */ | |
1400 | ||
ef416fc2 | 1401 | rank = -1; |
1402 | ||
1403 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
1404 | { | |
1405 | /* | |
1406 | * Skip leading attributes until we hit a job... | |
1407 | */ | |
1408 | ||
1409 | while (attr != NULL && attr->group_tag != IPP_TAG_JOB) | |
1410 | attr = attr->next; | |
1411 | ||
1412 | if (attr == NULL) | |
1413 | break; | |
1414 | ||
1415 | /* | |
1416 | * Pull the needed attributes from this job... | |
1417 | */ | |
1418 | ||
1419 | jobid = 0; | |
1420 | size = 0; | |
1421 | username = NULL; | |
1422 | dest = NULL; | |
1423 | jobtime = 0; | |
1424 | title = "no title"; | |
229681c1 | 1425 | message = NULL; |
09a101d6 | 1426 | reasons = NULL; |
ef416fc2 | 1427 | |
1428 | while (attr != NULL && attr->group_tag == IPP_TAG_JOB) | |
1429 | { | |
fa73b229 | 1430 | if (!strcmp(attr->name, "job-id") && |
ef416fc2 | 1431 | attr->value_tag == IPP_TAG_INTEGER) |
1432 | jobid = attr->values[0].integer; | |
229681c1 MS |
1433 | else if (!strcmp(attr->name, "job-k-octets") && |
1434 | attr->value_tag == IPP_TAG_INTEGER) | |
ef416fc2 | 1435 | size = attr->values[0].integer; |
229681c1 MS |
1436 | else if (!strcmp(attr->name, "time-at-creation") && |
1437 | attr->value_tag == IPP_TAG_INTEGER) | |
ef416fc2 | 1438 | jobtime = attr->values[0].integer; |
229681c1 MS |
1439 | else if (!strcmp(attr->name, "job-printer-state-message") && |
1440 | attr->value_tag == IPP_TAG_TEXT) | |
1441 | message = attr->values[0].string.text; | |
1442 | else if (!strcmp(attr->name, "job-printer-uri") && | |
1443 | attr->value_tag == IPP_TAG_URI) | |
1444 | { | |
ef416fc2 | 1445 | if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL) |
1446 | dest ++; | |
229681c1 MS |
1447 | } |
1448 | else if (!strcmp(attr->name, "job-originating-user-name") && | |
1449 | attr->value_tag == IPP_TAG_NAME) | |
ef416fc2 | 1450 | username = attr->values[0].string.text; |
229681c1 MS |
1451 | else if (!strcmp(attr->name, "job-name") && |
1452 | attr->value_tag == IPP_TAG_NAME) | |
ef416fc2 | 1453 | title = attr->values[0].string.text; |
229681c1 MS |
1454 | else if (!strcmp(attr->name, "job-state-reasons") && |
1455 | attr->value_tag == IPP_TAG_KEYWORD) | |
09a101d6 | 1456 | reasons = attr; |
1457 | ||
ef416fc2 | 1458 | attr = attr->next; |
1459 | } | |
1460 | ||
1461 | /* | |
1462 | * See if we have everything needed... | |
1463 | */ | |
1464 | ||
1465 | if (dest == NULL || jobid == 0) | |
1466 | { | |
1467 | if (attr == NULL) | |
1468 | break; | |
1469 | else | |
1470 | continue; | |
1471 | } | |
1472 | ||
1473 | /* | |
61cf44e2 | 1474 | * Display the job... |
ef416fc2 | 1475 | */ |
1476 | ||
ef416fc2 | 1477 | rank ++; |
1478 | ||
cc754834 | 1479 | if (match_list(dests, dest) && match_list(users, username)) |
ef416fc2 | 1480 | { |
ef416fc2 | 1481 | snprintf(temp, sizeof(temp), "%s-%d", dest, jobid); |
1482 | ||
a51f28ec | 1483 | _cupsStrDate(date, sizeof(date), jobtime); |
ef416fc2 | 1484 | |
a51f28ec MS |
1485 | if (ranking) |
1486 | _cupsLangPrintf(stdout, "%3d %-21s %-13s %8.0f %s", | |
1487 | rank, temp, username ? username : "unknown", | |
1488 | 1024.0 * size, date); | |
ef416fc2 | 1489 | else |
a51f28ec MS |
1490 | _cupsLangPrintf(stdout, "%-23s %-13s %8.0f %s", |
1491 | temp, username ? username : "unknown", | |
1492 | 1024.0 * size, date); | |
1493 | if (long_status) | |
ef416fc2 | 1494 | { |
a51f28ec MS |
1495 | if (message) |
1496 | _cupsLangPrintf(stdout, _("\tStatus: %s"), message); | |
0837b7e8 | 1497 | |
a51f28ec MS |
1498 | if (reasons) |
1499 | { | |
1500 | char alerts[1024], /* Alerts string */ | |
1501 | *aptr; /* Pointer into alerts string */ | |
0837b7e8 | 1502 | |
a51f28ec MS |
1503 | for (i = 0, aptr = alerts; i < reasons->num_values; i ++) |
1504 | { | |
1505 | if (i) | |
1506 | snprintf(aptr, sizeof(alerts) - (aptr - alerts), " %s", | |
1507 | reasons->values[i].string.text); | |
1508 | else | |
1509 | strlcpy(alerts, reasons->values[i].string.text, | |
1510 | sizeof(alerts)); | |
1511 | ||
1512 | aptr += strlen(aptr); | |
09a101d6 | 1513 | } |
0837b7e8 | 1514 | |
a51f28ec | 1515 | _cupsLangPrintf(stdout, _("\tAlerts: %s"), alerts); |
09a101d6 | 1516 | } |
a51f28ec MS |
1517 | |
1518 | _cupsLangPrintf(stdout, _("\tqueued for %s"), dest); | |
ef416fc2 | 1519 | } |
1520 | } | |
1521 | ||
1522 | if (attr == NULL) | |
1523 | break; | |
1524 | } | |
1525 | ||
1526 | ippDelete(response); | |
1527 | } | |
ef416fc2 | 1528 | |
1529 | return (0); | |
1530 | } | |
1531 | ||
1532 | ||
1533 | /* | |
1534 | * 'show_printers()' - Show printers. | |
1535 | */ | |
1536 | ||
1537 | static int /* O - 0 on success, 1 on fail */ | |
61cf44e2 | 1538 | show_printers(const char *printers, /* I - Destinations */ |
ef416fc2 | 1539 | int num_dests, /* I - Number of user-defined dests */ |
1540 | cups_dest_t *dests, /* I - User-defined destinations */ | |
1541 | int long_status) /* I - Show long status? */ | |
1542 | { | |
26d47ec6 | 1543 | int i, j; /* Looping vars */ |
ef416fc2 | 1544 | ipp_t *request, /* IPP Request */ |
1545 | *response, /* IPP Response */ | |
1546 | *jobs; /* IPP Get Jobs response */ | |
1547 | ipp_attribute_t *attr, /* Current attribute */ | |
1548 | *jobattr, /* Job ID attribute */ | |
1549 | *reasons; /* Job state reasons attribute */ | |
1550 | const char *printer, /* Printer name */ | |
1551 | *message, /* Printer state message */ | |
1552 | *description, /* Description of printer */ | |
1553 | *location, /* Location of printer */ | |
1554 | *make_model, /* Make and model of printer */ | |
1555 | *uri; /* URI of printer */ | |
1556 | ipp_attribute_t *allowed, /* requesting-user-name-allowed */ | |
1557 | *denied; /* requestint-user-name-denied */ | |
1558 | ipp_pstate_t pstate; /* Printer state */ | |
1559 | cups_ptype_t ptype; /* Printer type */ | |
1560 | time_t ptime; /* Printer state time */ | |
ef416fc2 | 1561 | int jobid; /* Job ID of current job */ |
ef416fc2 | 1562 | char printer_uri[HTTP_MAX_URI], |
1563 | /* Printer URI */ | |
1564 | printer_state_time[255];/* Printer state time */ | |
f8b3a85b | 1565 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
ef416fc2 | 1566 | static const char *pattrs[] = /* Attributes we need for printers... */ |
1567 | { | |
1568 | "printer-name", | |
1569 | "printer-state", | |
1570 | "printer-state-message", | |
1571 | "printer-state-reasons", | |
1572 | "printer-state-change-time", | |
1573 | "printer-type", | |
1574 | "printer-info", | |
1575 | "printer-location", | |
1576 | "printer-make-and-model", | |
1577 | "printer-uri-supported", | |
1578 | "requesting-user-name-allowed", | |
1579 | "requesting-user-name-denied" | |
1580 | }; | |
1581 | static const char *jattrs[] = /* Attributes we need for jobs... */ | |
1582 | { | |
7cf5915e MS |
1583 | "job-id", |
1584 | "job-state" | |
ef416fc2 | 1585 | }; |
1586 | ||
1587 | ||
bf3816c7 MS |
1588 | DEBUG_printf(("show_printers(printers=\"%s\", num_dests=%d, dests=%p, " |
1589 | "long_status=%d)\n", printers, num_dests, dests, long_status)); | |
ef416fc2 | 1590 | |
fa73b229 | 1591 | if (printers != NULL && !strcmp(printers, "all")) |
ef416fc2 | 1592 | printers = NULL; |
1593 | ||
1594 | /* | |
1595 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
1596 | * attributes: | |
1597 | * | |
1598 | * attributes-charset | |
1599 | * attributes-natural-language | |
1600 | * requested-attributes | |
fa73b229 | 1601 | * requesting-user-name |
ef416fc2 | 1602 | */ |
1603 | ||
1604 | request = ippNewRequest(CUPS_GET_PRINTERS); | |
1605 | ||
1606 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1607 | "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), | |
1608 | NULL, pattrs); | |
1609 | ||
fa73b229 | 1610 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", |
1611 | NULL, cupsUser()); | |
1612 | ||
ef416fc2 | 1613 | /* |
1614 | * Do the request and get back a response... | |
1615 | */ | |
1616 | ||
c606bcae MS |
1617 | response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/"); |
1618 | ||
1619 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || | |
1620 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
ef416fc2 | 1621 | { |
c606bcae MS |
1622 | _cupsLangPrintf(stderr, |
1623 | _("%s: Error - add '/version=1.1' to server name."), | |
1624 | "lpstat"); | |
1625 | ippDelete(response); | |
1626 | return (1); | |
1627 | } | |
1628 | else if (cupsLastError() > IPP_STATUS_OK_CONFLICTING) | |
1629 | { | |
1630 | _cupsLangPrintf(stderr, "lpstat: %s", cupsLastErrorString()); | |
1631 | ippDelete(response); | |
1632 | return (1); | |
1633 | } | |
ef416fc2 | 1634 | |
c606bcae MS |
1635 | if (response) |
1636 | { | |
1637 | DEBUG_puts("show_printers: request succeeded..."); | |
ef416fc2 | 1638 | |
1639 | /* | |
1640 | * Loop through the printers returned in the list and display | |
1641 | * their status... | |
1642 | */ | |
1643 | ||
1644 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
1645 | { | |
1646 | /* | |
1647 | * Skip leading attributes until we hit a job... | |
1648 | */ | |
1649 | ||
1650 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
1651 | attr = attr->next; | |
1652 | ||
1653 | if (attr == NULL) | |
1654 | break; | |
1655 | ||
1656 | /* | |
1657 | * Pull the needed attributes from this job... | |
1658 | */ | |
1659 | ||
1660 | printer = NULL; | |
1661 | ptime = 0; | |
1662 | ptype = CUPS_PRINTER_LOCAL; | |
1663 | pstate = IPP_PRINTER_IDLE; | |
1664 | message = NULL; | |
1665 | description = NULL; | |
1666 | location = NULL; | |
1667 | make_model = NULL; | |
1668 | reasons = NULL; | |
1669 | uri = NULL; | |
1670 | jobid = 0; | |
1671 | allowed = NULL; | |
1672 | denied = NULL; | |
1673 | ||
1674 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
1675 | { | |
1676 | if (!strcmp(attr->name, "printer-name") && | |
1677 | attr->value_tag == IPP_TAG_NAME) | |
1678 | printer = attr->values[0].string.text; | |
1679 | else if (!strcmp(attr->name, "printer-state") && | |
1680 | attr->value_tag == IPP_TAG_ENUM) | |
1681 | pstate = (ipp_pstate_t)attr->values[0].integer; | |
1682 | else if (!strcmp(attr->name, "printer-type") && | |
1683 | attr->value_tag == IPP_TAG_ENUM) | |
1684 | ptype = (cups_ptype_t)attr->values[0].integer; | |
1685 | else if (!strcmp(attr->name, "printer-state-message") && | |
1686 | attr->value_tag == IPP_TAG_TEXT) | |
1687 | message = attr->values[0].string.text; | |
1688 | else if (!strcmp(attr->name, "printer-state-change-time") && | |
1689 | attr->value_tag == IPP_TAG_INTEGER) | |
1690 | ptime = (time_t)attr->values[0].integer; | |
1691 | else if (!strcmp(attr->name, "printer-info") && | |
1692 | attr->value_tag == IPP_TAG_TEXT) | |
1693 | description = attr->values[0].string.text; | |
1694 | else if (!strcmp(attr->name, "printer-location") && | |
1695 | attr->value_tag == IPP_TAG_TEXT) | |
1696 | location = attr->values[0].string.text; | |
1697 | else if (!strcmp(attr->name, "printer-make-and-model") && | |
1698 | attr->value_tag == IPP_TAG_TEXT) | |
1699 | make_model = attr->values[0].string.text; | |
1700 | else if (!strcmp(attr->name, "printer-uri-supported") && | |
1701 | attr->value_tag == IPP_TAG_URI) | |
1702 | uri = attr->values[0].string.text; | |
1703 | else if (!strcmp(attr->name, "printer-state-reasons") && | |
1704 | attr->value_tag == IPP_TAG_KEYWORD) | |
1705 | reasons = attr; | |
1706 | else if (!strcmp(attr->name, "requesting-user-name-allowed") && | |
1707 | attr->value_tag == IPP_TAG_NAME) | |
1708 | allowed = attr; | |
1709 | else if (!strcmp(attr->name, "requesting-user-name-denied") && | |
1710 | attr->value_tag == IPP_TAG_NAME) | |
1711 | denied = attr; | |
1712 | ||
1713 | attr = attr->next; | |
1714 | } | |
1715 | ||
1716 | /* | |
1717 | * See if we have everything needed... | |
1718 | */ | |
1719 | ||
1720 | if (printer == NULL) | |
1721 | { | |
1722 | if (attr == NULL) | |
1723 | break; | |
1724 | else | |
1725 | continue; | |
1726 | } | |
1727 | ||
ef416fc2 | 1728 | /* |
1729 | * Display the printer entry if needed... | |
1730 | */ | |
1731 | ||
61cf44e2 | 1732 | if (match_list(printers, printer)) |
ef416fc2 | 1733 | { |
1734 | /* | |
1735 | * If the printer state is "IPP_PRINTER_PROCESSING", then grab the | |
1736 | * current job for the printer. | |
1737 | */ | |
1738 | ||
1739 | if (pstate == IPP_PRINTER_PROCESSING) | |
1740 | { | |
1741 | /* | |
1742 | * Build an IPP_GET_JOBS request, which requires the following | |
1743 | * attributes: | |
1744 | * | |
1745 | * attributes-charset | |
1746 | * attributes-natural-language | |
1747 | * printer-uri | |
1748 | * limit | |
1749 | * requested-attributes | |
1750 | */ | |
1751 | ||
1752 | request = ippNewRequest(IPP_GET_JOBS); | |
1753 | ||
1754 | request->request.op.operation_id = IPP_GET_JOBS; | |
1755 | request->request.op.request_id = 1; | |
1756 | ||
1757 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1758 | "requested-attributes", | |
1759 | sizeof(jattrs) / sizeof(jattrs[0]), NULL, jattrs); | |
1760 | ||
a4d04587 | 1761 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), |
1762 | "ipp", NULL, "localhost", 0, "/printers/%s", printer); | |
ef416fc2 | 1763 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
1764 | "printer-uri", NULL, printer_uri); | |
1765 | ||
61cf44e2 | 1766 | if ((jobs = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL) |
ef416fc2 | 1767 | { |
1768 | /* | |
1769 | * Get the current active job on this queue... | |
1770 | */ | |
1771 | ||
c934a06c | 1772 | ipp_jstate_t jobstate = IPP_JOB_PENDING; |
ef416fc2 | 1773 | jobid = 0; |
1774 | ||
1775 | for (jobattr = jobs->attrs; jobattr; jobattr = jobattr->next) | |
1776 | { | |
1777 | if (!jobattr->name) | |
c934a06c MS |
1778 | { |
1779 | if (jobstate == IPP_JOB_PROCESSING) | |
1780 | break; | |
1781 | else | |
1782 | continue; | |
1783 | } | |
ef416fc2 | 1784 | |
1785 | if (!strcmp(jobattr->name, "job-id") && | |
1786 | jobattr->value_tag == IPP_TAG_INTEGER) | |
1787 | jobid = jobattr->values[0].integer; | |
1788 | else if (!strcmp(jobattr->name, "job-state") && | |
c934a06c MS |
1789 | jobattr->value_tag == IPP_TAG_ENUM) |
1790 | jobstate = jobattr->values[0].integer; | |
ef416fc2 | 1791 | } |
1792 | ||
c934a06c MS |
1793 | if (jobstate != IPP_JOB_PROCESSING) |
1794 | jobid = 0; | |
1795 | ||
ef416fc2 | 1796 | ippDelete(jobs); |
1797 | } | |
1798 | } | |
1799 | ||
1800 | /* | |
1801 | * Display it... | |
1802 | */ | |
1803 | ||
a51f28ec | 1804 | _cupsStrDate(printer_state_time, sizeof(printer_state_time), ptime); |
ef416fc2 | 1805 | |
1806 | switch (pstate) | |
1807 | { | |
1808 | case IPP_PRINTER_IDLE : | |
fa73b229 | 1809 | _cupsLangPrintf(stdout, |
0837b7e8 | 1810 | _("printer %s is idle. enabled since %s"), |
ef416fc2 | 1811 | printer, printer_state_time); |
1812 | break; | |
1813 | case IPP_PRINTER_PROCESSING : | |
fa73b229 | 1814 | _cupsLangPrintf(stdout, |
ef416fc2 | 1815 | _("printer %s now printing %s-%d. " |
0837b7e8 | 1816 | "enabled since %s"), |
ef416fc2 | 1817 | printer, printer, jobid, printer_state_time); |
1818 | break; | |
1819 | case IPP_PRINTER_STOPPED : | |
fa73b229 | 1820 | _cupsLangPrintf(stdout, |
0837b7e8 | 1821 | _("printer %s disabled since %s -"), |
ef416fc2 | 1822 | printer, printer_state_time); |
1823 | break; | |
1824 | } | |
1825 | ||
1826 | if ((message && *message) || pstate == IPP_PRINTER_STOPPED) | |
1827 | { | |
1828 | if (!message || !*message) | |
0837b7e8 | 1829 | _cupsLangPuts(stdout, _("\treason unknown")); |
ef416fc2 | 1830 | else |
0837b7e8 | 1831 | _cupsLangPrintf(stdout, "\t%s", message); |
ef416fc2 | 1832 | } |
1833 | ||
1834 | if (long_status > 1) | |
0837b7e8 MS |
1835 | { |
1836 | _cupsLangPuts(stdout, _("\tForm mounted:")); | |
1837 | _cupsLangPuts(stdout, _("\tContent types: any")); | |
1838 | _cupsLangPuts(stdout, _("\tPrinter types: unknown")); | |
1839 | } | |
ef416fc2 | 1840 | |
1841 | if (long_status) | |
1842 | { | |
0837b7e8 | 1843 | _cupsLangPrintf(stdout, _("\tDescription: %s"), |
ef416fc2 | 1844 | description ? description : ""); |
1845 | ||
1846 | if (reasons) | |
1847 | { | |
0837b7e8 MS |
1848 | char alerts[1024], /* Alerts string */ |
1849 | *aptr; /* Pointer into alerts string */ | |
1850 | ||
1851 | for (i = 0, aptr = alerts; i < reasons->num_values; i ++) | |
1852 | { | |
1853 | if (i) | |
1854 | snprintf(aptr, sizeof(alerts) - (aptr - alerts), " %s", | |
1855 | reasons->values[i].string.text); | |
1856 | else | |
1857 | strlcpy(alerts, reasons->values[i].string.text, | |
1858 | sizeof(alerts)); | |
1859 | ||
1860 | aptr += strlen(aptr); | |
1861 | } | |
1862 | ||
1863 | _cupsLangPrintf(stdout, _("\tAlerts: %s"), alerts); | |
ef416fc2 | 1864 | } |
1865 | } | |
1866 | if (long_status > 1) | |
1867 | { | |
0837b7e8 | 1868 | _cupsLangPrintf(stdout, _("\tLocation: %s"), |
ef416fc2 | 1869 | location ? location : ""); |
1870 | ||
1871 | if (ptype & CUPS_PRINTER_REMOTE) | |
1872 | { | |
0837b7e8 | 1873 | _cupsLangPuts(stdout, _("\tConnection: remote")); |
ef416fc2 | 1874 | |
1875 | if (make_model && !strstr(make_model, "System V Printer") && | |
1876 | !strstr(make_model, "Raw Printer") && uri) | |
0837b7e8 | 1877 | _cupsLangPrintf(stdout, _("\tInterface: %s.ppd"), |
ef416fc2 | 1878 | uri); |
1879 | } | |
1880 | else | |
1881 | { | |
0837b7e8 | 1882 | _cupsLangPuts(stdout, _("\tConnection: direct")); |
ef416fc2 | 1883 | |
1884 | if (make_model && strstr(make_model, "System V Printer")) | |
fa73b229 | 1885 | _cupsLangPrintf(stdout, |
0837b7e8 | 1886 | _("\tInterface: %s/interfaces/%s"), |
f8b3a85b | 1887 | cg->cups_serverroot, printer); |
ef416fc2 | 1888 | else if (make_model && !strstr(make_model, "Raw Printer")) |
fa73b229 | 1889 | _cupsLangPrintf(stdout, |
0837b7e8 | 1890 | _("\tInterface: %s/ppd/%s.ppd"), |
f8b3a85b | 1891 | cg->cups_serverroot, printer); |
ef416fc2 | 1892 | } |
0837b7e8 MS |
1893 | _cupsLangPuts(stdout, _("\tOn fault: no alert")); |
1894 | _cupsLangPuts(stdout, _("\tAfter fault: continue")); | |
b423cd4c | 1895 | /* TODO update to use printer-error-policy */ |
ef416fc2 | 1896 | if (allowed) |
1897 | { | |
0837b7e8 | 1898 | _cupsLangPuts(stdout, _("\tUsers allowed:")); |
26d47ec6 | 1899 | for (j = 0; j < allowed->num_values; j ++) |
0837b7e8 | 1900 | _cupsLangPrintf(stdout, "\t\t%s", |
26d47ec6 | 1901 | allowed->values[j].string.text); |
ef416fc2 | 1902 | } |
1903 | else if (denied) | |
1904 | { | |
0837b7e8 | 1905 | _cupsLangPuts(stdout, _("\tUsers denied:")); |
26d47ec6 | 1906 | for (j = 0; j < denied->num_values; j ++) |
0837b7e8 | 1907 | _cupsLangPrintf(stdout, "\t\t%s", |
26d47ec6 | 1908 | denied->values[j].string.text); |
ef416fc2 | 1909 | } |
1910 | else | |
1911 | { | |
0837b7e8 MS |
1912 | _cupsLangPuts(stdout, _("\tUsers allowed:")); |
1913 | _cupsLangPuts(stdout, _("\t\t(all)")); | |
ef416fc2 | 1914 | } |
0837b7e8 MS |
1915 | _cupsLangPuts(stdout, _("\tForms allowed:")); |
1916 | _cupsLangPuts(stdout, _("\t\t(none)")); | |
1917 | _cupsLangPuts(stdout, _("\tBanner required")); | |
1918 | _cupsLangPuts(stdout, _("\tCharset sets:")); | |
1919 | _cupsLangPuts(stdout, _("\t\t(none)")); | |
1920 | _cupsLangPuts(stdout, _("\tDefault pitch:")); | |
1921 | _cupsLangPuts(stdout, _("\tDefault page size:")); | |
1922 | _cupsLangPuts(stdout, _("\tDefault port settings:")); | |
ef416fc2 | 1923 | } |
1924 | ||
1925 | for (i = 0; i < num_dests; i ++) | |
88f9aafc | 1926 | if (!_cups_strcasecmp(printer, dests[i].name) && dests[i].instance) |
ef416fc2 | 1927 | { |
1928 | switch (pstate) | |
1929 | { | |
1930 | case IPP_PRINTER_IDLE : | |
fa73b229 | 1931 | _cupsLangPrintf(stdout, |
ef416fc2 | 1932 | _("printer %s/%s is idle. " |
0837b7e8 | 1933 | "enabled since %s"), |
ef416fc2 | 1934 | printer, dests[i].instance, |
1935 | printer_state_time); | |
1936 | break; | |
1937 | case IPP_PRINTER_PROCESSING : | |
fa73b229 | 1938 | _cupsLangPrintf(stdout, |
ef416fc2 | 1939 | _("printer %s/%s now printing %s-%d. " |
0837b7e8 | 1940 | "enabled since %s"), |
ef416fc2 | 1941 | printer, dests[i].instance, printer, jobid, |
1942 | printer_state_time); | |
1943 | break; | |
1944 | case IPP_PRINTER_STOPPED : | |
fa73b229 | 1945 | _cupsLangPrintf(stdout, |
0837b7e8 | 1946 | _("printer %s/%s disabled since %s -"), |
ef416fc2 | 1947 | printer, dests[i].instance, |
1948 | printer_state_time); | |
1949 | break; | |
1950 | } | |
1951 | ||
1952 | if ((message && *message) || pstate == IPP_PRINTER_STOPPED) | |
1953 | { | |
1954 | if (!message || !*message) | |
0837b7e8 | 1955 | _cupsLangPuts(stdout, _("\treason unknown")); |
ef416fc2 | 1956 | else |
0837b7e8 | 1957 | _cupsLangPrintf(stdout, "\t%s", message); |
ef416fc2 | 1958 | } |
1959 | ||
1960 | if (long_status > 1) | |
0837b7e8 MS |
1961 | { |
1962 | _cupsLangPuts(stdout, _("\tForm mounted:")); | |
1963 | _cupsLangPuts(stdout, _("\tContent types: any")); | |
1964 | _cupsLangPuts(stdout, _("\tPrinter types: unknown")); | |
1965 | } | |
ef416fc2 | 1966 | |
1967 | if (long_status) | |
1968 | { | |
0837b7e8 | 1969 | _cupsLangPrintf(stdout, _("\tDescription: %s"), |
ef416fc2 | 1970 | description ? description : ""); |
1971 | ||
1972 | if (reasons) | |
1973 | { | |
0837b7e8 MS |
1974 | char alerts[1024], /* Alerts string */ |
1975 | *aptr; /* Pointer into alerts string */ | |
1976 | ||
1977 | for (i = 0, aptr = alerts; i < reasons->num_values; i ++) | |
1978 | { | |
1979 | if (i) | |
1980 | snprintf(aptr, sizeof(alerts) - (aptr - alerts), " %s", | |
1981 | reasons->values[i].string.text); | |
1982 | else | |
1983 | strlcpy(alerts, reasons->values[i].string.text, | |
1984 | sizeof(alerts)); | |
1985 | ||
1986 | aptr += strlen(aptr); | |
1987 | } | |
1988 | ||
1989 | _cupsLangPrintf(stdout, _("\tAlerts: %s"), alerts); | |
ef416fc2 | 1990 | } |
1991 | } | |
1992 | if (long_status > 1) | |
1993 | { | |
0837b7e8 | 1994 | _cupsLangPrintf(stdout, _("\tLocation: %s"), |
ef416fc2 | 1995 | location ? location : ""); |
1996 | ||
1997 | if (ptype & CUPS_PRINTER_REMOTE) | |
1998 | { | |
0837b7e8 | 1999 | _cupsLangPuts(stdout, _("\tConnection: remote")); |
ef416fc2 | 2000 | |
2001 | if (make_model && !strstr(make_model, "System V Printer") && | |
2002 | !strstr(make_model, "Raw Printer") && uri) | |
0837b7e8 | 2003 | _cupsLangPrintf(stdout, _("\tInterface: %s.ppd"), uri); |
ef416fc2 | 2004 | } |
2005 | else | |
2006 | { | |
0837b7e8 | 2007 | _cupsLangPuts(stdout, _("\tConnection: direct")); |
ef416fc2 | 2008 | |
2009 | if (make_model && strstr(make_model, "System V Printer")) | |
fa73b229 | 2010 | _cupsLangPrintf(stdout, |
0837b7e8 | 2011 | _("\tInterface: %s/interfaces/%s"), |
f8b3a85b | 2012 | cg->cups_serverroot, printer); |
ef416fc2 | 2013 | else if (make_model && !strstr(make_model, "Raw Printer")) |
fa73b229 | 2014 | _cupsLangPrintf(stdout, |
0837b7e8 | 2015 | _("\tInterface: %s/ppd/%s.ppd"), |
f8b3a85b | 2016 | cg->cups_serverroot, printer); |
ef416fc2 | 2017 | } |
0837b7e8 MS |
2018 | _cupsLangPuts(stdout, _("\tOn fault: no alert")); |
2019 | _cupsLangPuts(stdout, _("\tAfter fault: continue")); | |
b423cd4c | 2020 | /* TODO update to use printer-error-policy */ |
ef416fc2 | 2021 | if (allowed) |
2022 | { | |
0837b7e8 | 2023 | _cupsLangPuts(stdout, _("\tUsers allowed:")); |
26d47ec6 | 2024 | for (j = 0; j < allowed->num_values; j ++) |
0837b7e8 | 2025 | _cupsLangPrintf(stdout, "\t\t%s", |
26d47ec6 | 2026 | allowed->values[j].string.text); |
ef416fc2 | 2027 | } |
2028 | else if (denied) | |
2029 | { | |
0837b7e8 | 2030 | _cupsLangPuts(stdout, _("\tUsers denied:")); |
26d47ec6 | 2031 | for (j = 0; j < denied->num_values; j ++) |
0837b7e8 | 2032 | _cupsLangPrintf(stdout, "\t\t%s", |
26d47ec6 | 2033 | denied->values[j].string.text); |
ef416fc2 | 2034 | } |
2035 | else | |
2036 | { | |
0837b7e8 MS |
2037 | _cupsLangPuts(stdout, _("\tUsers allowed:")); |
2038 | _cupsLangPuts(stdout, _("\t\t(all)")); | |
ef416fc2 | 2039 | } |
0837b7e8 MS |
2040 | _cupsLangPuts(stdout, _("\tForms allowed:")); |
2041 | _cupsLangPuts(stdout, _("\t\t(none)")); | |
2042 | _cupsLangPuts(stdout, _("\tBanner required")); | |
2043 | _cupsLangPuts(stdout, _("\tCharset sets:")); | |
2044 | _cupsLangPuts(stdout, _("\t\t(none)")); | |
2045 | _cupsLangPuts(stdout, _("\tDefault pitch:")); | |
2046 | _cupsLangPuts(stdout, _("\tDefault page size:")); | |
2047 | _cupsLangPuts(stdout, _("\tDefault port settings:")); | |
ef416fc2 | 2048 | } |
2049 | } | |
2050 | } | |
2051 | ||
2052 | if (attr == NULL) | |
2053 | break; | |
2054 | } | |
2055 | ||
2056 | ippDelete(response); | |
2057 | } | |
ef416fc2 | 2058 | |
2059 | return (0); | |
2060 | } | |
2061 | ||
2062 | ||
2063 | /* | |
2064 | * 'show_scheduler()' - Show scheduler status. | |
2065 | */ | |
2066 | ||
2067 | static void | |
61cf44e2 | 2068 | show_scheduler(void) |
ef416fc2 | 2069 | { |
61cf44e2 MS |
2070 | http_t *http; /* Connection to server */ |
2071 | ||
2072 | ||
2073 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
2074 | cupsEncryption())) != NULL) | |
2075 | { | |
0837b7e8 | 2076 | _cupsLangPuts(stdout, _("scheduler is running")); |
61cf44e2 MS |
2077 | httpClose(http); |
2078 | } | |
ef416fc2 | 2079 | else |
0837b7e8 | 2080 | _cupsLangPuts(stdout, _("scheduler is not running")); |
ef416fc2 | 2081 | } |
2082 | ||
2083 | ||
2084 | /* | |
a51f28ec | 2085 | * End of "$Id: lpstat.c 11890 2014-05-22 13:59:21Z msweet $". |
ef416fc2 | 2086 | */ |