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