]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testmime.c
c1f2b6029be6c40642b57e1fdd98602000e20b87
2 * MIME test program for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/string-private.h>
20 #include <cups/debug-private.h>
21 #include <cups/ppd-private.h>
29 static void add_ppd_filter(mime_t
*mime
, mime_type_t
*filtertype
,
31 static void add_ppd_filters(mime_t
*mime
, ppd_file_t
*ppd
);
32 static void print_rules(mime_magic_t
*rules
);
33 static void type_dir(mime_t
*mime
, const char *dirname
);
37 * 'main()' - Main entry for the test program.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line args */
42 char *argv
[]) /* I - Command-line arguments */
44 int i
; /* Looping vars */
45 const char *filter_path
; /* Filter path */
46 char super
[MIME_MAX_SUPER
], /* Super-type name */
47 type
[MIME_MAX_TYPE
]; /* Type name */
48 int compression
; /* Compression of file */
49 int cost
; /* Cost of filters */
50 mime_t
*mime
; /* MIME database */
51 mime_type_t
*src
, /* Source type */
52 *dst
; /* Destination type */
53 struct stat srcinfo
; /* Source information */
54 ppd_file_t
*ppd
; /* PPD file */
55 cups_array_t
*filters
; /* Filters for the file */
56 mime_filter_t
*filter
; /* Current filter */
63 filter_path
= "../filter:" CUPS_SERVERBIN
"/filter";
67 for (i
= 1; i
< argc
; i
++)
68 if (!strcmp(argv
[i
], "-d"))
74 mime
= mimeLoad(argv
[i
], filter_path
);
77 add_ppd_filters(mime
, ppd
);
80 else if (!strcmp(argv
[i
], "-f"))
85 filter_path
= argv
[i
];
87 else if (!strcmp(argv
[i
], "-p"))
93 ppd
= ppdOpenFile(argv
[i
]);
96 add_ppd_filters(mime
, ppd
);
102 mime
= mimeLoad("../conf", filter_path
);
105 add_ppd_filters(mime
, ppd
);
107 src
= mimeFileType(mime
, argv
[i
], NULL
, &compression
);
108 stat(argv
[i
], &srcinfo
);
111 printf("%s: %s/%s%s\n", argv
[i
], src
->super
, src
->type
,
112 compression
? " (gzipped)" : "");
113 else if ((src
= mimeType(mime
, "application", "octet-stream")) != NULL
)
114 printf("%s: application/octet-stream\n", argv
[i
]);
117 printf("%s: unknown\n", argv
[i
]);
125 sscanf(argv
[i
], "%15[^/]/%255s", super
, type
);
126 dst
= mimeType(mime
, super
, type
);
128 filters
= mimeFilter2(mime
, src
, (size_t)srcinfo
.st_size
, dst
, &cost
);
132 printf("No filters to convert from %s/%s to %s.\n", src
->super
,
137 int first
= 1; /* First filter shown? */
139 printf("Filter cost = %d\n", cost
);
141 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
143 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
145 if (!strcmp(filter
->filter
, "-"))
151 fputs(filter
->filter
, stdout
);
154 printf(" | %s", filter
->filter
);
159 cupsArrayDelete(filters
);
165 mime
= mimeLoad("../conf", filter_path
);
167 add_ppd_filters(mime
, ppd
);
172 puts("MIME database types:");
173 for (src
= mimeFirstType(mime
); src
; src
= mimeNextType(mime
))
175 printf("\t%s/%s (%d):\n", src
->super
, src
->type
, src
->priority
);
176 print_rules(src
->rules
);
182 puts("MIME database filters:");
183 for (filter
= mimeFirstFilter(mime
); filter
; filter
= mimeNextFilter(mime
))
184 printf("\t%s/%s to %s/%s: %s (%d)\n",
185 filter
->src
->super
, filter
->src
->type
,
186 filter
->dst
->super
, filter
->dst
->type
,
187 filter
->filter
, filter
->cost
);
189 type_dir(mime
, "../doc");
197 * 'add_printer_filter()' - Add a printer filter from a PPD.
201 add_ppd_filter(mime_t
*mime
, /* I - MIME database */
202 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
203 const char *filter
) /* I - Filter to add */
205 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
206 type
[MIME_MAX_TYPE
], /* Type for filter */
207 dsuper
[MIME_MAX_SUPER
], /* Destination super-type for filter */
208 dtype
[MIME_MAX_TYPE
], /* Destination type for filter */
209 dest
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2],
210 /* Destination super/type */
211 program
[1024]; /* Program/filter name */
212 int cost
; /* Cost of filter */
213 size_t maxsize
= 0; /* Maximum supported file size */
214 mime_type_t
*temptype
, /* MIME type looping var */
215 *desttype
; /* Destination MIME type */
216 mime_filter_t
*filterptr
; /* MIME filter */
219 DEBUG_printf(("add_ppd_filter(mime=%p, filtertype=%p(%s/%s), filter=\"%s\")",
220 mime
, filtertype
, filtertype
->super
, filtertype
->type
, filter
));
223 * Parse the filter string; it should be in one of the following formats:
225 * source/type cost program
226 * source/type cost maxsize(nnnn) program
227 * source/type dest/type cost program
228 * source/type dest/type cost maxsize(nnnn) program
231 if (sscanf(filter
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
232 super
, type
, dsuper
, dtype
, &cost
, program
) == 6)
234 snprintf(dest
, sizeof(dest
), "test/%s/%s", dsuper
, dtype
);
236 if ((desttype
= mimeType(mime
, "printer", dest
)) == NULL
)
237 desttype
= mimeAddType(mime
, "printer", dest
);
241 if (sscanf(filter
, "%15[^/]/%255s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
244 desttype
= filtertype
;
248 printf("testmime: Invalid filter string \"%s\".\n", filter
);
253 if (!strncmp(program
, "maxsize(", 8))
255 char *ptr
; /* Pointer into maxsize(nnnn) program */
257 maxsize
= (size_t)strtoll(program
+ 8, &ptr
, 10);
261 printf("testmime: Invalid filter string \"%s\".\n", filter
);
266 while (_cups_isspace(*ptr
))
269 _cups_strcpy(program
, ptr
);
273 * Add the filter to the MIME database, supporting wildcards as needed...
276 for (temptype
= mimeFirstType(mime
);
278 temptype
= mimeNextType(mime
))
279 if (((super
[0] == '*' && _cups_strcasecmp(temptype
->super
, "printer")) ||
280 !_cups_strcasecmp(temptype
->super
, super
)) &&
281 (type
[0] == '*' || !_cups_strcasecmp(temptype
->type
, type
)))
283 if (desttype
!= filtertype
)
285 DEBUG_printf(("add_ppd_filter: Adding filter %s/%s %s/%s %d %s",
286 temptype
->super
, temptype
->type
, desttype
->super
,
287 desttype
->type
, cost
, program
));
288 filterptr
= mimeAddFilter(mime
, temptype
, desttype
, cost
, program
);
290 if (!mimeFilterLookup(mime
, desttype
, filtertype
))
292 DEBUG_printf(("add_printer_filter: Adding filter %s/%s %s/%s 0 -",
293 desttype
->super
, desttype
->type
, filtertype
->super
,
295 mimeAddFilter(mime
, desttype
, filtertype
, 0, "-");
300 DEBUG_printf(("add_printer_filter: Adding filter %s/%s %s/%s %d %s",
301 temptype
->super
, temptype
->type
, filtertype
->super
,
302 filtertype
->type
, cost
, program
));
303 filterptr
= mimeAddFilter(mime
, temptype
, filtertype
, cost
, program
);
307 filterptr
->maxsize
= maxsize
;
313 * 'add_ppd_filters()' - Add all filters from a PPD.
317 add_ppd_filters(mime_t
*mime
, /* I - MIME database */
318 ppd_file_t
*ppd
) /* I - PPD file */
320 _ppd_cache_t
*pc
; /* Cache data for PPD */
321 const char *value
; /* Filter definition value */
322 mime_type_t
*filter
, /* Filter type */
323 *prefilter
; /* Pre-filter type */
326 pc
= _ppdCacheCreateWithPPD(ppd
);
330 filter
= mimeAddType(mime
, "printer", "test");
334 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
336 value
= (const char *)cupsArrayNext(pc
->filters
))
337 add_ppd_filter(mime
, filter
, value
);
341 add_ppd_filter(mime
, filter
, "application/vnd.cups-raw 0 -");
342 add_ppd_filter(mime
, filter
, "application/vnd.cups-postscript 0 -");
347 prefilter
= mimeAddType(mime
, "prefilter", "test");
349 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
351 value
= (const char *)cupsArrayNext(pc
->prefilters
))
352 add_ppd_filter(mime
, prefilter
, value
);
358 * 'print_rules()' - Print the rules for a file type...
362 print_rules(mime_magic_t
*rules
) /* I - Rules to print */
364 int i
; /* Looping var */
365 static char indent
[255] = "\t"; /* Indentation for rules */
371 while (rules
!= NULL
)
373 printf("%s[%p] ", indent
, rules
);
380 case MIME_MAGIC_MATCH
:
381 printf("match(%s)", rules
->value
.matchv
);
383 case MIME_MAGIC_LOCALE
:
384 printf("locale(%s)", rules
->value
.localev
);
386 case MIME_MAGIC_ASCII
:
387 printf("ascii(%d,%d)", rules
->offset
, rules
->length
);
389 case MIME_MAGIC_PRINTABLE
:
390 printf("printable(%d,%d)", rules
->offset
, rules
->length
);
392 case MIME_MAGIC_STRING
:
393 printf("string(%d,", rules
->offset
);
394 for (i
= 0; i
< rules
->length
; i
++)
395 if (rules
->value
.stringv
[i
] < ' ' ||
396 rules
->value
.stringv
[i
] > 126)
397 printf("<%02X>", rules
->value
.stringv
[i
]);
399 putchar(rules
->value
.stringv
[i
]);
402 case MIME_MAGIC_CHAR
:
403 printf("char(%d,%d)", rules
->offset
, rules
->value
.charv
);
405 case MIME_MAGIC_SHORT
:
406 printf("short(%d,%d)", rules
->offset
, rules
->value
.shortv
);
408 case MIME_MAGIC_INT
:
409 printf("int(%d,%d)", rules
->offset
, rules
->value
.intv
);
411 case MIME_MAGIC_CONTAINS
:
412 printf("contains(%d,%d,", rules
->offset
, rules
->region
);
413 for (i
= 0; i
< rules
->length
; i
++)
414 if (rules
->value
.stringv
[i
] < ' ' ||
415 rules
->value
.stringv
[i
] > 126)
416 printf("<%02X>", rules
->value
.stringv
[i
]);
418 putchar(rules
->value
.stringv
[i
]);
425 if (rules
->child
!= NULL
)
427 if (rules
->op
== MIME_MAGIC_OR
)
432 strcat(indent
, "\t");
433 print_rules(rules
->child
);
434 indent
[strlen(indent
) - 1] = '\0';
435 printf("%s)\n", indent
);
446 * 'type_dir()' - Show the MIME types for a given directory.
450 type_dir(mime_t
*mime
, /* I - MIME database */
451 const char *dirname
) /* I - Directory */
453 cups_dir_t
*dir
; /* Directory */
454 cups_dentry_t
*dent
; /* Directory entry */
455 char filename
[1024]; /* File to type */
456 mime_type_t
*filetype
; /* File type */
457 int compression
; /* Compressed file? */
458 mime_type_t
*pstype
; /* application/vnd.cups-postscript */
459 cups_array_t
*filters
; /* Filters to pstype */
460 mime_filter_t
*filter
; /* Current filter */
461 int cost
; /* Filter cost */
464 dir
= cupsDirOpen(dirname
);
468 pstype
= mimeType(mime
, "application", "vnd.cups-postscript");
470 while ((dent
= cupsDirRead(dir
)) != NULL
)
472 if (dent
->filename
[0] == '.')
475 snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, dent
->filename
);
477 if (S_ISDIR(dent
->fileinfo
.st_mode
))
478 type_dir(mime
, filename
);
480 if (!S_ISREG(dent
->fileinfo
.st_mode
))
483 filetype
= mimeFileType(mime
, filename
, NULL
, &compression
);
487 printf("%s: %s/%s%s\n", filename
, filetype
->super
, filetype
->type
,
488 compression
? " (compressed)" : "");
490 filters
= mimeFilter(mime
, filetype
, pstype
, &cost
);
493 puts(" No filters to convert application/vnd.cups-postscript.");
496 printf(" Filter cost = %d\n", cost
);
498 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
499 printf(" %s", filter
->filter
);
501 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
503 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
504 printf(" | %s", filter
->filter
);
508 cupsArrayDelete(filters
);
512 printf("%s: unknown%s\n", filename
, compression
? " (compressed)" : "");