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