]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpinfo.c
Merge CUPS 1.4svn-r7493.
[thirdparty/cups.git] / systemv / lpinfo.c
1 /*
2 * "$Id: lpinfo.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * "lpinfo" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * main() - Parse options and show information.
18 * device_cb - Device callback.
19 * show_devices() - Show available devices.
20 * show_models() - Show available PPDs.
21 */
22
23 /*
24 * Include necessary headers...
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <cups/string.h>
31 #include <cups/cups.h>
32 #include <cups/i18n.h>
33 #include <cups/debug.h>
34
35
36 /*
37 * Local functions...
38 */
39
40 static void device_cb(const char *device_clas, const char *device_id,
41 const char *device_info,
42 const char *device_make_and_model,
43 const char *device_uri, void *user_data);
44 static int show_devices(http_t *, int);
45 static int show_models(http_t *, int);
46
47
48 /*
49 * 'main()' - Parse options and show status information.
50 */
51
52 int
53 main(int argc, /* I - Number of command-line arguments */
54 char *argv[]) /* I - Command-line arguments */
55 {
56 int i; /* Looping var */
57 http_t *http; /* Connection to server */
58 int long_status; /* Long listing? */
59
60
61 _cupsSetLocale(argv);
62
63 http = NULL;
64 long_status = 0;
65
66 for (i = 1; i < argc; i ++)
67 if (argv[i][0] == '-')
68 switch (argv[i][1])
69 {
70 case 'E' : /* Encrypt */
71 #ifdef HAVE_SSL
72 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
73
74 if (http)
75 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
76 #else
77 _cupsLangPrintf(stderr,
78 _("%s: Sorry, no encryption support compiled in!\n"),
79 argv[0]);
80 #endif /* HAVE_SSL */
81 break;
82
83 case 'l' : /* Show long listing */
84 long_status = 1;
85 break;
86
87 case 'm' : /* Show models */
88 if (!http)
89 {
90 http = httpConnectEncrypt(cupsServer(), ippPort(),
91 cupsEncryption());
92
93 if (http == NULL)
94 {
95 _cupsLangPrintf(stderr,
96 _("lpinfo: Unable to connect to server: %s\n"),
97 strerror(errno));
98 return (1);
99 }
100 }
101
102 if (show_models(http, long_status))
103 return (1);
104 break;
105
106 case 'v' : /* Show available devices */
107 if (!http)
108 {
109 http = httpConnectEncrypt(cupsServer(), ippPort(),
110 cupsEncryption());
111
112 if (http == NULL)
113 {
114 _cupsLangPrintf(stderr,
115 _("lpinfo: Unable to connect to server: %s\n"),
116 strerror(errno));
117 return (1);
118 }
119 }
120
121 if (show_devices(http, long_status))
122 return (1);
123 break;
124
125 case 'h' : /* Connect to host */
126 if (http)
127 {
128 httpClose(http);
129 http = NULL;
130 }
131
132 if (argv[i][2] != '\0')
133 cupsSetServer(argv[i] + 2);
134 else
135 {
136 i ++;
137
138 if (i >= argc)
139 {
140 _cupsLangPuts(stderr,
141 _("Error: need hostname after \'-h\' option!\n"));
142 return (1);
143 }
144
145 cupsSetServer(argv[i]);
146 }
147 break;
148
149 default :
150 _cupsLangPrintf(stderr, _("lpinfo: Unknown option \'%c\'!\n"),
151 argv[i][1]);
152 return (1);
153 }
154 else
155 {
156 _cupsLangPrintf(stderr, _("lpinfo: Unknown argument \'%s\'!\n"),
157 argv[i]);
158 return (1);
159 }
160
161 return (0);
162 }
163
164
165 /*
166 * 'device_cb()' - Device callback.
167 */
168
169 static void
170 device_cb(
171 const char *device_class, /* I - device-class string */
172 const char *device_id, /* I - device-id string */
173 const char *device_info, /* I - device-info string */
174 const char *device_make_and_model, /* I - device-make-and-model string */
175 const char *device_uri, /* I - device-uri string */
176 void *user_data) /* I - User data */
177 {
178 int *long_status; /* Show verbose info? */
179
180
181 /*
182 * Display the device...
183 */
184
185 long_status = (int *)user_data;
186
187 if (*long_status)
188 {
189 _cupsLangPrintf(stdout,
190 _("Device: uri = %s\n"
191 " class = %s\n"
192 " info = %s\n"
193 " make-and-model = %s\n"
194 " device-id = %s\n"),
195 device_uri, device_class, device_info,
196 device_make_and_model, device_id);
197 }
198 else
199 _cupsLangPrintf(stdout, "%s %s\n", device_class, device_uri);
200 }
201
202
203 /*
204 * 'show_devices()' - Show available devices.
205 */
206
207 static int /* O - 0 on success, 1 on failure */
208 show_devices(http_t *http, /* I - HTTP connection to server */
209 int long_status) /* I - Long status report? */
210 {
211 if (cupsGetDevices(http, CUPS_TIMEOUT_DEFAULT, CUPS_EXCLUDE_NONE, device_cb,
212 &long_status) != IPP_OK)
213 {
214 _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
215 return (1);
216 }
217
218 return (0);
219 }
220
221
222 /*
223 * 'show_models()' - Show available PPDs.
224 */
225
226 static int /* O - 0 on success, 1 on failure */
227 show_models(http_t *http, /* I - HTTP connection to server */
228 int long_status) /* I - Long status report? */
229 {
230 ipp_t *request, /* IPP Request */
231 *response; /* IPP Response */
232 ipp_attribute_t *attr; /* Current attribute */
233 const char *ppd_device_id, /* Pointer to ppd-device-id */
234 *ppd_language, /* Pointer to ppd-natural-language */
235 *ppd_make, /* Pointer to ppd-make-and-model */
236 *ppd_name; /* Pointer to ppd-name */
237
238
239 if (http == NULL)
240 return (1);
241
242 /*
243 * Build a CUPS_GET_PPDS request, which requires the following
244 * attributes:
245 *
246 * attributes-charset
247 * attributes-natural-language
248 */
249
250 request = ippNewRequest(CUPS_GET_PPDS);
251
252 /*
253 * Do the request and get back a response...
254 */
255
256 if ((response = cupsDoRequest(http, request, "/")) != NULL)
257 {
258 /*
259 * Loop through the device list and display them...
260 */
261
262 if (response->request.status.status_code > IPP_OK_CONFLICT)
263 {
264 _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
265 ippDelete(response);
266 return (1);
267 }
268
269 for (attr = response->attrs; attr != NULL; attr = attr->next)
270 {
271 /*
272 * Skip leading attributes until we hit a PPD...
273 */
274
275 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
276 attr = attr->next;
277
278 if (attr == NULL)
279 break;
280
281 /*
282 * Pull the needed attributes from this PPD...
283 */
284
285 ppd_device_id = "NONE";
286 ppd_language = NULL;
287 ppd_make = NULL;
288 ppd_name = NULL;
289
290 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
291 {
292 if (!strcmp(attr->name, "ppd-device-id") &&
293 attr->value_tag == IPP_TAG_TEXT)
294 ppd_device_id = attr->values[0].string.text;
295 else if (!strcmp(attr->name, "ppd-natural-language") &&
296 attr->value_tag == IPP_TAG_LANGUAGE)
297 ppd_language = attr->values[0].string.text;
298 else if (!strcmp(attr->name, "ppd-make-and-model") &&
299 attr->value_tag == IPP_TAG_TEXT)
300 ppd_make = attr->values[0].string.text;
301 else if (!strcmp(attr->name, "ppd-name") &&
302 attr->value_tag == IPP_TAG_NAME)
303 ppd_name = attr->values[0].string.text;
304
305 attr = attr->next;
306 }
307
308 /*
309 * See if we have everything needed...
310 */
311
312 if (ppd_language == NULL || ppd_make == NULL || ppd_name == NULL)
313 {
314 if (attr == NULL)
315 break;
316 else
317 continue;
318 }
319
320 /*
321 * Display the device...
322 */
323
324 if (long_status)
325 {
326 _cupsLangPrintf(stdout,
327 _("Model: name = %s\n"
328 " natural_language = %s\n"
329 " make-and-model = %s\n"
330 " device-id = %s\n"),
331 ppd_name, ppd_language, ppd_make, ppd_device_id);
332 }
333 else
334 _cupsLangPrintf(stdout, "%s %s\n", ppd_name, ppd_make);
335
336 if (attr == NULL)
337 break;
338 }
339
340 ippDelete(response);
341 }
342 else
343 {
344 _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
345
346 return (1);
347 }
348
349 return (0);
350 }
351
352
353 /*
354 * End of "$Id: lpinfo.c 6649 2007-07-11 21:46:42Z mike $".
355 */