]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpinfo.c
2 * "lpinfo" command for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
15 #include <cups/adminutil.h>
22 static void device_cb(const char *device_clas
, const char *device_id
,
23 const char *device_info
,
24 const char *device_make_and_model
,
25 const char *device_uri
, const char *device_location
,
27 static int show_devices(int long_status
, int timeout
,
28 const char *include_schemes
,
29 const char *exclude_schemes
);
30 static int show_models(int long_status
,
31 const char *device_id
, const char *language
,
32 const char *make_model
, const char *product
,
33 const char *include_schemes
,
34 const char *exclude_schemes
);
38 * 'main()' - Parse options and show status information.
42 main(int argc
, /* I - Number of command-line arguments */
43 char *argv
[]) /* I - Command-line arguments */
45 int i
; /* Looping var */
46 int long_status
; /* Long listing? */
47 const char *opt
, /* Option pointer */
48 *device_id
, /* 1284 device ID */
49 *language
, /* Language */
50 *make_model
, /* Make and model */
51 *product
, /* Product */
52 *include_schemes
, /* Schemes to include */
53 *exclude_schemes
; /* Schemes to exclude */
54 int timeout
; /* Device timeout */
64 include_schemes
= CUPS_INCLUDE_ALL
;
65 exclude_schemes
= CUPS_EXCLUDE_NONE
;
66 timeout
= CUPS_TIMEOUT_DEFAULT
;
68 for (i
= 1; i
< argc
; i
++)
70 if (!strcmp(argv
[i
], "--device-id"))
78 _cupsLangPuts(stderr
, _("lpinfo: Expected 1284 device ID string after \"--device-id\"."));
82 else if (!strncmp(argv
[i
], "--device-id=", 12) && argv
[i
][12])
84 device_id
= argv
[i
] + 12;
86 else if (!strcmp(argv
[i
], "--exclude-schemes"))
91 exclude_schemes
= argv
[i
];
94 _cupsLangPuts(stderr
, _("lpinfo: Expected scheme list after \"--exclude-schemes\"."));
98 else if (!strncmp(argv
[i
], "--exclude-schemes=", 18) && argv
[i
][18])
100 exclude_schemes
= argv
[i
] + 18;
102 else if (!strcmp(argv
[i
], "--include-schemes"))
107 include_schemes
= argv
[i
];
110 _cupsLangPuts(stderr
, _("lpinfo: Expected scheme list after \"--include-schemes\"."));
114 else if (!strncmp(argv
[i
], "--include-schemes=", 18) && argv
[i
][18])
116 include_schemes
= argv
[i
] + 18;
118 else if (!strcmp(argv
[i
], "--language"))
125 _cupsLangPuts(stderr
, _("lpinfo: Expected language after \"--language\"."));
129 else if (!strncmp(argv
[i
], "--language=", 11) && argv
[i
][11])
131 language
= argv
[i
] + 11;
133 else if (!strcmp(argv
[i
], "--make-and-model"))
140 _cupsLangPuts(stderr
, _("lpinfo: Expected make and model after \"--make-and-model\"."));
144 else if (!strncmp(argv
[i
], "--make-and-model=", 17) && argv
[i
][17])
146 make_model
= argv
[i
] + 17;
148 else if (!strcmp(argv
[i
], "--product"))
155 _cupsLangPuts(stderr
, _("lpinfo: Expected product string after \"--product\"."));
159 else if (!strncmp(argv
[i
], "--product=", 10) && argv
[i
][10])
161 product
= argv
[i
] + 10;
163 else if (!strcmp(argv
[i
], "--timeout"))
167 timeout
= atoi(argv
[i
]);
170 _cupsLangPuts(stderr
, _("lpinfo: Expected timeout after \"--timeout\"."));
174 else if (!strncmp(argv
[i
], "--timeout=", 10) && argv
[i
][10])
176 timeout
= atoi(argv
[i
] + 10);
178 else if (argv
[i
][0] == '-')
180 for (opt
= argv
[i
] + 1; *opt
; opt
++)
184 case 'E' : /* Encrypt */
186 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
188 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
189 #endif /* HAVE_SSL */
192 case 'h' : /* Connect to host */
195 cupsSetServer(opt
+ 1);
196 opt
+= strlen(opt
) - 1;
204 _cupsLangPuts(stderr
, _("Error: need hostname after \"-h\" option."));
208 cupsSetServer(argv
[i
]);
212 case 'l' : /* Show long listing */
216 case 'm' : /* Show models */
217 if (show_models(long_status
, device_id
, language
, make_model
, product
, include_schemes
, exclude_schemes
))
221 case 'v' : /* Show available devices */
222 if (show_devices(long_status
, timeout
, include_schemes
, exclude_schemes
))
227 _cupsLangPrintf(stderr
, _("%s: Unknown option \"%c\"."), argv
[0], *opt
);
234 _cupsLangPrintf(stderr
, _("%s: Unknown argument \"%s\"."), argv
[0], argv
[i
]);
244 * 'device_cb()' - Device callback.
249 const char *device_class
, /* I - device-class string */
250 const char *device_id
, /* I - device-id string */
251 const char *device_info
, /* I - device-info string */
252 const char *device_make_and_model
, /* I - device-make-and-model string */
253 const char *device_uri
, /* I - device-uri string */
254 const char *device_location
, /* I - device-location string */
255 void *user_data
) /* I - User data */
257 int *long_status
; /* Show verbose info? */
261 * Display the device...
264 long_status
= (int *)user_data
;
268 _cupsLangPrintf(stdout
,
269 _("Device: uri = %s\n"
272 " make-and-model = %s\n"
275 device_uri
, device_class
, device_info
,
276 device_make_and_model
, device_id
, device_location
);
279 _cupsLangPrintf(stdout
, "%s %s", device_class
, device_uri
);
284 * 'show_devices()' - Show available devices.
287 static int /* O - 0 on success, 1 on failure */
289 int long_status
, /* I - Long status report? */
290 int timeout
, /* I - Timeout */
291 const char *include_schemes
, /* I - List of schemes to include */
292 const char *exclude_schemes
) /* I - List of schemes to exclude */
294 if (cupsGetDevices(CUPS_HTTP_DEFAULT
, timeout
, include_schemes
,
295 exclude_schemes
, device_cb
, &long_status
) != IPP_OK
)
297 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
306 * 'show_models()' - Show available PPDs.
309 static int /* O - 0 on success, 1 on failure */
311 int long_status
, /* I - Long status report? */
312 const char *device_id
, /* I - 1284 device ID */
313 const char *language
, /* I - Language */
314 const char *make_model
, /* I - Make and model */
315 const char *product
, /* I - Product */
316 const char *include_schemes
, /* I - List of schemes to include */
317 const char *exclude_schemes
) /* I - List of schemes to exclude */
319 ipp_t
*request
, /* IPP Request */
320 *response
; /* IPP Response */
321 ipp_attribute_t
*attr
; /* Current attribute */
322 const char *ppd_device_id
, /* Pointer to ppd-device-id */
323 *ppd_language
, /* Pointer to ppd-natural-language */
324 *ppd_make_model
, /* Pointer to ppd-make-and-model */
325 *ppd_name
; /* Pointer to ppd-name */
326 cups_option_t option
; /* in/exclude-schemes option */
330 * Build a CUPS_GET_PPDS request...
333 request
= ippNewRequest(CUPS_GET_PPDS
);
336 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-device-id",
339 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "ppd-language",
342 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-make-and-model",
345 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "ppd-product",
350 option
.name
= "include-schemes";
351 option
.value
= (char *)include_schemes
;
353 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
358 option
.name
= "exclude-schemes";
359 option
.value
= (char *)exclude_schemes
;
361 cupsEncodeOptions2(request
, 1, &option
, IPP_TAG_OPERATION
);
365 * Do the request and get back a response...
368 if ((response
= cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/")) != NULL
)
371 * Loop through the device list and display them...
374 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
376 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());
381 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
384 * Skip leading attributes until we hit a PPD...
387 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
394 * Pull the needed attributes from this PPD...
397 ppd_device_id
= "NONE";
399 ppd_make_model
= NULL
;
402 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
404 if (!strcmp(attr
->name
, "ppd-device-id") &&
405 attr
->value_tag
== IPP_TAG_TEXT
)
406 ppd_device_id
= attr
->values
[0].string
.text
;
407 else if (!strcmp(attr
->name
, "ppd-natural-language") &&
408 attr
->value_tag
== IPP_TAG_LANGUAGE
)
409 ppd_language
= attr
->values
[0].string
.text
;
410 else if (!strcmp(attr
->name
, "ppd-make-and-model") &&
411 attr
->value_tag
== IPP_TAG_TEXT
)
412 ppd_make_model
= attr
->values
[0].string
.text
;
413 else if (!strcmp(attr
->name
, "ppd-name") &&
414 attr
->value_tag
== IPP_TAG_NAME
)
415 ppd_name
= attr
->values
[0].string
.text
;
421 * See if we have everything needed...
424 if (ppd_language
== NULL
|| ppd_make_model
== NULL
|| ppd_name
== NULL
)
433 * Display the device...
438 _cupsLangPrintf(stdout
,
439 _("Model: name = %s\n"
440 " natural_language = %s\n"
441 " make-and-model = %s\n"
443 ppd_name
, ppd_language
, ppd_make_model
, ppd_device_id
);
446 _cupsLangPrintf(stdout
, "%s %s", ppd_name
, ppd_make_model
);
456 _cupsLangPrintf(stderr
, "lpinfo: %s", cupsLastErrorString());