]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpinfo.c
2 * "$Id: lpinfo.c 7810 2008-07-29 01:11:15Z mike $"
4 * "lpinfo" command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
17 * main() - Parse options and show information.
18 * device_cb - Device callback.
19 * show_devices() - Show available devices.
20 * show_models() - Show available PPDs.
24 * Include necessary headers...
30 #include <cups/string.h>
31 #include <cups/cups.h>
32 #include <cups/i18n.h>
33 #include <cups/debug.h>
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
, const char *device_location
,
45 static int show_devices(int long_status
, int timeout
,
46 const char *include_schemes
,
47 const char *exclude_schemes
);
48 static int show_models(int long_status
,
49 const char *device_id
, const char *language
,
50 const char *make_model
, const char *product
,
51 const char *include_schemes
,
52 const char *exclude_schemes
);
56 * 'main()' - Parse options and show status information.
60 main(int argc
, /* I - Number of command-line arguments */
61 char *argv
[]) /* I - Command-line arguments */
63 int i
; /* Looping var */
64 int long_status
; /* Long listing? */
65 const char *device_id
, /* 1284 device ID */
66 *language
, /* Language */
67 *make_model
, /* Make and model */
68 *product
, /* Product */
69 *include_schemes
, /* Schemes to include */
70 *exclude_schemes
; /* Schemes to exclude */
71 int timeout
; /* Device timeout */
81 include_schemes
= CUPS_INCLUDE_ALL
;
82 exclude_schemes
= CUPS_EXCLUDE_NONE
;
83 timeout
= CUPS_TIMEOUT_DEFAULT
;
85 for (i
= 1; i
< argc
; i
++)
86 if (argv
[i
][0] == '-')
89 case 'E' : /* Encrypt */
91 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
93 _cupsLangPrintf(stderr
,
94 _("%s: Sorry, no encryption support compiled in!\n"),
99 case 'h' : /* Connect to host */
100 if (argv
[i
][2] != '\0')
101 cupsSetServer(argv
[i
] + 2);
108 _cupsLangPuts(stderr
,
109 _("Error: need hostname after \'-h\' option!\n"));
113 cupsSetServer(argv
[i
]);
117 case 'l' : /* Show long listing */
121 case 'm' : /* Show models */
122 if (show_models(long_status
, device_id
, language
, make_model
,
123 product
, include_schemes
, exclude_schemes
))
127 case 'v' : /* Show available devices */
128 if (show_devices(long_status
, timeout
, include_schemes
,
133 case '-' : /* --something */
134 if (!strcmp(argv
[i
], "--device-id"))
142 _cupsLangPuts(stderr
,
143 _("lpinfo: Expected 1284 device ID string "
144 "after --device-id!\n"));
148 else if (!strncmp(argv
[i
], "--device-id=", 12) && argv
[i
][12])
150 device_id
= argv
[i
] + 12;
152 else if (!strcmp(argv
[i
], "--exclude-schemes"))
157 exclude_schemes
= argv
[i
];
160 _cupsLangPuts(stderr
,
161 _("lpinfo: Expected scheme list after "
162 "--exclude-schemes!\n"));
166 else if (!strncmp(argv
[i
], "--exclude-schemes=", 18) && argv
[i
][18])
168 exclude_schemes
= argv
[i
] + 18;
170 else if (!strcmp(argv
[i
], "--include-schemes"))
175 include_schemes
= argv
[i
];
178 _cupsLangPuts(stderr
,
179 _("lpinfo: Expected scheme list after "
180 "--include-schemes!\n"));
184 else if (!strncmp(argv
[i
], "--include-schemes=", 18) && argv
[i
][18])
186 include_schemes
= argv
[i
] + 18;
188 else if (!strcmp(argv
[i
], "--language"))
195 _cupsLangPuts(stderr
,
196 _("lpinfo: Expected language after "
201 else if (!strncmp(argv
[i
], "--language=", 11) && argv
[i
][11])
203 language
= argv
[i
] + 11;
205 else if (!strcmp(argv
[i
], "--make-and-model"))
212 _cupsLangPuts(stderr
,
213 _("lpinfo: Expected make and model after "
214 "--make-and-model!\n"));
218 else if (!strncmp(argv
[i
], "--make-and-model=", 17) && argv
[i
][17])
220 make_model
= argv
[i
] + 17;
222 else if (!strcmp(argv
[i
], "--product"))
229 _cupsLangPuts(stderr
,
230 _("lpinfo: Expected product string after "
235 else if (!strncmp(argv
[i
], "--product=", 10) && argv
[i
][10])
237 product
= argv
[i
] + 10;
239 else if (!strcmp(argv
[i
], "--timeout"))
243 timeout
= atoi(argv
[i
]);
246 _cupsLangPuts(stderr
,
247 _("lpinfo: Expected timeout after --timeout!\n"));
251 else if (!strncmp(argv
[i
], "--timeout=", 10) && argv
[i
][10])
253 timeout
= atoi(argv
[i
] + 10);
257 _cupsLangPrintf(stderr
, _("lpinfo: Unknown option \'%s\'!\n"),
264 _cupsLangPrintf(stderr
, _("lpinfo: Unknown option \'%c\'!\n"),
270 _cupsLangPrintf(stderr
, _("lpinfo: Unknown argument \'%s\'!\n"),
280 * 'device_cb()' - Device callback.
285 const char *device_class
, /* I - device-class string */
286 const char *device_id
, /* I - device-id string */
287 const char *device_info
, /* I - device-info string */
288 const char *device_make_and_model
, /* I - device-make-and-model string */
289 const char *device_uri
, /* I - device-uri string */
290 const char *device_location
, /* I - device-location string */
291 void *user_data
) /* I - User data */
293 int *long_status
; /* Show verbose info? */
297 * Display the device...
300 long_status
= (int *)user_data
;
304 _cupsLangPrintf(stdout
,
305 _("Device: uri = %s\n"
308 " make-and-model = %s\n"
311 device_uri
, device_class
, device_info
,
312 device_make_and_model
, device_id
, device_location
);
315 _cupsLangPrintf(stdout
, "%s %s\n", device_class
, device_uri
);
320 * 'show_devices()' - Show available devices.
323 static int /* O - 0 on success, 1 on failure */
325 int long_status
, /* I - Long status report? */
326 int timeout
, /* I - Timeout */
327 const char *include_schemes
, /* I - List of schemes to include */
328 const char *exclude_schemes
) /* I - List of schemes to exclude */
330 if (cupsGetDevices(CUPS_HTTP_DEFAULT
, timeout
, include_schemes
,
331 exclude_schemes
, device_cb
, &long_status
) != IPP_OK
)
333 _cupsLangPrintf(stderr
, "lpinfo: %s\n", cupsLastErrorString());
342 * 'show_models()' - Show available PPDs.
345 static int /* O - 0 on success, 1 on failure */
347 int long_status
, /* I - Long status report? */
348 const char *device_id
, /* I - 1284 device ID */
349 const char *language
, /* I - Language */
350 const char *make_model
, /* I - Make and model */
351 const char *product
, /* I - Product */
352 const char *include_schemes
, /* I - List of schemes to include */
353 const char *exclude_schemes
) /* I - List of schemes to exclude */
355 ipp_t
*request
, /* IPP Request */
356 *response
; /* IPP Response */
357 ipp_attribute_t
*attr
; /* Current attribute */
358 const char *ppd_device_id
, /* Pointer to ppd-device-id */
359 *ppd_language
, /* Pointer to ppd-natural-language */
360 *ppd_make_model
, /* Pointer to ppd-make-and-model */
361 *ppd_name
; /* Pointer to ppd-name */
362 cups_option_t option
; /* in/exclude-schemes option */
366 * Build a CUPS_GET_PPDS request...
369 request
= ippNewRequest(CUPS_GET_PPDS
);
372 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-device-id",
375 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "ppd-language",
378 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-make-and-model",
381 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-product",
386 option
.name
= "include-schemes";
387 option
.value
= (char *)include_schemes
;
389 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
394 option
.name
= "exclude-schemes";
395 option
.value
= (char *)exclude_schemes
;
397 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
401 * Do the request and get back a response...
404 if ((response
= cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/")) != NULL
)
407 * Loop through the device list and display them...
410 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
412 _cupsLangPrintf(stderr
, "lpinfo: %s\n", cupsLastErrorString());
417 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
420 * Skip leading attributes until we hit a PPD...
423 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
430 * Pull the needed attributes from this PPD...
433 ppd_device_id
= "NONE";
435 ppd_make_model
= NULL
;
438 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
440 if (!strcmp(attr
->name
, "ppd-device-id") &&
441 attr
->value_tag
== IPP_TAG_TEXT
)
442 ppd_device_id
= attr
->values
[0].string
.text
;
443 else if (!strcmp(attr
->name
, "ppd-natural-language") &&
444 attr
->value_tag
== IPP_TAG_LANGUAGE
)
445 ppd_language
= attr
->values
[0].string
.text
;
446 else if (!strcmp(attr
->name
, "ppd-make-and-model") &&
447 attr
->value_tag
== IPP_TAG_TEXT
)
448 ppd_make_model
= attr
->values
[0].string
.text
;
449 else if (!strcmp(attr
->name
, "ppd-name") &&
450 attr
->value_tag
== IPP_TAG_NAME
)
451 ppd_name
= attr
->values
[0].string
.text
;
457 * See if we have everything needed...
460 if (ppd_language
== NULL
|| ppd_make_model
== NULL
|| ppd_name
== NULL
)
469 * Display the device...
474 _cupsLangPrintf(stdout
,
475 _("Model: name = %s\n"
476 " natural_language = %s\n"
477 " make-and-model = %s\n"
478 " device-id = %s\n"),
479 ppd_name
, ppd_language
, ppd_make_model
, ppd_device_id
);
482 _cupsLangPrintf(stdout
, "%s %s\n", ppd_name
, ppd_make_model
);
492 _cupsLangPrintf(stderr
, "lpinfo: %s\n", cupsLastErrorString());
502 * End of "$Id: lpinfo.c 7810 2008-07-29 01:11:15Z mike $".