]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpinfo.c
2 * "lpinfo" command for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include <cups/cups-private.h>
16 #include <cups/adminutil.h>
23 static void device_cb(const char *device_class
, const char *device_id
,
24 const char *device_info
,
25 const char *device_make_and_model
,
26 const char *device_uri
, const char *device_location
,
28 static int show_devices(int long_status
, int timeout
,
29 const char *include_schemes
,
30 const char *exclude_schemes
);
31 static int show_models(int long_status
,
32 const char *device_id
, const char *language
,
33 const char *make_model
, const char *product
,
34 const char *include_schemes
,
35 const char *exclude_schemes
);
36 static void usage(void) _CUPS_NORETURN
;
40 * 'main()' - Parse options and show status information.
44 main(int argc
, /* I - Number of command-line arguments */
45 char *argv
[]) /* I - Command-line arguments */
47 int i
; /* Looping var */
48 int long_status
; /* Long listing? */
49 const char *opt
, /* Option pointer */
50 *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
++)
72 if (!strcmp(argv
[i
], "--device-id"))
80 _cupsLangPuts(stderr
, _("lpinfo: Expected 1284 device ID string after \"--device-id\"."));
84 else if (!strncmp(argv
[i
], "--device-id=", 12) && argv
[i
][12])
86 device_id
= argv
[i
] + 12;
88 else if (!strcmp(argv
[i
], "--exclude-schemes"))
93 exclude_schemes
= argv
[i
];
96 _cupsLangPuts(stderr
, _("lpinfo: Expected scheme list after \"--exclude-schemes\"."));
100 else if (!strncmp(argv
[i
], "--exclude-schemes=", 18) && argv
[i
][18])
102 exclude_schemes
= argv
[i
] + 18;
104 else if (!strcmp(argv
[i
], "--help"))
106 else if (!strcmp(argv
[i
], "--include-schemes"))
111 include_schemes
= argv
[i
];
114 _cupsLangPuts(stderr
, _("lpinfo: Expected scheme list after \"--include-schemes\"."));
118 else if (!strncmp(argv
[i
], "--include-schemes=", 18) && argv
[i
][18])
120 include_schemes
= argv
[i
] + 18;
122 else if (!strcmp(argv
[i
], "--language"))
129 _cupsLangPuts(stderr
, _("lpinfo: Expected language after \"--language\"."));
133 else if (!strncmp(argv
[i
], "--language=", 11) && argv
[i
][11])
135 language
= argv
[i
] + 11;
137 else if (!strcmp(argv
[i
], "--make-and-model"))
144 _cupsLangPuts(stderr
, _("lpinfo: Expected make and model after \"--make-and-model\"."));
148 else if (!strncmp(argv
[i
], "--make-and-model=", 17) && argv
[i
][17])
150 make_model
= argv
[i
] + 17;
152 else if (!strcmp(argv
[i
], "--product"))
159 _cupsLangPuts(stderr
, _("lpinfo: Expected product string after \"--product\"."));
163 else if (!strncmp(argv
[i
], "--product=", 10) && argv
[i
][10])
165 product
= argv
[i
] + 10;
167 else if (!strcmp(argv
[i
], "--timeout"))
171 timeout
= atoi(argv
[i
]);
174 _cupsLangPuts(stderr
, _("lpinfo: Expected timeout after \"--timeout\"."));
178 else if (!strncmp(argv
[i
], "--timeout=", 10) && argv
[i
][10])
180 timeout
= atoi(argv
[i
] + 10);
182 else if (argv
[i
][0] == '-')
184 for (opt
= argv
[i
] + 1; *opt
; opt
++)
188 case 'E' : /* Encrypt */
190 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
192 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
193 #endif /* HAVE_SSL */
196 case 'h' : /* Connect to host */
199 cupsSetServer(opt
+ 1);
200 opt
+= strlen(opt
) - 1;
208 _cupsLangPuts(stderr
, _("Error: need hostname after \"-h\" option."));
212 cupsSetServer(argv
[i
]);
216 case 'l' : /* Show long listing */
220 case 'm' : /* Show models */
221 if (show_models(long_status
, device_id
, language
, make_model
, product
, include_schemes
, exclude_schemes
))
225 case 'v' : /* Show available devices */
226 if (show_devices(long_status
, timeout
, include_schemes
, exclude_schemes
))
231 _cupsLangPrintf(stderr
, _("%s: Unknown option \"%c\"."), argv
[0], *opt
);
238 _cupsLangPrintf(stderr
, _("%s: Unknown argument \"%s\"."), argv
[0], argv
[i
]);
248 * 'device_cb()' - Device callback.
253 const char *device_class
, /* I - device-class string */
254 const char *device_id
, /* I - device-id string */
255 const char *device_info
, /* I - device-info string */
256 const char *device_make_and_model
, /* I - device-make-and-model string */
257 const char *device_uri
, /* I - device-uri string */
258 const char *device_location
, /* I - device-location string */
259 void *user_data
) /* I - User data */
261 int *long_status
; /* Show verbose info? */
265 * Display the device...
268 long_status
= (int *)user_data
;
272 _cupsLangPrintf(stdout
,
273 _("Device: uri = %s\n"
276 " make-and-model = %s\n"
279 device_uri
, device_class
, device_info
,
280 device_make_and_model
, device_id
, device_location
);
283 _cupsLangPrintf(stdout
, "%s %s", device_class
, device_uri
);
288 * 'show_devices()' - Show available devices.
291 static int /* O - 0 on success, 1 on failure */
293 int long_status
, /* I - Long status report? */
294 int timeout
, /* I - Timeout */
295 const char *include_schemes
, /* I - List of schemes to include */
296 const char *exclude_schemes
) /* I - List of schemes to exclude */
298 if (cupsGetDevices(CUPS_HTTP_DEFAULT
, timeout
, include_schemes
,
299 exclude_schemes
, device_cb
, &long_status
) != IPP_OK
)
301 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
310 * 'show_models()' - Show available PPDs.
313 static int /* O - 0 on success, 1 on failure */
315 int long_status
, /* I - Long status report? */
316 const char *device_id
, /* I - 1284 device ID */
317 const char *language
, /* I - Language */
318 const char *make_model
, /* I - Make and model */
319 const char *product
, /* I - Product */
320 const char *include_schemes
, /* I - List of schemes to include */
321 const char *exclude_schemes
) /* I - List of schemes to exclude */
323 ipp_t
*request
, /* IPP Request */
324 *response
; /* IPP Response */
325 ipp_attribute_t
*attr
; /* Current attribute */
326 const char *ppd_device_id
, /* Pointer to ppd-device-id */
327 *ppd_language
, /* Pointer to ppd-natural-language */
328 *ppd_make_model
, /* Pointer to ppd-make-and-model */
329 *ppd_name
; /* Pointer to ppd-name */
330 cups_option_t option
; /* in/exclude-schemes option */
334 * Build a CUPS_GET_PPDS request...
337 request
= ippNewRequest(CUPS_GET_PPDS
);
340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-device-id",
343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "ppd-language",
346 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-make-and-model",
349 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-product",
354 option
.name
= "include-schemes";
355 option
.value
= (char *)include_schemes
;
357 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
362 option
.name
= "exclude-schemes";
363 option
.value
= (char *)exclude_schemes
;
365 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
369 * Do the request and get back a response...
372 if ((response
= cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/")) != NULL
)
375 * Loop through the device list and display them...
378 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
380 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
385 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
388 * Skip leading attributes until we hit a PPD...
391 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
398 * Pull the needed attributes from this PPD...
401 ppd_device_id
= "NONE";
403 ppd_make_model
= NULL
;
406 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
408 if (!strcmp(attr
->name
, "ppd-device-id") &&
409 attr
->value_tag
== IPP_TAG_TEXT
)
410 ppd_device_id
= attr
->values
[0].string
.text
;
411 else if (!strcmp(attr
->name
, "ppd-natural-language") &&
412 attr
->value_tag
== IPP_TAG_LANGUAGE
)
413 ppd_language
= attr
->values
[0].string
.text
;
414 else if (!strcmp(attr
->name
, "ppd-make-and-model") &&
415 attr
->value_tag
== IPP_TAG_TEXT
)
416 ppd_make_model
= attr
->values
[0].string
.text
;
417 else if (!strcmp(attr
->name
, "ppd-name") &&
418 attr
->value_tag
== IPP_TAG_NAME
)
419 ppd_name
= attr
->values
[0].string
.text
;
425 * See if we have everything needed...
428 if (ppd_language
== NULL
|| ppd_make_model
== NULL
|| ppd_name
== NULL
)
437 * Display the device...
442 _cupsLangPrintf(stdout
,
443 _("Model: name = %s\n"
444 " natural_language = %s\n"
445 " make-and-model = %s\n"
447 ppd_name
, ppd_language
, ppd_make_model
, ppd_device_id
);
450 _cupsLangPrintf(stdout
, "%s %s", ppd_name
, ppd_make_model
);
459 * Show the "everywhere" model, which is handled by the lpadmin command...
462 if ((!include_schemes
|| strstr(include_schemes
, "everywhere")) && (!exclude_schemes
|| !strstr(exclude_schemes
, "everywhere")))
466 _cupsLangPrintf(stdout
,
467 _("Model: name = %s\n"
468 " natural_language = %s\n"
469 " make-and-model = %s\n"
471 "everywhere", cupsLangDefault()->language
, "IPP Everywhere™", "CMD:PwgRaster");
474 _cupsLangPuts(stdout
, "everywhere IPP Everywhere");
479 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
489 * 'usage()' - Show program usage and exit.
495 _cupsLangPuts(stdout
, _("Usage: lpinfo [options] -m\n"
496 " lpinfo [options] -v"));
497 _cupsLangPuts(stdout
, _("Options:"));
498 _cupsLangPuts(stdout
, _("-E Encrypt the connection to the server"));
499 _cupsLangPuts(stdout
, _("-h server[:port] Connect to the named server and port"));
500 _cupsLangPuts(stdout
, _("-l Show verbose (long) output"));
501 _cupsLangPuts(stdout
, _("-m Show models"));
502 _cupsLangPuts(stdout
, _("-U username Specify the username to use for authentication"));
503 _cupsLangPuts(stdout
, _("-v Show devices"));
504 _cupsLangPuts(stdout
, _("--device-id device-id Show models matching the given IEEE 1284 device ID"));
505 _cupsLangPuts(stdout
, _("--exclude-schemes scheme-list\n"
506 " Exclude the specified URI schemes"));
507 _cupsLangPuts(stdout
, _("--include-schemes scheme-list\n"
508 " Include only the specified URI schemes"));
509 _cupsLangPuts(stdout
, _("--language locale Show models matching the given locale"));
510 _cupsLangPuts(stdout
, _("--make-and-model name Show models matching the given make and model name"));
511 _cupsLangPuts(stdout
, _("--product name Show models matching the given PostScript product"));
512 _cupsLangPuts(stdout
, _("--timeout seconds Specify the maximum number of seconds to discover devices"));