summaryrefslogtreecommitdiff
path: root/systemv/lpinfo.c
blob: b250df557450b9734df07b4eab487e7054c8f5bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * "lpinfo" command for CUPS.
 *
 * Copyright 2007-2016 by Apple Inc.
 * Copyright 1997-2006 by Easy Software Products.
 *
 * These coded instructions, statements, and computer programs are the
 * property of Apple Inc. and are protected by Federal copyright
 * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 * which should have been included with this file.  If this file is
 * missing or damaged, see the license at "http://www.cups.org/".
 */

/*
 * Include necessary headers...
 */

#include <cups/cups-private.h>
#include <cups/adminutil.h>


/*
 * Local functions...
 */

static void	device_cb(const char *device_clas, const char *device_id,
		          const char *device_info,
			  const char *device_make_and_model,
			  const char *device_uri, const char *device_location,
			  void *user_data);
static int	show_devices(int long_status, int timeout,
			     const char *include_schemes,
			     const char *exclude_schemes);
static int	show_models(int long_status,
			    const char *device_id, const char *language,
			    const char *make_model, const char *product,
			    const char *include_schemes,
			    const char *exclude_schemes);


/*
 * 'main()' - Parse options and show status information.
 */

int
main(int  argc,				/* I - Number of command-line arguments */
     char *argv[])			/* I - Command-line arguments */
{
  int		i;			/* Looping var */
  int		long_status;		/* Long listing? */
  const char	*opt,			/* Option pointer */
		*device_id,		/* 1284 device ID */
		*language,		/* Language */
		*make_model,		/* Make and model */
		*product,		/* Product */
		*include_schemes,	/* Schemes to include */
		*exclude_schemes;	/* Schemes to exclude */
  int		timeout;		/* Device timeout */


  _cupsSetLocale(argv);

  long_status     = 0;
  device_id       = NULL;
  language        = NULL;
  make_model      = NULL;
  product         = NULL;
  include_schemes = CUPS_INCLUDE_ALL;
  exclude_schemes = CUPS_EXCLUDE_NONE;
  timeout         = CUPS_TIMEOUT_DEFAULT;

  for (i = 1; i < argc; i ++)
  {
    if (!strcmp(argv[i], "--device-id"))
    {
      i ++;

      if (i < argc)
	device_id = argv[i];
      else
      {
	_cupsLangPuts(stderr, _("lpinfo: Expected 1284 device ID string after \"--device-id\"."));
	return (1);
      }
    }
    else if (!strncmp(argv[i], "--device-id=", 12) && argv[i][12])
    {
      device_id = argv[i] + 12;
    }
    else if (!strcmp(argv[i], "--exclude-schemes"))
    {
      i ++;

      if (i < argc)
	exclude_schemes = argv[i];
      else
      {
	_cupsLangPuts(stderr, _("lpinfo: Expected scheme list after \"--exclude-schemes\"."));
	return (1);
      }
    }
    else if (!strncmp(argv[i], "--exclude-schemes=", 18) && argv[i][18])
    {
      exclude_schemes = argv[i] + 18;
    }
    else if (!strcmp(argv[i], "--include-schemes"))
    {
      i ++;

      if (i < argc)
	include_schemes = argv[i];
      else
      {
	_cupsLangPuts(stderr, _("lpinfo: Expected scheme list after \"--include-schemes\"."));
	return (1);
      }
    }
    else if (!strncmp(argv[i], "--include-schemes=", 18) && argv[i][18])
    {
      include_schemes = argv[i] + 18;
    }
    else if (!strcmp(argv[i], "--language"))
    {
      i ++;
      if (i < argc)
	language = argv[i];
      else
      {
	_cupsLangPuts(stderr, _("lpinfo: Expected language after \"--language\"."));
	return (1);
      }
    }
    else if (!strncmp(argv[i], "--language=", 11) && argv[i][11])
    {
      language = argv[i] + 11;
    }
    else if (!strcmp(argv[i], "--make-and-model"))
    {
      i ++;
      if (i < argc)
	make_model= argv[i];
      else
      {
	_cupsLangPuts(stderr, _("lpinfo: Expected make and model after \"--make-and-model\"."));
	return (1);
      }
    }
    else if (!strncmp(argv[i], "--make-and-model=", 17) && argv[i][17])
    {
      make_model = argv[i] + 17;
    }
    else if (!strcmp(argv[i], "--product"))
    {
      i ++;
      if (i < argc)
	product = argv[i];
      else
      {
	_cupsLangPuts(stderr, _("lpinfo: Expected product string after \"--product\"."));
	return (1);
      }
    }
    else if (!strncmp(argv[i], "--product=", 10) && argv[i][10])
    {
      product = argv[i] + 10;
    }
    else if (!strcmp(argv[i], "--timeout"))
    {
      i ++;
      if (i < argc)
	timeout = atoi(argv[i]);
      else
      {
	_cupsLangPuts(stderr, _("lpinfo: Expected timeout after \"--timeout\"."));
	return (1);
      }
    }
    else if (!strncmp(argv[i], "--timeout=", 10) && argv[i][10])
    {
      timeout = atoi(argv[i] + 10);
    }
    else if (argv[i][0] == '-')
    {
      for (opt = argv[i] + 1; *opt; opt ++)
      {
	switch (*opt)
	{
	  case 'E' : /* Encrypt */
#ifdef HAVE_SSL
	      cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
#else
	      _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]);
#endif /* HAVE_SSL */
	      break;

	  case 'h' : /* Connect to host */
	      if (opt[1] != '\0')
	      {
		cupsSetServer(opt + 1);
		opt += strlen(opt) - 1;
	      }
	      else
	      {
		i ++;

		if (i >= argc)
		{
		  _cupsLangPuts(stderr, _("Error: need hostname after \"-h\" option."));
		  return (1);
		}

		cupsSetServer(argv[i]);
	      }
	      break;

	  case 'l' : /* Show long listing */
	      long_status = 1;
	      break;

	  case 'm' : /* Show models */
	      if (show_models(long_status, device_id, language, make_model, product, include_schemes, exclude_schemes))
		return (1);
	      break;

	  case 'v' : /* Show available devices */
	      if (show_devices(long_status, timeout, include_schemes, exclude_schemes))
		return (1);
	      break;

	  default :
	      _cupsLangPrintf(stderr, _("%s: Unknown option \"%c\"."), argv[0], *opt);
	      return (1);
	}
      }
    }
    else
    {
      _cupsLangPrintf(stderr, _("%s: Unknown argument \"%s\"."), argv[0], argv[i]);
      return (1);
    }
  }

  return (0);
}


