]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testmime.c
2 * "$Id: testmime.c 7670 2008-06-17 22:42:08Z mike $"
4 * MIME test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * main() - Main entry for the test program.
18 * print_rules() - Print the rules for a file type...
19 * type_dir() - Show the MIME types for a given directory.
23 * Include necessary headers...
28 #include <cups/string.h>
37 static void print_rules(mime_magic_t
*rules
);
38 static void type_dir(mime_t
*mime
, const char *dirname
);
42 * 'main()' - Main entry for the test program.
45 int /* O - Exit status */
46 main(int argc
, /* I - Number of command-line args */
47 char *argv
[]) /* I - Command-line arguments */
49 int i
; /* Looping vars */
50 const char *filter_path
; /* Filter path */
51 char super
[MIME_MAX_SUPER
], /* Super-type name */
52 type
[MIME_MAX_TYPE
]; /* Type name */
53 int compression
; /* Compression of file */
54 int cost
; /* Cost of filters */
55 mime_t
*mime
; /* MIME database */
56 mime_type_t
*src
, /* Source type */
57 *dst
; /* Destination type */
58 cups_array_t
*filters
; /* Filters for the file */
59 mime_filter_t
*filter
; /* Current filter */
65 filter_path
= "../filter:../pdftops:" CUPS_SERVERBIN
"/filter";
67 for (i
= 1; i
< argc
; i
++)
68 if (!strcmp(argv
[i
], "-d"))
73 mime
= mimeLoad(argv
[i
], filter_path
);
75 else if (!strcmp(argv
[i
], "-f"))
80 filter_path
= argv
[i
];
85 mime
= mimeLoad("../conf", filter_path
);
87 src
= mimeFileType(mime
, argv
[i
], NULL
, &compression
);
90 printf("%s: %s/%s%s\n", argv
[i
], src
->super
, src
->type
,
91 compression
? " (gzipped)" : "");
92 else if ((src
= mimeType(mime
, "application", "octet-stream")) != NULL
)
93 printf("%s: application/octet-stream\n", argv
[i
]);
96 printf("%s: unknown\n", argv
[i
]);
104 sscanf(argv
[i
], "%15[^/]/%31s", super
, type
);
105 dst
= mimeType(mime
, super
, type
);
107 filters
= mimeFilter(mime
, src
, dst
, &cost
);
111 printf("No filters to convert from %s/%s to %s.\n", src
->super
,
116 printf("Filter cost = %d\n", cost
);
118 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
119 fputs(filter
->filter
, stdout
);
121 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
123 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
124 printf(" | %s", filter
->filter
);
128 cupsArrayDelete(filters
);
133 mime
= mimeLoad("../conf", filter_path
);
137 puts("MIME database types:");
138 for (src
= mimeFirstType(mime
); src
; src
= mimeNextType(mime
))
140 printf("\t%s/%s:\n", src
->super
, src
->type
);
141 print_rules(src
->rules
);
147 puts("MIME database filters:");
148 for (filter
= mimeFirstFilter(mime
); filter
; filter
= mimeNextFilter(mime
))
149 printf("\t%s/%s to %s/%s: %s (%d)\n",
150 filter
->src
->super
, filter
->src
->type
,
151 filter
->dst
->super
, filter
->dst
->type
,
152 filter
->filter
, filter
->cost
);
154 type_dir(mime
, "../doc");
155 type_dir(mime
, "../man");
163 * 'print_rules()' - Print the rules for a file type...
167 print_rules(mime_magic_t
*rules
) /* I - Rules to print */
169 int i
; /* Looping var */
170 static char indent
[255] = "\t"; /* Indentation for rules */
176 while (rules
!= NULL
)
178 printf("%s[%p] ", indent
, rules
);
185 case MIME_MAGIC_MATCH
:
186 printf("match(%s)", rules
->value
.matchv
);
188 case MIME_MAGIC_LOCALE
:
189 printf("locale(%s)", rules
->value
.localev
);
191 case MIME_MAGIC_ASCII
:
192 printf("ascii(%d,%d)", rules
->offset
, rules
->length
);
194 case MIME_MAGIC_PRINTABLE
:
195 printf("printable(%d,%d)", rules
->offset
, rules
->length
);
197 case MIME_MAGIC_STRING
:
198 printf("string(%d,", rules
->offset
);
199 for (i
= 0; i
< rules
->length
; i
++)
200 if (rules
->value
.stringv
[i
] < ' ' ||
201 rules
->value
.stringv
[i
] > 126)
202 printf("<%02X>", rules
->value
.stringv
[i
]);
204 putchar(rules
->value
.stringv
[i
]);
207 case MIME_MAGIC_CHAR
:
208 printf("char(%d,%d)", rules
->offset
, rules
->value
.charv
);
210 case MIME_MAGIC_SHORT
:
211 printf("short(%d,%d)", rules
->offset
, rules
->value
.shortv
);
213 case MIME_MAGIC_INT
:
214 printf("int(%d,%d)", rules
->offset
, rules
->value
.intv
);
216 case MIME_MAGIC_CONTAINS
:
217 printf("contains(%d,%d,", rules
->offset
, rules
->region
);
218 for (i
= 0; i
< rules
->length
; i
++)
219 if (rules
->value
.stringv
[i
] < ' ' ||
220 rules
->value
.stringv
[i
] > 126)
221 printf("<%02X>", rules
->value
.stringv
[i
]);
223 putchar(rules
->value
.stringv
[i
]);
230 if (rules
->child
!= NULL
)
232 if (rules
->op
== MIME_MAGIC_OR
)
237 strcat(indent
, "\t");
238 print_rules(rules
->child
);
239 indent
[strlen(indent
) - 1] = '\0';
240 printf("%s)\n", indent
);
251 * 'type_dir()' - Show the MIME types for a given directory.
255 type_dir(mime_t
*mime
, /* I - MIME database */
256 const char *dirname
) /* I - Directory */
258 cups_dir_t
*dir
; /* Directory */
259 cups_dentry_t
*dent
; /* Directory entry */
260 char filename
[1024]; /* File to type */
261 mime_type_t
*filetype
; /* File type */
262 int compression
; /* Compressed file? */
263 mime_type_t
*pstype
; /* application/vnd.cups-postscript */
264 cups_array_t
*filters
; /* Filters to pstype */
265 mime_filter_t
*filter
; /* Current filter */
266 int cost
; /* Filter cost */
269 dir
= cupsDirOpen(dirname
);
273 pstype
= mimeType(mime
, "application", "vnd.cups-postscript");
275 while ((dent
= cupsDirRead(dir
)) != NULL
)
277 if (dent
->filename
[0] == '.')
280 snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, dent
->filename
);
282 if (S_ISDIR(dent
->fileinfo
.st_mode
))
283 type_dir(mime
, filename
);
285 if (!S_ISREG(dent
->fileinfo
.st_mode
))
288 filetype
= mimeFileType(mime
, filename
, NULL
, &compression
);
292 printf("%s: %s/%s%s\n", filename
, filetype
->super
, filetype
->type
,
293 compression
? " (compressed)" : "");
295 filters
= mimeFilter(mime
, filetype
, pstype
, &cost
);
298 puts(" No filters to convert application/vnd.cups-postscript.");
301 printf(" Filter cost = %d\n", cost
);
303 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
304 printf(" %s", filter
->filter
);
306 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
308 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
309 printf(" | %s", filter
->filter
);
313 cupsArrayDelete(filters
);
317 printf("%s: unknown%s\n", filename
, compression
? " (compressed)" : "");
325 * End of "$Id: testmime.c 7670 2008-06-17 22:42:08Z mike $".