Imported Upstream version 2.1.2
[platform/upstream/cups.git] / cups / testdest.c
1 /*
2  * "$Id: testdest.c 11884 2014-05-16 21:54:22Z msweet $"
3  *
4  * CUPS destination API test program for CUPS.
5  *
6  * Copyright 2014 by Apple Inc.
7  *
8  * These coded instructions, statements, and computer programs are the
9  * property of Apple Inc. and are protected by Federal copyright
10  * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
11  * which should have been included with this file.  If this file is
12  * file is missing or damaged, see the license at "http://www.cups.org/".
13  *
14  * This file is subject to the Apple OS-Developed Software exception.
15  */
16
17 /*
18  * Include necessary headers...
19  */
20
21 #include <stdio.h>
22 #include "cups.h"
23
24
25 /*
26  * Local functions...
27  */
28
29 static void     localize(http_t *http, cups_dest_t *dest, cups_dinfo_t *dinfo, const char *option, const char *value);
30 static void     print_file(http_t *http, cups_dest_t *dest, cups_dinfo_t *dinfo, const char *filename, int num_options, cups_option_t *options);
31 static void     show_conflicts(http_t *http, cups_dest_t *dest, cups_dinfo_t *dinfo, int num_options, cups_option_t *options);
32 static void     show_default(http_t *http, cups_dest_t *dest, cups_dinfo_t *dinfo, const char *option);
33 static void     show_media(http_t *http, cups_dest_t *dest, cups_dinfo_t *dinfo, unsigned flags, const char *name);
34 static void     show_supported(http_t *http, cups_dest_t *dest, cups_dinfo_t *dinfo, const char *option, const char *value);
35 static void     usage(const char *arg) __attribute__((noreturn));
36
37
38 /*
39  * 'main()' - Main entry.
40  */
41
42 int                                     /* O - Exit status */
43 main(int  argc,                         /* I - Number of command-line arguments */
44      char *argv[])                      /* I - Command-line arguments */
45 {
46   http_t        *http;                  /* Connection to destination */
47   cups_dest_t   *dest = NULL;           /* Destination */
48   cups_dinfo_t  *dinfo;                 /* Destination info */
49
50
51   if (argc < 2)
52     usage(NULL);
53
54   if (!strcmp(argv[1], "--enum"))
55   {
56     return (0);
57   }
58   else if (!strncmp(argv[1], "ipp://", 6) || !strncmp(argv[1], "ipps://", 7))
59     dest = cupsGetDestWithURI(NULL, argv[1]);
60   else
61     dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, argv[1], NULL);
62
63   if (!dest)
64   {
65     printf("testdest: Unable to get destination \"%s\": %s\n", argv[1], cupsLastErrorString());
66     return (1);
67   }
68
69   if ((http = cupsConnectDest(dest, CUPS_DEST_FLAGS_NONE, 30000, NULL, NULL, 0, NULL, NULL)) == NULL)
70   {
71     printf("testdest: Unable to connect to destination \"%s\": %s\n", argv[1], cupsLastErrorString());
72     return (1);
73   }
74
75   if ((dinfo = cupsCopyDestInfo(http, dest)) == NULL)
76   {
77     printf("testdest: Unable to get information for destination \"%s\": %s\n", argv[1], cupsLastErrorString());
78     return (1);
79   }
80
81   if (argc == 2 || (!strcmp(argv[2], "supported") && argc < 6))
82   {
83     if (argc > 3)
84       show_supported(http, dest, dinfo, argv[3], argv[4]);
85     else if (argc > 2)
86       show_supported(http, dest, dinfo, argv[3], NULL);
87     else
88       show_supported(http, dest, dinfo, NULL, NULL);
89   }
90   else if (!strcmp(argv[2], "conflicts") && argc > 3)
91   {
92     int                 i,              /* Looping var */
93                         num_options = 0;/* Number of options */
94     cups_option_t       *options = NULL;/* Options */
95
96     for (i = 3; i < argc; i ++)
97       num_options = cupsParseOptions(argv[i], num_options, &options);
98
99     show_conflicts(http, dest, dinfo, num_options, options);
100   }
101   else if (!strcmp(argv[2], "default") && argc == 4)
102   {
103     show_default(http, dest, dinfo, argv[3]);
104   }
105   else if (!strcmp(argv[2], "localize") && argc > 3 && argc < 6)
106   {
107     localize(http, dest, dinfo, argv[3], argv[4]);
108   }
109   else if (!strcmp(argv[2], "media"))
110   {
111     int         i;                      /* Looping var */
112     const char  *name = NULL;           /* Media name, if any */
113     unsigned    flags = CUPS_MEDIA_FLAGS_DEFAULT;
114                                         /* Media selection flags */
115
116     for (i = 3; i < argc; i ++)
117     {
118       if (!strcmp(argv[i], "borderless"))
119         flags = CUPS_MEDIA_FLAGS_BORDERLESS;
120       else if (!strcmp(argv[i], "duplex"))
121         flags = CUPS_MEDIA_FLAGS_DUPLEX;
122       else if (!strcmp(argv[i], "exact"))
123         flags = CUPS_MEDIA_FLAGS_EXACT;
124       else if (!strcmp(argv[i], "ready"))
125         flags = CUPS_MEDIA_FLAGS_READY;
126       else if (name)
127         usage(argv[i]);
128       else
129         name = argv[i];
130     }
131
132     show_media(http, dest, dinfo, flags, name);
133   }
134   else if (!strcmp(argv[2], "print") && argc > 3)
135   {
136     int                 i,              /* Looping var */
137                         num_options = 0;/* Number of options */
138     cups_option_t       *options = NULL;/* Options */
139
140     for (i = 4; i < argc; i ++)
141       num_options = cupsParseOptions(argv[i], num_options, &options);
142
143     print_file(http, dest, dinfo, argv[3], num_options, options);
144   }
145   else
146     usage(argv[2]);
147
148   return (0);
149 }
150
151
152 /*
153  * 'localize()' - Localize an option and value.
154  */
155
156 static void
157 localize(http_t       *http,            /* I - Connection to destination */
158          cups_dest_t  *dest,            /* I - Destination */
159          cups_dinfo_t *dinfo,           /* I - Destination information */
160          const char   *option,          /* I - Option */
161          const char   *value)           /* I - Value, if any */
162 {
163   (void)http;
164   (void)dest;
165   (void)dinfo;
166   (void)option;
167   (void)value;
168 }
169
170
171 /*
172  * 'print_file()' - Print a file.
173  */
174
175 static void
176 print_file(http_t        *http,         /* I - Connection to destination */
177            cups_dest_t   *dest,         /* I - Destination */
178            cups_dinfo_t  *dinfo,        /* I - Destination information */
179            const char    *filename,     /* I - File to print */
180            int           num_options,   /* I - Number of options */
181            cups_option_t *options)      /* I - Options */
182 {
183   (void)http;
184   (void)dest;
185   (void)dinfo;
186   (void)filename;
187   (void)num_options;
188   (void)options;
189 }
190
191
192 /*
193  * 'show_conflicts()' - Show conflicts for selected options.
194  */
195
196 static void
197 show_conflicts(
198     http_t        *http,                /* I - Connection to destination */
199     cups_dest_t   *dest,                /* I - Destination */
200     cups_dinfo_t  *dinfo,               /* I - Destination information */
201     int           num_options,          /* I - Number of options */
202     cups_option_t *options)             /* I - Options */
203 {
204   (void)http;
205   (void)dest;
206   (void)dinfo;
207   (void)num_options;
208   (void)options;
209 }
210
211
212 /*
213  * 'show_default()' - Show default value for option.
214  */
215
216 static void
217 show_default(http_t       *http,        /* I - Connection to destination */
218              cups_dest_t  *dest,        /* I - Destination */
219              cups_dinfo_t *dinfo,       /* I - Destination information */
220              const char  *option)       /* I - Option */
221 {
222   (void)http;
223   (void)dest;
224   (void)dinfo;
225   (void)option;
226 }
227
228
229 /*
230  * 'show_media()' - Show available media.
231  */
232
233 static void
234 show_media(http_t       *http,          /* I - Connection to destination */
235            cups_dest_t  *dest,          /* I - Destination */
236            cups_dinfo_t *dinfo,         /* I - Destination information */
237            unsigned     flags,          /* I - Media flags */
238            const char   *name)          /* I - Size name */
239 {
240   int           i,                      /* Looping var */
241                 count;                  /* Number of sizes */
242   cups_size_t   size;                   /* Media size info */
243
244
245   if (name)
246   {
247     double      dw, dl;                 /* Width and length from name */
248     char        units[32];              /* Units */
249     int         width,                  /* Width in 100ths of millimeters */
250                 length;                 /* Length in 100ths of millimeters */
251
252
253     if (sscanf(name, "%lfx%lf%31s", &dw, &dl, units) == 3)
254     {
255       if (!strcmp(units, "in"))
256       {
257         width  = (int)(dw * 2540.0);
258         length = (int)(dl * 2540.0);
259       }
260       else if (!strcmp(units, "mm"))
261       {
262         width  = (int)(dw * 100.0);
263         length = (int)(dl * 100.0);
264       }
265       else
266       {
267         puts("  bad units in size");
268         return;
269       }
270
271       if (cupsGetDestMediaBySize(http, dest, dinfo, width, length, flags, &size))
272       {
273         printf("  %s (%s) %dx%d B%d L%d R%d T%d\n", size.media, cupsLocalizeDestMedia(http, dest, dinfo, flags, &size), size.width, size.length, size.bottom, size.left, size.right, size.top);
274       }
275       else
276       {
277         puts("  not supported");
278       }
279     }
280     else if (cupsGetDestMediaByName(http, dest, dinfo, name, flags, &size))
281     {
282       printf("  %s (%s) %dx%d B%d L%d R%d T%d\n", size.media, cupsLocalizeDestMedia(http, dest, dinfo, flags, &size), size.width, size.length, size.bottom, size.left, size.right, size.top);
283     }
284     else
285     {
286       puts("  not supported");
287     }
288   }
289   else
290   {
291     count = cupsGetDestMediaCount(http, dest, dinfo, flags);
292     printf("%d size%s:\n", count, count == 1 ? "" : "s");
293
294     for (i = 0; i < count; i ++)
295     {
296       if (cupsGetDestMediaByIndex(http, dest, dinfo, i, flags, &size))
297         printf("  %s (%s) %dx%d B%d L%d R%d T%d\n", size.media, cupsLocalizeDestMedia(http, dest, dinfo, flags, &size), size.width, size.length, size.bottom, size.left, size.right, size.top);
298       else
299         puts("  error");
300     }
301   }
302 }
303
304
305 /*
306  * 'show_supported()' - Show supported options, values, etc.
307  */
308
309 static void
310 show_supported(http_t       *http,      /* I - Connection to destination */
311                cups_dest_t  *dest,      /* I - Destination */
312                cups_dinfo_t *dinfo,     /* I - Destination information */
313                const char   *option,    /* I - Option, if any */
314                const char   *value)     /* I - Value, if any */
315 {
316   ipp_attribute_t       *attr;          /* Attribute */
317   int                   i,              /* Looping var */
318                         count;          /* Number of values */
319
320
321   if (!option)
322   {
323     attr = cupsFindDestSupported(http, dest, dinfo, "job-creation-attributes");
324     if (attr)
325     {
326       count = ippGetCount(attr);
327       for (i = 0; i < count; i ++)
328         show_supported(http, dest, dinfo, ippGetString(attr, i, NULL), NULL);
329     }
330     else
331     {
332       static const char * const options[] =
333       {                                 /* List of standard options */
334         CUPS_COPIES,
335         CUPS_FINISHINGS,
336         CUPS_MEDIA,
337         CUPS_NUMBER_UP,
338         CUPS_ORIENTATION,
339         CUPS_PRINT_COLOR_MODE,
340         CUPS_PRINT_QUALITY,
341         CUPS_SIDES
342       };
343
344       puts("No job-creation-attributes-supported attribute, probing instead.");
345
346       for (i = 0; i < (int)(sizeof(options) / sizeof(options[0])); i ++)
347         if (cupsCheckDestSupported(http, dest, dinfo, options[i], NULL))
348           show_supported(http, dest, dinfo, options[i], NULL);
349     }
350   }
351   else if (!value)
352   {
353     puts(option);
354     if ((attr = cupsFindDestSupported(http, dest, dinfo, option)) != NULL)
355     {
356       count = ippGetCount(attr);
357
358       switch (ippGetValueTag(attr))
359       {
360         case IPP_TAG_INTEGER :
361             for (i = 0; i < count; i ++)
362               printf("  %d\n", ippGetInteger(attr, i));
363             break;
364
365         case IPP_TAG_ENUM :
366             for (i = 0; i < count; i ++)
367               printf("  %s\n", ippEnumString(option, ippGetInteger(attr, i)));
368             break;
369
370         case IPP_TAG_RANGE :
371             for (i = 0; i < count; i ++)
372             {
373               int upper, lower = ippGetRange(attr, i, &upper);
374
375               printf("  %d-%d\n", lower, upper);
376             }
377             break;
378
379         case IPP_TAG_TEXTLANG :
380         case IPP_TAG_NAMELANG :
381         case IPP_TAG_TEXT :
382         case IPP_TAG_NAME :
383         case IPP_TAG_KEYWORD :
384         case IPP_TAG_URI :
385         case IPP_TAG_URISCHEME :
386         case IPP_TAG_CHARSET :
387         case IPP_TAG_LANGUAGE :
388         case IPP_TAG_MIMETYPE :
389             for (i = 0; i < count; i ++)
390               printf("  %s\n", ippGetString(attr, i, NULL));
391             break;
392
393         case IPP_TAG_STRING :
394             for (i = 0; i < count; i ++)
395             {
396               int j, len;
397               unsigned char *data = ippGetOctetString(attr, i, &len);
398
399               fputs("  ", stdout);
400               for (j = 0; j < len; j ++)
401               {
402                 if (data[j] < ' ' || data[j] >= 0x7f)
403                   printf("<%02X>", data[j]);
404                 else
405                   putchar(data[j]);
406               }
407               putchar('\n');
408             }
409             break;
410
411         case IPP_TAG_BOOLEAN :
412             break;
413
414         default :
415             printf("  %s\n", ippTagString(ippGetValueTag(attr)));
416             break;
417       }
418     }
419     
420   }
421   else if (cupsCheckDestSupported(http, dest, dinfo, option, value))
422     puts("YES");
423   else
424     puts("NO");
425
426 }
427
428
429 /*
430  * 'usage()' - Show program usage.
431  */
432
433 static void
434 usage(const char *arg)                  /* I - Argument for usage message */
435 {
436   if (arg)
437     printf("testdest: Unknown option \"%s\".\n", arg);
438
439   puts("Usage:");
440   puts("  ./testdest name [operation ...]");
441   puts("  ./testdest ipp://... [operation ...]");
442   puts("  ./testdest ipps://... [operation ...]");
443   puts("  ./testdest --enum [grayscale] [color] [duplex] [staple] [small]\n"
444        "                    [medium] [large]");
445   puts("");
446   puts("Operations:");
447   puts("  conflicts options");
448   puts("  default option");
449   puts("  localize option [value]");
450   puts("  media [borderless] [duplex] [exact] [ready] [name or size]");
451   puts("  print filename [options]");
452   puts("  supported [option [value]]");
453
454   exit(arg != NULL);
455 }
456
457
458 /*
459  * End of "$Id: testdest.c 11884 2014-05-16 21:54:22Z msweet $".
460  */