]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: testmime.c 7670 2008-06-17 22:42:08Z mike $" |
ef416fc2 | 3 | * |
71e16022 | 4 | * MIME test program for CUPS. |
ef416fc2 | 5 | * |
71e16022 | 6 | * Copyright 2007-2010 by Apple Inc. |
fa73b229 | 7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
4400e98d | 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. | |
ef416fc2 | 20 | */ |
21 | ||
22 | /* | |
23 | * Include necessary headers... | |
24 | */ | |
25 | ||
71e16022 | 26 | #include <cups/string-private.h> |
4400e98d | 27 | #include <cups/dir.h> |
71e16022 | 28 | #include "mime.h" |
ef416fc2 | 29 | |
30 | ||
31 | /* | |
32 | * Local functions... | |
33 | */ | |
34 | ||
4400e98d | 35 | static void print_rules(mime_magic_t *rules); |
36 | static void type_dir(mime_t *mime, const char *dirname); | |
ef416fc2 | 37 | |
38 | ||
39 | /* | |
40 | * 'main()' - Main entry for the test program. | |
41 | */ | |
42 | ||
43 | int /* O - Exit status */ | |
44 | main(int argc, /* I - Number of command-line args */ | |
45 | char *argv[]) /* I - Command-line arguments */ | |
46 | { | |
fa73b229 | 47 | int i; /* Looping vars */ |
ef416fc2 | 48 | const char *filter_path; /* Filter path */ |
49 | char super[MIME_MAX_SUPER], /* Super-type name */ | |
50 | type[MIME_MAX_TYPE]; /* Type name */ | |
51 | int compression; /* Compression of file */ | |
fa73b229 | 52 | int cost; /* Cost of filters */ |
ef416fc2 | 53 | mime_t *mime; /* MIME database */ |
54 | mime_type_t *src, /* Source type */ | |
fa73b229 | 55 | *dst; /* Destination type */ |
56 | cups_array_t *filters; /* Filters for the file */ | |
57 | mime_filter_t *filter; /* Current filter */ | |
ef416fc2 | 58 | |
59 | ||
60 | mime = NULL; | |
61 | src = NULL; | |
62 | dst = NULL; | |
ed486911 | 63 | filter_path = "../filter:../pdftops:" CUPS_SERVERBIN "/filter"; |
ef416fc2 | 64 | |
65 | for (i = 1; i < argc; i ++) | |
fa73b229 | 66 | if (!strcmp(argv[i], "-d")) |
ef416fc2 | 67 | { |
68 | i ++; | |
69 | ||
70 | if (i < argc) | |
71 | mime = mimeLoad(argv[i], filter_path); | |
72 | } | |
fa73b229 | 73 | else if (!strcmp(argv[i], "-f")) |
ef416fc2 | 74 | { |
75 | i ++; | |
76 | ||
77 | if (i < argc) | |
78 | filter_path = argv[i]; | |
79 | } | |
fa73b229 | 80 | else if (!src) |
ef416fc2 | 81 | { |
82 | if (!mime) | |
83 | mime = mimeLoad("../conf", filter_path); | |
84 | ||
4400e98d | 85 | src = mimeFileType(mime, argv[i], NULL, &compression); |
ef416fc2 | 86 | |
fa73b229 | 87 | if (src) |
ef416fc2 | 88 | printf("%s: %s/%s%s\n", argv[i], src->super, src->type, |
89 | compression ? " (gzipped)" : ""); | |
f301802f | 90 | else if ((src = mimeType(mime, "application", "octet-stream")) != NULL) |
91 | printf("%s: application/octet-stream\n", argv[i]); | |
ef416fc2 | 92 | else |
93 | { | |
94 | printf("%s: unknown\n", argv[i]); | |
95 | if (mime) | |
96 | mimeDelete(mime); | |
97 | return (1); | |
98 | } | |
99 | } | |
100 | else | |
101 | { | |
102 | sscanf(argv[i], "%15[^/]/%31s", super, type); | |
103 | dst = mimeType(mime, super, type); | |
104 | ||
bd7854cb | 105 | filters = mimeFilter(mime, src, dst, &cost); |
ef416fc2 | 106 | |
fa73b229 | 107 | if (!filters) |
ef416fc2 | 108 | { |
109 | printf("No filters to convert from %s/%s to %s.\n", src->super, | |
110 | src->type, argv[i]); | |
111 | } | |
112 | else | |
113 | { | |
fa73b229 | 114 | printf("Filter cost = %d\n", cost); |
ef416fc2 | 115 | |
fa73b229 | 116 | filter = (mime_filter_t *)cupsArrayFirst(filters); |
117 | fputs(filter->filter, stdout); | |
118 | ||
119 | for (filter = (mime_filter_t *)cupsArrayNext(filters); | |
120 | filter; | |
121 | filter = (mime_filter_t *)cupsArrayNext(filters)) | |
122 | printf(" | %s", filter->filter); | |
123 | ||
124 | putchar('\n'); | |
125 | ||
126 | cupsArrayDelete(filters); | |
ef416fc2 | 127 | } |
128 | } | |
129 | ||
130 | if (!mime) | |
131 | mime = mimeLoad("../conf", filter_path); | |
132 | ||
fa73b229 | 133 | if (!src) |
ef416fc2 | 134 | { |
135 | puts("MIME database types:"); | |
fa73b229 | 136 | for (src = mimeFirstType(mime); src; src = mimeNextType(mime)) |
ef416fc2 | 137 | { |
c168a833 | 138 | printf("\t%s/%s (%d):\n", src->super, src->type, src->priority); |
fa73b229 | 139 | print_rules(src->rules); |
ef416fc2 | 140 | puts(""); |
141 | } | |
142 | ||
143 | puts(""); | |
144 | ||
145 | puts("MIME database filters:"); | |
fa73b229 | 146 | for (filter = mimeFirstFilter(mime); filter; filter = mimeNextFilter(mime)) |
ef416fc2 | 147 | printf("\t%s/%s to %s/%s: %s (%d)\n", |
fa73b229 | 148 | filter->src->super, filter->src->type, |
149 | filter->dst->super, filter->dst->type, | |
150 | filter->filter, filter->cost); | |
4400e98d | 151 | |
f301802f | 152 | type_dir(mime, "../doc"); |
153 | type_dir(mime, "../man"); | |
ef416fc2 | 154 | } |
155 | ||
156 | return (0); | |
157 | } | |
158 | ||
159 | ||
160 | /* | |
161 | * 'print_rules()' - Print the rules for a file type... | |
162 | */ | |
163 | ||
164 | static void | |
165 | print_rules(mime_magic_t *rules) /* I - Rules to print */ | |
166 | { | |
167 | int i; /* Looping var */ | |
168 | static char indent[255] = "\t"; /* Indentation for rules */ | |
169 | ||
170 | ||
171 | if (rules == NULL) | |
172 | return; | |
173 | ||
174 | while (rules != NULL) | |
175 | { | |
176 | printf("%s[%p] ", indent, rules); | |
177 | ||
178 | if (rules->invert) | |
179 | printf("NOT "); | |
180 | ||
181 | switch (rules->op) | |
182 | { | |
183 | case MIME_MAGIC_MATCH : | |
184 | printf("match(%s)", rules->value.matchv); | |
185 | break; | |
186 | case MIME_MAGIC_LOCALE : | |
187 | printf("locale(%s)", rules->value.localev); | |
188 | break; | |
189 | case MIME_MAGIC_ASCII : | |
190 | printf("ascii(%d,%d)", rules->offset, rules->length); | |
191 | break; | |
192 | case MIME_MAGIC_PRINTABLE : | |
193 | printf("printable(%d,%d)", rules->offset, rules->length); | |
194 | break; | |
195 | case MIME_MAGIC_STRING : | |
196 | printf("string(%d,", rules->offset); | |
197 | for (i = 0; i < rules->length; i ++) | |
198 | if (rules->value.stringv[i] < ' ' || | |
199 | rules->value.stringv[i] > 126) | |
200 | printf("<%02X>", rules->value.stringv[i]); | |
201 | else | |
202 | putchar(rules->value.stringv[i]); | |
203 | putchar(')'); | |
204 | break; | |
205 | case MIME_MAGIC_CHAR : | |
206 | printf("char(%d,%d)", rules->offset, rules->value.charv); | |
207 | break; | |
208 | case MIME_MAGIC_SHORT : | |
209 | printf("short(%d,%d)", rules->offset, rules->value.shortv); | |
210 | break; | |
211 | case MIME_MAGIC_INT : | |
212 | printf("int(%d,%d)", rules->offset, rules->value.intv); | |
213 | break; | |
214 | case MIME_MAGIC_CONTAINS : | |
215 | printf("contains(%d,%d,", rules->offset, rules->region); | |
216 | for (i = 0; i < rules->length; i ++) | |
217 | if (rules->value.stringv[i] < ' ' || | |
218 | rules->value.stringv[i] > 126) | |
219 | printf("<%02X>", rules->value.stringv[i]); | |
220 | else | |
221 | putchar(rules->value.stringv[i]); | |
222 | putchar(')'); | |
223 | break; | |
224 | default : | |
225 | break; | |
226 | } | |
227 | ||
228 | if (rules->child != NULL) | |
229 | { | |
230 | if (rules->op == MIME_MAGIC_OR) | |
231 | puts("OR ("); | |
232 | else | |
233 | puts("AND ("); | |
234 | ||
235 | strcat(indent, "\t"); | |
236 | print_rules(rules->child); | |
237 | indent[strlen(indent) - 1] = '\0'; | |
238 | printf("%s)\n", indent); | |
239 | } | |
240 | else | |
241 | putchar('\n'); | |
242 | ||
243 | rules = rules->next; | |
244 | } | |
245 | } | |
246 | ||
247 | ||
248 | /* | |
4400e98d | 249 | * 'type_dir()' - Show the MIME types for a given directory. |
250 | */ | |
251 | ||
252 | static void | |
253 | type_dir(mime_t *mime, /* I - MIME database */ | |
254 | const char *dirname) /* I - Directory */ | |
255 | { | |
256 | cups_dir_t *dir; /* Directory */ | |
257 | cups_dentry_t *dent; /* Directory entry */ | |
258 | char filename[1024]; /* File to type */ | |
259 | mime_type_t *filetype; /* File type */ | |
260 | int compression; /* Compressed file? */ | |
261 | mime_type_t *pstype; /* application/vnd.cups-postscript */ | |
262 | cups_array_t *filters; /* Filters to pstype */ | |
263 | mime_filter_t *filter; /* Current filter */ | |
264 | int cost; /* Filter cost */ | |
265 | ||
266 | ||
267 | dir = cupsDirOpen(dirname); | |
268 | if (!dir) | |
269 | return; | |
270 | ||
271 | pstype = mimeType(mime, "application", "vnd.cups-postscript"); | |
272 | ||
273 | while ((dent = cupsDirRead(dir)) != NULL) | |
274 | { | |
f301802f | 275 | if (dent->filename[0] == '.') |
276 | continue; | |
277 | ||
4400e98d | 278 | snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->filename); |
279 | ||
280 | if (S_ISDIR(dent->fileinfo.st_mode)) | |
281 | type_dir(mime, filename); | |
282 | ||
283 | if (!S_ISREG(dent->fileinfo.st_mode)) | |
284 | continue; | |
285 | ||
286 | filetype = mimeFileType(mime, filename, NULL, &compression); | |
287 | ||
288 | if (filetype) | |
289 | { | |
290 | printf("%s: %s/%s%s\n", filename, filetype->super, filetype->type, | |
291 | compression ? " (compressed)" : ""); | |
292 | ||
bd7854cb | 293 | filters = mimeFilter(mime, filetype, pstype, &cost); |
4400e98d | 294 | |
295 | if (!filters) | |
296 | puts(" No filters to convert application/vnd.cups-postscript."); | |
297 | else | |
298 | { | |
299 | printf(" Filter cost = %d\n", cost); | |
300 | ||
301 | filter = (mime_filter_t *)cupsArrayFirst(filters); | |
302 | printf(" %s", filter->filter); | |
303 | ||
304 | for (filter = (mime_filter_t *)cupsArrayNext(filters); | |
305 | filter; | |
306 | filter = (mime_filter_t *)cupsArrayNext(filters)) | |
307 | printf(" | %s", filter->filter); | |
308 | ||
309 | putchar('\n'); | |
310 | ||
311 | cupsArrayDelete(filters); | |
312 | } | |
313 | } | |
314 | else | |
315 | printf("%s: unknown%s\n", filename, compression ? " (compressed)" : ""); | |
316 | } | |
317 | ||
318 | cupsDirClose(dir); | |
319 | } | |
320 | ||
321 | ||
322 | /* | |
75bd9771 | 323 | * End of "$Id: testmime.c 7670 2008-06-17 22:42:08Z mike $". |
ef416fc2 | 324 | */ |