]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/testmime.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / testmime.c
CommitLineData
ef416fc2 1/*
f7faf1f5 2 * "$Id: testmime.c 5606 2006-05-30 19:40:34Z 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 46static void print_rules(mime_magic_t *rules);
47static void type_dir(mime_t *mime, const char *dirname);
ef416fc2 48
49
50/*
51 * 'main()' - Main entry for the test program.
52 */
53
54int /* O - Exit status */
55main(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;
ed486911 74 filter_path = "../filter:../pdftops:" CUPS_SERVERBIN "/filter";
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)" : "");
f301802f 101 else if ((src = mimeType(mime, "application", "octet-stream")) != NULL)
102 printf("%s: application/octet-stream\n", argv[i]);
ef416fc2 103 else
104 {
105 printf("%s: unknown\n", argv[i]);
106 if (mime)
107 mimeDelete(mime);
108 return (1);
109 }
110 }
111 else
112 {
113 sscanf(argv[i], "%15[^/]/%31s", super, type);
114 dst = mimeType(mime, super, type);
115
bd7854cb 116 filters = mimeFilter(mime, src, dst, &cost);
ef416fc2 117
fa73b229 118 if (!filters)
ef416fc2 119 {
120 printf("No filters to convert from %s/%s to %s.\n", src->super,
121 src->type, argv[i]);
122 }
123 else
124 {
fa73b229 125 printf("Filter cost = %d\n", cost);
ef416fc2 126
fa73b229 127 filter = (mime_filter_t *)cupsArrayFirst(filters);
128 fputs(filter->filter, stdout);
129
130 for (filter = (mime_filter_t *)cupsArrayNext(filters);
131 filter;
132 filter = (mime_filter_t *)cupsArrayNext(filters))
133 printf(" | %s", filter->filter);
134
135 putchar('\n');
136
137 cupsArrayDelete(filters);
ef416fc2 138 }
139 }
140
141 if (!mime)
142 mime = mimeLoad("../conf", filter_path);
143
fa73b229 144 if (!src)
ef416fc2 145 {
146 puts("MIME database types:");
fa73b229 147 for (src = mimeFirstType(mime); src; src = mimeNextType(mime))
ef416fc2 148 {
fa73b229 149 printf("\t%s/%s:\n", src->super, src->type);
150 print_rules(src->rules);
ef416fc2 151 puts("");
152 }
153
154 puts("");
155
156 puts("MIME database filters:");
fa73b229 157 for (filter = mimeFirstFilter(mime); filter; filter = mimeNextFilter(mime))
ef416fc2 158 printf("\t%s/%s to %s/%s: %s (%d)\n",
fa73b229 159 filter->src->super, filter->src->type,
160 filter->dst->super, filter->dst->type,
161 filter->filter, filter->cost);
4400e98d 162
f301802f 163 type_dir(mime, "../doc");
164 type_dir(mime, "../man");
ef416fc2 165 }
166
167 return (0);
168}
169
170
171/*
172 * 'print_rules()' - Print the rules for a file type...
173 */
174
175static void
176print_rules(mime_magic_t *rules) /* I - Rules to print */
177{
178 int i; /* Looping var */
179 static char indent[255] = "\t"; /* Indentation for rules */
180
181
182 if (rules == NULL)
183 return;
184
185 while (rules != NULL)
186 {
187 printf("%s[%p] ", indent, rules);
188
189 if (rules->invert)
190 printf("NOT ");
191
192 switch (rules->op)
193 {
194 case MIME_MAGIC_MATCH :
195 printf("match(%s)", rules->value.matchv);
196 break;
197 case MIME_MAGIC_LOCALE :
198 printf("locale(%s)", rules->value.localev);
199 break;
200 case MIME_MAGIC_ASCII :
201 printf("ascii(%d,%d)", rules->offset, rules->length);
202 break;
203 case MIME_MAGIC_PRINTABLE :
204 printf("printable(%d,%d)", rules->offset, rules->length);
205 break;
206 case MIME_MAGIC_STRING :
207 printf("string(%d,", rules->offset);
208 for (i = 0; i < rules->length; i ++)
209 if (rules->value.stringv[i] < ' ' ||
210 rules->value.stringv[i] > 126)
211 printf("<%02X>", rules->value.stringv[i]);
212 else
213 putchar(rules->value.stringv[i]);
214 putchar(')');
215 break;
216 case MIME_MAGIC_CHAR :
217 printf("char(%d,%d)", rules->offset, rules->value.charv);
218 break;
219 case MIME_MAGIC_SHORT :
220 printf("short(%d,%d)", rules->offset, rules->value.shortv);
221 break;
222 case MIME_MAGIC_INT :
223 printf("int(%d,%d)", rules->offset, rules->value.intv);
224 break;
225 case MIME_MAGIC_CONTAINS :
226 printf("contains(%d,%d,", rules->offset, rules->region);
227 for (i = 0; i < rules->length; i ++)
228 if (rules->value.stringv[i] < ' ' ||
229 rules->value.stringv[i] > 126)
230 printf("<%02X>", rules->value.stringv[i]);
231 else
232 putchar(rules->value.stringv[i]);
233 putchar(')');
234 break;
235 default :
236 break;
237 }
238
239 if (rules->child != NULL)
240 {
241 if (rules->op == MIME_MAGIC_OR)
242 puts("OR (");
243 else
244 puts("AND (");
245
246 strcat(indent, "\t");
247 print_rules(rules->child);
248 indent[strlen(indent) - 1] = '\0';
249 printf("%s)\n", indent);
250 }
251 else
252 putchar('\n');
253
254 rules = rules->next;
255 }
256}
257
258
259/*
4400e98d 260 * 'type_dir()' - Show the MIME types for a given directory.
261 */
262
263static void
264type_dir(mime_t *mime, /* I - MIME database */
265 const char *dirname) /* I - Directory */
266{
267 cups_dir_t *dir; /* Directory */
268 cups_dentry_t *dent; /* Directory entry */
269 char filename[1024]; /* File to type */
270 mime_type_t *filetype; /* File type */
271 int compression; /* Compressed file? */
272 mime_type_t *pstype; /* application/vnd.cups-postscript */
273 cups_array_t *filters; /* Filters to pstype */
274 mime_filter_t *filter; /* Current filter */
275 int cost; /* Filter cost */
276
277
278 dir = cupsDirOpen(dirname);
279 if (!dir)
280 return;
281
282 pstype = mimeType(mime, "application", "vnd.cups-postscript");
283
284 while ((dent = cupsDirRead(dir)) != NULL)
285 {
f301802f 286 if (dent->filename[0] == '.')
287 continue;
288
4400e98d 289 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->filename);
290
291 if (S_ISDIR(dent->fileinfo.st_mode))
292 type_dir(mime, filename);
293
294 if (!S_ISREG(dent->fileinfo.st_mode))
295 continue;
296
297 filetype = mimeFileType(mime, filename, NULL, &compression);
298
299 if (filetype)
300 {
301 printf("%s: %s/%s%s\n", filename, filetype->super, filetype->type,
302 compression ? " (compressed)" : "");
303
bd7854cb 304 filters = mimeFilter(mime, filetype, pstype, &cost);
4400e98d 305
306 if (!filters)
307 puts(" No filters to convert application/vnd.cups-postscript.");
308 else
309 {
310 printf(" Filter cost = %d\n", cost);
311
312 filter = (mime_filter_t *)cupsArrayFirst(filters);
313 printf(" %s", filter->filter);
314
315 for (filter = (mime_filter_t *)cupsArrayNext(filters);
316 filter;
317 filter = (mime_filter_t *)cupsArrayNext(filters))
318 printf(" | %s", filter->filter);
319
320 putchar('\n');
321
322 cupsArrayDelete(filters);
323 }
324 }
325 else
326 printf("%s: unknown%s\n", filename, compression ? " (compressed)" : "");
327 }
328
329 cupsDirClose(dir);
330}
331
332
333/*
f7faf1f5 334 * End of "$Id: testmime.c 5606 2006-05-30 19:40:34Z mike $".
ef416fc2 335 */