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