/*
 * 'device_cb()' - Device callback.
 */

static void
device_cb(
    const char *device_class,		/* I - device-class string */
    const char *device_id,		/* I - device-id string */
    const char *device_info,		/* I - device-info string */
    const char *device_make_and_model,	/* I - device-make-and-model string */
    const char *device_uri,		/* I - device-uri string */
    const char *device_location,	/* I - device-location string */
    void       *user_data)		/* I - User data */
{
  int	*long_status;			/* Show verbose info? */


 /*
  * Display the device...
  */

  long_status = (int *)user_data;

  if (*long_status)
  {
    _cupsLangPrintf(stdout,
		    _("Device: uri = %s\n"
		      "        class = %s\n"
		      "        info = %s\n"
		      "        make-and-model = %s\n"
		      "        device-id = %s\n"
		      "        location = %s"),
		    device_uri, device_class, device_info,
		    device_make_and_model, device_id, device_location);
  }
  else
    _cupsLangPrintf(stdout, "%s %s", device_class, device_uri);
}


/*
 * 'show_devices()' - Show available devices.
 */

static int				/* O - 0 on success, 1 on failure */
show_devices(
    int        long_status,		/* I - Long status report? */
    int        timeout,			/* I - Timeout */
    const char *include_schemes,	/* I - List of schemes to include */
    const char *exclude_schemes)	/* I - List of schemes to exclude */
{
  if (cupsGetDevices(CUPS_HTTP_DEFAULT, timeout, include_schemes,
                     exclude_schemes, device_cb, &long_status) != IPP_OK)
  {
    _cupsLangPrintf(stderr, "lpinfo: %s", cupsLastErrorString());
    return (1);
  }

  return (0);
}


