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