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