]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testmime.c
2 * "$Id: testmime.c 5061 2006-02-03 16:32:18Z mike $"
4 * MIME test program for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
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.
32 * Include necessary headers...
37 #include <cups/string.h>
46 static void print_rules(mime_magic_t
*rules
);
47 static void type_dir(mime_t
*mime
, const char *dirname
);
51 * 'main()' - Main entry for the test program.
54 int /* O - Exit status */
55 main(int argc
, /* I - Number of command-line args */
56 char *argv
[]) /* I - Command-line arguments */
58 int i
; /* Looping vars */
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 */
63 int cost
; /* Cost of filters */
64 mime_t
*mime
; /* MIME database */
65 mime_type_t
*src
, /* Source type */
66 *dst
; /* Destination type */
67 cups_array_t
*filters
; /* Filters for the file */
68 mime_filter_t
*filter
; /* Current filter */
74 filter_path
= "../filter:../pdftops";
76 for (i
= 1; i
< argc
; i
++)
77 if (!strcmp(argv
[i
], "-d"))
82 mime
= mimeLoad(argv
[i
], filter_path
);
84 else if (!strcmp(argv
[i
], "-f"))
89 filter_path
= argv
[i
];
94 mime
= mimeLoad("../conf", filter_path
);
96 src
= mimeFileType(mime
, argv
[i
], NULL
, &compression
);
99 printf("%s: %s/%s%s\n", argv
[i
], src
->super
, src
->type
,
100 compression
? " (gzipped)" : "");
103 printf("%s: unknown\n", argv
[i
]);
111 sscanf(argv
[i
], "%15[^/]/%31s", super
, type
);
112 dst
= mimeType(mime
, super
, type
);
114 filters
= mimeFilter(mime
, src
, dst
, &cost
);
118 printf("No filters to convert from %s/%s to %s.\n", src
->super
,
123 printf("Filter cost = %d\n", cost
);
125 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
126 fputs(filter
->filter
, stdout
);
128 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
130 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
131 printf(" | %s", filter
->filter
);
135 cupsArrayDelete(filters
);
140 mime
= mimeLoad("../conf", filter_path
);
144 puts("MIME database types:");
145 for (src
= mimeFirstType(mime
); src
; src
= mimeNextType(mime
))
147 printf("\t%s/%s:\n", src
->super
, src
->type
);
148 print_rules(src
->rules
);
154 puts("MIME database filters:");
155 for (filter
= mimeFirstFilter(mime
); filter
; filter
= mimeNextFilter(mime
))
156 printf("\t%s/%s to %s/%s: %s (%d)\n",
157 filter
->src
->super
, filter
->src
->type
,
158 filter
->dst
->super
, filter
->dst
->type
,
159 filter
->filter
, filter
->cost
);
161 type_dir(mime
, "..");
169 * 'print_rules()' - Print the rules for a file type...
173 print_rules(mime_magic_t
*rules
) /* I - Rules to print */
175 int i
; /* Looping var */
176 static char indent
[255] = "\t"; /* Indentation for rules */
182 while (rules
!= NULL
)
184 printf("%s[%p] ", indent
, rules
);
191 case MIME_MAGIC_MATCH
:
192 printf("match(%s)", rules
->value
.matchv
);
194 case MIME_MAGIC_LOCALE
:
195 printf("locale(%s)", rules
->value
.localev
);
197 case MIME_MAGIC_ASCII
:
198 printf("ascii(%d,%d)", rules
->offset
, rules
->length
);
200 case MIME_MAGIC_PRINTABLE
:
201 printf("printable(%d,%d)", rules
->offset
, rules
->length
);
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
]);
210 putchar(rules
->value
.stringv
[i
]);
213 case MIME_MAGIC_CHAR
:
214 printf("char(%d,%d)", rules
->offset
, rules
->value
.charv
);
216 case MIME_MAGIC_SHORT
:
217 printf("short(%d,%d)", rules
->offset
, rules
->value
.shortv
);
219 case MIME_MAGIC_INT
:
220 printf("int(%d,%d)", rules
->offset
, rules
->value
.intv
);
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
]);
229 putchar(rules
->value
.stringv
[i
]);
236 if (rules
->child
!= NULL
)
238 if (rules
->op
== MIME_MAGIC_OR
)
243 strcat(indent
, "\t");
244 print_rules(rules
->child
);
245 indent
[strlen(indent
) - 1] = '\0';
246 printf("%s)\n", indent
);
257 * 'type_dir()' - Show the MIME types for a given directory.
261 type_dir(mime_t
*mime
, /* I - MIME database */
262 const char *dirname
) /* I - Directory */
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 */
275 dir
= cupsDirOpen(dirname
);
279 pstype
= mimeType(mime
, "application", "vnd.cups-postscript");
281 while ((dent
= cupsDirRead(dir
)) != NULL
)
283 snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, dent
->filename
);
285 if (S_ISDIR(dent
->fileinfo
.st_mode
))
286 type_dir(mime
, filename
);
288 if (!S_ISREG(dent
->fileinfo
.st_mode
))
291 filetype
= mimeFileType(mime
, filename
, NULL
, &compression
);
295 printf("%s: %s/%s%s\n", filename
, filetype
->super
, filetype
->type
,
296 compression
? " (compressed)" : "");
298 filters
= mimeFilter(mime
, filetype
, pstype
, &cost
);
301 puts(" No filters to convert application/vnd.cups-postscript.");
304 printf(" Filter cost = %d\n", cost
);
306 filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
307 printf(" %s", filter
->filter
);
309 for (filter
= (mime_filter_t
*)cupsArrayNext(filters
);
311 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
312 printf(" | %s", filter
->filter
);
316 cupsArrayDelete(filters
);
320 printf("%s: unknown%s\n", filename
, compression
? " (compressed)" : "");
328 * End of "$Id: testmime.c 5061 2006-02-03 16:32:18Z mike $".