]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/lpinfo.c
Fix printer restart bug (printer started after stopping a job)
[thirdparty/cups.git] / systemv / lpinfo.c
CommitLineData
38138d28 1/*
1c9e0181 2 * "$Id: lpinfo.c,v 1.3 2001/01/23 17:36:23 mike Exp $"
38138d28 3 *
4 * "lpinfo" command for the Common UNIX Printing System (CUPS).
5 *
d2935a0f 6 * Copyright 1997-2001 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
48static void show_devices(http_t *, int);
49static void show_models(http_t *, int);
50
51
52/*
53 * 'main()' - Parse options and show status information.
54 */
55
56int
57main(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 {
93 http = httpConnect(cupsServer(), ippPort());
94
95 if (http == NULL)
96 {
97 perror("lpinfo: Unable to connect to server");
98 return (1);
99 }
1c9e0181 100
101 httpEncryption(http, encryption);
38138d28 102 }
103
104 show_models(http, long_status);
105 break;
106
107 case 'v' : /* Show available devices */
108 if (!http)
109 {
110 http = httpConnect(cupsServer(), ippPort());
111
112 if (http == NULL)
113 {
114 perror("lpinfo: Unable to connect to server");
115 return (1);
116 }
1c9e0181 117
118 httpEncryption(http, encryption);
38138d28 119 }
120
121 show_devices(http, long_status);
122 break;
123
124 case 'h' : /* Connect to host */
125 if (http)
126 httpClose(http);
127
128 if (argv[i][2] != '\0')
129 http = httpConnect(argv[i] + 2, ippPort());
130 else
131 {
132 i ++;
133
134 if (i >= argc)
135 {
136 fputs("Error: need hostname after \'-h\' option!\n", stderr);
137 return (1);
138 }
139
140 http = httpConnect(argv[i], ippPort());
141 }
142
143 if (http == NULL)
144 {
145 perror("lpinfo: Unable to connect to server");
146 return (1);
147 }
1c9e0181 148
149 httpEncryption(http, encryption);
38138d28 150 break;
151
152 default :
153 fprintf(stderr, "lpinfo: Unknown option \'%c\'!\n", argv[i][1]);
154 return (1);
155 }
156 else
157 {
158 fprintf(stderr, "lpinfo: Unknown argument \'%s\'!\n", argv[i]);
159 return (1);
160 }
161
162 return (0);
163}
164
165
166/*
167 * 'show_devices()' - Show available devices.
168 */
169
170static void
171show_devices(http_t *http, /* I - HTTP connection to server */
172 int long_status)/* I - Long status report? */
173{
174 ipp_t *request, /* IPP Request */
175 *response; /* IPP Response */
176 ipp_attribute_t *attr; /* Current attribute */
177 cups_lang_t *language; /* Default language */
178 const char *device_class, /* Pointer into device-class */
179 *device_info, /* Pointer into device-info */
180 *device_make, /* Pointer into device-make-and-model */
181 *device_uri; /* Pointer into device-uri */
182
183
184 if (http == NULL)
185 return;
186
187 /*
188 * Build a CUPS_GET_DEVICES request, which requires the following
189 * attributes:
190 *
191 * attributes-charset
192 * attributes-natural-language
193 * printer-uri
194 */
195
196 request = ippNew();
197
198 request->request.op.operation_id = CUPS_GET_DEVICES;
199 request->request.op.request_id = 1;
200
201 language = cupsLangDefault();
202
203 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
204 "attributes-charset", NULL, cupsLangEncoding(language));
205
206 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
207 "attributes-natural-language", NULL, language->language);
208
209 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
210 NULL, "ipp://localhost/printers/");
211
212 /*
213 * Do the request and get back a response...
214 */
215
216 if ((response = cupsDoRequest(http, request, "/")) != NULL)
217 {
218 /*
219 * Loop through the device list and display them...
220 */
221
222 if (response->request.status.status_code > IPP_OK_CONFLICT)
223 {
224 fprintf(stderr, "lpinfo: cups-get-devices failed: %s\n",
225 ippErrorString(response->request.status.status_code));
226 ippDelete(response);
227 return;
228 }
229
230 for (attr = response->attrs; attr != NULL; attr = attr->next)
231 {
232 /*
233 * Skip leading attributes until we hit a device...
234 */
235
236 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
237 attr = attr->next;
238
239 if (attr == NULL)
240 break;
241
242 /*
243 * Pull the needed attributes from this device...
244 */
245
246 device_class = NULL;
247 device_info = NULL;
248 device_make = NULL;
249 device_uri = NULL;
250
251 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
252 {
253 if (strcmp(attr->name, "device-class") == 0 &&
254 attr->value_tag == IPP_TAG_KEYWORD)
255 device_class = attr->values[0].string.text;
256
257 if (strcmp(attr->name, "device-info") == 0 &&
258 attr->value_tag == IPP_TAG_TEXT)
259 device_info = attr->values[0].string.text;
260
261 if (strcmp(attr->name, "device-make-and-model") == 0 &&
262 attr->value_tag == IPP_TAG_TEXT)
263 device_make = attr->values[0].string.text;
264
265 if (strcmp(attr->name, "device-uri") == 0 &&
266 attr->value_tag == IPP_TAG_URI)
267 device_uri = attr->values[0].string.text;
268
269 attr = attr->next;
270 }
271
272 /*
273 * See if we have everything needed...
274 */
275
276 if (device_class == NULL || device_info == NULL ||
277 device_make == NULL || device_uri == NULL)
278 {
279 if (attr == NULL)
280 break;
281 else
282 continue;
283 }
284
285 /*
286 * Display the device...
287 */
288
289 if (long_status)
290 {
291 printf("Device: uri = %s\n", device_uri);
292 printf(" class = %s\n", device_class);
293 printf(" info = %s\n", device_info);
294 printf(" make-and-model = %s\n", device_make);
295 }
296 else
297 printf("%s %s\n", device_class, device_uri);
298
299 if (attr == NULL)
300 break;
301 }
302
303 ippDelete(response);
304 }
305 else
306 fprintf(stderr, "lpinfo: cups-get-devices failed: %s\n",
307 ippErrorString(cupsLastError()));
308}
309
310
311/*
312 * 'show_models()' - Show available PPDs.
313 */
314
315static void
316show_models(http_t *http, /* I - HTTP connection to server */
317 int long_status) /* I - Long status report? */
318{
319 ipp_t *request, /* IPP Request */
320 *response; /* IPP Response */
321 ipp_attribute_t *attr; /* Current attribute */
322 cups_lang_t *language; /* Default language */
323 const char *ppd_language, /* Pointer into ppd-natural-language */
324 *ppd_make, /* Pointer into ppd-make-and-model */
325 *ppd_name; /* Pointer into ppd-name */
326
327
328 if (http == NULL)
329 return;
330
331 /*
332 * Build a CUPS_GET_PPDS request, which requires the following
333 * attributes:
334 *
335 * attributes-charset
336 * attributes-natural-language
337 * printer-uri
338 */
339
340 request = ippNew();
341
342 request->request.op.operation_id = CUPS_GET_PPDS;
343 request->request.op.request_id = 1;
344
345 language = cupsLangDefault();
346
347 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
348 "attributes-charset", NULL, cupsLangEncoding(language));
349
350 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
351 "attributes-natural-language", NULL, language->language);
352
353 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
354 NULL, "ipp://localhost/printers/");
355
356 /*
357 * Do the request and get back a response...
358 */
359
360 if ((response = cupsDoRequest(http, request, "/")) != NULL)
361 {
362 /*
363 * Loop through the device list and display them...
364 */
365
366 if (response->request.status.status_code > IPP_OK_CONFLICT)
367 {
368 fprintf(stderr, "lpinfo: cups-get-ppds failed: %s\n",
369 ippErrorString(response->request.status.status_code));
370 ippDelete(response);
371 return;
372 }
373
374 for (attr = response->attrs; attr != NULL; attr = attr->next)
375 {
376 /*
377 * Skip leading attributes until we hit a PPD...
378 */
379
380 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
381 attr = attr->next;
382
383 if (attr == NULL)
384 break;
385
386 /*
387 * Pull the needed attributes from this PPD...
388 */
389
390 ppd_language = NULL;
391 ppd_make = NULL;
392 ppd_name = NULL;
393
394 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
395 {
396 if (strcmp(attr->name, "ppd-natural-language") == 0 &&
397 attr->value_tag == IPP_TAG_LANGUAGE)
398 ppd_language = attr->values[0].string.text;
399
400 if (strcmp(attr->name, "ppd-make-and-model") == 0 &&
401 attr->value_tag == IPP_TAG_TEXT)
402 ppd_make = attr->values[0].string.text;
403
404 if (strcmp(attr->name, "ppd-name") == 0 &&
405 attr->value_tag == IPP_TAG_NAME)
406 ppd_name = attr->values[0].string.text;
407
408 attr = attr->next;
409 }
410
411 /*
412 * See if we have everything needed...
413 */
414
415 if (ppd_language == NULL || ppd_make == NULL || ppd_name == NULL)
416 {
417 if (attr == NULL)
418 break;
419 else
420 continue;
421 }
422
423 /*
424 * Display the device...
425 */
426
427 if (long_status)
428 {
429 printf("Model: name = %s\n", ppd_name);
430 printf(" natural_language = %s\n", ppd_language);
431 printf(" make-and-model = %s\n", ppd_make);
432 }
433 else
434 printf("%s %s\n", ppd_name, ppd_make);
435
436 if (attr == NULL)
437 break;
438 }
439
440 ippDelete(response);
441 }
442 else
443 fprintf(stderr, "lpinfo: cups-get-ppds failed: %s\n",
444 ippErrorString(cupsLastError()));
445}
446
447
448/*
1c9e0181 449 * End of "$Id: lpinfo.c,v 1.3 2001/01/23 17:36:23 mike Exp $".
38138d28 450 */