]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpinfo.c
b8f77264c60fa0959be761a8e82591547a88dbbf
2 * "lpinfo" command for CUPS.
4 * Copyright 2007-2010 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/cups-private.h>
25 static void device_cb(const char *device_clas
, const char *device_id
,
26 const char *device_info
,
27 const char *device_make_and_model
,
28 const char *device_uri
, const char *device_location
,
30 static int show_devices(int long_status
, int timeout
,
31 const char *include_schemes
,
32 const char *exclude_schemes
);
33 static int show_models(int long_status
,
34 const char *device_id
, const char *language
,
35 const char *make_model
, const char *product
,
36 const char *include_schemes
,
37 const char *exclude_schemes
);
41 * 'main()' - Parse options and show status information.
45 main(int argc
, /* I - Number of command-line arguments */
46 char *argv
[]) /* I - Command-line arguments */
48 int i
; /* Looping var */
49 int long_status
; /* Long listing? */
50 const char *device_id
, /* 1284 device ID */
51 *language
, /* Language */
52 *make_model
, /* Make and model */
53 *product
, /* Product */
54 *include_schemes
, /* Schemes to include */
55 *exclude_schemes
; /* Schemes to exclude */
56 int timeout
; /* Device timeout */
66 include_schemes
= CUPS_INCLUDE_ALL
;
67 exclude_schemes
= CUPS_EXCLUDE_NONE
;
68 timeout
= CUPS_TIMEOUT_DEFAULT
;
70 for (i
= 1; i
< argc
; i
++)
71 if (argv
[i
][0] == '-')
74 case 'E' : /* Encrypt */
76 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
78 _cupsLangPrintf(stderr
,
79 _("%s: Sorry, no encryption support."),
84 case 'h' : /* Connect to host */
85 if (argv
[i
][2] != '\0')
86 cupsSetServer(argv
[i
] + 2);
94 _("Error: need hostname after \"-h\" option."));
98 cupsSetServer(argv
[i
]);
102 case 'l' : /* Show long listing */
106 case 'm' : /* Show models */
107 if (show_models(long_status
, device_id
, language
, make_model
,
108 product
, include_schemes
, exclude_schemes
))
112 case 'v' : /* Show available devices */
113 if (show_devices(long_status
, timeout
, include_schemes
,
118 case '-' : /* --something */
119 if (!strcmp(argv
[i
], "--device-id"))
127 _cupsLangPuts(stderr
,
128 _("lpinfo: Expected 1284 device ID string "
129 "after \"--device-id\"."));
133 else if (!strncmp(argv
[i
], "--device-id=", 12) && argv
[i
][12])
135 device_id
= argv
[i
] + 12;
137 else if (!strcmp(argv
[i
], "--exclude-schemes"))
142 exclude_schemes
= argv
[i
];
145 _cupsLangPuts(stderr
,
146 _("lpinfo: Expected scheme list after "
147 "\"--exclude-schemes\"."));
151 else if (!strncmp(argv
[i
], "--exclude-schemes=", 18) && argv
[i
][18])
153 exclude_schemes
= argv
[i
] + 18;
155 else if (!strcmp(argv
[i
], "--include-schemes"))
160 include_schemes
= argv
[i
];
163 _cupsLangPuts(stderr
,
164 _("lpinfo: Expected scheme list after "
165 "\"--include-schemes\"."));
169 else if (!strncmp(argv
[i
], "--include-schemes=", 18) && argv
[i
][18])
171 include_schemes
= argv
[i
] + 18;
173 else if (!strcmp(argv
[i
], "--language"))
180 _cupsLangPuts(stderr
,
181 _("lpinfo: Expected language after "
186 else if (!strncmp(argv
[i
], "--language=", 11) && argv
[i
][11])
188 language
= argv
[i
] + 11;
190 else if (!strcmp(argv
[i
], "--make-and-model"))
197 _cupsLangPuts(stderr
,
198 _("lpinfo: Expected make and model after "
199 "\"--make-and-model\"."));
203 else if (!strncmp(argv
[i
], "--make-and-model=", 17) && argv
[i
][17])
205 make_model
= argv
[i
] + 17;
207 else if (!strcmp(argv
[i
], "--product"))
214 _cupsLangPuts(stderr
,
215 _("lpinfo: Expected product string after "
220 else if (!strncmp(argv
[i
], "--product=", 10) && argv
[i
][10])
222 product
= argv
[i
] + 10;
224 else if (!strcmp(argv
[i
], "--timeout"))
228 timeout
= atoi(argv
[i
]);
231 _cupsLangPuts(stderr
,
232 _("lpinfo: Expected timeout after "
237 else if (!strncmp(argv
[i
], "--timeout=", 10) && argv
[i
][10])
239 timeout
= atoi(argv
[i
] + 10);
243 _cupsLangPrintf(stderr
, _("lpinfo: Unknown option \"%s\"."),
250 _cupsLangPrintf(stderr
, _("lpinfo: Unknown option \"%c\"."),
256 _cupsLangPrintf(stderr
, _("lpinfo: Unknown argument \"%s\"."),
266 * 'device_cb()' - Device callback.
271 const char *device_class
, /* I - device-class string */
272 const char *device_id
, /* I - device-id string */
273 const char *device_info
, /* I - device-info string */
274 const char *device_make_and_model
, /* I - device-make-and-model string */
275 const char *device_uri
, /* I - device-uri string */
276 const char *device_location
, /* I - device-location string */
277 void *user_data
) /* I - User data */
279 int *long_status
; /* Show verbose info? */
283 * Display the device...
286 long_status
= (int *)user_data
;
290 _cupsLangPrintf(stdout
,
291 _("Device: uri = %s\n"
294 " make-and-model = %s\n"
297 device_uri
, device_class
, device_info
,
298 device_make_and_model
, device_id
, device_location
);
301 _cupsLangPrintf(stdout
, "%s %s", device_class
, device_uri
);
306 * 'show_devices()' - Show available devices.
309 static int /* O - 0 on success, 1 on failure */
311 int long_status
, /* I - Long status report? */
312 int timeout
, /* I - Timeout */
313 const char *include_schemes
, /* I - List of schemes to include */
314 const char *exclude_schemes
) /* I - List of schemes to exclude */
316 if (cupsGetDevices(CUPS_HTTP_DEFAULT
, timeout
, include_schemes
,
317 exclude_schemes
, device_cb
, &long_status
) != IPP_OK
)
319 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
328 * 'show_models()' - Show available PPDs.
331 static int /* O - 0 on success, 1 on failure */
333 int long_status
, /* I - Long status report? */
334 const char *device_id
, /* I - 1284 device ID */
335 const char *language
, /* I - Language */
336 const char *make_model
, /* I - Make and model */
337 const char *product
, /* I - Product */
338 const char *include_schemes
, /* I - List of schemes to include */
339 const char *exclude_schemes
) /* I - List of schemes to exclude */
341 ipp_t
*request
, /* IPP Request */
342 *response
; /* IPP Response */
343 ipp_attribute_t
*attr
; /* Current attribute */
344 const char *ppd_device_id
, /* Pointer to ppd-device-id */
345 *ppd_language
, /* Pointer to ppd-natural-language */
346 *ppd_make_model
, /* Pointer to ppd-make-and-model */
347 *ppd_name
; /* Pointer to ppd-name */
348 cups_option_t option
; /* in/exclude-schemes option */
352 * Build a CUPS_GET_PPDS request...
355 request
= ippNewRequest(CUPS_GET_PPDS
);
358 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-device-id",
361 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "ppd-language",
364 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-make-and-model",
367 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-product",
372 option
.name
= "include-schemes";
373 option
.value
= (char *)include_schemes
;
375 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
380 option
.name
= "exclude-schemes";
381 option
.value
= (char *)exclude_schemes
;
383 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
387 * Do the request and get back a response...
390 if ((response
= cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/")) != NULL
)
393 * Loop through the device list and display them...
396 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
398 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
403 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
406 * Skip leading attributes until we hit a PPD...
409 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
416 * Pull the needed attributes from this PPD...
419 ppd_device_id
= "NONE";
421 ppd_make_model
= NULL
;
424 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
426 if (!strcmp(attr
->name
, "ppd-device-id") &&
427 attr
->value_tag
== IPP_TAG_TEXT
)
428 ppd_device_id
= attr
->values
[0].string
.text
;
429 else if (!strcmp(attr
->name
, "ppd-natural-language") &&
430 attr
->value_tag
== IPP_TAG_LANGUAGE
)
431 ppd_language
= attr
->values
[0].string
.text
;
432 else if (!strcmp(attr
->name
, "ppd-make-and-model") &&
433 attr
->value_tag
== IPP_TAG_TEXT
)
434 ppd_make_model
= attr
->values
[0].string
.text
;
435 else if (!strcmp(attr
->name
, "ppd-name") &&
436 attr
->value_tag
== IPP_TAG_NAME
)
437 ppd_name
= attr
->values
[0].string
.text
;
443 * See if we have everything needed...
446 if (ppd_language
== NULL
|| ppd_make_model
== NULL
|| ppd_name
== NULL
)
455 * Display the device...
460 _cupsLangPrintf(stdout
,
461 _("Model: name = %s\n"
462 " natural_language = %s\n"
463 " make-and-model = %s\n"
465 ppd_name
, ppd_language
, ppd_make_model
, ppd_device_id
);
468 _cupsLangPrintf(stdout
, "%s %s", ppd_name
, ppd_make_model
);
478 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());