]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpinfo.c
4 * "lpinfo" command for CUPS.
6 * Copyright 2007-2010 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...
27 #include <cups/cups-private.h>
34 static void device_cb(const char *device_clas
, const char *device_id
,
35 const char *device_info
,
36 const char *device_make_and_model
,
37 const char *device_uri
, const char *device_location
,
39 static int show_devices(int long_status
, int timeout
,
40 const char *include_schemes
,
41 const char *exclude_schemes
);
42 static int show_models(int long_status
,
43 const char *device_id
, const char *language
,
44 const char *make_model
, const char *product
,
45 const char *include_schemes
,
46 const char *exclude_schemes
);
50 * 'main()' - Parse options and show status information.
54 main(int argc
, /* I - Number of command-line arguments */
55 char *argv
[]) /* I - Command-line arguments */
57 int i
; /* Looping var */
58 int long_status
; /* Long listing? */
59 const char *device_id
, /* 1284 device ID */
60 *language
, /* Language */
61 *make_model
, /* Make and model */
62 *product
, /* Product */
63 *include_schemes
, /* Schemes to include */
64 *exclude_schemes
; /* Schemes to exclude */
65 int timeout
; /* Device timeout */
75 include_schemes
= CUPS_INCLUDE_ALL
;
76 exclude_schemes
= CUPS_EXCLUDE_NONE
;
77 timeout
= CUPS_TIMEOUT_DEFAULT
;
79 for (i
= 1; i
< argc
; i
++)
80 if (argv
[i
][0] == '-')
83 case 'E' : /* Encrypt */
85 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
87 _cupsLangPrintf(stderr
,
88 _("%s: Sorry, no encryption support."),
93 case 'h' : /* Connect to host */
94 if (argv
[i
][2] != '\0')
95 cupsSetServer(argv
[i
] + 2);
102 _cupsLangPuts(stderr
,
103 _("Error: need hostname after \"-h\" option."));
107 cupsSetServer(argv
[i
]);
111 case 'l' : /* Show long listing */
115 case 'm' : /* Show models */
116 if (show_models(long_status
, device_id
, language
, make_model
,
117 product
, include_schemes
, exclude_schemes
))
121 case 'v' : /* Show available devices */
122 if (show_devices(long_status
, timeout
, include_schemes
,
127 case '-' : /* --something */
128 if (!strcmp(argv
[i
], "--device-id"))
136 _cupsLangPuts(stderr
,
137 _("lpinfo: Expected 1284 device ID string "
138 "after \"--device-id\"."));
142 else if (!strncmp(argv
[i
], "--device-id=", 12) && argv
[i
][12])
144 device_id
= argv
[i
] + 12;
146 else if (!strcmp(argv
[i
], "--exclude-schemes"))
151 exclude_schemes
= argv
[i
];
154 _cupsLangPuts(stderr
,
155 _("lpinfo: Expected scheme list after "
156 "\"--exclude-schemes\"."));
160 else if (!strncmp(argv
[i
], "--exclude-schemes=", 18) && argv
[i
][18])
162 exclude_schemes
= argv
[i
] + 18;
164 else if (!strcmp(argv
[i
], "--include-schemes"))
169 include_schemes
= argv
[i
];
172 _cupsLangPuts(stderr
,
173 _("lpinfo: Expected scheme list after "
174 "\"--include-schemes\"."));
178 else if (!strncmp(argv
[i
], "--include-schemes=", 18) && argv
[i
][18])
180 include_schemes
= argv
[i
] + 18;
182 else if (!strcmp(argv
[i
], "--language"))
189 _cupsLangPuts(stderr
,
190 _("lpinfo: Expected language after "
195 else if (!strncmp(argv
[i
], "--language=", 11) && argv
[i
][11])
197 language
= argv
[i
] + 11;
199 else if (!strcmp(argv
[i
], "--make-and-model"))
206 _cupsLangPuts(stderr
,
207 _("lpinfo: Expected make and model after "
208 "\"--make-and-model\"."));
212 else if (!strncmp(argv
[i
], "--make-and-model=", 17) && argv
[i
][17])
214 make_model
= argv
[i
] + 17;
216 else if (!strcmp(argv
[i
], "--product"))
223 _cupsLangPuts(stderr
,
224 _("lpinfo: Expected product string after "
229 else if (!strncmp(argv
[i
], "--product=", 10) && argv
[i
][10])
231 product
= argv
[i
] + 10;
233 else if (!strcmp(argv
[i
], "--timeout"))
237 timeout
= atoi(argv
[i
]);
240 _cupsLangPuts(stderr
,
241 _("lpinfo: Expected timeout after "
246 else if (!strncmp(argv
[i
], "--timeout=", 10) && argv
[i
][10])
248 timeout
= atoi(argv
[i
] + 10);
252 _cupsLangPrintf(stderr
, _("lpinfo: Unknown option \"%s\"."),
259 _cupsLangPrintf(stderr
, _("lpinfo: Unknown option \"%c\"."),
265 _cupsLangPrintf(stderr
, _("lpinfo: Unknown argument \"%s\"."),
275 * 'device_cb()' - Device callback.
280 const char *device_class
, /* I - device-class string */
281 const char *device_id
, /* I - device-id string */
282 const char *device_info
, /* I - device-info string */
283 const char *device_make_and_model
, /* I - device-make-and-model string */
284 const char *device_uri
, /* I - device-uri string */
285 const char *device_location
, /* I - device-location string */
286 void *user_data
) /* I - User data */
288 int *long_status
; /* Show verbose info? */
292 * Display the device...
295 long_status
= (int *)user_data
;
299 _cupsLangPrintf(stdout
,
300 _("Device: uri = %s\n"
303 " make-and-model = %s\n"
306 device_uri
, device_class
, device_info
,
307 device_make_and_model
, device_id
, device_location
);
310 _cupsLangPrintf(stdout
, "%s %s", device_class
, device_uri
);
315 * 'show_devices()' - Show available devices.
318 static int /* O - 0 on success, 1 on failure */
320 int long_status
, /* I - Long status report? */
321 int timeout
, /* I - Timeout */
322 const char *include_schemes
, /* I - List of schemes to include */
323 const char *exclude_schemes
) /* I - List of schemes to exclude */
325 if (cupsGetDevices(CUPS_HTTP_DEFAULT
, timeout
, include_schemes
,
326 exclude_schemes
, device_cb
, &long_status
) != IPP_OK
)
328 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
337 * 'show_models()' - Show available PPDs.
340 static int /* O - 0 on success, 1 on failure */
342 int long_status
, /* I - Long status report? */
343 const char *device_id
, /* I - 1284 device ID */
344 const char *language
, /* I - Language */
345 const char *make_model
, /* I - Make and model */
346 const char *product
, /* I - Product */
347 const char *include_schemes
, /* I - List of schemes to include */
348 const char *exclude_schemes
) /* I - List of schemes to exclude */
350 ipp_t
*request
, /* IPP Request */
351 *response
; /* IPP Response */
352 ipp_attribute_t
*attr
; /* Current attribute */
353 const char *ppd_device_id
, /* Pointer to ppd-device-id */
354 *ppd_language
, /* Pointer to ppd-natural-language */
355 *ppd_make_model
, /* Pointer to ppd-make-and-model */
356 *ppd_name
; /* Pointer to ppd-name */
357 cups_option_t option
; /* in/exclude-schemes option */
361 * Build a CUPS_GET_PPDS request...
364 request
= ippNewRequest(CUPS_GET_PPDS
);
367 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-device-id",
370 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "ppd-language",
373 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-make-and-model",
376 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-product",
381 option
.name
= "include-schemes";
382 option
.value
= (char *)include_schemes
;
384 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
389 option
.name
= "exclude-schemes";
390 option
.value
= (char *)exclude_schemes
;
392 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
396 * Do the request and get back a response...
399 if ((response
= cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/")) != NULL
)
402 * Loop through the device list and display them...
405 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
407 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
412 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
415 * Skip leading attributes until we hit a PPD...
418 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
425 * Pull the needed attributes from this PPD...
428 ppd_device_id
= "NONE";
430 ppd_make_model
= NULL
;
433 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
435 if (!strcmp(attr
->name
, "ppd-device-id") &&
436 attr
->value_tag
== IPP_TAG_TEXT
)
437 ppd_device_id
= attr
->values
[0].string
.text
;
438 else if (!strcmp(attr
->name
, "ppd-natural-language") &&
439 attr
->value_tag
== IPP_TAG_LANGUAGE
)
440 ppd_language
= attr
->values
[0].string
.text
;
441 else if (!strcmp(attr
->name
, "ppd-make-and-model") &&
442 attr
->value_tag
== IPP_TAG_TEXT
)
443 ppd_make_model
= attr
->values
[0].string
.text
;
444 else if (!strcmp(attr
->name
, "ppd-name") &&
445 attr
->value_tag
== IPP_TAG_NAME
)
446 ppd_name
= attr
->values
[0].string
.text
;
452 * See if we have everything needed...
455 if (ppd_language
== NULL
|| ppd_make_model
== NULL
|| ppd_name
== NULL
)
464 * Display the device...
469 _cupsLangPrintf(stdout
,
470 _("Model: name = %s\n"
471 " natural_language = %s\n"
472 " make-and-model = %s\n"
474 ppd_name
, ppd_language
, ppd_make_model
, ppd_device_id
);
477 _cupsLangPrintf(stdout
, "%s %s", ppd_name
, ppd_make_model
);
487 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());