]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testmime.c
4 * MIME test program for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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 * Include necessary headers...
20 #include <cups/string-private.h>
22 #include <cups/debug-private.h>
23 #include <cups/ppd-private.h>
31 static void add_ppd_filter(mime_t
*mime
, mime_type_t
*filtertype
,
33 static void add_ppd_filters(mime_t
*mime
, ppd_file_t
*ppd
);
34 static void print_rules(mime_magic_t
*rules
);
35 static void type_dir(mime_t
*mime
, const char *dirname
);
39 * 'main()' - Main entry for the test program.
42 int /* O - Exit status */
43 main(int argc
, /* I - Number of command-line args */
44 char *argv
[]) /* I - Command-line arguments */
46 int i
; /* Looping vars */
47 const char *filter_path
; /* Filter path */
48 char super
[MIME_MAX_SUPER
], /* Super-type name */
49 type
[MIME_MAX_TYPE
]; /* Type name */
50 int compression
; /* Compression of file */
51 int cost
; /* Cost of filters */
52 mime_t
*mime
; /* MIME database */
53 mime_type_t
*src
, /* Source type */
54 *dst
; /* Destination type */
55 struct stat srcinfo
; /* Source information */
56 ppd_file_t
*ppd
; /* PPD file */
57 cups_array_t
*filters
; /* Filters for the file */
58 mime_filter_t
*filter
; /* Current filter */
65 filter_path
= "../filter:" CUPS_SERVERBIN
"/filter";
69 for (i
= 1; i
< argc
; i
++)
70 if (!strcmp(argv
[i
], "-d"))
76 mime
= mimeLoad(argv
[i
], filter_path
);
79 add_ppd_filters(mime
, ppd
);
82 else if (!strcmp(argv
[i
], "-f"))
87 filter_path
= argv
[i
];
89 else if (!strcmp(argv
[i
], "-p"))
95 ppd
= ppdOpenFile(argv
[i
]);
98 add_ppd_filters(mime
, ppd
);
104 mime
= mimeLoad("../conf", filter_path
);
107 add_ppd_filters(mime
, ppd
);
109 src
= mimeFileType(mime
, argv
[i
], NULL
, &compression
);
110 stat(argv
[i
], &srcinfo
);
113 printf("%s: %s/%s%s\n", argv
[i
], src
->super
, src
->type
,
114 compression
? " (gzipped)" : "");
115 else if ((src
= mimeType(mime
, "application", "octet-stream")) != NULL
)
116 printf("%s: application/octet-stream\n", argv
[i
]);
119 printf("%s: unknown\n", argv
[i
]);
127 sscanf(argv
[i
], "%15[^/]/%255s", super
, type
);
128 dst
= mimeType(mime
, super
, type
);
130 filters
= mimeFilter2(mime
, src
, (size_t)srcinfo
.st_size
, dst
, &cost
);
134 printf("No filters to convert from %s/%s to %s.\n", src
->super
,
139 int first
= 1; /* First filter shown? */
141 printf("Filter cost = %d\n", cost
);
143 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
145 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
147 if (!strcmp(filter
->filter
, "-"))
153 fputs(filter
->filter
, stdout
);
156 printf(" | %s", filter
->filter
);
161 cupsArrayDelete(filters
);
167 mime
= mimeLoad("../conf", filter_path
);
169 add_ppd_filters(mime
, ppd
);
174 puts("MIME database types:");
175 for (src
= mimeFirstType(mime
); src
; src
= mimeNextType(mime
))
177 printf("\t%s/%s (%d):\n", src
->super
, src
->type
, src
->priority
);
178 print_rules(src
->rules
);
184 puts("MIME database filters:");
185 for (filter
= mimeFirstFilter(mime
); filter
; filter
= mimeNextFilter(mime
))
186 printf("\t%s/%s to %s/%s: %s (%d)\n",
187 filter
->src
->super
, filter
->src
->type
,
188 filter
->dst
->super
, filter
->dst
->type
,
189 filter
->filter
, filter
->cost
);
191 type_dir(mime
, "../doc");
199 * 'add_printer_filter()' - Add a printer filter from a PPD.
203 add_ppd_filter(mime_t
*mime
, /* I - MIME database */
204 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
205 const char *filter
) /* I - Filter to add */
207 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
208 type
[MIME_MAX_TYPE
], /* Type for filter */
209 dsuper
[MIME_MAX_SUPER
], /* Destination super-type for filter */
210 dtype
[MIME_MAX_TYPE
], /* Destination type for filter */
211 dest
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2],
212 /* Destination super/type */
213 program
[1024]; /* Program/filter name */
214 int cost
; /* Cost of filter */
215 size_t maxsize
= 0; /* Maximum supported file size */
216 mime_type_t
*temptype
, /* MIME type looping var */
217 *desttype
; /* Destination MIME type */
218 mime_filter_t
*filterptr
; /* MIME filter */
221 DEBUG_printf(("add_ppd_filter(mime=%p, filtertype=%p(%s/%s), filter=\"%s\")",
222 mime
, filtertype
, filtertype
->super
, filtertype
->type
, filter
));
225 * Parse the filter string; it should be in one of the following formats:
227 * source/type cost program
228 * source/type cost maxsize(nnnn) program
229 * source/type dest/type cost program
230 * source/type dest/type cost maxsize(nnnn) program
233 if (sscanf(filter
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
234 super
, type
, dsuper
, dtype
, &cost
, program
) == 6)
236 snprintf(dest
, sizeof(dest
), "test/%s/%s", dsuper
, dtype
);
238 if ((desttype
= mimeType(mime
, "printer", dest
)) == NULL
)
239 desttype
= mimeAddType(mime
, "printer", dest
);
243 if (sscanf(filter
, "%15[^/]/%255s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
246 desttype
= filtertype
;
250 printf("testmime: Invalid filter string \"%s\".\n", filter
);
255 if (!strncmp(program
, "maxsize(", 8))
257 char *ptr
; /* Pointer into maxsize(nnnn) program */
259 maxsize
= (size_t)strtoll(program
+ 8, &ptr
, 10);
263 printf("testmime: Invalid filter string \"%s\".\n", filter
);
268 while (_cups_isspace(*ptr
))
271 _cups_strcpy(program
, ptr
);
275 * Add the filter to the MIME database, supporting wildcards as needed...
278 for (temptype
= mimeFirstType(mime
);
280 temptype
= mimeNextType(mime
))
281 if (((super
[0] == '*' && _cups_strcasecmp(temptype
->super
, "printer")) ||
282 !_cups_strcasecmp(temptype
->super
, super
)) &&
283 (type
[0] == '*' || !_cups_strcasecmp(temptype
->type
, type
)))
285 if (desttype
!= filtertype
)
287 DEBUG_printf(("add_ppd_filter: Adding filter %s/%s %s/%s %d %s",
288 temptype
->super
, temptype
->type
, desttype
->super
,
289 desttype
->type
, cost
, program
));
290 filterptr
= mimeAddFilter(mime
, temptype
, desttype
, cost
, program
);
292 if (!mimeFilterLookup(mime
, desttype
, filtertype
))
294 DEBUG_printf(("add_printer_filter: Adding filter %s/%s %s/%s 0 -",
295 desttype
->super
, desttype
->type
, filtertype
->super
,
297 mimeAddFilter(mime
, desttype
, filtertype
, 0, "-");
302 DEBUG_printf(("add_printer_filter: Adding filter %s/%s %s/%s %d %s",
303 temptype
->super
, temptype
->type
, filtertype
->super
,
304 filtertype
->type
, cost
, program
));
305 filterptr
= mimeAddFilter(mime
, temptype
, filtertype
, cost
, program
);
309 filterptr
->maxsize
= maxsize
;
315 * 'add_ppd_filters()' - Add all filters from a PPD.
319 add_ppd_filters(mime_t
*mime
, /* I - MIME database */
320 ppd_file_t
*ppd
) /* I - PPD file */
322 _ppd_cache_t
*pc
; /* Cache data for PPD */
323 const char *value
; /* Filter definition value */
324 mime_type_t
*filter
, /* Filter type */
325 *prefilter
; /* Pre-filter type */
328 pc
= _ppdCacheCreateWithPPD(ppd
);
332 filter
= mimeAddType(mime
, "printer", "test");
336 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
338 value
= (const char *)cupsArrayNext(pc
->filters
))
339 add_ppd_filter(mime
, filter
, value
);
343 add_ppd_filter(mime
, filter
, "application/vnd.cups-raw 0 -");
344 add_ppd_filter(mime
, filter
, "application/vnd.cups-postscript 0 -");
349 prefilter
= mimeAddType(mime
, "prefilter", "test");
351 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
353 value
= (const char *)cupsArrayNext(pc
->prefilters
))
354 add_ppd_filter(mime
, prefilter
, value
);
360 * 'print_rules()' - Print the rules for a file type...
364 print_rules(mime_magic_t
*rules
) /* I - Rules to print */
366 int i
; /* Looping var */
367 static char indent
[255] = "\t"; /* Indentation for rules */
373 while (rules
!= NULL
)
375 printf("%s[%p] ", indent
, rules
);
382 case MIME_MAGIC_MATCH
:
383 printf("match(%s)", rules
->value
.matchv
);
385 case MIME_MAGIC_LOCALE
:
386 printf("locale(%s)", rules
->value
.localev
);
388 case MIME_MAGIC_ASCII
:
389 printf("ascii(%d,%d)", rules
->offset
, rules
->length
);
391 case MIME_MAGIC_PRINTABLE
:
392 printf("printable(%d,%d)", rules
->offset
, rules
->length
);
394 case MIME_MAGIC_STRING
:
395 printf("string(%d,", rules
->offset
);
396 for (i
= 0; i
< rules
->length
; i
++)
397 if (rules
->value
.stringv
[i
] < ' ' ||
398 rules
->value
.stringv
[i
] > 126)
399 printf("<%02X>", rules
->value
.stringv
[i
]);
401 putchar(rules
->value
.stringv
[i
]);
404 case MIME_MAGIC_CHAR
:
405 printf("char(%d,%d)", rules
->offset
, rules
->value
.charv
);
407 case MIME_MAGIC_SHORT
:
408 printf("short(%d,%d)", rules
->offset
, rules
->value
.shortv
);
410 case MIME_MAGIC_INT
:
411 printf("int(%d,%d)", rules
->offset
, rules
->value
.intv
);
413 case MIME_MAGIC_CONTAINS
:
414 printf("contains(%d,%d,", rules
->offset
, rules
->region
);
415 for (i
= 0; i
< rules
->length
; i
++)
416 if (rules
->value
.stringv
[i
] < ' ' ||
417 rules
->value
.stringv
[i
] > 126)
418 printf("<%02X>", rules
->value
.stringv
[i
]);
420 putchar(rules
->value
.stringv
[i
]);
427 if (rules
->child
!= NULL
)
429 if (rules
->op
== MIME_MAGIC_OR
)
434 strcat(indent
, "\t");
435 print_rules(rules
->child
);
436 indent
[strlen(indent
) - 1] = '\0';
437 printf("%s)\n", indent
);
448 * 'type_dir()' - Show the MIME types for a given directory.
452 type_dir(mime_t
*mime
, /* I - MIME database */
453 const char *dirname
) /* I - Directory */
455 cups_dir_t
*dir
; /* Directory */
456 cups_dentry_t
*dent
; /* Directory entry */
457 char filename
[1024]; /* File to type */
458 mime_type_t
*filetype
; /* File type */
459 int compression
; /* Compressed file? */
460 mime_type_t
*pstype
; /* application/vnd.cups-postscript */
461 cups_array_t
*filters
; /* Filters to pstype */
462 mime_filter_t
*filter
; /* Current filter */
463 int cost
; /* Filter cost */
466 dir
= cupsDirOpen(dirname
);
470 pstype
= mimeType(mime
, "application", "vnd.cups-postscript");
472 while ((dent
= cupsDirRead(dir
)) != NULL
)
474 if (dent
->filename
[0] == '.')
477 snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, dent
->filename
);
479 if (S_ISDIR(dent
->fileinfo
.st_mode
))
480 type_dir(mime
, filename
);
482 if (!S_ISREG(dent
->fileinfo
.st_mode
))
485 filetype
= mimeFileType(mime
, filename
, NULL
, &compression
);
489 printf("%s: %s/%s%s\n", filename
, filetype
->super
, filetype
->type
,
490 compression
? " (compressed)" : "");
492 filters
= mimeFilter(mime
, filetype
, pstype
, &cost
);
495 puts(" No filters to convert application/vnd.cups-postscript.");
498 printf(" Filter cost = %d\n", cost
);
500 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
501 printf(" %s", filter
->filter
);
503 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
505 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
506 printf(" | %s", filter
->filter
);
510 cupsArrayDelete(filters
);
514 printf("%s: unknown%s\n", filename
, compression
? " (compressed)" : "");