]>
Commit | Line | Data |
---|---|---|
38138d28 | 1 | /* |
b2e10895 | 2 | * "$Id$" |
38138d28 | 3 | * |
3d94661a | 4 | * "lpinfo" command for CUPS. |
38138d28 | 5 | * |
3d94661a | 6 | * Copyright 2007-2010 by Apple Inc. |
0e66904d | 7 | * Copyright 1997-2006 by Easy Software Products. |
38138d28 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
4e8d321f | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
38138d28 | 14 | * |
15 | * Contents: | |
16 | * | |
17 | * main() - Parse options and show information. | |
7e8f437e | 18 | * device_cb - Device callback. |
38138d28 | 19 | * show_devices() - Show available devices. |
20 | * show_models() - Show available PPDs. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Include necessary headers... | |
25 | */ | |
26 | ||
3d94661a | 27 | #include <cups/cups-private.h> |
38138d28 | 28 | |
29 | ||
30 | /* | |
31 | * Local functions... | |
32 | */ | |
33 | ||
7e8f437e | 34 | static void device_cb(const char *device_clas, const char *device_id, |
35 | const char *device_info, | |
36 | const char *device_make_and_model, | |
2317ac1e | 37 | const char *device_uri, const char *device_location, |
38 | void *user_data); | |
05e571cf | 39 | static int show_devices(int long_status, int timeout, |
40 | const char *include_schemes, | |
41 | const char *exclude_schemes); | |
42 | static int show_models(int long_status, | |
904836ce | 43 | const char *device_id, const char *language, |
05e571cf | 44 | const char *make_model, const char *product, |
45 | const char *include_schemes, | |
46 | const char *exclude_schemes); | |
38138d28 | 47 | |
48 | ||
49 | /* | |
50 | * 'main()' - Parse options and show status information. | |
51 | */ | |
52 | ||
53 | int | |
b7382443 | 54 | main(int argc, /* I - Number of command-line arguments */ |
55 | char *argv[]) /* I - Command-line arguments */ | |
38138d28 | 56 | { |
b7382443 | 57 | int i; /* Looping var */ |
b7382443 | 58 | int long_status; /* Long listing? */ |
904836ce | 59 | const char *device_id, /* 1284 device ID */ |
60 | *language, /* Language */ | |
61 | *make_model, /* Make and model */ | |
05e571cf | 62 | *product, /* Product */ |
63 | *include_schemes, /* Schemes to include */ | |
64 | *exclude_schemes; /* Schemes to exclude */ | |
904836ce | 65 | int timeout; /* Device timeout */ |
38138d28 | 66 | |
67 | ||
156dd8aa | 68 | _cupsSetLocale(argv); |
ebad28ad | 69 | |
05e571cf | 70 | long_status = 0; |
71 | device_id = NULL; | |
72 | language = NULL; | |
73 | make_model = NULL; | |
74 | product = NULL; | |
75 | include_schemes = CUPS_INCLUDE_ALL; | |
76 | exclude_schemes = CUPS_EXCLUDE_NONE; | |
77 | timeout = CUPS_TIMEOUT_DEFAULT; | |
38138d28 | 78 | |
79 | for (i = 1; i < argc; i ++) | |
80 | if (argv[i][0] == '-') | |
81 | switch (argv[i][1]) | |
82 | { | |
1c9e0181 | 83 | case 'E' : /* Encrypt */ |
bcf61448 | 84 | #ifdef HAVE_SSL |
192a8a08 | 85 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); |
1c9e0181 | 86 | #else |
89fd567e | 87 | _cupsLangPrintf(stderr, |
157fcea9 | 88 | _("%s: Sorry, no encryption support compiled in\n"), |
b7382443 | 89 | argv[0]); |
bcf61448 | 90 | #endif /* HAVE_SSL */ |
1c9e0181 | 91 | break; |
92 | ||
904836ce | 93 | case 'h' : /* Connect to host */ |
904836ce | 94 | if (argv[i][2] != '\0') |
95 | cupsSetServer(argv[i] + 2); | |
96 | else | |
97 | { | |
98 | i ++; | |
99 | ||
100 | if (i >= argc) | |
101 | { | |
102 | _cupsLangPuts(stderr, | |
157fcea9 | 103 | _("Error: need hostname after \'-h\' option\n")); |
904836ce | 104 | return (1); |
105 | } | |
106 | ||
107 | cupsSetServer(argv[i]); | |
108 | } | |
109 | break; | |
110 | ||
38138d28 | 111 | case 'l' : /* Show long listing */ |
112 | long_status = 1; | |
113 | break; | |
114 | ||
115 | case 'm' : /* Show models */ | |
05e571cf | 116 | if (show_models(long_status, device_id, language, make_model, |
117 | product, include_schemes, exclude_schemes)) | |
b2e10895 | 118 | return (1); |
38138d28 | 119 | break; |
120 | ||
121 | case 'v' : /* Show available devices */ | |
05e571cf | 122 | if (show_devices(long_status, timeout, include_schemes, |
123 | exclude_schemes)) | |
b2e10895 | 124 | return (1); |
38138d28 | 125 | break; |
126 | ||
904836ce | 127 | case '-' : /* --something */ |
128 | if (!strcmp(argv[i], "--device-id")) | |
192a8a08 | 129 | { |
904836ce | 130 | i ++; |
38138d28 | 131 | |
904836ce | 132 | if (i < argc) |
133 | device_id = argv[i]; | |
134 | else | |
135 | { | |
136 | _cupsLangPuts(stderr, | |
137 | _("lpinfo: Expected 1284 device ID string " | |
157fcea9 | 138 | "after --device-id\n")); |
904836ce | 139 | return (1); |
140 | } | |
141 | } | |
142 | else if (!strncmp(argv[i], "--device-id=", 12) && argv[i][12]) | |
143 | { | |
144 | device_id = argv[i] + 12; | |
145 | } | |
05e571cf | 146 | else if (!strcmp(argv[i], "--exclude-schemes")) |
147 | { | |
148 | i ++; | |
149 | ||
150 | if (i < argc) | |
151 | exclude_schemes = argv[i]; | |
152 | else | |
153 | { | |
154 | _cupsLangPuts(stderr, | |
155 | _("lpinfo: Expected scheme list after " | |
157fcea9 | 156 | "--exclude-schemes\n")); |
05e571cf | 157 | return (1); |
158 | } | |
159 | } | |
160 | else if (!strncmp(argv[i], "--exclude-schemes=", 18) && argv[i][18]) | |
161 | { | |
162 | exclude_schemes = argv[i] + 18; | |
163 | } | |
164 | else if (!strcmp(argv[i], "--include-schemes")) | |
165 | { | |
166 | i ++; | |
167 | ||
168 | if (i < argc) | |
169 | include_schemes = argv[i]; | |
170 | else | |
171 | { | |
172 | _cupsLangPuts(stderr, | |
173 | _("lpinfo: Expected scheme list after " | |
157fcea9 | 174 | "--include-schemes\n")); |
05e571cf | 175 | return (1); |
176 | } | |
177 | } | |
178 | else if (!strncmp(argv[i], "--include-schemes=", 18) && argv[i][18]) | |
179 | { | |
180 | include_schemes = argv[i] + 18; | |
181 | } | |
904836ce | 182 | else if (!strcmp(argv[i], "--language")) |
38138d28 | 183 | { |
184 | i ++; | |
904836ce | 185 | if (i < argc) |
186 | language = argv[i]; | |
187 | else | |
38138d28 | 188 | { |
904836ce | 189 | _cupsLangPuts(stderr, |
190 | _("lpinfo: Expected language after " | |
157fcea9 | 191 | "--language\n")); |
38138d28 | 192 | return (1); |
904836ce | 193 | } |
194 | } | |
195 | else if (!strncmp(argv[i], "--language=", 11) && argv[i][11]) | |
196 | { | |
197 | language = argv[i] + 11; | |
198 | } | |
199 | else if (!strcmp(argv[i], "--make-and-model")) | |
200 | { | |
201 | i ++; | |
202 | if (i < argc) | |
203 | make_model= argv[i]; | |
204 | else | |
205 | { | |
206 | _cupsLangPuts(stderr, | |
207 | _("lpinfo: Expected make and model after " | |
157fcea9 | 208 | "--make-and-model\n")); |
904836ce | 209 | return (1); |
210 | } | |
211 | } | |
212 | else if (!strncmp(argv[i], "--make-and-model=", 17) && argv[i][17]) | |
213 | { | |
214 | make_model = argv[i] + 17; | |
215 | } | |
216 | else if (!strcmp(argv[i], "--product")) | |
217 | { | |
218 | i ++; | |
219 | if (i < argc) | |
220 | product = argv[i]; | |
221 | else | |
222 | { | |
223 | _cupsLangPuts(stderr, | |
224 | _("lpinfo: Expected product string after " | |
157fcea9 | 225 | "--product\n")); |
904836ce | 226 | return (1); |
227 | } | |
228 | } | |
229 | else if (!strncmp(argv[i], "--product=", 10) && argv[i][10]) | |
230 | { | |
231 | product = argv[i] + 10; | |
232 | } | |
233 | else if (!strcmp(argv[i], "--timeout")) | |
234 | { | |
235 | i ++; | |
236 | if (i < argc) | |
237 | timeout = atoi(argv[i]); | |
238 | else | |
239 | { | |
240 | _cupsLangPuts(stderr, | |
157fcea9 | 241 | _("lpinfo: Expected timeout after --timeout\n")); |
904836ce | 242 | return (1); |
243 | } | |
244 | } | |
245 | else if (!strncmp(argv[i], "--timeout=", 10) && argv[i][10]) | |
246 | { | |
247 | timeout = atoi(argv[i] + 10); | |
248 | } | |
249 | else | |
250 | { | |
157fcea9 | 251 | _cupsLangPrintf(stderr, _("lpinfo: Unknown option \'%s\'\n"), |
904836ce | 252 | argv[i]); |
253 | return (1); | |
38138d28 | 254 | } |
255 | break; | |
256 | ||
257 | default : | |
157fcea9 | 258 | _cupsLangPrintf(stderr, _("lpinfo: Unknown option \'%c\'\n"), |
b7382443 | 259 | argv[i][1]); |
38138d28 | 260 | return (1); |
261 | } | |
262 | else | |
263 | { | |
157fcea9 | 264 | _cupsLangPrintf(stderr, _("lpinfo: Unknown argument \'%s\'\n"), |
b7382443 | 265 | argv[i]); |
38138d28 | 266 | return (1); |
267 | } | |
268 | ||
269 | return (0); | |
270 | } | |
271 | ||
272 | ||
273 | /* | |
7e8f437e | 274 | * 'device_cb()' - Device callback. |
38138d28 | 275 | */ |
276 | ||
7e8f437e | 277 | static void |
278 | device_cb( | |
279 | const char *device_class, /* I - device-class string */ | |
280 | const char *device_id, /* I - device-id string */ | |
281 | const char *device_info, /* I - device-info string */ | |
282 | const char *device_make_and_model, /* I - device-make-and-model string */ | |
283 | const char *device_uri, /* I - device-uri string */ | |
2317ac1e | 284 | const char *device_location, /* I - device-location string */ |
7e8f437e | 285 | void *user_data) /* I - User data */ |
38138d28 | 286 | { |
7e8f437e | 287 | int *long_status; /* Show verbose info? */ |
38138d28 | 288 | |
38138d28 | 289 | |
290 | /* | |
7e8f437e | 291 | * Display the device... |
38138d28 | 292 | */ |
293 | ||
7e8f437e | 294 | long_status = (int *)user_data; |
38138d28 | 295 | |
7e8f437e | 296 | if (*long_status) |
38138d28 | 297 | { |
7e8f437e | 298 | _cupsLangPrintf(stdout, |
299 | _("Device: uri = %s\n" | |
300 | " class = %s\n" | |
301 | " info = %s\n" | |
302 | " make-and-model = %s\n" | |
2317ac1e | 303 | " device-id = %s\n" |
304 | " location = %s\n"), | |
7e8f437e | 305 | device_uri, device_class, device_info, |
2317ac1e | 306 | device_make_and_model, device_id, device_location); |
7e8f437e | 307 | } |
308 | else | |
309 | _cupsLangPrintf(stdout, "%s %s\n", device_class, device_uri); | |
310 | } | |
38138d28 | 311 | |
38138d28 | 312 | |
7e8f437e | 313 | /* |
314 | * 'show_devices()' - Show available devices. | |
315 | */ | |
38138d28 | 316 | |
7e8f437e | 317 | static int /* O - 0 on success, 1 on failure */ |
05e571cf | 318 | show_devices( |
319 | int long_status, /* I - Long status report? */ | |
320 | int timeout, /* I - Timeout */ | |
321 | const char *include_schemes, /* I - List of schemes to include */ | |
322 | const char *exclude_schemes) /* I - List of schemes to exclude */ | |
7e8f437e | 323 | { |
05e571cf | 324 | if (cupsGetDevices(CUPS_HTTP_DEFAULT, timeout, include_schemes, |
325 | exclude_schemes, device_cb, &long_status) != IPP_OK) | |
b2e10895 | 326 | { |
00f3aaf5 | 327 | _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString()); |
b2e10895 | 328 | return (1); |
329 | } | |
330 | ||
331 | return (0); | |
38138d28 | 332 | } |
333 | ||
334 | ||
335 | /* | |
336 | * 'show_models()' - Show available PPDs. | |
337 | */ | |
338 | ||
b7382443 | 339 | static int /* O - 0 on success, 1 on failure */ |
05e571cf | 340 | show_models( |
341 | int long_status, /* I - Long status report? */ | |
342 | const char *device_id, /* I - 1284 device ID */ | |
343 | const char *language, /* I - Language */ | |
344 | const char *make_model, /* I - Make and model */ | |
345 | const char *product, /* I - Product */ | |
346 | const char *include_schemes, /* I - List of schemes to include */ | |
347 | const char *exclude_schemes) /* I - List of schemes to exclude */ | |
38138d28 | 348 | { |
b7382443 | 349 | ipp_t *request, /* IPP Request */ |
350 | *response; /* IPP Response */ | |
351 | ipp_attribute_t *attr; /* Current attribute */ | |
00f3aaf5 | 352 | const char *ppd_device_id, /* Pointer to ppd-device-id */ |
353 | *ppd_language, /* Pointer to ppd-natural-language */ | |
904836ce | 354 | *ppd_make_model, /* Pointer to ppd-make-and-model */ |
00f3aaf5 | 355 | *ppd_name; /* Pointer to ppd-name */ |
05e571cf | 356 | cups_option_t option; /* in/exclude-schemes option */ |
38138d28 | 357 | |
358 | ||
38138d28 | 359 | /* |
904836ce | 360 | * Build a CUPS_GET_PPDS request... |
38138d28 | 361 | */ |
362 | ||
00f3aaf5 | 363 | request = ippNewRequest(CUPS_GET_PPDS); |
38138d28 | 364 | |
904836ce | 365 | if (device_id) |
366 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-device-id", | |
367 | NULL, device_id); | |
368 | if (language) | |
369 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "ppd-language", | |
370 | NULL, language); | |
371 | if (make_model) | |
372 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-make-and-model", | |
373 | NULL, make_model); | |
374 | if (product) | |
375 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-product", | |
376 | NULL, product); | |
377 | ||
05e571cf | 378 | if (include_schemes) |
379 | { | |
380 | option.name = "include-schemes"; | |
381 | option.value = (char *)include_schemes; | |
382 | ||
383 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
384 | } | |
385 | ||
386 | if (exclude_schemes) | |
387 | { | |
388 | option.name = "exclude-schemes"; | |
389 | option.value = (char *)exclude_schemes; | |
390 | ||
391 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
392 | } | |
393 | ||
38138d28 | 394 | /* |
395 | * Do the request and get back a response... | |
396 | */ | |
397 | ||
05e571cf | 398 | if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL) |
38138d28 | 399 | { |
400 | /* | |
401 | * Loop through the device list and display them... | |
402 | */ | |
403 | ||
0a3ac972 | 404 | if (response->request.status.status_code > IPP_OK_CONFLICT) |
38138d28 | 405 | { |
00f3aaf5 | 406 | _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString()); |
38138d28 | 407 | ippDelete(response); |
b2e10895 | 408 | return (1); |
38138d28 | 409 | } |
410 | ||
411 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
412 | { | |
413 | /* | |
414 | * Skip leading attributes until we hit a PPD... | |
415 | */ | |
416 | ||
417 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
418 | attr = attr->next; | |
419 | ||
420 | if (attr == NULL) | |
421 | break; | |
422 | ||
423 | /* | |
424 | * Pull the needed attributes from this PPD... | |
425 | */ | |
426 | ||
904836ce | 427 | ppd_device_id = "NONE"; |
428 | ppd_language = NULL; | |
429 | ppd_make_model = NULL; | |
430 | ppd_name = NULL; | |
38138d28 | 431 | |
432 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
433 | { | |
00f3aaf5 | 434 | if (!strcmp(attr->name, "ppd-device-id") && |
38138d28 | 435 | attr->value_tag == IPP_TAG_TEXT) |
00f3aaf5 | 436 | ppd_device_id = attr->values[0].string.text; |
437 | else if (!strcmp(attr->name, "ppd-natural-language") && | |
438 | attr->value_tag == IPP_TAG_LANGUAGE) | |
439 | ppd_language = attr->values[0].string.text; | |
440 | else if (!strcmp(attr->name, "ppd-make-and-model") && | |
441 | attr->value_tag == IPP_TAG_TEXT) | |
904836ce | 442 | ppd_make_model = attr->values[0].string.text; |
00f3aaf5 | 443 | else if (!strcmp(attr->name, "ppd-name") && |
444 | attr->value_tag == IPP_TAG_NAME) | |
38138d28 | 445 | ppd_name = attr->values[0].string.text; |
446 | ||
447 | attr = attr->next; | |
448 | } | |
449 | ||
450 | /* | |
451 | * See if we have everything needed... | |
452 | */ | |
453 | ||
904836ce | 454 | if (ppd_language == NULL || ppd_make_model == NULL || ppd_name == NULL) |
38138d28 | 455 | { |
456 | if (attr == NULL) | |
457 | break; | |
458 | else | |
459 | continue; | |
460 | } | |
461 | ||
462 | /* | |
463 | * Display the device... | |
464 | */ | |
465 | ||
466 | if (long_status) | |
467 | { | |
89fd567e | 468 | _cupsLangPrintf(stdout, |
b7382443 | 469 | _("Model: name = %s\n" |
470 | " natural_language = %s\n" | |
00f3aaf5 | 471 | " make-and-model = %s\n" |
472 | " device-id = %s\n"), | |
904836ce | 473 | ppd_name, ppd_language, ppd_make_model, ppd_device_id); |
38138d28 | 474 | } |
475 | else | |
904836ce | 476 | _cupsLangPrintf(stdout, "%s %s\n", ppd_name, ppd_make_model); |
38138d28 | 477 | |
478 | if (attr == NULL) | |
479 | break; | |
480 | } | |
481 | ||
482 | ippDelete(response); | |
483 | } | |
484 | else | |
b2e10895 | 485 | { |
00f3aaf5 | 486 | _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString()); |
b2e10895 | 487 | |
488 | return (1); | |
489 | } | |
490 | ||
491 | return (0); | |
38138d28 | 492 | } |
493 | ||
494 | ||
495 | /* | |
b2e10895 | 496 | * End of "$Id$". |
38138d28 | 497 | */ |