/*
 * 'show_models()' - Show available PPDs.
 */

static int				/* O - 0 on success, 1 on failure */
show_models(
    int        long_status,		/* I - Long status report? */
    const char *device_id,		/* I - 1284 device ID */
    const char *language,		/* I - Language */
    const char *make_model,		/* I - Make and model */
    const char *product,		/* I - Product */
    const char *include_schemes,	/* I - List of schemes to include */
    const char *exclude_schemes)	/* I - List of schemes to exclude */
{
  ipp_t		*request,		/* IPP Request */
		*response;		/* IPP Response */
  ipp_attribute_t *attr;		/* Current attribute */
  const char	*ppd_device_id,		/* Pointer to ppd-device-id */
		*ppd_language,		/* Pointer to ppd-natural-language */
		*ppd_make_model,	/* Pointer to ppd-make-and-model */
		*ppd_name;		/* Pointer to ppd-name */
  cups_option_t	option;			/* in/exclude-schemes option */


 /*
  * Build a CUPS_GET_PPDS request...
  */

  request = ippNewRequest(CUPS_GET_PPDS);

  if (device_id)
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-device-id",
                 NULL, device_id);
  if (language)
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "ppd-language",
                 NULL, language);
  if (make_model)
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-make-and-model",
                 NULL, make_model);
  if (product)
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-product",
                 NULL, product);

  if (include_schemes)
  {
    option.name  = "include-schemes";
    option.value = (char *)include_schemes;

    cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
  }

  if (exclude_schemes)
  {
    option.name  = "exclude-schemes";
    option.value = (char *)exclude_schemes;

    cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
  }

 /*
  * Do the request and get back a response...
  */

  if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
  {
   /*
    * Loop through the device list and display them...
    */

    if (response->request.status.status_code > IPP_OK_CONFLICT)
    {
      _cupsLangPrintf(stderr, "lpinfo: %s", cupsLastErrorString());
      ippDelete(response);
      return (1);
    }

    for (attr = response->attrs; attr != NULL; attr = attr->next)
    {
     /*
      * Skip leading attributes until we hit a PPD...
      */

      while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
        attr = attr->next;

      if (attr == NULL)
        break;

     /*
      * Pull the needed attributes from this PPD...
      */

      ppd_device_id  = "NONE";
      ppd_language   = NULL;
      ppd_make_model = NULL;
      ppd_name       = NULL;

      while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
      {
        if (!strcmp(attr->name, "ppd-device-id") &&
	    attr->value_tag == IPP_TAG_TEXT)
	  ppd_device_id = attr->values[0].string.text;
        else if (!strcmp(attr->name, "ppd-natural-language") &&
	         attr->value_tag == IPP_TAG_LANGUAGE)
	  ppd_language = attr->values[0].string.text;
        else if (!strcmp(attr->name, "ppd-make-and-model") &&
	         attr->value_tag == IPP_TAG_TEXT)
	  ppd_make_model = attr->values[0].string.text;
        else if (!strcmp(attr->name, "ppd-name") &&
	         attr->value_tag == IPP_TAG_NAME)
	  ppd_name = attr->values[0].string.text;

        attr = attr->next;
      }

     /*
      * See if we have everything needed...
      */

      if (ppd_language == NULL || ppd_make_model == NULL || ppd_name == NULL)
      {
        if (attr == NULL)
	  break;
	else
          continue;
      }

     /*
      * Display the device...
      */

      if (long_status)
      {
	_cupsLangPrintf(stdout,
	                _("Model:  name = %s\n"
			  "        natural_language = %s\n"
			  "        make-and-model = %s\n"
			  "        device-id = %s"),
			ppd_name, ppd_language, ppd_make_model, ppd_device_id);
      }
      else
        _cupsLangPrintf(stdout, "%s %s", ppd_name, ppd_make_model);

      if (attr == NULL)
        break;
    }

    ippDelete(response);
  }
  else
  {
    _cupsLangPrintf(stderr, "lpinfo: %s", cupsLastErrorString());

    return (1);
  }

  return (0);
}