2 * "$Id: lpinfo.c,v 1.3 2001/01/23 17:36:23 mike Exp $"
4 * "lpinfo" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2001 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Parse options and show information.
27 * show_devices() - Show available devices.
28 * show_models() - Show available PPDs.
32 * Include necessary headers...
38 #include <cups/cups.h>
39 #include <cups/language.h>
40 #include <cups/debug.h>
41 #include <cups/string.h>
48 static void show_devices(http_t
*, int);
49 static void show_models(http_t
*, int);
53 * 'main()' - Parse options and show status information.
57 main(int argc
, /* I - Number of command-line arguments */
58 char *argv
[]) /* I - Command-line arguments */
60 int i
; /* Looping var */
61 http_t
*http
; /* Connection to server */
62 int long_status
; /* Long listing? */
63 http_encryption_t encryption
; /* Encryption? */
68 encryption
= cupsEncryption();
70 for (i
= 1; i
< argc
; i
++)
71 if (argv
[i
][0] == '-')
74 case 'E' : /* Encrypt */
76 encryption
= HTTP_ENCRYPT_REQUIRED
;
79 httpEncryption(http
, encryption
);
81 fprintf(stderr
, "%s: Sorry, no encryption support compiled in!\n",
83 #endif /* HAVE_LIBSSL */
86 case 'l' : /* Show long listing */
90 case 'm' : /* Show models */
93 http
= httpConnect(cupsServer(), ippPort());
97 perror("lpinfo: Unable to connect to server");
101 httpEncryption(http
, encryption
);
104 show_models(http
, long_status
);
107 case 'v' : /* Show available devices */
110 http
= httpConnect(cupsServer(), ippPort());
114 perror("lpinfo: Unable to connect to server");
118 httpEncryption(http
, encryption
);
121 show_devices(http
, long_status
);
124 case 'h' : /* Connect to host */
128 if (argv
[i
][2] != '\0')
129 http
= httpConnect(argv
[i
] + 2, ippPort());
136 fputs("Error: need hostname after \'-h\' option!\n", stderr
);
140 http
= httpConnect(argv
[i
], ippPort());
145 perror("lpinfo: Unable to connect to server");
149 httpEncryption(http
, encryption
);
153 fprintf(stderr
, "lpinfo: Unknown option \'%c\'!\n", argv
[i
][1]);
158 fprintf(stderr
, "lpinfo: Unknown argument \'%s\'!\n", argv
[i
]);
167 * 'show_devices()' - Show available devices.
171 show_devices(http_t
*http
, /* I - HTTP connection to server */
172 int long_status
)/* I - Long status report? */
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 */
188 * Build a CUPS_GET_DEVICES request, which requires the following
192 * attributes-natural-language
198 request
->request
.op
.operation_id
= CUPS_GET_DEVICES
;
199 request
->request
.op
.request_id
= 1;
201 language
= cupsLangDefault();
203 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
204 "attributes-charset", NULL
, cupsLangEncoding(language
));
206 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
207 "attributes-natural-language", NULL
, language
->language
);
209 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
210 NULL
, "ipp://localhost/printers/");
213 * Do the request and get back a response...
216 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
219 * Loop through the device list and display them...
222 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
224 fprintf(stderr
, "lpinfo: cups-get-devices failed: %s\n",
225 ippErrorString(response
->request
.status
.status_code
));
230 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
233 * Skip leading attributes until we hit a device...
236 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
243 * Pull the needed attributes from this device...
251 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
253 if (strcmp(attr
->name
, "device-class") == 0 &&
254 attr
->value_tag
== IPP_TAG_KEYWORD
)
255 device_class
= attr
->values
[0].string
.text
;
257 if (strcmp(attr
->name
, "device-info") == 0 &&
258 attr
->value_tag
== IPP_TAG_TEXT
)
259 device_info
= attr
->values
[0].string
.text
;
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
;
265 if (strcmp(attr
->name
, "device-uri") == 0 &&
266 attr
->value_tag
== IPP_TAG_URI
)
267 device_uri
= attr
->values
[0].string
.text
;
273 * See if we have everything needed...
276 if (device_class
== NULL
|| device_info
== NULL
||
277 device_make
== NULL
|| device_uri
== NULL
)
286 * Display the device...
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
);
297 printf("%s %s\n", device_class
, device_uri
);
306 fprintf(stderr
, "lpinfo: cups-get-devices failed: %s\n",
307 ippErrorString(cupsLastError()));
312 * 'show_models()' - Show available PPDs.
316 show_models(http_t
*http
, /* I - HTTP connection to server */
317 int long_status
) /* I - Long status report? */
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 */
332 * Build a CUPS_GET_PPDS request, which requires the following
336 * attributes-natural-language
342 request
->request
.op
.operation_id
= CUPS_GET_PPDS
;
343 request
->request
.op
.request_id
= 1;
345 language
= cupsLangDefault();
347 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
348 "attributes-charset", NULL
, cupsLangEncoding(language
));
350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
351 "attributes-natural-language", NULL
, language
->language
);
353 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
354 NULL
, "ipp://localhost/printers/");
357 * Do the request and get back a response...
360 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
363 * Loop through the device list and display them...
366 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
368 fprintf(stderr
, "lpinfo: cups-get-ppds failed: %s\n",
369 ippErrorString(response
->request
.status
.status_code
));
374 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
377 * Skip leading attributes until we hit a PPD...
380 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
387 * Pull the needed attributes from this PPD...
394 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
396 if (strcmp(attr
->name
, "ppd-natural-language") == 0 &&
397 attr
->value_tag
== IPP_TAG_LANGUAGE
)
398 ppd_language
= attr
->values
[0].string
.text
;
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
;
404 if (strcmp(attr
->name
, "ppd-name") == 0 &&
405 attr
->value_tag
== IPP_TAG_NAME
)
406 ppd_name
= attr
->values
[0].string
.text
;
412 * See if we have everything needed...
415 if (ppd_language
== NULL
|| ppd_make
== NULL
|| ppd_name
== NULL
)
424 * Display the device...
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
);
434 printf("%s %s\n", ppd_name
, ppd_make
);
443 fprintf(stderr
, "lpinfo: cups-get-ppds failed: %s\n",
444 ippErrorString(cupsLastError()));
449 * End of "$Id: lpinfo.c,v 1.3 2001/01/23 17:36:23 mike Exp $".