]>
Commit | Line | Data |
---|---|---|
38138d28 | 1 | /* |
efb2f309 | 2 | * "$Id: lpinfo.c,v 1.6 2002/01/02 17:59:19 mike Exp $" |
38138d28 | 3 | * |
4 | * "lpinfo" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
efb2f309 | 6 | * Copyright 1997-2002 by Easy Software Products. |
38138d28 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Parse options and show information. | |
27 | * show_devices() - Show available devices. | |
28 | * show_models() - Show available PPDs. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Include necessary headers... | |
33 | */ | |
34 | ||
35 | #include <stdio.h> | |
36 | #include <stdlib.h> | |
37 | #include <ctype.h> | |
38 | #include <cups/cups.h> | |
39 | #include <cups/language.h> | |
40 | #include <cups/debug.h> | |
41 | #include <cups/string.h> | |
42 | ||
43 | ||
44 | /* | |
45 | * Local functions... | |
46 | */ | |
47 | ||
2dfe0f01 | 48 | static int show_devices(http_t *, int); |
49 | static int show_models(http_t *, int); | |
38138d28 | 50 | |
51 | ||
52 | /* | |
53 | * 'main()' - Parse options and show status information. | |
54 | */ | |
55 | ||
56 | int | |
57 | main(int argc, /* I - Number of command-line arguments */ | |
58 | char *argv[]) /* I - Command-line arguments */ | |
59 | { | |
60 | int i; /* Looping var */ | |
61 | http_t *http; /* Connection to server */ | |
62 | int long_status; /* Long listing? */ | |
1c9e0181 | 63 | http_encryption_t encryption; /* Encryption? */ |
38138d28 | 64 | |
65 | ||
66 | http = NULL; | |
67 | long_status = 0; | |
1c9e0181 | 68 | encryption = cupsEncryption(); |
38138d28 | 69 | |
70 | for (i = 1; i < argc; i ++) | |
71 | if (argv[i][0] == '-') | |
72 | switch (argv[i][1]) | |
73 | { | |
1c9e0181 | 74 | case 'E' : /* Encrypt */ |
75 | #ifdef HAVE_LIBSSL | |
76 | encryption = HTTP_ENCRYPT_REQUIRED; | |
77 | ||
78 | if (http) | |
79 | httpEncryption(http, encryption); | |
80 | #else | |
81 | fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n", | |
82 | argv[0]); | |
83 | #endif /* HAVE_LIBSSL */ | |
84 | break; | |
85 | ||
38138d28 | 86 | case 'l' : /* Show long listing */ |
87 | long_status = 1; | |
88 | break; | |
89 | ||
90 | case 'm' : /* Show models */ | |
91 | if (!http) | |
92 | { | |
a1793153 | 93 | http = httpConnectEncrypt(cupsServer(), ippPort(), encryption); |
38138d28 | 94 | |
95 | if (http == NULL) | |
96 | { | |
97 | perror("lpinfo: Unable to connect to server"); | |
98 | return (1); | |
99 | } | |
100 | } | |
101 | ||
2dfe0f01 | 102 | if (show_models(http, long_status)) |
103 | return (1); | |
38138d28 | 104 | break; |
105 | ||
106 | case 'v' : /* Show available devices */ | |
107 | if (!http) | |
108 | { | |
a1793153 | 109 | http = httpConnectEncrypt(cupsServer(), ippPort(), encryption); |
38138d28 | 110 | |
111 | if (http == NULL) | |
112 | { | |
113 | perror("lpinfo: Unable to connect to server"); | |
114 | return (1); | |
115 | } | |
116 | } | |
117 | ||
2dfe0f01 | 118 | if (show_devices(http, long_status)) |
119 | return (1); | |
38138d28 | 120 | break; |
121 | ||
122 | case 'h' : /* Connect to host */ | |
123 | if (http) | |
124 | httpClose(http); | |
125 | ||
126 | if (argv[i][2] != '\0') | |
a1793153 | 127 | http = httpConnectEncrypt(argv[i] + 2, ippPort(), encryption); |
38138d28 | 128 | else |
129 | { | |
130 | i ++; | |
131 | ||
132 | if (i >= argc) | |
133 | { | |
134 | fputs("Error: need hostname after \'-h\' option!\n", stderr); | |
135 | return (1); | |
136 | } | |
137 | ||
a1793153 | 138 | http = httpConnectEncrypt(argv[i], ippPort(), encryption); |
38138d28 | 139 | } |
140 | ||
141 | if (http == NULL) | |
142 | { | |
143 | perror("lpinfo: Unable to connect to server"); | |
144 | return (1); | |
145 | } | |
146 | break; | |
147 | ||
148 | default : | |
149 | fprintf(stderr, "lpinfo: Unknown option \'%c\'!\n", argv[i][1]); | |
150 | return (1); | |
151 | } | |
152 | else | |
153 | { | |
154 | fprintf(stderr, "lpinfo: Unknown argument \'%s\'!\n", argv[i]); | |
155 | return (1); | |
156 | } | |
157 | ||
158 | return (0); | |
159 | } | |
160 | ||
161 | ||
162 | /* | |
163 | * 'show_devices()' - Show available devices. | |
164 | */ | |
165 | ||
2dfe0f01 | 166 | static int /* O - 0 on success, 1 on failure */ |
38138d28 | 167 | show_devices(http_t *http, /* I - HTTP connection to server */ |
168 | int long_status)/* I - Long status report? */ | |
169 | { | |
170 | ipp_t *request, /* IPP Request */ | |
171 | *response; /* IPP Response */ | |
172 | ipp_attribute_t *attr; /* Current attribute */ | |
173 | cups_lang_t *language; /* Default language */ | |
174 | const char *device_class, /* Pointer into device-class */ | |
175 | *device_info, /* Pointer into device-info */ | |
176 | *device_make, /* Pointer into device-make-and-model */ | |
177 | *device_uri; /* Pointer into device-uri */ | |
178 | ||
179 | ||
180 | if (http == NULL) | |
2dfe0f01 | 181 | return (1); |
38138d28 | 182 | |
183 | /* | |
184 | * Build a CUPS_GET_DEVICES request, which requires the following | |
185 | * attributes: | |
186 | * | |
187 | * attributes-charset | |
188 | * attributes-natural-language | |
189 | * printer-uri | |
190 | */ | |
191 | ||
192 | request = ippNew(); | |
193 | ||
194 | request->request.op.operation_id = CUPS_GET_DEVICES; | |
195 | request->request.op.request_id = 1; | |
196 | ||
197 | language = cupsLangDefault(); | |
198 | ||
199 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
200 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
201 | ||
202 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
203 | "attributes-natural-language", NULL, language->language); | |
204 | ||
205 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
206 | NULL, "ipp://localhost/printers/"); | |
207 | ||
208 | /* | |
209 | * Do the request and get back a response... | |
210 | */ | |
211 | ||
212 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
213 | { | |
214 | /* | |
215 | * Loop through the device list and display them... | |
216 | */ | |
217 | ||
218 | if (response->request.status.status_code > IPP_OK_CONFLICT) | |
219 | { | |
220 | fprintf(stderr, "lpinfo: cups-get-devices failed: %s\n", | |
221 | ippErrorString(response->request.status.status_code)); | |
222 | ippDelete(response); | |
2dfe0f01 | 223 | return (1); |
38138d28 | 224 | } |
225 | ||
226 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
227 | { | |
228 | /* | |
229 | * Skip leading attributes until we hit a device... | |
230 | */ | |
231 | ||
232 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
233 | attr = attr->next; | |
234 | ||
235 | if (attr == NULL) | |
236 | break; | |
237 | ||
238 | /* | |
239 | * Pull the needed attributes from this device... | |
240 | */ | |
241 | ||
242 | device_class = NULL; | |
243 | device_info = NULL; | |
244 | device_make = NULL; | |
245 | device_uri = NULL; | |
246 | ||
247 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
248 | { | |
249 | if (strcmp(attr->name, "device-class") == 0 && | |
250 | attr->value_tag == IPP_TAG_KEYWORD) | |
251 | device_class = attr->values[0].string.text; | |
252 | ||
253 | if (strcmp(attr->name, "device-info") == 0 && | |
254 | attr->value_tag == IPP_TAG_TEXT) | |
255 | device_info = attr->values[0].string.text; | |
256 | ||
257 | if (strcmp(attr->name, "device-make-and-model") == 0 && | |
258 | attr->value_tag == IPP_TAG_TEXT) | |
259 | device_make = attr->values[0].string.text; | |
260 | ||
261 | if (strcmp(attr->name, "device-uri") == 0 && | |
262 | attr->value_tag == IPP_TAG_URI) | |
263 | device_uri = attr->values[0].string.text; | |
264 | ||
265 | attr = attr->next; | |
266 | } | |
267 | ||
268 | /* | |
269 | * See if we have everything needed... | |
270 | */ | |
271 | ||
272 | if (device_class == NULL || device_info == NULL || | |
273 | device_make == NULL || device_uri == NULL) | |
274 | { | |
275 | if (attr == NULL) | |
276 | break; | |
277 | else | |
278 | continue; | |
279 | } | |
280 | ||
281 | /* | |
282 | * Display the device... | |
283 | */ | |
284 | ||
285 | if (long_status) | |
286 | { | |
287 | printf("Device: uri = %s\n", device_uri); | |
288 | printf(" class = %s\n", device_class); | |
289 | printf(" info = %s\n", device_info); | |
290 | printf(" make-and-model = %s\n", device_make); | |
291 | } | |
292 | else | |
293 | printf("%s %s\n", device_class, device_uri); | |
294 | ||
295 | if (attr == NULL) | |
296 | break; | |
297 | } | |
298 | ||
299 | ippDelete(response); | |
300 | } | |
301 | else | |
2dfe0f01 | 302 | { |
38138d28 | 303 | fprintf(stderr, "lpinfo: cups-get-devices failed: %s\n", |
304 | ippErrorString(cupsLastError())); | |
2dfe0f01 | 305 | return (1); |
306 | } | |
307 | ||
308 | return (0); | |
38138d28 | 309 | } |
310 | ||
311 | ||
312 | /* | |
313 | * 'show_models()' - Show available PPDs. | |
314 | */ | |
315 | ||
2dfe0f01 | 316 | static int /* O - 0 on success, 1 on failure */ |
38138d28 | 317 | show_models(http_t *http, /* I - HTTP connection to server */ |
318 | int long_status) /* I - Long status report? */ | |
319 | { | |
320 | ipp_t *request, /* IPP Request */ | |
321 | *response; /* IPP Response */ | |
322 | ipp_attribute_t *attr; /* Current attribute */ | |
323 | cups_lang_t *language; /* Default language */ | |
324 | const char *ppd_language, /* Pointer into ppd-natural-language */ | |
325 | *ppd_make, /* Pointer into ppd-make-and-model */ | |
326 | *ppd_name; /* Pointer into ppd-name */ | |
327 | ||
328 | ||
329 | if (http == NULL) | |
2dfe0f01 | 330 | return (1); |
38138d28 | 331 | |
332 | /* | |
333 | * Build a CUPS_GET_PPDS request, which requires the following | |
334 | * attributes: | |
335 | * | |
336 | * attributes-charset | |
337 | * attributes-natural-language | |
338 | * printer-uri | |
339 | */ | |
340 | ||
341 | request = ippNew(); | |
342 | ||
343 | request->request.op.operation_id = CUPS_GET_PPDS; | |
344 | request->request.op.request_id = 1; | |
345 | ||
346 | language = cupsLangDefault(); | |
347 | ||
348 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
349 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
350 | ||
351 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
352 | "attributes-natural-language", NULL, language->language); | |
353 | ||
354 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
355 | NULL, "ipp://localhost/printers/"); | |
356 | ||
357 | /* | |
358 | * Do the request and get back a response... | |
359 | */ | |
360 | ||
361 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
362 | { | |
363 | /* | |
364 | * Loop through the device list and display them... | |
365 | */ | |
366 | ||
367 | if (response->request.status.status_code > IPP_OK_CONFLICT) | |
368 | { | |
369 | fprintf(stderr, "lpinfo: cups-get-ppds failed: %s\n", | |
370 | ippErrorString(response->request.status.status_code)); | |
371 | ippDelete(response); | |
2dfe0f01 | 372 | return (1); |
38138d28 | 373 | } |
374 | ||
375 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
376 | { | |
377 | /* | |
378 | * Skip leading attributes until we hit a PPD... | |
379 | */ | |
380 | ||
381 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
382 | attr = attr->next; | |
383 | ||
384 | if (attr == NULL) | |
385 | break; | |
386 | ||
387 | /* | |
388 | * Pull the needed attributes from this PPD... | |
389 | */ | |
390 | ||
391 | ppd_language = NULL; | |
392 | ppd_make = NULL; | |
393 | ppd_name = NULL; | |
394 | ||
395 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
396 | { | |
397 | if (strcmp(attr->name, "ppd-natural-language") == 0 && | |
398 | attr->value_tag == IPP_TAG_LANGUAGE) | |
399 | ppd_language = attr->values[0].string.text; | |
400 | ||
401 | if (strcmp(attr->name, "ppd-make-and-model") == 0 && | |
402 | attr->value_tag == IPP_TAG_TEXT) | |
403 | ppd_make = attr->values[0].string.text; | |
404 | ||
405 | if (strcmp(attr->name, "ppd-name") == 0 && | |
406 | attr->value_tag == IPP_TAG_NAME) | |
407 | ppd_name = attr->values[0].string.text; | |
408 | ||
409 | attr = attr->next; | |
410 | } | |
411 | ||
412 | /* | |
413 | * See if we have everything needed... | |
414 | */ | |
415 | ||
416 | if (ppd_language == NULL || ppd_make == NULL || ppd_name == NULL) | |
417 | { | |
418 | if (attr == NULL) | |
419 | break; | |
420 | else | |
421 | continue; | |
422 | } | |
423 | ||
424 | /* | |
425 | * Display the device... | |
426 | */ | |
427 | ||
428 | if (long_status) | |
429 | { | |
430 | printf("Model: name = %s\n", ppd_name); | |
431 | printf(" natural_language = %s\n", ppd_language); | |
432 | printf(" make-and-model = %s\n", ppd_make); | |
433 | } | |
434 | else | |
435 | printf("%s %s\n", ppd_name, ppd_make); | |
436 | ||
437 | if (attr == NULL) | |
438 | break; | |
439 | } | |
440 | ||
441 | ippDelete(response); | |
442 | } | |
443 | else | |
2dfe0f01 | 444 | { |
38138d28 | 445 | fprintf(stderr, "lpinfo: cups-get-ppds failed: %s\n", |
446 | ippErrorString(cupsLastError())); | |
2dfe0f01 | 447 | |
448 | return (1); | |
449 | } | |
450 | ||
451 | return (0); | |
38138d28 | 452 | } |
453 | ||
454 | ||
455 | /* | |
efb2f309 | 456 | * End of "$Id: lpinfo.c,v 1.6 2002/01/02 17:59:19 mike Exp $". |
38138d28 | 457 | */ |