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